From: Alexei Starovoitov Let user space load many main programs with one copy of the functions that they call. Add an array of struct bpf_prog_entry to the attributes. A record names a function, its type, flags, attach point and name. The other attributes hold for all. The syscall returns the fd of the main program at insn 0 and writes the other fds to the records. All get an fd, or none. It takes CAP_BPF. A load of N main programs creates N + 1 struct bpf_prog: - N entries, the one at insn 0 too. User space attaches, pins and runs them like any other program. An entry points to its function in the JITed code of the owner. - One owner of the JITed code. It has no name and cannot be attached, pinned or run. The load returns no fd for it. Its id is in bpf_prog_info.owner_prog_id, so tools can get an fd by id to dump the code. It's freed with the last entry. Each entry passes the token and LSM checks like a load of its own. For now only the member of a struct_ops and BPF_F_SLEEPABLE can differ between entries. The rest is in the record to allow more later. Shared code has aux of the owner: - Prints and reports walk the stack to find the entry that runs. Async callbacks have none below them. They use the streams of the owner, which are the streams of the first entry. - Kfuncs get the struct_ops map of the entries, or none if there are two. - A tracing program names a shared function through the fd of any entry and sees the calls from all of them. For bpf_sys_bpf() the verifier checks only attr_size bytes at attr, so the records have to follow the attributes there. The first entry is made from the attributes, so don't overwrite them with the ids that bpf_struct_ops_resolve_attach() finds. It works where bpf_throw() does, on x86-64 and arm64. On x86-64 the walk of the stack needs ORC. Others get -EOPNOTSUPP. Signed-off-by: Alexei Starovoitov --- Documentation/bpf/verifier.rst | 47 +++- include/linux/bpf.h | 12 +- include/uapi/linux/bpf.h | 32 ++- kernel/bpf/arena.c | 2 +- kernel/bpf/bpf_struct_ops.c | 64 +++-- kernel/bpf/core.c | 50 +++- kernel/bpf/fixups.c | 1 + kernel/bpf/stream.c | 33 ++- kernel/bpf/syscall.c | 260 ++++++++++++++++-- kernel/bpf/verifier.c | 5 + tools/include/uapi/linux/bpf.h | 32 ++- .../selftests/bpf/prog_tests/bpf_attr_size.c | 2 +- 12 files changed, 472 insertions(+), 68 deletions(-) diff --git a/Documentation/bpf/verifier.rst b/Documentation/bpf/verifier.rst index dfb5048a3054..aba25fefb4f5 100644 --- a/Documentation/bpf/verifier.rst +++ b/Documentation/bpf/verifier.rst @@ -292,7 +292,8 @@ gets below ``min_len``, ``r`` is at least ``min_len``:: A pass ====== -A pass checks the main function, then each global function that is called. +A pass checks the main function, then each global function that is called. See +`Entries`_ for more than one main function. The states move through the program like this:: +---------+ next +-------------------+ @@ -1134,6 +1135,47 @@ A global function is different because its callers are checked without it and only see a number as its return value. A compiler that wants an object to get through a global function gives the call there a landing pad of its own. +Entries +======= + +The function at insn 0 is the main program. A load can name more main +programs, see ``struct bpf_prog_entry``. Then each main program, the first one +too, becomes a program of one function with its own fd, an *entry*. It holds the +*owner*, which has the code of all functions and cannot be attached or run:: + + insns: main | f | g | entry1 | entry2 | h + | ^ ^ | | ^ + +----+ +-----+--------+------+ + +All have one type, attach type and flags. For struct_ops it is one struct and +any member, sleepable or not. + +Each main program has passes of its own and the full limit on cost, see +``do_check_mains()``. The passes start with no precision, and with the fixups of +the programs before it. ``enter_main()`` makes ``env->prog`` answer for the +attach point of the main program. What it calls is checked like this: + +============================================= =================================== +function checked +============================================= =================================== +static for each main program that calls it +global, the type does not convert ctx access the same +global, the type converts ctx access once +============================================= =================================== + +Main programs of a type that converts ctx access, like tc and XDP, are the same +to a function. For other types, like struct_ops and tracepoints, the ctx, the +kfuncs and the valid results depend on the attach point. + +An insn has one fixup, a loop has one ``may_goto`` and a site has one landing +pad for the states of all main programs. A main program that is proven is not +checked again. So the load is rejected when a later one changes a site that it +went through, see ``check_proven_mains()``. Programs that are valid on their own +can be rejected together, the loader then loads them with copies. + +What is not tracked for each main program holds for all of them, as if they were +one program. What cannot hold for all, like a tail call, is rejected. + After an error ============== @@ -1711,7 +1753,7 @@ correlated two branches: one tests a result that the other one stored cost the work that is done data_deps xregs whose value flows into an operand of an unproven insn deps data_deps and control_deps -entry a state that enters a loop +entry a state that enters a loop; in a load: one more main program equation how two or three numbers are related failing path a path without joins on which the check of an unproven insn fails; it is the result of a path-sensitive run @@ -1747,6 +1789,7 @@ live read later, before it is written location a register or a stack slot that holds a pointer at a site may_goto a jump that is taken when the program has looped too long on its own a function is checked without the frames of its callers +owner the program that has the code of entries partition keep more states apart in the next pass pass one run of the main analysis over all functions path with a guess the failing path of a run that made a guess; it can be false diff --git a/include/linux/bpf.h b/include/linux/bpf.h index 28280e2db1e6..340d66a523d8 100644 --- a/include/linux/bpf.h +++ b/include/linux/bpf.h @@ -295,6 +295,7 @@ struct bpf_map_owner { enum bpf_prog_type type; bool jited; bool xdp_has_frags; + bool async_cb; bool has_prologue; bool sleepable; u64 storage_cookie[MAX_BPF_CGROUP_STORAGE_TYPE]; @@ -1922,6 +1923,7 @@ struct bpf_prog_aux { u32 subprog_start; u32 entry_cnt; /* main progs whose code it has, see struct bpf_prog_entry */ struct bpf_prog **entries; /* while they are loaded */ + struct bpf_prog *entry; /* what the symbol of this prog or func is the code of */ /* * Up to recursion_detected: where the prog is attached, all that check_attach_btf_id() * writes for types that can have entries. Each entry has its own, see enter_main(). @@ -1979,6 +1981,7 @@ struct bpf_prog_aux { bool tramp_linked; /* true if it was ever linked to a trampoline */ bool func_proto_unreliable; bool xdp_has_frags; + bool async_cb; bool has_prologue; bool can_unwind; /* frames of recursive subprogs can be on the stack when it is called */ @@ -2070,6 +2073,7 @@ struct bpf_prog_aux { struct bpf_stream *stream[2]; spinlock_t st_ops_assoc_lock; /* protects st_ops_assoc */ struct bpf_map __rcu *st_ops_assoc; + u32 st_ops_links; /* of entries in st_ops_assoc */ }; struct bpf_prog_stats { @@ -2457,7 +2461,7 @@ int bpf_struct_ops_supported(const struct bpf_struct_ops *st_ops, u32 moff); int bpf_struct_ops_map_sys_lookup_elem(struct bpf_map *map, void *key, void *value); u32 bpf_struct_ops_map_update_size(const struct bpf_map *map); -int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr); +int bpf_struct_ops_resolve_attach(struct btf **btf, u32 *id, u32 *member_idx); int bpf_struct_ops_prepare_trampoline(struct bpf_tramp_nodes *tnodes, struct bpf_tramp_node *node, const struct btf_func_model *model, @@ -2542,8 +2546,7 @@ static inline u32 bpf_struct_ops_map_update_size(const struct bpf_map *map) return map->value_size; } -static inline int bpf_struct_ops_resolve_attach(struct btf **btf, - union bpf_attr *attr) +static inline int bpf_struct_ops_resolve_attach(struct btf **btf, u32 *id, u32 *member_idx) { return -EOPNOTSUPP; } @@ -3454,6 +3457,7 @@ int bpf_check_uarg_tail_zero(bpfptr_t uaddr, size_t expected_size, struct bpf_log_attr; int bpf_check(struct bpf_prog **fp, union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log); +int bpf_prog_alloc_entries(struct bpf_prog *prog, union bpf_attr *attr, bpfptr_t uattr); #ifndef CONFIG_BPF_JIT_ALWAYS_ON int bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth); @@ -4533,6 +4537,7 @@ int bpf_try_get_buffers(struct bpf_bprintf_buffers **bufs); void bpf_put_buffers(void); int bpf_prog_stream_init(struct bpf_prog *prog, gfp_t gfp_extra_flags); +void bpf_prog_stream_share(struct bpf_prog *entry, struct bpf_prog *owner); void bpf_prog_stream_free(struct bpf_prog *prog); int bpf_prog_stream_read(struct bpf_prog *prog, enum bpf_stream_id stream_id, void __user *buf, u32 len); int bpf_prog_stream_new_fd(struct bpf_prog *prog, enum bpf_stream_id stream_id, u32 flags); @@ -4596,6 +4601,7 @@ void bpf_get_linfo_source(struct btf *btf, const struct bpf_line_info *linfo, int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char **filep, const char **linep, int *nump); struct bpf_prog *bpf_prog_find_from_stack(void); +struct bpf_prog *bpf_prog_find_main(const struct bpf_prog *prog); int bpf_insn_array_init(struct bpf_map *map, const struct bpf_prog *prog); int bpf_insn_array_ready(struct bpf_map *map); diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index ce8de1302f7f..9f41c63f1831 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -289,6 +289,10 @@ union bpf_iter_link_info { * The close-on-exec file descriptor flag (see **fcntl**\ (2)) is * automatically enabled for the new file descriptor. * + * Each of the *entries* names a function of *insns* that is + * loaded as one more program. Its file descriptor is returned in + * *prog_fd* of the entry. + * * Return * A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). @@ -1568,6 +1572,25 @@ struct bpf_common_attr { #define BPF_OBJ_NAME_LEN 16U +/* + * A function of the insns of BPF_PROG_LOAD that is a main program too. It becomes + * a program of its own, like the function at insn 0, and they share the other + * functions. The other attributes of the load hold for it as well. + * + * Type, flags and attach point have to be those of the load, but for the member and + * BPF_F_SLEEPABLE of a struct_ops. It takes CAP_BPF and func_info. All get an fd, or none. + */ +struct bpf_prog_entry { + __u32 insn_off; + __u32 prog_type; + __u32 prog_flags; + __u32 expected_attach_type; + __u32 attach_btf_id; + __u32 attach_btf_obj_fd; + __u32 prog_fd; /* output */ + char prog_name[BPF_OBJ_NAME_LEN]; +}; + enum { BPF_STREAM_STDOUT = 1, BPF_STREAM_STDERR = 2, @@ -1738,6 +1761,10 @@ union bpf_attr { * verification. */ __s32 keyring_id; + /* More main programs in the same insns, see struct bpf_prog_entry. */ + __aligned_u64 entries; + __u32 entry_cnt; + __u32 entry_rec_size; }; struct { /* anonymous struct used by BPF_OBJ_* commands */ @@ -5457,6 +5484,9 @@ union bpf_attr { * long bpf_sys_bpf(u32 cmd, void *attr, u32 attr_size) * Description * Execute bpf syscall with given arguments. + * + * The *entries* of **BPF_PROG_LOAD** have to follow the attributes, + * *attr_size* covers both. * Return * A syscall result. * @@ -6887,7 +6917,7 @@ struct bpf_prog_info { __u32 verified_insns; __u32 attach_btf_obj_id; __u32 attach_btf_id; - __u32 :32; + __u32 owner_prog_id; /* the program that has the code, see struct bpf_prog_entry */ } __attribute__((aligned(8))); struct bpf_map_info { diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c index ebe4ac74f219..c0c431124a13 100644 --- a/kernel/bpf/arena.c +++ b/kernel/bpf/arena.c @@ -1406,7 +1406,7 @@ static void __bpf_prog_report_arena_violation(struct bpf_prog *prog, bool write, u64 user_vm_start; /* Use main prog for stream access */ - prog = prog->aux->main_prog_aux->prog; + prog = bpf_prog_find_main(prog); user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena); addr += clear_lo32(user_vm_start); diff --git a/kernel/bpf/bpf_struct_ops.c b/kernel/bpf/bpf_struct_ops.c index e3608ec9da09..6ac596c156c2 100644 --- a/kernel/bpf/bpf_struct_ops.c +++ b/kernel/bpf/bpf_struct_ops.c @@ -580,14 +580,40 @@ static void bpf_struct_ops_map_put_progs(struct bpf_struct_ops_map *st_map) } } +/* Kfuncs get aux of the owner. It knows the map of its entries until they are in two maps. */ +static void entry_assoc(struct bpf_prog_aux *aux, struct bpf_map *map) +{ + guard(spinlock_irqsave)(&aux->st_ops_assoc_lock); + + if (!aux->st_ops_links) + rcu_assign_pointer(aux->st_ops_assoc, map); + if (rcu_access_pointer(aux->st_ops_assoc) == map) + aux->st_ops_links++; + else + rcu_assign_pointer(aux->st_ops_assoc, BPF_PTR_POISON); +} + +static void entry_disassoc(struct bpf_prog_aux *aux, struct bpf_map *map) +{ + guard(spinlock_irqsave)(&aux->st_ops_assoc_lock); + + if (rcu_access_pointer(aux->st_ops_assoc) == map && !--aux->st_ops_links) + RCU_INIT_POINTER(aux->st_ops_assoc, NULL); +} + static void bpf_struct_ops_map_dissoc_progs(struct bpf_struct_ops_map *st_map) { + struct bpf_prog *prog; u32 i; for (i = 0; i < st_map->funcs_cnt; i++) { if (!st_map->links[i]) break; - bpf_prog_disassoc_struct_ops(st_map->links[i]->prog); + prog = st_map->links[i]->prog; + if (prog->aux->is_entry) + entry_disassoc(prog->aux->main_prog_aux, &st_map->map); + else + bpf_prog_disassoc_struct_ops(prog); } } @@ -795,10 +821,10 @@ find_desc_by_local_type(const struct btf *btf, u32 id, } /* - * attach_btf_id is a local struct and expected_attach_type is the index of its - * member. Replace them, and the BTF, with what the kernel has under the same names. + * 'id' is a local struct and 'member_idx' is the index of its member. + * Replace them, and the BTF, with what the kernel has under the same names. */ -int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr) +int bpf_struct_ops_resolve_attach(struct btf **btf, u32 *id, u32 *member_idx) { const struct bpf_struct_ops_desc *st_ops_desc; const struct btf_member *member; @@ -807,23 +833,23 @@ int bpf_struct_ops_resolve_attach(struct btf **btf, union bpf_attr *attr) struct module *mod; int err = 0; - st_ops_desc = find_desc_by_local_type(*btf, attr->attach_btf_id, &kern_btf, &mod); + st_ops_desc = find_desc_by_local_type(*btf, *id, &kern_btf, &mod); if (IS_ERR(st_ops_desc)) return PTR_ERR(st_ops_desc); - t = btf_type_by_id(*btf, attr->attach_btf_id); - if (attr->expected_attach_type >= btf_type_vlen(t)) { + t = btf_type_by_id(*btf, *id); + if (*member_idx >= btf_type_vlen(t)) { err = -EINVAL; goto out; } - member = btf_type_member(t) + attr->expected_attach_type; + member = btf_type_member(t) + *member_idx; member = find_member(kern_btf, st_ops_desc->type, btf_name_by_offset(*btf, member->name_off)); if (!member) { err = -ENOENT; goto out; } - attr->attach_btf_id = st_ops_desc->type_id; - attr->expected_attach_type = member - btf_type_member(st_ops_desc->type); + *id = st_ops_desc->type_id; + *member_idx = member - btf_type_member(st_ops_desc->type); btf_put(*btf); *btf = kern_btf; out: @@ -1055,7 +1081,10 @@ static long __bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key, *plink++ = &link->link; /* Poison pointer on error instead of return for backward compatibility */ - bpf_prog_assoc_struct_ops(prog, &st_map->map); + if (prog->aux->is_entry) + entry_assoc(prog->aux->main_prog_aux, &st_map->map); + else + bpf_prog_assoc_struct_ops(prog, &st_map->map); ksym = kzalloc_obj(*ksym, GFP_USER); if (!ksym) { @@ -1800,12 +1829,13 @@ int bpf_struct_ops_link_create(union bpf_attr *attr) int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map) { + struct bpf_prog_aux *aux = prog->aux->main_prog_aux; struct bpf_map *st_ops_assoc; - guard(spinlock_irqsave)(&prog->aux->st_ops_assoc_lock); + guard(spinlock_irqsave)(&aux->st_ops_assoc_lock); - st_ops_assoc = rcu_dereference_protected(prog->aux->st_ops_assoc, - lockdep_is_held(&prog->aux->st_ops_assoc_lock)); + st_ops_assoc = rcu_dereference_protected(aux->st_ops_assoc, + lockdep_is_held(&aux->st_ops_assoc_lock)); if (st_ops_assoc && st_ops_assoc == map) return 0; @@ -1813,7 +1843,7 @@ int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map) if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) return -EBUSY; - rcu_assign_pointer(prog->aux->st_ops_assoc, BPF_PTR_POISON); + rcu_assign_pointer(aux->st_ops_assoc, BPF_PTR_POISON); } else { /* * struct_ops map does not track associated non-struct_ops programs. @@ -1822,7 +1852,7 @@ int bpf_prog_assoc_struct_ops(struct bpf_prog *prog, struct bpf_map *map) if (prog->type != BPF_PROG_TYPE_STRUCT_OPS) bpf_map_inc(map); - rcu_assign_pointer(prog->aux->st_ops_assoc, map); + rcu_assign_pointer(aux->st_ops_assoc, map); } return 0; @@ -1862,6 +1892,8 @@ void *bpf_prog_get_assoc_struct_ops(const struct bpf_prog_aux *aux, struct bpf_struct_ops_map *st_map; struct bpf_map *st_ops_assoc; + /* ->recursion_detected() gets the entry */ + aux = aux->main_prog_aux; st_ops_assoc = rcu_dereference_check(aux->st_ops_assoc, bpf_rcu_lock_held()); if (!st_ops_assoc || st_ops_assoc == BPF_PTR_POISON) return NULL; diff --git a/kernel/bpf/core.c b/kernel/bpf/core.c index 3653a2ecf5b7..8230dcdf14f4 100644 --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -958,7 +958,7 @@ void bpf_ksym_add(struct bpf_ksym *ksym) static void __bpf_ksym_del(struct bpf_ksym *ksym) { - if (list_empty(&ksym->lnode)) + if (list_empty(&ksym->lnode) || ksym->lnode.prev == LIST_POISON2) return; latch_tree_erase(&ksym->tnode, &bpf_tree, &bpf_tree_ops); @@ -3593,11 +3593,29 @@ static void bpf_prog_free_images(struct work_struct *work) if (aux->func_cnt) { kvfree(aux->func); bpf_prog_unlock_free(aux->prog); + } else if (aux->is_entry) { + if (aux->main_prog_aux != aux) + bpf_prog_put(aux->main_prog_aux->prog); + bpf_prog_unlock_free(aux->prog); } else { bpf_jit_free(aux->prog); } } +/* Nobody calls the function any more. The image goes with the owner */ +static void bpf_prog_kallsyms_del_entry(struct bpf_prog *entry) +{ + struct bpf_prog *code; + + guard(rcu)(); + /* ksym_prefix ends at the first byte */ + code = bpf_prog_ksym_find((unsigned long)entry->bpf_func + 1); + if (!code) + return; + WRITE_ONCE(code->aux->entry, NULL); + bpf_prog_kallsyms_del(code); +} + static void bpf_prog_free_deferred(struct work_struct *work) { struct bpf_prog_aux *aux; @@ -3610,6 +3628,8 @@ static void bpf_prog_free_deferred(struct work_struct *work) */ ksyms = aux->func_cnt || !list_empty(&aux->ksym.lnode); bpf_prog_kallsyms_del_all(aux->prog); + if (aux->is_entry && aux->main_prog_aux != aux) + bpf_prog_kallsyms_del_entry(aux->prog); #ifdef CONFIG_BPF_SYSCALL bpf_free_kfunc_btf_tab(aux->kfunc_btf_tab); #endif @@ -4385,6 +4405,7 @@ int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char * struct walk_stack_ctx { struct bpf_prog *prog; + bool async_cb; }; static bool find_from_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp) @@ -4400,19 +4421,36 @@ static bool find_from_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp) rcu_read_lock(); prog = bpf_prog_ksym_find(ip); rcu_read_unlock(); + /* What an async callback has interrupted did not call it */ if (!prog) - return true; + return !ctxp->async_cb; + if (ctxp->prog && ctxp->prog->aux != prog->aux->main_prog_aux) + return false; /* Make sure we return the main prog if we found a subprog */ - ctxp->prog = prog->aux->main_prog_aux->prog; - return false; + ctxp->prog = READ_ONCE(prog->aux->entry) ?: prog->aux->main_prog_aux->prog; + ctxp->async_cb = prog->aux->async_cb; + return ctxp->prog->aux->entry_cnt; } -struct bpf_prog *bpf_prog_find_from_stack(void) +static struct bpf_prog *find_from_stack(struct bpf_prog *prog) { - struct walk_stack_ctx ctx = {}; + struct walk_stack_ctx ctx = { prog }; arch_bpf_stack_walk(find_from_stack_cb, &ctx); return ctx.prog; } +struct bpf_prog *bpf_prog_find_from_stack(void) +{ + return find_from_stack(NULL); +} + +/* The code of @prog runs. For which entry? It's the owner if there is none in the stack */ +struct bpf_prog *bpf_prog_find_main(const struct bpf_prog *prog) +{ + struct bpf_prog *owner = prog->aux->main_prog_aux->prog; + + return owner->aux->entry_cnt ? find_from_stack(owner) : owner; +} + #endif diff --git a/kernel/bpf/fixups.c b/kernel/bpf/fixups.c index 247eb6372f69..4d76b9a83b39 100644 --- a/kernel/bpf/fixups.c +++ b/kernel/bpf/fixups.c @@ -2287,6 +2287,7 @@ static int jit_subprogs(struct bpf_verifier_env *env) func[i]->blinded = prog->blinded; func[i]->aux->func_idx = i; func[i]->aux->is_entry = i && main; + func[i]->aux->async_cb = env->subprog_info[i].is_async_cb; /* Below members will be freed only at prog->aux */ func[i]->aux->btf = prog->aux->btf; func[i]->aux->subprog_start = subprog_start; diff --git a/kernel/bpf/stream.c b/kernel/bpf/stream.c index 53519a17bf26..c726e54a63c0 100644 --- a/kernel/bpf/stream.c +++ b/kernel/bpf/stream.c @@ -403,7 +403,7 @@ __bpf_kfunc int bpf_stream_vprintk(int stream_id, const char *fmt__str, const vo u32 data_len = len__sz; int ret, num_args; - stream = bpf_stream_get(stream_id, aux); + stream = bpf_stream_get(stream_id, bpf_prog_find_main(aux->prog)->aux); if (!stream) return -ENOENT; @@ -439,7 +439,7 @@ __bpf_kfunc int bpf_stream_print_stack(int stream_id, struct bpf_prog_aux *aux) if (!bpf_stream_get(stream_id, aux)) return -ENOENT; - prog = aux->main_prog_aux->prog; + prog = bpf_prog_find_main(aux->prog); bpf_stream_stage(ss, prog, stream_id, ({ bpf_stream_dump_stack(ss); @@ -475,8 +475,21 @@ int bpf_prog_stream_init(struct bpf_prog *prog, gfp_t gfp_extra_flags) return 0; } +/* The loader gets no fd of the owner. The first entry has its streams */ +void bpf_prog_stream_share(struct bpf_prog *entry, struct bpf_prog *owner) +{ + int i; + + bpf_prog_stream_free(entry); + for (i = 0; i < ARRAY_SIZE(owner->aux->stream); i++) { + refcount_inc(&owner->aux->stream[i]->refcnt); + entry->aux->stream[i] = owner->aux->stream[i]; + } +} + void bpf_prog_stream_free(struct bpf_prog *prog) { + bool shared = prog->aux->is_entry && !prog->aux->subprog_start; int i; for (i = 0; i < ARRAY_SIZE(prog->aux->stream); i++) { @@ -484,13 +497,15 @@ void bpf_prog_stream_free(struct bpf_prog *prog) if (!stream) continue; - /* - * Pairs with smp_load_acquire() in bpf_stream_file_read(): every - * publication precedes the dead flag, so a reader that observes - * it also observes all buffered data. - */ - smp_store_release(&stream->dead, true); - wake_up_interruptible_poll(&stream->waitq, EPOLLHUP); + if (!shared) { + /* + * Pairs with smp_load_acquire() in bpf_stream_file_read(): every + * publication precedes the dead flag, so a reader that observes + * it also observes all buffered data. + */ + smp_store_release(&stream->dead, true); + wake_up_interruptible_poll(&stream->waitq, EPOLLHUP); + } bpf_stream_put(stream); prog->aux->stream[i] = NULL; } diff --git a/kernel/bpf/syscall.c b/kernel/bpf/syscall.c index 2bff30bb3669..b9cd3d50dee5 100644 --- a/kernel/bpf/syscall.c +++ b/kernel/bpf/syscall.c @@ -2694,6 +2694,10 @@ EXPORT_SYMBOL_GPL(bpf_prog_inc_not_zero); bool bpf_prog_get_ok(struct bpf_prog *prog, enum bpf_prog_type *attach_type, bool attach_drv) { + /* it has the code of its entries, and that's all */ + if (prog->aux->entry_cnt) + return false; + /* not an attachment, just a refcount inc, always allow */ if (!attach_type) return true; @@ -2706,18 +2710,23 @@ bool bpf_prog_get_ok(struct bpf_prog *prog, return true; } -static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, - bool attach_drv) +static struct bpf_prog *bpf_prog_of_fd(struct fd f) { - CLASS(fd, f)(ufd); - struct bpf_prog *prog; - if (fd_empty(f)) return ERR_PTR(-EBADF); if (fd_file(f)->f_op != &bpf_prog_fops) return ERR_PTR(-EINVAL); + return fd_file(f)->private_data; +} + +static struct bpf_prog *__bpf_prog_get(u32 ufd, enum bpf_prog_type *attach_type, + bool attach_drv) +{ + CLASS(fd, f)(ufd); + struct bpf_prog *prog = bpf_prog_of_fd(f); - prog = fd_file(f)->private_data; + if (IS_ERR(prog)) + return prog; if (!bpf_prog_get_ok(prog, attach_type, attach_drv)) return ERR_PTR(-EINVAL); @@ -2956,11 +2965,12 @@ int __init __used bpf_multi_func(void) { return 0; } BTF_ID_LIST_GLOBAL_SINGLE(bpf_multi_func_btf_id, func, bpf_multi_func) /* last field in 'union bpf_attr' used by this command */ -#define BPF_PROG_LOAD_LAST_FIELD keyring_id +#define BPF_PROG_LOAD_LAST_FIELD entry_rec_size static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr) { enum bpf_prog_type type = attr->prog_type; + u32 attach_type, attach_btf_id = attr->attach_btf_id; struct bpf_prog *prog, *dst_prog = NULL; struct btf *attach_btf = NULL; struct bpf_token *token = NULL; @@ -2981,6 +2991,7 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr return ERR_PTR(-EINVAL); bpf_prog_load_fixup_attach_type(attr); + attach_type = attr->expected_attach_type; if (attr->prog_flags & BPF_F_TOKEN_FD) { token = bpf_token_get_from_fd(attr->prog_token_fd); @@ -3050,12 +3061,21 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr /* only struct_ops can name its target in the BTF of the object */ err = -ENOTSUPP; if (type == BPF_PROG_TYPE_STRUCT_OPS) - err = bpf_struct_ops_resolve_attach(&attach_btf, attr); + err = bpf_struct_ops_resolve_attach(&attach_btf, + &attach_btf_id, + &attach_type); if (err) { btf_put(attach_btf); goto put_token; } } + } else if (dst_prog->aux->is_entry && + dst_prog->aux->func_info[0].type_id != attr->attach_btf_id) { + /* what an entry calls is a function of its owner */ + prog = dst_prog; + dst_prog = prog->aux->main_prog_aux->prog; + bpf_prog_inc(dst_prog); + bpf_prog_put(prog); } } else if (attr->attach_btf_id || multi_func) { /* fall back to vmlinux BTF, if BTF type ID is specified */ @@ -3071,8 +3091,7 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr btf_get(attach_btf); } - if (bpf_prog_load_check_attach(type, attr->expected_attach_type, - attach_btf, attr->attach_btf_id, + if (bpf_prog_load_check_attach(type, attach_type, attach_btf, attach_btf_id, dst_prog, multi_func)) { if (dst_prog) bpf_prog_put(dst_prog); @@ -3093,10 +3112,10 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr goto put_token; } - prog->expected_attach_type = attr->expected_attach_type; + prog->expected_attach_type = attach_type; prog->sleepable = !!(attr->prog_flags & BPF_F_SLEEPABLE); prog->aux->attach_btf = attach_btf; - prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0] : attr->attach_btf_id; + prog->aux->attach_btf_id = multi_func ? bpf_multi_func_btf_id[0] : attach_btf_id; prog->aux->dst_prog = dst_prog; if (dst_prog) { prog->aux->saved_dst_ctx_type = resolve_prog_type(dst_prog); @@ -3190,6 +3209,152 @@ static struct bpf_prog *bpf_prog_alloc_attr(union bpf_attr *attr, bpfptr_t uattr return ERR_PTR(err); } +/* + * An entry is checked like a load of its own. It gets its insns from the verifier. + * The first one is for the function at insn 0. + */ +int bpf_prog_alloc_entries(struct bpf_prog *prog, union bpf_attr *attr, bpfptr_t uattr) +{ + bpfptr_t urec = make_bpfptr(attr->entries, uattr.is_kernel); + struct bpf_prog_aux *aux = prog->aux; + union bpf_attr eattr = *attr; + struct bpf_prog_entry rec = {}; + struct bpf_prog *entry; + int err; + + if (!attr->entry_cnt) + return attr->entries || attr->entry_rec_size ? -EINVAL : 0; + if (!bpf_token_capable(aux->token, CAP_BPF)) + return -EPERM; + if (attr->entry_cnt >= attr->insn_cnt || + attr->entry_rec_size < sizeof(rec) || attr->entry_rec_size > PAGE_SIZE || + attr->entry_rec_size % sizeof(u32)) + return -EINVAL; + aux->entries = kvzalloc_objs(*aux->entries, attr->entry_cnt + 1, GFP_KERNEL_ACCOUNT); + if (!aux->entries) + return -ENOMEM; + + for (;;) { + if (bpf_must_abort()) + return -EINTR; + eattr.insn_cnt = 1; + entry = bpf_prog_alloc_attr(&eattr, uattr); + if (IS_ERR(entry)) + return PTR_ERR(entry); + /* the license can be another one when it's read again */ + entry->gpl_compatible = prog->gpl_compatible; + if (!aux->entry_cnt) + bpf_prog_stream_share(entry, prog); + entry->aux->is_entry = true; + entry->aux->subprog_start = rec.insn_off; + aux->entries[aux->entry_cnt++] = entry; + + eattr.insn_cnt = attr->insn_cnt; + err = security_bpf_prog_load(entry, &eattr, entry->aux->token, uattr.is_kernel); + if (err) + return err < 0 ? err : -EPERM; + if (aux->entry_cnt > attr->entry_cnt) + return 0; + + err = bpf_check_uarg_tail_zero(urec, sizeof(rec), attr->entry_rec_size); + if (err) + return err; + if (copy_from_bpfptr(&rec, urec, sizeof(rec))) + return -EFAULT; + bpfptr_add(&urec, attr->entry_rec_size); + if ((rec.prog_flags ^ attr->prog_flags) & ~BPF_F_SLEEPABLE) + return -EINVAL; + eattr.prog_type = rec.prog_type; + eattr.prog_flags = rec.prog_flags; + eattr.expected_attach_type = rec.expected_attach_type; + eattr.attach_btf_id = rec.attach_btf_id; + eattr.attach_btf_obj_fd = rec.attach_btf_obj_fd; + memcpy(eattr.prog_name, rec.prog_name, sizeof(rec.prog_name)); + } +} + +/* Those that have no id */ +static void bpf_prog_free_entries(struct bpf_prog *prog) +{ + struct bpf_prog_aux *aux = prog->aux; + u32 i; + + for (i = 0; i < aux->entry_cnt; i++) + if (aux->entries[i]) + __bpf_prog_put_noref(aux->entries[i], false); + kvfree(aux->entries); + aux->entries = NULL; +} + +static void bpf_prog_announce(struct bpf_prog *prog) +{ + perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); + bpf_audit_prog(prog, BPF_AUDIT_LOAD); +} + +/* All entries get an fd, or none. Returns the fd of the first one. */ +static int bpf_prog_entries_new_fds(struct bpf_prog *prog, union bpf_attr *attr, bpfptr_t uattr) +{ + bpfptr_t urec = make_bpfptr(attr->entries, uattr.is_kernel); + u32 i, n, cnt = prog->aux->entry_cnt; + struct bpf_prog *entry; + struct { + struct file *file; + int fd; + } *fds; + int err = 0; + + bpf_prog_add(prog, cnt); + for (n = 0; n < cnt; n++) + prog->aux->entries[n]->aux->main_prog_aux = prog->aux; + + fds = kvmalloc_objs(*fds, cnt, GFP_KERNEL_ACCOUNT); + if (!fds) + return -ENOMEM; + for (n = 0; n < cnt; n++) { + entry = prog->aux->entries[n]; + err = bpf_prog_alloc_id(entry); + if (err) + break; + prog->aux->entries[n] = NULL; + bpf_prog_announce(entry); + + fds[n].fd = security_bpf_prog(entry); + if (fds[n].fd >= 0) + fds[n].fd = get_unused_fd_flags(O_CLOEXEC); + if (fds[n].fd < 0) { + err = fds[n].fd; + bpf_prog_put(entry); + break; + } + fds[n].file = anon_inode_getfile("bpf-prog", &bpf_prog_fops, entry, + O_RDWR | O_CLOEXEC); + if (IS_ERR(fds[n].file)) { + err = PTR_ERR(fds[n].file); + put_unused_fd(fds[n].fd); + bpf_prog_put(entry); + break; + } + } + for (i = 1; i < n && !err; i++) { + if (copy_to_bpfptr_offset(urec, offsetof(struct bpf_prog_entry, prog_fd), + &fds[i].fd, sizeof(u32))) + err = -EFAULT; + bpfptr_add(&urec, attr->entry_rec_size); + } + for (i = 0; i < n; i++) { + if (err) { + put_unused_fd(fds[i].fd); + fput(fds[i].file); + } else { + fd_install(fds[i].fd, fds[i].file); + } + } + err = err ?: fds[0].fd; + kvfree(fds); + return err; +} + static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_attr *attr_log) { struct bpf_prog *prog; @@ -3211,6 +3376,10 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at if (err < 0) goto free_used_maps; + /* it is the name of the first entry */ + if (prog->aux->entry_cnt) + memset(prog->aux->name, 0, sizeof(prog->aux->name)); + /* bpf_throw() and faults look for the symbol. The prog can run once it has the id. */ bpf_prog_kallsyms_add(prog); @@ -3232,8 +3401,14 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at * Also, any failure handling from this point onwards must * be using bpf_prog_put() given the program is exposed. */ - perf_event_bpf_event(prog, PERF_BPF_EVENT_PROG_LOAD, 0); - bpf_audit_prog(prog, BPF_AUDIT_LOAD); + bpf_prog_announce(prog); + + if (prog->aux->entry_cnt) { + err = bpf_prog_entries_new_fds(prog, attr, uattr); + bpf_prog_free_entries(prog); + bpf_prog_put(prog); + return err; + } err = bpf_prog_new_fd(prog); if (err < 0) @@ -3241,6 +3416,7 @@ static int bpf_prog_load(union bpf_attr *attr, bpfptr_t uattr, struct bpf_log_at return err; free_used_maps: + bpf_prog_free_entries(prog); /* In case we have subprogs, we need to wait for a grace * period before we can tear down JIT memory since symbols * are already exposed under kallsyms. @@ -5132,7 +5308,8 @@ static void prepare_dump_pseudo_call(const struct bpf_prog *prog, struct bpf_ins if (insn->code == (BPF_JMP | BPF_CALL_ARGS)) insn->imm = bpf_call_args_imm(insn->off); else - insn->imm = bpf_prog_find_func(prog, idx + call_off + 1); + insn->imm = bpf_prog_find_func(prog->aux->main_prog_aux->prog, + prog->aux->subprog_start + idx + call_off + 1); /* Avoid dumping a truncated and misleading pc-relative offset. */ if (call_off > S16_MAX || call_off < S16_MIN) @@ -5258,7 +5435,7 @@ static int bpf_prog_get_info_by_fd(struct file *file, u32 ulen, len; int err; - len = offsetofend(struct bpf_prog_info, attach_btf_id); + len = offsetofend(struct bpf_prog_info, owner_prog_id); err = bpf_check_uarg_tail_zero(USER_BPFPTR(uinfo), len, info_len); if (err) return err; @@ -5274,6 +5451,7 @@ static int bpf_prog_get_info_by_fd(struct file *file, info.created_by_uid = from_kuid_munged(current_user_ns(), prog->aux->user->uid); info.gpl_compatible = prog->gpl_compatible; + info.owner_prog_id = prog->aux->is_entry ? prog->aux->main_prog_aux->id : 0; memcpy(info.tag, prog->tag, sizeof(prog->tag)); memcpy(info.name, prog->aux->name, sizeof(prog->aux->name)); @@ -6427,20 +6605,18 @@ static int prog_stream_read(union bpf_attr *attr) { char __user *buf = u64_to_user_ptr(attr->prog_stream_read.stream_buf); u32 len = attr->prog_stream_read.stream_buf_len; + CLASS(fd, f)(attr->prog_stream_read.prog_fd); struct bpf_prog *prog; - int ret; if (CHECK_ATTR(BPF_PROG_STREAM_READ_BY_FD)) return -EINVAL; - prog = bpf_prog_get(attr->prog_stream_read.prog_fd); + /* the owner of entries has streams too */ + prog = bpf_prog_of_fd(f); if (IS_ERR(prog)) return PTR_ERR(prog); - ret = bpf_prog_stream_read(prog, attr->prog_stream_read.stream_id, buf, len); - bpf_prog_put(prog); - - return ret; + return bpf_prog_stream_read(prog, attr->prog_stream_read.stream_id, buf, len); } #define BPF_PROG_ASSOC_STRUCT_OPS_LAST_FIELD prog_assoc_struct_ops.flags @@ -6490,22 +6666,20 @@ static int prog_assoc_struct_ops(union bpf_attr *attr) static int prog_stream_open(union bpf_attr *attr) { + CLASS(fd, f)(attr->prog_stream_open.prog_fd); struct bpf_prog *prog; u32 flags = attr->prog_stream_open.flags; - int ret; if (CHECK_ATTR(BPF_PROG_STREAM_OPEN)) return -EINVAL; if (flags & ~BPF_F_STREAM_NONBLOCK) return -EINVAL; - prog = bpf_prog_get(attr->prog_stream_open.prog_fd); + prog = bpf_prog_of_fd(f); if (IS_ERR(prog)) return PTR_ERR(prog); - ret = bpf_prog_stream_new_fd(prog, attr->prog_stream_open.stream_id, flags); - bpf_prog_put(prog); - return ret; + return bpf_prog_stream_new_fd(prog, attr->prog_stream_open.stream_id, flags); } static int __sys_bpf(enum bpf_cmd cmd, bpfptr_t uattr, unsigned int size, @@ -6715,18 +6889,48 @@ static bool syscall_prog_is_valid_access(int off, int size, return true; } +/* + * The kernel writes fds to the entries, and all that the caller is known to own + * are @size bytes at @uattr. So that's where they have to be, after the attr. + */ +static int sys_bpf_prog_load(union bpf_attr *uattr, u32 size) +{ + union bpf_attr attr = {}; + u64 off; + int ret; + + memcpy(&attr, uattr, min_t(u32, size, sizeof(attr))); + if (attr.entry_cnt) { + off = attr.entries - (unsigned long)uattr; + if (off < offsetofend(union bpf_attr, entry_rec_size) || off > size || + (u64)attr.entry_cnt * attr.entry_rec_size > size - off) + return -EFAULT; + size = off; + } + ret = bpf_check_uarg_tail_zero(KERNEL_BPFPTR(uattr), sizeof(attr), size); + if (ret) + return ret; + size = min_t(u32, size, sizeof(attr)); + + /* what was checked can't change */ + ret = __sys_bpf(BPF_PROG_LOAD, KERNEL_BPFPTR(&attr), size, KERNEL_BPFPTR(NULL), 0); + memcpy(uattr, &attr, size); + return ret; +} + BPF_CALL_3(bpf_sys_bpf, int, cmd, union bpf_attr *, attr, u32, attr_size) { if (bpf_in_async_cb()) return -EPERM; switch (cmd) { + case BPF_PROG_LOAD: + return sys_bpf_prog_load(attr, attr_size); case BPF_MAP_CREATE: case BPF_MAP_DELETE_ELEM: case BPF_MAP_UPDATE_ELEM: case BPF_MAP_FREEZE: case BPF_MAP_GET_FD_BY_ID: - case BPF_PROG_LOAD: case BPF_BTF_LOAD: case BPF_LINK_CREATE: case BPF_RAW_TRACEPOINT_OPEN: diff --git a/kernel/bpf/verifier.c b/kernel/bpf/verifier.c index f6fda89be3e6..cc5a39abe9a7 100644 --- a/kernel/bpf/verifier.c +++ b/kernel/bpf/verifier.c @@ -26789,6 +26789,7 @@ static int finish_entry(struct bpf_verifier_env *env, int s) if (!entry) return -ENOMEM; from->entries[sub->main - 1] = entry; + (s ? func->aux : from)->entry = entry; aux = entry->aux; entry->len = func->len; @@ -27637,6 +27638,10 @@ int bpf_check(struct bpf_prog **prog, union bpf_attr *attr, bpfptr_t uattr, if (ret) goto err_prep; + ret = bpf_prog_alloc_entries(env->prog, attr, uattr); + if (ret) + goto err_prep; + bpf_get_btf_vmlinux(); /* Serialize verification of unprivileged programs. */ diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index ce8de1302f7f..9f41c63f1831 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -289,6 +289,10 @@ union bpf_iter_link_info { * The close-on-exec file descriptor flag (see **fcntl**\ (2)) is * automatically enabled for the new file descriptor. * + * Each of the *entries* names a function of *insns* that is + * loaded as one more program. Its file descriptor is returned in + * *prog_fd* of the entry. + * * Return * A new file descriptor (a nonnegative integer), or -1 if an * error occurred (in which case, *errno* is set appropriately). @@ -1568,6 +1572,25 @@ struct bpf_common_attr { #define BPF_OBJ_NAME_LEN 16U +/* + * A function of the insns of BPF_PROG_LOAD that is a main program too. It becomes + * a program of its own, like the function at insn 0, and they share the other + * functions. The other attributes of the load hold for it as well. + * + * Type, flags and attach point have to be those of the load, but for the member and + * BPF_F_SLEEPABLE of a struct_ops. It takes CAP_BPF and func_info. All get an fd, or none. + */ +struct bpf_prog_entry { + __u32 insn_off; + __u32 prog_type; + __u32 prog_flags; + __u32 expected_attach_type; + __u32 attach_btf_id; + __u32 attach_btf_obj_fd; + __u32 prog_fd; /* output */ + char prog_name[BPF_OBJ_NAME_LEN]; +}; + enum { BPF_STREAM_STDOUT = 1, BPF_STREAM_STDERR = 2, @@ -1738,6 +1761,10 @@ union bpf_attr { * verification. */ __s32 keyring_id; + /* More main programs in the same insns, see struct bpf_prog_entry. */ + __aligned_u64 entries; + __u32 entry_cnt; + __u32 entry_rec_size; }; struct { /* anonymous struct used by BPF_OBJ_* commands */ @@ -5457,6 +5484,9 @@ union bpf_attr { * long bpf_sys_bpf(u32 cmd, void *attr, u32 attr_size) * Description * Execute bpf syscall with given arguments. + * + * The *entries* of **BPF_PROG_LOAD** have to follow the attributes, + * *attr_size* covers both. * Return * A syscall result. * @@ -6887,7 +6917,7 @@ struct bpf_prog_info { __u32 verified_insns; __u32 attach_btf_obj_id; __u32 attach_btf_id; - __u32 :32; + __u32 owner_prog_id; /* the program that has the code, see struct bpf_prog_entry */ } __attribute__((aligned(8))); struct bpf_map_info { diff --git a/tools/testing/selftests/bpf/prog_tests/bpf_attr_size.c b/tools/testing/selftests/bpf/prog_tests/bpf_attr_size.c index 87842c4347a6..559e4aa5f56d 100644 --- a/tools/testing/selftests/bpf/prog_tests/bpf_attr_size.c +++ b/tools/testing/selftests/bpf/prog_tests/bpf_attr_size.c @@ -93,7 +93,7 @@ static void test_prog_info_tail_zero(void) BPF_EXIT_INSN(), }; struct bpf_prog_info_fake { - __u8 info[offsetofend(struct bpf_prog_info, attach_btf_id)]; + __u8 info[offsetofend(struct bpf_prog_info, owner_prog_id)]; __u32 pad; } info = { .pad = 1, -- 2.55.0