eBPF-based security platform
sysinfo: read the image identity from os-release and the command line
John Fastabenddcdb4adbc3f4
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex fe3c09cc..f47be16e 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -588,16 +588,15 @@ signed config image.Unit suites and fuzz targets~~~~~~~~~~~~~~~~~~~~~~~~~~~~-The unit suites in :src:`testing/unit/` cover 28 of the modules in+The unit suites in :src:`testing/unit/` cover 29 of the modules in:src:`util/`: ``attest``, ``bpm_oci``, ``bpm_package``, ``bpm_state``,``bpm_tree``, ``bpm_tree_overlap``, ``bpm_tree_state``, ``chain``,``conditions``, ``curl``,``eventlog``, ``format``, ``hex``, ``io``, ``json``, ``kernel_abi``,``keepalive``, ``kernel_build_id``, ``kernel_release``, ``measured``,-``paths``,-``string``, ``upatch``, ``upatch_decode``, ``upatch_pins``,-``upatch_resolve``, ``upatch_verify`` and ``xdp``, and ``overflow`` the-arithmetic of+``paths``, ``string``, ``sysinfo``, ``upatch``, ``upatch_decode``,+``upatch_pins``, ``upatch_resolve``, ``upatch_verify`` and ``xdp``, and+``overflow`` the arithmetic of:src:`include/shirudo/overflow.h`. The other modulesare reached through the test helpers, the fuzz targets and the binariesthe functional tests run.@@ -748,7 +747,8 @@ Coverage by areainheritance: ``t6141``, ``t6142``* - Telemetry- ``t6139``, ``t8001`` to ``t8007``, ``t8101`` to ``t8105``,- ``t9101`` to ``t9105``. Fuzz: ``otel``, ``serializer``+ ``t9101`` to ``t9105``. Fuzz: ``otel``, ``serializer``. Unit:+ ``sysinfo``* - Decoders- ``t3002``, ``t8102``, ``t9101``. Fuzz: ``decoder``* - Tamper counters@@ -987,7 +987,7 @@ Parsers and units- The dm-verity superblock check in :src:`util/config_image.c` has nofuzz target. ``t5001`` covers truncation and unsupported fields.-- 41 of the 68 modules in :src:`util/` have no unit suite of their own.+- 40 of the 68 modules in :src:`util/` have no unit suite of their own.Related-------diff --git a/testing/unit/sysinfo.c b/testing/unit/sysinfo.cnew file mode 100644index 00000000..3598dd50--- /dev/null+++ b/testing/unit/sysinfo.c@@ -0,0 +1,317 @@+// SPDX-License-Identifier: BSD-2-Clause+/* Copyright (c) 2026 Authors of shirudo */++#include <fcntl.h>+#include <stdlib.h>+#include <unistd.h>++#include <shirudo/string.h>++#include "io.h"+#include "sysinfo.h"+#include "unit.h"++#define ROOT_LOWER \+ "a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c"+#define ROOT_UPPER \+ "A3C3F1B0E4C2D7F09E3B6B2A8B1D5E7C4F6A9D0B2C8E1F3A5B7D9C0E2F4A6B8C"+#define USR_LOWER \+ "1d8465c5b17838303fee92b3905420d294d2c22c576a0bdef0f69f190ed22807"++static char dir[4096], etc_path[4200], usr_path[4200], cmdline_path[4200];++static void use_fixtures(void)+{+ const char *tmp = getenv("TMPDIR");++ ASSERT_TRUE(strsprintf(dir, sizeof(dir),+ "%s/shirudo-unit-sysinfo-XXXXXX",+ tmp && tmp[0] ? tmp : "/tmp") > 0);+ ASSERT_TRUE(mkdtemp(dir));+ ASSERT_TRUE(strsprintf(etc_path, sizeof(etc_path),+ "%s/etc-os-release", dir) > 0);+ ASSERT_TRUE(strsprintf(usr_path, sizeof(usr_path),+ "%s/usr-lib-os-release", dir) > 0);+ ASSERT_TRUE(strsprintf(cmdline_path, sizeof(cmdline_path),+ "%s/cmdline", dir) > 0);+}++static void drop_fixtures(void)+{+ unlink(etc_path);+ unlink(usr_path);+ unlink(cmdline_path);+ rmdir(dir);+}++static void write_fixture(const char *path, const char *text, size_t len)+{+ int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0600);++ ASSERT_TRUE(fd >= 0);+ ASSERT_EQ(write_complete(fd, text, len), 0);+ ASSERT_EQ(close(fd), 0);+}++/* A path left NULL names a file that does not exist. */+static struct sysinfo_image image_of(const char *etc, const char *usr,+ const char *cmdline)+{+ struct sysinfo_image img;++ use_fixtures();+ if (etc)+ write_fixture(etc_path, etc, strlen(etc));+ if (usr)+ write_fixture(usr_path, usr, strlen(usr));+ if (cmdline)+ write_fixture(cmdline_path, cmdline, strlen(cmdline));+ sysinfo_image_read_at(etc_path, usr_path, cmdline_path, &img);+ drop_fixtures();+ return img;+}++static void unquoted_values_are_copied(void)+{+ struct sysinfo_image img;++ img = image_of("NAME=Fleet\nIMAGE_ID=fleet-os\n"+ "IMAGE_VERSION=2026.11.1\n", NULL, NULL);+ EXPECT_STREQ(img.id, "fleet-os");+ EXPECT_STREQ(img.version, "2026.11.1");+ EXPECT_STREQ(img.root, "");+}++static void one_pair_of_matching_quotes_is_stripped(void)+{+ struct sysinfo_image img;++ img = image_of("IMAGE_ID=\"fleet os\"\nIMAGE_VERSION='2026 \"11\"'\n",+ NULL, NULL);+ EXPECT_STREQ(img.id, "fleet os");+ EXPECT_STREQ(img.version, "2026 \"11\"");++ img = image_of("IMAGE_ID=\"\"fleet\"\"\nIMAGE_VERSION=\"2026'\n",+ NULL, NULL);+ EXPECT_STREQ(img.id, "\"fleet\"");+ EXPECT_STREQ(img.version, "\"2026'");+}++static void double_quotes_drop_the_escaping_backslash(void)+{+ struct sysinfo_image img;++ img = image_of("IMAGE_ID=\"a\\\"b\\\\c\\$d\\`e\\nf\"\n"+ "IMAGE_VERSION='a\\\"b'\n", NULL, NULL);+ EXPECT_STREQ(img.id, "a\"b\\c$d`e\\nf");+ EXPECT_STREQ(img.version, "a\\\"b");++ img = image_of("IMAGE_ID=a\\\"b\n", NULL, NULL);+ EXPECT_STREQ(img.id, "a\\\"b");+}++static void missing_key_is_absent(void)+{+ struct sysinfo_image img;++ img = image_of("NAME=Fleet\nXIMAGE_ID=no\nIMAGE_ID_LIKE=no\n"+ "#IMAGE_ID=no\nIMAGE_VERSION=7\n", NULL, NULL);+ EXPECT_STREQ(img.id, "");+ EXPECT_STREQ(img.version, "7");++ img = image_of(NULL, NULL, NULL);+ EXPECT_STREQ(img.id, "");+ EXPECT_STREQ(img.version, "");+}++static void empty_value_is_absent(void)+{+ struct sysinfo_image img;++ img = image_of("IMAGE_ID=\nIMAGE_VERSION=\"\"\n", NULL, NULL);+ EXPECT_STREQ(img.id, "");+ EXPECT_STREQ(img.version, "");+}++static void last_assignment_counts(void)+{+ struct sysinfo_image img;++ img = image_of("IMAGE_ID=first\nIMAGE_ID=second\n"+ "IMAGE_VERSION=1\nIMAGE_VERSION=caf\xc3\xa9", NULL,+ NULL);+ EXPECT_STREQ(img.id, "second");+ EXPECT_STREQ(img.version, "");+}++static void usr_lib_is_read_when_etc_does_not_exist(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, "IMAGE_ID=from-usr\nIMAGE_VERSION=2\n", NULL);+ EXPECT_STREQ(img.id, "from-usr");+ EXPECT_STREQ(img.version, "2");++ img = image_of("IMAGE_ID=from-etc\n",+ "IMAGE_ID=from-usr\nIMAGE_VERSION=2\n", NULL);+ EXPECT_STREQ(img.id, "from-etc");+ EXPECT_STREQ(img.version, "");+}++static void value_longer_than_255_bytes_is_absent(void)+{+ char text[600], longest[256], too_long[257];+ struct sysinfo_image img;++ memset(longest, 'a', sizeof(longest) - 1);+ longest[sizeof(longest) - 1] = '\0';+ memset(too_long, 'b', sizeof(too_long) - 1);+ too_long[sizeof(too_long) - 1] = '\0';+ ASSERT_TRUE(strsprintf(text, sizeof(text),+ "IMAGE_ID=\"%s\"\nIMAGE_VERSION=%s\n", longest,+ too_long) > 0);+ img = image_of(text, NULL, NULL);+ EXPECT_STREQ(img.id, longest);+ EXPECT_STREQ(img.version, "");+}++static void value_outside_printable_ascii_is_absent(void)+{+ struct sysinfo_image img;++ img = image_of("IMAGE_ID=caf\xc3\xa9\nIMAGE_VERSION=1\t2\n", NULL,+ NULL);+ EXPECT_STREQ(img.id, "");+ EXPECT_STREQ(img.version, "");++ img = image_of("IMAGE_ID=fleet\r\nIMAGE_VERSION=1\x7f\n", NULL, NULL);+ EXPECT_STREQ(img.id, "");+ EXPECT_STREQ(img.version, "");+}++static void value_with_a_nul_is_absent(void)+{+ static const char text[] = "IMAGE_ID=fle\0et\nIMAGE_VERSION=3\n";+ struct sysinfo_image img;++ use_fixtures();+ write_fixture(etc_path, text, sizeof(text) - 1);+ sysinfo_image_read_at(etc_path, usr_path, cmdline_path, &img);+ drop_fixtures();+ EXPECT_STREQ(img.id, "");+ EXPECT_STREQ(img.version, "3");+}++static void roothash_wins_over_usrhash(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, NULL,+ "quiet usrhash=" USR_LOWER " roothash=" ROOT_LOWER+ " ro\n");+ EXPECT_STREQ(img.root, ROOT_LOWER);++ img = image_of(NULL, NULL,+ "roothash=" ROOT_LOWER " usrhash=" USR_LOWER "\n");+ EXPECT_STREQ(img.root, ROOT_LOWER);+}++static void usrhash_is_used_without_roothash(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, NULL,+ "BOOT_IMAGE=/vmlinuz xroothash=" ROOT_LOWER+ "\tusrhash=" USR_LOWER);+ EXPECT_STREQ(img.root, USR_LOWER);+}++static void hash_is_normalised_to_lowercase(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, NULL, "roothash=" ROOT_UPPER "\n");+ EXPECT_STREQ(img.root, ROOT_LOWER);+}++static void malformed_hash_is_absent(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, NULL, "roothash=abc123\n");+ EXPECT_STREQ(img.root, "");+ img = image_of(NULL, NULL, "roothash=" ROOT_LOWER "0\n");+ EXPECT_STREQ(img.root, "");+ img = image_of(NULL, NULL, "roothash=g" USR_LOWER "\n");+ EXPECT_STREQ(img.root, "");+ img = image_of(NULL, NULL, "roothash=\"" ROOT_LOWER "\"\n");+ EXPECT_STREQ(img.root, "");+ img = image_of(NULL, NULL, "roothash=sha256:" ROOT_LOWER "\n");+ EXPECT_STREQ(img.root, "");+}++static void malformed_roothash_does_not_fall_back_to_usrhash(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, NULL,+ "roothash=abc123 usrhash=" USR_LOWER "\n");+ EXPECT_STREQ(img.root, "");+}++static void no_hash_is_absent(void)+{+ struct sysinfo_image img;++ img = image_of(NULL, NULL, "BOOT_IMAGE=/vmlinuz root=/dev/sda2 ro\n");+ EXPECT_STREQ(img.root, "");+ img = image_of(NULL, NULL, "");+ EXPECT_STREQ(img.root, "");+ img = image_of("IMAGE_ID=fleet-os\n", NULL, NULL);+ EXPECT_STREQ(img.id, "fleet-os");+ EXPECT_STREQ(img.root, "");+}++static void command_line_that_does_not_fit_is_absent(void)+{+ static char cmdline[8192 + 128];+ struct sysinfo_image img;+ size_t used;++ memset(cmdline, 'x', sizeof(cmdline));+ used = sizeof(cmdline) - sizeof(" roothash=" ROOT_LOWER);+ memcpy(cmdline + used, " roothash=" ROOT_LOWER,+ sizeof(" roothash=" ROOT_LOWER));+ img = image_of(NULL, NULL, cmdline);+ EXPECT_STREQ(img.root, "");++ memset(cmdline, 'x', sizeof(cmdline));+ memcpy(cmdline, "roothash=" ROOT_LOWER " ",+ sizeof("roothash=" ROOT_LOWER " ") - 1);+ cmdline[sizeof(cmdline) - 1] = '\0';+ img = image_of(NULL, NULL, cmdline);+ EXPECT_STREQ(img.root, "");+}++static const struct unit_case cases[] = {+ UNIT_CASE(unquoted_values_are_copied),+ UNIT_CASE(one_pair_of_matching_quotes_is_stripped),+ UNIT_CASE(double_quotes_drop_the_escaping_backslash),+ UNIT_CASE(missing_key_is_absent),+ UNIT_CASE(empty_value_is_absent),+ UNIT_CASE(last_assignment_counts),+ UNIT_CASE(usr_lib_is_read_when_etc_does_not_exist),+ UNIT_CASE(value_longer_than_255_bytes_is_absent),+ UNIT_CASE(value_outside_printable_ascii_is_absent),+ UNIT_CASE(value_with_a_nul_is_absent),+ UNIT_CASE(roothash_wins_over_usrhash),+ UNIT_CASE(usrhash_is_used_without_roothash),+ UNIT_CASE(hash_is_normalised_to_lowercase),+ UNIT_CASE(malformed_hash_is_absent),+ UNIT_CASE(malformed_roothash_does_not_fall_back_to_usrhash),+ UNIT_CASE(no_hash_is_absent),+ UNIT_CASE(command_line_that_does_not_fit_is_absent),+ {}+};++UNIT_SUITE(sysinfo, cases);diff --git a/util/sysinfo.c b/util/sysinfo.cindex 93a25612..c3489d71 100644--- a/util/sysinfo.c+++ b/util/sysinfo.c@@ -6,6 +6,7 @@*/#include <ctype.h>+#include <errno.h>#include <fcntl.h>#include <limits.h>#include <stdio.h>@@ -18,6 +19,7 @@#include <shirudo/misc.h>#include <shirudo/string.h>+#include "io.h"#include "json.h"#include "kernel_build_id.h"#include "malloc.h"@@ -42,32 +44,85 @@ static void read_first_line(const char *path, char *buf, size_t cap)buf[strcspn(buf, "\r\n")] = '\0';}-static void read_os_pretty(char *buf, size_t cap)+#define OS_RELEASE_SIZE_MAX (64u * 1024u)+/* Above the COMMAND_LINE_SIZE of every architecture. */+#define CMDLINE_SIZE_MAX 8192u++static bool os_release_escapable(char c){- char line[512];- FILE *f;+ return c == '"' || c == '\\' || c == '$' || c == '`';+}- buf[0] = '\0';- f = fopen("/etc/os-release", "re");- if (!f)- return;- while (fgets(line, sizeof(line), f)) {- const char *v;- size_t len;+static int os_release_unquote(const char *value, size_t len,+ char out[SYSINFO_IMAGE_VALUE_MAX])+{+ bool double_quoted = false;+ size_t n = 0, i;++ if (len >= 2 && value[0] == value[len - 1] &&+ (value[0] == '"' || value[0] == '\'')) {+ double_quoted = value[0] == '"';+ value++;+ len -= 2;+ }+ for (i = 0; i < len; i++) {+ if (double_quoted && value[i] == '\\' && i + 1 < len &&+ os_release_escapable(value[i + 1]))+ i++;+ if ((__u8)value[i] < 0x20 || (__u8)value[i] > 0x7e ||+ n == SYSINFO_IMAGE_VALUE_MAX - 1)+ return -EINVAL;+ out[n++] = value[i];+ }+ if (!n)+ return -EINVAL;+ out[n] = '\0';+ return 0;+}- if (strncmp(line, "PRETTY_NAME=", 12) != 0)+static void os_release_value(const char *text, size_t len, const char *key,+ char out[SYSINFO_IMAGE_VALUE_MAX])+{+ const char *end = text + len, *value = NULL, *line, *eol;+ size_t key_len = strlen(key), value_len = 0;++ for (line = text; line < end; line = eol < end ? eol + 1 : end) {+ eol = memchr(line, '\n', (size_t)(end - line));+ if (!eol)+ eol = end;+ if ((size_t)(eol - line) <= key_len ||+ memcmp(line, key, key_len) || line[key_len] != '=')continue;- v = line + 12;- if (*v == '"')- v++;- strscpy(buf, v, cap);- len = strlen(buf);- while (len && (buf[len - 1] == '\n' || buf[len - 1] == '\r' ||- buf[len - 1] == '"'))- buf[--len] = '\0';- break;+ value = line + key_len + 1;+ value_len = (size_t)(eol - value);}- fclose(f);+ if (!value || os_release_unquote(value, value_len, out))+ out[0] = '\0';+}++/* os-release(5): /usr/lib/os-release stands in only where /etc has none. */+static int read_os_release(const char *path, const char *fallback,+ char **text, size_t *len)+{+ int ret = read_file(path, OS_RELEASE_SIZE_MAX, text, len);++ if (ret == -ENOENT)+ ret = read_file(fallback, OS_RELEASE_SIZE_MAX, text, len);+ return ret;+}++static void read_os_release_value(const char *key,+ char out[SYSINFO_IMAGE_VALUE_MAX])+{+ size_t len;+ char *text;++ out[0] = '\0';+ if (read_os_release("/etc/os-release", "/usr/lib/os-release", &text,+ &len))+ return;+ os_release_value(text, len, key, out);+ xfree(text, len + 1);}/* /proc/uptime holds the seconds since boot, with a fraction. */@@ -304,7 +359,7 @@ void sysinfo_gather(struct sysinfo *si)sizeof(si->hostname));}- read_os_pretty(si->os_pretty, sizeof(si->os_pretty));+ read_os_release_value("PRETTY_NAME", si->os_pretty);read_first_line("/etc/machine-id", si->machine_id,sizeof(si->machine_id));read_first_line("/proc/sys/kernel/random/boot_id", si->boot_id,@@ -317,6 +372,92 @@ void sysinfo_gather(struct sysinfo *si)si->uptime_sec = read_uptime_sec();}+static bool cmdline_space(char c)+{+ return c == ' ' || c == '\t' || c == '\n';+}++static int image_root_normalise(const char *arg, size_t len,+ char out[SYSINFO_IMAGE_ROOT_MAX])+{+ size_t i;++ if (len != SYSINFO_IMAGE_ROOT_MAX - 1)+ return -EINVAL;+ for (i = 0; i < len; i++) {+ if (!isxdigit((__u8)arg[i]))+ return -EINVAL;+ out[i] = (char)tolower((__u8)arg[i]);+ }+ out[len] = '\0';+ return 0;+}++struct cmdline_arg {+ const char *value;+ size_t len;+};++static void cmdline_arg_match(const char *arg, size_t len, const char *key,+ struct cmdline_arg *found)+{+ size_t key_len = strlen(key);++ if (len < key_len || memcmp(arg, key, key_len))+ return;+ found->value = arg + key_len;+ found->len = len - key_len;+}++static void cmdline_image_root(const char *cmdline, size_t len,+ char out[SYSINFO_IMAGE_ROOT_MAX])+{+ const char *end = cmdline + len, *arg, *arg_end;+ struct cmdline_arg roothash = {}, usrhash = {};+ const struct cmdline_arg *hash;++ for (arg = cmdline; arg < end; arg = arg_end) {+ while (arg < end && cmdline_space(*arg))+ arg++;+ arg_end = arg;+ while (arg_end < end && !cmdline_space(*arg_end))+ arg_end++;+ cmdline_arg_match(arg, (size_t)(arg_end - arg), "roothash=",+ &roothash);+ cmdline_arg_match(arg, (size_t)(arg_end - arg), "usrhash=",+ &usrhash);+ }+ hash = roothash.value ? &roothash : &usrhash;+ if (!hash->value || image_root_normalise(hash->value, hash->len, out))+ out[0] = '\0';+}++void sysinfo_image_read_at(const char *os_release,+ const char *os_release_fallback,+ const char *cmdline, struct sysinfo_image *img)+{+ char line[CMDLINE_SIZE_MAX];+ size_t len;+ char *text;++ *img = (struct sysinfo_image){};+ if (!read_os_release(os_release, os_release_fallback, &text, &len)) {+ os_release_value(text, len, "IMAGE_ID", img->id);+ os_release_value(text, len, "IMAGE_VERSION", img->version);+ xfree(text, len + 1);+ }+ read_first_line(cmdline, line, sizeof(line));+ len = strlen(line);+ if (len < sizeof(line) - 1)+ cmdline_image_root(line, len, img->root);+}++void sysinfo_image_read(struct sysinfo_image *img)+{+ sysinfo_image_read_at("/etc/os-release", "/usr/lib/os-release",+ "/proc/cmdline", img);+}+void sysinfo_emit_json(struct json_emit *e,const struct sysinfo *si){diff --git a/util/sysinfo.h b/util/sysinfo.hindex 6793bce5..d045c7f1 100644--- a/util/sysinfo.h+++ b/util/sysinfo.h@@ -2,7 +2,8 @@/** Copyright (c) 2026 Authors of shirudo*- * The host overview of `status --include-system`, without a subprocess.+ * The host overview of `status --include-system` and the identity of the+ * booted image, without a subprocess.*/#ifndef SHIRUDO_SYSINFO_H@@ -10,9 +11,12 @@#include "kernel_build_id.h"+#define SYSINFO_IMAGE_VALUE_MAX 256u+#define SYSINFO_IMAGE_ROOT_MAX 65u+struct sysinfo {char hostname[256];- char os_pretty[256]; /* /etc/os-release PRETTY_NAME */+ char os_pretty[SYSINFO_IMAGE_VALUE_MAX]; /* PRETTY_NAME */char kernel_release[96]; /* uname -r */char kernel_version[256]; /* uname -v */char kernel_build_id[KERNEL_BUILD_ID_STRING_MAX];@@ -43,4 +47,23 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,void sysinfo_emit_json(struct json_emit *e,const struct sysinfo *si);+struct sysinfo_image {+ char id[SYSINFO_IMAGE_VALUE_MAX];+ char version[SYSINFO_IMAGE_VALUE_MAX];+ char root[SYSINFO_IMAGE_ROOT_MAX];+};++/*+ * A value that is absent or refused is "". An os-release value is+ * unquoted as os-release(5) defines and must then be 1 to 255 bytes of+ * printable ASCII; of a key given twice the last one counts, as a shell+ * reading the file has it. The root is roothash=, else usrhash=, in+ * lowercase, and none from a command line that does not fit.+ */+void sysinfo_image_read_at(const char *os_release,+ const char *os_release_fallback,+ const char *cmdline, struct sysinfo_image *img);++void sysinfo_image_read(struct sysinfo_image *img);+#endif /* SHIRUDO_SYSINFO_H */