Debian Codegraph @

src/libsystemd/sd-id128/sd-id128.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <threads.h>
6
7 #include "sd-id128.h"
8
9 #include "alloc-util.h"
10 #include "chase.h"
11 #include "fd-util.h"
12 #include "hexdecoct.h"
13 #include "hmac.h"
14 #include "id128-util.h"
15 #include "keyring-util.h"
16 #include "path-util.h"
17 #include "random-util.h"
18 #include "stat-util.h"
19 #include "user-util.h"
20
21 _public_ char *sd_id128_to_string(sd_id128_t id, char s[static SD_ID128_STRING_MAX]) {
22 size_t k = 0;
23
24 assert_return(s, NULL);
25
26 for (size_t n = 0; n < sizeof(sd_id128_t); n++) {
27 s[k++] = hexchar(id.bytes[n] >> 4);
28 s[k++] = hexchar(id.bytes[n] & 0xF);
29 }
30
31 assert(k == SD_ID128_STRING_MAX - 1);
32 s[k] = 0;
33
34 return s;
35 }
36
37 _public_ char *sd_id128_to_uuid_string(sd_id128_t id, char s[static SD_ID128_UUID_STRING_MAX]) {
38 size_t k = 0;
39
40 assert_return(s, NULL);
41
42 /* Similar to sd_id128_to_string() but formats the result as UUID instead of plain hex chars */
43
44 for (size_t n = 0; n < sizeof(sd_id128_t); n++) {
45
46 if (IN_SET(n, 4, 6, 8, 10))
47 s[k++] = '-';
48
49 s[k++] = hexchar(id.bytes[n] >> 4);
50 s[k++] = hexchar(id.bytes[n] & 0xF);
51 }
52
53 assert(k == SD_ID128_UUID_STRING_MAX - 1);
54 s[k] = 0;
55
56 return s;
57 }
58
59 _public_ int sd_id128_from_string(const char *s, sd_id128_t *ret) {
60 size_t n, i;
61 sd_id128_t t;
62 bool is_guid = false;
63
64 assert_return(s, -EINVAL);
65
66 for (n = 0, i = 0; n < sizeof(sd_id128_t);) {
67 int a, b;
68
69 if (s[i] == '-') {
70 /* Is this a GUID? Then be nice, and skip over
71 * the dashes */
72
73 if (i == 8)
74 is_guid = true;
75 else if (IN_SET(i, 13, 18, 23)) {
76 if (!is_guid)
77 return -EINVAL;
78 } else
79 return -EINVAL;
80
81 i++;
82 continue;
83 }
84
85 a = unhexchar(s[i++]);
86 if (a < 0)
87 return -EINVAL;
88
89 b = unhexchar(s[i++]);
90 if (b < 0)
91 return -EINVAL;
92
93 t.bytes[n++] = (a << 4) | b;
94 }
95
96 if (i != (is_guid ? SD_ID128_UUID_STRING_MAX : SD_ID128_STRING_MAX) - 1)
97 return -EINVAL;
98
99 if (s[i] != 0)
100 return -EINVAL;
101
102 if (ret)
103 *ret = t;
104 return 0;
105 }
106
107 _public_ int sd_id128_string_equal(const char *s, sd_id128_t id) {
108 sd_id128_t parsed;
109 int r;
110
111 if (!s)
112 return false;
113
114 /* Checks if the specified string matches a valid string representation of the specified 128 bit ID/uuid */
115
116 r = sd_id128_from_string(s, &parsed);
117 if (r < 0)
118 return r;
119
120 return sd_id128_equal(parsed, id);
121 }
122
123 _public_ int sd_id128_get_machine(sd_id128_t *ret) {
124 static thread_local sd_id128_t saved_machine_id = {};
125 int r;
126
127 if (sd_id128_is_null(saved_machine_id)) {
128 r = id128_read("/etc/machine-id", ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, &saved_machine_id);
129 if (r < 0)
130 return r;
131 }
132
133 if (ret)
134 *ret = saved_machine_id;
135 return 0;
136 }
137
138 int id128_get_machine_at(int rfd, sd_id128_t *ret) {
139 int r;
140
141 assert(wildcard_fd_is_valid(rfd));
142
143 r = dir_fd_is_root_or_cwd(rfd);
144 if (r < 0)
145 return r;
146 if (r > 0)
147 return sd_id128_get_machine(ret);
148
149 _cleanup_close_ int fd =
150 chase_and_openat(rfd, rfd, "/etc/machine-id", CHASE_MUST_BE_REGULAR, O_RDONLY|O_CLOEXEC|O_NOCTTY, /* ret_path= */ NULL);
151 if (fd < 0)
152 return fd;
153
154 return id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret);
155 }
156
157 int id128_get_machine(const char *root, sd_id128_t *ret) {
158 if (empty_or_root(root))
159 return sd_id128_get_machine(ret);
160
161 _cleanup_close_ int fd =
162 chase_and_open("/etc/machine-id", root, CHASE_PREFIX_ROOT|CHASE_MUST_BE_REGULAR, O_RDONLY|O_CLOEXEC|O_NOCTTY, /* ret_path= */ NULL);
163 if (fd < 0)
164 return fd;
165
166 return id128_read_fd(fd, ID128_FORMAT_PLAIN | ID128_REFUSE_NULL, ret);
167 }
168
169 int id128_get_boot(sd_id128_t *ret) {
170 int r;
171
172 assert(ret);
173
174 r = id128_read("/proc/sys/kernel/random/boot_id", ID128_FORMAT_UUID | ID128_REFUSE_NULL, ret);
175 if (r == -ENOENT && proc_mounted() == 0)
176 return -ENOSYS;
177
178 return r;
179 }
180
181 _public_ int sd_id128_get_boot(sd_id128_t *ret) {
182 static thread_local sd_id128_t saved_boot_id = {};
183 int r;
184
185 if (sd_id128_is_null(saved_boot_id)) {
186 r = id128_get_boot(&saved_boot_id);
187 if (r < 0)
188 return r;
189 }
190
191 if (ret)
192 *ret = saved_boot_id;
193 return 0;
194 }
195
196 static int get_invocation_from_keyring(sd_id128_t *ret) {
197 _cleanup_free_ char *description = NULL;
198 char *d, *p, *g, *u, *e;
199 unsigned long perms;
200 key_serial_t key;
201 uid_t uid;
202 gid_t gid;
203 int r, c;
204
205 #define MAX_PERMS ((unsigned long) (KEY_POS_VIEW|KEY_POS_READ|KEY_POS_SEARCH| \
206 KEY_USR_VIEW|KEY_USR_READ|KEY_USR_SEARCH))
207
208 assert(ret);
209
210 key = request_key("user", "invocation_id", NULL, 0);
211 if (key == -1) {
212 /* Keyring support not available? Keyring access locked down? No invocation key stored? */
213 if (ERRNO_IS_NOT_SUPPORTED(errno) ||
214 ERRNO_IS_PRIVILEGE(errno) ||
215 errno == ENOKEY)
216 return -ENXIO;
217
218 return -errno;
219 }
220
221 r = keyring_describe(key, &description);
222 if (r < 0)
223 return r;
224
225 /* Chop off the final description string */
226 d = strrchr(description, ';');
227 if (!d)
228 return -EUCLEAN;
229 *d = 0;
230
231 /* Look for the permissions */
232 p = strrchr(description, ';');
233 if (!p)
234 return -EUCLEAN;
235
236 errno = 0;
237 perms = strtoul(p + 1, &e, 16);
238 if (errno > 0)
239 return -errno;
240 if (e == p + 1) /* Read at least one character */
241 return -EUCLEAN;
242 if (e != d) /* Must reached the end */
243 return -EUCLEAN;
244
245 if ((perms & ~MAX_PERMS) != 0)
246 return -EPERM;
247
248 *p = 0;
249
250 /* Look for the group ID */
251 g = strrchr(description, ';');
252 if (!g)
253 return -EUCLEAN;
254 r = parse_gid(g + 1, &gid);
255 if (r < 0)
256 return r;
257 if (gid != 0)
258 return -EPERM;
259 *g = 0;
260
261 /* Look for the user ID */
262 u = strrchr(description, ';');
263 if (!u)
264 return -EUCLEAN;
265 r = parse_uid(u + 1, &uid);
266 if (r < 0)
267 return r;
268 if (uid != 0)
269 return -EPERM;
270
271 c = keyctl(KEYCTL_READ, key, (unsigned long) ret, sizeof(sd_id128_t), 0);
272 if (c < 0)
273 return -errno;
274 if (c != sizeof(sd_id128_t))
275 return -EUCLEAN;
276
277 return 0;
278 }
279
280 static int get_invocation_from_environment(sd_id128_t *ret) {
281 const char *e;
282 int r;
283
284 assert(ret);
285
286 e = secure_getenv("INVOCATION_ID");
287 if (!e)
288 return -ENXIO;
289
290 r = sd_id128_from_string(e, ret);
291 return r == -EINVAL ? -EUCLEAN : r;
292 }
293
294 _public_ int sd_id128_get_invocation(sd_id128_t *ret) {
295 static thread_local sd_id128_t saved_invocation_id = {};
296 int r;
297
298 if (sd_id128_is_null(saved_invocation_id)) {
299 /* We first check the environment. The environment variable is primarily relevant for user
300 * services, and sufficiently safe as long as no privilege boundary is involved. */
301 r = get_invocation_from_environment(&saved_invocation_id);
302 if (r == -ENXIO)
303 /* The kernel keyring is relevant for system services (as for user services we don't
304 * store the invocation ID in the keyring, as there'd be no trust benefit in that). */
305 r = get_invocation_from_keyring(&saved_invocation_id);
306 if (r < 0)
307 return r;
308
309 if (sd_id128_is_null(saved_invocation_id))
310 return -ENOMEDIUM;
311 }
312
313 if (ret)
314 *ret = saved_invocation_id;
315 return 0;
316 }
317
318 _public_ int sd_id128_randomize(sd_id128_t *ret) {
319 sd_id128_t t;
320
321 assert_return(ret, -EINVAL);
322
323 random_bytes(&t, sizeof(t));
324
325 /* Turn this into a valid v4 UUID, to be nice. Note that we
326 * only guarantee this for newly generated UUIDs, not for
327 * pre-existing ones. */
328
329 *ret = id128_make_v4_uuid(t);
330 return 0;
331 }
332
333 _public_ int sd_id128_get_app_specific(sd_id128_t base, sd_id128_t app_id, sd_id128_t *ret) {
334 assert_cc(sizeof(sd_id128_t) < SHA256_DIGEST_SIZE); /* Check that we don't need to pad with zeros. */
335 union {
336 uint8_t hmac[SHA256_DIGEST_SIZE];
337 sd_id128_t result;
338 } buf;
339
340 assert_return(ret, -EINVAL);
341 assert_return(!sd_id128_is_null(app_id), -ENXIO);
342
343 hmac_sha256(&base, sizeof(base), &app_id, sizeof(app_id), buf.hmac);
344
345 /* Take only the first half. */
346 *ret = id128_make_v4_uuid(buf.result);
347 return 0;
348 }
349
350 _public_ int sd_id128_get_machine_app_specific(sd_id128_t app_id, sd_id128_t *ret) {
351 sd_id128_t id;
352 int r;
353
354 assert_return(ret, -EINVAL);
355
356 r = sd_id128_get_machine(&id);
357 if (r < 0)
358 return r;
359
360 return sd_id128_get_app_specific(id, app_id, ret);
361 }
362
363 _public_ int sd_id128_get_boot_app_specific(sd_id128_t app_id, sd_id128_t *ret) {
364 sd_id128_t id;
365 int r;
366
367 assert_return(ret, -EINVAL);
368
369 r = sd_id128_get_boot(&id);
370 if (r < 0)
371 return r;
372
373 return sd_id128_get_app_specific(id, app_id, ret);
374 }
375
376 _public_ int sd_id128_get_invocation_app_specific(sd_id128_t app_id, sd_id128_t *ret) {
377 sd_id128_t id;
378 int r;
379
380 assert_return(ret, -EINVAL);
381
382 r = sd_id128_get_invocation(&id);
383 if (r < 0)
384 return r;
385
386 return sd_id128_get_app_specific(id, app_id, ret);
387 }