[EDIT: Revisiting this question and seeing it is still wrongly marked as a duplicate] The following question on SE is not a duplicate as it asks about executing bash with the suid bit, which is a special case and does not work at all: Setuid bit seems to have no effect on bash The first difference is that in my example I execute whoami, not bash. The second difference is that it actually works as expected on Ubuntu, but not on SuSE.
Suid bit works fine on my PC running Ubuntu, but not on a SLES test instance.
The nosuid flag is not set on the mounted xfs file system on the SLES machine. ls
shows that ony my machine and the SLE Server, the same permissions are set for the executable. So why does the executable still run as the current user instead of as the owner?
execsudo.c:
#include <stdio.h>
#include <stdlib.h>
int main(int argc,char *argv[]) {
system(argv[1]);
return 0;
}
bash:
gcc -o setuid-test execsudo.c ;
sudo chown nobody ./setuid-test;
sudo chmod +s ./setuid-test;
./setuid-test "whoami"
# Outputs current user instead of nobody
[EDIT 2] I still have not worked out the problem, but I suppose it might be because the SuSE machine is a VM. A workaround might be to configure this behaviour via /etc/sudoers instead.
chown
after thechmod
? That might cause the setuid bit to be removed... – Stephen Harris Aug 19 '16 at 15:11ls
; the bit is set correctly. Thanks for pointing out the mistake, I will correct my post. – phobic Aug 19 '16 at 15:53setuid(geteuid())
call before thesystem()
? Or just aprintf("%d\n",geteuid());
call? – Stephen Harris Aug 19 '16 at 16:01id
instead ofwhoami
? – Stephen Harris Aug 19 '16 at 16:46id
also shows the current user.id -u
differs from the output ofgeteuid
. – phobic Aug 19 '16 at 17:38./setuid-test "setuid-test id"
; Now it prints getuid of 'nobody' twice and then outputs the id of the current user. So it works with some programs, but maybe not shell builtins? Edit: No, calling /usr/bin/whoami does not change the result. – phobic Aug 19 '16 at 17:41id
typically isn't a builtin (/usr/bin/id
). Makes me wonder if there's some other layer (SELinux, Apparmour or SLES equiv) that's not allowing transitions in some cases. confusing – Stephen Harris Aug 19 '16 at 17:45sh
(likesystem(3)
) does. I don't know if that's a distro-specific change, though. And no, I didn't rundash
, but a renamed, setuid copy ofbash
. – ilkkachu Aug 21 '16 at 13:29setuid()
in your program. I'll vote to reopen it to remove the duplicate flag so it can be flagged off-topic. – Julie Pelletier Aug 22 '16 at 16:25setuid
to no effect. Also, as mentioned in my post calling the function is not necessary on my Ubuntu machine. Apart from that, if you can point out a programming error I can't see, please do so. – phobic Aug 23 '16 at 07:11