Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop root privileges on linux in C?

Tags:

c

linux

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();
  }
like image 822
clockley1 Avatar asked Sep 06 '25 03:09

clockley1


1 Answers

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

like image 105
Wojtek Surowka Avatar answered Sep 07 '25 22:09

Wojtek Surowka