[libvirt] [PATCH 0/2] Fix virHostdev with session libvirt

Reported by Richard W.M. Jones: https://www.redhat.com/archives/libvir-list/2014-March/msg01780.html Ján Tomko (2): Remove double free in virHostdevManagerDispose Create hostdevmgr in UserRuntimeDirectory for session libvirt src/util/virhostdev.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) -- 1.8.3.2

The object itself is freed by virObjectUnref. --- src/util/virhostdev.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c index 54023bd..d80dbf7 100644 --- a/src/util/virhostdev.c +++ b/src/util/virhostdev.c @@ -83,8 +83,6 @@ virHostdevManagerDispose(void *obj) virObjectUnref(hostdevMgr->activeUSBHostdevs); virObjectUnref(hostdevMgr->activeSCSIHostdevs); VIR_FREE(hostdevMgr->stateDir); - - VIR_FREE(hostdevMgr); } static virHostdevManagerPtr -- 1.8.3.2

Without this, session libvirt won't start if it fails to create the directory. --- So far the directory is only used by SRIOV, which I believe does not work for unprivileged libvirt, but I found always creating the state dir nicer than possibly crashing on NULL derefernce. src/util/virhostdev.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/src/util/virhostdev.c b/src/util/virhostdev.c index d80dbf7..7936289 100644 --- a/src/util/virhostdev.c +++ b/src/util/virhostdev.c @@ -89,6 +89,7 @@ static virHostdevManagerPtr virHostdevManagerNew(void) { virHostdevManagerPtr hostdevMgr; + bool privileged = geteuid() == 0; if (!(hostdevMgr = virObjectNew(virHostdevManagerClass))) return NULL; @@ -105,14 +106,39 @@ virHostdevManagerNew(void) if ((hostdevMgr->activeSCSIHostdevs = virSCSIDeviceListNew()) == NULL) goto error; - if (VIR_STRDUP(hostdevMgr->stateDir, HOSTDEV_STATE_DIR) < 0) - goto error; + if (privileged) { + if (VIR_STRDUP(hostdevMgr->stateDir, HOSTDEV_STATE_DIR) < 0) + goto error; - if (virFileMakePath(hostdevMgr->stateDir) < 0) { - virReportError(VIR_ERR_OPERATION_FAILED, - _("Failed to create state dir '%s'"), - hostdevMgr->stateDir); - goto error; + if (virFileMakePath(hostdevMgr->stateDir) < 0) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("Failed to create state dir '%s'"), + hostdevMgr->stateDir); + goto error; + } + } else { + char *rundir = NULL; + mode_t old_umask; + + if (!(rundir = virGetUserRuntimeDirectory())) + goto error; + + if (virAsprintf(&hostdevMgr->stateDir, "%s/hostdevmgr", rundir) < 0) { + VIR_FREE(rundir); + goto error; + } + VIR_FREE(rundir); + + old_umask = umask(077); + + if (virFileMakePath(hostdevMgr->stateDir) < 0) { + umask(old_umask); + virReportError(VIR_ERR_OPERATION_FAILED, + _("Failed to create state dir '%s'"), + hostdevMgr->stateDir); + goto error; + } + umask(old_umask); } return hostdevMgr; -- 1.8.3.2

On Fri, Mar 28, 2014 at 10:20:29AM +0100, Ján Tomko wrote:
Reported by Richard W.M. Jones: https://www.redhat.com/archives/libvir-list/2014-March/msg01780.html
Ján Tomko (2): Remove double free in virHostdevManagerDispose Create hostdevmgr in UserRuntimeDirectory for session libvirt
src/util/virhostdev.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-)
I applied these patches to libvirt, and the segfault went away. Therefore: ACK. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-p2v converts physical machines to virtual machines. Boot with a live CD or over the network (PXE) and turn machines into KVM guests. http://libguestfs.org/virt-v2v

On 03/28/2014 11:17 AM, Richard W.M. Jones wrote:
On Fri, Mar 28, 2014 at 10:20:29AM +0100, Ján Tomko wrote:
Reported by Richard W.M. Jones: https://www.redhat.com/archives/libvir-list/2014-March/msg01780.html
Ján Tomko (2): Remove double free in virHostdevManagerDispose Create hostdevmgr in UserRuntimeDirectory for session libvirt
src/util/virhostdev.c | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-)
I applied these patches to libvirt, and the segfault went away.
Therefore: ACK.
Thanks, pushed now. Jan
participants (2)
-
Ján Tomko
-
Richard W.M. Jones