I was writing some bash
code and I wanted to check if the script had root privileges. All of the code I see from other people only checks for $EUID == 0
. It is perfectly possible to have other programs run with UID = 0 and EUID > 0. Shouldn't my code check for both? Or is there something about bash which prevents this (strange) situation?
For those who want to see such a situation, here's a proof of concept:
[root@new-host bennett]# cp /bin/sleep .
[root@new-host bennett]# chown bennett sleep
[root@new-host bennett]# chmod 4755 sleep
[root@new-host bennett]# ll sleep
-rwsr-xr-x. 1 bennett root 32188 Sep 9 02:38 sleep
[root@new-host bennett]# ./sleep 1000 &
[root@new-host bennett]# ps -e -o user= -o ruser= -o comm= | awk '$1 != $2'
bennett root sleep
To recap the question: Can bash
ever encounter a situation where (( $UID == 0 && $EUID > 0 ))
, under any circumstances? Other programs can.
/bin/bash
directly? Or indeed your script? – JdeBP Sep 09 '17 at 07:32