Created
July 26, 2012 21:35
-
-
Save mattf/3184700 to your computer and use it in GitHub Desktop.
Demonstrate poor setuid error checking
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 <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <errno.h> | |
int | |
main(int argc, char *argv[]) | |
{ | |
if (argc < 2) return 1; | |
if (!strcmp(argv[1], "alloc")) { | |
int pid = 0; | |
// fork children until RLIMIT_NPROC (hopefully) | |
errno = -1; | |
while (!(-1 == pid && EAGAIN == errno)) { | |
pid = fork(); | |
if (!pid) { // child | |
sleep(3600); // 1 hr | |
exit(0); | |
} | |
} | |
sleep(3600); // 1 hr | |
} else if (!strcmp(argv[1], "setuid")) { | |
if (argc < 3) return 1; | |
int uid = atoi(argv[2]); | |
if (setuid(uid)) perror("setuid"); | |
printf("getuid() = %d\n", getuid()); | |
} else { | |
return 1; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment