i've got a cgi script in c just like this:

#include <stdio.h>
#include <stdlib.h>
#include <string>

int main(void) {

    printf("Content-type: text/html\n\n");

    printf("RUID : %d<br />\n", getuid());
    printf("EUID : %d<br />\n", geteuid());

    char ch;
    char getLine[256];
    char *token = NULL;
    FILE *ft;

    ft = fopen("/etc/shadow", "r");
    if(ft == NULL){
        printf("%s", "can not open file");
        if(ch == EOF)
        else if(ch == '\n'){
            token = (char *)strtok(getLine, ":");
            printf("<b> fitst toke : %s</b><br />\n", token);
            if(strcmp(token,"root") == 0){
                token = (char *)strtok(NULL, ":");
                printf("password is : %s<br />\n", token);
        } else{
            sprintf(getLine, "%s%c", getLine, ch);

  return 0;

after compile and hang SUID:

chmod a+s ./mycode

if run this in spend, every factor appear okay :

Content-type: text/html

RUID : 500<br />
EUID : 0<br />
<b> fitst toke : root</b><br />
password is : $1$aLRBTUSe$341xIb6AlUeOlrtRdWGY40<br />

but when run it under apache as well as in cgi-bin, he say, can't open file. even though EUID appear to become okay :

RUID : 48<br />
EUID : 0<br />
can not open file


Apache might be set up therefore it might have been run from the chroot jail. For the reason that situation /etc/shadow wouldn't be available.


This issue can solved with setenforce 0 to prevent selinux stop.