2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2019-03-27 11:32:41 +01:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "sd-daemon.h"
|
|
|
|
#include "sd-id128.h"
|
2012-01-05 16:01:58 +01:00
|
|
|
|
2015-10-27 03:01:06 +01:00
|
|
|
#include "alloc-util.h"
|
2016-07-25 20:50:24 +02:00
|
|
|
#include "fd-util.h"
|
|
|
|
#include "id128-util.h"
|
2011-10-07 21:06:39 +02:00
|
|
|
#include "macro.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "string-util.h"
|
2018-11-30 21:05:27 +01:00
|
|
|
#include "tmpfile-util.h"
|
2015-10-24 22:58:24 +02:00
|
|
|
#include "util.h"
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
#define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
|
2013-04-29 23:39:12 +02:00
|
|
|
#define STR_WALDI "0102030405060708090a0b0c0d0e0f10"
|
|
|
|
#define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10"
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
sd_id128_t id, id2;
|
2019-12-10 11:33:28 +01:00
|
|
|
char t[SD_ID128_STRING_MAX], q[ID128_UUID_STRING_MAX];
|
2013-04-29 23:39:12 +02:00
|
|
|
_cleanup_free_ char *b = NULL;
|
2016-07-25 20:50:24 +02:00
|
|
|
_cleanup_close_ int fd = -1;
|
core: store the invocation ID in the per-service keyring
Let's store the invocation ID in the per-service keyring as a root-owned key,
with strict access rights. This has the advantage over the environment-based ID
passing that it also works from SUID binaries (as they key cannot be overidden
by unprivileged code starting them), in contrast to the secure_getenv() based
mode.
The invocation ID is now passed in three different ways to a service:
- As environment variable $INVOCATION_ID. This is easy to use, but may be
overriden by unprivileged code (which might be a bad or a good thing), which
means it's incompatible with SUID code (see above).
- As extended attribute on the service cgroup. This cannot be overriden by
unprivileged code, and may be queried safely from "outside" of a service.
However, it is incompatible with containers right now, as unprivileged
containers generally cannot set xattrs on cgroupfs.
- As "invocation_id" key in the kernel keyring. This has the benefit that the
key cannot be changed by unprivileged service code, and thus is safe to
access from SUID code (see above). But do note that service code can replace
the session keyring with a fresh one that lacks the key. However in that case
the key will not be owned by root, which is easily detectable. The keyring is
also incompatible with containers right now, as it is not properly namespace
aware (but this is being worked on), and thus most container managers mask
the keyring-related system calls.
Ideally we'd only have one way to pass the invocation ID, but the different
ways all have limitations. The invocation ID hookup in journald is currently
only available on the host but not in containers, due to the mentioned
limitations.
How to verify the new invocation ID in the keyring:
# systemd-run -t /bin/sh
Running as unit: run-rd917366c04f847b480d486017f7239d6.service
Press ^] three times within 1s to disconnect TTY.
# keyctl show
Session Keyring
680208392 --alswrv 0 0 keyring: _ses
250926536 ----s-rv 0 0 \_ user: invocation_id
# keyctl request user invocation_id
250926536
# keyctl read 250926536
16 bytes of data in key:
9c96317c ac64495a a42b9cd7 4f3ff96b
# echo $INVOCATION_ID
9c96317cac64495aa42b9cd74f3ff96b
# ^D
This creates a new transient service runnint a shell. Then verifies the
contents of the keyring, requests the invocation ID key, and reads its payload.
For comparison the invocation ID as passed via the environment variable is also
displayed.
2016-12-02 15:05:55 +01:00
|
|
|
int r;
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
assert_se(sd_id128_randomize(&id) == 0);
|
|
|
|
printf("random: %s\n", sd_id128_to_string(id, t));
|
|
|
|
|
|
|
|
assert_se(sd_id128_from_string(t, &id2) == 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
2013-07-18 08:30:06 +02:00
|
|
|
if (sd_booted() > 0) {
|
|
|
|
assert_se(sd_id128_get_machine(&id) == 0);
|
|
|
|
printf("machine: %s\n", sd_id128_to_string(id, t));
|
2011-10-07 21:06:39 +02:00
|
|
|
|
2013-07-18 08:30:06 +02:00
|
|
|
assert_se(sd_id128_get_boot(&id) == 0);
|
|
|
|
printf("boot: %s\n", sd_id128_to_string(id, t));
|
|
|
|
}
|
2011-10-07 21:06:39 +02:00
|
|
|
|
|
|
|
printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
|
2013-04-29 23:39:12 +02:00
|
|
|
assert_se(streq(t, STR_WALDI));
|
|
|
|
|
|
|
|
assert_se(asprintf(&b, SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 32);
|
|
|
|
printf("waldi2: %s\n", b);
|
|
|
|
assert_se(streq(t, b));
|
|
|
|
|
2016-07-21 17:57:57 +02:00
|
|
|
printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
|
|
|
|
assert_se(streq(q, UUID_WALDI));
|
|
|
|
|
|
|
|
b = mfree(b);
|
2019-04-05 13:46:33 +02:00
|
|
|
assert_se(asprintf(&b, SD_ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 36);
|
2016-07-21 17:57:57 +02:00
|
|
|
printf("waldi4: %s\n", b);
|
|
|
|
assert_se(streq(q, b));
|
|
|
|
|
|
|
|
assert_se(sd_id128_from_string(STR_WALDI, &id) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, ID128_WALDI));
|
|
|
|
|
2013-04-29 23:39:12 +02:00
|
|
|
assert_se(sd_id128_from_string(UUID_WALDI, &id) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, ID128_WALDI));
|
|
|
|
|
|
|
|
assert_se(sd_id128_from_string("", &id) < 0);
|
|
|
|
assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f101", &id) < 0);
|
|
|
|
assert_se(sd_id128_from_string("01020304-0506-0708-090a-0b0c0d0e0f10-", &id) < 0);
|
|
|
|
assert_se(sd_id128_from_string("01020304-0506-0708-090a0b0c0d0e0f10", &id) < 0);
|
|
|
|
assert_se(sd_id128_from_string("010203040506-0708-090a-0b0c0d0e0f10", &id) < 0);
|
|
|
|
|
|
|
|
assert_se(id128_is_valid(STR_WALDI));
|
|
|
|
assert_se(id128_is_valid(UUID_WALDI));
|
|
|
|
assert_se(!id128_is_valid(""));
|
|
|
|
assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f101"));
|
|
|
|
assert_se(!id128_is_valid("01020304-0506-0708-090a-0b0c0d0e0f10-"));
|
|
|
|
assert_se(!id128_is_valid("01020304-0506-0708-090a0b0c0d0e0f10"));
|
|
|
|
assert_se(!id128_is_valid("010203040506-0708-090a-0b0c0d0e0f10"));
|
2012-01-04 02:09:05 +01:00
|
|
|
|
2016-07-25 20:50:24 +02:00
|
|
|
fd = open_tmpfile_unlinkable(NULL, O_RDWR|O_CLOEXEC);
|
|
|
|
assert_se(fd >= 0);
|
|
|
|
|
|
|
|
/* First, write as UUID */
|
|
|
|
assert_se(sd_id128_randomize(&id) >= 0);
|
|
|
|
assert_se(id128_write_fd(fd, ID128_UUID, id, false) >= 0);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
|
|
|
/* Second, write as plain */
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(ftruncate(fd, 0) >= 0);
|
|
|
|
|
|
|
|
assert_se(sd_id128_randomize(&id) >= 0);
|
|
|
|
assert_se(id128_write_fd(fd, ID128_PLAIN, id, false) >= 0);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_ANY, &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
|
|
|
/* Third, write plain without trailing newline */
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(ftruncate(fd, 0) >= 0);
|
|
|
|
|
|
|
|
assert_se(sd_id128_randomize(&id) >= 0);
|
|
|
|
assert_se(write(fd, sd_id128_to_string(id, t), 32) == 32);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_UUID, &id2) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
|
|
|
/* Third, write UUID without trailing newline */
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(ftruncate(fd, 0) >= 0);
|
|
|
|
|
|
|
|
assert_se(sd_id128_randomize(&id) >= 0);
|
test: fix stack overflow reported by ASAN
It was meant to write to q instead of t
FAIL: test-id128
================
=================================================================
==125770==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd4615bd31 at pc 0x7a2f41b1bf33 bp 0x7ffd4615b750 sp 0x7ffd4615b748
WRITE of size 1 at 0x7ffd4615bd31 thread T0
#0 0x7a2f41b1bf32 in id128_to_uuid_string src/libsystemd/sd-id128/id128-util.c:42
#1 0x401f73 in main src/test/test-id128.c:147
#2 0x7a2f41336341 in __libc_start_main (/lib64/libc.so.6+0x20341)
#3 0x401129 in _start (/home/crrodriguez/scm/systemd/.libs/test-id128+0x401129)
Address 0x7ffd4615bd31 is located in stack of thread T0 at offset 1409 in frame
#0 0x401205 in main src/test/test-id128.c:37
This frame has 23 object(s):
[32, 40) 'b'
[96, 112) 'id'
[160, 176) 'id2'
[224, 240) 'a'
[288, 304) 'b'
[352, 368) 'a'
[416, 432) 'b'
[480, 496) 'a'
[544, 560) 'b'
[608, 624) 'a'
[672, 688) 'b'
[736, 752) 'a'
[800, 816) 'b'
[864, 880) 'a'
[928, 944) 'b'
[992, 1008) 'a'
[1056, 1072) 'b'
[1120, 1136) 'a'
[1184, 1200) 'b'
[1248, 1264) 'a'
[1312, 1328) 'b'
[1376, 1409) 't' <== Memory access at offset 1409 overflows this variable
[1472, 1509) 'q'
HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext
(longjmp and C++ exceptions *are* supported)
SUMMARY: AddressSanitizer: stack-buffer-overflow src/libsystemd/sd-id128/id128-util.c:42 in id128_to_uuid_string
Shadow bytes around the buggy address:
0x100028c23750: f2 f2 00 00 f4 f4 f2 f2 f2 f2 00 00 f4 f4 f2 f2
0x100028c23760: f2 f2 00 00 f4 f4 f2 f2 f2 f2 00 00 f4 f4 f2 f2
0x100028c23770: f2 f2 00 00 f4 f4 f2 f2 f2 f2 00 00 f4 f4 f2 f2
0x100028c23780: f2 f2 00 00 f4 f4 f2 f2 f2 f2 00 00 f4 f4 f2 f2
0x100028c23790: f2 f2 00 00 f4 f4 f2 f2 f2 f2 00 00 f4 f4 f2 f2
=>0x100028c237a0: f2 f2 00 00 00 00[01]f4 f4 f4 f2 f2 f2 f2 00 00
0x100028c237b0: 00 00 05 f4 f4 f4 00 00 00 00 00 00 00 00 00 00
0x100028c237c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100028c237d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100028c237e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x100028c237f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
Shadow byte legend (one shadow byte represents 8 application bytes):
Addressable: 00
Partially addressable: 01 02 03 04 05 06 07
Heap left redzone: fa
Heap right redzone: fb
Freed heap region: fd
Stack left redzone: f1
Stack mid redzone: f2
Stack right redzone: f3
Stack partial redzone: f4
Stack after return: f5
Stack use after scope: f8
Global redzone: f9
Global init order: f6
Poisoned by user: f7
Container overflow: fc
Array cookie: ac
Intra object redzone: bb
ASan internal: fe
Left alloca redzone: ca
Right alloca redzone: cb
==125770==ABORTING
FAIL test-id128 (exit status: 1)
2016-08-05 18:07:49 +02:00
|
|
|
assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36);
|
2016-07-25 20:50:24 +02:00
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_PLAIN, &id2) == -EINVAL);
|
|
|
|
|
|
|
|
assert_se(lseek(fd, 0, SEEK_SET) == 0);
|
|
|
|
assert_se(id128_read_fd(fd, ID128_UUID, &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
|
2017-03-02 23:42:01 +01:00
|
|
|
r = sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id);
|
2018-02-26 13:46:58 +01:00
|
|
|
if (r == -EOPNOTSUPP)
|
2017-03-02 23:42:01 +01:00
|
|
|
log_info("khash not supported on this kernel, skipping sd_id128_get_machine_app_specific() checks");
|
2018-02-26 13:46:58 +01:00
|
|
|
else {
|
2017-03-02 23:42:01 +01:00
|
|
|
assert_se(r >= 0);
|
|
|
|
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(f0,3d,aa,eb,1c,33,4b,43,a7,32,17,29,44,bf,77,2e), &id2) >= 0);
|
|
|
|
assert_se(sd_id128_equal(id, id2));
|
|
|
|
assert_se(sd_id128_get_machine_app_specific(SD_ID128_MAKE(51,df,0b,4b,c3,b0,4c,97,80,e2,99,b9,8c,a3,73,b8), &id2) >= 0);
|
|
|
|
assert_se(!sd_id128_equal(id, id2));
|
|
|
|
}
|
2016-11-17 17:07:46 +01:00
|
|
|
|
core: store the invocation ID in the per-service keyring
Let's store the invocation ID in the per-service keyring as a root-owned key,
with strict access rights. This has the advantage over the environment-based ID
passing that it also works from SUID binaries (as they key cannot be overidden
by unprivileged code starting them), in contrast to the secure_getenv() based
mode.
The invocation ID is now passed in three different ways to a service:
- As environment variable $INVOCATION_ID. This is easy to use, but may be
overriden by unprivileged code (which might be a bad or a good thing), which
means it's incompatible with SUID code (see above).
- As extended attribute on the service cgroup. This cannot be overriden by
unprivileged code, and may be queried safely from "outside" of a service.
However, it is incompatible with containers right now, as unprivileged
containers generally cannot set xattrs on cgroupfs.
- As "invocation_id" key in the kernel keyring. This has the benefit that the
key cannot be changed by unprivileged service code, and thus is safe to
access from SUID code (see above). But do note that service code can replace
the session keyring with a fresh one that lacks the key. However in that case
the key will not be owned by root, which is easily detectable. The keyring is
also incompatible with containers right now, as it is not properly namespace
aware (but this is being worked on), and thus most container managers mask
the keyring-related system calls.
Ideally we'd only have one way to pass the invocation ID, but the different
ways all have limitations. The invocation ID hookup in journald is currently
only available on the host but not in containers, due to the mentioned
limitations.
How to verify the new invocation ID in the keyring:
# systemd-run -t /bin/sh
Running as unit: run-rd917366c04f847b480d486017f7239d6.service
Press ^] three times within 1s to disconnect TTY.
# keyctl show
Session Keyring
680208392 --alswrv 0 0 keyring: _ses
250926536 ----s-rv 0 0 \_ user: invocation_id
# keyctl request user invocation_id
250926536
# keyctl read 250926536
16 bytes of data in key:
9c96317c ac64495a a42b9cd7 4f3ff96b
# echo $INVOCATION_ID
9c96317cac64495aa42b9cd74f3ff96b
# ^D
This creates a new transient service runnint a shell. Then verifies the
contents of the keyring, requests the invocation ID key, and reads its payload.
For comparison the invocation ID as passed via the environment variable is also
displayed.
2016-12-02 15:05:55 +01:00
|
|
|
/* Query the invocation ID */
|
|
|
|
r = sd_id128_get_invocation(&id);
|
|
|
|
if (r < 0)
|
|
|
|
log_warning_errno(r, "Failed to get invocation ID, ignoring: %m");
|
|
|
|
else
|
|
|
|
log_info("Invocation ID: " SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(id));
|
|
|
|
|
2011-10-07 21:06:39 +02:00
|
|
|
return 0;
|
|
|
|
}
|