Skip to content

Instantly share code, notes, and snippets.

@vanhoefm
Created November 5, 2012 19:22
Show Gist options
  • Save vanhoefm/4019750 to your computer and use it in GitHub Desktop.
Save vanhoefm/4019750 to your computer and use it in GitHub Desktop.
Bash drop priviliges
int main()
{
running_setuid = uidget();
...
if (running_setuid && privileged_mode == 0)
disable_priv_mode();
...
// start interactive shell or execute command
}
/* Fetch uids and gids, return 1 if we're running setuid or setgid. */
static int uidget()
{
...
current_user.uid = getuid();
current_user.gid = getgid();
current_user.euid = geteuid();
current_user.egid = getegid();
/* See whether or not we are running setuid or setgid. */
return (current_user.uid != current_user.euid) ||
(current_user.gid != current_user.egid);
}
void disable_priv_mode()
{
setuid (current_user.uid);
setgid (current_user.gid);
current_user.euid = current_user.uid;
current_user.egid = current_user.gid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment