eBPF-based security platform
upatch: bound the values and depth of a .rodata text dump
Daniel Borkmann50306d862c17
diff --git a/Documentation/contribute/testing/proof-coverage.rst b/Documentation/contribute/testing/proof-coverage.rstindex fffe812a..01b6d170 100644--- a/Documentation/contribute/testing/proof-coverage.rst+++ b/Documentation/contribute/testing/proof-coverage.rst@@ -715,5 +715,5 @@ composition: ``upatch_parse()`` and ``upatch_open_memory()`` join theheader, the signed sections, the decoders and the policy labels theproofs cover one by one, and a proof of the whole file runs out ofmemory. A program table of two or more programs, ``find_rodata_data()``-on loaders of other shapes, ``btf_dumpable()``, the ``.rodata`` printers+on loaders of other shapes, ``btf_dump_cost()``, the ``.rodata`` printersand the libbpf calls behind ``btf_rodata_btf()`` have no proof.diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex da5c6261..2f04b205 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -265,7 +265,11 @@ feed, in a member or upatch name all of them. ``info data`` refuses a``.rodata`` whose BTF names a variable or a type with anything butprintable ASCII, and without ``--json`` also a member or an enumerator,or spells a type through a function prototype or through a chain of-more than 32 modifiers, pointers and arrays.+more than 32 modifiers, pointers and arrays. Without ``--json`` it also+refuses a ``.rodata`` whose types nest more than eight deep, typedefs+and modifiers counted, or whose dump would print more than 1048576+values; with ``--json``, one whose structs and arrays nest more than+eight deep or whose JSON would exceed 8 MiB.push----diff --git a/testing/functional/upatch/t3011-string-bounds.sh b/testing/functional/upatch/t3011-string-bounds.shindex e030a599..c8358b40 100755--- a/testing/functional/upatch/t3011-string-bounds.sh+++ b/testing/functional/upatch/t3011-string-bounds.sh@@ -6,7 +6,8 @@# pointer type whose name fills the buffer, a signed name section with# no terminator, an upatch name the targets refuse or the header cannot# hold, and a tree repository longer than its field. The .rodata view-# prints no BTF name with a control in it and spells no type that loops.+# prints no BTF name with a control in it, spells no type that loops and+# dumps no type nested too deep or with more values than it prints.cd "$(dirname "$0")/../..". ./test-lib.sh@@ -37,6 +38,15 @@ test_expect_success 'a .rodata type with controls in a name or a loop is not dumdone'+test_expect_success 'a .rodata type too deep or with too many values is not dumped' '+ "$R" dumpable shared >/dev/null &&+ ! "$R" dumpable deep >"$WORK/deep" && grep -q "rc=-22" "$WORK/deep" &&+ for what in elements overlaid; do+ ! "$R" dumpable $what >"$WORK/$what" &&+ grep -q "rc=-7" "$WORK/$what" || return 1+ done+'+test_expect_success 'an upatch name the targets refuse is not signed' '! "$B" --unload ../x -o "$WORK/bad.upatch" --payload-sha256 "$PAYLOAD" \--keys-dir "$WORK/keys" >/dev/null 2>"$WORK/bad.err" &&diff --git a/testing/fuzz/corpus/rodata/many-elements b/testing/fuzz/corpus/rodata/many-elementsnew file mode 100644index 00000000..c4a647c8Binary files /dev/null and b/testing/fuzz/corpus/rodata/many-elements differdiff --git a/testing/helper/test-upatch-rodata.c b/testing/helper/test-upatch-rodata.cindex 22758d08..185c91d1 100644--- a/testing/helper/test-upatch-rodata.c+++ b/testing/helper/test-upatch-rodata.c@@ -42,6 +42,34 @@ static int name_into(size_t len, int ptrs, size_t cap)return ret ? 3 : 0;}+/* A chain of n typedefs to type, or -1. */+static int typedefs(struct btf *btf, const char *prefix, int n, int type)+{+ char name[16];++ while (type > 0 && n--) {+ if (strsprintf(name, sizeof(name), "%s%d", prefix, n) < 0)+ return -1;+ type = btf__add_typedef(btf, name, type);+ }+ return type;+}++/* Four levels of 64 members all laid over the bytes of an int, or -1. */+static int overlaid(struct btf *btf, int type)+{+ int level, inner, i;++ for (level = 0; type > 0 && level < 4; level++) {+ inner = type;+ type = btf__add_struct(btf, NULL, 4);+ for (i = 0; type > 0 && i < 64; i++)+ if (btf__add_field(btf, NULL, inner, 0, 0))+ type = -1;+ }+ return type;+}+/** A struct of an int, an enum and a pointer, spoiled as what says,* through the walk that guards libbpf's dumper: 0 when it may be dumped,@@ -50,11 +78,11 @@ static int name_into(size_t len, int ptrs, size_t cap)static int dumpable(const char *what){const char *member = "a", *value = "E", *pointee = "int";- int ints, en, target, ptr, st, ret = 2;+ int ints, en, target, ptr, st, a, b, ret = 2;+ struct dump_cost *cost;struct btf_type *t;struct btf *btf;- size_t done_len;- __u8 *done;+ __u32 types;if (!strcmp(what, "member"))member = "a\x1b[2J";@@ -80,19 +108,31 @@ static int dumpable(const char *what)t = (struct btf_type *)btf__type_by_id(btf, ptr);t->type = ptr;}+ a = ints;+ b = en;+ if (!strcmp(what, "elements")) {+ a = btf__add_array(btf, ints, ints, 1u << 20);+ } else if (!strcmp(what, "overlaid")) {+ a = overlaid(btf, ints);+ } else if (!strcmp(what, "shared") || !strcmp(what, "deep")) {+ a = typedefs(btf, "t", 6, ints);+ b = !strcmp(what, "deep") ? typedefs(btf, "u", 2, a) : a;+ }st = btf__add_struct(btf, "s", 16);- if (ints <= 0 || target <= 0 || en <= 0 || ptr <= 0 || st <= 0 ||- btf__add_field(btf, member, ints, 0, 0) ||- btf__add_field(btf, "b", en, 32, 0) ||+ if (ints <= 0 || target <= 0 || en <= 0 || ptr <= 0 || a <= 0 ||+ b <= 0 || st <= 0 ||+ btf__add_field(btf, member, a, 0, 0) ||+ btf__add_field(btf, "b", b, 32, 0) ||btf__add_field(btf, "p", ptr, 64, 0))goto out;- done_len = btf__type_cnt(btf) / 8 + 1;- done = xcalloc(done_len, 1);- if (!done)+ types = btf__type_cnt(btf);+ cost = xcalloc(types, sizeof(*cost));+ if (!cost)goto out;- ret = btf_dumpable(btf, (__u32)st, 0, done) ? 0 : 1;- printf("%s dumpable=%d\n", what, !ret);- xfree(done, done_len);+ ret = btf_dump_cost(btf, (__u32)st, 0, cost);+ printf("%s dumpable=%d rc=%d\n", what, !ret, ret);+ ret = ret ? 1 : 0;+ xfree(cost, types * sizeof(*cost));out:btf__free(btf);return ret;diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex bc6b7455..d33c169b 100644--- a/util/upatch_rodata.c+++ b/util/upatch_rodata.c@@ -26,6 +26,7 @@#define RODATA_MAPS_MAX 64#define RODATA_JSON_MAX_DEPTH 8+#define RODATA_TEXT_MAX_VALUES (1u << 20)#define RODATA_DECL_MAX 32struct rodata_map {@@ -844,31 +845,42 @@ static bool btf_decl_printable(const struct btf *btf, __u32 id)return false;}+/* What the dumper visits of a value of a type: values printed, types deep. */+struct dump_cost {+ __u32 values;+ __u32 height;+};+/** libbpf's dumper follows member, element and modifier types with no* bound, and the BTF may come from an unsigned package: a VAR whose type* is the DATASEC holding it sends it round until the stack is gone, and- * zero-sized elements counted in billions keep it busy. Walk what it- * would follow first, through value types alone and within the depth- * the JSON view keeps to, with the cast and the member and enumerator- * names it would print; done marks the types already found fine.+ * billions of elements, of zero size or not, or members laid over the+ * same bytes keep it busy. Walk what it would follow first, through value+ * types alone, with the cast and the member and enumerator names it would+ * print, and count what it would visit: -EINVAL for more than+ * RODATA_JSON_MAX_DEPTH types deep, -E2BIG for more than+ * RODATA_TEXT_MAX_VALUES values. cost keeps the counts of the types found+ * fine, so a type reached again brings its depth and values along.*/-static bool btf_dumpable(const struct btf *btf, __u32 id, __u32 depth,- __u8 *done)+static int btf_dump_cost(const struct btf *btf, __u32 id, __u32 depth,+ struct dump_cost *cost){const struct btf_type *t = btf__type_by_id(btf, id);+ struct dump_cost c = { .values = 1, .height = 1 };const struct btf_enum64 *e64;const struct btf_member *m;const struct btf_array *a;const struct btf_enum *e;- __u32 i;+ __u32 i, child;+ int ret;if (!t || depth >= RODATA_JSON_MAX_DEPTH)- return false;- if (done[id / 8] & (1u << (id % 8)))- return true;+ return -EINVAL;+ if (cost[id].values)+ return 0;if (!btf_decl_printable(btf, id))- return false;+ return -EINVAL;switch (BTF_INFO_KIND(t->info)) {case BTF_KIND_INT:case BTF_KIND_PTR:@@ -878,41 +890,63 @@ static bool btf_dumpable(const struct btf *btf, __u32 id, __u32 depth,e = (const struct btf_enum *)(t + 1);for (i = 0; i < BTF_INFO_VLEN(t->info); i++)if (!btf_name_printable(btf, e[i].name_off))- return false;+ return -EINVAL;break;case BTF_KIND_ENUM64:e64 = (const struct btf_enum64 *)(t + 1);for (i = 0; i < BTF_INFO_VLEN(t->info); i++)if (!btf_name_printable(btf, e64[i].name_off))- return false;+ return -EINVAL;break;case BTF_KIND_TYPEDEF:case BTF_KIND_VOLATILE:case BTF_KIND_CONST:case BTF_KIND_RESTRICT:case BTF_KIND_TYPE_TAG:- if (!btf_dumpable(btf, t->type, depth + 1, done))- return false;+ ret = btf_dump_cost(btf, t->type, depth + 1, cost);+ if (ret)+ return ret;+ c.values = cost[t->type].values;+ c.height = cost[t->type].height + 1;break;case BTF_KIND_ARRAY:a = (const struct btf_array *)(t + 1);- if ((a->nelems && btf__resolve_size(btf, a->type) <= 0) ||- !btf_dumpable(btf, a->type, depth + 1, done))- return false;+ if (a->nelems && btf__resolve_size(btf, a->type) <= 0)+ return -EINVAL;+ ret = btf_dump_cost(btf, a->type, depth + 1, cost);+ if (ret)+ return ret;+ if (check_mul_overflow(cost[a->type].values, a->nelems,+ &child) ||+ check_add_overflow(c.values, child, &c.values))+ return -E2BIG;+ c.height = cost[a->type].height + 1;break;case BTF_KIND_STRUCT:case BTF_KIND_UNION:m = (const struct btf_member *)(t + 1);- for (i = 0; i < BTF_INFO_VLEN(t->info); i++)- if (!btf_name_printable(btf, m[i].name_off) ||- !btf_dumpable(btf, m[i].type, depth + 1, done))- return false;+ for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {+ if (!btf_name_printable(btf, m[i].name_off))+ return -EINVAL;+ ret = btf_dump_cost(btf, m[i].type, depth + 1, cost);+ if (ret)+ return ret;+ if (check_add_overflow(c.values,+ cost[m[i].type].values,+ &c.values))+ return -E2BIG;+ c.height = max(c.height, cost[m[i].type].height + 1);+ }break;default:- return false;+ return -EINVAL;}- done[id / 8] |= 1u << (id % 8);- return true;+ if (c.height > RODATA_JSON_MAX_DEPTH)+ return -EINVAL;+ if (c.values > RODATA_TEXT_MAX_VALUES)+ return -E2BIG;+ cost[id] = c;+ return 0;}static int print_rodata_text(const struct upatch *u,@@ -929,10 +963,10 @@ static int print_rodata_text(const struct upatch *u,view->type_id);const struct btf_var_secinfo *vars =(const struct btf_var_secinfo *)(sec + 1);- size_t done_len = btf__type_cnt(view->btf) / 8 + 1;- __u32 i, vlen = BTF_INFO_VLEN(sec->info);+ __u32 i, values = 0, vlen = BTF_INFO_VLEN(sec->info);+ __u32 types = btf__type_cnt(view->btf);char name[UPATCH_NAME_MAX + 1];- __u8 *done;+ struct dump_cost *cost;int ret = 0;upatch_header_name(u, name);@@ -940,12 +974,12 @@ static int print_rodata_text(const struct upatch *u,printf("upatch %s (%s) .rodata: %u bytes at data+%u, file+%u\n",name, path, view->size, view->data_offset,u->hdr.data_off + view->data_offset);- done = xcalloc(done_len, 1);- if (!done)+ cost = xcalloc(types, sizeof(*cost));+ if (!cost)return -ENOMEM;dump = btf_dump__new(view->btf, print_text, stdout, NULL);if (libbpf_get_error(dump)) {- xfree(done, done_len);+ xfree(cost, types * sizeof(*cost));return -EINVAL;}for (i = 0; i < vlen; i++) {@@ -964,11 +998,20 @@ static int print_rodata_text(const struct upatch *u,if (ret)goto out;if (vars[i].offset > view->size ||- vars[i].size > view->size - vars[i].offset ||- !btf_dumpable(view->btf, type_id, 0, done)) {+ vars[i].size > view->size - vars[i].offset) {ret = -EINVAL;goto out;}+ ret = btf_dump_cost(view->btf, type_id, 0, cost);+ if (!ret &&+ (check_add_overflow(values, cost[type_id].values, &values) ||+ values > RODATA_TEXT_MAX_VALUES))+ ret = -E2BIG;+ if (ret == -E2BIG)+ pr_err("BTF text output exceeds %u values",+ RODATA_TEXT_MAX_VALUES);+ if (ret)+ goto out;printf("\n%s: %s, size=%u, rodata+%u, data+%u, file+%u = ",var_name, type, vars[i].size, vars[i].offset,view->data_offset + vars[i].offset,@@ -983,7 +1026,7 @@ static int print_rodata_text(const struct upatch *u,putchar('\n');out:btf_dump__free(dump);- xfree(done, done_len);+ xfree(cost, types * sizeof(*cost));return min(ret, 0);}