Adam Williamson 1f94b5
From 6f8ef80bb3ba5d244a428aee200c168e809a0079 Mon Sep 17 00:00:00 2001
Adam Williamson 1f94b5
From: Mike Yuan <me@yhndnzj.com>
Adam Williamson 1f94b5
Date: Sun, 26 May 2024 00:53:46 +0800
Adam Williamson 1f94b5
Subject: [PATCH 2/2] core/dbus-execute: don't trigger assertion if
Adam Williamson 1f94b5
 WorkingDirectory="" or "-"
Adam Williamson 1f94b5
Adam Williamson 1f94b5
Follow-up for 14631951cea807de2d482a430841c604c2040718
Adam Williamson 1f94b5
Adam Williamson 1f94b5
Before this commit, if WorkingDirectory= is empty or literally "-",
Adam Williamson 1f94b5
'simplified' is not populated, resulting in the ASSERT_PTR
Adam Williamson 1f94b5
in unit_write_settingf() below getting triggered.
Adam Williamson 1f94b5
Adam Williamson 1f94b5
Also, do not accept "-", so that the parser is consistent
Adam Williamson 1f94b5
with load-fragment.c
Adam Williamson 1f94b5
Adam Williamson 1f94b5
Fixes #33015
Adam Williamson 1f94b5
---
Adam Williamson 1f94b5
 src/core/dbus-execute.c | 49 ++++++++++++++++++++++-------------------
Adam Williamson 1f94b5
 1 file changed, 26 insertions(+), 23 deletions(-)
Adam Williamson 1f94b5
Adam Williamson 1f94b5
diff --git a/src/core/dbus-execute.c b/src/core/dbus-execute.c
Adam Williamson 1f94b5
index e55fb6ee16..21c260b26b 100644
Adam Williamson 1f94b5
--- a/src/core/dbus-execute.c
Adam Williamson 1f94b5
+++ b/src/core/dbus-execute.c
Adam Williamson 1f94b5
@@ -2716,38 +2716,38 @@ int bus_exec_context_set_transient_property(
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
         } else if (streq(name, "WorkingDirectory")) {
Adam Williamson 1f94b5
                 _cleanup_free_ char *simplified = NULL;
Adam Williamson 1f94b5
-                bool missing_ok, is_home;
Adam Williamson 1f94b5
+                bool missing_ok = false, is_home = false;
Adam Williamson 1f94b5
                 const char *s;
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
                 r = sd_bus_message_read(message, "s", &s);
Adam Williamson 1f94b5
                 if (r < 0)
Adam Williamson 1f94b5
                         return r;
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
-                if (s[0] == '-') {
Adam Williamson 1f94b5
-                        missing_ok = true;
Adam Williamson 1f94b5
-                        s++;
Adam Williamson 1f94b5
-                } else
Adam Williamson 1f94b5
-                        missing_ok = false;
Adam Williamson 1f94b5
-
Adam Williamson 1f94b5
-                if (isempty(s))
Adam Williamson 1f94b5
-                        is_home = false;
Adam Williamson 1f94b5
-                else if (streq(s, "~"))
Adam Williamson 1f94b5
-                        is_home = true;
Adam Williamson 1f94b5
-                else {
Adam Williamson 1f94b5
-                        if (!path_is_absolute(s))
Adam Williamson 1f94b5
-                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects an absolute path or '~'");
Adam Williamson 1f94b5
+                if (!isempty(s)) {
Adam Williamson 1f94b5
+                        if (s[0] == '-') {
Adam Williamson 1f94b5
+                                missing_ok = true;
Adam Williamson 1f94b5
+                                s++;
Adam Williamson 1f94b5
+                        }
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
-                        r = path_simplify_alloc(s, &simplified);
Adam Williamson 1f94b5
-                        if (r < 0)
Adam Williamson 1f94b5
-                                return r;
Adam Williamson 1f94b5
+                        if (streq(s, "~"))
Adam Williamson 1f94b5
+                                is_home = true;
Adam Williamson 1f94b5
+                        else {
Adam Williamson 1f94b5
+                                if (!path_is_absolute(s))
Adam Williamson 1f94b5
+                                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
Adam Williamson 1f94b5
+                                                                "WorkingDirectory= expects an absolute path or '~'");
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
-                        if (!path_is_normalized(simplified))
Adam Williamson 1f94b5
-                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= expects a normalized path or '~'");
Adam Williamson 1f94b5
+                                r = path_simplify_alloc(s, &simplified);
Adam Williamson 1f94b5
+                                if (r < 0)
Adam Williamson 1f94b5
+                                        return r;
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
-                        if (path_below_api_vfs(simplified))
Adam Williamson 1f94b5
-                                return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "WorkingDirectory= may not be below /proc/, /sys/ or /dev/.");
Adam Williamson 1f94b5
+                                if (!path_is_normalized(simplified))
Adam Williamson 1f94b5
+                                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
Adam Williamson 1f94b5
+                                                                "WorkingDirectory= expects a normalized path or '~'");
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
-                        is_home = false;
Adam Williamson 1f94b5
+                                if (path_below_api_vfs(simplified))
Adam Williamson 1f94b5
+                                        return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS,
Adam Williamson 1f94b5
+                                                                "WorkingDirectory= may not be below /proc/, /sys/ or /dev/");
Adam Williamson 1f94b5
+                        }
Adam Williamson 1f94b5
                 }
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
                 if (!UNIT_WRITE_FLAGS_NOOP(flags)) {
Adam Williamson 1f94b5
@@ -2755,7 +2755,10 @@ int bus_exec_context_set_transient_property(
Adam Williamson 1f94b5
                         c->working_directory_home = is_home;
Adam Williamson 1f94b5
                         c->working_directory_missing_ok = missing_ok;
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
-                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name, "WorkingDirectory=%s%s", missing_ok ? "-" : "", c->working_directory_home ? "~" : ASSERT_PTR(c->working_directory));
Adam Williamson 1f94b5
+                        unit_write_settingf(u, flags|UNIT_ESCAPE_SPECIFIERS, name,
Adam Williamson 1f94b5
+                                            "WorkingDirectory=%s%s",
Adam Williamson 1f94b5
+                                            c->working_directory_missing_ok ? "-" : "",
Adam Williamson 1f94b5
+                                            c->working_directory_home ? "~" : strempty(c->working_directory));
Adam Williamson 1f94b5
                 }
Adam Williamson 1f94b5
 
Adam Williamson 1f94b5
                 return 1;
Adam Williamson 1f94b5
-- 
Adam Williamson 1f94b5
2.45.1
Adam Williamson 1f94b5