How to change the permissions of a executable

170 views
Skip to first unread message

Wink Saville

unread,
Sep 3, 2016, 12:58:27 PM9/3/16
to The Meson Build System
So as not to have to run an executable with sudo I need the owner
to be root and SUID so I've created a script:

$ cat tools/make.root.suid.sh 
#!/usr/bin/env bash
# Do two things:
#  1) Change user and group for the file to root
#  2) SUID the file (i.e. have it run with the permissions of the owner.)

# Parameters:
#   $1 is the file to execute
echo "param1=$1"
param1=$1
shift
sudo chown root:root $param1
sudo chmod u+s $param1


And then in my meson.build I tried this:

make_suid = find_program('@0@/tools/make.root.suid.sh'.format(meson.source_root()))
gen = generator(make_suid,
      output : '@PLAINNAME@',
      arguments : 'file_name',
      dependencies : test_posix_ac_inet_link)
generated = gen.process('test_posix_ac_inet_link')
run_target('run-test-posix-ac_inet_link', generated)


But I get an error:

$ ninja run-test-posix-ac_inet_link 
[0/1] Regenerating build files
The Meson build system
Version: 0.32.0
Source dir: /home/wink/prgs/sadie
Build dir: /home/wink/prgs/sadie/build-Posix
Build type: cross build
Host machine cpu family: x86
Host machine cpu: x86_64
Target machine cpu family: x86
Target machine cpu: x86_64
Build machine cpu family: x86_64
Build machine cpu: x86_64
Project name: sadie
Native c compiler: cc (gcc 6.1.1)
Cross c compiler: clang (clang 3.8.1)
Program /home/wink/prgs/sadie/tools/make.root.suid.sh found: YES (/home/wink/prgs/sadie/tools/make.root.suid.sh)

Meson encountered an error in file platform/Posix/components/ac_inet_link/tests/meson.build, line 41, column 4:
Invalid argument to run_target.
FAILED: build.ninja 
'/usr/bin/python' '/home/wink/opt/bin/meson.py' --internal regenerate '/home/wink/prgs/sadie' '/home/wink/prgs/sadie/build-Posix' --backend ninja
ninja: error: rebuilding 'build.ninja': subcommand failed

Thanks in advance!

Jussi Pakkanen

unread,
Sep 3, 2016, 5:42:06 PM9/3/16
to Wink Saville, The Meson Build System
On Sat, Sep 3, 2016 at 7:58 PM, Wink Saville <wi...@saville.com> wrote:

> make_suid =
> find_program('@0@/tools/make.root.suid.sh'.format(meson.source_root()))
> gen = generator(make_suid,
> output : '@PLAINNAME@',
> arguments : 'file_name',
> dependencies : test_posix_ac_inet_link)
> generated = gen.process('test_posix_ac_inet_link')
> run_target('run-test-posix-ac_inet_link', generated)
>
> But I get an error:

You should probably use a custom_target instead of a generator. The
difference is subtle but think of files generated by a generator like
.o files from a C compiler. They do not do anything on their own, and
only come into existance when used in a top level target (i.e. are
linked). For creating one single output file a custom_target is
better.

An alternative is to have your runner script be set to run as root
with suid parameters and have it just invoke the result binary. This
is safer as you don't alter permission bits during build which is
always a bit dangerous.

Wink Saville

unread,
Sep 4, 2016, 9:42:33 PM9/4/16
to The Meson Build System, wi...@saville.com
So I tried the following meson.build which has a custom_target :

incDirs = runtimeIncDirs + componentIncDirs
deps = [ component_dep, libruntime_dep ]

if Platform == 'Posix'
  srcFiles = firstSrcFiles + ['srcs/test.c']

  # Create test_posix_ac_inet_link executable
  test_posix_ac_inet_link = executable( 'test_posix_ac_inet_link', srcFiles,
    include_directories : incDirs,
    link_args : linkArgs,
    c_args : compilerArgs,
    dependencies : deps,
  )

  if true
    in_file = '@0@/test_posix_ac_inet_link'.format(meson.current_build_dir())
    out_file = '@0@/test_posix_ac_inet_link_x'.format(meson.current_build_dir())
    make_root_suid_file =  '@0@/tools/make_root_suid.sh'.format(meson.source_root())

    make_root_suid = find_program(make_root_suid_file)

    test_posix_ac_inet_link_x = custom_target('test_posix_ac_inet_link_x',
      input : [in_file],
      output : ['test_posix_ac_inet_link_x'],
      command : [make_root_suid, '@INPUT@', '@OUTPUT@'],
      depends : [test_posix_ac_inet_link])

    run_target('run-test-posix-ac_inet_link_x', test_posix_ac_inet_link_x)
  else
    run_target('run-test-posix-ac_inet_link', '@0@/tools/sudo.runner.sh'.format(meson.source_root()),
      test_posix_ac_inet_link)
  endif
endif

But I get the following error from meson:

$ ninja
[0/1] Regenerating build files
The Meson build system
Version: 0.32.0
Source dir: /home/wink/prgs/sadie
Build dir: /home/wink/prgs/sadie/build-Posix
Build type: cross build
Host machine cpu family: x86
Host machine cpu: x86_64
Target machine cpu family: x86
Target machine cpu: x86_64
Build machine cpu family: x86_64
Build machine cpu: x86_64
Project name: sadie
Native c compiler: cc (gcc 6.1.1)
Cross c compiler: clang (clang 3.8.1)
Program /home/wink/prgs/sadie/tools/make_root_suid.sh found: YES (/home/wink/prgs/sadie/tools/make_root_suid.sh)
Build targets in project: 61
Traceback (most recent call last):
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 265, in run
    app.generate()
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/mesonmain.py", line 160, in generate
    g.generate(intr)
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 178, in generate
    [self.generate_target(t, outfile) for t in self.build.get_targets().values()]
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 178, in <listcomp>
    [self.generate_target(t, outfile) for t in self.build.get_targets().values()]
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 225, in generate_target
    self.generate_run_target(target, outfile)
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 425, in generate_run_target
    elem.write(outfile)
  File "/home/wink/opt/lib/python3.5/site-packages/mesonbuild/backend/ninjabackend.py", line 112, in write
    i = i.replace('\\', '\\\\')
