From 2d17d699bb10803a46624569322900e9f7086406 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 9 Feb 2022 09:45:12 +0100 Subject: [PATCH] conf-parse: add generic config_parse_safe_string() helper This helper is just like config_parse_string() but does some superficial checks for control characters and quotes. In most cases we currently use config_parse_string() we probably want to use config_parse_safe_string() for safety reasons. --- src/shared/conf-parser.c | 27 +++++++++++++++++++++++++++ src/shared/conf-parser.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/shared/conf-parser.c b/src/shared/conf-parser.c index 64e67fb6f57..bc80eaffeb9 100644 --- a/src/shared/conf-parser.c +++ b/src/shared/conf-parser.c @@ -873,6 +873,33 @@ int config_parse_string( return free_and_strdup_warn(s, empty_to_null(rvalue)); } +int config_parse_safe_string( + const char *unit, + const char *filename, + unsigned line, + const char *section, + unsigned section_line, + const char *lvalue, + int ltype, + const char *rvalue, + void *data, + void *userdata) { + + char **s = data; + + assert(filename); + assert(lvalue); + assert(rvalue); + assert(data); + + if (!string_is_safe(rvalue)) { + log_syntax(unit, LOG_WARNING, filename, line, 0, "Specified string contains unsafe characters, ignoring: %s", rvalue); + return 0; + } + + return free_and_strdup_warn(s, empty_to_null(rvalue)); +} + int config_parse_path( const char *unit, const char *filename, diff --git a/src/shared/conf-parser.h b/src/shared/conf-parser.h index 1e03f93bce1..e93ea89b29f 100644 --- a/src/shared/conf-parser.h +++ b/src/shared/conf-parser.h @@ -168,6 +168,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_bool); CONFIG_PARSER_PROTOTYPE(config_parse_id128); CONFIG_PARSER_PROTOTYPE(config_parse_tristate); CONFIG_PARSER_PROTOTYPE(config_parse_string); +CONFIG_PARSER_PROTOTYPE(config_parse_safe_string); CONFIG_PARSER_PROTOTYPE(config_parse_path); CONFIG_PARSER_PROTOTYPE(config_parse_strv); CONFIG_PARSER_PROTOTYPE(config_parse_sec);