user244145
user244145

Reputation: 73

Open file with owner's file permissions in c

I need to write a program in C that opens a text file and prints the contents. However, it needs to be able to read and print a file that only the owner has access to read, even if someone else runs the program. I can't figure out how I would get the owner's read permissions.

I worded the problem poorly, let me be more specific. I need to make two programs, one that just reads a text file normally, and another that can read a text file with the permissions of the owner.

Here is the normal file reading program, read_unprivileged.c

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

int main(int argc, char *argv[])
{   
    FILE *fp;
    char ch;
    char *filename = argv[1];

    fp = fopen(filename, "r"); //lets you read file                              

    if(fp == NULL){
        printf("File is null!");
    }

    while( ( ch = fgetc(fp) ) != EOF ){
        printf("%c",ch);
    }
    fclose(fp);
    return 0;
}

And here is the one that should be able to read the file with owner's permissions (same thing except for the highlighted lines), read_privileged.c

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

int main(int argc, char *argv[])
{   
    FILE *fp;
    char ch;
    char *filename = argv[1];

    setreuid(geteuid(), getuid());
    fp = fopen(filename, "r"); //lets you read file                              
    setreuid(geteuid(), getuid());

    if(fp == NULL){
        printf("File is null!");
    }

    while( ( ch = fgetc(fp) ) != EOF ){
        printf("%c",ch);
    }
    fclose(fp);
    return 0;
}

So the file in question is only readable by the owner (me).

-rwx------ secret.txt

Both programs can read files when I (the owner) call it, as they should. So I do: chmod 4755 read_privileged.c chmod 4755 read_unprivileged.c

When either one compiles into a.out, the a.out file doesn't have the userid bit.

-rwxr-xr-x a.out

When I run the compiled a.out as another user, neither one works. However, if I do:

chmod 4755 a.out

Both of them work, which I guess defeats the purpose (since read_unprivileged works and it shouldn't). I don't understand why the executable loses the privileges, and why the set setreuid(geteuid(), getuid()); doesn't work.

Upvotes: 0

Views: 2080

Answers (1)

Ronald
Ronald

Reputation: 2882

on a unix system the resulting executable needs to have the s-bit set. This can be done by the root user or by the user owning the file (he must also own the executable).

If neither root nor the file owner assists you in this, you don't have the right to read the file.

let me assume your program file is called readit and has been compiled by the file owner. The file owner now has to do a chmod +s readit. After doing so, starting the program (by any user) will set the effective userid to the userid of the file owner.

Be careful though. Programs with s-bit set are a beloved target for break-in attempts.

Upvotes: 2

Related Questions