AttributeError: 'CustomTarget' object has no attribute 'replace'
FAILED: build.ninja 
'/usr/bin/python' '/home/wink/opt/bin/meson.py' --internal regenerate '/home/wink/prgs/sadie' '/home/wink/prgs/sadie/build-Posix' --backend ninja
ninja: error: rebuilding 'build.ninja': subcommand failed



Setting the suid bit for scripts didn't work, apparently scripts with suid are no longer supported.
Also, when I change the "if true" to "if false" and use the sudo.runner.sh that works.

What stupid thing am I doing???

Jussi Pakkanen

unread,
Sep 7, 2016, 4:21:05 PM9/7/16
to Wink Saville, The Meson Build System
On Mon, Sep 5, 2016 at 4:42 AM, Wink Saville <wi...@saville.com> wrote:

> if true
> in_file =
> '@0@/test_posix_ac_inet_link'.format(meson.current_build_dir())
> out_file =
> '@0@/test_posix_ac_inet_link_x'.format(meson.current_build_dir())
> make_root_suid_file =
> '@0@/tools/make_root_suid.sh'.format(meson.source_root())

You should not do this. You can put the outputs of targets in command
lines and Meson will automatically make things work for you. As an
example this is not portable because a target file name is 'mytarget'
on Unix but 'mytarget.exe' on Windows. For files in the source
directory you should use make_root_suid_file =
files('make_root_suid.sh') and then put that in the command lines.

> test_posix_ac_inet_link_x = custom_target('test_posix_ac_inet_link_x',
> input : [in_file],

So here in_file should just be 'test_posix_ac_inet_link'.

> run_target('run-test-posix-ac_inet_link',
> '@0@/tools/sudo.runner.sh'.format(meson.source_root()),

And here instead of self-formatted string you should have the return
value of files() above.


> But I get the following error from meson:
>
> What stupid thing am I doing???

Nothing, this was a bug. Please test if this MR fixes it for you:

https://github.com/mesonbuild/meson/pull/768

Wink Saville

unread,
Sep 7, 2016, 9:19:46 PM9/7/16
to Jussi Pakkanen, The Meson Build System
I've tried to clean it up:

incDirs = runtimeIncDirs + componentIncDirs
deps = [ component_dep, libruntime_dep ]

if Platform == 'Posix'
  srcFiles = firstSrcFiles + ['srcs/test.c']

  # Create test_posix_ac_inet_link_regular executable
  test_posix_ac_inet_link_regular = executable( 'test_posix_ac_inet_link_regular', srcFiles,
    include_directories : incDirs,
    link_args : linkArgs,
    c_args : compilerArgs,
    dependencies : deps,
  )

  if true
    make_root_suid_file =  '@0@/tools/make_root_suid.sh'.format(meson.source_root())
    make_root_suid = find_program(make_root_suid_file)

    test_posix_ac_inet_link = custom_target('test_posix_ac_inet_link',
      input : ['test_posix_ac_inet_link_regular'],
      output : ['test_posix_ac_inet_link'],
      command : ['make_root_suid', '@INPUT@', '@OUTPUT@'],
      depends : [test_posix_ac_inet_link_regular])

    run_target('run-test-posix-ac_inet_link', test_posix_ac_inet_link)
  else
    run_target('run-test-posix-ac_inet_link_regular', '@0@/tools/sudo.runner.sh'.format(meson.source_root()),
      test_posix_ac_inet_link_regular)
  endif
endif


I getting a DEPRECATION error about positional parameters on run_target
using 0.35-dev, but the documentation hasn't been updated.

More problematic is I don't have the dependencies and probably
something else :)


$ rm -rf * ; ~/foss/meson/meson.py -D Platform=Posix --cross-file ../cross-file-Posix-clang --buildtype=plain ..
The Meson build system
Version: 0.35.0.dev1
Source dir: /home/wink/prgs/sadie
Build dir: /home/wink/prgs/sadie/build-Posix
Build type: cross build
Host machine cpu family: x86
Host machine cpu: x86_64
Target machine cpu family: x86
Target machine cpu: x86_64
Build machine cpu family: x86_64
Build machine cpu: x86_64
Project name: sadie
Native c compiler: cc (gcc 6.1.1)
Cross c compiler: clang (clang 3.8.1)
DEPRECATION positional version of run_target is deprecated, use the keyword version instead.
Program /home/wink/prgs/sadie/tools/make_root_suid.sh found: YES (/home/wink/prgs/sadie/tools/make_root_suid.sh)
Build targets in project: 61
wink@wink-desktop:~/prgs/sadie/build-Posix
$ ninja
ninja: error: '../platform/Posix/components/ac_inet_link/tests/test_posix_ac_inet_link_regular', needed by 'platform/Posix/components/ac_inet_link/tests/test_posix_ac_inet_link', missing and no known rule to make it


Wink Saville

unread,
Sep 9, 2016, 1:33:26 PM9/9/16
to The Meson Build System, jpak...@gmail.com
With the updates to 0.35.0-dev in #770 and additional cleanup I've done things
are look'n pretty good now.
On Wed, Sep 7, 2016 at 1:21 PM Jussi Pakkanen  wrote:
Reply all
Reply to author
Forward
0 new messages