Skip to contentSkip to conversation

run QEMU as non-root

PATCH Superseded Stefano Stabellini 3 participants 6 messages Jun 30, 2015 – Jul 1, 2015

v5 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 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.

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 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 |   26 +++++++++++++++++++++
 tools/libxl/libxl_dm.c         |   50 ++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h   |    4 ++++
 4 files changed, 87 insertions(+)
 create mode 100644 docs/misc/qemu-deprivilege.txt
INSTALL +7 -0
index a0f2e7b..fe83c20 100644
@@ -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 +26 -0
new file mode 100644
index 0000000..783874b
@@ -0,0 +1,26 @@
+For security reasons, libxl tries to create QEMU as non-root.
+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 --system xen-qemuuser-domid$i
+done
+
+
+2) a user named "xen-qemuuser-shared"
+As a fall back if both 1) and 2) fail, 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 --system xen-qemuuser-shared
+
+
+3) root
+As a last resort, libxl will start QEMU as root.
tools/libxl/libxl_dm.c +50 -0

74 lines not on this page · raw message

tools/libxl/libxl_internal.h +4 -0
index 8eb38aa..7d0af40 100644
@@ -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
 
 /*
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 76 lines · Stefano Stabellini

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.

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 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 |   26 +++++++++++++++++++++
  tools/libxl/libxl_dm.c         |   50 ++++++++++++++++++++++++++++++++++++++++
  tools/libxl/libxl_internal.h   |    4 ++++
  4 files changed, 87 insertions(+)

create mode 100644 docs/misc/qemu-deprivilege.txt

INSTALL +7 -0
index a0f2e7b..fe83c20 100644
@@ -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 +16 -0
new file mode 100644
index 0000000..783874b
@@ -0,0 +1,26 @@
+For security reasons, libxl tries to create QEMU as non-root.
+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 --system xen-qemuuser-domid$i
+done
+
+
+2) a user named "xen-qemuuser-shared"

docs/misc/qemu-deprivilege.txt line 26

Quoted 10 lines
docs/misc/qemu-deprivilege.txt new file +10 -0
+As a fall back if both 1) and 2) fail, 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 --system xen-qemuuser-shared
+
+
+3) root
+As a last resort, libxl will start QEMU as root.

The more I think about it, the more I feel libxl is the wrong place for this policy. As mentioned earlier [0], libvirt allows apps to control the user:group policy. It is already supported by the qemu driver. It could be used by the libxl driver to inform libxl that the emulator (and other binaries?) it spawns should be in the context of the specified user:group.

Regards,
Jim

[0] http://lists.xenproject.org/archives/html/xen-devel/2015-05/msg02139.html

Quoted 97 lines
tools/libxl/libxl_dm.c +50 -0
index 0c6408d..66d638d 100644
@@ -19,6 +19,8 @@
 
  #include "libxl_internal.h"
  #include <xen/hvm/e820.h>
+#include <sys/types.h>
+#include <pwd.h>
 
  static const char *libxl_tapif_script(libxl__gc *gc)
  {
@@ -418,6 +420,33 @@ static char *dm_spice_options(libxl__gc *gc,
      return opt;
  }
 
+/* return 1 if the user was found, 0 if it was not, -1 on error */
+static int libxl__dm_runas_helper(libxl__gc *gc, char *username)
+{
+    struct passwd pwd, *user = NULL;
+    char *buf = NULL;
+    long buf_size;
+
+    buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+    if (buf_size < 0) {
+        LOGE(ERROR, "sysconf(_SC_GETPW_R_SIZE_MAX) returned error %ld",
+                buf_size);
+        return -1;
+    }
+
+retry:
+    buf = libxl__realloc(gc, buf, buf_size);
+    errno = 0;
+    getpwnam_r(username, &pwd, buf, buf_size, &user);
+    if (user != NULL)
+        return 1;
+    if (errno == ERANGE) {
+        buf_size += 128;
+        goto retry;
+    }
+    return 0;
+}
+
  static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                                          const char *dm, int guest_domid,
                                          const libxl_domain_config *guest_config,
@@ -439,6 +468,7 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
      int i, connection, devid;
      uint64_t ram_size;
      const char *path, *chardev;
