1758620 Members
1984 Online
108874 Solutions
New Discussion юеВ

Re: setuid

 
SOLVED
Go to solution
j773303
Super Advisor

setuid

I have a c program as below. the execute file owner is not root, assume is use1. I'd like to set the user id to ZERO while run the program.
But this program "setuid(0)" always run error.
Does anyone has the experience how to use the setuid() in C program?

#include
#include

main()
if(setuid(0) < 0) {
printf("setuid error ");
exit(1);
}
Hero
3 REPLIES 3
Rajeev  Shukla
Honored Contributor
Solution

Re: setuid

With setuid() you have to have root as the owner of the file and have setuid bit set on the file. Then it will work properly.

Here is a sample program which sets the uid to root and runs some file and then exits.

#include
#include
#include

main(argc,argv)
int argc;
int *argv[];
{

int uid;
char *command=(char *)malloc(2048);

uid=getuid();
setuid(0);
strcpy(command,"/home/rshukla/scripts/killfsq");
if ( system(command) !=0 ) {
printf("1:Failed to kill the FSQ \n");
exit(1);
}

printf("0:FSQ Killed ...\n");
}

the compiled program fsqkill should have following permissions.

-r-sr-xr-x 1 root sys 20480 Sep 23 2002 fsqkill

Then it will work properly.

Thanks
Rajeev
A. Clay Stephenson
Acclaimed Contributor

Re: setuid

Yes, I have just a little bit of experience in using setuid in c. What you are trying to do is hopeless unless 1) root owns this executable and 2) the setuid bit is set on the file (e.g chmod 4755 myfile). It's a very good thing it works this way!! If your method worked then anyone could become super-user anytime they wanted to!
If it ain't broke, I can fix that.
Niel Greeff
Occasional Contributor

Re: setuid

I found that with the Goldbase June 2003 and Dec 2003 pathes installed, it fails all the time. Don't know if this is on your system.

Tried uninstalling and it started working again. They must have changed the library ??