Skip to content

Instantly share code, notes, and snippets.

@mattf
Created July 26, 2012 21:35
Show Gist options
  • Save mattf/3184700 to your computer and use it in GitHub Desktop.
Save mattf/3184700 to your computer and use it in GitHub Desktop.
Demonstrate poor setuid error checking
#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