0

I have read many questions and answers about how the setuid bit works and about how sudo works, and I think I have understood that stuff.

However, I couldn't find out what should happen when a program is executed via sudo if that program itself has the setuid bit set. The question is best explained by an example:

On the system in question, there is a user called user1. The system has sudo installed and configured correctly so that the root user can execute programs as whichever different user he wants. Furthermore, there is a program /usr/bin/exampleprog which is owned by root:wheel and has the setuid and setgid bits set:

root@morn ~ # dir /usr/bin/exampleprog
-rwsr-sr-x 1 root wheel 92K 2017-11-16 23:42 /usr/bin/exampleprog

Now I login as root and execute the following command:

root@morn ~ # sudo -u user1 /usr/bin/exampleprog

What happens then? Does exampleprog run as user1 (as could be expected from the options given to sudo), or does it run as root (because the executable is owned by root and has its setuid bit set)?

To make things more complicated, there is an additional program /usr/bin/wrapper, also owned by root:wheel, but without the setuid and setgid bits:

root@morn ~ # dir /usr/bin/wrapper
-rwxr-xr-x 1 root wheel 15K 2017-11-16 23:42 /usr/bin/wrapper

The wrapper program, when being executed, at some point executes (spawns) /usr/bin/exampleprog.

Now I execute:

root@morn ~ # sudo -u user1 /usr/bin/wrapper

When wrapper spawns exampleprog, will the latter run as user1 or as root?

1 Answer 1

2

You can experiment with this by copying the id program somewhere, and changing its permissions. This will show that exampleprog runs with an effective uid of root, and a real uid corresponding to user1.

Consider what would happen if you logged in as user1 and ran exampleprog: since the latter is suid root, you’d expect it to run with an effective uid of root. The same applies when you run the program through sudo.

2
  • Thank you very much, and +1. I would have expected this, but I am having a weird situation here where things seem to behave differently. Now knowing how they should behave, I can investigate further. Also thanks for the hint regarding id; I didn't know about it until now. I'll report back when I know more ...
    – Binarus
    Commented Jan 24, 2022 at 9:44
  • OK, I've found the problem: The program I was executing via sudo indeed had its setuid and setgid bits set, and therefore began execution as root:wheel when starting, but immediately dropped its privileges afterwards, thus again becoming the user and group who involved it (that is, the user and group which had been set by the sudo options -u and -g). I didn't know that this program drops it privileges during startup, and therefore I thought that sudo would override the setsuid mechanism somehow (although I was wondering how that could be possible). Thanks again!
    – Binarus
    Commented Jan 24, 2022 at 10:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .