diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-10-17 20:40:09 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-10-26 10:52:41 +0200 |
commit | d68c645bd3323ae1f0dfcb8fd74ea6b19681db8a (patch) | |
tree | eff23147292bf0e0ee6066054b8e6e933ccd1b64 /src/basic/exec-util.c | |
parent | core: make manager_serialize() a bit easier to read by adding predicate function (diff) | |
download | systemd-d68c645bd3323ae1f0dfcb8fd74ea6b19681db8a.tar.xz systemd-d68c645bd3323ae1f0dfcb8fd74ea6b19681db8a.zip |
core: rework serialization
Let's be more careful with what we serialize: let's ensure we never
serialize strings that are longer than LONG_LINE_MAX, so that we know we
can read them back with read_line(…, LONG_LINE_MAX, …) safely.
In order to implement this all serialization functions are move to
serialize.[ch], and internally will do line size checks. We'd rather
skip a serialization line (with a loud warning) than write an overly
long line out. Of course, this is just a second level protection, after
all the data we serialize shouldn't be this long in the first place.
While we are at it also clean up logging: while serializing make sure to
always log about errors immediately. Also, (void)ify all calls we don't
expect errors in (or catch errors as part of the general
fflush_and_check() at the end.
Diffstat (limited to 'src/basic/exec-util.c')
-rw-r--r-- | src/basic/exec-util.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/basic/exec-util.c b/src/basic/exec-util.c index a6c020b0f8..2a96ecf9bb 100644 --- a/src/basic/exec-util.c +++ b/src/basic/exec-util.c @@ -9,6 +9,7 @@ #include "alloc-util.h" #include "conf-files.h" +#include "def.h" #include "env-util.h" #include "exec-util.h" #include "fd-util.h" @@ -16,6 +17,7 @@ #include "hashmap.h" #include "macro.h" #include "process-util.h" +#include "serialize.h" #include "set.h" #include "signal-util.h" #include "stat-util.h" @@ -282,7 +284,7 @@ static int gather_environment_collect(int fd, void *arg) { return -errno; } - r = serialize_environment(f, *env); + r = serialize_strv(f, "env", *env); if (r < 0) return r; @@ -294,29 +296,47 @@ static int gather_environment_collect(int fd, void *arg) { } static int gather_environment_consume(int fd, void *arg) { - char ***env = arg; _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; - int r = 0, k; + char ***env = arg; + int r = 0; /* Read a series of env=cescape(VAR=value) assignments from fd into env. */ assert(env); - f = fdopen(fd, "r"); + f = fdopen(fd, "re"); if (!f) { safe_close(fd); return -errno; } - FOREACH_LINE(line, f, return -EIO) { - truncate_nl(line); + for (;;) { + _cleanup_free_ char *line = NULL; + const char *v; + int k; - k = deserialize_environment(env, line); + k = read_line(f, LONG_LINE_MAX, &line); if (k < 0) - log_error_errno(k, "Invalid line \"%s\": %m", line); - if (k < 0 && r == 0) - r = k; + return k; + if (k == 0) + break; + + v = startswith(line, "env="); + if (!v) { + log_debug("Serialization line \"%s\" unexpectedly didn't start with \"env=\".", line); + if (r == 0) + r = -EINVAL; + + continue; + } + + k = deserialize_environment(v, env); + if (k < 0) { + log_debug_errno(k, "Invalid serialization line \"%s\": %m", line); + + if (r == 0) + r = k; + } } return r; |