Debian Codegraph @

src/basic/uid-classification.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
4 #include <threads.h>
5
6 #include "chase.h"
7 #include "fd-util.h"
8 #include "fileio.h"
9 #include "log.h"
10 #include "string-util.h"
11 #endif
12
13 #include "uid-classification.h"
14 #include "user-util.h"
15
16 static const UGIDAllocationRange default_ugid_allocation_range = {
17 .system_alloc_uid_min = SYSTEM_ALLOC_UID_MIN,
18 .system_uid_max = SYSTEM_UID_MAX,
19 .system_alloc_gid_min = SYSTEM_ALLOC_GID_MIN,
20 .system_gid_max = SYSTEM_GID_MAX,
21 };
22
23 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
24 static int parse_alloc_uid(const char *path, const char *name, const char *t, uid_t *ret_uid) {
25 uid_t uid;
26 int r;
27
28 assert(ret_uid);
29
30 r = parse_uid(t, &uid);
31 if (r < 0)
32 return log_debug_errno(r, "%s: failed to parse %s %s, ignoring: %m", path, name, t);
33 if (uid == 0)
34 uid = 1;
35
36 *ret_uid = uid;
37 return 0;
38 }
39 #endif
40
41 int read_login_defs(UGIDAllocationRange *ret_defs, const char *path, const char *root) {
42 assert(ret_defs);
43
44 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
45 _cleanup_fclose_ FILE *f = NULL;
46 UGIDAllocationRange defs;
47 int r;
48
49 if (!path)
50 path = "/etc/login.defs";
51
52 r = chase_and_fopen_unlocked(path, root, CHASE_PREFIX_ROOT, "re", NULL, &f);
53 if (r == -ENOENT)
54 goto defaults;
55 if (r < 0)
56 return log_debug_errno(r, "Failed to open %s: %m", path);
57
58 defs = default_ugid_allocation_range;
59
60 for (;;) {
61 _cleanup_free_ char *line = NULL;
62 char *t;
63
64 r = read_line(f, LINE_MAX, &line);
65 if (r < 0)
66 return log_debug_errno(r, "Failed to read %s: %m", path);
67 if (r == 0)
68 break;
69
70 if ((t = first_word(line, "SYS_UID_MIN")))
71 (void) parse_alloc_uid(path, "SYS_UID_MIN", t, &defs.system_alloc_uid_min);
72 else if ((t = first_word(line, "SYS_UID_MAX")))
73 (void) parse_alloc_uid(path, "SYS_UID_MAX", t, &defs.system_uid_max);
74 else if ((t = first_word(line, "SYS_GID_MIN")))
75 (void) parse_alloc_uid(path, "SYS_GID_MIN", t, &defs.system_alloc_gid_min);
76 else if ((t = first_word(line, "SYS_GID_MAX")))
77 (void) parse_alloc_uid(path, "SYS_GID_MAX", t, &defs.system_gid_max);
78 }
79
80 if (defs.system_alloc_uid_min > defs.system_uid_max) {
81 log_debug("%s: SYS_UID_MIN > SYS_UID_MAX, resetting.", path);
82 defs.system_alloc_uid_min = MIN(defs.system_uid_max - 1, (uid_t) SYSTEM_ALLOC_UID_MIN);
83 /* Look at sys_uid_max to make sure sys_uid_min..sys_uid_max remains a valid range. */
84 }
85 if (defs.system_alloc_gid_min > defs.system_gid_max) {
86 log_debug("%s: SYS_GID_MIN > SYS_GID_MAX, resetting.", path);
87 defs.system_alloc_gid_min = MIN(defs.system_gid_max - 1, (gid_t) SYSTEM_ALLOC_GID_MIN);
88 /* Look at sys_gid_max to make sure sys_gid_min..sys_gid_max remains a valid range. */
89 }
90
91 *ret_defs = defs;
92 return 1;
93 defaults:
94 #endif
95 *ret_defs = default_ugid_allocation_range;
96 return 0;
97 }
98
99 const UGIDAllocationRange *acquire_ugid_allocation_range(void) {
100 #if ENABLE_COMPAT_MUTABLE_UID_BOUNDARIES
101 static thread_local UGIDAllocationRange defs;
102 static thread_local int initialized = 0; /* == 0 → not initialized yet
103 * < 0 → failure
104 * > 0 → success */
105
106 /* This function will ignore failure to read the file, so it should only be called from places where
107 * we don't crucially depend on the answer. In other words, it's appropriate for journald, but
108 * probably not for sysusers. */
109
110 if (initialized == 0)
111 initialized = read_login_defs(&defs, NULL, NULL) < 0 ? -1 : 1;
112 if (initialized < 0)
113 return &default_ugid_allocation_range;
114
115 return &defs;
116
117 #endif
118 return &default_ugid_allocation_range;
119 }
120
121 bool uid_is_system(uid_t uid) {
122 const UGIDAllocationRange *defs;
123 assert_se(defs = acquire_ugid_allocation_range());
124
125 return uid <= defs->system_uid_max;
126 }
127
128 bool gid_is_system(gid_t gid) {
129 const UGIDAllocationRange *defs;
130 assert_se(defs = acquire_ugid_allocation_range());
131
132 return gid <= defs->system_gid_max;
133 }
134
135 bool uid_for_system_journal(uid_t uid) {
136
137 /* Returns true if the specified UID shall get its data stored in the system journal. */
138
139 return uid_is_system(uid) || uid_is_dynamic(uid) || uid_is_greeter(uid) || uid == UID_NOBODY || uid_is_container(uid) || uid_is_foreign(uid);
140 }