1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include "alloc-util.h"
11 #include "hexdecoct.h"
12 #include "id128-util.h"
17 #include "random-util.h"
18 #include "user-util.h"
21 _public_
char *sd_id128_to_string(sd_id128_t id
, char s
[SD_ID128_STRING_MAX
]) {
24 assert_return(s
, NULL
);
26 for (n
= 0; n
< 16; n
++) {
27 s
[n
*2] = hexchar(id
.bytes
[n
] >> 4);
28 s
[n
*2+1] = hexchar(id
.bytes
[n
] & 0xF);
36 _public_
int sd_id128_from_string(const char s
[], sd_id128_t
*ret
) {
41 assert_return(s
, -EINVAL
);
43 for (n
= 0, i
= 0; n
< 16;) {
47 /* Is this a GUID? Then be nice, and skip over
52 else if (IN_SET(i
, 13, 18, 23)) {
62 a
= unhexchar(s
[i
++]);
66 b
= unhexchar(s
[i
++]);
70 t
.bytes
[n
++] = (a
<< 4) | b
;
73 if (i
!= (is_guid
? 36 : 32))
84 _public_
int sd_id128_get_machine(sd_id128_t
*ret
) {
85 static thread_local sd_id128_t saved_machine_id
= {};
88 assert_return(ret
, -EINVAL
);
90 if (sd_id128_is_null(saved_machine_id
)) {
91 r
= id128_read("/etc/machine-id", ID128_PLAIN
, &saved_machine_id
);
95 if (sd_id128_is_null(saved_machine_id
))
99 *ret
= saved_machine_id
;
103 _public_
int sd_id128_get_boot(sd_id128_t
*ret
) {
104 static thread_local sd_id128_t saved_boot_id
= {};
107 assert_return(ret
, -EINVAL
);
109 if (sd_id128_is_null(saved_boot_id
)) {
110 r
= id128_read("/proc/sys/kernel/random/boot_id", ID128_UUID
, &saved_boot_id
);
115 *ret
= saved_boot_id
;
119 static int get_invocation_from_keyring(sd_id128_t
*ret
) {
121 _cleanup_free_
char *description
= NULL
;
122 char *d
, *p
, *g
, *u
, *e
;
130 #define MAX_PERMS ((unsigned long) (KEY_POS_VIEW|KEY_POS_READ|KEY_POS_SEARCH| \
131 KEY_USR_VIEW|KEY_USR_READ|KEY_USR_SEARCH))
135 key
= request_key("user", "invocation_id", NULL
, 0);
137 /* Keyring support not available? No invocation key stored? */
138 if (IN_SET(errno
, ENOSYS
, ENOKEY
))
145 description
= new(char, sz
);
149 c
= keyctl(KEYCTL_DESCRIBE
, key
, (unsigned long) description
, sz
, 0);
153 if ((size_t) c
<= sz
)
160 /* The kernel returns a final NUL in the string, verify that. */
161 assert(description
[c
-1] == 0);
163 /* Chop off the final description string */
164 d
= strrchr(description
, ';');
169 /* Look for the permissions */
170 p
= strrchr(description
, ';');
175 perms
= strtoul(p
+ 1, &e
, 16);
178 if (e
== p
+ 1) /* Read at least one character */
180 if (e
!= d
) /* Must reached the end */
183 if ((perms
& ~MAX_PERMS
) != 0)
188 /* Look for the group ID */
189 g
= strrchr(description
, ';');
192 r
= parse_gid(g
+ 1, &gid
);
199 /* Look for the user ID */
200 u
= strrchr(description
, ';');
203 r
= parse_uid(u
+ 1, &uid
);
209 c
= keyctl(KEYCTL_READ
, key
, (unsigned long) ret
, sizeof(sd_id128_t
), 0);
212 if (c
!= sizeof(sd_id128_t
))
218 _public_
int sd_id128_get_invocation(sd_id128_t
*ret
) {
219 static thread_local sd_id128_t saved_invocation_id
= {};
222 assert_return(ret
, -EINVAL
);
224 if (sd_id128_is_null(saved_invocation_id
)) {
226 /* We first try to read the invocation ID from the kernel keyring. This has the benefit that it is not
227 * fakeable by unprivileged code. If the information is not available in the keyring, we use
228 * $INVOCATION_ID but ignore the data if our process was called by less privileged code
229 * (i.e. secure_getenv() instead of getenv()).
231 * The kernel keyring is only relevant for system services (as for user services we don't store the
232 * invocation ID in the keyring, as there'd be no trust benefit in that). The environment variable is
233 * primarily relevant for user services, and sufficiently safe as no privilege boundary is involved. */
235 r
= get_invocation_from_keyring(&saved_invocation_id
);
242 e
= secure_getenv("INVOCATION_ID");
246 r
= sd_id128_from_string(e
, &saved_invocation_id
);
252 *ret
= saved_invocation_id
;
256 static sd_id128_t
make_v4_uuid(sd_id128_t id
) {
257 /* Stolen from generate_random_uuid() of drivers/char/random.c
258 * in the kernel sources */
260 /* Set UUID version to 4 --- truly random generation */
261 id
.bytes
[6] = (id
.bytes
[6] & 0x0F) | 0x40;
263 /* Set the UUID variant to DCE */
264 id
.bytes
[8] = (id
.bytes
[8] & 0x3F) | 0x80;
269 _public_
int sd_id128_randomize(sd_id128_t
*ret
) {
273 assert_return(ret
, -EINVAL
);
275 /* We allow usage if x86-64 RDRAND here. It might not be trusted enough for keeping secrets, but it should be
277 r
= genuine_random_bytes(&t
, sizeof t
, RANDOM_ALLOW_RDRAND
);
281 /* Turn this into a valid v4 UUID, to be nice. Note that we
282 * only guarantee this for newly generated UUIDs, not for
283 * pre-existing ones. */
285 *ret
= make_v4_uuid(t
);
289 static int get_app_specific(sd_id128_t base
, sd_id128_t app_id
, sd_id128_t
*ret
) {
290 _cleanup_(khash_unrefp
) khash
*h
= NULL
;
297 r
= khash_new_with_key(&h
, "hmac(sha256)", &base
, sizeof(base
));
301 r
= khash_put(h
, &app_id
, sizeof(app_id
));
305 r
= khash_digest_data(h
, &p
);
309 /* We chop off the trailing 16 bytes */
310 memcpy(&result
, p
, MIN(khash_get_size(h
), sizeof(result
)));
312 *ret
= make_v4_uuid(result
);
316 _public_
int sd_id128_get_machine_app_specific(sd_id128_t app_id
, sd_id128_t
*ret
) {
320 assert_return(ret
, -EINVAL
);
322 r
= sd_id128_get_machine(&id
);
326 return get_app_specific(id
, app_id
, ret
);
329 _public_
int sd_id128_get_boot_app_specific(sd_id128_t app_id
, sd_id128_t
*ret
) {
333 assert_return(ret
, -EINVAL
);
335 r
= sd_id128_get_boot(&id
);
339 return get_app_specific(id
, app_id
, ret
);