+    char *user;
 
      dm_args = flexarray_make(gc, 16, 1);
 
@@ -878,6 +908,26 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
          default:
              break;
          }
+
+        user = libxl__sprintf(gc, "%s%d", LIBXL_QEMU_USER_BASE, guest_domid);
+        if (libxl__dm_runas_helper(gc, user) > 0)
+            goto end_search;
+
+        user = LIBXL_QEMU_USER_SHARED;
+        if (libxl__dm_runas_helper(gc, user) > 0) {
+            LOG(WARN, "Could not find user %s%d, falling back to %s",
+                    LIBXL_QEMU_USER_BASE, guest_domid, LIBXL_QEMU_USER_SHARED);
+            goto end_search;
+        }
+
+        user = NULL;
+        LOG(WARN, "Could not find user %s, starting QEMU as root", LIBXL_QEMU_USER_SHARED);
+
+end_search:
+        if (user) {
+            flexarray_append(dm_args, "-runas");
+            flexarray_append(dm_args, user);
+        }
      }
      flexarray_append(dm_args, NULL);
      return (char **) flexarray_contents(dm_args);
tools/libxl/libxl_internal.h +4 -0
index 8eb38aa..7d0af40 100644
@@ -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
 
  /*
#2 lore raw

Il 01/07/2015 02:04, Jim Fehlig ha scritto:

Quoted 91 lines · Stefano Stabellini
Quoted 80 lines

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.

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 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 |   26 +++++++++++++++++++++
  tools/libxl/libxl_dm.c         |   50 
++++++++++++++++++++++++++++++++++++++++
  tools/libxl/libxl_internal.h   |    4 ++++
  4 files changed, 87 insertions(+)

create mode 100644 docs/misc/qemu-deprivilege.txt

INSTALL +6 -0
index a0f2e7b..fe83c20 100644
@@ -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
  ==================
  diff --git a/docs/misc/qemu-deprivilege.txt 

b/docs/misc/qemu-deprivilege.txt
new file mode 100644
index 0000000..783874b

docs/misc/qemu-deprivilege.txt new file +19 -0
@@ -0,0 +1,26 @@
+For security reasons, libxl tries to create QEMU as non-root.
+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 --system xen-qemuuser-domid$i
+done
+
+
+2) a user named "xen-qemuuser-shared"
+As a fall back if both 1) and 2) fail, 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

docs/misc/qemu-deprivilege.txt line 26

Quoted 7 lines
docs/misc/qemu-deprivilege.txt new file +7 -0
+simple as creating just one more user on your host:
+
+adduser --system xen-qemuuser-shared
+
+
+3) root
+As a last resort, libxl will start QEMU as root.

The more I think about it, the more I feel libxl is the wrong place for this policy. As mentioned earlier [0], libvirt allows apps to control the user:group policy. It is already supported by the qemu driver. It could be used by the libxl driver to inform libxl that the emulator (and other binaries?) it spawns should be in the context of the specified user:group.

Quoted 2 lines

Regards,
Jim

In the patch I saw this:

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 930

Quoted 4 lines
tools/libxl/libxl_dm.c +4 -0
+        if (user) {
+            flexarray_append(dm_args, "-runas");
+            flexarray_append(dm_args, user);
+        }

So seems that already use qemu parameter for it.

What you mean is to add also the possibility to specify user for use it from libvirt and similar instead doing a different thing to support the xen specific one? If yes I also think can be a good idea. In that case can you explain a better way to do it? Probably Stabellini didn't done it because don't know other similar implementation already present in other systems that use qemu and how are good and used.

Quoted 108 lines

[0] http://lists.xenproject.org/archives/html/xen-devel/2015-05/msg02139.html

Quoted 89 lines
tools/libxl/libxl_dm.c +28 -0
index 0c6408d..66d638d 100644
@@ -19,6 +19,8 @@
    #include "libxl_internal.h"
  #include <xen/hvm/e820.h>
+#include <sys/types.h>
+#include <pwd.h>
    static const char *libxl_tapif_script(libxl__gc *gc)
  {
@@ -418,6 +420,33 @@ static char *dm_spice_options(libxl__gc *gc,
      return opt;
  }
  +/* return 1 if the user was found, 0 if it was not, -1 on error */
