setuid programming question

denali

Golden Member
Oct 10, 1999
1,122
0
0
Under RedHat 7.2 I need to be able to stat /dev/hda as a typical user, this is part of a larger program. Since /dev/hda has permissions of 660 I think I need to use setuid. I have tried with just a simple program
but can not get setuid to work. After I compile the program I chmod 4755 the program and when I run it setuid always fails. What am I doing wrong.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
int status;
status = setuid(0);

if(status < 0)
{
fprintf("setuid failed.\n");
exit(status);
}
}
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
If the program is setuid it's already running as the owning uid (make sure root owns it), there's no need to change your euid unless you're dropping privs.

Also if you set the permissions on /dev/hda properly, you shouldn't need uid to be 0. Just add your user to the disk group and make sure the disk group has rwx to /dev/hda.