b3415f5dae
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.
172 lines
6.5 KiB
C
172 lines
6.5 KiB
C
/***
|
|
This file is part of systemd.
|
|
|
|
Copyright 2011 Lennart Poettering
|
|
|
|
systemd is free software; you can redistribute it and/or modify it
|
|
under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
(at your option) any later version.
|
|
|
|
systemd is distributed in the hope that it will be useful, but
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with systemd; If not, see <http://www.gnu.org/licenses/>.
|
|
***/
|
|
|
|
#include <string.h>
|
|
|
|
#include "sd-daemon.h"
|
|
#include "sd-id128.h"
|
|
|
|
#include "alloc-util.h"
|
|
#include "fd-util.h"
|
|
#include "fileio.h"
|
|
#include "id128-util.h"
|
|
#include "macro.h"
|
|
#include "string-util.h"
|
|
#include "util.h"
|
|
|
|
#define ID128_WALDI SD_ID128_MAKE(01, 02, 03, 04, 05, 06, 07, 08, 09, 0a, 0b, 0c, 0d, 0e, 0f, 10)
|
|
#define STR_WALDI "0102030405060708090a0b0c0d0e0f10"
|
|
#define UUID_WALDI "01020304-0506-0708-090a-0b0c0d0e0f10"
|
|
|
|
int main(int argc, char *argv[]) {
|
|
sd_id128_t id, id2;
|
|
char t[33], q[37];
|
|
_cleanup_free_ char *b = NULL;
|
|
_cleanup_close_ int fd = -1;
|
|
int r;
|
|
|
|
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));
|
|
|
|
if (sd_booted() > 0) {
|
|
assert_se(sd_id128_get_machine(&id) == 0);
|
|
printf("machine: %s\n", sd_id128_to_string(id, t));
|
|
|
|
assert_se(sd_id128_get_boot(&id) == 0);
|
|
printf("boot: %s\n", sd_id128_to_string(id, t));
|
|
}
|
|
|
|
printf("waldi: %s\n", sd_id128_to_string(ID128_WALDI, t));
|
|
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));
|
|
|
|
printf("waldi3: %s\n", id128_to_uuid_string(ID128_WALDI, q));
|
|
assert_se(streq(q, UUID_WALDI));
|
|
|
|
b = mfree(b);
|
|
assert_se(asprintf(&b, ID128_UUID_FORMAT_STR, SD_ID128_FORMAT_VAL(ID128_WALDI)) == 36);
|
|
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));
|
|
|
|
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"));
|
|
|
|
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);
|
|
assert_se(write(fd, id128_to_uuid_string(id, q), 36) == 36);
|
|
|
|
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(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) >= 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));
|
|
|
|
/* 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));
|
|
|
|
return 0;
|
|
}
|