public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
* [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label
@ 2023-03-09  4:49 Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary Laine Stump
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: Laine Stump @ 2023-03-09  4:49 UTC (permalink / raw)
  To: libvir-list; +Cc: passt-dev

All the necessary explanation is in Path 3/4

We may want to turn on this same behavior for some other external
processes, but right now the one we need it for is passt.

Resolves: https://bugzilla.redhat.com/2172267

Laine Stump (4):
  util: add an API to retrieve the resolved path to a virCommand's
    binary
  security: make args to virSecuritySELinuxContextAddRange() const
  security: make it possible to set SELinux label of child process from
    its binary
  qemu: set SELinux label of passt process to its own binary's label

 src/libvirt_private.syms         |  1 +
 src/qemu/qemu_dbus.c             |  2 +-
 src/qemu/qemu_passt.c            |  2 +-
 src/qemu/qemu_process.c          |  2 +-
 src/qemu/qemu_security.c         |  5 ++-
 src/qemu/qemu_security.h         |  1 +
 src/qemu/qemu_slirp.c            |  2 +-
 src/qemu/qemu_tpm.c              |  3 +-
 src/qemu/qemu_vhost_user_gpu.c   |  2 +-
 src/security/security_apparmor.c |  1 +
 src/security/security_dac.c      |  1 +
 src/security/security_driver.h   |  1 +
 src/security/security_manager.c  |  8 +++-
 src/security/security_manager.h  |  1 +
 src/security/security_nop.c      |  1 +
 src/security/security_selinux.c  | 77 ++++++++++++++++++++++++++++++--
 src/security/security_stack.c    |  5 ++-
 src/util/vircommand.c            | 51 ++++++++++++++++-----
 src/util/vircommand.h            |  1 +
 19 files changed, 143 insertions(+), 24 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary
  2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
@ 2023-03-09  4:49 ` Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 2/4] security: make args to virSecuritySELinuxContextAddRange() const Laine Stump
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Laine Stump @ 2023-03-09  4:49 UTC (permalink / raw)
  To: libvir-list; +Cc: passt-dev

The binary to be exec'ed by virExec() is stored in
virCommand::args[0], and is resolved to a full absolute path (stored
in a local of virExec() just prior to execve().

Since we will have another use for the full absolute path, lets make
an API to resolve/retrieve the absolute path, and cache it in
virCommand::binaryPath so we only have to do the resolution once.

Signed-off-by: Laine Stump <laine@redhat.com>
---
 src/libvirt_private.syms |  1 +
 src/util/vircommand.c    | 51 +++++++++++++++++++++++++++++++---------
 src/util/vircommand.h    |  1 +
 3 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 4959dcec67..6f44788233 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -2091,6 +2091,7 @@ virCommandDryRunTokenNew;
 virCommandExec;
 virCommandFree;
 virCommandGetArgList;
+virCommandGetBinaryPath;
 virCommandGetGID;
 virCommandGetUID;
 virCommandHandshakeNotify;
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 1144dfb597..757d462ba1 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -88,6 +88,7 @@ struct _virCommandSendBuffer {
 struct _virCommand {
     int has_error; /* 0 on success, -1 on error  */
 
+    char *binaryPath; /* only valid if args[0] isn't absolute path */
     char **args;
     size_t nargs;
     size_t maxargs;
@@ -630,6 +631,7 @@ virCommandMassClose(virCommand *cmd,
 
 # endif /* ! __FreeBSD__ */
 
+
 /*
  * virExec:
  * @cmd virCommand * containing all information about the program to
@@ -646,22 +648,13 @@ virExec(virCommand *cmd)
     int childin = cmd->infd;
     int childout = -1;
     int childerr = -1;
-    g_autofree char *binarystr = NULL;
     const char *binary = NULL;
     int ret;
     g_autofree gid_t *groups = NULL;
     int ngroups;
 
-    if (!g_path_is_absolute(cmd->args[0])) {
-        if (!(binary = binarystr = virFindFileInPath(cmd->args[0]))) {
-            virReportSystemError(ENOENT,
-                                 _("Cannot find '%s' in path"),
-                                 cmd->args[0]);
-            return -1;
-        }
-    } else {
-        binary = cmd->args[0];
-    }
+    if (!(binary = virCommandGetBinaryPath(cmd)))
+        return -1;
 
     if (childin < 0) {
         if (getDevNull(&null) < 0)
@@ -2164,6 +2157,40 @@ virCommandGetArgList(virCommand *cmd,
 }
 
 
+/*
+ * virCommandGetBinaryPath:
+ * @cmd: virCommand* containing all information about the program
+ *
+ * If args[0] is an absolute path, return that. If not, then resolve
+ * args[0] to a full absolute path, cache that in binaryPath, and
+ * return a pointer to this resolved string. binaryPath is only set by
+ * calling this function, so even other virCommand functions should
+ * access binaryPath via this function.
+ *
+ * returns const char* with the full path of the binary to be
+ * executed, or NULL on failure.
+ */
+const char *
+virCommandGetBinaryPath(virCommand *cmd)
+{
+
+    if (cmd->binaryPath)
+        return cmd->binaryPath;
+
+    if (g_path_is_absolute(cmd->args[0]))
+        return cmd->args[0];
+
+    if (!(cmd->binaryPath = virFindFileInPath(cmd->args[0]))) {
+        virReportSystemError(ENOENT,
+                             _("Cannot find '%s' in path"),
+                             cmd->args[0]);
+        return NULL;
+    }
+
+    return cmd->binaryPath;
+}
+
+
 #ifndef WIN32
 /*
  * Manage input and output to the child process.
@@ -3015,6 +3042,8 @@ virCommandFree(virCommand *cmd)
     VIR_FORCE_CLOSE(cmd->outfd);
     VIR_FORCE_CLOSE(cmd->errfd);
 
+    g_free(cmd->binaryPath);
+
     for (i = 0; i < cmd->nargs; i++)
         g_free(cmd->args[i]);
     g_free(cmd->args);
diff --git a/src/util/vircommand.h b/src/util/vircommand.h
index e0002103b6..d51449ac90 100644
--- a/src/util/vircommand.h
+++ b/src/util/vircommand.h
@@ -170,6 +170,7 @@ int virCommandToStringBuf(virCommand *cmd,
                           bool linebreaks,
                           bool stripCommandPath);
 
+const char *virCommandGetBinaryPath(virCommand *cmd);
 int virCommandGetArgList(virCommand *cmd, char ***args);
 
 int virCommandExec(virCommand *cmd, gid_t *groups, int ngroups) G_GNUC_WARN_UNUSED_RESULT;
-- 
@@ -170,6 +170,7 @@ int virCommandToStringBuf(virCommand *cmd,
                           bool linebreaks,
                           bool stripCommandPath);
 
+const char *virCommandGetBinaryPath(virCommand *cmd);
 int virCommandGetArgList(virCommand *cmd, char ***args);
 
 int virCommandExec(virCommand *cmd, gid_t *groups, int ngroups) G_GNUC_WARN_UNUSED_RESULT;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [libvirt PATCH 2/4] security: make args to virSecuritySELinuxContextAddRange() const
  2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary Laine Stump
@ 2023-03-09  4:49 ` Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 3/4] security: make it possible to set SELinux label of child process from its binary Laine Stump
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Laine Stump @ 2023-03-09  4:49 UTC (permalink / raw)
  To: libvir-list; +Cc: passt-dev

