Skip to content

eBPF-based security platform

sysinfo, preflight: refuse a NUL in /proc and sysfs text

Daniel Borkmann6c045fd391a1
diff --git a/Documentation/reference/formats/status-json.rst b/Documentation/reference/formats/status-json.rst
index e8de13e5..a10e706b 100644
--- a/Documentation/reference/formats/status-json.rst
+++ b/Documentation/reference/formats/status-json.rst
@@ -704,7 +704,10 @@ Optional blocks
the file), ``architecture``, ``machine_id``, ``boot_id``,
``nproc``, ``mem_total_kb``, ``mem_avail_kb``, ``disk_total_kb``
and ``disk_used_kb`` (of ``/``), ``uptime_seconds``. A number
- that cannot be read is ``-1``.
+ that cannot be read is ``-1`` and a text that cannot be read is
+ empty. A NUL byte where a value is read counts as a read error:
+ ``kernel_config`` then ends with the line before it. A line of
+ the kernel config longer than 8191 bytes is left out.
* - ``--include-eventlog``
- ``tpm.eventlog.events``: one object per line of
``/var/lib/shirudo/eventlog``, all boots, with ``ts``, ``type``
diff --git a/testing/unit/sysinfo.c b/testing/unit/sysinfo.c
index 95d22654..0d6579a6 100644
--- a/testing/unit/sysinfo.c
+++ b/testing/unit/sysinfo.c
@@ -293,6 +293,19 @@ static void command_line_that_does_not_fit_is_absent(void)
EXPECT_STREQ(img.root, "");
}
+/* A NUL would end the line short of the bytes the kernel wrote. */
+static void command_line_with_a_nul_is_absent(void)
+{
+ static const char text[] = "roothash=" ROOT_LOWER "\0 ro\n";
+ struct sysinfo_image img;
+
+ use_fixtures();
+ write_fixture(cmdline_path, text, sizeof(text) - 1);
+ sysinfo_image_read_at(etc_path, usr_path, cmdline_path, &img);
+ drop_fixtures();
+ EXPECT_STREQ(img.root, "");
+}
+
static void memory_of_this_host_is_read(void)
{
struct sysinfo si;
@@ -321,6 +334,7 @@ static const struct unit_case cases[] = {
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_CASE(command_line_with_a_nul_is_absent),
UNIT_CASE(memory_of_this_host_is_read),
{}
};
diff --git a/util/preflight.c b/util/preflight.c
index 085e2bb9..02359e66 100644
--- a/util/preflight.c
+++ b/util/preflight.c
@@ -25,6 +25,7 @@
#include "config.h"
#include "initrd.h"
#include "crypto.h"
+#include "io.h"
#include "kernel_abi.h"
#include "kernel_prereq.h"
#include "keys.h"
@@ -90,8 +91,8 @@ static void check_root(struct preflight_result *r)
static bool lsm_lists_bpf(void)
{
+ char buf[1024 + 1];
const char *name;
- char buf[1024];
size_t len;
ssize_t n;
int fd;
@@ -99,9 +100,9 @@ static bool lsm_lists_bpf(void)
fd = open("/sys/kernel/security/lsm", O_RDONLY | O_CLOEXEC);
if (fd < 0)
return false;
- n = read(fd, buf, sizeof(buf));
+ n = read(fd, buf, sizeof(buf) - 1);
close(fd);
- if (n <= 0)
+ if (n <= 0 || span_to_str(buf, sizeof(buf), buf, (size_t)n))
return false;
for_each_token(name, len, buf, (size_t)n, ",\n") {
if (span_equals(name, len, "bpf"))
@@ -123,18 +124,27 @@ static void check_bpf_lsm(struct preflight_result *r)
"(needs CONFIG_BPF_LSM=y, bpf in CONFIG_LSM)");
}
+#define FILESYSTEMS_LINE_MAX 127u
+
static bool bpffs_in_proc_filesystems(void)
{
FILE *f = fopen("/proc/filesystems", "re");
- char line[128];
+ size_t capacity = 0;
bool found = false;
+ char *line = NULL;
+ int got;
if (!f)
return false;
- while (fgets(line, sizeof(line), f)) {
- char *tab = strchr(line, '\t');
- char *name, *nl;
+ while ((got = read_text_line(f, &line, &capacity,
+ FILESYSTEMS_LINE_MAX)) != 0) {
+ char *tab, *name, *nl;
+ if (got == -E2BIG)
+ continue;
+ if (got < 0)
+ break;
+ tab = strchr(line, '\t');
if (!tab)
continue;
name = tab + 1;
@@ -146,6 +156,7 @@ static bool bpffs_in_proc_filesystems(void)
break;
}
}
+ libc_free(line);
fclose(f);
return found;
}
diff --git a/util/sysinfo.c b/util/sysinfo.c
index fb5e8c98..27be362f 100644
--- a/util/sysinfo.c
+++ b/util/sysinfo.c
@@ -36,11 +36,9 @@ static void read_first_line(const char *path, char *buf, size_t cap)
return;
n = read(fd, buf, cap - 1);
close(fd);
- if (n <= 0) {
+ if (n <= 0 ||
+ span_to_str(buf, cap, buf, span_until(buf, (size_t)n, "\r\n")))
buf[0] = '\0';
- return;
- }
- buf[span_until(buf, (size_t)n, "\r\n")] = '\0';
}
#define OS_RELEASE_SIZE_MAX (64u * 1024u)
@@ -137,7 +135,7 @@ static long read_uptime_sec(void)
return -1;
n = read(fd, buf, sizeof(buf) - 1);
close(fd);
- if (n <= 0)
+ if (n <= 0 || span_to_str(buf, sizeof(buf), buf, (size_t)n))
return -1;
if (parse_u64(buf, digits(buf, (size_t)n), LONG_MAX, &sec))
return -1;
@@ -159,17 +157,26 @@ static long meminfo_kb(const char *line, const char *name)
return (long)kb;
}
+#define MEMINFO_LINE_MAX 255u
+
static void read_mem(long *total_kb, long *avail_kb)
{
- char line[256];
+ size_t capacity = 0;
+ char *line = NULL;
FILE *f;
+ int got;
*total_kb = -1;
*avail_kb = -1;
f = fopen("/proc/meminfo", "re");
if (!f)
return;
- while (fgets(line, sizeof(line), f)) {
+ while ((got = read_text_line(f, &line, &capacity,
+ MEMINFO_LINE_MAX)) != 0) {
+ if (got == -E2BIG)
+ continue;
+ if (got < 0)
+ break;
if (*total_kb < 0)
*total_kb = meminfo_kb(line, "MemTotal:");
if (*avail_kb < 0)
@@ -177,6 +184,7 @@ static void read_mem(long *total_kb, long *avail_kb)
if (*total_kb >= 0 && *avail_kb >= 0)
break;
}
+ libc_free(line);
fclose(f);
}
@@ -245,16 +253,18 @@ static bool config_name(const char *key)
return n && key[n];
}
+#define KERNEL_CONFIG_LINE_MAX 8191u
+
int sysinfo_kernel_config_foreach(const char *kernel_release,
shirudo_kernel_config_cb cb,
void *ctx)
{
- char path[PATH_MAX];
- char line[8192];
struct kernel_config_entry *entries = NULL;
- size_t n = 0, cap = 0;
+ size_t n = 0, cap = 0, capacity = 0;
+ char path[PATH_MAX], *line = NULL;
FILE *f;
size_t i;
+ int got;
if (!kernel_release || !kernel_release[0] || !cb ||
strsprintf(path, sizeof(path), "/boot/config-%s", kernel_release) < 0)
@@ -262,9 +272,14 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
f = fopen(path, "re");
if (!f)
return -1;
- while (fgets(line, sizeof(line), f)) {
+ while ((got = read_text_line(f, &line, &capacity,
+ KERNEL_CONFIG_LINE_MAX)) != 0) {
char *key, *value, *eq, *not_set, unset[] = "n";
+ if (got == -E2BIG)
+ continue;
+ if (got < 0)
+ break;
line[span_until(line, strlen(line), "\r\n")] = '\0';
key = trim_config_text(line);
if (!*key)
@@ -305,6 +320,7 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
next = xreallocarray(entries, cap, grown,
sizeof(*entries));
if (!next) {
+ libc_free(line);
fclose(f);
kernel_config_entries_free(entries, n, cap);
return -1;
@@ -319,12 +335,14 @@ int sysinfo_kernel_config_foreach(const char *kernel_release,
xstrfree(entries[n].value);
entries[n].key = NULL;
entries[n].value = NULL;
+ libc_free(line);
fclose(f);
kernel_config_entries_free(entries, n + 1, cap);
return -1;
}
n++;
}
+ libc_free(line);
fclose(f);
if (n)