I'm trying to write a program that run /bin/bash with user smith privileges,
smith:x:1000:1000:Basket:/home/smith:/bin/bash
I tried this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
setgid(1000);
setuid(1000);
char command[50];
strcpy( command, "/bin/bash" );
system(command);
return(0);
}
and I used those command to set the owner, group, and the permissions
chown smith command
chgrp smith command
chmod +x command
chmod u+s command
the permissions after the commands:
-rwsr-xr-x 1 smith smith 16840 Jun 6 17:11 command
and didn't work, I tried with root as next:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
setgid(0);
setuid(0);
char command[50];
strcpy( command, "/bin/bash" );
system(command);
return(0);
}
and I used the same commands for permissions and so on but instead of smith, I wrote root and I worked and when I run it I'm getting a shell as root.
So how I can do it with a smith user?
Linux Just Works Like That (tm)
If any user could become another user willy-nilly then Linux would have no access permissions at all.
Only a process running with root permission can change its effective credentials.
That being said, Linux provides fine-grained credentials. See:
for the details.
A better solution to the "I want to be root" syndrome is to check out sudo(8). Using sudo(8) is better because: