run QEMU as non-root
PATCH v8 1/1 run QEMU as non-root
Try to use "xen-qemudepriv-domid$domid" first, then "xen-qemudepriv-shared" and root if everything else fails.
The uids need to be manually created by the user or, more likely, by the xen package maintainer.
Expose a device_model_user setting in libxl_domain_build_info, so that opinionated callers, such as libvirt, can set any user they like. Do not fall back to root if device_model_user is set.
QEMU is going to setuid and setgid to the user ID and the group ID of the specified user, soon after initialization, before starting to deal with any guest IO.
To actually secure QEMU when running in Dom0, we need at least to deprivilege the privcmd and xenstore interfaces, this is just the first step in that direction.
---
Changes in v8:
- no need to pass the -runas option if the user requested for root
- return ERROR_FAIL from libxl__dm_runas_helper in case of errors
- return NULL from libxl__build_device_model_args_new if libxl__dm_runas_helper failed
- fix line too long
- remove setting errno
- replace retry goto loop, with a while loop
- const char * as argument to libxl__dm_runas_helper
- fix comment
Changes in v7:
- do not fall back to root if the user explicitly set b_info->device_model_user.
Changes in v6:
- add device_model_user to libxl_domain_build_info
- improve doc
- improve wording in commit message
Changes in v5:
- improve wording in doc
- fix wording in warning message
- fix example in doc
- drop xen-qemudepriv-$domname
Changes in v4:
- rename qemu-deprivilege to qemu-deprivilege.txt
- add a note about qemu-deprivilege.txt to INSTALL
- instead of xen-qemudepriv-base + $domid, try xen-qemudepriv-domid$domid
- introduce libxl__dm_runas_helper to make the code nicer
Changes in v3:
- clarify doc
- handle errno == ERANGE
--- INSTALL | 7 +++++ docs/misc/qemu-deprivilege.txt | 31 +++++++++++++++++++ tools/libxl/libxl.h | 6 ++++ tools/libxl/libxl_dm.c | 67 +++++++++++++++++++++++++++++++++++++++- tools/libxl/libxl_internal.h | 4 +++ tools/libxl/libxl_types.idl | 1 + 6 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 docs/misc/qemu-deprivilege.txt
INSTALL +7 -0
@@ -297,6 +297,13 @@ systemctl enable xendomains.service systemctl enable xen-watchdog.service +QEMU Deprivilege +================ +It is recommended to run QEMU as non-root. +See docs/misc/qemu-deprivilege.txt for an explanation on what you need +to do at installation time to run QEMU as a dedicated user. + + History of options ==================
docs/misc/qemu-deprivilege.txt new file +31 -0
@@ -0,0 +1,31 @@ +For security reasons, libxl tries to pass a non-root username to QEMU as +argument. During initialization QEMU calls setuid and setgid with the +user ID and the group ID of the user passed as argument. +Libxl looks for the following users in this order: + +1) a user named "xen-qemuuser-domid$domid", +Where $domid is the domid of the domain being created. +This requires the reservation of 65535 uids from xen-qemuuser-domid1 +to xen-qemuuser-domid65535. To use this mechanism, you might want to +create a large number of users at installation time. For example: + +for ((i=1; i<65536; i++)) +do + adduser --no-create-home --system xen-qemuuser-domid$i +done + +You might want to consider passing --group to adduser to create a new +group for each new user. + + +2) a user named "xen-qemuuser-shared" +As a fall back if both 1) fails, libxl will use a single user for +all QEMU instances. The user is named xen-qemuuser-shared. This is +less secure but still better than running QEMU as root. Using this is as +simple as creating just one more user on your host: + +adduser --no-create-home --system xen-qemuuser-shared + + +3) root +As a last resort, libxl will start QEMU as root.
tools/libxl/libxl.h +6 -0
@@ -192,6 +192,12 @@* is not present, instead of ERROR_INVAL. */ #define LIBXL_HAVE_ERROR_DOMAIN_NOTFOUND 1 + +/* libxl_domain_build_info has device_model_user to specify the user to +* run the device model with. See docs/misc/qemu-deprivilege.txt. +*/ +#define LIBXL_HAVE_DEVICE_MODEL_USER 1 +/* * libxl ABI compatibility *
tools/libxl/libxl_dm.c +66 -1
93 lines not on this page · raw message
tools/libxl/libxl_internal.h +4 -0
@@ -3692,6 +3692,10 @@ static inline void libxl__update_config_vtpm(libxl__gc *gc,*/ void libxl__bitmap_copy_best_effort (libxl__gc *gc,libxl_bitmap *dptr,const libxl_bitmap *sptr); + +#define LIBXL_QEMU_USER_PREFIX "xen-qemuuser" +#define LIBXL_QEMU_USER_BASE LIBXL_QEMU_USER_PREFIX"-domid" +#define LIBXL_QEMU_USER_SHARED LIBXL_QEMU_USER_PREFIX"-shared"#endif /*
tools/libxl/libxl_types.idl +1 -0
@@ -395,6 +395,7 @@ libxl_domain_build_info = Struct("domain_build_info",[ ("device_model", string), ("device_model_ssidref", uint32), ("device_model_ssid_label", string), + ("device_model_user", string), # extra parameters pass directly to qemu, NULL terminated ("extra", libxl_string_list),
Signature · 1 line
1.7.10.4
Quoted 3 lines · Stefano Stabellini
QEMU is going to setuid and setgid to the user ID and the group ID of the specified user, soon after initialization, before starting to deal with any guest IO.
Can you confirm that QEMU will bail if the user given via -runas doesn't exist. IOW if the user gives b_info->device_model_user != NULL we will correctly end up bailing if that specific user doesn't exist rather than running as root?
If that is the case then:
Wasn't there some code to plumb this into xl at one point? Did that get dropped along the way?
tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 942
Quoted 7 lines
[...]
tools/libxl/libxl_dm.c +6
-0+ user =NULL ; +LOG (WARN ,"Could not find user %s, starting QEMU as root" , +LIBXL_QEMU_USER_SHARED ); + +end_search: +if (user !=NULL &&strcmp (user,"root" )) {
This strcmp struck me as odd given the user = NULL just above, but this is for the case where the user explicitly requested root, right?
Quoted 3 lines
tools/libxl/libxl_dm.c +3
-0+flexarray_append (dm_args,"-runas" ); +flexarray_append (dm_args, user); + }
Quoted 8 lines · Ian Campbell
Quoted 3 lines · Stefano Stabellini
QEMU is going to setuid and setgid to the user ID and the group ID of the specified user, soon after initialization, before starting to deal with any guest IO.
Can you confirm that QEMU will bail if the user given via -runas doesn't exist.
It prints the error
User "blah" doesn't exist
and exits.
Quoted 3 lines
IOW if the user gives
b_info->device_model_user!= NULL we will correctly end up bailing if that specific user doesn't exist rather than running as root?
Yes
Quoted 3 lines
If that is the case then:
Acked-by Ian Campbell <[email protected]>
Thanks
Quoted 2 lines
Wasn't there some code to plumb this into xl at one point? Did that get dropped along the way?
device_model_user is added to the idl by this patch, I think that is enough, right?
Quoted 10 lines
tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 942
Quoted 7 lines
[...]
tools/libxl/libxl_dm.c +6
-0+ user =NULL ; +LOG (WARN ,"Could not find user %s, starting QEMU as root" , +LIBXL_QEMU_USER_SHARED ); + +end_search: +if (user !=NULL &&strcmp (user,"root" )) {This strcmp struck me as odd given the user = NULL just above, but this is for the case where the user explicitly requested root, right?
Yes, that's right
Quoted 3 lines
Quoted 3 lines
tools/libxl/libxl_dm.c +3
-0+flexarray_append (dm_args,"-runas" ); +flexarray_append (dm_args, user); + }
Quoted 5 lines · Stefano Stabellini
Quoted 2 lines
Wasn't there some code to plumb this into xl at one point? Did that get dropped along the way?
device_model_useris added to the idl by this patch, I think that is enough, right?
Depends what you mean by "enough", it adds it to the _libxl_ API, which is sufficient for the patch to be useful but it doesn't actually cause the option to be available to users of xl via a cfg file, which arguably it should.
To make it available to xl users patches to tools/libxl/xl_cmdimpl.c would be needed (plus docs changes)
Ian.
Quoted 7 lines · Ian Campbell
Quoted 5 lines · Stefano Stabellini
Quoted 2 lines
Wasn't there some code to plumb this into xl at one point? Did that get dropped along the way?
device_model_useris added to the idl by this patch, I think that is enough, right?
Quoted 7 lines
Depends what you mean by "enough", it adds it to the _libxl_ API, which is sufficient for the patch to be useful but it doesn't actually cause the option to be available to users of xl via a cfg file, which arguably it should.
To make it available to xl users patches to
tools/libxl/xl_cmdimpl.cwould be needed (plus docs changes)
I see. No, xl_cmdimpl.c support for the option was never written, as it was introduced principally for libvirt's benefit.
Quoted 15 lines · Stefano Stabellini
Quoted 14 lines · Ian Campbell
Quoted 6 lines · Stefano Stabellini
Quoted 3 lines
Wasn't there some code to plumb this into xl at one point? Did that get
dropped along the way?
device_model_useris added to the idl by this patch, I think that is enough, right?Depends what you mean by "enough", it adds it to the _libxl_ API, which is
sufficient for the patch to be useful but it doesn't actually cause the option to be available to users of xl via a cfg file, which arguably it should.
Quoted 6 lines
Quoted 3 lines
To make it available to xl users patches to
tools/libxl/xl_cmdimpl.cwould
be needed (plus docs changes)I see. No,
xl_cmdimpl.csupport for the option was never written, as it was introduced principally for libvirt's benefit.
Ah, I was wondering how this had been tested...
Ian.
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Stefano Stabellini
Quoted 12 lines · Ian Campbell
Quoted 5 lines · Stefano Stabellini
Quoted 3 lines
Wasn't there some code to plumb this into xl at one point? Did that get
dropped along the way?
device_model_useris added to the idl by this patch, I think that is enough, right?Depends what you mean by "enough", it adds it to the _libxl_ API, which is
sufficient for the patch to be useful but it doesn't actually cause the option to be available to users of xl via a cfg file, which arguably it should.
Quoted 7 lines
Quoted 5 lines
Quoted 3 lines
To make it available to xl users patches to
tools/libxl/xl_cmdimpl.cwould
be needed (plus docs changes)I see. No,
xl_cmdimpl.csupport for the option was never written, as it was introduced principally for libvirt's benefit.Ah, I was wondering how this had been tested...
Currently, there is no libivrt code to using this. But surely users would like to specify the qemu user in their xl config right?
Regards,
Jim
Quoted 2 lines · Jim Fehlig
Currently, there is no libivrt code to using this. But surely users would like to specify the qemu user in their xl config right?
Surely. Stefano can you take care of this in a followup please.
NB I went to apply this patch for now but there were several conflicts with something in staging.
Ian.