teknoraver / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
Blob Blame History Raw
From 6f8ef80bb3ba5d244a428aee200c168e809a0079 Mon Sep 17 00:00:00 2001
From: Mike Yuan <me@yhndnzj.com>
Date: Sun, 26 May 2024 00:53:46 +0800
Subject: [PATCH 2/2] core/dbus-execute: don't trigger assertion if
 WorkingDirectory="" or "-"

Follow-up for 14631951cea807de2d482a430841c604c2040718

Before this commit, if WorkingDirectory= is empty or literally "-",
'simplified' is not populated, resulting in the ASSERT_PTR
in unit_write_settingf() below getting triggered.

Also, do not accept "-", so that the parser is consistent
with load-fragment.c

Fixes #33015
---
 src/core/dbus-execute.c | 49 ++++++++++++++++++++++-------------------
 1 file changed, 26 insertions(+), 23 deletions(-)

diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
index e55fb6ee16..21c260b26b 100644
--- a/src/core/dbus-execute.c
+++ b/src/core/dbus-execute.c
@@ -2716,38 +2716,38 @@ int bus_exec_context_set_transient_property(
 
         } else if (streq(name, "WorkingDirectory")) {
                 _cleanup_free_ char *simplified = NULL;
-                bool missing_ok, is_home;
+                bool missing_ok = false, is_home = false;
                 const char *s;
 
                 r = sd_bus_message_read(message, "s", &s);
                 if (r < 0)
                         return r;
 
-                if (s[0] == '-') {
-                        missing_ok = true;
-                        s++;
-                } else
-                        missing_ok = false;
-
-                if (isempty(s))
-                        is_home = false;
-                else if (streq(s, "~"))
-                        is_home = true;
-                else {
-                        if (!path_is_absolute(s))
-                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
+                if (!isempty(s)) {
+                        if (s[0] == '-') {
+                                missing_ok = true;
+                                s++;
+                        }
 
-                        r = path_simplify_alloc(s, &simplified);
-                        if (r < 0)
-                                return r;
+                        if (streq(s, "~"))
+                                is_home = true;
+                        else {
+                                if (!path_is_absolute(s))
+                                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
+                                                                "WorkingDirectory= expects an absolute path or '~'");
 
-                        if (!path_is_normalized(simplified))
-                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects a normalized path or '~'");
+                                r = path_simplify_alloc(s, &simplified);
+                                if (r < 0)
+                                        return r;
 
-                        if (path_below_api_vfs(simplified))
-                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= may not be below /proc/, /sys/ or /dev/.");
+                                if (!path_is_normalized(simplified))
+                                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
+                                                                "WorkingDirectory= expects a normalized path or '~'");
 
-                        is_home = false;
+                                if (path_below_api_vfs(simplified))
+                                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
+                                                                "WorkingDirectory= may not be below /proc/, /sys/ or /dev/");
+                        }
                 }
 
                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
@@ -2755,7 +2755,10 @@ int bus_exec_context_set_transient_property(
                         c->working_directory_home = is_home;
                         c->working_directory_missing_ok = missing_ok;
 
-                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", c->working_directory_home ? "~" : ASSERT_PTR(c->working_directory));
+                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
+                                            "WorkingDirectory=%s%s",
+                                            c->working_directory_missing_ok ? "-" : "",
+                                            c->working_directory_home ? "~" : strempty(c->working_directory));
                 }
 
                 return 1;
-- 
2.45.1