Neither of these are modified anywhere in the function, and the
function will soon be called with an arg that actually is a const.

Signed-off-by: Laine Stump <laine@redhat.com>
---
 src/security/security_selinux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index e43962435f..cd1d9d14f7 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -523,8 +523,8 @@ virSecuritySELinuxMCSGetProcessRange(char **sens,
 }
 
 static char *
-virSecuritySELinuxContextAddRange(char *src,
-                                  char *dst)
+virSecuritySELinuxContextAddRange(const char *src,
+                                  const char *dst)
 {
     const char *str = NULL;
     char *ret = NULL;
-- 
@@ -523,8 +523,8 @@ virSecuritySELinuxMCSGetProcessRange(char **sens,
 }
 
 static char *
-virSecuritySELinuxContextAddRange(char *src,
-                                  char *dst)
+virSecuritySELinuxContextAddRange(const char *src,
+                                  const char *dst)
 {
     const char *str = NULL;
     char *ret = NULL;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [libvirt PATCH 3/4] security: make it possible to set SELinux label of child process from its binary
  2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 2/4] security: make args to virSecuritySELinuxContextAddRange() const Laine Stump
@ 2023-03-09  4:49 ` Laine Stump
  2023-03-09  4:49 ` [libvirt PATCH 4/4] qemu: set SELinux label of passt process to its own binary's label Laine Stump
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: Laine Stump @ 2023-03-09  4:49 UTC (permalink / raw)
  To: libvir-list; +Cc: passt-dev

Normally when a child process is started by libvirt, the SELinux label
of that process is set to virtd_t (plus an MCS range). In at least one
case (passt) we need for the SELinux label of a child process label to
match the label that the binary would have transitioned to
automatically if it had been run standalone (in the case of passt,
that label is passt_t).

This patch modifies virSecuritySELinuxSetChildProcessLabel() (and all
the functions above it in the call chain) so that the toplevel
function can set a new argument "useBinarySpecificLabel" to true. If
it is true, then virSecuritySELinuxSetChildProcessLabel() will call
the new function virSecuritySELinuxContextSetFromFile(), which uses
the selinux library function security_compute_create() to determine
what would be the label of the new process if it had been run
standalone (rather than being run by libvirt) - the MCS range from the
normally-used label is added to this newly derived label, and that is
what is used for the new process rather than whatever is in the
domain's security label (which will usually be virtd_t).

In order to easily verify that nothing was broken by these changes to
the call chain, all callers currently set useBinarySpecificPath =
false, so all behavior should be completely unchanged. (The next
patch will set it to true only for the case of running passt.)

https://bugzilla.redhat.com/2172267
Signed-off-by: Laine Stump <laine@redhat.com>
---
 src/qemu/qemu_dbus.c             |  2 +-
 src/qemu/qemu_passt.c            |  2 +-
 src/qemu/qemu_process.c          |  2 +-
 src/qemu/qemu_security.c         |  5 ++-
 src/qemu/qemu_security.h         |  1 +
 src/qemu/qemu_slirp.c            |  2 +-
 src/qemu/qemu_tpm.c              |  3 +-
 src/qemu/qemu_vhost_user_gpu.c   |  2 +-
 src/security/security_apparmor.c |  1 +
 src/security/security_dac.c      |  1 +
 src/security/security_driver.h   |  1 +
 src/security/security_manager.c  |  8 +++-
 src/security/security_manager.h  |  1 +
 src/security/security_nop.c      |  1 +
 src/security/security_selinux.c  | 73 +++++++++++++++++++++++++++++++-
 src/security/security_stack.c    |  5 ++-
 16 files changed, 99 insertions(+), 11 deletions(-)

diff --git a/src/qemu/qemu_dbus.c b/src/qemu/qemu_dbus.c
index a6dc802637..2e4067e704 100644
--- a/src/qemu/qemu_dbus.c
+++ b/src/qemu/qemu_dbus.c
@@ -217,7 +217,7 @@ qemuDBusStart(virQEMUDriver *driver,
     virCommandDaemonize(cmd);
     virCommandAddArgFormat(cmd, "--config-file=%s", configfile);
 
-    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, NULL) < 0)
+    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
         goto cleanup;
 
     if (virPidFileReadPath(pidfile, &cpid) < 0) {
diff --git a/src/qemu/qemu_passt.c b/src/qemu/qemu_passt.c
index 0afa8bdb3a..fd0076077e 100644
--- a/src/qemu/qemu_passt.c
+++ b/src/qemu/qemu_passt.c
@@ -281,7 +281,7 @@ qemuPasstStart(virDomainObj *vm,
     if (qemuExtDeviceLogCommand(driver, vm, cmd, "passt") < 0)
         return -1;
 
-    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, NULL) < 0)
+    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
         goto error;
 
     return 0;
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index deebd03717..be418ad8e6 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -7747,7 +7747,7 @@ qemuProcessLaunch(virConnectPtr conn,
 
     VIR_DEBUG("Setting up security labelling");
     if (qemuSecuritySetChildProcessLabel(driver->securityManager,
-                                         vm->def, cmd) < 0)
+                                         vm->def, false, cmd) < 0)
         goto cleanup;
 
     virCommandSetOutputFD(cmd, &logfile);
diff --git a/src/qemu/qemu_security.c b/src/qemu/qemu_security.c
index ee03e2225e..8bcef14d08 100644
--- a/src/qemu/qemu_security.c
+++ b/src/qemu/qemu_security.c
@@ -636,6 +636,7 @@ qemuSecurityCommandRun(virQEMUDriver *driver,
                        virCommand *cmd,
                        uid_t uid,
                        gid_t gid,
+                       bool useBinarySpecificLabel,
                        int *exitstatus)
 {
     g_autoptr(virQEMUDriverConfig) cfg = virQEMUDriverGetConfig(driver);
@@ -643,8 +644,10 @@ qemuSecurityCommandRun(virQEMUDriver *driver,
     int ret = -1;
 
     if (virSecurityManagerSetChildProcessLabel(driver->securityManager,
-                                               vm->def, cmd) < 0)
+                                               vm->def, useBinarySpecificLabel,
+                                               cmd) < 0) {
         return -1;
+    }
 
     if (uid != (uid_t) -1)
         virCommandSetUID(cmd, uid);
diff --git a/src/qemu/qemu_security.h b/src/qemu/qemu_security.h
index dc8e67cc81..10f11771b4 100644
--- a/src/qemu/qemu_security.h
+++ b/src/qemu/qemu_security.h
@@ -115,6 +115,7 @@ int qemuSecurityCommandRun(virQEMUDriver *driver,
                            virCommand *cmd,
                            uid_t uid,
                            gid_t gid,
+                           bool useBinarySpecificLabel,
                            int *exitstatus);
 
 /* Please note that for these APIs there is no wrapper yet. Do NOT blindly add
diff --git a/src/qemu/qemu_slirp.c b/src/qemu/qemu_slirp.c
index 9697542cd3..fdf0823d03 100644
--- a/src/qemu/qemu_slirp.c
+++ b/src/qemu/qemu_slirp.c
@@ -325,7 +325,7 @@ qemuSlirpStart(virDomainObj *vm,
     if (qemuExtDeviceLogCommand(driver, vm, cmd, "slirp") < 0)
         goto error;
 
-    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, NULL) < 0)
+    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
         goto error;
 
     rc = virPidFileReadPath(pidfile, &pid);
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index 982e5f13b6..abe0ba7429 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -962,8 +962,9 @@ qemuTPMEmulatorStart(virQEMUDriver *driver,
         return -1;
 
     if (qemuSecurityCommandRun(driver, vm, cmd, cfg->swtpm_user,
-                               cfg->swtpm_group, NULL) < 0)
+                               cfg->swtpm_group, false, NULL) < 0) {
         goto error;
+    }
 
     if (virPidFileReadPath(pidfile, &pid) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
diff --git a/src/qemu/qemu_vhost_user_gpu.c b/src/qemu/qemu_vhost_user_gpu.c
index 5b49ef4e28..ced41b0466 100644
--- a/src/qemu/qemu_vhost_user_gpu.c
+++ b/src/qemu/qemu_vhost_user_gpu.c
@@ -152,7 +152,7 @@ int qemuExtVhostUserGPUStart(virQEMUDriver *driver,
             virCommandAddArgFormat(cmd, "--render-node=%s", video->accel->rendernode);
     }
 
-    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, NULL) < 0)
+    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
         goto error;
 
     rc = virPidFileReadPath(pidfile, &pid);
diff --git a/src/security/security_apparmor.c b/src/security/security_apparmor.c
index b63b248975..b5642c9a28 100644
--- a/src/security/security_apparmor.c
+++ b/src/security/security_apparmor.c
@@ -570,6 +570,7 @@ AppArmorSetSecurityProcessLabel(virSecurityManager *mgr G_GNUC_UNUSED,
 static int
 AppArmorSetSecurityChildProcessLabel(virSecurityManager *mgr G_GNUC_UNUSED,
                                      virDomainDef *def,
+                                     bool useBinarySpecificLabel G_GNUC_UNUSED,
                                      virCommand *cmd)
 {
     g_autofree char *profile_name = NULL;
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 9be8f458d1..ca3f4d2dc5 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -2273,6 +2273,7 @@ virSecurityDACSetProcessLabel(virSecurityManager *mgr,
 static int
 virSecurityDACSetChildProcessLabel(virSecurityManager *mgr,
                                    virDomainDef *def,
+                                   bool useBinarySpecificLabel G_GNUC_UNUSED,
                                    virCommand *cmd)
 {
     virSecurityDACData *priv = virSecurityManagerGetPrivateData(mgr);
diff --git a/src/security/security_driver.h b/src/security/security_driver.h
index fe6982ceca..aa1fb2125d 100644
--- a/src/security/security_driver.h
+++ b/src/security/security_driver.h
@@ -96,6 +96,7 @@ typedef int (*virSecurityDomainSetProcessLabel) (virSecurityManager *mgr,
                                                  virDomainDef *def);
 typedef int (*virSecurityDomainSetChildProcessLabel) (virSecurityManager *mgr,
                                                       virDomainDef *def,
+                                                      bool useBinarySpecificLabel,
                                                       virCommand *cmd);
 typedef int (*virSecurityDomainSecurityVerify) (virSecurityManager *mgr,
                                                 virDomainDef *def);
diff --git a/src/security/security_manager.c b/src/security/security_manager.c
index 2f8e89cb04..b0578d7209 100644
--- a/src/security/security_manager.c
+++ b/src/security/security_manager.c
@@ -885,10 +885,14 @@ virSecurityManagerSetProcessLabel(virSecurityManager *mgr,
 int
 virSecurityManagerSetChildProcessLabel(virSecurityManager *mgr,
                                        virDomainDef *vm,
+                                       bool useBinarySpecificLabel,
                                        virCommand *cmd)
 {
-    if (mgr->drv->domainSetSecurityChildProcessLabel)
-       return mgr->drv->domainSetSecurityChildProcessLabel(mgr, vm, cmd);
+    if (mgr->drv->domainSetSecurityChildProcessLabel) {
+       return mgr->drv->domainSetSecurityChildProcessLabel(mgr, vm,
+                                                           useBinarySpecificLabel,
+                                                           cmd);
+    }
 
     virReportUnsupportedError();
     return -1;
diff --git a/src/security/security_manager.h b/src/security/security_manager.h
index 4afdcc167b..97add3294d 100644
--- a/src/security/security_manager.h
+++ b/src/security/security_manager.h
@@ -145,6 +145,7 @@ int virSecurityManagerSetProcessLabel(virSecurityManager *mgr,
                                       virDomainDef *def);
 int virSecurityManagerSetChildProcessLabel(virSecurityManager *mgr,
                                            virDomainDef *def,
+                                           bool useBinarySpecificLabel,
                                            virCommand *cmd);
 int virSecurityManagerVerify(virSecurityManager *mgr,
                              virDomainDef *def);
diff --git a/src/security/security_nop.c b/src/security/security_nop.c
index 0dbc547feb..1413f43d57 100644
--- a/src/security/security_nop.c
+++ b/src/security/security_nop.c
@@ -152,6 +152,7 @@ virSecurityDomainSetProcessLabelNop(virSecurityManager *mgr G_GNUC_UNUSED,
 static int
 virSecurityDomainSetChildProcessLabelNop(virSecurityManager *mgr G_GNUC_UNUSED,
                                          virDomainDef *vm G_GNUC_UNUSED,
+                                         bool useBinarySpecificLabel G_GNUC_UNUSED,
                                          virCommand *cmd G_GNUC_UNUSED)
 {
     return 0;
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index cd1d9d14f7..7f409af525 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -560,6 +560,52 @@ virSecuritySELinuxContextAddRange(const char *src,
     return ret;
 }
 
+
+static char *
+virSecuritySELinuxContextSetFromFile(const char *origLabel,
+                                     const char *binaryPath)
+{
+    g_autofree char *currentCon = NULL;
+    g_autofree char *binaryCon = NULL;
+    g_autofree char *naturalLabel = NULL;
+    g_autofree char *updatedLabel = NULL;
+
+    /* First learn what would be the context set
+     * if binaryPath was exec'ed from this process.
+     */
+    if (getcon(&currentCon) < 0) {
+        virReportSystemError(errno, "%s",
+                             _("unable to get SELinux context for current process"));
+        return NULL;
+    }
+
+    if (getfilecon(binaryPath, &binaryCon) < 0) {
+        virReportSystemError(errno, _("unable to get SELinux context for '%s'"),
+                             binaryPath);
+        return NULL;
+    }
+
+    if (security_compute_create(currentCon, binaryCon,
+                                string_to_security_class("process"),
+                                &naturalLabel) < 0) {
+        virReportSystemError(errno,
+                             _("unable create new SELinux label based on label '%s' and file '%s'"),
+                             origLabel, binaryPath);
+        return NULL;
+    }
+
+    /* now get the type from the original label
+     * (which already has proper MCS set) and add it to
+     * the new label
+     */
+    updatedLabel = virSecuritySELinuxContextAddRange(origLabel, naturalLabel);
+
+    VIR_DEBUG("original label: '%s' binary: '%s' binary-specific label: '%s'",
+              origLabel, binaryPath, NULLSTR(updatedLabel));
+    return g_steal_pointer(&updatedLabel);
+}
+
+
 static char *
 virSecuritySELinuxGenNewContext(const char *basecontext,
                                 const char *mcs,
@@ -2986,10 +3032,13 @@ virSecuritySELinuxSetProcessLabel(virSecurityManager *mgr G_GNUC_UNUSED,
 static int
 virSecuritySELinuxSetChildProcessLabel(virSecurityManager *mgr G_GNUC_UNUSED,
                                        virDomainDef *def,
+                                       bool useBinarySpecificLabel G_GNUC_UNUSED,
                                        virCommand *cmd)
 {
     /* TODO: verify DOI */
     virSecurityLabelDef *secdef;
+    g_autofree char *tmpLabel = NULL;
+    const char *label = NULL;
 
     secdef = virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAME);
     if (!secdef || !secdef->label)
@@ -3006,8 +3055,30 @@ virSecuritySELinuxSetChildProcessLabel(virSecurityManager *mgr G_GNUC_UNUSED,
             return -1;
     }
 
+    /* pick either the common label used by most binaries exec'ed by
+     * libvirt, or the specific label of this binary.
+     */
+    if (useBinarySpecificLabel) {
+        const char *binaryPath = virCommandGetBinaryPath(cmd);
+
+        if (!binaryPath)
+            return -1; /* error was already logged */
+
+        tmpLabel = virSecuritySELinuxContextSetFromFile(secdef->label,
+                                                        binaryPath);
+        if (!tmpLabel)
+            return -1;
+
+        label = tmpLabel;
+
+    } else {
+
+        label = secdef->label;
+
+    }
+
     /* save in cmd to be set after fork/before child process is exec'ed */
-    virCommandSetSELinuxLabel(cmd, secdef->label);
+    virCommandSetSELinuxLabel(cmd, label);
     return 0;
 }
 
diff --git a/src/security/security_stack.c b/src/security/security_stack.c
index 560f797030..369b5dd3a6 100644
--- a/src/security/security_stack.c
+++ b/src/security/security_stack.c
@@ -458,6 +458,7 @@ virSecurityStackSetProcessLabel(virSecurityManager *mgr,
 static int
 virSecurityStackSetChildProcessLabel(virSecurityManager *mgr,
                                      virDomainDef *vm,
+                                     bool useBinarySpecificLabel,
                                      virCommand *cmd)
 {
     virSecurityStackData *priv = virSecurityManagerGetPrivateData(mgr);
@@ -465,8 +466,10 @@ virSecurityStackSetChildProcessLabel(virSecurityManager *mgr,
     int rc = 0;
 
     for (; item; item = item->next) {
-        if (virSecurityManagerSetChildProcessLabel(item->securityManager, vm, cmd) < 0)
+        if (virSecurityManagerSetChildProcessLabel(item->securityManager, vm,
+                                                   useBinarySpecificLabel, cmd) < 0) {
             rc = -1;
+        }
     }
 
     return rc;
-- 
@@ -458,6 +458,7 @@ virSecurityStackSetProcessLabel(virSecurityManager *mgr,
 static int
 virSecurityStackSetChildProcessLabel(virSecurityManager *mgr,
                                      virDomainDef *vm,
+                                     bool useBinarySpecificLabel,
                                      virCommand *cmd)
 {
     virSecurityStackData *priv = virSecurityManagerGetPrivateData(mgr);
@@ -465,8 +466,10 @@ virSecurityStackSetChildProcessLabel(virSecurityManager *mgr,
     int rc = 0;
 
     for (; item; item = item->next) {
-        if (virSecurityManagerSetChildProcessLabel(item->securityManager, vm, cmd) < 0)
+        if (virSecurityManagerSetChildProcessLabel(item->securityManager, vm,
+                                                   useBinarySpecificLabel, cmd) < 0) {
             rc = -1;
+        }
     }
 
     return rc;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [libvirt PATCH 4/4] qemu: set SELinux label of passt process to its own binary's label
  2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
                   ` (2 preceding siblings ...)
  2023-03-09  4:49 ` [libvirt PATCH 3/4] security: make it possible to set SELinux label of child process from its binary Laine Stump
@ 2023-03-09  4:49 ` Laine Stump
  2023-03-09  5:06 ` [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
  2023-03-10 11:58 ` Michal Prívozník
  5 siblings, 0 replies; 8+ messages in thread
From: Laine Stump @ 2023-03-09  4:49 UTC (permalink / raw)
  To: libvir-list; +Cc: passt-dev

set useBinarySpecificLabel = true when calling qemuSecurityCommandRun
for the passt process, so that the new process context will include
the binary-specific label that should be used for passt (passt_t)
rather than svirt_t (as would happen if useBinarySpecificLabel was
false). (The MCS part of the label, which is common to all child
processes related to a particular qemu domain instance, is also set).

Resolves: https://bugzilla.redhat.com/2172267
Signed-off-by: Laine Stump <laine@redhat.com>
---
 src/qemu/qemu_passt.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/qemu/qemu_passt.c b/src/qemu/qemu_passt.c
index fd0076077e..2153c0ac46 100644
--- a/src/qemu/qemu_passt.c
+++ b/src/qemu/qemu_passt.c
@@ -281,7 +281,7 @@ qemuPasstStart(virDomainObj *vm,
     if (qemuExtDeviceLogCommand(driver, vm, cmd, "passt") < 0)
         return -1;
 
-    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
+    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, true, NULL) < 0)
         goto error;
 
     return 0;
-- 
@@ -281,7 +281,7 @@ qemuPasstStart(virDomainObj *vm,
     if (qemuExtDeviceLogCommand(driver, vm, cmd, "passt") < 0)
         return -1;
 
-    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, false, NULL) < 0)
+    if (qemuSecurityCommandRun(driver, vm, cmd, -1, -1, true, NULL) < 0)
         goto error;
 
     return 0;
-- 
2.39.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label
  2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
                   ` (3 preceding siblings ...)
  2023-03-09  4:49 ` [libvirt PATCH 4/4] qemu: set SELinux label of passt process to its own binary's label Laine Stump
@ 2023-03-09  5:06 ` Laine Stump
  2023-03-10 11:58 ` Michal Prívozník
  5 siblings, 0 replies; 8+ messages in thread
From: Laine Stump @ 2023-03-09  5:06 UTC (permalink / raw)
  To: libvir-list; +Cc: passt-dev

On 3/8/23 11:49 PM, Laine Stump wrote:
> All the necessary explanation is in Path 3/4
> 
> We may want to turn on this same behavior for some other external
> processes, but right now the one we need it for is passt.
> 
> Resolves: https://bugzilla.redhat.com/2172267

I forgot to mention that proper operation requires the latest updates to 
passt, as well as a patch to selinux-policy that still needs to be 
posted/merged.

> 
> Laine Stump (4):
>    util: add an API to retrieve the resolved path to a virCommand's
>      binary
>    security: make args to virSecuritySELinuxContextAddRange() const
>    security: make it possible to set SELinux label of child process from
>      its binary
>    qemu: set SELinux label of passt process to its own binary's label
> 
>   src/libvirt_private.syms         |  1 +
>   src/qemu/qemu_dbus.c             |  2 +-
>   src/qemu/qemu_passt.c            |  2 +-
>   src/qemu/qemu_process.c          |  2 +-
>   src/qemu/qemu_security.c         |  5 ++-
>   src/qemu/qemu_security.h         |  1 +
>   src/qemu/qemu_slirp.c            |  2 +-
>   src/qemu/qemu_tpm.c              |  3 +-
>   src/qemu/qemu_vhost_user_gpu.c   |  2 +-
>   src/security/security_apparmor.c |  1 +
>   src/security/security_dac.c      |  1 +
>   src/security/security_driver.h   |  1 +
>   src/security/security_manager.c  |  8 +++-
>   src/security/security_manager.h  |  1 +
>   src/security/security_nop.c      |  1 +
>   src/security/security_selinux.c  | 77 ++++++++++++++++++++++++++++++--
>   src/security/security_stack.c    |  5 ++-
>   src/util/vircommand.c            | 51 ++++++++++++++++-----
>   src/util/vircommand.h            |  1 +
>   19 files changed, 143 insertions(+), 24 deletions(-)
> 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label
  2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
                   ` (4 preceding siblings ...)
  2023-03-09  5:06 ` [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
@ 2023-03-10 11:58 ` Michal Prívozník
  2023-03-10 14:39   ` Andrea Bolognani
  5 siblings, 1 reply; 8+ messages in thread
From: Michal Prívozník @ 2023-03-10 11:58 UTC (permalink / raw)
  To: Laine Stump, libvir-list; +Cc: passt-dev

On 3/9/23 05:49, Laine Stump wrote:
> All the necessary explanation is in Path 3/4
> 
> We may want to turn on this same behavior for some other external
> processes, but right now the one we need it for is passt.
> 
> Resolves: https://bugzilla.redhat.com/2172267
> 
> Laine Stump (4):
>   util: add an API to retrieve the resolved path to a virCommand's
>     binary
>   security: make args to virSecuritySELinuxContextAddRange() const
>   security: make it possible to set SELinux label of child process from
>     its binary
>   qemu: set SELinux label of passt process to its own binary's label
> 
>  src/libvirt_private.syms         |  1 +
>  src/qemu/qemu_dbus.c             |  2 +-
>  src/qemu/qemu_passt.c            |  2 +-
>  src/qemu/qemu_process.c          |  2 +-
>  src/qemu/qemu_security.c         |  5 ++-
>  src/qemu/qemu_security.h         |  1 +
>  src/qemu/qemu_slirp.c            |  2 +-
>  src/qemu/qemu_tpm.c              |  3 +-
>  src/qemu/qemu_vhost_user_gpu.c   |  2 +-
>  src/security/security_apparmor.c |  1 +
>  src/security/security_dac.c      |  1 +
>  src/security/security_driver.h   |  1 +
>  src/security/security_manager.c  |  8 +++-
>  src/security/security_manager.h  |  1 +
>  src/security/security_nop.c      |  1 +
>  src/security/security_selinux.c  | 77 ++++++++++++++++++++++++++++++--
>  src/security/security_stack.c    |  5 ++-
>  src/util/vircommand.c            | 51 ++++++++++++++++-----
>  src/util/vircommand.h            |  1 +
>  19 files changed, 143 insertions(+), 24 deletions(-)
> 

Reviewed-by: Michal Privoznik <mprivozn@redhat.com>

Does this mean, we should lift the temporary limitation documented in
NEWS.rst?

Michal


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label
  2023-03-10 11:58 ` Michal Prívozník
@ 2023-03-10 14:39   ` Andrea Bolognani
  0 siblings, 0 replies; 8+ messages in thread
From: Andrea Bolognani @ 2023-03-10 14:39 UTC (permalink / raw)
  To: Michal Prívozník; +Cc: Laine Stump, libvir-list, passt-dev

On Fri, Mar 10, 2023 at 12:58:46PM +0100, Michal Prívozník wrote:
> On 3/9/23 05:49, Laine Stump wrote:
> > Laine Stump (4):
> >   util: add an API to retrieve the resolved path to a virCommand's
> >     binary
> >   security: make args to virSecuritySELinuxContextAddRange() const
> >   security: make it possible to set SELinux label of child process from
> >     its binary
> >   qemu: set SELinux label of passt process to its own binary's label
> >
> >  src/libvirt_private.syms         |  1 +
> >  src/qemu/qemu_dbus.c             |  2 +-
> >  src/qemu/qemu_passt.c            |  2 +-
> >  src/qemu/qemu_process.c          |  2 +-
> >  src/qemu/qemu_security.c         |  5 ++-
> >  src/qemu/qemu_security.h         |  1 +
> >  src/qemu/qemu_slirp.c            |  2 +-
> >  src/qemu/qemu_tpm.c              |  3 +-
> >  src/qemu/qemu_vhost_user_gpu.c   |  2 +-
> >  src/security/security_apparmor.c |  1 +
> >  src/security/security_dac.c      |  1 +
> >  src/security/security_driver.h   |  1 +
> >  src/security/security_manager.c  |  8 +++-
> >  src/security/security_manager.h  |  1 +
> >  src/security/security_nop.c      |  1 +
> >  src/security/security_selinux.c  | 77 ++++++++++++++++++++++++++++++--
> >  src/security/security_stack.c    |  5 ++-
> >  src/util/vircommand.c            | 51 ++++++++++++++++-----
> >  src/util/vircommand.h            |  1 +
> >  19 files changed, 143 insertions(+), 24 deletions(-)

Reviewed-by: Andrea Bolognani <abologna@redhat.com>

> Does this mean, we should lift the temporary limitation documented in
> NEWS.rst?

Yes, we should definitely include that information in the release
notes. And since I've just pushed the patch that addresses the same
limitation for AppArmor, we can mention both in the same entry.

-- 
Andrea Bolognani / Red Hat / Virtualization


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-03-10 14:39 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-09  4:49 [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
2023-03-09  4:49 ` [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary Laine Stump
2023-03-09  4:49 ` [libvirt PATCH 2/4] security: make args to virSecuritySELinuxContextAddRange() const Laine Stump
2023-03-09  4:49 ` [libvirt PATCH 3/4] security: make it possible to set SELinux label of child process from its binary Laine Stump
2023-03-09  4:49 ` [libvirt PATCH 4/4] qemu: set SELinux label of passt process to its own binary's label Laine Stump
2023-03-09  5:06 ` [libvirt PATCH 0/4] qemu/security: start passt process with correct SELinux label Laine Stump
2023-03-10 11:58 ` Michal Prívozník
2023-03-10 14:39   ` Andrea Bolognani

Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).