In Linux there are so many permission mechanisms, depending on exactly what you want to do, it dazzles the mind. There is suid, dbus policies, polkit, Linux capabilities, files attributes, PAM modules, SELinux and the list goes on. It does not surprise then, that choosing the correct approach can become paralyzing or give anxiety inducing.
In the end though, most of us will just use sudo and that is fine for fast scripts or manual interventions, but what about using it inside an unprevileged program? Better, while we are being pragmatic why not use good old system C stdlib function and call the application with it and sudo. At first look it might seems a bad idea as per manual:
Do not use system() from a privileged program (a set-user-ID or
set-group-ID program, or a program with capabilities) because
strange values for some environment variables might be used to
subvert system integrity. For example, PATH could be manipulated
so that an arbitrary program is executed with privilege. Use the
exec(3) family of functions instead, but not execlp(3) or
execvp(3) (which also use the PATH environment variable to search
for an executable).
system() will not, in fact, work properly from programs with set-
user-ID or set-group-ID privileges on systems on which /bin/sh is
bash version 2: as a security measure, bash 2 drops privileges on
startup. (Debian uses a different shell, dash(1), which does not
do this when invoked as sh.)
Any user input that is employed as part of command should be
carefully sanitized, to ensure that unexpected shell commands or
command options are not executed. Such risks are especially
grave when using system() from a privileged program.
Summarizing, do not use system() if:
- Invoking from a program with setuid or capabilities
- The program is found or uses $PATH
- Invoking Bash v2
- User input is not sanitized and allowing for shell injection
Coming back to the use of sudo with system() we might think it a bad combination. In reality it is not so, as the corners are covered by sudo, a truly well thought out tool.
Regarding the setuid constraint, it is hardly going to be an issue as if you need to run sudo it is exactly because your program calling system(“sudo …”) is not privileged. Regardless the effective UID/GID is the same as the real GID.
The caveat regarding relying on $PATH for binary location is also made secure with 2 defenses:
- Hard-code your system() call with an absolute path, like system(“/bin/echo Abra kadabra”) and allow that specific absolute path on the sudoers file. Use man sudoers for more information. The manual is truly high quality.
- Delete your /etc/environment, as for example in Ubuntu it contains $PATH. Even though the manual refers to /etc/environment as the source of environment variables, the code shows sudo works as intended if it does not exist at all.
Below is an example for an entry of /etc/sudoers.d/myspecial_permission that illustrates the point made above:
%puny_user ALL=NOPASSWD: /sbin/ifconfig wlan1 up
In a correctly configured system this means that sudo will allow privileged execution of ifconfig only if it is called exactly as written in the sudoers configuration. For example:
system("/sbin/ifconfig wlan1 up"); //Would work
system("/sbin/ifconfig wlan2 up"); //Would not work
The issue with shell invocations deserves special emphasis. Try very hard to not use a shell inside sudo as if for any reason you need to pass user input to the shell command it can be trivial to craft user input that allows arbitrary privileged code execution. I would go as far as stating that if you have shell invocations inside sudo, then you are not serious about security. If you need shell work just write a script. A trivial example of an exploit for the record:
//input = "&& reboot"
void sudo_myself(const char* input) {
int cmd_len = 17 + strlen(input);
const char buf = malloc(cmd_len);
int r = snprintf(buf, cmd_len, "sudo bash -c 'echo Abra kadabra %s'); //check for r
r = system(buf);
}
sudo_myself("&& reboot"); Will reboot!
What the example above also illustrates is when taking unsanitized input, you need to consider if the user might do something you do not want. Thus, craft your system() call in tandem with the /etc/sudoers so that ideally the user can only input the argument expected and not inject unexpected behavior. In a sense this is a general concern with sudo usage, where system() does little to make it better or worse.
Finally keep in mind that it is likely there is an API that does the same work as the command you are passing to system() and sudo, but my experience is that due to the privilege escalation mitigations, it maybe quite cumbersome to implement in the best case, and in the worst case require other external system-wide configurations/permissions that may also have their own pitfalls. An example that immediately comes to my mind is making a change in some network interface. You likely need to set your application to have capabilities configured, which means you will not be able to just run your binary without some kind of installation process.
As usual if you find any inaccuracy let me know.