Skip to contentSkip to conversation

run QEMU as non-root

PATCH Superseded Stefano Stabellini 2 participants 3 messages May 29, 2015 – Jun 1, 2015

v3 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 run QEMU as non-root

Try to use "xen-qemudepriv-$domname" first, then "xen-qemudepriv-base" + domid, finally "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 v3:
- clarify doc
- handle errno == ERANGE

---
 docs/misc/qemu-deprivilege   |   36 +++++++++++++++++++++++++
 tools/libxl/libxl_dm.c       |   60 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |    4 +++
 3 files changed, 100 insertions(+)
 create mode 100644 docs/misc/qemu-deprivilege
docs/misc/qemu-deprivilege new file +36 -0
new file mode 100644
index 0000000..3a61867
@@ -0,0 +1,36 @@
+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-qemudepriv-$domname"
+Where $domname is the name of the domain being created.
+To use this, you just need to create a user with the appropriate name
+for each domain. For example, if your virtual machine is named "windows":
+
+adduser --system xen-qemudepriv-windows
+
+
+2) a user named "xen-qemudepriv-base", adding domid to its uid
+This requires the reservation of 65536 uids from the uid of
+xen-qemudepriv-base to uid+65535.  For example, if xen-qemudepriv-base
+has uid 6000, and the domid is 25, libxl will try to use uid 6025. To
+use this mechanism, you might want to create a large number of users at
+installation time. For example:
+
+adduser --system xen-qemudepriv-base
+for i in '' $(seq 1 65335)
+do
+    adduser --system xen-qemudepriv-base$i
+done
+
+
+3) a user named "xen-qemudepriv-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-qemudepriv-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-qemudepriv-shared
+
+
+4) root
+As a last resort, libxl will start QEMU as root.
tools/libxl/libxl_dm.c +60 -0

78 lines not on this page · raw message

