clockley1
clockley1

Reputation: 494

How to drop root privileges on linux in C?

I'd like to fork and exec and program as a non root user while the parent retains root permissions.

pseudo code:

  pid = fork();

  if (pid == 0) {
      if (RunAsUser(ConvertStringToUserId("John")) == false) {
          stop();
      }
      if (RunAsUser(ConvertStringToUserId("admin")) == true) {
          stop();
      }
      CreateProcess();
  }

Upvotes: 4

Views: 2375

Answers (2)

chiastic-security
chiastic-security

Reputation: 20520

Get your program to invoke the child process as

sudo -u user /path/to/externalprogram

instead of just

/path/to/externalprogram

Upvotes: 0

Wojtek Surowka
Wojtek Surowka

Reputation: 21003

If you want to drop privileges in C code, use the function setuid.

Upvotes: 4

Related Questions