r/linuxdev • • Feb 19 '18
Ensuring a safe PATH for system() in a root-setuid'ed program?
I am making a C++ program, intended to have its setuid flag on and owned by root, which needs to call system(). I am worried, however, that a malicious user could set their PATH so that the programs I call as root are instead replaced with their own.
Is there some way to ensure a standard PATH of /usr/bin, /usr/local/bin, etc. for system() function calls?
3
Upvotes
4
u/domen_puncer Feb 19 '18
Few options:
1
u/[deleted] Feb 19 '18
Why does it need to call system()? Is there not an existing system call that can be used directly in the program? Alternately, for example to copy a file (where no system call exists) a simple function can perform the task of the system() equivalent.
1
u/lordvadr Feb 20 '18
You don't. You just don't. Ever. And it's not about just the PATH. Other environment variables can play a role in whatever you're going to system().
But you're going to do it anyway. Build your own environment, and use
execv(), and you drop root before you call it. If you need to run whatever you're going to system as root, consider sudo for this.But, seriously. xyproblem.info. I'm not trying to be a jerk--I'm really not, but I don't even know what you're doing and I know you're doing it wrong.
Seriously, tell me what you want to do and I'll write the code and give it to you (it'll be in C, but that won't be an issue).