tools/libxl/libxl_internal.h +4 -0
index 8eb38aa..cf271b3 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-qemudepriv"
+#define LIBXL_QEMU_USER_BASE   LIBXL_QEMU_USER_PREFIX"-base"
+#define LIBXL_QEMU_USER_SHARED LIBXL_QEMU_USER_PREFIX"-shared"
 #endif
 
 /*
Signature · 1 line
1.7.10.4
#1 lore raw

docs/misc/qemu-deprivilege

Quoted 28 lines · Stefano Stabellini

Try to use "xen-qemudepriv-$domname" first, then "xen-qemudepriv-base" + domid, finally "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 v3:
- clarify doc
- handle errno == ERANGE

---
 docs/misc/qemu-deprivilege   |   36 +++++++++++++++++++++++++
 tools/libxl/libxl_dm.c       |   60 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |    4 +++
 3 files changed, 100 insertions(+)
 create mode 100644 docs/misc/qemu-deprivilege
docs/misc/qemu-deprivilege new file +0 -0
new file mode 100644
index 0000000..3a61867

Could you name this with a .txt or even a .markdown or .pandoc please.

I think you should also add a reference to this to the toplevel INSTALL file, so there is some hope of someone seeing it.

And I presume you will update the relevant wikipages (e.g. the installing from source one) once this change lands?

docs/misc/qemu-deprivilege line 23

Quoted 12 lines

+2) a user named "xen-qemudepriv-base", adding domid to its uid

docs/misc/qemu-deprivilege new file +11 -0
+This requires the reservation of 65536 uids from the uid of
+xen-qemudepriv-base to uid+65535.  For example, if xen-qemudepriv-base
+has uid 6000, and the domid is 25, libxl will try to use uid 6025. To
+use this mechanism, you might want to create a large number of users at
+installation time. For example:
+
+adduser --system xen-qemudepriv-base
+for i in '' $(seq 1 65335)
+do
+    adduser --system xen-qemudepriv-base$i
+done

I'm not sure that adduser is necessarily guaranteed to create users sequentially, even if it might do so most of the time. Did you check this?

Perhaps rather than doing base + domid we should just lookup the user xen-qemudepriv-domid<NNN> and document creating a user for every domid? The advantage with that is we don't need to figure out how to document the creation of 65K _consecutive_ users.

Quoted 50 lines
docs/misc/qemu-deprivilege new file +39 -0
@@ -439,6 +441,9 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     int i, connection, devid;
     uint64_t ram_size;
     const char *path, *chardev;
+    struct passwd pwd, *user = NULL;
+    char *buf = NULL;
+    long buf_size;
 
     dm_args = flexarray_make(gc, 16, 1);
 
@@ -878,6 +883,61 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         default:
             break;
         }
+
+        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);
+            goto end_search;
+        }
+        errno = 0;
+
+retry:
+        if (errno == ERANGE)
+            buf_size += 512;
+        buf = libxl__realloc(gc, buf, buf_size);
+        if (c_info->name) {
+            errno = 0;
+            getpwnam_r(libxl__sprintf(gc, "%s-%s",
+                        LIBXL_QEMU_USER_PREFIX, c_info->name),
+                    &pwd, buf, buf_size, &user);
+            if (user != NULL)
+                goto end_search;
+            if (errno == ERANGE)
+                goto retry;
+        }
+
+        errno = 0;
+        getpwnam_r(LIBXL_QEMU_USER_BASE, &pwd, buf, buf_size, &user);
+        if (user != NULL) {
+            errno = 0;
+            getpwuid_r(user->pw_uid + guest_domid, &pwd, buf, buf_size, &user);
+            if (user != NULL)
+                goto end_search;
+            if (errno == ERANGE)
+                goto retry;
+        }
+        if (errno == ERANGE)
+            goto retry;
+

docs/misc/qemu-deprivilege static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 931

Quoted 10 lines
docs/misc/qemu-deprivilege new file +10 -0
+        errno = 0;
+        getpwnam_r(LIBXL_QEMU_USER_SHARED, &pwd, buf, buf_size, &user);
+        if (user != NULL) {
+            LOG(WARN, "Could not find user %s-%s or user %s (+domid %d), falling back to %s",
+                    LIBXL_QEMU_USER_PREFIX, c_info->name, LIBXL_QEMU_USER_BASE,
+                    guest_domid, LIBXL_QEMU_USER_SHARED);
+            goto end_search;
+        }
+        if (errno == ERANGE)
+            goto retry;

This is all rather repetitive, please add a helper which takes care of allocating the buffer, retrying and logging on fail.

I don't think you need to worry about making all three attempts use the same buffer, so the helper doesn't need to have the complexity of doing that.

That would also let you avoid repeating all three searches when the last one returns ERANGE.

Quoted 17 lines
docs/misc/qemu-deprivilege new file +9 -0
+        
+
+        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->pw_name);
+        }
     }
     flexarray_append(dm_args, NULL);
     return (char **) flexarray_contents(dm_args);
tools/libxl/libxl_internal.h +0 -0
index 8eb38aa..cf271b3 100644

tools/libxl/libxl_internal.h static inline void libxl__update_config_vtpm(libxl__gc *gc, · line 3696

Quoted 5 lines
tools/libxl/libxl_internal.h +2 -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-qemudepriv"

I'd drop the "depriv" or s/depriv/user/. The fact that it has a specific user != root is enough to suggest limited privileges I think.

Quoted 5 lines
tools/libxl/libxl_internal.h +2 -0
+#define LIBXL_QEMU_USER_BASE   LIBXL_QEMU_USER_PREFIX"-base"
+#define LIBXL_QEMU_USER_SHARED LIBXL_QEMU_USER_PREFIX"-shared"
 #endif
 
 /*
#2 lore raw
Quoted 32 lines · Ian Campbell

docs/misc/qemu-deprivilege

Quoted 28 lines · Stefano Stabellini

Try to use "xen-qemudepriv-$domname" first, then "xen-qemudepriv-base" + domid, finally "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 v3:
- clarify doc
- handle errno == ERANGE

---
 docs/misc/qemu-deprivilege   |   36 +++++++++++++++++++++++++
 tools/libxl/libxl_dm.c       |   60 ++++++++++++++++++++++++++++++++++++++++++
 tools/libxl/libxl_internal.h |    4 +++
 3 files changed, 100 insertions(+)
 create mode 100644 docs/misc/qemu-deprivilege
docs/misc/qemu-deprivilege new file +0 -0
new file mode 100644
index 0000000..3a61867

Could you name this with a .txt or even a .markdown or .pandoc please.

Quoted 2 lines

I think you should also add a reference to this to the toplevel INSTALL file, so there is some hope of someone seeing it.

Good idea

Quoted 2 lines

And I presume you will update the relevant wikipages (e.g. the installing from source one) once this change lands?

Yes, I will.

Quoted 17 lines

docs/misc/qemu-deprivilege line 23

Quoted 12 lines

+2) a user named "xen-qemudepriv-base", adding domid to its uid

docs/misc/qemu-deprivilege new file +11 -0
+This requires the reservation of 65536 uids from the uid of
+xen-qemudepriv-base to uid+65535.  For example, if xen-qemudepriv-base
+has uid 6000, and the domid is 25, libxl will try to use uid 6025. To
+use this mechanism, you might want to create a large number of users at
+installation time. For example:
+
+adduser --system xen-qemudepriv-base
+for i in '' $(seq 1 65335)
+do
+    adduser --system xen-qemudepriv-base$i
+done

I'm not sure that adduser is necessarily guaranteed to create users sequentially, even if it might do so most of the time. Did you check this?

Quoted 4 lines

Perhaps rather than doing base + domid we should just lookup the user xen-qemudepriv-domid<NNN> and document creating a user for every domid? The advantage with that is we don't need to figure out how to document the creation of 65K _consecutive_ users.

That might be better actually

Quoted 64 lines
Quoted 50 lines
docs/misc/qemu-deprivilege new file +39 -0
@@ -439,6 +441,9 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
     int i, connection, devid;
     uint64_t ram_size;
     const char *path, *chardev;
+    struct passwd pwd, *user = NULL;
+    char *buf = NULL;
+    long buf_size;
 
     dm_args = flexarray_make(gc, 16, 1);
 
@@ -878,6 +883,61 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         default:
             break;
         }
+
+        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);
+            goto end_search;
+        }
+        errno = 0;
+
+retry:
+        if (errno == ERANGE)
+            buf_size += 512;
+        buf = libxl__realloc(gc, buf, buf_size);
+        if (c_info->name) {
+            errno = 0;
+            getpwnam_r(libxl__sprintf(gc, "%s-%s",
+                        LIBXL_QEMU_USER_PREFIX, c_info->name),
+                    &pwd, buf, buf_size, &user);
+            if (user != NULL)
+                goto end_search;
+            if (errno == ERANGE)
+                goto retry;
+        }
+
+        errno = 0;
+        getpwnam_r(LIBXL_QEMU_USER_BASE, &pwd, buf, buf_size, &user);
+        if (user != NULL) {
+            errno = 0;
+            getpwuid_r(user->pw_uid + guest_domid, &pwd, buf, buf_size, &user);
+            if (user != NULL)
+                goto end_search;
+            if (errno == ERANGE)
+                goto retry;
+        }
+        if (errno == ERANGE)
+            goto retry;
+

docs/misc/qemu-deprivilege static char ** libxl__build_device_model_args_new(libxl__gc *gc, · line 931

Quoted 10 lines
docs/misc/qemu-deprivilege new file +10 -0
+        errno = 0;
+        getpwnam_r(LIBXL_QEMU_USER_SHARED, &pwd, buf, buf_size, &user);
+        if (user != NULL) {
+            LOG(WARN, "Could not find user %s-%s or user %s (+domid %d), falling back to %s",
+                    LIBXL_QEMU_USER_PREFIX, c_info->name, LIBXL_QEMU_USER_BASE,
+                    guest_domid, LIBXL_QEMU_USER_SHARED);
+            goto end_search;
+        }
+        if (errno == ERANGE)
+            goto retry;

This is all rather repetitive, please add a helper which takes care of allocating the buffer, retrying and logging on fail.

Quoted 6 lines

I don't think you need to worry about making all three attempts use the same buffer, so the helper doesn't need to have the complexity of doing that.

That would also let you avoid repeating all three searches when the last one returns ERANGE.

OK, I'll do

Quoted 17 lines
Quoted 17 lines
docs/misc/qemu-deprivilege new file +9 -0
+        
+
+        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->pw_name);
+        }
     }
     flexarray_append(dm_args, NULL);
     return (char **) flexarray_contents(dm_args);
tools/libxl/libxl_internal.h +0 -0
index 8eb38aa..cf271b3 100644
@@ -3692,6 +3692,10 @@ static inline void libxl__update_config_vtpm(libxl__gc *gc,
Quoted 8 lines

tools/libxl/libxl_internal.h static inline void libxl__update_config_vtpm(libxl__gc *gc, · line 3696

Quoted 5 lines
tools/libxl/libxl_internal.h +2 -0
  */
 void libxl__bitmap_copy_best_effort(libxl__gc *gc, libxl_bitmap *dptr,
                                     const libxl_bitmap *sptr);
+
+#define LIBXL_QEMU_USER_PREFIX "xen-qemudepriv"

I'd drop the "depriv" or s/depriv/user/. The fact that it has a specific user != root is enough to suggest limited privileges I think.

I agree

Quoted 7 lines
Quoted 5 lines
tools/libxl/libxl_internal.h +2 -0
+#define LIBXL_QEMU_USER_BASE   LIBXL_QEMU_USER_PREFIX"-base"
+#define LIBXL_QEMU_USER_SHARED LIBXL_QEMU_USER_PREFIX"-shared"
 #endif
 
 /*