Debian Codegraph @

src/core/taint.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/utsname.h>
4
5 #include "alloc-util.h"
6 #include "clock-util.h"
7 #include "constants.h"
8 #include "errno-util.h"
9 #include "fileio.h"
10 #include "fs-util.h"
11 #include "log.h"
12 #include "os-util.h"
13 #include "path-util.h"
14 #include "string-util.h"
15 #include "strv.h"
16 #include "taint.h"
17 #include "uid-range.h"
18
19 static int short_uid_gid_range(UIDRangeUsernsMode mode) {
20 _cleanup_(uid_range_freep) UIDRange *p = NULL;
21 int r;
22
23 /* Taint systemd if we the UID/GID range assigned to this environment doesn't at least cover 0…65534,
24 * i.e. from root to nobody. */
25
26 r = uid_range_load_userns(/* path= */ NULL, mode, &p);
27 if (ERRNO_IS_NEG_NOT_SUPPORTED(r))
28 return false;
29 if (r < 0)
30 return log_debug_errno(r, "Failed to load uid_map or gid_map: %m");
31
32 return !uid_range_covers(p, 0, 65535);
33 }
34
35 char** taint_strv(void) {
36 const char *stage[11] = {};
37 size_t n = 0;
38
39 /* Returns a "taint string", e.g. "local-hwclock:var-run-bad". Only things that are detected at
40 * runtime should be tagged here. For stuff that is known during compilation, emit a warning in the
41 * configuration phase. */
42
43 _cleanup_free_ char *bin = NULL, *usr_sbin = NULL, *var_run = NULL;
44
45 if (readlink_malloc("/bin", &bin) < 0 || !PATH_IN_SET(bin, "usr/bin", "/usr/bin"))
46 stage[n++] = "unmerged-usr";
47
48 /* Note that the check is different from default_PATH(), as we want to taint on uncanonical symlinks
49 * too. */
50 if (readlink_malloc("/usr/sbin", &usr_sbin) < 0 || !PATH_IN_SET(usr_sbin, "bin", "/usr/bin"))
51 stage[n++] = "unmerged-bin";
52
53 if (readlink_malloc("/var/run", &var_run) < 0 || !PATH_IN_SET(var_run, "../run", "/run"))
54 stage[n++] = "var-run-bad";
55
56 if (clock_is_localtime(NULL) > 0)
57 stage[n++] = "local-hwclock";
58
59 if (os_release_support_ended(NULL, /* quiet= */ true, NULL) > 0)
60 stage[n++] = "support-ended";
61
62 struct utsname uts;
63 assert_se(uname(&uts) >= 0);
64 if (strverscmp_improved(uts.release, KERNEL_BASELINE_VERSION) < 0)
65 stage[n++] = "old-kernel";
66
67 _cleanup_free_ char *overflowuid = NULL, *overflowgid = NULL;
68 if (read_one_line_file("/proc/sys/kernel/overflowuid", &overflowuid) >= 0 &&
69 !streq(overflowuid, "65534"))
70 stage[n++] = "overflowuid-not-65534";
71 if (read_one_line_file("/proc/sys/kernel/overflowgid", &overflowgid) >= 0 &&
72 !streq(overflowgid, "65534"))
73 stage[n++] = "overflowgid-not-65534";
74
75 if (short_uid_gid_range(UID_RANGE_USERNS_INSIDE) > 0)
76 stage[n++] = "short-uid-range";
77 if (short_uid_gid_range(GID_RANGE_USERNS_INSIDE) > 0)
78 stage[n++] = "short-gid-range";
79
80 assert(n < ELEMENTSOF(stage) - 1); /* One extra for NULL terminator */
81
82 return strv_copy((char *const *) stage);
83 }
84
85 char* taint_string(void) {
86 _cleanup_strv_free_ char **taints = NULL;
87
88 taints = taint_strv();
89 if (!taints)
90 return NULL;
91
92 return strv_join(taints, ":");
93 }