Skip to content

eBPF-based security platform

bpm: read a gzip'd kernel config whole, not through gzgets()

Daniel Borkmannc499660a2971
diff --git a/Documentation/upatches/publish/beefile.rst b/Documentation/upatches/publish/beefile.rst
index ba9a9e40..ead6cdcd 100644
--- a/Documentation/upatches/publish/beefile.rst
+++ b/Documentation/upatches/publish/beefile.rst
@@ -304,8 +304,10 @@ to 16 accepted values, and each key may appear once.
Up to 64 keys, each a Kconfig option, once, with its accepted values,
such as ``CONFIG_IPV6 = ["y", "m"]``. An option that is not set matches
``"n"``. The target reads ``/proc/config.gz`` and, when that is
- missing or names none of the options, ``/boot/config-<release>``. A
- target with neither does not match. A line of 1024 bytes or more is
+ missing, is not one whole gzip stream or names none of the options,
+ ``/boot/config-<release>``, each of at most 4 MiB. A target with
+ neither does not match, and one whose configuration cannot be read
+ stops the sync at the operation. A line of 1024 bytes or more is
skipped whole, so its option reads as not set.
``[upatches.tree.selector.labels]``
Up to 64 keys, each a node label, once, with its accepted values, such
diff --git a/testing/unit/bpm_tree_state.c b/testing/unit/bpm_tree_state.c
index faecebf9..4335b26e 100644
--- a/testing/unit/bpm_tree_state.c
+++ b/testing/unit/bpm_tree_state.c
@@ -72,8 +72,50 @@ static void long_line_is_one_line(void)
EXPECT_STREQ(modules_in("CONFIG_MODULES=m", true), "m");
}
+/* A config written gzip'd or not, cut by cut bytes and extra appended. */
+static int read_gz(bool gz, off_t cut, const char *extra)
+{
+ static char values[TREE_MAX_KCONFIG][TREE_STRING_MAX];
+ static int slots[TREE_SELECTOR_HASH_SIZE];
+ bool found[TREE_MAX_KCONFIG] = {};
+ struct tree_selector sel = {};
+ struct tree_selector_kv *kv;
+ struct stat st;
+ int fd, ret;
+
+ ASSERT_EQ(tree_selector_kv_add(&sel.kconfig, &sel.n_kconfig,
+ TREE_MAX_KCONFIG, &kv), 0);
+ strscpy(kv->key, "CONFIG_MODULES", sizeof(kv->key));
+ selector_hash_build(&sel, slots);
+ write_config("CONFIG_MODULES=y\n", gz);
+ ASSERT_EQ(stat(path, &st), 0);
+ ASSERT_EQ(truncate(path, st.st_size - cut), 0);
+ if (extra) {
+ fd = open(path, O_WRONLY | O_APPEND | O_CLOEXEC);
+ ASSERT_TRUE(fd >= 0);
+ ASSERT_EQ(write_complete(fd, extra, strlen(extra)), 0);
+ ASSERT_EQ(close(fd), 0);
+ }
+ ret = read_selector_config_file(path, &sel, slots, values, found, true);
+ tree_selector_free(&sel);
+ unlink(path);
+ if (!ret && (!found[0] || strcmp(values[0], "y")))
+ return 1;
+ return ret;
+}
+
+static void gzip_config_is_one_whole_member(void)
+{
+ EXPECT_EQ(read_gz(true, 0, NULL), 0);
+ EXPECT_EQ(read_gz(true, 1, NULL), -EINVAL);
+ EXPECT_EQ(read_gz(true, 8, NULL), -EINVAL);
+ EXPECT_EQ(read_gz(true, 0, "x"), -EINVAL);
+ EXPECT_EQ(read_gz(false, 0, NULL), -EINVAL);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(long_line_is_one_line),
+ UNIT_CASE(gzip_config_is_one_whole_member),
{}
};
diff --git a/util/bpm_tree_state.c b/util/bpm_tree_state.c
index 97e69c1b..797ceccd 100644
--- a/util/bpm_tree_state.c
+++ b/util/bpm_tree_state.c
@@ -304,60 +304,118 @@ static int selector_config_line(const char *line,
return 0;
}
+#define KCONFIG_MAX (4U << 20) /* a distribution's is some 300 KiB */
+
+/*
+ * The one gzip member of the len bytes at in, with nothing behind it,
+ * into *out, NUL-terminated, of *cap + 1 bytes. Not through gzgets():
+ * zlib 1.3.2 reads on past the end of a member into a next one, finds
+ * that cut short, and has gzclose() fail the whole file for it.
+ */
+static int gunzip(char *in, size_t len, char **out, size_t *out_len,
+ size_t *cap)
+{
+ size_t have = 0, size = 0, want;
+ z_stream z = {};
+ char *buf = NULL, *grown;
+ int zret, ret = 0;
+
+ if (inflateInit2(&z, 16 + MAX_WBITS) != Z_OK)
+ return -ENOMEM;
+ z.next_in = (Bytef *)in;
+ z.avail_in = (uInt)len;
+ do {
+ if (have == size) {
+ if (size >= KCONFIG_MAX) {
+ ret = -EFBIG;
+ break;
+ }
+ want = min(size ? size * 2 : 65536U, KCONFIG_MAX);
+ grown = xreallocarray(buf, size ? size + 1 : 0, want + 1, 1);
+ if (!grown) {
+ ret = -ENOMEM;
+ break;
+ }
+ buf = grown;
+ size = want;
+ }
+ z.next_out = (Bytef *)buf + have;
+ z.avail_out = (uInt)(size - have);
+ zret = inflate(&z, Z_NO_FLUSH);
+ have = size - z.avail_out;
+ if (zret == Z_MEM_ERROR)
+ ret = -ENOMEM;
+ else if (zret != Z_OK && zret != Z_STREAM_END)
+ ret = -EINVAL;
+ } while (!ret && zret != Z_STREAM_END);
+ if (!ret && z.avail_in)
+ ret = -EINVAL;
+ inflateEnd(&z);
+ if (ret) {
+ xfree(buf, size ? size + 1 : 0);
+ return ret;
+ }
+ buf[have] = '\0';
+ *out = buf;
+ *out_len = have;
+ *cap = size + 1;
+ return 0;
+}
+
+/*
+ * A line of sizeof(line) bytes or more with its newline is skipped whole,
+ * as a part from the middle of a long string value would read as an
+ * option of its own: its option reads as not set.
+ */
+static int selector_config_text(const char *text, size_t len,
+ const struct tree_selector *s,
+ const int slots[TREE_SELECTOR_HASH_SIZE],
+ char values[][TREE_STRING_MAX], bool found[])
+{
+ char line[1024];
+ size_t off, n;
+ int ret;
+
+ for (off = 0; off < len; off += n + 1) {
+ const char *nl = memchr(text + off, '\n', len - off);
+
+ n = nl ? (size_t)(nl - text) - off : len - off;
+ if (n >= sizeof(line) - 1)
+ continue;
+ memcpy(line, text + off, n);
+ line[n] = '\0';
+ ret = selector_config_line(line, s, slots, values, found);
+ if (ret)
+ return ret;
+ }
+ return 0;
+}
+
static int read_selector_config_file(const char *path,
const struct tree_selector *s,
const int slots[TREE_SELECTOR_HASH_SIZE],
char values[][TREE_STRING_MAX],
bool found[], bool gz)
{
- bool skip = false, whole;
- char line[1024];
- int ret = 0;
-
- /*
- * A line longer than the buffer comes in parts, and a part from the
- * middle of a long string value would read as an option of its own:
- * none of such a line is taken, and its option reads as not set.
- */
- if (gz) {
- gzFile f = gzopen(path, "rb");
+ size_t len, text_len, cap;
+ char *raw, *text;
+ int ret;
- if (!f)
- return -errno;
- while (gzgets(f, line, sizeof(line))) {
- whole = strchr(line, '\n') || gzeof(f);
- if (!skip && whole)
- ret = selector_config_line(line, s, slots, values,
- found);
- skip = !whole;
- if (ret)
- break;
- }
- if (!ret && gzclose(f) != Z_OK)
- ret = -EIO;
- else if (ret)
- gzclose(f);
+ ret = read_file(path, KCONFIG_MAX, &raw, &len);
+ if (ret)
return ret;
+ if (gz) {
+ ret = gunzip(raw, len, &text, &text_len, &cap);
+ xfree(raw, len + 1);
+ if (ret)
+ return ret;
+ } else {
+ text = raw;
+ text_len = len;
+ cap = len + 1;
}
-
- {
- FILE *f = fopen(path, "re");
-
- if (!f)
- return -errno;
- while (fgets(line, sizeof(line), f)) {
- whole = strchr(line, '\n') || feof(f);
- if (!skip && whole)
- ret = selector_config_line(line, s, slots, values,
- found);
- skip = !whole;
- if (ret)
- break;
- }
- if (!ret && ferror(f))
- ret = errno ? -errno : -EIO;
- fclose(f);
- }
+ ret = selector_config_text(text, text_len, s, slots, values, found);
+ xfree(text, cap);
return ret;
}