From ce610af143b2a11011f00c65ea96f7a88453373b Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 25 Aug 2022 17:16:02 +0200 Subject: [PATCH 1/2] tmpfiles: in C lines, make missing source graceful error MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don't see where it would ever be a good thing that file copies done via tmpfiles.d/ C lines cause the tmpfiles operation to fail if their source happens to be missing. It's a problem if we can't set up the destination properly (which is the job of systemd-tmpfiles after all), but if the source is simply missing (NB: setting up the source is the job of of the rules writer) this shouldn't be a problem. This is useful for copying stuff into place if it happens to exist. For example, if systemd-stub passes additional data into the initrd's /.extra/ directory, we can copy it into a better place (e.g. /run/) with this, where it will survive the initrd→host transition. This mirrors behaviour of the recently added "^" line modifier which may be used source "w" lines from credentials – there two the behaviour is to simply skip the line if the source is missing. --- src/tmpfiles/tmpfiles.c | 7 +++++++ test/units/testsuite-22.02.sh | 2 ++ 2 files changed, 9 insertions(+) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 07432a1e51..4bbf9b4ace 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -3153,6 +3153,13 @@ static int parse_line( } path_simplify(i.argument); + + if (laccess(i.argument, F_OK) == -ENOENT) { + /* Silently skip over lines where the source file is missing. */ + log_syntax(NULL, LOG_INFO, fname, line, 0, "Copy source path '%s' does not exist, skipping line.", i.argument); + return 0; + } + break; case CREATE_CHAR_DEVICE: diff --git a/test/units/testsuite-22.02.sh b/test/units/testsuite-22.02.sh index 0719d68292..49c55f136b 100755 --- a/test/units/testsuite-22.02.sh +++ b/test/units/testsuite-22.02.sh @@ -116,6 +116,8 @@ test "$(stat -c %U:%G:%a /tmp/C/2/f1)" = "daemon:daemon:755" systemd-tmpfiles --create - < Date: Fri, 26 Aug 2022 10:12:29 +0200 Subject: [PATCH 2/2] tmpfiles: move validation/normalization of path before we use it We need to normalize/validate the path (i.e. first column) of tmpfiles.d/ lines before we start using the path, otherwise we'll use it before it's known to be good. This matters since for some line types the path is mangled into the argument column (i.e. sevents column), and we should only do that once we know it's in a good state. --- src/tmpfiles/tmpfiles.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index 4bbf9b4ace..8012d0d5df 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -3082,6 +3082,14 @@ static int parse_line( if (r < 0) return r; + if (!path_is_absolute(i.path)) { + *invalid_config = true; + return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), + "Path '%s' not absolute.", i.path); + } + + path_simplify(i.path); + switch (i.type) { case CREATE_DIRECTORY: @@ -3238,14 +3246,6 @@ static int parse_line( "Unknown command type '%c'.", (char) i.type); } - if (!path_is_absolute(i.path)) { - *invalid_config = true; - return log_syntax(NULL, LOG_ERR, fname, line, SYNTHETIC_ERRNO(EBADMSG), - "Path '%s' not absolute.", i.path); - } - - path_simplify(i.path); - if (!should_include_path(i.path)) return 0;