Skip to contentSkip to conversation

run QEMU as non-root

RFC PATCH Superseded Stefano Stabellini 2 participants 4 messages May 14, 2015 – May 15, 2015

v1 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 run QEMU as non-root

Run QEMU as non-root. Starting from uid 6000, the chosen uid is base+domid. If the uid doesn't exist, try just 6000. This is less
secure: ideally we don't want different domains having their QEMUs running with the same uid. Finally if uid 6000 doesn't exist either, fall back to running QEMU as root.

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.

---
 tools/libxl/libxl_dm.c       |   17 +++++++++++++++++
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 19 insertions(+)
tools/libxl/libxl_dm.c +17 -0
index 0c6408d..942c5df 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)
 {
@@ -439,6 +441,7 @@ 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 *user = NULL;
 
     dm_args = flexarray_make(gc, 16, 1);
 
@@ -878,6 +881,20 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         default:
             break;
         }
+
+        user = getpwuid(LIBXL_QEMU_BASE_UID + guest_domid);
+        if (user == NULL) {
+            LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "Could not find uid %d, falling back to %d\n",
+                    LIBXL_QEMU_BASE_UID + guest_domid, LIBXL_QEMU_BASE_UID);
+            user = getpwuid(LIBXL_QEMU_BASE_UID);
+            if (user == NULL)
+                LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "Could not find uid %d, starting QEMU as root\n",
+                    LIBXL_QEMU_BASE_UID);
+        }
+        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 +2 -0
index 8eb38aa..065ff98 100644
@@ -3692,6 +3692,8 @@ 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_BASE_UID (6000)
 #endif
 
 /*
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 5 lines · Stefano Stabellini

Run QEMU as non-root. Starting from uid 6000, the chosen uid is base+domid. If the uid doesn't exist, try just 6000. This is less
secure: ideally we don't want different domains having their QEMUs running with the same uid. Finally if uid 6000 doesn't exist either, fall back to running QEMU as root.

We can't just pick a random number like that, especially not hardcoded.

You should call getpwent_r.

IIRC what was suggested yesterday IRL was to look for, in order, users named (prefixes TBD):

xen-qemudepriv-$domname
xen-qemudepriv-base (+domid) xen-qemudepriv-shared (all qemu in same non-root uid)

If none of those are present then the qemu should not be deprivileged. There should probably be a nob to fiddle to allow the fallback to be to fail to create the domain.

Then the admin/postinst can do as they prefer:

        adduser --system xen-qemudepriv-mysecuredomain
        
        for i in '' $(seq 1 65335) ; do
              adduser --system xen-qemudepriv-base$i
        done
        
        adduser --system xen-qemudepriv-shared

(and can combine the first with either the second or third as they desire)

There needs to be a documentation update associated with this.

Quoted 35 lines

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.

---
 tools/libxl/libxl_dm.c       |   17 +++++++++++++++++
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 19 insertions(+)
tools/libxl/libxl_dm.c +3 -0
index 0c6408d..942c5df 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)
 {
@@ -439,6 +441,7 @@ 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 *user = NULL;
 
     dm_args = flexarray_make(gc, 16, 1);
 

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

Quoted 8 lines
tools/libxl/libxl_dm.c +5 -0
@@ -878,6 +881,20 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         default:
             break;
         }
+
+        user = getpwuid(LIBXL_QEMU_BASE_UID + guest_domid);
+        if (user == NULL) {
+            LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "Could not find uid %d, falling back to %d\n",
+                    LIBXL_QEMU_BASE_UID + guest_domid, LIBXL_QEMU_BASE_UID);

LOG(WARNING, "Could not..")

And *LOG* appends \n itself.

Quoted 25 lines
tools/libxl/libxl_dm.c +9 -0
+            user = getpwuid(LIBXL_QEMU_BASE_UID);
+            if (user == NULL)
+                LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "Could not find uid %d, starting QEMU as root\n",
+                    LIBXL_QEMU_BASE_UID);
+        }
+        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 +2 -0
index 8eb38aa..065ff98 100644
@@ -3692,6 +3692,8 @@ 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_BASE_UID (6000)
 #endif
 
 /*
#2 lore raw
Quoted 10 lines · Ian Campbell
Quoted 5 lines · Stefano Stabellini

Run QEMU as non-root. Starting from uid 6000, the chosen uid is base+domid. If the uid doesn't exist, try just 6000. This is less
secure: ideally we don't want different domains having their QEMUs running with the same uid. Finally if uid 6000 doesn't exist either, fall back to running QEMU as root.

We can't just pick a random number like that, especially not hardcoded.

You should call getpwent_r.

Are you suggesting to go over the full list of records in passwd? It doesn't sound like a good idea. Did you mean getpwnam_r?

Quoted 8 lines

IIRC what was suggested yesterday IRL was to look for, in order, users named (prefixes TBD):

xen-qemudepriv-$domname
xen-qemudepriv-base (+domid) xen-qemudepriv-shared (all qemu in same non-root uid)

If none of those are present then the qemu should not be deprivileged.

This is better. I'll go for this.

Quoted 2 lines

There should probably be a nob to fiddle to allow the fallback to be to fail to create the domain.

I agree, but it is a bit too early for that.

Quoted 10 lines

Then the admin/postinst can do as they prefer:

        adduser --system xen-qemudepriv-mysecuredomain
        
        for i in '' $(seq 1 65335) ; do
              adduser --system xen-qemudepriv-base$i
        done
        
        adduser --system xen-qemudepriv-shared
Quoted 4 lines

(and can combine the first with either the second or third as they desire)

There needs to be a documentation update associated with this.

OK

Quoted 46 lines
Quoted 35 lines

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.

---
 tools/libxl/libxl_dm.c       |   17 +++++++++++++++++
 tools/libxl/libxl_internal.h |    2 ++
 2 files changed, 19 insertions(+)
tools/libxl/libxl_dm.c +3 -0
index 0c6408d..942c5df 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)
 {
@@ -439,6 +441,7 @@ 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 *user = NULL;
 
     dm_args = flexarray_make(gc, 16, 1);
 

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

Quoted 8 lines
tools/libxl/libxl_dm.c +5 -0
@@ -878,6 +881,20 @@ static char ** libxl__build_device_model_args_new(libxl__gc *gc,
         default:
             break;
         }
+
+        user = getpwuid(LIBXL_QEMU_BASE_UID + guest_domid);
+        if (user == NULL) {
+            LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "Could not find uid %d, falling back to %d\n",
+                    LIBXL_QEMU_BASE_UID + guest_domid, LIBXL_QEMU_BASE_UID);

LOG(WARNING, "Could not..")

Quoted 1 line

And *LOG* appends \n itself.

OK

Quoted 27 lines
Quoted 25 lines
tools/libxl/libxl_dm.c +9 -0
+            user = getpwuid(LIBXL_QEMU_BASE_UID);
+            if (user == NULL)
+                LIBXL__LOG(ctx, LIBXL__LOG_WARNING, "Could not find uid %d, starting QEMU as root\n",
+                    LIBXL_QEMU_BASE_UID);
+        }
+        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 +2 -0
index 8eb38aa..065ff98 100644
@@ -3692,6 +3692,8 @@ 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_BASE_UID (6000)
 #endif
 
 /*
#3 lore raw
Quoted 8 lines · Stefano Stabellini
Quoted 7 lines · Ian Campbell
Quoted 5 lines · Stefano Stabellini

Run QEMU as non-root. Starting from uid 6000, the chosen uid is base+domid. If the uid doesn't exist, try just 6000. This is less
secure: ideally we don't want different domains having their QEMUs running with the same uid. Finally if uid 6000 doesn't exist either, fall back to running QEMU as root.

Quoted 6 lines
Quoted 3 lines

We can't just pick a random number like that, especially not hardcoded.

You should call getpwent_r.

Are you suggesting to go over the full list of records in passwd? It doesn't sound like a good idea. Did you mean getpwnam_r?

yes, sorry.