tag_of_vmlinux attaches to the bpf_dummy_reg() of bpf_testmod. It shares the FUNC of vmlinux with a bpf_dummy_reg() which has a tag, and the verifier must not take that tag. But the one of vmlinux takes a struct bpf_link, and the build is going to stop tagging such functions. The test would then pass without testing anything. Use do_epoll_create() instead, and load a program on the one of vmlinux: if it loses its tag, that test fails. It needs CONFIG_EPOLL, add it to the config of the selftests. (It is on by default, but make it explicit.) Signed-off-by: Anton Protopopov --- tools/testing/selftests/bpf/config | 1 + .../selftests/bpf/progs/fmodret_retval.c | 17 ++++++++++++----- .../bpf/test_kmods/bpf_testmod_retval.c | 6 ++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/bpf/config b/tools/testing/selftests/bpf/config index 30b744844ef5..ce0db4be4d26 100644 --- a/tools/testing/selftests/bpf/config +++ b/tools/testing/selftests/bpf/config @@ -30,6 +30,7 @@ CONFIG_DMABUF_HEAPS=y CONFIG_DMABUF_HEAPS_SYSTEM=y CONFIG_DUMMY=y CONFIG_DYNAMIC_FTRACE=y +CONFIG_EPOLL=y CONFIG_FPROBE=y CONFIG_FTRACE_SYSCALLS=y CONFIG_FUNCTION_ERROR_INJECTION=y diff --git a/tools/testing/selftests/bpf/progs/fmodret_retval.c b/tools/testing/selftests/bpf/progs/fmodret_retval.c index 112f1575ce1f..ae2332bff2f0 100644 --- a/tools/testing/selftests/bpf/progs/fmodret_retval.c +++ b/tools/testing/selftests/bpf/progs/fmodret_retval.c @@ -176,13 +176,20 @@ int BPF_PROG(not_a_target, int value, int ret) return 0; } +SEC("fmod_ret/do_epoll_create") +__success +int BPF_PROG(tag_in_vmlinux, int flags, int ret) +{ + return 0; +} + /* - * vmlinux has a bpf_dummy_reg() of the same type, so the two share the FUNC of - * vmlinux. That one returns an error and has a tag. This one returns only 0. + * The do_epoll_create() of bpf_testmod has the type of the one of vmlinux, so + * the two share the FUNC of vmlinux. This one returns only 0. */ -SEC("fmod_ret/bpf_testmod:bpf_dummy_reg") -__failure __msg("bpf_dummy_reg() is not modifiable") -int BPF_PROG(tag_of_vmlinux, void *kdata, struct bpf_link *link, int ret) +SEC("fmod_ret/bpf_testmod:do_epoll_create") +__failure __msg("do_epoll_create() is not modifiable") +int BPF_PROG(tag_of_vmlinux, int flags, int ret) { return 0; } diff --git a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c index 85722e48d34c..ba48f0f393f3 100644 --- a/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c +++ b/tools/testing/selftests/bpf/test_kmods/bpf_testmod_retval.c @@ -159,3 +159,9 @@ static __used noinline __init int bpf_testmod_retval_init(int value) { return value ? -EINVAL : -ENOMEM; } + +/* vmlinux has a do_epoll_create() of this type, which returns errors */ +static __used noinline int do_epoll_create(int flags) +{ + return 0; +} -- 2.43.0 A program on tracing_iter_filter() which returns -EACCES makes the verifier miss that a global subprog throws. A function which takes or returns a bpf_* or btf_* type, or struct btf, works for BPF itself: for the verifier, a link or a map. Do not tag it. __sys_bpf() loses its tag. A program can still deny a bpf command on the syscall, which is on the error injection list, or on a security_bpf*() hook. Signed-off-by: Anton Protopopov --- tools/bpf/resolve_btfids/fmodret.c | 34 ++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c index e1afdb6dedd5..aaff58718da3 100644 --- a/tools/bpf/resolve_btfids/fmodret.c +++ b/tools/bpf/resolve_btfids/fmodret.c @@ -56,6 +56,37 @@ static bool fits_trampoline(const struct btf *btf, const struct btf_type *func) return btf_vlen(t) <= 12; } +static bool is_bpf_type(const struct btf *btf, __u32 type) +{ + while (type) { + const struct btf_type *t = btf__type_by_id(btf, type); + const char *name = btf__name_by_offset(btf, t->name_off); + + if (!strncmp(name, "bpf_", 4) || !strcmp(name, "btf") || !strncmp(name, "btf_", 4)) + return true; + if (btf_is_array(t)) + type = btf_array(t)->type; + else if (btf_is_ptr(t) || btf_is_mod(t) || btf_is_typedef(t)) + type = t->type; + else + return false; + } + return false; +} + +static bool has_bpf_types(const struct btf *btf, const struct btf_type *func) +{ + const struct btf_type *proto = btf__type_by_id(btf, func->type); + unsigned int i; + + if (is_bpf_type(btf, proto->type)) + return true; + for (i = 0; i < btf_vlen(proto); i++) + if (is_bpf_type(btf, btf_params(proto)[i].type)) + return true; + return false; +} + /* * The codes of include/linux/errno.h, 512 and up, never reach user space. They, * -EAGAIN, -EMSGSIZE and -EINPROGRESS tell the caller to retry, to go on or to @@ -220,6 +251,9 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) size = int_return_size(btf, t); if ((size != 4 && size != 8) || !fits_trampoline(btf, t)) continue; + /* Skip functions which take or return BPF objects */ + if (has_bpf_types(btf, t)) + continue; /* Init code is gone before a program can attach */ name = btf__name_by_offset(btf, t->name_off); func = image_func(&img, name); -- 2.43.0 The verifier relies on what a kfunc or a helper does, also on an error. bpf_skb_load_bytes() clears the buffer on an error, and the verifier takes it as initialized. After a skip a socket filter reads what was on the kernel stack. Do not tag the functions with the "bpf_kfunc" decl tag. A helper takes only u64 values, so the bpf_* types do not tell it. BPF_CALL_x() declares a typedef btf_ for each: do not tag the function of such a typedef. Signed-off-by: Anton Protopopov --- tools/bpf/resolve_btfids/fmodret.c | 32 ++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c index aaff58718da3..7d26a5c9b028 100644 --- a/tools/bpf/resolve_btfids/fmodret.c +++ b/tools/bpf/resolve_btfids/fmodret.c @@ -87,6 +87,29 @@ static bool has_bpf_types(const struct btf *btf, const struct btf_type *func) return false; } +/* Find kfuncs and helpers */ +static bool *find_bpf_funcs(const struct btf *btf) +{ + const struct btf *base = btf__base_btf(btf); + __u32 id, nr_types = btf__type_cnt(btf); + bool *funcs = calloc(nr_types, sizeof(*funcs)); + + for (id = base ? btf__type_cnt(base) : 1; funcs && id < nr_types; id++) { + const struct btf_type *t = btf__type_by_id(btf, id); + const char *name = btf__name_by_offset(btf, t->name_off); + + if (btf_is_decl_tag(t) && !strcmp(name, "bpf_kfunc")) + funcs[t->type] = true; + if (btf_is_typedef(t) && !strncmp(name, "btf_", 4) && strncmp(name, "btf_trace_", 10)) { + int func = btf__find_by_name_kind(btf, name + 4, BTF_KIND_FUNC); + + if (func > 0) + funcs[func] = true; + } + } + return funcs; +} + /* * The codes of include/linux/errno.h, 512 and up, never reach user space. They, * -EAGAIN, -EMSGSIZE and -EINPROGRESS tell the caller to retry, to go on or to @@ -225,6 +248,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) const struct btf *base = btf__base_btf(btf); __u32 id, nr_types = btf__type_cnt(btf); struct scan *scan = NULL; + bool *bpf_funcs = NULL; struct image img; char counts[200]; int err; @@ -237,6 +261,10 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) if (!scan) goto out; set_protos(scan, &img, btf); + err = -ENOMEM; + bpf_funcs = find_bpf_funcs(btf); + if (!bpf_funcs) + goto out; for (id = base ? btf__type_cnt(base) : 1, err = 0; id < nr_types && err >= 0; id++) { const struct btf_type *t = btf__type_by_id(btf, id); @@ -254,6 +282,9 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) /* Skip functions which take or return BPF objects */ if (has_bpf_types(btf, t)) continue; + /* Skip kfuncs and helpers which escaped the previous check */ + if (bpf_funcs[id]) + continue; /* Init code is gone before a program can attach */ name = btf__name_by_offset(btf, t->name_off); func = image_func(&img, name); @@ -291,6 +322,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) printf(" FMODRET %s\n", counts); } out: + free(bpf_funcs); scan_close(scan); image_close(&img); return err < 0 ? err : 0; -- 2.43.0 ftrace reads the code it patches with copy_from_kernel_nofault(). A program which makes it fail breaks the detach of fentry on x86: unregister_fentry() update_ftrace_direct_del() returns 0 ftrace_replace_code() ftrace_bug(), no text poke ftrace_verify_code() -EFAULT bpf_tramp_image_put() the function still calls the freed image On arm64 ftrace reads and writes the code with aarch64_insn_read() and aarch64_insn_patch_text_nosync(), which calls aarch64_insn_write(). They are traceable without KPROBES. The same holds for them. Two more functions break BPF after a skip: target error after the skip ----------------------------- ------- --------------------------------- copy_to_kernel_nofault() -EFAULT arm64, with KPROBES too: __aarch64_insn_write() fails. ftrace_bug(), no text poke. text_poke_memcpy() ignores it. bpf_arch_text_copy() returns dst. The new prog is all BRK insns tracepoint_probe_unregister() -ENOENT bpf_raw_tp_link_release() ignores it. __bpf_trace_run() reads the freed link Do not tag these six. Signed-off-by: Anton Protopopov --- tools/bpf/resolve_btfids/fmodret.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c index 7d26a5c9b028..b425208702b4 100644 --- a/tools/bpf/resolve_btfids/fmodret.c +++ b/tools/bpf/resolve_btfids/fmodret.c @@ -87,6 +87,24 @@ static bool has_bpf_types(const struct btf *btf, const struct btf_type *func) return false; } +static bool excluded_by_name(const char *name) +{ + static const char *const names[] = { + "copy_from_kernel_nofault", + "copy_to_kernel_nofault", + "aarch64_insn_read", + "aarch64_insn_write", + "aarch64_insn_patch_text_nosync", + "tracepoint_probe_unregister", + }; + unsigned int i; + + for (i = 0; i < sizeof(names) / sizeof(names[0]); i++) + if (!strcmp(name, names[i])) + return true; + return false; +} + /* Find kfuncs and helpers */ static bool *find_bpf_funcs(const struct btf *btf) { @@ -290,6 +308,8 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) func = image_func(&img, name); if (!func || !strcmp(func->section, ".init.text")) continue; + if (excluded_by_name(name)) + continue; nr_scanned++; err = scan_func(scan, func, &values); if (err == -EINVAL) { -- 2.43.0 bpf_unlocked_sk_getsockopt() has no tag, but it passes optval, an ARG_PTR_TO_UNINIT_MEM buffer, to __bpf_getsockopt(). That one clears the buffer on an error. It takes no BPF type and is in net/core, so it keeps its tag. After a skip the program reads its stack. __bpf_skb_load_bytes() has the same memset() before -EFAULT. Do not tag bpf_*() and __bpf_*() of vmlinux. This also covers bpf_set_dentry_xattr_locked() and bpf_remove_dentry_xattr_locked(), kfuncs without the "bpf_kfunc" tag: specialize_kfunc() calls them instead of the kfuncs without _locked. The functions of modules keep their tags, the selftests need bpf_testmod_retval_*(). Signed-off-by: Anton Protopopov --- tools/bpf/resolve_btfids/fmodret.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c index b425208702b4..d2d4db1ca3d5 100644 --- a/tools/bpf/resolve_btfids/fmodret.c +++ b/tools/bpf/resolve_btfids/fmodret.c @@ -105,6 +105,12 @@ static bool excluded_by_name(const char *name) return false; } +/* bpf_*() and __bpf_*() */ +static bool excluded_by_prefix(const char *name, bool vmlinux) +{ + return vmlinux && !strncmp(name + strspn(name, "_"), "bpf_", 4); +} + /* Find kfuncs and helpers */ static bool *find_bpf_funcs(const struct btf *btf) { @@ -310,6 +316,8 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) continue; if (excluded_by_name(name)) continue; + if (excluded_by_prefix(name, !base)) + continue; nr_scanned++; err = scan_func(scan, func, &values); if (err == -EINVAL) { -- 2.43.0 Not every function of kernel/bpf and of the JIT has a BPF type in its prototype or a bpf_ prefix, e.g. check_attach_modify_return() of the verifier, range_tree_set() and btf_get_fd_by_id(). Let gen-btf.sh list the symbols of kernel/bpf/built-in.a and arch/${SRCARCH}/net/built-in.a with ${NM}, which reads what ${CC} writes, LTO bitcode included. resolve_btfids gets the list with --fmodret_deny and does not tag these functions. Weak symbols count too. Only one tagged function outside BPF has a weak copy in kernel/bpf, skb_copy_bits(), and it loses its tag. BPF helpers call it, so this is acceptable. Co-developed-by: Alexei Starovoitov Signed-off-by: Alexei Starovoitov Signed-off-by: Anton Protopopov --- scripts/gen-btf.sh | 10 +++++++ tools/bpf/resolve_btfids/fmodret.c | 46 ++++++++++++++++++++++++++++-- tools/bpf/resolve_btfids/fmodret.h | 4 +-- tools/bpf/resolve_btfids/main.c | 5 +++- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/scripts/gen-btf.sh b/scripts/gen-btf.sh index b17f1189298a..af40d746f218 100755 --- a/scripts/gen-btf.sh +++ b/scripts/gen-btf.sh @@ -75,8 +75,17 @@ gen_btf_data() --btf_encode_detached=${btf1} \ "${ELF_FILE}" + # The code of BPF itself is not a target of fmod_ret + fmodret_deny= + if [ -z "${BTF_BASE}" ] && is_enabled CONFIG_BPF_FMODRET_SCAN; then + fmodret_deny="${ELF_FILE}.fmodret_deny" + ${NM} --defined-only ${objtree}/kernel/bpf/built-in.a \ + ${objtree}/arch/${SRCARCH}/net/built-in.a > ${fmodret_deny} + fi + ${RESOLVE_BTFIDS} ${RESOLVE_BTFIDS_FLAGS} \ ${BTF_BASE:+--btf_base ${BTF_BASE}} \ + ${fmodret_deny:+--fmodret_deny ${fmodret_deny}} \ ${inline} \ --btf ${btf1} "${ELF_FILE}" } @@ -148,6 +157,7 @@ cleanup() rm -f "${ELF_FILE}.BTF" rm -f "${ELF_FILE}.BTF.inline" rm -f "${ELF_FILE}.BTF.inline.link" + rm -f "${ELF_FILE}.fmodret_deny" if [ "${BTFGEN_MODE}" = "module" ]; then rm -f "${ELF_FILE}.BTF.base" rm -f "${ELF_FILE}.BTF_ids" diff --git a/tools/bpf/resolve_btfids/fmodret.c b/tools/bpf/resolve_btfids/fmodret.c index d2d4db1ca3d5..b950d6b52554 100644 --- a/tools/bpf/resolve_btfids/fmodret.c +++ b/tools/bpf/resolve_btfids/fmodret.c @@ -266,13 +266,46 @@ static void set_protos(struct scan *scan, const struct image *img, const struct } } -int fmodret_tag(Elf *elf, struct btf *btf, int verbose) +/* + * Mark the functions of IMG which are in PATH, the output of nm. The line of a + * symbol is "
". The other lines have fewer words. + */ +static bool *read_denied(const struct image *img, const char *path) +{ + bool *denied = calloc(img->nr_funcs, sizeof(*denied)); + FILE *f = fopen(path, "r"); + char line[1024], name[1024]; + + if (!denied || !f) { + fprintf(stderr, "FAILED: --fmodret_deny: cannot read %s\n", path); + free(denied); + denied = NULL; + } + while (denied && fgets(line, sizeof(line), f)) { + const struct image_func *func = NULL; + + if (sscanf(line, "%*s %*s %1023s", name) == 1) + func = image_func(img, name); + if (func) + denied[func - img->funcs] = true; + } + if (denied && !feof(f)) { + fprintf(stderr, "FAILED: --fmodret_deny: cannot read %s\n", path); + free(denied); + denied = NULL; + } + if (f) + fclose(f); + return denied; +} + +int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose) { size_t nr_funcs = 0, nr_scanned = 0, nr_undecodable = 0, nr_tags = 0; const struct btf *base = btf__base_btf(btf); __u32 id, nr_types = btf__type_cnt(btf); struct scan *scan = NULL; - bool *bpf_funcs = NULL; + bool *bpf_funcs = NULL, *denied = NULL; struct image img; char counts[200]; int err; @@ -289,6 +322,11 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) bpf_funcs = find_bpf_funcs(btf); if (!bpf_funcs) goto out; + if (deny_path) { + denied = read_denied(&img, deny_path); + if (!denied) + goto out; + } for (id = base ? btf__type_cnt(base) : 1, err = 0; id < nr_types && err >= 0; id++) { const struct btf_type *t = btf__type_by_id(btf, id); @@ -318,6 +356,9 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) continue; if (excluded_by_prefix(name, !base)) continue; + /* Skip the code of kernel/bpf and of the JIT */ + if (denied && denied[func - img.funcs]) + continue; nr_scanned++; err = scan_func(scan, func, &values); if (err == -EINVAL) { @@ -350,6 +391,7 @@ int fmodret_tag(Elf *elf, struct btf *btf, int verbose) printf(" FMODRET %s\n", counts); } out: + free(denied); free(bpf_funcs); scan_close(scan); image_close(&img); diff --git a/tools/bpf/resolve_btfids/fmodret.h b/tools/bpf/resolve_btfids/fmodret.h index d61a9de026df..177b116ee799 100644 --- a/tools/bpf/resolve_btfids/fmodret.h +++ b/tools/bpf/resolve_btfids/fmodret.h @@ -6,7 +6,7 @@ struct btf; -/* Tag the functions of ELF which return errors in BTF */ -int fmodret_tag(Elf *elf, struct btf *btf, int verbose); +/* Tag the functions of ELF which return errors in BTF, but not those in DENY_PATH */ +int fmodret_tag(Elf *elf, struct btf *btf, const char *deny_path, int verbose); #endif diff --git a/tools/bpf/resolve_btfids/main.c b/tools/bpf/resolve_btfids/main.c index 0ac895222a14..919b301e88f3 100644 --- a/tools/bpf/resolve_btfids/main.c +++ b/tools/bpf/resolve_btfids/main.c @@ -2104,6 +2104,7 @@ int main(int argc, const char **argv) bool fatal_warnings = false; bool resolve_btfids = true; bool fmodret_scan = false; + const char *fmodret_deny = NULL; char out_path[PATH_MAX]; struct option btfid_options[] = { @@ -2121,6 +2122,8 @@ int main(int argc, const char **argv) "extract location BTF into a .BTF.inline file"), OPT_BOOLEAN(0, "fmodret_scan", &fmodret_scan, "tag the functions which return errors as fmod_ret targets"), + OPT_STRING(0, "fmodret_deny", &fmodret_deny, "file", + "output of nm with the functions which get no tag"), OPT_CALLBACK(0, "btf_link", &btf_links, "section:module:btf-file", "patch a BTF link with --patch_btfids", parse_btf_link), OPT_STRING(0, "patch_btfids", &btfids_path, "file", @@ -2182,7 +2185,7 @@ int main(int argc, const char **argv) if (btf2btf(&obj)) goto out; - if (fmodret_scan && fmodret_tag(obj.efile.elf, obj.btf, verbose)) + if (fmodret_scan && fmodret_tag(obj.efile.elf, obj.btf, fmodret_deny, verbose)) goto out; if (finalize_btf(&obj)) -- 2.43.0 Check in the BTF of vmlinux that the build has not tagged a few functions which return errors. Each of them loses its tag by one rule only: two which work on BPF objects, a helper, one which is excluded by name, one by prefix, and one of kernel/bpf. Signed-off-by: Anton Protopopov --- .../bpf/prog_tests/fmodret_untagged.c | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/fmodret_untagged.c diff --git a/tools/testing/selftests/bpf/prog_tests/fmodret_untagged.c b/tools/testing/selftests/bpf/prog_tests/fmodret_untagged.c new file mode 100644 index 000000000000..9f4b0586fe8f --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/fmodret_untagged.c @@ -0,0 +1,58 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Functions which return errors, but which the build must not tag as fmod_ret targets */ +#include +#include + +#define TAG "bpf_fmodret_retval:" + +static const char * const funcs[] = { + /* They work on BPF objects */ + "tracing_iter_filter", + "sock_map_delete_elem", + /* A helper */ + "sk_skb_adjust_room", + /* By name */ + "copy_from_kernel_nofault", + /* By prefix */ + "bpf_sk_storage_clone", + /* Of kernel/bpf */ + "range_tree_set", +}; + +/* Has the build tagged the function FUNC_ID, or any function if FUNC_ID is 0? */ +static bool tagged(const struct btf *btf, int func_id) +{ + int id; + + for (id = 1; id < btf__type_cnt(btf); id++) { + const struct btf_type *t = btf__type_by_id(btf, id); + const char *name = btf__name_by_offset(btf, t->name_off); + + if (btf_is_decl_tag(t) && (!func_id || t->type == func_id) && + !strncmp(name, TAG, strlen(TAG))) + return true; + } + return false; +} + +void test_fmodret_untagged(void) +{ + struct btf *btf; + int i, id; + + btf = btf__load_vmlinux_btf(); + if (!ASSERT_OK_PTR(btf, "btf__load_vmlinux_btf")) + return; + if (!tagged(btf, 0)) { + printf("%s:SKIP:no CONFIG_BPF_FMODRET_SCAN\n", __func__); + test__skip(); + goto out; + } + for (i = 0; i < ARRAY_SIZE(funcs); i++) { + id = btf__find_by_name_kind(btf, funcs[i], BTF_KIND_FUNC); + if (ASSERT_GT(id, 0, funcs[i])) + ASSERT_FALSE(tagged(btf, id), funcs[i]); + } +out: + btf__free(btf); +} -- 2.43.0