public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Laine Stump <laine@redhat.com>
To: libvir-list@redhat.com
Cc: passt-dev@passt.top
Subject: [libvirt PATCH 1/4] util: add an API to retrieve the resolved path to a virCommand's binary
Date: Wed,  8 Mar 2023 23:49:05 -0500	[thread overview]
Message-ID: <20230309044908.29316-2-laine@redhat.com> (raw)
In-Reply-To: <20230309044908.29316-1-laine@redhat.com>

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


  reply	other threads:[~2023-03-09  4:49 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230309044908.29316-2-laine@redhat.com \
    --to=laine@redhat.com \
    --cc=libvir-list@redhat.com \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).