The setuid
permission bit tells Linux to run a program with the effective user id of the owner instead of the executor:
> cat setuid-test.c
#include <stdio.h>
#include <unistd.h>
int main(int argc, char** argv) {
printf("%d", geteuid());
return 0;
}
> gcc -o setuid-test setuid-test.c
> ./setuid-test
1000
> sudo chown nobody ./setuid-test; sudo chmod +s ./setuid-test
> ./setuid-test
65534
However, this only applies to executables; shell scripts ignore the setuid bit:
> cat setuid-test2
#!/bin/bash
id -u
> ./setuid-test2
1000
> sudo chown nobody ./setuid-test2; sudo chmod +s ./setuid-test2
> ./setuid-test2
1000
Wikipedia says:
Due to the increased likelihood of security flaws, many operating systems ignore the setuid attribute when applied to executable shell scripts.
Assuming I'm willing to accept those risks, is there any way to tell Linux to treat the setuid bit the same on shell scripts as it does on executables?
If not, is there a common workaround for this problem? My current solution is to add a sudoers
entry to allow ALL
to run a given script as the user I want it run as, with NOPASSWD
to avoid the password prompt. The main downsides to that is the need for a sudoers
entry every time I want to do this, and the need for the caller to sudo some-script
instead of just some-script
Best Answer
Linux ignores the setuid¹ bit on all interpreted executables (i.e. executables starting with a
#!
line). The comp.unix.questions FAQ explains the security problems with setuid shell scripts. These problems are of two kinds: shebang-related and shell-related; I go into more details below.If you don't care about security and want to allow setuid scripts, under Linux, you'll need to patch the kernel. As of 3.x kernels, I think you need to add a call to
install_exec_creds
in theload_script
function, before the call toopen_exec
, but I haven't tested.Setuid shebang
There is a race condition inherent to the way shebang (
#!
) is typically implemented:#!
.argv[1]
), and executes the interpreter.If setuid scripts are allowed with this implementation, an attacker can invoke an arbitrary script by creating a symbolic link to an existing setuid script, executing it, and arranging to change the link after the kernel has performed step 1 and before the interpreter gets around to opening its first argument. For this reason, most unices ignore the setuid bit when they detect a shebang.
One way to secure this implementation would be for the kernel to lock the script file until the interpreter has opened it (note that this must prevent not only unlinking or overwriting the file, but also renaming any directory in the path). But unix systems tend to shy away from mandatory locks, and symbolic links would make a correct lock feature especially difficult and invasive. I don't think anyone does it this way.
A few unix systems (mainly OpenBSD, NetBSD and Mac OS X, all of which require a kernel setting to be enabled) implement secure setuid shebang using an additional feature: the path
/dev/fd/N
refers to the file already opened on file descriptor N (so opening/dev/fd/N
is roughly equivalent todup(N)
). Many unix systems (including Linux) have/dev/fd
but not setuid scripts.#!
. Let's say the file descriptor for the executable is 3./dev/fd/3
the argument list (asargv[1]
), and executes the interpreter.Sven Mascheck's shebang page has a lot of information on shebang across unices, including setuid support.
Setuid interpreters
Let's assume you've managed to make your program run as root, either because your OS supports setuid shebang or because you've used a native binary wrapper (such as
sudo
). Have you opened a security hole? Maybe. The issue here is not about interpreted vs compiled programs. The issue is whether your runtime system behaves safely if executed with privileges.Any dynamically linked native binary executable is in a way interpreted by the dynamic loader (e.g.
/lib/ld.so
), which loads the dynamic libraries required by the program. On many unices, you can configure the search path for dynamic libraries through the environment (LD_LIBRARY_PATH
is a common name for the environment variable), and even load additional libraries into all executed binaries (LD_PRELOAD
). The invoker of the program can execute arbitrary code in that program's context by placing a specially-craftedlibc.so
in$LD_LIBRARY_PATH
(amongst other tactics). All sane systems ignore theLD_*
variables in setuid executables.In shells such as sh, csh and derivatives, environment variables automatically become shell parameters. Through parameters such as
PATH
,IFS
, and many more, the invoker of the script has many opportunities to execute arbitrary code in the shell scripts's context. Some shells set these variables to sane defaults if they detect that the script has been invoked with privileges, but I don't know that there is any particular implementation that I would trust.Most runtime environments (whether native, bytecode or interpreted) have similar features. Few take special precautions in setuid executables, though the ones that run native code often don't do anything fancier than dynamic linking (which does take precautions).
Perl is a notable exception. It explicitly supports setuid scripts in a secure way. In fact, your script can run setuid even if your OS ignored the setuid bit on scripts. This is because perl ships with a setuid root helper that performs the necessary checks and reinvokes the interpreter on the desired scripts with the desired privileges. This is explained in the perlsec manual. It used to be that setuid perl scripts needed
#!/usr/bin/suidperl -wT
instead of#!/usr/bin/perl -wT
, but on most modern systems,#!/usr/bin/perl -wT
is sufficient.Note that using a native binary wrapper does nothing in itself to prevent these problems. In fact, it can make the situation worse, because it might prevent your runtime environment from detecting that it is invoked with privileges and bypassing its runtime configurability.
A native binary wrapper can make a shell script safe if the wrapper sanitizes the environment. The script must take care not to make too many assumptions (e.g. about the current directory) but this goes. You can use sudo for this provided that it's set up to sanitize the environment. Blacklisting variables is error-prone, so always whitelist. With sudo, make sure that the
env_reset
option is turned on, thatsetenv
is off, and thatenv_file
andenv_keep
only contain innocuous variables.TL,DR:
env_reset
option).¹ This discussion applies equally if you substitute “setgid” for “setuid”; they are both ignored by the Linux kernel on scripts