+static int libxl__dm_runas_helper(libxl__gc *gc, char *username)
+{
+    struct passwd pwd, *user = NULL;
+    char *buf = NULL;
+    long buf_size;
+
+    buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+    if (buf_size < 0) {
+        LOGE(ERROR, "sysconf(_SC_GETPW_R_SIZE_MAX) returned error %ld",
+                buf_size);
+        return -1;
+    }
+
+retry:
+    buf = libxl__realloc(gc, buf, buf_size);
+    errno = 0;
+    getpwnam_r(username, &pwd, buf, buf_size, &user);
+    if (user != NULL)
+        return 1;
+    if (errno == ERANGE) {
+        buf_size += 128;
+        goto retry;
+    }
+    return 0;
+}
+
  static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                                          const char *dm, int 
 guest_domid,
                                          const libxl_domain_config 

*guest_config,

tools/libxl/libxl_dm.c +1 -0
@@ -439,6 +468,7 @@ static char ** 
 libxl__build_device_model_args_new(libxl__gc *gc,
      int i, connection, devid;
      uint64_t ram_size;
      const char *path, *chardev;
+    char *user;
        dm_args = flexarray_make(gc, 16, 1);
  @@ -878,6 +908,26 @@ static char ** 

libxl__build_device_model_args_new(libxl__gc *gc,

          default:
              break;
          }
tools/libxl/libxl_dm.c +2 -0
+
+        user = libxl__sprintf(gc, "%s%d", LIBXL_QEMU_USER_BASE, 

guest_domid);

tools/libxl/libxl_dm.c +7 -0
+        if (libxl__dm_runas_helper(gc, user) > 0)
+            goto end_search;
+
+        user = LIBXL_QEMU_USER_SHARED;
+        if (libxl__dm_runas_helper(gc, user) > 0) {
+            LOG(WARN, "Could not find user %s%d, falling back to %s",
+                    LIBXL_QEMU_USER_BASE, guest_domid, 

LIBXL_QEMU_USER_SHARED);

tools/libxl/libxl_dm.c +5 -0
+            goto end_search;
+        }
+
+        user = NULL;
+        LOG(WARN, "Could not find user %s, starting QEMU as root", 

LIBXL_QEMU_USER_SHARED);

tools/libxl/libxl_dm.c +6 -0
+
+end_search:
+        if (user) {
+            flexarray_append(dm_args, "-runas");
+            flexarray_append(dm_args, user);
+        }
      }
      flexarray_append(dm_args, NULL);
      return (char **) flexarray_contents(dm_args);
tools/libxl/libxl_internal.h +0 -0
index 8eb38aa..7d0af40 100644

tools/libxl/libxl_internal.h static inline void · line 3701

