tree-wide: add basic validation of --background argument

Check whether the argument of the `--background` option of
`systemd-run`, `run0`, `systemd-nspawn`, `systemd-vmspawn`, and
`systemd-pty-forward` is either empty or looks like an ANSI color code,
and reject invalid values when parsing arguments.

We consider a string to look like an ANSI color code if it consists of
one or more sequences of ASCII digits separated by semicolons. This
permits every valid ANSI color code, and should reject anything that
results in garbled output.
This commit is contained in:
Daniel Hast
2025-10-25 09:56:31 +09:00
committed by Yu Watanabe
parent 6fac8bb9b1
commit 101dd41cb4
9 changed files with 75 additions and 5 deletions
+8
View File
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include "alloc-util.h"
#include "ansi-color.h"
#include "bus-util.h"
#include "format-table.h"
#include "hostname-util.h"
@@ -163,3 +164,10 @@ int parse_machine_argument(const char *s, const char **ret_host, BusTransport *r
return 0;
}
int parse_background_argument(const char *s, char **arg) {
if (!isempty(s) && !looks_like_ansi_color_code(s))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid --background= argument: %s", s);
return free_and_strdup_warn(arg, s);
}
+1
View File
@@ -9,3 +9,4 @@ int parse_json_argument(const char *s, sd_json_format_flags_t *ret);
int parse_path_argument(const char *path, bool suppress_root, char **arg);
int parse_signal_argument(const char *s, int *ret);
int parse_machine_argument(const char *s, const char **ret_host, BusTransport *ret_transport);
int parse_background_argument(const char *s, char **arg);