提交 ad521843 编写于 作者: E Eric Blake 提交者: Daniel P. Berrange

CVE-2013-6456: Avoid unsafe use of /proc/$PID/root in LXC shutdown/reboot code

Use helper virProcessRunInMountNamespace in lxcDomainShutdownFlags and
lxcDomainReboot.  Otherwise, a malicious guest could use symlinks
to force the host to manipulate the wrong file in the host's namespace.

Idea by Dan Berrange, based on an initial report by Reco
<recoverym4n@gmail.com> at
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=732394Signed-off-by: NEric Blake <eblake@redhat.com>
(cherry picked from commit aebbcdd3)
上级 158b5373
...@@ -2699,13 +2699,21 @@ lxcConnectListAllDomains(virConnectPtr conn, ...@@ -2699,13 +2699,21 @@ lxcConnectListAllDomains(virConnectPtr conn,
} }
static int
lxcDomainInitctlCallback(pid_t pid ATTRIBUTE_UNUSED,
void *opaque)
{
int *command = opaque;
return virInitctlSetRunLevel(*command);
}
static int static int
lxcDomainShutdownFlags(virDomainPtr dom, lxcDomainShutdownFlags(virDomainPtr dom,
unsigned int flags) unsigned int flags)
{ {
virLXCDomainObjPrivatePtr priv; virLXCDomainObjPrivatePtr priv;
virDomainObjPtr vm; virDomainObjPtr vm;
char *vroot = NULL;
int ret = -1; int ret = -1;
int rc; int rc;
...@@ -2732,16 +2740,14 @@ lxcDomainShutdownFlags(virDomainPtr dom, ...@@ -2732,16 +2740,14 @@ lxcDomainShutdownFlags(virDomainPtr dom,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&vroot, "/proc/%llu/root",
(unsigned long long)priv->initpid) < 0)
goto cleanup;
if (flags == 0 || if (flags == 0 ||
(flags & VIR_DOMAIN_SHUTDOWN_INITCTL)) { (flags & VIR_DOMAIN_SHUTDOWN_INITCTL)) {
if ((rc = virInitctlSetRunLevel(VIR_INITCTL_RUNLEVEL_POWEROFF, int command = VIR_INITCTL_RUNLEVEL_POWEROFF;
vroot)) < 0) {
if ((rc = virProcessRunInMountNamespace(priv->initpid,
lxcDomainInitctlCallback,
&command)) < 0)
goto cleanup; goto cleanup;
}
if (rc == 0 && flags != 0 && if (rc == 0 && flags != 0 &&
((flags & ~VIR_DOMAIN_SHUTDOWN_INITCTL) == 0)) { ((flags & ~VIR_DOMAIN_SHUTDOWN_INITCTL) == 0)) {
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
...@@ -2767,7 +2773,6 @@ lxcDomainShutdownFlags(virDomainPtr dom, ...@@ -2767,7 +2773,6 @@ lxcDomainShutdownFlags(virDomainPtr dom,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(vroot);
if (vm) if (vm)
virObjectUnlock(vm); virObjectUnlock(vm);
return ret; return ret;
...@@ -2779,13 +2784,13 @@ lxcDomainShutdown(virDomainPtr dom) ...@@ -2779,13 +2784,13 @@ lxcDomainShutdown(virDomainPtr dom)
return lxcDomainShutdownFlags(dom, 0); return lxcDomainShutdownFlags(dom, 0);
} }
static int static int
lxcDomainReboot(virDomainPtr dom, lxcDomainReboot(virDomainPtr dom,
unsigned int flags) unsigned int flags)
{ {
virLXCDomainObjPrivatePtr priv; virLXCDomainObjPrivatePtr priv;
virDomainObjPtr vm; virDomainObjPtr vm;
char *vroot = NULL;
int ret = -1; int ret = -1;
int rc; int rc;
...@@ -2812,16 +2817,14 @@ lxcDomainReboot(virDomainPtr dom, ...@@ -2812,16 +2817,14 @@ lxcDomainReboot(virDomainPtr dom,
goto cleanup; goto cleanup;
} }
if (virAsprintf(&vroot, "/proc/%llu/root",
(unsigned long long)priv->initpid) < 0)
goto cleanup;
if (flags == 0 || if (flags == 0 ||
(flags & VIR_DOMAIN_REBOOT_INITCTL)) { (flags & VIR_DOMAIN_REBOOT_INITCTL)) {
if ((rc = virInitctlSetRunLevel(VIR_INITCTL_RUNLEVEL_REBOOT, int command = VIR_INITCTL_RUNLEVEL_REBOOT;
vroot)) < 0) {
if ((rc = virProcessRunInMountNamespace(priv->initpid,
lxcDomainInitctlCallback,
&command)) < 0)
goto cleanup; goto cleanup;
}
if (rc == 0 && flags != 0 && if (rc == 0 && flags != 0 &&
((flags & ~VIR_DOMAIN_SHUTDOWN_INITCTL) == 0)) { ((flags & ~VIR_DOMAIN_SHUTDOWN_INITCTL) == 0)) {
virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
...@@ -2847,7 +2850,6 @@ lxcDomainReboot(virDomainPtr dom, ...@@ -2847,7 +2850,6 @@ lxcDomainReboot(virDomainPtr dom,
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(vroot);
if (vm) if (vm)
virObjectUnlock(vm); virObjectUnlock(vm);
return ret; return ret;
......
...@@ -111,16 +111,18 @@ struct virInitctlRequest { ...@@ -111,16 +111,18 @@ struct virInitctlRequest {
# endif # endif
/* /*
* Send a message to init to change the runlevel * Send a message to init to change the runlevel. This function is
* asynchronous-signal-safe (thus safe to use after fork of a
* multithreaded parent) - which is good, because it should only be
* used after forking and entering correct namespace.
* *
* Returns 1 on success, 0 if initctl does not exist, -1 on error * Returns 1 on success, 0 if initctl does not exist, -1 on error
*/ */
int virInitctlSetRunLevel(virInitctlRunLevel level, int
const char *vroot) virInitctlSetRunLevel(virInitctlRunLevel level)
{ {
struct virInitctlRequest req; struct virInitctlRequest req;
int fd = -1; int fd = -1;
char *path = NULL;
int ret = -1; int ret = -1;
memset(&req, 0, sizeof(req)); memset(&req, 0, sizeof(req));
...@@ -131,36 +133,28 @@ int virInitctlSetRunLevel(virInitctlRunLevel level, ...@@ -131,36 +133,28 @@ int virInitctlSetRunLevel(virInitctlRunLevel level,
/* Yes it is an 'int' field, but wants a numeric character. Go figure */ /* Yes it is an 'int' field, but wants a numeric character. Go figure */
req.runlevel = '0' + level; req.runlevel = '0' + level;
if (vroot) { if ((fd = open(VIR_INITCTL_FIFO,
if (virAsprintf(&path, "%s/%s", vroot, VIR_INITCTL_FIFO) < 0) O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY)) < 0) {
return -1;
} else {
if (VIR_STRDUP(path, VIR_INITCTL_FIFO) < 0)
return -1;
}
if ((fd = open(path, O_WRONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY)) < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }
virReportSystemError(errno, virReportSystemError(errno,
_("Cannot open init control %s"), _("Cannot open init control %s"),
path); VIR_INITCTL_FIFO);
goto cleanup; goto cleanup;
} }
if (safewrite(fd, &req, sizeof(req)) != sizeof(req)) { if (safewrite(fd, &req, sizeof(req)) != sizeof(req)) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to send request to init control %s"), _("Failed to send request to init control %s"),
path); VIR_INITCTL_FIFO);
goto cleanup; goto cleanup;
} }
ret = 1; ret = 1;
cleanup: cleanup:
VIR_FREE(path);
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
return ret; return ret;
} }
......
/* /*
* virinitctl.h: API for talking to init systems via initctl * virinitctl.h: API for talking to init systems via initctl
* *
* Copyright (C) 2012 Red Hat, Inc. * Copyright (C) 2012-2014 Red Hat, Inc.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
...@@ -37,7 +37,6 @@ enum virInitctlRunLevel { ...@@ -37,7 +37,6 @@ enum virInitctlRunLevel {
VIR_INITCTL_RUNLEVEL_LAST VIR_INITCTL_RUNLEVEL_LAST
}; };
int virInitctlSetRunLevel(virInitctlRunLevel level, int virInitctlSetRunLevel(virInitctlRunLevel level);
const char *vroot);
#endif #endif
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册