Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
YesWeHack - LeHack challenge 2019
#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
You can’t perform that action at this time.