1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
14 #include "alloc-util.h"
16 #include "hexdecoct.h"
17 #include "id128-util.h"
22 #include "random-util.h"
23 #include "user-util.h"
26 _public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) {
29 assert_return(s, NULL);
31 for (n = 0; n < 16; n++) {
32 s[n*2] = hexchar(id.bytes[n] >> 4);
33 s[n*2+1] = hexchar(id.bytes[n] & 0xF);
41 _public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
46 assert_return(s, -EINVAL);
48 for (n = 0, i = 0; n < 16;) {
52 /* Is this a GUID? Then be nice, and skip over
57 else if (IN_SET(i, 13, 18, 23)) {
67 a = unhexchar(s[i++]);
71 b = unhexchar(s[i++]);
75 t.bytes[n++] = (a << 4) | b;
78 if (i != (is_guid ? 36 : 32))
89 _public_ int sd_id128_get_machine(sd_id128_t *ret) {
90 static thread_local sd_id128_t saved_machine_id = {};
93 assert_return(ret, -EINVAL);
95 if (sd_id128_is_null(saved_machine_id)) {
96 r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id);
100 if (sd_id128_is_null(saved_machine_id))
104 *ret = saved_machine_id;
108 _public_ int sd_id128_get_boot(sd_id128_t *ret) {
109 static thread_local sd_id128_t saved_boot_id = {};
112 assert_return(ret, -EINVAL);
114 if (sd_id128_is_null(saved_boot_id)) {
115 r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id);
120 *ret = saved_boot_id;
124 static int get_invocation_from_keyring(sd_id128_t *ret) {
126 _cleanup_free_ char *description = NULL;
127 char *d, *p, *g, *u, *e;
135 #define MAX_PERMS ((unsigned long) (KEY_POS_VIEW|KEY_POS_READ|KEY_POS_SEARCH| \
136 KEY_USR_VIEW|KEY_USR_READ|KEY_USR_SEARCH))
140 key = request_key("user", "invocation_id", NULL, 0);
142 /* Keyring support not available? No invocation key stored? */
143 if (IN_SET(errno, ENOSYS, ENOKEY))
150 description = new(char, sz);
154 c = keyctl(KEYCTL_DESCRIBE, key, (unsigned long) description, sz, 0);
158 if ((size_t) c <= sz)
165 /* The kernel returns a final NUL in the string, verify that. */
166 assert(description[c-1] == 0);
168 /* Chop off the final description string */
169 d = strrchr(description, ';');
174 /* Look for the permissions */
175 p = strrchr(description, ';');
180 perms = strtoul(p + 1, &e, 16);
183 if (e == p + 1) /* Read at least one character */
185 if (e != d) /* Must reached the end */
188 if ((perms & ~MAX_PERMS) != 0)
193 /* Look for the group ID */
194 g = strrchr(description, ';');
197 r = parse_gid(g + 1, &gid);
204 /* Look for the user ID */
205 u = strrchr(description, ';');
208 r = parse_uid(u + 1, &uid);
214 c = keyctl(KEYCTL_READ, key, (unsigned long) ret, sizeof(sd_id128_t), 0);
217 if (c != sizeof(sd_id128_t))
223 _public_ int sd_id128_get_invocation(sd_id128_t *ret) {
224 static thread_local sd_id128_t saved_invocation_id = {};
227 assert_return(ret, -EINVAL);
229 if (sd_id128_is_null(saved_invocation_id)) {
231 /* We first try to read the invocation ID from the kernel keyring. This has the benefit that it is not
232 * fakeable by unprivileged code. If the information is not available in the keyring, we use
233 * $INVOCATION_ID but ignore the data if our process was called by less privileged code
234 * (i.e. secure_getenv() instead of getenv()).
236 * The kernel keyring is only relevant for system services (as for user services we don't store the
237 * invocation ID in the keyring, as there'd be no trust benefit in that). The environment variable is
238 * primarily relevant for user services, and sufficiently safe as no privilege boundary is involved. */
240 r = get_invocation_from_keyring(&saved_invocation_id);
247 e = secure_getenv("INVOCATION_ID");
251 r = sd_id128_from_string(e, &saved_invocation_id);
257 *ret = saved_invocation_id;
261 static sd_id128_t make_v4_uuid(sd_id128_t id) {
262 /* Stolen from generate_random_uuid() of drivers/char/random.c
263 * in the kernel sources */
265 /* Set UUID version to 4 --- truly random generation */
266 id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
268 /* Set the UUID variant to DCE */
269 id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
274 _public_ int sd_id128_randomize(sd_id128_t *ret) {
278 assert_return(ret, -EINVAL);
280 r = acquire_random_bytes(&t, sizeof t, true);
284 /* Turn this into a valid v4 UUID, to be nice. Note that we
285 * only guarantee this for newly generated UUIDs, not for
286 * pre-existing ones. */
288 *ret = make_v4_uuid(t);
292 _public_ int sd_id128_get_machine_app_specific(sd_id128_t app_id, sd_id128_t *ret) {
293 _cleanup_(khash_unrefp) khash *h = NULL;
294 sd_id128_t m, result;
298 assert_return(ret, -EINVAL);
300 r = sd_id128_get_machine(&m);
304 r = khash_new_with_key(&h, "hmac(sha256)", &m, sizeof(m));
308 r = khash_put(h, &app_id, sizeof(app_id));
312 r = khash_digest_data(h, &p);
316 /* We chop off the trailing 16 bytes */
317 memcpy(&result, p, MIN(khash_get_size(h), sizeof(result)));
319 *ret = make_v4_uuid(result);