Quoted 10 lines
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
    /*

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#3 lore raw
Quoted 92 lines · Fabio Fantoni

Il 01/07/2015 02:04, Jim Fehlig ha scritto:

Quoted 88 lines · Stefano Stabellini
Quoted 74 lines

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.

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 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 |   26 +++++++++++++++++++++
  tools/libxl/libxl_dm.c         |   50 
++++++++++++++++++++++++++++++++++++++++
  tools/libxl/libxl_internal.h   |    4 ++++
  4 files changed, 87 insertions(+)

create mode 100644 docs/misc/qemu-deprivilege.txt

INSTALL +6 -0
index a0f2e7b..fe83c20 100644
@@ -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
  ==================
  diff --git a/docs/misc/qemu-deprivilege.txt b/docs/misc/qemu-deprivilege.txt

new file mode 100644
index 0000000..783874b

docs/misc/qemu-deprivilege.txt new file +16 -0
@@ -0,0 +1,26 @@
+For security reasons, libxl tries to create QEMU as non-root.
+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 --system xen-qemuuser-domid$i
+done
+
+
+2) a user named "xen-qemuuser-shared"

docs/misc/qemu-deprivilege.txt line 26

Quoted 10 lines
docs/misc/qemu-deprivilege.txt new file +10 -0
+As a fall back if both 1) and 2) fail, 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 --system xen-qemuuser-shared
+
+
+3) root
+As a last resort, libxl will start QEMU as root.

The more I think about it, the more I feel libxl is the wrong place for this policy. As mentioned earlier [0], libvirt allows apps to control the user:group policy. It is already supported by the qemu driver. It could be used by the libxl driver to inform libxl that the emulator (and other binaries?) it spawns should be in the context of the specified user:group.

Quoted 2 lines

Regards,
Jim

Quoted 8 lines

In the patch I saw this:

tools/libxl/libxl_dm.c static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 930

Quoted 4 lines
tools/libxl/libxl_dm.c +4 -0
+        if (user) {
+            flexarray_append(dm_args, "-runas");
+            flexarray_append(dm_args, user);
+        }

So seems that already use qemu parameter for it.

What you mean is to add also the possibility to specify user for use it from libvirt and similar instead doing a different thing to support the xen specific one?

Yes.

Quoted 1 line

If yes I also think can be a good idea. In that case can you explain a better way to do it? Probably Stabellini didn't done it because don't know other similar implementation already present in other systems that use qemu and how are good and used.

I replied to Stefano's mail with some additional thoughts and ideas, but I think he understands.

Regards,
Jim

#4 lore raw
In reply to Jim Fehlig
Quoted 93 lines · Jim Fehlig
Quoted 75 lines · Stefano Stabellini

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.

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 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 |   26 +++++++++++++++++++++
  tools/libxl/libxl_dm.c         |   50
++++++++++++++++++++++++++++++++++++++++
  tools/libxl/libxl_internal.h   |    4 ++++
  4 files changed, 87 insertions(+)

create mode 100644 docs/misc/qemu-deprivilege.txt

INSTALL +6 -0
index a0f2e7b..fe83c20 100644
@@ -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
  ==================
  diff --git a/docs/misc/qemu-deprivilege.txt

b/docs/misc/qemu-deprivilege.txt
new file mode 100644
index 0000000..783874b

docs/misc/qemu-deprivilege.txt new file +16 -0
@@ -0,0 +1,26 @@
+For security reasons, libxl tries to create QEMU as non-root.
+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 --system xen-qemuuser-domid$i
+done
+
+
+2) a user named "xen-qemuuser-shared"

docs/misc/qemu-deprivilege.txt line 26

Quoted 10 lines
docs/misc/qemu-deprivilege.txt new file +10 -0
+As a fall back if both 1) and 2) fail, 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 --system xen-qemuuser-shared
+
+
+3) root
+As a last resort, libxl will start QEMU as root.

The more I think about it, the more I feel libxl is the wrong place for this policy. As mentioned earlier [0], libvirt allows apps to control the user:group policy. It is already supported by the qemu driver. It could be used by the libxl driver to inform libxl that the emulator (and other binaries?) it spawns should be in the context of the specified user:group.

Quoted 4 lines

Regards,
Jim

[0] http://lists.xenproject.org/archives/html/xen-devel/2015-05/msg02139.html

Are you suggesting to expose a per-domain user:group setting that can be passed down by libvirt? Maybe something under libxl_domain_build_info.u.hvm?

Then I could move the code below to xl_cmdimpl.c, making it xl specific (rather than libxl).

Quoted 99 lines
Quoted 98 lines
tools/libxl/libxl_dm.c +28 -0
index 0c6408d..66d638d 100644
@@ -19,6 +19,8 @@
    #include "libxl_internal.h"
  #include <xen/hvm/e820.h>
+#include <sys/types.h>
+#include <pwd.h>
    static const char *libxl_tapif_script(libxl__gc *gc)
  {
@@ -418,6 +420,33 @@ static char *dm_spice_options(libxl__gc *gc,
      return opt;
  }
  +/* return 1 if the user was found, 0 if it was not, -1 on error */
