run: escape command for description
authorYu Watanabe <watanabe.yu+github@gmail.com>
Fri, 24 Nov 2023 16:05:41 +0000 (01:05 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 25 Nov 2023 03:52:35 +0000 (12:52 +0900)
The command arguments may contain spurious characters, e.g. line-break.
When we use command arguments as a description of a unit, we should
escape them.

Fixes #30187.

src/run/run.c

index fdca8fa..f97150e 100644 (file)
@@ -18,6 +18,7 @@
 #include "bus-wait-for-jobs.h"
 #include "calendarspec.h"
 #include "env-util.h"
+#include "escape.h"
 #include "exit-status.h"
 #include "fd-util.h"
 #include "format-util.h"
@@ -1932,17 +1933,19 @@ static int run(int argc, char* argv[]) {
         }
 
         if (!arg_description) {
-                description = strv_join(arg_cmdline, " ");
-                if (!description)
-                        return log_oom();
+                if (strv_isempty(arg_cmdline))
+                        arg_description = arg_unit;
+                else {
+                        _cleanup_free_ char *joined = strv_join(arg_cmdline, " ");
+                        if (!joined)
+                                return log_oom();
 
-                if (arg_unit && isempty(description)) {
-                        r = free_and_strdup(&description, arg_unit);
-                        if (r < 0)
+                        description = shell_escape(joined, "\"");
+                        if (!description)
                                 return log_oom();
-                }
 
-                arg_description = description;
+                        arg_description = description;
+                }
         }
 
         /* For backward compatibility reasons env var expansion is disabled by default for scopes, and