#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <stdio.h>
int main(int argc, char **argv, char **envp)
{
gid_t gid;
uid_t uid;
gid = getegid();
uid = geteuid();
setresgid(gid, gid, gid);
setresuid(uid, uid, uid);
system("/usr/bin/env echo and now what?");
}
The way I understand it, the code above allows arbitrary code (or program) execution — what makes this vulnerable, and how does one take advantage of this?
You can override the
PATHvariable to point to a directory with your custom version ofechoand sinceechois executed usingenv, it isn’t treated as a built-in.This constitues a vulnerability only if the code is run as privileged user.
In the example below file v.c contains the code from the question.
Note that the setting of real user ID, effective user ID and saved set-user-ID by a call to
setresuid()before the call tosystem()in the vulnerable code posted in the question allows one to exploit the vulnerability even when only effective user ID is set to a privileged user ID and real user ID remains unprivileged (as is for example the case when relying on set-user-ID bit on a file as above). Without the call tosetresuid()the shell run bysystem()would reset the effective user ID back to the real user ID making the exploit ineffective. However, in the case when the vulnerable code is run with real user ID of a privileged user,system()call alone is enough. Quotingshman page:Also, note that
setresuid()isn’t portable, butsetuid()orsetreuid()may also be used to the same effect.