When searching for how to drop root permissions in shell scripts, I often see answers using su
. However, when you're done using su
you can just type logout
and be back at the shell of the original user. I have a bash script running as root, and I do this at the end:
exec su -c "external_com" - muser
This results in the following process tree:
PID USER PRI NI VIRT RES SHR S CPU% MEM% TIME+ Command
1 root 20 0 37352 2500 2192 S 0.0 0.0 0:00.02 su -c external_com
5 muser 20 0 85548 6292 5156 S 0.0 0.0 0:00.47 └─ external_com
13 muser 20 0 85548 6292 5156 S 0.0 0.0 0:00.35 ├─ external_com
12 muser 20 0 85548 6292 5156 S 0.0 0.0 0:00.02 ├─ external_com
I cannot control external_com
, and it doesn't call setuid internally. It presents a web interface that's public to the world. If someone somehow gets a console through a security hole in the web interface, could they just call logout
and be a root user?
EDIT: If I'm reading correctly, a better approach is to just run the script as muser
and allow muser
to sudo the few commands it needs. I'd still like an answer to this question though, as it's valuable information to know :)