eBPF-based security platform
upatch: bound and check the type names the .rodata dump prints
Daniel Borkmann0431b4e3d6f1
diff --git a/Documentation/reference/cli/bpm.rst b/Documentation/reference/cli/bpm.rstindex 36882392..47317cbc 100644--- a/Documentation/reference/cli/bpm.rst+++ b/Documentation/reference/cli/bpm.rst@@ -261,7 +261,11 @@ its ``upatches/*.upatch`` members, with ``--all`` also theNone of them checks the package signature, so each shows a controlcharacter, and a byte that starts no UTF-8 character, as ``?``: in the``README.md`` all but tab, line feed and a carriage return before a line-feed, in a member or upatch name all of them.+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 but+printable 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.push----diff --git a/testing/functional/upatch/t3011-string-bounds.sh b/testing/functional/upatch/t3011-string-bounds.shindex 5ca2e4a6..9230cef3 100755--- a/testing/functional/upatch/t3011-string-bounds.sh+++ b/testing/functional/upatch/t3011-string-bounds.sh@@ -5,7 +5,8 @@# t3011 -- names keep their terminator and are never cut into others: a# 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.+# 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.cd "$(dirname "$0")/../..". ./test-lib.sh@@ -27,6 +28,15 @@ test_expect_success 'a pointer type name that fills the buffer is refused, termi! "$R" 252 2 >"$WORK/full2" && grep -q "terminated=1" "$WORK/full2"'+test_expect_success 'a .rodata type with controls in a name or a loop is not dumped' '+ "$R" dumpable plain >/dev/null &&+ for what in member enum pointee loop proto; do+ rc=0 &&+ "$R" dumpable $what >/dev/null || rc=$? &&+ [ "$rc" -eq 1 ] || 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/helper/test-upatch-rodata.c b/testing/helper/test-upatch-rodata.cindex a8c360a7..22758d08 100644--- a/testing/helper/test-upatch-rodata.c+++ b/testing/helper/test-upatch-rodata.c@@ -42,10 +42,68 @@ static int name_into(size_t len, int ptrs, size_t cap)return ret ? 3 : 0;}+/*+ * 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,+ * 1 when it is refused.+ */+static int dumpable(const char *what)+{+ const char *member = "a", *value = "E", *pointee = "int";+ int ints, en, target, ptr, st, ret = 2;+ struct btf_type *t;+ struct btf *btf;+ size_t done_len;+ __u8 *done;++ if (!strcmp(what, "member"))+ member = "a\x1b[2J";+ else if (!strcmp(what, "enum"))+ value = "E\x07";+ else if (!strcmp(what, "pointee"))+ pointee = "int\xc2\x9b";+ btf = btf__new_empty();+ if (!btf)+ return 2;+ ints = btf__add_int(btf, "int", 4, BTF_INT_SIGNED);+ target = btf__add_int(btf, pointee, 4, BTF_INT_SIGNED);+ en = btf__add_enum(btf, "e", 4);+ if (en > 0 && btf__add_enum_value(btf, value, 0))+ en = -1;+ if (!strcmp(what, "proto")) {+ target = btf__add_func_proto(btf, ints);+ if (target > 0 && btf__add_func_param(btf, "x", ints))+ target = -1;+ }+ ptr = btf__add_ptr(btf, target);+ if (ptr > 0 && !strcmp(what, "loop")) {+ t = (struct btf_type *)btf__type_by_id(btf, ptr);+ t->type = ptr;+ }+ 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) ||+ 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)+ goto out;+ ret = btf_dumpable(btf, (__u32)st, 0, done) ? 0 : 1;+ printf("%s dumpable=%d\n", what, !ret);+ xfree(done, done_len);+out:+ btf__free(btf);+ return ret;+}+int main(int argc, char **argv){__u64 len, ptrs;+ if (argc == 3 && !strcmp(argv[1], "dumpable"))+ return dumpable(argv[2]);if (argc != 3 || parse_u64(argv[1], strlen(argv[1]), 500, &len) ||parse_u64(argv[2], strlen(argv[2]), 4, &ptrs))return 2;diff --git a/util/upatch_rodata.c b/util/upatch_rodata.cindex d8f6e8e2..7b23f55b 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_DECL_MAX 32struct rodata_map {char name[BPF_OBJ_NAME_LEN + 1];@@ -386,6 +387,22 @@ out:return ret;}+/*+ * The BTF may come from an unsigned package, and its names go to the+ * terminal: take printable ASCII alone, all that a kernel would load.+ */+static bool btf_name_printable(const struct btf *btf, __u32 off)+{+ const char *name = btf__name_by_offset(btf, off);++ if (!name)+ return false;+ for (; *name; name++)+ if ((__u8)*name < 0x20 || (__u8)*name > 0x7e)+ return false;+ return true;+}+static __u32 btf_unwrap(const struct btf *btf, __u32 id){const struct btf_type *t;@@ -424,6 +441,8 @@ static int append_type_name(const struct btf *btf, __u32 id, char *buf,if (!t)return -EINVAL;kind = BTF_INFO_KIND(t->info);+ if (!btf_name_printable(btf, t->name_off))+ return -EINVAL;name = btf__name_by_offset(btf, t->name_off);if (kind == BTF_KIND_TYPEDEF && name && name[0]) {n = strsprintf(buf + *off, cap - *off, "%s", name);@@ -755,7 +774,8 @@ static const char *btf_var_name(const struct btf *btf,{const struct btf_type *t = btf__type_by_id(btf, var->type);- return t && BTF_INFO_KIND(t->info) == BTF_KIND_VAR ?+ return t && BTF_INFO_KIND(t->info) == BTF_KIND_VAR &&+ btf_name_printable(btf, t->name_off) ?btf__name_by_offset(btf, t->name_off) : NULL;}@@ -778,32 +798,91 @@ static __printf(2, 0) void print_text(void *ctx, const char *fmt,vfprintf(fp, fmt, args);}+/*+ * The cast libbpf's dumper writes before a value spells its type through+ * modifiers, pointers and arrays, and stops at no loop: a pointer to+ * itself grows its stack until memory runs out, and a function prototype+ * whose parameter leads back to it recurses until the stack does.+ */+static bool btf_decl_printable(const struct btf *btf, __u32 id)+{+ const struct btf_type *t;+ __u32 step;++ for (step = 0; step < RODATA_DECL_MAX; step++) {+ if (!id)+ return true;+ t = btf__type_by_id(btf, id);+ if (!t)+ return false;+ switch (BTF_INFO_KIND(t->info)) {+ case BTF_KIND_PTR:+ case BTF_KIND_VOLATILE:+ case BTF_KIND_CONST:+ case BTF_KIND_RESTRICT:+ case BTF_KIND_TYPE_TAG:+ id = t->type;+ break;+ case BTF_KIND_ARRAY:+ id = ((const struct btf_array *)(t + 1))->type;+ break;+ case BTF_KIND_INT:+ case BTF_KIND_ENUM:+ case BTF_KIND_ENUM64:+ case BTF_KIND_FWD:+ case BTF_KIND_STRUCT:+ case BTF_KIND_UNION:+ case BTF_KIND_TYPEDEF:+ case BTF_KIND_FLOAT:+ return btf_name_printable(btf, t->name_off);+ default:+ return false;+ }+ }+ return false;+}+/** 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; done marks the types already found fine.+ * the JSON view keeps to, with the cast and the member and enumerator+ * names it would print; done marks the types already found fine.*/static bool btf_dumpable(const struct btf *btf, __u32 id, __u32 depth,__u8 *done){const struct btf_type *t = btf__type_by_id(btf, id);+ const struct btf_enum64 *e64;const struct btf_member *m;const struct btf_array *a;+ const struct btf_enum *e;__u32 i;if (!t || depth >= RODATA_JSON_MAX_DEPTH)return false;if (done[id / 8] & (1u << (id % 8)))return true;+ if (!btf_decl_printable(btf, id))+ return false;switch (BTF_INFO_KIND(t->info)) {case BTF_KIND_INT:case BTF_KIND_PTR:+ case BTF_KIND_FLOAT:+ break;case BTF_KIND_ENUM:+ 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;+ break;case BTF_KIND_ENUM64:- case BTF_KIND_FLOAT:+ 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;break;case BTF_KIND_TYPEDEF:case BTF_KIND_VOLATILE:@@ -823,7 +902,8 @@ static bool btf_dumpable(const struct btf *btf, __u32 id, __u32 depth,case BTF_KIND_UNION:m = (const struct btf_member *)(t + 1);for (i = 0; i < BTF_INFO_VLEN(t->info); i++)- if (!btf_dumpable(btf, m[i].type, depth + 1, done))+ if (!btf_name_printable(btf, m[i].name_off) ||+ !btf_dumpable(btf, m[i].type, depth + 1, done))return false;break;default: