1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright 2011 Lennart Poettering
12 #include "alloc-util.h"
14 #include "hexdecoct.h"
15 #include "id128-util.h"
20 #include "random-util.h"
21 #include "user-util.h"
24 _public_ char *sd_id128_to_string(sd_id128_t id, char s[SD_ID128_STRING_MAX]) {
27 assert_return(s, NULL);
29 for (n = 0; n < 16; n++) {
30 s[n*2] = hexchar(id.bytes[n] >> 4);
31 s[n*2+1] = hexchar(id.bytes[n] & 0xF);
39 _public_ int sd_id128_from_string(const char s[], sd_id128_t *ret) {
44 assert_return(s, -EINVAL);
46 for (n = 0, i = 0; n < 16;) {
50 /* Is this a GUID? Then be nice, and skip over
55 else if (IN_SET(i, 13, 18, 23)) {
65 a = unhexchar(s[i++]);
69 b = unhexchar(s[i++]);
73 t.bytes[n++] = (a << 4) | b;
76 if (i != (is_guid ? 36 : 32))
87 _public_ int sd_id128_get_machine(sd_id128_t *ret) {
88 static thread_local sd_id128_t saved_machine_id = {};
91 assert_return(ret, -EINVAL);
93 if (sd_id128_is_null(saved_machine_id)) {
94 r = id128_read("/etc/machine-id", ID128_PLAIN, &saved_machine_id);
98 if (sd_id128_is_null(saved_machine_id))
102 *ret = saved_machine_id;
106 _public_ int sd_id128_get_boot(sd_id128_t *ret) {
107 static thread_local sd_id128_t saved_boot_id = {};
110 assert_return(ret, -EINVAL);
112 if (sd_id128_is_null(saved_boot_id)) {
113 r = id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID, &saved_boot_id);
118 *ret = saved_boot_id;
122 static int get_invocation_from_keyring(sd_id128_t *ret) {
124 _cleanup_free_ char *description = NULL;
125 char *d, *p, *g, *u, *e;
133 #define MAX_PERMS ((unsigned long) (KEY_POS_VIEW|KEY_POS_READ|KEY_POS_SEARCH| \
134 KEY_USR_VIEW|KEY_USR_READ|KEY_USR_SEARCH))
138 key = request_key("user", "invocation_id", NULL, 0);
140 /* Keyring support not available? No invocation key stored? */
141 if (IN_SET(errno, ENOSYS, ENOKEY))
148 description = new(char, sz);
152 c = keyctl(KEYCTL_DESCRIBE, key, (unsigned long) description, sz, 0);
156 if ((size_t) c <= sz)
163 /* The kernel returns a final NUL in the string, verify that. */
164 assert(description[c-1] == 0);
166 /* Chop off the final description string */
167 d = strrchr(description, ';');
172 /* Look for the permissions */
173 p = strrchr(description, ';');
178 perms = strtoul(p + 1, &e, 16);
181 if (e == p + 1) /* Read at least one character */
183 if (e != d) /* Must reached the end */
186 if ((perms & ~MAX_PERMS) != 0)
191 /* Look for the group ID */
192 g = strrchr(description, ';');
195 r = parse_gid(g + 1, &gid);
202 /* Look for the user ID */
203 u = strrchr(description, ';');
206 r = parse_uid(u + 1, &uid);
212 c = keyctl(KEYCTL_READ, key, (unsigned long) ret, sizeof(sd_id128_t), 0);
215 if (c != sizeof(sd_id128_t))
221 _public_ int sd_id128_get_invocation(sd_id128_t *ret) {
222 static thread_local sd_id128_t saved_invocation_id = {};
225 assert_return(ret, -EINVAL);
227 if (sd_id128_is_null(saved_invocation_id)) {
229 /* We first try to read the invocation ID from the kernel keyring. This has the benefit that it is not
230 * fakeable by unprivileged code. If the information is not available in the keyring, we use
231 * $INVOCATION_ID but ignore the data if our process was called by less privileged code
232 * (i.e. secure_getenv() instead of getenv()).
234 * The kernel keyring is only relevant for system services (as for user services we don't store the
235 * invocation ID in the keyring, as there'd be no trust benefit in that). The environment variable is
236 * primarily relevant for user services, and sufficiently safe as no privilege boundary is involved. */
238 r = get_invocation_from_keyring(&saved_invocation_id);
245 e = secure_getenv("INVOCATION_ID");
249 r = sd_id128_from_string(e, &saved_invocation_id);
255 *ret = saved_invocation_id;
259 static sd_id128_t make_v4_uuid(sd_id128_t id) {
260 /* Stolen from generate_random_uuid() of drivers/char/random.c
261 * in the kernel sources */
263 /* Set UUID version to 4 --- truly random generation */
264 id.bytes[6] = (id.bytes[6] & 0x0F) | 0x40;
266 /* Set the UUID variant to DCE */
267 id.bytes[8] = (id.bytes[8] & 0x3F) | 0x80;
272 _public_ int sd_id128_randomize(sd_id128_t *ret) {
276 assert_return(ret, -EINVAL);
278 r = acquire_random_bytes(&t, sizeof t, true);
282 /* Turn this into a valid v4 UUID, to be nice. Note that we
283 * only guarantee this for newly generated UUIDs, not for
284 * pre-existing ones. */
286 *ret = make_v4_uuid(t);
290 _public_ int sd_id128_get_machine_app_specific(sd_id128_t app_id, sd_id128_t *ret) {
291 _cleanup_(khash_unrefp) khash *h = NULL;
292 sd_id128_t m, result;
296 assert_return(ret, -EINVAL);
298 r = sd_id128_get_machine(&m);
302 r = khash_new_with_key(&h, "hmac(sha256)", &m, sizeof(m));
306 r = khash_put(h, &app_id, sizeof(app_id));
310 r = khash_digest_data(h, &p);
314 /* We chop off the trailing 16 bytes */
315 memcpy(&result, p, MIN(khash_get_size(h), sizeof(result)));
317 *ret = make_v4_uuid(result);