Is there any way to augment privileges for your process? Here's an example: Pretend program A was compiled in C with only user-mode privileges. Now let's just say this program wanted to get root privileges but it needed the user name and password for root. So it called upon a macro, or library that then requested the user name, and password. If the user name, or password was incorrect it returned a error, but if the user name, and password were correct it would grant the process root privileges.

  • I don't think so. You should use sudo or su for that. In general the program would have to be setuid and owned by the user with higher privileges but that you don't get the password prompt. – Lucas Jan 31 '17 at 21:57
  • This sounds something like what polkit(8) offers. – thrig Jan 31 '17 at 22:39
  • ill have to look at polkit then – Fumerian Gaming Jan 31 '17 at 22:52
  • One simple handling you could do from within the program would be to print the message (to standard error), "This program must be run with root permissions," and then exit with an error status. Of course, be prepared for sysadmins to just delete it instead if they don't trust you. :) – Wildcard Jan 31 '17 at 23:44
  • Ok ill try doing that – Fumerian Gaming Feb 1 '17 at 0:08

This is not possible. Not because there's no function in C, but because the Unix security model forbids it.

Under the Unix security model, a program can only reduce its privileges, it can never increase its privileges. A process can temporarily reduce its effective privileges, and regain its original “real” privileges later, but it can never go further up than where it started.

A process that logs users in must start with root privileges, i.e. with the right to do everything. Once the user has entered their name and password and the login program has verified them, the process permanently changes to the desired user. It can get the root privileges in either of two ways:

  • It can be started by another process that is running as root. This is how console login and SSH login work.
  • It can be a setuid executable belonging to root. This is how su and sudo work.

I've simplified some things here, but the fundamental principle that a program cannot increase its privileges is always true. There is only one way in which a process can gain privileges, and that's by executing a setuid¹ executable.

If a program needs to execute some tasks with higher privileges, it won't be able to do it by itself. It must get a higher-privileges process to do it. It can do this either by contacting an already-running server that runs with the desired privileges, or by running a setuid executable. The setuid executable can be something like su or sudo which in turn runs an executable that doesn't have any special permissions, but if there is privilege elevation, there has to be a setuid executable in the chain.

¹ or setgid, setpcap…

  • When you siad "It can be started by another process that is running as root. This is how console login and SSH login work." does that mean it reads off of a already running process or does it start a new process as root. – Fumerian Gaming Feb 1 '17 at 13:37
  • @FumerianGaming It is started by another process that is running as root. All processes are started by another process, eventually going back to process number 1 (conventionally called init), which is started by the kernel at boot time. – Gilles 'SO- stop being evil' Feb 1 '17 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.