January 10, 2021

How to race condition

Hi guys, I’ve got some questions about race conditions. I’ve written some code below and I do know that it has a vulnerability: a race condition. This between the lines that check the path / stream, and the point where the file is opened. I want to misuse the race condition to open up “/etc/shadow” to print all passwords. How do I do that?

​

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <unistd.h>`
`#include <sys/stat.h>`
`#include <sys/types.h>`
`#include <errno.h>`
`#define LEN 800`
`int main (int argc, char *argv[])`
`{`
`struct stat st;`
`FILE * fs;`
`char buffer_in[LEN+1];`
`if (argc < 2)`
`{`
`fprintf(stderr, “Usage: %s FILEn”, argv[0]);`
`return -1;`
`}`
`char *path = argv[1];`
`//checks whether the calling process can access the file pathname`
`if (access(path, W_OK) == 0)`
`{`
`int r = lstat(path, &st);`
`if (r < 0)`
`{`
`fprintf(stderr, “lstat failed on %sn”, path);`
`exit(1);`
`}`
`// Checks whether file is a symbolic link`
`if ((st.st_mode & S_IFMT) == S_IFLNK)`
`{`
`fprintf(stderr, “Error: file %s is a symbolic linkn”, path);`
`exit(1);`
`}`
`puts(“continue?”);`
`fgets(buffer_in, 10, stdin);`
`fs = fopen(path, “r”);`
`if (!fs)`
`{`
`fprintf(stderr, “Opening file %s for reading failedn”, path);`
`if(errno == 13)`
`printf(“Errno: %d. Permission denied”, errno);`
`exit(1);`
`}`
`int ret_val = fread(buffer_in, 1, LEN, fs);`
`if(ret_val){`
`buffer_in[ret_val] = 0;`
`printf(“%sn”,buffer_in);`
`}`
`fclose(fs);`
`}`
`else`
`{`
`fprintf(stderr, “Error: access denied!n”);`
`}`
`return 0;`
`}`

Leave a Reply

Your email address will not be published. Required fields are marked *

Note: By filling this form and submitting your commen, you acknowledge, agree and comply with our terms of service. In addition you acknowledge that you are willingly sharing your email address with AiOWikis and you might receive notification emails from AiOWikis for comment notifications. AiOWiksi guarantees that your email address WILL NOT be used for advertisement or email marketting purposes.

This site uses Akismet to reduce spam. Learn how your comment data is processed.