Search code examples
cbashprocessuid

Can I get a different UID than EUID and GID than EGID in a command?


I have a program that gets its own UID, EUID, GID, EGID, and I want to get UID!=EUID and GID!=EGID.

I've tried changing the owner and group of the program file, but I'm always getting the same results.

Here is the c code:

#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

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

    pid_t pid;
    pid = getpid();

    char s1[] = "ps -p ";
    char s2[] = " -o cmd,pid,ppid,uid,euid,gid,egid,pri,stat";
    char spid[42];
    sprintf(spid, "%d", (int)pid);
    char result[2048];

    strcpy(result, s1);
    strcat(result, spid);
    strcat(result, s2);

    sprintf(cad,"Program %s started.\n",argv[0]);
    write(1,cad,strlen(cad));

    system(result);

    read(0,cad,1);

    sprintf(cad,"Program %s ended.\n",argv[0]);
    write(1,cad,strlen(cad));

    exit(0);
}

The commands I've tried are:

$ sudo chown 2000 filename
$ sudo chgrp 2000 filename
$ sudo chmod u+r,g+x filename

And then executing the compiled program. Always getting:

Program ./pgm1_1_1 started.
CMD                           PID  PPID   UID  EUID   GID  EGID PRI STAT
./pgm1_1_1                  18216 17683  1000  1000  1000  1000  19 S+

Solution

  • You've not set the SUID and SGID bits in the permissions:

    sudo chmod ug+s filename
    

    Or, doing the permissions setting all in one, you can use either of these:

    sudo chmod 6755 filename              # Numeric permissions (octal)
    sudo chmod a=rx,u+w,ug+s filename     # Symbolic permissions
    

    You should run ls -l filename after you've finished the permissions setting. With the SUID and SGID bits set, you should see something like:

    -rwsr-sr-x  1 2000  2000  8712 May  8 00:18 filename
    

    where the two s values are crucial in the permissions. You might have names where I get to see the UID and GID numbers.