Skip to content

Instantly share code, notes, and snippets.

@pastleo
Last active July 5, 2020 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pastleo/fa5d5f14b90b1b28e743f522ae682c04 to your computer and use it in GitHub Desktop.
Save pastleo/fa5d5f14b90b1b28e743f522ae682c04 to your computer and use it in GitHub Desktop.
setuid experiment
#include <stdio.h>
// provides printf()
#include <stdlib.h>
// provides system()
#include <unistd.h>
// provides getuid(), geteuid(), setuid()
int main ()
{
// https://en.wikipedia.org/wiki/Setuid
// setuid will not work on script executables (ex. files begin with #!/bin/bash)
// because the real executable is /bin/bash (or node/ruby...)
// and sub-process will be run as original user
// root:
// gcc setuid-experiment.c -o setuid-experiment
// chmod 4711 setuid-experiment
// user:
// ./setuid-experiment
// at first a.out is started with uid: user and effective uid: 0
printf("uid: %d, euid: %d\n", getuid(), geteuid());
system("id"); // when running system call, bash runs using uid, not euid
// https://unix.stackexchange.com/questions/369883/setuid-root-does-not-work
// we need this to make the process really running as root
setuid(0);
// now we are really root
printf("uid: %d, euid: %d\n", getuid(), geteuid());
system("id");
// finally this will work
system("ls /root");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment