1 year ago

#188280

test-img

Dennis

Problems with readlink and set uid

I'm having trouble getting setuid to work as hoped (chmod 3755).

I'm trying to implement, using C language, my own version of psgrep (or pgrep) for a very specific need. In order to accomplish this, I need to get the value of the link at /proc/[pid]/exe for every listed [pid]

To demonstrate the issue, I made this tiny program: (proof.c)

#define _GNU_SOURCE
#include <unistd.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    char procFilename[] = "/proc/1/exe" ;
    char pointsTo[80] ;
    int rc ;

    memset(pointsTo, 0x00, sizeof(pointsTo)) ;
    rc = readlink(procFilename, pointsTo, sizeof(pointsTo)) ;
    if ( rc < 0 ) {
        perror("Trying to read /proc/1/exe link") ;
        }
    else {
        printf("%s points to %s.\n", procFilename, pointsTo) ;
        }
    }

In the same folder, I exeucte:

gcc -o proof proof.c
sudo chown root:root proof
sudo chmod 3755 proof

Now, here are the execution results:

> ./proof
Trying to read /proc/1/exe link: Permission denied
> sudo ./proof
/proc/1/exe points to /usr/lib/systemd/systemd.
> ls -l proof
-rwxr-sr-t 1 root root 16888 Feb 16 14:06 proof

As I understand it, the chmod and chown combination should have gotten me past the permission error, but obviously I am missing something.

When I searched stackoverflow for "sticky-bit problems" there were no good matches (though a surprising number of people expect setuid to work with scripts). One problem about 2.6 kernel didn't seem to fit either. Anyway, what might I be missing?

c

chmod

chown

setuid

0 Answers

Your Answer

Accepted video resources