Created
July 1, 2019 13:18
-
-
Save CERT-W/c68f48f654d50ef5df9e2f63ec4dc19e to your computer and use it in GitHub Desktop.
YesWeHack - LeHack challenge 2019
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <sys/types.h> | |
#include <sys/sendfile.h> | |
#include <sys/stat.h> | |
#include <fcntl.h> | |
void die(const char *fmt, ...) | |
{ | |
va_list args; | |
va_start(args, fmt); | |
vprintf(fmt, args); | |
va_end(args); | |
exit(1); | |
} | |
int file_belong_to_user(const char *filename) | |
{ | |
struct stat path_stat; | |
stat(filename, &path_stat); | |
return (path_stat.st_uid == getuid()) && (path_stat.st_gid == getgid()) ; | |
} | |
size_t get_file_size(const char *filename){ | |
struct stat path_stat; | |
stat(filename, &path_stat); | |
return path_stat.st_size; | |
} | |
void read_file(const char *filename){ | |
int fd = open(filename, O_RDONLY); | |
if (fd < 0){ | |
die("Unable to open \"%s\"\n", filename); | |
} | |
sendfile(fileno(stdout), fd, 0, get_file_size((filename))); | |
} | |
int main(const int argc, const char **argv){ | |
if (argc < 2){ | |
die("Usage: ./file_reader FILENAME...\n"); | |
} | |
for (int x = 0; x < argc - 1; ++x){ | |
const char *filename = argv[x + 1]; | |
if (!file_belong_to_user(filename)){ | |
die("[ERROR] \"%s\" does not belong to you.\n", filename); | |
} | |
} | |
for (int x = 0; x < argc - 1; ++x){ | |
const char *filename = argv[x + 1]; | |
read_file(filename); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment