Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to make a C program run always as root, no matter who is calling it. Basically, I want it to invoke "mkdir /test" as an example. So I created the C program as follows:

#include <stdio.h>

int main()
{
system("mkdir /test");
printf("bye...\n");
return 0;
}

Now, I just compiled it: gcc test.c -o test

And now I tried to set the permissions:

chmod +s test

However running it as a normal user, I get a permission denied error. So, it executes the file but not with root's permissions. I also tried setting the permissions as:

chmod a+s test
chmod o+s test

But I always get the same problem.

Anyone can help me with this? By the way, the file test.c is being created by root and it's also being compiled as root.

bash-3.2# ls -al | grep test 
-rwxr-xr-x   1 root   staff       8796  5 Ago 19:07 test
bash-3.2# chmod +s test
bash-3.2# ls -al | grep test 
-rwsr-sr-x   1 root   staff       8796  5 Ago 19:07 test
bash-3.2# whoami
root
bash-3.2#

Thanks in advance! Cheerz!

share|improve this question

1 Answer 1

There are two things to know here:

  • The sticky bit, which could be used on files and directories, but won't do what you want. From sticky(8): The sticky bit has no effect on executable files.

  • The setuid flag, which would allow a program to be run with its owners permissions. It is ignored for security reasons though. Why? What you seek would be a very severe security exploit, and this is why setuid is ignored in OS X. Trusting a non-admin user to run a command with root privileges would defeat the purpose of even having user groups or passwords. And in fact, that's what sudo is for.

Anyway, you can modify the sudoers file in such a way that it won't require a password for one command. Remember that you have to use visudo to edit it. If you manage to get the syntax wrong while editing the file, you won't be able to run sudo at all anymore.

sudo visudo

Then, press I, and at the bottom insert:

username    ALL= NOPASSWD: /path/to/command

Here, you obviously need to change the username for the user who is supposed to run the command without having to type a password. Also, change the path to your executable. Note that at this point, the executable can be owned by root and have execute permissions only for root as well.

Press Esc, then write :wq, then Enter.

Now, the user username can run the command with sudo /path/to/command and doesn't need to enter a password to do that.

share|improve this answer
    
The sticky bit has nothing to do with setuid, which is why I thought it strange when the asker brought it up. –  Ignacio Vazquez-Abrams Aug 5 '12 at 18:27
    
Cool! I didn't know that. Yes it seems way safer that way. –  yaroze Aug 5 '12 at 18:29
    
@IgnacioVazquez-Abrams, you're completely right. Thanks for pointing that out. I already edited the question. –  yaroze Aug 5 '12 at 18:30
    
If the program requires root permissions to function, you can always check your current user ID, and if non-zero, try to run sudo $0 (or equivalent). If that works, great; otherwise print an error message and exit. Since this isn't exactly expected behavior, you shouldn't do this for something you want to distribute. –  Daniel Beck Aug 5 '12 at 20:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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