libxl: Kill QEMU by uid when possible
authorGeorge Dunlap <george.dunlap@citrix.com>
Fri, 21 Dec 2018 15:41:09 +0000 (15:41 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Fri, 21 Dec 2018 18:41:57 +0000 (18:41 +0000)
commit0c653574d39cb61c83a036c21cbd035c99c931e8
tree60131d5319022fa71f86c381e50a4e5eeefa9cd4
parent6a05c1a9b624f54b3687087829a25503088d1be5
libxl: Kill QEMU by uid when possible

The privcmd fd that a dm_restrict'ed QEMU has gives it permission to
one specific domain ID.  This domain ID will probably eventually be
used again.  It is therefore necessary to make absolutely sure that a
rogue QEMU process cannot hang around after its domain has exited.

Killing QEMU by pid is insufficient in this situation, because QEMU
may be able to fork() to escape killing.  It is surprisingly tricky to
kill a process which can call fork() without races; the only reliable
way is to use kill(-1) to kill all processes with a given uid.

We can use this method only when we're sure that there's only one QEMU
instance per uid.  Add a dm_uid into the domain_build_state struct,
and set it in libxl__domain_get_device_model_uid() when it's safe to
kill by UID.  Store this in xenstore next to device-model-pid.

On domain destroy, check to see if device-model-uid is present in
xenstore.  If so, fork off a reaper process, setuid to that uid, and
do kill(-9) to kill all uids of that type.  Otherwise, carry on
destroying by pid.

While we're here, make libxl__destroy_device_model() consistently:
 1. Return an error when anything fails
 2. But continue to do as much clean-up as possible

NOTE that this is not yet completely safe: with ruid == dm_uid, the
device model may be able to kill(-9) the 'reaper' process before the
reaper process can kill it.  Further patches will address this.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
tools/libxl/libxl_dm.c
tools/libxl/libxl_internal.h