+static int libxl__dm_runas_helper(libxl__gc *gc, char *username)
+{
+    struct passwd pwd, *user = NULL;
+    char *buf = NULL;
+    long buf_size;
+
+    buf_size = sysconf(_SC_GETPW_R_SIZE_MAX);
+    if (buf_size < 0) {
+        LOGE(ERROR, "sysconf(_SC_GETPW_R_SIZE_MAX) returned error %ld",
+                buf_size);
+        return -1;
+    }
+
+retry:
+    buf = libxl__realloc(gc, buf, buf_size);
+    errno = 0;
+    getpwnam_r(username, &pwd, buf, buf_size, &user);
+    if (user != NULL)
+        return 1;
+    if (errno == ERANGE) {
+        buf_size += 128;
+        goto retry;
+    }
+    return 0;
+}
+
  static char ** libxl__build_device_model_args_new(libxl__gc *gc,
                                          const char *dm, int guest_domid,
                                          const libxl_domain_config

*guest_config,

tools/libxl/libxl_dm.c +1 -0
@@ -439,6 +468,7 @@ static char **
 libxl__build_device_model_args_new(libxl__gc *gc,
      int i, connection, devid;
      uint64_t ram_size;
      const char *path, *chardev;
+    char *user;
        dm_args = flexarray_make(gc, 16, 1);
  @@ -878,6 +908,26 @@ static char **

libxl__build_device_model_args_new(libxl__gc *gc,

          default:
              break;
          }
tools/libxl/libxl_dm.c +2 -0
+
+        user = libxl__sprintf(gc, "%s%d", LIBXL_QEMU_USER_BASE,

guest_domid);

tools/libxl/libxl_dm.c +7 -0
+        if (libxl__dm_runas_helper(gc, user) > 0)
+            goto end_search;
+
+        user = LIBXL_QEMU_USER_SHARED;
+        if (libxl__dm_runas_helper(gc, user) > 0) {
+            LOG(WARN, "Could not find user %s%d, falling back to %s",
+                    LIBXL_QEMU_USER_BASE, guest_domid,

LIBXL_QEMU_USER_SHARED);

tools/libxl/libxl_dm.c +5 -0
+            goto end_search;
+        }
+
+        user = NULL;
+        LOG(WARN, "Could not find user %s, starting QEMU as root",

LIBXL_QEMU_USER_SHARED);

tools/libxl/libxl_dm.c +6 -0
+
+end_search:
+        if (user) {
+            flexarray_append(dm_args, "-runas");
+            flexarray_append(dm_args, user);
+        }
      }
      flexarray_append(dm_args, NULL);
      return (char **) flexarray_contents(dm_args);
tools/libxl/libxl_internal.h +4 -0
index 8eb38aa..7d0af40 100644
@@ -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
    /*
#5 lore raw
Quoted 2 lines · Stefano Stabellini
Quoted 1 line · Jim Fehlig

On 06/30/2015 07:55 AM, Stefano Stabellini wrote:

[...]

Quoted 38 lines
Quoted 38 lines
Quoted 22 lines

b/docs/misc/qemu-deprivilege.txt
new file mode 100644
index 0000000..783874b

docs/misc/qemu-deprivilege.txt new file +16 -0
@@ -0,0 +1,26 @@
+For security reasons, libxl tries to create QEMU as non-root.
+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 --system xen-qemuuser-domid$i
+done
+
+
+2) a user named "xen-qemuuser-shared"

docs/misc/qemu-deprivilege.txt line 26

Quoted 10 lines
docs/misc/qemu-deprivilege.txt new file +10 -0
+As a fall back if both 1) and 2) fail, 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 --system xen-qemuuser-shared
+
+
+3) root
+As a last resort, libxl will start QEMU as root.

The more I think about it, the more I feel libxl is the wrong place for this policy. As mentioned earlier [0], libvirt allows apps to control the user:group policy. It is already supported by the qemu driver. It could be used by the libxl driver to inform libxl that the emulator (and other binaries?) it spawns should be in the context of the specified user:group.

Quoted 7 lines
Quoted 4 lines

Regards,
Jim

[0] http://lists.xenproject.org/archives/html/xen-devel/2015-05/msg02139.html

Are you suggesting to expose a per-domain user:group setting that can be passed down by libvirt? Maybe something under libxl_domain_build_info.u.hvm?

Yes. But should it be restricted to .hvm? Maybe it will be useful now (or in the future) for pv too. E.g. older pv bootloaders that run in dom0 could be executed as the user:group.

Quoted 2 lines

Then I could move the code below to xl_cmdimpl.c, making it xl specific (rather than libxl).

Right. Which means adding 'user=' and 'group=' to xl.cfg, correct? If so, perhaps xl.conf should provide "xl-wide" defaults, applicable to all domains created with xl that don't contain explicit 'user=' and 'group='.

Regards,
Jim