Introduce unstable CT lookup helpers
Subsystems (10)
Kumar Kartikeya Dwivedi
maintainerv4 cover Introduce unstable CT lookup helpers
This series adds unstable conntrack lookup helpers using BPF kfunc support. The patch adding the lookup helper is based off of Maxim's recent patch to aid in rebasing their series on top of this, all adjusted to work with module kfuncs [0].
[0]: https://lore.kernel.org/bpf/[email protected]
To enable returning a reference to struct nf_conn, the verifier is extended to support reference tracking for PTR_TO_BTF_ID, and kfunc is extended with support for working as acquire/release functions, similar to existing BPF helpers. kfunc returning pointer (limited to PTR_TO_BTF_ID in the kernel) can also return a PTR_TO_BTF_ID_OR_NULL now, typically needed when acquiring a resource can fail. kfunc can also receive PTR_TO_CTX and PTR_TO_MEM (with some limitations) as arguments now. There is also support for passing a mem, len pair as argument to kfunc now. In such cases, passing pointer to unsized type (void) is also permitted.
Please see individual commits for details.
Note: BPF CI needs to add the following to config to test the set. I did update the selftests config in patch 8, but not sure if that is enough.
CONFIG_NETFILTER=y
CONFIG_NF_DEFRAG_IPV4=y
CONFIG_NF_DEFRAG_IPV6=y
CONFIG_NF_CONNTRACK=y
Changelog:
----------
v3 -> v4:
v3: https://lore.kernel.org/bpf/[email protected]
* Guard unstable CT helpers with CONFIG_DEBUG_INFO_BTF_MODULES
* Move addition of prog_test test kfuncs to selftest commit
* Move negative kfunc tests to test_verifier suite
* Limit struct nesting depth to 4, which should be enough for now
v2 -> v3:
v2: https://lore.kernel.org/bpf/[email protected]
* Fix build error for !CONFIG_BPF_SYSCALL (Patchwork)
RFC v1 -> v2:
v1: https://lore.kernel.org/bpf/[email protected]
* Limit PTR_TO_MEM support to pointer to scalar, or struct with scalars (Alexei)
* Use btf_id_set for checking acquire, release, ret type null (Alexei)
* Introduce opts struct for CT helpers, move int err parameter to it
* Add l4proto as parameter to CT helper's opts, remove separate tcp/udp helpers
* Add support for mem, len argument pair to kfunc
* Allow void * as pointer type for mem, len argument pair
* Extend selftests to cover new additions to kfuncs
* Copy ref_obj_id to PTR_TO_BTF_ID dst_reg on btf_struct_access, test it
* Fix other misc nits, bugs, and expand commit messages
Kumar Kartikeya Dwivedi (10):
bpf: Refactor bpf_check_mod_kfunc_call
bpf: Remove DEFINE_KFUNC_BTF_ID_SET
bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support
bpf: Introduce mem, size argument pair support for kfunc
bpf: Add reference tracking support to kfunc
bpf: Track provenance for pointers formed from referenced
PTR_TO_BTF_ID
net/netfilter: Add unstable CT lookup helpers for XDP and TC-BPF selftests/bpf: Add test for unstable CT lookup API selftests/bpf: Add test_verifier support to fixup kfunc call insns selftests/bpf: Extend kfunc selftests
include/linux/bpf.h | 27 +- include/linux/bpf_verifier.h | 12 + include/linux/btf.h | 46 ++- kernel/bpf/btf.c | 216 ++++++++++++-- kernel/bpf/verifier.c | 232 +++++++++++---- net/bpf/test_run.c | 135 +++++++++ net/core/filter.c | 27 ++ net/core/net_namespace.c | 1 + net/ipv4/tcp_bbr.c | 5 +- net/ipv4/tcp_cubic.c | 5 +- net/ipv4/tcp_dctcp.c | 5 +- net/netfilter/nf_conntrack_core.c | 278 ++++++++++++++++++ .../selftests/bpf/bpf_testmod/bpf_testmod.c | 5 +- tools/testing/selftests/bpf/config | 4 + .../testing/selftests/bpf/prog_tests/bpf_nf.c | 48 +++ .../selftests/bpf/prog_tests/kfunc_call.c | 6 + .../selftests/bpf/progs/kfunc_call_test.c | 52 +++- .../testing/selftests/bpf/progs/test_bpf_nf.c | 105 +++++++ tools/testing/selftests/bpf/test_verifier.c | 28 ++ tools/testing/selftests/bpf/verifier/calls.c | 75 +++++ .../selftests/bpf/verifier/ref_tracking.c | 44 +++ 21 files changed, 1248 insertions(+), 108 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_nf.c create mode 100644 tools/testing/selftests/bpf/progs/test_bpf_nf.c
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH v4 1/10 bpf: Refactor bpf_check_mod_kfunc_call
Future commits adding more callbacks will implement the same pattern of matching module owner of kfunc_btf_id_set, and then operating on more sets inside the struct. Instead of having to repeat the same code of walking over the list, lift code searching individual sets corresponding to a owner module out into a common helper, and index into sets array.
While the btf_id_set 'set' member for check_kfunc_call wouldn't have been NULL so far, future commits introduce sets that are optional, hence the common code also checks whether the pointer is valid (i.e. not NULL).
Note that we must continue search on owner match and btf_id_set_contains returning false, since more entries may have same owner (since it can be NULL for built-in modules). To clarify this case, a comment is added, so that future commits don't regress the search, as it was fixed recently in commit b12f03104324 ("bpf: Fix bpf_check_mod_kfunc_call for built-in modules").
--- include/linux/btf.h | 15 +++++++++++++-- kernel/bpf/btf.c | 25 +++++++++++++++++++------ 2 files changed, 32 insertions(+), 8 deletions(-)
include/linux/btf.h +13 -2
@@ -320,9 +320,19 @@ static inline const char *btf_name_by_offset(const struct btf *btf, }#endif +enum kfunc_btf_id_set_type { +BTF_SET_CHECK , + __BTF_SET_MAX, +}; +struct kfunc_btf_id_set {struct list_head list; -struct btf_id_set *set; +union { +struct btf_id_set *sets[__BTF_SET_MAX]; +struct { +struct btf_id_set *set; + }; + };struct module *owner; };
@@ -361,7 +371,8 @@ static struct kfunc_btf_id_list prog_test_kfunc_list __maybe_unused;#endif #define DEFINE_KFUNC_BTF_ID_SET (set, name) \ - struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), (set), \ + struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), \ + { { (set) } }, \ THIS_MODULE }#endif
kernel/bpf/btf.c +19 -6
@@ -6384,20 +6384,33 @@ void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l, }EXPORT_SYMBOL_GPL (unregister_kfunc_btf_id_set); -bool bpf_check_mod_kfunc_call (struct kfunc_btf_id_list *klist,u32 kfunc_id, -struct module *owner) +/* Caller must hold reference to module 'owner' */ +static bool kfunc_btf_id_set_contains (struct kfunc_btf_id_list *klist, +u32 kfunc_id,struct module *owner, +enum kfunc_btf_id_set_type type) {struct kfunc_btf_id_set *s; +bool ret = false; +if (type >= __BTF_SET_MAX) +return false;mutex_lock (&klist->mutex);list_for_each_entry (s, &klist->list, list) { -if (s->owner == owner &&btf_id_set_contains (s->set, kfunc_id)) { -mutex_unlock (&klist->mutex); -return true; +if (s->owner == owner && s->sets[type] && +btf_id_set_contains (s->sets[type], kfunc_id)) { + ret = true; +break ; } +/* continue search, since multiple sets may have same owner */ }mutex_unlock (&klist->mutex); -return false; +return ret; +} + +bool bpf_check_mod_kfunc_call (struct kfunc_btf_id_list *klist,u32 kfunc_id, +struct module *owner) +{ +return kfunc_btf_id_set_contains (klist, kfunc_id, owner,BTF_SET_CHECK ); }#define DEFINE_KFUNC_BTF_ID_LIST (name) \
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH v4 2/10 bpf: Remove DEFINE_KFUNC_BTF_ID_SET
In reply to Kumar Kartikeya DwivediThe only reason to keep it was to initialize list head, but future commits will introduce more members that need to be set, which is more convenient to do using designated initializer.
Hence, remove the macro, convert users, and initialize list head inside register_kfunc_btf_id_set.
--- include/linux/btf.h | 5 ----- kernel/bpf/btf.c | 1 + net/ipv4/tcp_bbr.c | 5 ++++- net/ipv4/tcp_cubic.c | 5 ++++- net/ipv4/tcp_dctcp.c | 5 ++++- tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c | 5 ++++- 6 files changed, 17 insertions(+), 9 deletions(-)
include/linux/btf.h +0 -5
@@ -370,9 +370,4 @@ static struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list __maybe_unused;static struct kfunc_btf_id_list prog_test_kfunc_list__maybe_unused ;#endif -#define DEFINE_KFUNC_BTF_ID_SET (set, name) \ - struct kfunc_btf_id_set name = { LIST_HEAD_INIT(name.list), \ - { { (set) } }, \ - THIS_MODULE } -#endif
kernel/bpf/btf.c +1 -0
@@ -6369,6 +6369,7 @@ BTF_TRACING_TYPE_xxxvoid register_kfunc_btf_id_set (struct kfunc_btf_id_list *l,struct kfunc_btf_id_set *s) { +INIT_LIST_HEAD (&s->list);mutex_lock (&l->mutex);list_add (&s->list, &l->list);mutex_unlock (&l->mutex);
net/ipv4/tcp_bbr.c +4 -1
@@ -1169,7 +1169,10 @@ BTF_ID(func, bbr_set_state)#endif BTF_SET_END (tcp_bbr_kfunc_ids) -static DEFINE_KFUNC_BTF_ID_SET(&tcp_bbr_kfunc_ids, tcp_bbr_kfunc_btf_set); +static struct kfunc_btf_id_set tcp_bbr_kfunc_btf_set = { + .owner =THIS_MODULE , + .set = &tcp_bbr_kfunc_ids, +};static int __init bbr_register (void ) {
net/ipv4/tcp_cubic.c +4 -1
@@ -498,7 +498,10 @@ BTF_ID(func, cubictcp_acked)#endif BTF_SET_END (tcp_cubic_kfunc_ids) -static DEFINE_KFUNC_BTF_ID_SET(&tcp_cubic_kfunc_ids, tcp_cubic_kfunc_btf_set); +static struct kfunc_btf_id_set tcp_cubic_kfunc_btf_set = { + .owner =THIS_MODULE , + .set = &tcp_cubic_kfunc_ids, +};static int __init cubictcp_register (void ) {
net/ipv4/tcp_dctcp.c +4 -1
@@ -251,7 +251,10 @@ BTF_ID(func, dctcp_state)#endif BTF_SET_END (tcp_dctcp_kfunc_ids) -static DEFINE_KFUNC_BTF_ID_SET(&tcp_dctcp_kfunc_ids, tcp_dctcp_kfunc_btf_set); +static struct kfunc_btf_id_set tcp_dctcp_kfunc_btf_set = { + .owner =THIS_MODULE , + .set = &tcp_dctcp_kfunc_ids, +};static int __init dctcp_register (void ) {
tools/testing/selftests/bpf/bpf_testmod/bpf_testmod.c +4 -1
@@ -93,7 +93,10 @@ BTF_SET_START(bpf_testmod_kfunc_ids) BTF_ID(func, bpf_testmod_test_mod_kfunc)BTF_SET_END (bpf_testmod_kfunc_ids) -static DEFINE_KFUNC_BTF_ID_SET(&bpf_testmod_kfunc_ids, bpf_testmod_kfunc_btf_set); +static struct kfunc_btf_id_set bpf_testmod_kfunc_btf_set = { + .owner =THIS_MODULE , + .set = &bpf_testmod_kfunc_ids, +};static int bpf_testmod_init (void ) {
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH v4 3/10 bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support
In reply to Kumar Kartikeya DwivediAllow passing PTR_TO_CTX, if the kfunc expects a matching struct type, and punt to PTR_TO_MEM block if reg->type does not fall in one of PTR_TO_BTF_ID or PTR_TO_SOCK* types. This will be used by future commits to get access to XDP and TC PTR_TO_CTX, and pass various data (flags, l4proto, netns_id, etc.) encoded in opts struct passed as pointer to kfunc.
For PTR_TO_MEM support, arguments are currently limited to pointer to scalar, or pointer to struct composed of scalars. This is done so that unsafe scenarios (like passing PTR_TO_MEM where PTR_TO_BTF_ID of in-kernel valid structure is expected, which may have pointers) are avoided. Since the argument checking happens basd on argument register type, it is not easy to ascertain what the expected type is. In the future, support for PTR_TO_MEM for kfunc can be extended to serve other usecases. The struct type whose pointer is passed in may have maximum nesting depth of 4, all recursively composed of scalars or struct with scalars.
Future commits will add negative tests that check whether these restrictions imposed for kfunc arguments are duly rejected by BPF verifier or not.
--- kernel/bpf/btf.c | 96 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 75 insertions(+), 21 deletions(-)
kernel/bpf/btf.c +75 -21
@@ -5574,12 +5574,56 @@ static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = {#endif }; +/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ +static bool __btf_type_is_scalar_struct (struct bpf_verifier_log *log, +const struct btf *btf, +const struct btf_type *t,int rec) +{ +const struct btf_type *member_type; +const struct btf_member *member; +u16 i; + +if (rec ==4 ) { +bpf_log (log,"max struct nesting depth 4 exceeded\n" ); +return false; + } + +if (!btf_type_is_struct (t)) +return false; + +for_each_member (i, t, member) { +const struct btf_array *array; + + member_type =btf_type_skip_modifiers (btf, member->type,NULL ); + +if (btf_type_is_struct (member_type)) { +if (!__btf_type_is_scalar_struct (log, btf, member_type, rec +1 )) +return false; +continue ; + } +if (btf_type_is_array (member_type)) { + array =btf_type_array (member_type); +/* FAM */ +if (!array->nelems) +return false; + member_type =btf_type_skip_modifiers (btf, array->type,NULL ); +if (!btf_type_is_scalar (member_type)) +return false; +continue ; + } +if (!btf_type_is_scalar (member_type)) +return false; + } +return true; +} +static int btf_check_func_arg_match (struct bpf_verifier_env *env,const struct btf *btf,u32 func_id,struct bpf_reg_state *regs,bool ptr_to_mem_ok) {struct bpf_verifier_log *log = &env->log; +bool is_kfunc =btf_is_kernel (btf);const char *func_name, *ref_tname;const struct btf_type *t, *ref_t;const struct btf_param *args;
@@ -5632,7 +5676,20 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, ref_t =btf_type_skip_modifiers (btf, t->type, &ref_id); ref_tname =btf_name_by_offset (btf, ref_t->name_off); -if (btf_is_kernel (btf)) { +if (btf_get_prog_ctx_type (log, btf, t, + env->prog->type, i)) { +/* If function expects ctx type in BTF check that caller +* is passing PTR_TO_CTX. +*/ +if (reg->type !=PTR_TO_CTX ) { +bpf_log (log, +"arg#%d expected pointer to ctx, but got %s\n" , + i,btf_type_str (t)); +return -EINVAL ; + } +if (check_ctx_reg (env, reg, regno)) +return -EINVAL ; + }else if (is_kfunc && (reg->type ==PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {const struct btf_type *reg_ref_t;const struct btf *reg_btf;const char *reg_ref_tname;
@@ -5648,14 +5705,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,if (reg->type ==PTR_TO_BTF_ID ) { reg_btf = reg->btf; reg_ref_id = reg->btf_id; - }else if (reg2btf_ids[reg->type]) { + }else { reg_btf = btf_vmlinux; reg_ref_id = *reg2btf_ids[reg->type]; - }else { -bpf_log (log,"kernel function %s args#%d expected pointer to %s %s but R%d is not a pointer to btf_id\n" , - func_name, i, -btf_type_str (ref_t), ref_tname, regno); -return -EINVAL ; } reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id,
@@ -5671,23 +5723,23 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env, reg_ref_tname);return -EINVAL ; } - }else if (btf_get_prog_ctx_type (log, btf, t, - env->prog->type, i)) { -/* If function expects ctx type in BTF check that caller -* is passing PTR_TO_CTX. -*/ -if (reg->type !=PTR_TO_CTX ) { -bpf_log (log, -"arg#%d expected pointer to ctx, but got %s\n" , - i,btf_type_str (t)); -return -EINVAL ; - } -if (check_ctx_reg (env, reg, regno)) -return -EINVAL ; }else if (ptr_to_mem_ok) {const struct btf_type *resolve_ret;u32 type_size; +if (is_kfunc) { +/* Permit pointer to mem, but only when argument +* type is pointer to scalar, or struct composed +* (recursively) of scalars. +*/ +if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (log, btf, ref_t,0 )) { +bpf_log (log, +"arg#%d pointer type %s %s must point to scalar or struct with scalar\n" , + i,btf_type_str (ref_t), ref_tname); +return -EINVAL ; + } + } + resolve_ret =btf_resolve_size (btf, ref_t, &type_size);if (IS_ERR (resolve_ret)) { bpf_log(log,
@@ -5700,6 +5752,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,if (check_mem_reg (env, reg, regno, type_size))return -EINVAL ; }else { +bpf_log (log,"reg type unsupported for arg#%d %sfunction %s#%d\n" , i, + is_kfunc ?"kernel " :"" , func_name, func_id);return -EINVAL ; } }
@@ -5749,7 +5803,7 @@ int btf_check_kfunc_arg_match(struct bpf_verifier_env *env, const struct btf *btf, u32 func_id, struct bpf_reg_state *regs) { - return btf_check_func_arg_match(env, btf, func_id, regs, false); + return btf_check_func_arg_match(env, btf, func_id, regs, true); }/* Convert BTF of a function into bpf_reg_state if possible
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v4 03/10] bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support
Quoted 4 lines · Kumar Kartikeya Dwivedi
kernel/bpf/btf.c +3
-0+/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ +static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log, + const struct btf *btf,
kernel/bpf/btf.c static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { · line 5589
Quoted 10 lines
kernel/bpf/btf.c +10
-0+ const struct btf_type *t, int rec) +{ +const struct btf_type *member_type; +const struct btf_member *member; +u16 i; + +if (rec ==4 ) { +bpf_log (log,"max struct nesting depth 4 exceeded\n" ); +return false; + }
As Matteo found out that saves stack with gcc only,
so I moved this check few lines below, just before recursive call.
kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 5735
Quoted 6 lines
kernel/bpf/btf.c +6
-0+if (is_kfunc) { +/* Permit pointer to mem, but only when argument +* type is pointer to scalar, or struct composed +* (recursively) of scalars. +*/ +if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (log, btf, ref_t,0 )) {
... and reformatted this line to fit screen width.
... and applied.
Please add individual selftest for this feature
(not tied into refcnted kfuncs and CT).
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 03/10] bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support
Quoted 28 lines · Alexei Starovoitov
Quoted 4 lines · Kumar Kartikeya Dwivedi
kernel/bpf/btf.c +3
-0+/* Returns true if struct is composed of scalars, 4 levels of nesting allowed */ +static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log, + const struct btf *btf,kernel/bpf/btf.c static u32 *reg2btf_ids[__BPF_REG_TYPE_MAX] = { · line 5589
Quoted 10 lines
kernel/bpf/btf.c +10
-0+ const struct btf_type *t, int rec) +{ +const struct btf_type *member_type; +const struct btf_member *member; +u16 i; + +if (rec ==4 ) { +bpf_log (log,"max struct nesting depth 4 exceeded\n" ); +return false; + }As Matteo found out that saves stack with gcc only,
so I moved this check few lines below, just before recursive call.kernel/bpf/btf.c static int btf_check_func_arg_match(struct bpf_verifier_env *env, · line 5735
Quoted 6 lines
kernel/bpf/btf.c +6
-0+if (is_kfunc) { +/* Permit pointer to mem, but only when argument +* type is pointer to scalar, or struct composed +* (recursively) of scalars. +*/ +if (!btf_type_is_scalar (ref_t) && !__btf_type_is_scalar_struct (log, btf, ref_t,0 )) {... and reformatted this line to fit screen width.
Quoted 2 lines
... and applied.
Thanks.
Quoted 2 lines
Please add individual selftest for this feature
(not tied into refcnted kfuncs and CT).
Ok, I'll add more in v5, but the second one in calls.c in patch 10/10 does check it.
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 4/10 bpf: Introduce mem, size argument pair support for kfunc
In reply to Kumar Kartikeya DwivediBPF helpers can associate two adjacent arguments together to pass memory of certain size, using ARG_PTR_TO_MEM and ARG_CONST_SIZE arguments. Since we don't use bpf_func_proto for kfunc, we need to leverage BTF to implement similar support.
The ARG_CONST_SIZE processing for helpers is refactored into a common check_mem_size_reg helper that is shared with kfunc as well. kfunc ptr_to_mem support follows logic similar to global functions, where verification is done as if pointer is not null, even when it may be null.
This leads to a simple to follow rule for writing kfunc: always check the argument pointer for NULL, except when it is PTR_TO_CTX. Also, the PTR_TO_CTX case is also only safe when the helper expecting pointer to program ctx is not exposed to other programs where same struct is not ctx type. In that case, the type check will fall through to other cases and would permit passing other types of pointers, possibly NULL at runtime.
Currently, we require the size argument to be prefixed with "len__" in the parameter name. This information is then recorded in kernel BTF and verified during function argument checking. In the future we can use BTF tagging instead, and modify the kernel function definitions. This will be a purely kernel-side change.
This allows us to have some form of backwards compatibility for structures that are passed in to the kernel function with their size, and allow variable length structures to be passed in if they are accompanied by a size parameter.
--- include/linux/bpf_verifier.h | 2 + kernel/bpf/btf.c | 41 +++++++++++- kernel/bpf/verifier.c | 124 ++++++++++++++++++++++------------- 3 files changed, 119 insertions(+), 48 deletions(-)
include/linux/bpf_verifier.h +2 -0
@@ -507,6 +507,8 @@ bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt);int check_ctx_reg (struct bpf_verifier_env *env,const struct bpf_reg_state *reg,int regno); +int check_kfunc_mem_size_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg, +u32 regno);int check_mem_reg (struct bpf_verifier_env *env,struct bpf_reg_state *reg,u32 regno,u32 mem_size);
kernel/bpf/btf.c +38 -3
60 lines not on this page · raw message
kernel/bpf/verifier.c +79 -45
142 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v4 04/10] bpf: Introduce mem, size argument pair support for kfunc
Quoted 5 lines · Kumar Kartikeya Dwivedi
kernel/bpf/btf.c +5
-0+static bool is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) +{ +const struct btf_type *t;
kernel/bpf/btf.c static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log, · line 5634
Quoted 10 lines
kernel/bpf/btf.c +10
-0+const char *param_name; + + t =btf_type_skip_modifiers (btf, arg->type,NULL ); +if (!btf_type_is_scalar (t) || reg->type !=SCALAR_VALUE ) +return false; + +/* In the future, this can be ported to use BTF tagging */ + param_name =btf_name_by_offset (btf, arg->name_off); +if (strncmp (param_name,"len__" ,sizeof ("len__" ) -1 )) +return false;
I like the feature and approach, but have a suggestion:
The "__sz" suffix would be shorter and more readable. wdyt?
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 04/10] bpf: Introduce mem, size argument pair support for kfunc
Quoted 12 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi
kernel/bpf/btf.c +11
-0+static bool is_kfunc_arg_mem_size (const struct btf *btf, +const struct btf_param *arg, +const struct bpf_reg_state *reg) +{ +const struct btf_type *t; +const char *param_name; + + t =btf_type_skip_modifiers (btf, arg->type,NULL ); +if (!btf_type_is_scalar (t) || reg->type !=SCALAR_VALUE ) +return false; +
Quoted 8 lines
kernel/bpf/btf.c static bool __btf_type_is_scalar_struct(struct bpf_verifier_log *log, · line 5634
Quoted 4 lines
kernel/bpf/btf.c +4
-0+/* In the future, this can be ported to use BTF tagging */ + param_name =btf_name_by_offset (btf, arg->name_off); +if (strncmp (param_name,"len__" ,sizeof ("len__" ) -1 )) +return false;I like the feature and approach, but have a suggestion:
The "__sz" suffix would be shorter and more readable. wdyt?
Sounds good, I'll change this in v5.
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 5/10 bpf: Add reference tracking support to kfunc
In reply to Kumar Kartikeya DwivediThis patch adds verifier support for PTR_TO_BTF_ID return type of kfunc to be a reference, by reusing acquire_reference_state/release_reference support for existing in-kernel bpf helpers.
Verifier ops struct is extended with three callbacks:
- is_acquire_kfunc
Return true if kfunc_btf_id, module pair is an acquire kfunc. This will acquire_reference_state for the returned PTR_TO_BTF_ID (this is the only allow return value). Note that acquire kfunc must always return a PTR_TO_BTF_ID{_OR_NULL}, otherwise the program is rejected.
- is_release_kfunc
Return true if kfunc_btf_id, module pair is a release kfunc. This will release the reference to the passed in PTR_TO_BTF_ID which has a reference state (from earlier acquire kfunc).
The btf_check_func_arg_match returns the regno (of argument register, hence > 0) if the kfunc is a release kfunc, and a proper referenced PTR_TO_BTF_ID is being passed to it.
This is similar to how helper call check uses bpf_call_arg_meta to store the ref_obj_id that is later used to release the reference. Similar to in-kernel helper, we only allow passing one referenced PTR_TO_BTF_ID as an argument. It can also be passed in to normal kfunc, but in case of release kfunc there must always be one PTR_TO_BTF_ID argument that is referenced.
- is_kfunc_ret_type_null
For kfunc returning PTR_TO_BTF_ID, tells if it can be NULL, hence force caller to mark the pointer not null (using check) before accessing it. Note that taking into account the case fixed by commit 93c230e3f5bd ("bpf: Enforce id generation for all may-be-null register type") we assign a non-zero id for mark_ptr_or_null_reg logic. Later, if more return types are supported by kfunc, which have a _OR_NULL variant, it might be better to move this id generation under a common reg_type_may_be_null check, similar to the case in the commit.
Later patches will implement these callbacks.
Referenced PTR_TO_BTF_ID is currently only limited to kfunc, but can be extended in the future to other BPF helpers as well. For now, we can rely on the btf_struct_ids_match check to ensure we get the pointer to the expected struct type. In the future, care needs to be taken to avoid ambiguity for reference PTR_TO_BTF_ID passed to release function, in case multiple candidates can release same BTF ID.
e.g. there might be two release kfuncs (or kfunc and helper):
foo(struct abc *p);bar(struct abc *p);
... such that both release a PTR_TO_BTF_ID with btf_id of struct abc. In this case we would need to track the acquire function corresponding to the release function to avoid type confusion, and store this information in the register state so that an incorrect program can be rejected. This is not a problem right now, hence it is left as an exercise for the future patch introducing such a case in the kernel.
--- include/linux/bpf.h | 6 ++++- include/linux/btf.h | 30 +++++++++++++++++++++- kernel/bpf/btf.c | 60 ++++++++++++++++++++++++++++++++++++++----- kernel/bpf/verifier.c | 54 +++++++++++++++++++++++++++++++++----- 4 files changed, 135 insertions(+), 15 deletions(-)
include/linux/bpf.h +5 -1
@@ -521,6 +521,9 @@ struct bpf_verifier_ops {enum bpf_access_type atype, u32 *next_btf_id);bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); +bool (*is_acquire_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_release_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_kfunc_ret_type_null)(u32 kfunc_btf_id,struct module *owner); };struct bpf_prog_offload_ops {
@@ -1712,7 +1715,8 @@ int btf_check_subprog_arg_match(struct bpf_verifier_env *env, int subprog, struct bpf_reg_state *regs);int btf_check_kfunc_arg_match (struct bpf_verifier_env *env,const struct btf *btf,u32 func_id, -struct bpf_reg_state *regs); +struct bpf_reg_state *regs, +struct module *btf_mod);int btf_prepare_func_args (struct bpf_verifier_env *env,int subprog,struct bpf_reg_state *reg); int btf_check_type_match(struct bpf_verifier_log *log, const struct bpf_prog *prog,
include/linux/btf.h +29 -1
@@ -321,7 +321,10 @@ static inline const char *btf_name_by_offset(const struct btf *btf,#endif enum kfunc_btf_id_set_type { -BTF_SET_CHECK , +BTF_SET_CHECK ,/* Allowed kfunc set */ +BTF_SET_ACQUIRE ,/* Acquire kfunc set */ +BTF_SET_RELEASE ,/* Release kfunc set */ +BTF_SET_RET_NULL ,/* kfunc with 'return type PTR_TO_BTF_ID_OR_NULL' set */ __BTF_SET_MAX, };
@@ -331,6 +334,9 @@ struct kfunc_btf_id_set {struct btf_id_set *sets[__BTF_SET_MAX];struct {struct btf_id_set *set; +struct btf_id_set *acq_set; +struct btf_id_set *rel_set; +struct btf_id_set *null_set; }; };struct module *owner;
@@ -348,6 +354,12 @@ void unregister_kfunc_btf_id_set(struct kfunc_btf_id_list *l, struct kfunc_btf_id_set *s);bool bpf_check_mod_kfunc_call (struct kfunc_btf_id_list *klist,u32 kfunc_id,struct module *owner); +bool bpf_is_mod_acquire_kfunc (struct kfunc_btf_id_list *klist,u32 kfunc_id, +struct module *owner); +bool bpf_is_mod_release_kfunc (struct kfunc_btf_id_list *klist,u32 kfunc_id, +struct module *owner); +bool bpf_is_mod_kfunc_ret_type_null (struct kfunc_btf_id_list *klist, +u32 kfunc_id,struct module *owner);extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list;extern struct kfunc_btf_id_list prog_test_kfunc_list;
@@ -365,6 +377,22 @@ static inline bool bpf_check_mod_kfunc_call(struct kfunc_btf_id_list *klist, {return false; } +static inline bool bpf_is_mod_acquire_kfunc (struct kfunc_btf_id_list *klist, +u32 kfunc_id,struct module *owner) +{ +return false; +} +static inline bool bpf_is_mod_release_kfunc (struct kfunc_btf_id_list *klist, +u32 kfunc_id,struct module *owner) +{ +return false; +} +static inline bool +bpf_is_mod_kfunc_ret_type_null (struct kfunc_btf_id_list *klist,u32 kfunc_id, +struct module *owner) +{ +return false; +}static struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list__maybe_unused ;static struct kfunc_btf_id_list prog_test_kfunc_list__maybe_unused ;
kernel/bpf/btf.c +54 -6
103 lines not on this page · raw message
kernel/bpf/verifier.c +47 -7
104 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v4 05/10] bpf: Add reference tracking support to kfunc
include/linux/bpf.h struct bpf_verifier_ops { · line 526
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf.h +3
-0@@ -521,6 +521,9 @@ struct bpf_verifier_ops {enum bpf_access_type atype, u32 *next_btf_id);bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); +bool (*is_acquire_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_release_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_kfunc_ret_type_null)(u32 kfunc_btf_id,struct module *owner);
Same feedback as before...
Those callbacks are not necessary.
The existing check_kfunc_call() is just as inconvenient.
When module's BTF comes in could you add it to mod's info instead of introducing callbacks for every kind of data the module has. Those callbacks don't server any purpose other than passing the particular data set back. The verifier side should access those data sets directly.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 05/10] bpf: Add reference tracking support to kfunc
Quoted 16 lines · Alexei Starovoitov
include/linux/bpf.h struct bpf_verifier_ops { · line 526
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf.h +3
-0@@ -521,6 +521,9 @@ struct bpf_verifier_ops {enum bpf_access_type atype, u32 *next_btf_id);bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); +bool (*is_acquire_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_release_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_kfunc_ret_type_null)(u32 kfunc_btf_id,struct module *owner);Same feedback as before...
Quoted 6 lines
Those callbacks are not necessary.
The existingcheck_kfunc_call()is just as inconvenient.
When module's BTF comes in could you add it to mod's info instead of introducing callbacks for every kind of data the module has. Those callbacks don't server any purpose other than passing the particular data set back. The verifier side should access those data sets directly.
Ok, interesting idea. So these then go into the ".modinfo" section? I think then we can also drop the check_kfunc_call callback?
--
Kartikeya
Re: [PATCH bpf-next v4 05/10] bpf: Add reference tracking support to kfunc
Quoted 18 lines · Kumar Kartikeya Dwivedi
Quoted 16 lines · Alexei Starovoitov
include/linux/bpf.h struct bpf_verifier_ops { · line 526
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf.h +3
-0@@ -521,6 +521,9 @@ struct bpf_verifier_ops {enum bpf_access_type atype, u32 *next_btf_id);bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); +bool (*is_acquire_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_release_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_kfunc_ret_type_null)(u32 kfunc_btf_id,struct module *owner);Same feedback as before...
Quoted 8 lines
Quoted 6 lines
Those callbacks are not necessary.
The existingcheck_kfunc_call()is just as inconvenient.
When module's BTF comes in could you add it to mod's info instead of introducing callbacks for every kind of data the module has. Those callbacks don't server any purpose other than passing the particular data set back. The verifier side should access those data sets directly.Ok, interesting idea. So these then go into the ".modinfo" section?
It doesn't need to be a special section.
The btf_module_notify() parses BTF.
At the same time it can add a kfunc whitelist to "struct module". The btf_ids[ACQUIRE/RELEASE][] arrays will be a part of
the "struct module" too.
If we can do a btf name convention then this job can be performed generically by btf_module_notify().
Otherwise __init of the module can populate arrays in "struct module".
Quoted 2 lines
I think then
we can also drop thecheck_kfunc_callcallback?
Right. Would be great to remove that callback too.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 05/10] bpf: Add reference tracking support to kfunc
Quoted 29 lines · Alexei Starovoitov
Quoted 18 lines · Kumar Kartikeya Dwivedi
Quoted 16 lines · Alexei Starovoitov
include/linux/bpf.h struct bpf_verifier_ops { · line 526
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf.h +3
-0@@ -521,6 +521,9 @@ struct bpf_verifier_ops {enum bpf_access_type atype, u32 *next_btf_id);bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); +bool (*is_acquire_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_release_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_kfunc_ret_type_null)(u32 kfunc_btf_id,struct module *owner);Same feedback as before...
Quoted 8 lines
Quoted 6 lines
Those callbacks are not necessary.
The existingcheck_kfunc_call()is just as inconvenient.
When module's BTF comes in could you add it to mod's info instead of introducing callbacks for every kind of data the module has. Those callbacks don't server any purpose other than passing the particular data set back. The verifier side should access those data sets directly.Ok, interesting idea. So these then go into the ".modinfo" section?
Quoted 9 lines
It doesn't need to be a special section.
Thebtf_module_notify()parses BTF.
At the same time it can add a kfunc whitelist to "struct module". The btf_ids[ACQUIRE/RELEASE][] arrays will be a part of
the "struct module" too.
If we can do a btf name convention then this job can be performed generically bybtf_module_notify().
Otherwise__initof the module can populate arrays in "struct module".
Nice idea, I think this is better than what I am doing (it also prevents constant researching into the list).
But IIUC I think this btf_ids array needs to go into struct btf instead, since if module is compiled as built-in, we will not have any struct module for it.
Then we can concatenate all sets of same type (check/acquire/release etc.) and sort them to directly search using a single btf_id_set_contains call, the code becomes same for btf_vmlinux or module btf. struct module is not needed anymore.
WDYT?
Quoted 4 lines
Quoted 2 lines
I think then
we can also drop thecheck_kfunc_callcallback?Right. Would be great to remove that callback too.
Ok, will do.
--
Kartikeya
Re: [PATCH bpf-next v4 05/10] bpf: Add reference tracking support to kfunc
Quoted 48 lines · Kumar Kartikeya Dwivedi
Quoted 29 lines · Alexei Starovoitov
Quoted 18 lines · Kumar Kartikeya Dwivedi
Quoted 16 lines · Alexei Starovoitov
include/linux/bpf.h struct bpf_verifier_ops { · line 526
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf.h +3
-0@@ -521,6 +521,9 @@ struct bpf_verifier_ops {enum bpf_access_type atype, u32 *next_btf_id);bool (*check_kfunc_call)(u32 kfunc_btf_id,struct module *owner); +bool (*is_acquire_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_release_kfunc)(u32 kfunc_btf_id,struct module *owner); +bool (*is_kfunc_ret_type_null)(u32 kfunc_btf_id,struct module *owner);Same feedback as before...
Quoted 8 lines
Quoted 6 lines
Those callbacks are not necessary.
The existingcheck_kfunc_call()is just as inconvenient.
When module's BTF comes in could you add it to mod's info instead of introducing callbacks for every kind of data the module has. Those callbacks don't server any purpose other than passing the particular data set back. The verifier side should access those data sets directly.Ok, interesting idea. So these then go into the ".modinfo" section?
Quoted 9 lines
It doesn't need to be a special section.
Thebtf_module_notify()parses BTF.
At the same time it can add a kfunc whitelist to "struct module". The btf_ids[ACQUIRE/RELEASE][] arrays will be a part of
the "struct module" too.
If we can do a btf name convention then this job can be performed generically bybtf_module_notify().
Otherwise__initof the module can populate arrays in "struct module".Nice idea, I think this is better than what I am doing (it also prevents constant researching into the list).
But IIUC I think this
btf_idsarray needs to go into struct btf instead, since if module is compiled as built-in, we will not have any struct module for it.
Quoted 5 lines
Then we can concatenate all sets of same type (check/acquire/release etc.) and sort them to directly search using a single
btf_id_set_containscall, the code becomes same forbtf_vmlinuxor module btf. struct module is not needed anymore.WDYT?
You mean that btf_parse_module() will do this?
That would work for vmlinux's BTF too then?
Makes sense to me!
Kumar Kartikeya Dwivedi
maintainerPATCH v4 6/10 bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
In reply to Kumar Kartikeya DwivediThe goal of this commit is to enable invalidation of pointers formed from walking a referenced PTR_TO_BTF_ID. Currently, mark_btf_ld_reg can either change the destination register to be SCALAR_VALUE or PTR_TO_BTF_ID, but with the code until previous commit any such PTR_TO_BTF_ID will remain valid after we release the referenced pointer from which it was formed.
However, we cannot simply copy ref_obj_id (such that release_reference will match and invalidate all pointers formed by pointer walking), as we can obtain the same BTF ID in the destination register, leading to confusion during release. An example is shown below:
For a type like so:
struct foo { struct foo *next; };
r1 = acquire(...); // BTF ID of struct foo
if (r1) {
r2 = r1->next; // BTF ID of struct foo, and we copied ref_obj_id
// in mark_btf_ld_reg.
release(r2);
}With this logic, the above snippet succeeds. Hence we need to distinguish the canonical reference and pointers formed from it.
We introduce a 'parent_ref_obj_id' member in bpf_reg_state, for a referenced register, only one of ref_obj_id or parent_ref_obj_id may be set, i.e. either a register holds a canonical reference, or it is related to a canonical reference for invalidation purposes (contains an edge pointing to it by way of having the same ref_obj_id in parent_ref_obj_id, in the graph of objects).
When releasing reference, we ensure that both are not set at once, and then release if either of them match the requested ref_obj_id to be released. This ensures that the example given above will not succeed. A test to this end has been added in later patches.
Typically, kernel objects have a nested object lifetime (where the parent object 'owns' the objects it holds references to). However, this is not always true. For now, we don't need support to hold on to references to objects obtained from a refcounted PTR_TO_BTF_ID after its release, but this can be relaxed on a case by case basis (i.e. based on the BTF ID and program type/attach type) in the future.
The safest assumption for the verifier to make in absence of any other hints, is that all such pointers formed from refcounted PTR_TO_BTF_ID shall be invalidated.
--- include/linux/bpf_verifier.h | 10 +++++++ kernel/bpf/verifier.c | 54 ++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 12 deletions(-)
include/linux/bpf_verifier.h +10 -0
@@ -128,6 +128,16 @@ struct bpf_reg_state {* allowed and has the same effect as bpf_sk_release(sk). */ u32 ref_obj_id; +/* This is set for pointers which are derived from referenced +* pointer (e.g. PTR_TO_BTF_ID pointer walking), so that the +* pointers obtained by walking referenced PTR_TO_BTF_ID +* are appropriately invalidated when the lifetime of their +* parent object ends. +* +* Only one of ref_obj_id and parent_ref_obj_id can be set, +* never both at once. +*/ +u32 parent_ref_obj_id;/* For scalar types (SCALAR_VALUE), this represents our knowledge of * the actual value. * For pointer types, this represents the variable part of the offset
kernel/bpf/verifier.c +42 -12
109 lines not on this page · raw message
Signature · 1 line
2.34.1
Re: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 8 lines · Kumar Kartikeya Dwivedi
include/linux/bpf_verifier.h +0
-0@@ -128,6 +128,16 @@ struct bpf_reg_state {* allowed and has the same effect as bpf_sk_release(sk). */ u32 ref_obj_id;
include/linux/bpf_verifier.h struct bpf_reg_state { · line 140
Quoted 10 lines
include/linux/bpf_verifier.h +10
-0+/* This is set for pointers which are derived from referenced +* pointer (e.g. PTR_TO_BTF_ID pointer walking), so that the +* pointers obtained by walking referenced PTR_TO_BTF_ID +* are appropriately invalidated when the lifetime of their +* parent object ends. +* +* Only one of ref_obj_id and parent_ref_obj_id can be set, +* never both at once. +*/ +u32 parent_ref_obj_id;
How would it handle parent of parent?
Did you consider map_uid approach ?
Similar uid can be added for PTR_TO_BTF_ID.
Then every such pointer will be unique. Each deref will get its own uid. I think the advantage of parent_ref_obj_id approach is that the program can acquire a pointer through one kernel type, do some deref, and then release it through a deref of other type. I'm not sure how practical is that and it feels a bit dangerous.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf_verifier.h +4
-0@@ -128,6 +128,16 @@ struct bpf_reg_state {* allowed and has the same effect as bpf_sk_release(sk). */ u32 ref_obj_id; +/* This is set for pointers which are derived from referenced +* pointer (e.g. PTR_TO_BTF_ID pointer walking), so that the +* pointers obtained by walking referenced PTR_TO_BTF_ID +* are appropriately invalidated when the lifetime of their
Quoted 8 lines
include/linux/bpf_verifier.h struct bpf_reg_state { · line 140
Quoted 6 lines
include/linux/bpf_verifier.h +6
-0+* parent object ends. +* +* Only one of ref_obj_id and parent_ref_obj_id can be set, +* never both at once. +*/ +u32 parent_ref_obj_id;How would it handle parent of parent?
When you do:
r1 = acquire();
it gets ref_obj_id as N, then when you load r1->next, it does mark_btf_ld_reg with reg->ref_obj_id ?: reg->parent_ref_obj_id, the latter is zero so it copies ref, but into parent_ref_obj_id.
r2 = r1->next;
From here on, parent_ref_obj_id is propagated into all further mark_btf_ld_reg, so if we do since ref_obj_id will be zero from previous mark_btf_ld_reg:
r3 = r2->next; // it will copy parent_ref_obj_id
I think it even works fine when you reach it indirectly, like foo->bar->foo, if first foo is referenced.
... but maybe I missed some detail, do you see a problem in this approach?
Quoted 3 lines
Did you consider
map_uidapproach ?
Similar uid can be added forPTR_TO_BTF_ID.
Then every such pointer will be unique. Each deref will get its own uid.
I'll look into it, I didn't consider it before. My idea was to invalidate pointers obtained from a referenced ptr_to_btf_id so I copied the same ref_obj_id into parent_ref_obj_id, so that it can be matched during release. How would that work in the btf_uid approach if they are unique? Do we copy the same ref_obj_id into btf_uid? Then it's not very different except being btf_id ptr specific state, right?
Or we can copy ref_obj_id and also set uid to disallow it from being released, but still allow invalidation.
Quoted 4 lines
I think the advantage of
parent_ref_obj_idapproach is that the program can acquire a pointer through one kernel type, do some deref, and then release it through a deref of other type. I'm not sure how practical is that and it feels a bit dangerous.
I think I don't allow releasing when ref_obj_id is 0 (which would be the case when parent_ref_obj_id is set), only indirectly invalidating them.
--
Kartikeya
Re: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 55 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf_verifier.h +4
-0@@ -128,6 +128,16 @@ struct bpf_reg_state {* allowed and has the same effect as bpf_sk_release(sk). */ u32 ref_obj_id; +/* This is set for pointers which are derived from referenced +* pointer (e.g. PTR_TO_BTF_ID pointer walking), so that the +* pointers obtained by walking referenced PTR_TO_BTF_ID +* are appropriately invalidated when the lifetime of their Quoted 8 lines
include/linux/bpf_verifier.h struct bpf_reg_state { · line 140
Quoted 6 lines
include/linux/bpf_verifier.h +6
-0+* parent object ends. +* +* Only one of ref_obj_id and parent_ref_obj_id can be set, +* never both at once. +*/ +u32 parent_ref_obj_id;How would it handle parent of parent?
When you do:
r1 =
acquire();it gets
ref_obj_idas N, then when you loadr1->next, it doesmark_btf_ld_regwithreg->ref_obj_id?:reg->parent_ref_obj_id, the latter is zero so it copies ref, but intoparent_ref_obj_id.r2 =
r1->next;From here on,
parent_ref_obj_idis propagated into all furthermark_btf_ld_reg, so if we do sinceref_obj_idwill be zero from previousmark_btf_ld_reg:r3 =
r2->next; // it will copyparent_ref_obj_idI think it even works fine when you reach it indirectly, like
foo->bar->foo, if first foo is referenced.... but maybe I missed some detail, do you see a problem in this approach?
Quoted 3 lines
Did you consider
map_uidapproach ?
Similar uid can be added forPTR_TO_BTF_ID.
Then every such pointer will be unique. Each deref will get its own uid.I'll look into it, I didn't consider it before. My idea was to invalidate pointers obtained from a referenced
ptr_to_btf_idso I copied the sameref_obj_idintoparent_ref_obj_id, so that it can be matched during release. How would that work in thebtf_uidapproach if they are unique? Do we copy the sameref_obj_idintobtf_uid? Then it's not very different except beingbtf_idptr specific state, right?
Quoted 2 lines
Or we can copy
ref_obj_idand also set uid to disallow it from being released, but still allow invalidation.
The goal is to disallow:
struct foo { struct foo *next; };
r1 = acquire(...); // BTF ID of struct foo
if (r1) {
r2 = r1->next;
release(r2);
}right?
With btf_uid approach each deref gets its own uid.
r2 = r1->next
and
r3 = r1->next
will get different uids.
When type == PTR_TO_BTF_ID its reg->ref_obj_id will be considered together with btf_uid.
Both ref_obj_id and btf_uid need to be the same.
But let's go back a bit.
Why ref_obj_id is copied on deref?
Shouldn't r2 get a different ref_obj_id after r2 = r1->next ?
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 63 lines · Alexei Starovoitov
Quoted 55 lines · Kumar Kartikeya Dwivedi
Quoted 13 lines · Alexei Starovoitov
Quoted 12 lines · Kumar Kartikeya Dwivedi
include/linux/bpf_verifier.h +4
-0@@ -128,6 +128,16 @@ struct bpf_reg_state {* allowed and has the same effect as bpf_sk_release(sk). */ u32 ref_obj_id; +/* This is set for pointers which are derived from referenced +* pointer (e.g. PTR_TO_BTF_ID pointer walking), so that the +* pointers obtained by walking referenced PTR_TO_BTF_ID +* are appropriately invalidated when the lifetime of their Quoted 8 lines
include/linux/bpf_verifier.h struct bpf_reg_state { · line 140
Quoted 6 lines
include/linux/bpf_verifier.h +6
-0+* parent object ends. +* +* Only one of ref_obj_id and parent_ref_obj_id can be set, +* never both at once. +*/ +u32 parent_ref_obj_id;How would it handle parent of parent?
When you do:
r1 =
acquire();it gets
ref_obj_idas N, then when you loadr1->next, it doesmark_btf_ld_regwithreg->ref_obj_id?:reg->parent_ref_obj_id, the latter is zero so it copies ref, but intoparent_ref_obj_id.r2 =
r1->next;From here on,
parent_ref_obj_idis propagated into all furthermark_btf_ld_reg, so if we do sinceref_obj_idwill be zero from previousmark_btf_ld_reg:r3 =
r2->next; // it will copyparent_ref_obj_idI think it even works fine when you reach it indirectly, like
foo->bar->foo, if first foo is referenced.... but maybe I missed some detail, do you see a problem in this approach?
Quoted 3 lines
Did you consider
map_uidapproach ?
Similar uid can be added forPTR_TO_BTF_ID.
Then every such pointer will be unique. Each deref will get its own uid.I'll look into it, I didn't consider it before. My idea was to invalidate pointers obtained from a referenced
ptr_to_btf_idso I copied the sameref_obj_idintoparent_ref_obj_id, so that it can be matched during release. How would that work in thebtf_uidapproach if they are unique? Do we copy the sameref_obj_idintobtf_uid? Then it's not very different except beingbtf_idptr specific state, right?Quoted 2 lines
Or we can copy
ref_obj_idand also set uid to disallow it from being released, but still allow invalidation.The goal is to disallow:
struct foo { struct foo *next; };
Quoted 7 lines
r1 = acquire(...); // BTF ID of struct foo
if (r1) { r2 = r1->next; release(r2); }right?
Yes, but not just that, we also want to prevent use of r2 after release(r1). Then snippet above is problematic if we get same BTF ID ptr in r2 and try to solve that in the naive way (just copy ref_obj_id in dst_reg), because verifier will not be able to distinguish between r1 and r2 for purposes of release kfunc call.
Quoted 9 lines
With
btf_uidapproach each deref gets its own uid.
r2 =r1->next
and
r3 =r1->next
will get different uids.
When type ==PTR_TO_BTF_IDitsreg->ref_obj_idwill be considered together withbtf_uid.
Bothref_obj_idandbtf_uidneed to be the same.
That will indeed work, I can rework it this way. After acquire_reference_state we can set btf_uid = ref_obj_id, then simply assign fresh btf_uid on mark_btf_ld_reg.
Not pushing back, but I am trying to understand why you think this is better than simply doing it the way in this patch? What additional cases is btf_uid approach considering that I am missing? I don't understand what we get if each deref gets its own unique btf_uid.
There are only two objectives: prevent use of r2 after r1 is gone, and prevent r2 being passed into release kfunc (discovered when I simply copied ref_obj_id).
Quoted 2 lines
But let's go back a bit.
Whyref_obj_idis copied on deref?
It is, but into parent_ref_obj_id, to match during release_reference.
Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?
It's ref_obj_id is still 0.
Thinking about this more, we actually only need 1 extra bit of information in reg_state, not even a new member. We can simply copy ref_obj_id and set this bit, then we can reject this register during release but consider it during release_reference.
--
Kartikeya
Re: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 11 lines · Kumar Kartikeya Dwivedi
It is, but into
parent_ref_obj_id, to match duringrelease_reference.Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?It's
ref_obj_idis still 0.Thinking about this more, we actually only need 1 extra bit of information in
reg_state, not even a new member. We can simply copyref_obj_idand set this bit, then we can reject this register during release but consider it duringrelease_reference.
It seems to me that this patch created the problem and it's trying to fix it at the same time.
mark_btf_ld_reg() shouldn't be copying ref_obj_id. If it keeps it as zero the problem will not happen, no?
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 9 lines · Alexei Starovoitov
Quoted 7 lines · Kumar Kartikeya Dwivedi
It is, but into
parent_ref_obj_id, to match duringrelease_reference.Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?It's
ref_obj_idis still 0.
Quoted 8 lines
Quoted 4 lines
Thinking about this more, we actually only need 1 extra bit of information in
reg_state, not even a new member. We can simply copyref_obj_idand set this bit, then we can reject this register during release but consider it duringrelease_reference.It seems to me that this patch created the problem and it's trying to fix it at the same time.
Yes, sort of. Maybe I need to improve the commit message? I give an example below, and the first half of commit explains that if we simply did copy ref_obj_id, it would lead to the case in the previous mail (same BTF ID ptr can be passed), so we need to do something different.
Maybe that is what is confusing you.
Quoted 2 lines
mark_btf_ld_reg()shouldn't be copyingref_obj_id. If it keeps it as zero the problem will not happen, no?
It is copying it but writing it to parent_ref_obj_id. It keeps ref_obj_id as 0 for all deref pointers.
r1 = acq(); // r1.ref = acquire_reference_state();
ref = N
r2 = r1->a; // mark_btf_ld_reg -> copy r1.(ref ?: parent_ref) -> so r2.parent_ref = r1.ref r3 = r2->b; // mark_btf_ld_reg -> copy r2.(ref ?: parent_ref) -> so r3.parent_ref = r2.parent_ref r4 = r3->c; // mark_btf_ld_reg -> copy r3.(ref ?: parent_ref) -> so r4.parent_ref = r3.parent_ref
rel(r1); // if (reg.ref == r1.ref || reg.parent_ref == r1.ref) invalidate(reg)
As you see, mark_btf_ld_reg only ever writes to parent_ref_obj_id, not ref_obj_id. It just copies ref_obj_id when it is set, over parent_ref_obj_id, and only one of two can be set.
--
Kartikeya
Re: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 20 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · Alexei Starovoitov
Quoted 7 lines · Kumar Kartikeya Dwivedi
It is, but into
parent_ref_obj_id, to match duringrelease_reference.Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?It's
ref_obj_idis still 0.Quoted 8 lines
Quoted 4 lines
Thinking about this more, we actually only need 1 extra bit of information in
reg_state, not even a new member. We can simply copyref_obj_idand set this bit, then we can reject this register during release but consider it duringrelease_reference.It seems to me that this patch created the problem and it's trying to fix it at the same time.
Quoted 6 lines
Yes, sort of. Maybe I need to improve the commit message? I give an example below, and the first half of commit explains that if we simply did copy
ref_obj_id, it would lead to the case in the previous mail (same BTF ID ptr can be passed), so we need to do something different.Maybe that is what is confusing you.
I'm still confused.
Why does mark_btf_ld_reg() need to copy ref_obj_id ? It should keep it as zero.mark_btf_ld_reg() is used in deref only.
The ref_obj_id is assigned by check_helper_call(). r2 = r0; will copy it, but
r2 = r0->next; will keep r2->ref_obj_id as zero.
Quoted 13 lines
Quoted 2 lines
mark_btf_ld_reg()shouldn't be copyingref_obj_id. If it keeps it as zero the problem will not happen, no?It is copying it but writing it to
parent_ref_obj_id. It keepsref_obj_idas 0 for all deref pointers.r1 =
acq(); // r1.ref =acquire_reference_state();
ref = N
r2 =r1->a; //mark_btf_ld_reg-> copy r1.(ref ?:parent_ref) -> so r2.parent_ref = r1.ref r3 =r2->b; //mark_btf_ld_reg-> copy r2.(ref ?:parent_ref) -> so r3.parent_ref = r2.parent_ref r4 =r3->c; //mark_btf_ld_reg-> copy r3.(ref ?:parent_ref) -> so r4.parent_ref = r3.parent_ref
rel(r1); // if (reg.ref == r1.ref || reg.parent_ref == r1.ref) invalidate(reg)
Quoted 3 lines
As you see,
mark_btf_ld_regonly ever writes toparent_ref_obj_id, notref_obj_id. It just copiesref_obj_idwhen it is set, overparent_ref_obj_id, and only one of two can be set.
I don't understand why such logic is needed.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 27 lines · Alexei Starovoitov
Quoted 25 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · Alexei Starovoitov
Quoted 7 lines · Kumar Kartikeya Dwivedi
It is, but into
parent_ref_obj_id, to match duringrelease_reference.Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?It's
ref_obj_idis still 0.Quoted 8 lines
Quoted 4 lines
Thinking about this more, we actually only need 1 extra bit of information in
reg_state, not even a new member. We can simply copyref_obj_idand set this bit, then we can reject this register during release but consider it duringrelease_reference.It seems to me that this patch created the problem and it's trying to fix it at the same time.
Yes, sort of. Maybe I need to improve the commit message? I give an example below, and the first half of commit explains that if we simply did copy
ref_obj_id, it would lead to the case in the previous mail (same BTF ID ptr can be passed), so we need to do something different.
Quoted 5 lines
Quoted 1 line
Maybe that is what is confusing you.
I'm still confused.
Why doesmark_btf_ld_reg()need to copyref_obj_id? It should keep it as zero.
So that we can find deref pointers obtained from the reg having that ref_obj_id when it is released, and invalidate them. But since directly storing in ref_obj_id of deref dst_reg will be bad (if we get same BTF ID from deref we could now pass it to release kfunc), we add a new member and match on that.
Quoted 18 lines
mark_btf_ld_reg()is used in deref only. Theref_obj_idis assigned bycheck_helper_call(). r2 = r0; will copy it, but
r2 =r0->next; will keepr2->ref_obj_idas zero.Quoted 13 lines
Quoted 2 lines
mark_btf_ld_reg()shouldn't be copyingref_obj_id. If it keeps it as zero the problem will not happen, no?It is copying it but writing it to
parent_ref_obj_id. It keepsref_obj_idas 0 for all deref pointers.r1 =
acq(); // r1.ref =acquire_reference_state();
ref = N
r2 =r1->a; //mark_btf_ld_reg-> copy r1.(ref ?:parent_ref) -> so r2.parent_ref = r1.ref r3 =r2->b; //mark_btf_ld_reg-> copy r2.(ref ?:parent_ref) -> so r3.parent_ref = r2.parent_ref r4 =r3->c; //mark_btf_ld_reg-> copy r3.(ref ?:parent_ref) -> so r4.parent_ref = r3.parent_ref
rel(r1); // if (reg.ref == r1.ref || reg.parent_ref == r1.ref) invalidate(reg)
Quoted 5 lines
Quoted 3 lines
As you see,
mark_btf_ld_regonly ever writes toparent_ref_obj_id, notref_obj_id. It just copiesref_obj_idwhen it is set, overparent_ref_obj_id, and only one of two can be set.I don't understand why such logic is needed.
Ok, let me try to explain once how I arrived at it. If you still don't like it, I'll drop it from the series.
So until this patch, when we do the following:
struct nf_conn *ct = bpf_xdp_ct_lookup(...);
if (ct) {
struct nf_conn *master = ct->master;
bpf_ct_release(ct);
unsigned long status = master->status; // I want to prevent this
}... this will work, which is ok (as in won't crash the kernel) since the load will be converted to BPF_PROBE_MEM, but I want to disallow this case since it is obviously incorrect.
The obvious solution (to me) was to kill all registers and stack slots for deref pointers.
My first naive solution was to simply copy ref_obj_id on mark_btf_ld_reg, so that it can be matched and released from release_reference.
But then I noticed that if the BTF ID is same, there is no difference when it is passed to release kfunc compared to the original register it was loaded from.
struct nf_conn *ct = bpf_xdp_ct_lookup(...);
if (ct) {
struct nf_conn *master = ct->master; // copied ref_obj_id
bpf_ct_release(master); // works, but shouldn't!
}So the code needed some way to distinguish this deref pointer that must be invalidated only when its 'parent' goes away. Hence the introduction of parent_ref_obj_id, and the invariant that only one of ref_obj_id or parent_ref_obj_id must be set.
Now release_reference becomes: kill all registers/stack slots with ref == ref_obj_id (directly acquired) and ref == parent_ref_obj_id (formed using deref chains (of arbitrary length)).
Either a register is dst_reg of acquire kfunc, or deref of that reg, but can't be both. Only the first case can be passed to release kfunc (or a copy from mov), not the second (since it will always have invalid ref_obj_id == 0).
If this is still not inspiring confidence, I'll drop the patch for now.
--
Kartikeya
Re: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 75 lines · Kumar Kartikeya Dwivedi
Quoted 27 lines · Alexei Starovoitov
Quoted 25 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · Alexei Starovoitov
Quoted 7 lines · Kumar Kartikeya Dwivedi
It is, but into
parent_ref_obj_id, to match duringrelease_reference.Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?It's
ref_obj_idis still 0.Quoted 8 lines
Quoted 4 lines
Thinking about this more, we actually only need 1 extra bit of information in
reg_state, not even a new member. We can simply copyref_obj_idand set this bit, then we can reject this register during release but consider it duringrelease_reference.It seems to me that this patch created the problem and it's trying to fix it at the same time.
Yes, sort of. Maybe I need to improve the commit message? I give an example below, and the first half of commit explains that if we simply did copy
ref_obj_id, it would lead to the case in the previous mail (same BTF ID ptr can be passed), so we need to do something different.Quoted 5 lines
Quoted 1 line
Maybe that is what is confusing you.
I'm still confused.
Why doesmark_btf_ld_reg()need to copyref_obj_id? It should keep it as zero.So that we can find deref pointers obtained from the reg having that
ref_obj_idwhen it is released, and invalidate them. But since directly storing inref_obj_idof derefdst_regwill be bad (if we get same BTF ID from deref we could now pass it to release kfunc), we add a new member and match on that.Quoted 18 lines
mark_btf_ld_reg()is used in deref only. Theref_obj_idis assigned bycheck_helper_call(). r2 = r0; will copy it, but
r2 =r0->next; will keepr2->ref_obj_idas zero.Quoted 13 lines
Quoted 2 lines
mark_btf_ld_reg()shouldn't be copyingref_obj_id. If it keeps it as zero the problem will not happen, no?It is copying it but writing it to
parent_ref_obj_id. It keepsref_obj_idas 0 for all deref pointers.r1 =
acq(); // r1.ref =acquire_reference_state();
ref = N
r2 =r1->a; //mark_btf_ld_reg-> copy r1.(ref ?:parent_ref) -> so r2.parent_ref = r1.ref r3 =r2->b; //mark_btf_ld_reg-> copy r2.(ref ?:parent_ref) -> so r3.parent_ref = r2.parent_ref r4 =r3->c; //mark_btf_ld_reg-> copy r3.(ref ?:parent_ref) -> so r4.parent_ref = r3.parent_ref
rel(r1); // if (reg.ref == r1.ref || reg.parent_ref == r1.ref) invalidate(reg)Quoted 5 lines
Quoted 3 lines
As you see,
mark_btf_ld_regonly ever writes toparent_ref_obj_id, notref_obj_id. It just copiesref_obj_idwhen it is set, overparent_ref_obj_id, and only one of two can be set.I don't understand why such logic is needed.
Ok, let me try to explain once how I arrived at it. If you still don't like it, I'll drop it from the series.
So until this patch, when we do the following:
struct nf_conn *ct = bpf_xdp_ct_lookup(...); if (ct) { struct nf_conn *master = ct->master; bpf_ct_release(ct); unsigned long status = master->status; // I want to prevent this }
Quoted 3 lines
... this will work, which is ok (as in won't crash the kernel) since the load will be converted to
BPF_PROBE_MEM, but I want to disallow this case since it is obviously incorrect.
Finally we're talking! This motivation should have been in the commit log and this thread wouldn't have been that long.
Quoted 5 lines
The obvious solution (to me) was to kill all registers and stack slots for deref pointers.
My first naive solution was to simply copy
ref_obj_idonmark_btf_ld_reg, so that it can be matched and released fromrelease_reference.
That what I was guessing.
Quoted 9 lines
But then I noticed that if the BTF ID is same, there is no difference when it is passed to release kfunc compared to the original register it was loaded from.
struct nf_conn *ct = bpf_xdp_ct_lookup(...); if (ct) { struct nf_conn *master = ct->master; // copied ref_obj_id bpf_ct_release(master); // works, but shouldn't! }
Quoted 4 lines
So the code needed some way to distinguish this deref pointer that must be invalidated only when its 'parent' goes away. Hence the introduction of
parent_ref_obj_id, and the invariant that only one ofref_obj_idorparent_ref_obj_idmust be set.
The goal is clear now, but look at it differently:
struct nf_conn *ct = bpf_xdp_ct_lookup(...); if (ct) {
struct nf_conn *master = ct->master; struct net *net = ct->ct_net.net;
bpf_ct_release(ct);
master->status; // prevent this ?
net->ifindex; // but allow this ?
}
The verifier cannot statically check this. That's why all such deref are done via BPF_PROBE_MEM (which is the same as probe_read_kernel). We must disallow use after free when it can cause a crash. This case is not the one.
This one, though:
struct nf_conn *ct = bpf_xdp_ct_lookup(...); struct nf_conn *master = ct->master; bpf_ct_release(master);
definitely has to be prevented, since it will cause a crash.
As a follow up to this set would be great to allow ptr_to_btf_id pointers persist longer than program execution.
Users already asked to allow the following:
map_value = bpf_map_lookup_elem(...);
struct nf_conn *ct = bpf_xdp_ct_lookup(...); map_value->saved_ct = ct;
and some time later in a different or the same program:
map_value = bpf_map_lookup_elem(...); bpf_ct_release(map_value->saved_ct);
Currently folks work around this deficiency by storing some sort of id and doing extra lookups while performance is suffering. wdyt?
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 76 lines · Alexei Starovoitov
Quoted 75 lines · Kumar Kartikeya Dwivedi
Quoted 27 lines · Alexei Starovoitov
Quoted 25 lines · Kumar Kartikeya Dwivedi
Quoted 9 lines · Alexei Starovoitov
Quoted 7 lines · Kumar Kartikeya Dwivedi
It is, but into
parent_ref_obj_id, to match duringrelease_reference.Quoted 1 line
Shouldn't r2 get a different
ref_obj_idafter r2 =r1->next?It's
ref_obj_idis still 0.Quoted 8 lines
Quoted 4 lines
Thinking about this more, we actually only need 1 extra bit of information in
reg_state, not even a new member. We can simply copyref_obj_idand set this bit, then we can reject this register during release but consider it duringrelease_reference.It seems to me that this patch created the problem and it's trying to fix it at the same time.
Yes, sort of. Maybe I need to improve the commit message? I give an example below, and the first half of commit explains that if we simply did copy
ref_obj_id, it would lead to the case in the previous mail (same BTF ID ptr can be passed), so we need to do something different.Quoted 5 lines
Quoted 1 line
Maybe that is what is confusing you.
I'm still confused.
Why doesmark_btf_ld_reg()need to copyref_obj_id? It should keep it as zero.So that we can find deref pointers obtained from the reg having that
ref_obj_idwhen it is released, and invalidate them. But since directly storing inref_obj_idof derefdst_regwill be bad (if we get same BTF ID from deref we could now pass it to release kfunc), we add a new member and match on that.Quoted 18 lines
mark_btf_ld_reg()is used in deref only. Theref_obj_idis assigned bycheck_helper_call(). r2 = r0; will copy it, but
r2 =r0->next; will keepr2->ref_obj_idas zero.Quoted 13 lines
Quoted 2 lines
mark_btf_ld_reg()shouldn't be copyingref_obj_id. If it keeps it as zero the problem will not happen, no?It is copying it but writing it to
parent_ref_obj_id. It keepsref_obj_idas 0 for all deref pointers.r1 =
acq(); // r1.ref =acquire_reference_state();
ref = N
r2 =r1->a; //mark_btf_ld_reg-> copy r1.(ref ?:parent_ref) -> so r2.parent_ref = r1.ref r3 =r2->b; //mark_btf_ld_reg-> copy r2.(ref ?:parent_ref) -> so r3.parent_ref = r2.parent_ref r4 =r3->c; //mark_btf_ld_reg-> copy r3.(ref ?:parent_ref) -> so r4.parent_ref = r3.parent_ref
rel(r1); // if (reg.ref == r1.ref || reg.parent_ref == r1.ref) invalidate(reg)Quoted 5 lines
Quoted 3 lines
As you see,
mark_btf_ld_regonly ever writes toparent_ref_obj_id, notref_obj_id. It just copiesref_obj_idwhen it is set, overparent_ref_obj_id, and only one of two can be set.I don't understand why such logic is needed.
Ok, let me try to explain once how I arrived at it. If you still don't like it, I'll drop it from the series.
So until this patch, when we do the following:
struct nf_conn *ct = bpf_xdp_ct_lookup(...); if (ct) { struct nf_conn *master = ct->master; bpf_ct_release(ct); unsigned long status = master->status; // I want to prevent this }
Quoted 7 lines
Quoted 3 lines
... this will work, which is ok (as in won't crash the kernel) since the load will be converted to
BPF_PROBE_MEM, but I want to disallow this case since it is obviously incorrect.Finally we're talking! This motivation should have been in the commit log and this thread wouldn't have been that long.
Indeed, sorry for wasting your time. I'll try to do better, and thanks for being patient.
Quoted 28 lines
Quoted 5 lines
The obvious solution (to me) was to kill all registers and stack slots for deref pointers.
My first naive solution was to simply copy
ref_obj_idonmark_btf_ld_reg, so that it can be matched and released fromrelease_reference.That what I was guessing.
Quoted 9 lines
But then I noticed that if the BTF ID is same, there is no difference when it is passed to release kfunc compared to the original register it was loaded from.
struct nf_conn *ct = bpf_xdp_ct_lookup(...); if (ct) { struct nf_conn *master = ct->master; // copied ref_obj_id bpf_ct_release(master); // works, but shouldn't! }Quoted 4 lines
So the code needed some way to distinguish this deref pointer that must be invalidated only when its 'parent' goes away. Hence the introduction of
parent_ref_obj_id, and the invariant that only one ofref_obj_idorparent_ref_obj_idmust be set.The goal is clear now, but look at it differently:
structnf_conn*ct =bpf_xdp_ct_lookup(...); if (ct) {
structnf_conn*master =ct->master; struct net *net =ct->ct_net.net;
Quoted 3 lines
bpf_ct_release(ct);
master->status; // prevent this ?
net->ifindex; // but allow this ?
I think both will be prevented with the current logic, no?
net will be ct + offset, so if mark_btf_ld_reg writes PTR_TO_BTF_ID to dst_reg for net, it will copy ct's reg's ref_obj_id to parent_ref_obj_id of dst_reg (net). Then on release of ct, net's reg gets killed too since reg[ct]->ref_obj_id matches its parent_ref_obj_id.
I just tried it out:
7: (85) call bpf_skb_ct_lookup#121995last_idx 7 first_idx 0
regs=8 stack=0 before 6: (b4) w5 = 4
regs=8 stack=0 before 5: (bf) r4 = r2
regs=8 stack=0 before 4: (b4) w3 = 4last_idx 7 first_idx 0
regs=20 stack=0 before 6: (b4) w5 = 4
; if (ct) {
8: (15) if r0 == 0x0 goto pc+4
R0_w=ptr_nf_conn(id=0,ref_obj_id=2,off=0,imm=0) R6_w=invP0 R10=fp0 fp-8=mmmm???? refs=2
; struct net *net = ct->ct_net.net;
9: (79) r6 = *(u64 *)(r0 +208)
; bpf_ct_release(ct);
10: (bf) r1 = r0
11: (85) call bpf_ct_release#121993
; return net->ifindex; // but allow this ?
12: (61) r6 = *(u32 *)(r6 +80)
R6 invalid mem access 'inv'
processed 13 insns (limit 1000000) max_states_per_insn 0 total_states 1 peak_states 1 mark_read 1 -- END PROG LOAD LOG --
Same result when you follow pointer chains.
Quoted 5 lines
}
The verifier cannot statically check this. That's why all such deref are done viaBPF_PROBE_MEM(which is the same asprobe_read_kernel). We must disallow use after free when it can cause a crash. This case is not the one.
That is a valid point, this is certainly in 'nice to have/prevents obvious misuse' territory, but if this can be done without introducing too much complexity, I'd like us to do it.
A bit of a digression, but:
I'm afraid this patch is going to be brought up again for a future effort related to XDP queueing that Toke is working on. We have a similar scenario there, when xdp_md (aliasing xdp_frame) is dequeued from the PIFO map, and PTR_TO_PACKET is obtained by reading xdp_md->data. The xdp_md is referenced, so we need to invalidate these pkt pointers as well, in addition to killing xdp_md copies. Also this parent_ref_obj_id state allows us to reject comparisons between pkt pointers pointing into different xdp_md's (when you dequeue more than one at once and form multiple pkt pointers pointing into different xdp_mds).
Then it would be required for correctness purposes, but we'll discuss that once it is posted (but wanted to let you know as an FYI). I can probably also consider your map_uid suggestion for that, but IMO parent_ref_obj_id is more generic, and requires less special cases in verifier.
Quoted 17 lines
This one, though:
structnf_conn*ct =bpf_xdp_ct_lookup(...); structnf_conn*master =ct->master;bpf_ct_release(master);
definitely has to be prevented, since it will cause a crash.As a follow up to this set would be great to allow
ptr_to_btf_idpointers persist longer than program execution.
Users already asked to allow the following:
map_value=bpf_map_lookup_elem(...);
structnf_conn*ct =bpf_xdp_ct_lookup(...);map_value->saved_ct= ct;
and some time later in a different or the same program:
map_value=bpf_map_lookup_elem(...);bpf_ct_release(map_value->saved_ct);
Quoted 3 lines
Currently folks work around this deficiency by storing some sort of id and doing extra lookups while performance is suffering. wdyt?
Very interesting idea! I'm guessing we'll need something akin to bpf_timer support, i.e. a dedicated type verified using BTF which can be embedded in map_value? I'll be happy to work on enabling this.
One thought though (just confirming):
If user does map_value->saved_ct = ct, we have to ignore reference leak check for ct's ref_id, but if they rewrite saved_ct, we would also have to unignore it, correct?
I think we can make this tracking easier by limiting to one bpf_ptr_to_btf struct in map_value, then it can simply be part of ptr_to_map_value's reg_state.
--
Kartikeya
Re: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 11 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines
The goal is clear now, but look at it differently:
structnf_conn*ct =bpf_xdp_ct_lookup(...); if (ct) {
structnf_conn*master =ct->master; struct net *net =ct->ct_net.net;
bpf_ct_release(ct);
master->status; // prevent this ?
net->ifindex; // but allow this ?
Quoted 5 lines
I think both will be prevented with the current logic, no?
net will be ct + offset, so ifmark_btf_ld_regwritesPTR_TO_BTF_IDtodst_regfor net, it will copy ct's reg'sref_obj_idtoparent_ref_obj_idofdst_reg(net). Then on release of ct, net's reg gets killed too since reg[ct]->ref_obj_id matches itsparent_ref_obj_id.
Excatly, but it should be allowed.
There is nothing wrong with 'net' access after ct_release.
Quoted 10 lines
Quoted 5 lines
}
The verifier cannot statically check this. That's why all such deref are done viaBPF_PROBE_MEM(which is the same asprobe_read_kernel). We must disallow use after free when it can cause a crash. This case is not the one.That is a valid point, this is certainly in 'nice to have/prevents obvious misuse' territory, but if this can be done without introducing too much complexity, I'd like us to do it.
Quoted 10 lines
A bit of a digression, but:
I'm afraid this patch is going to be brought up again for a future effort related to XDP queueing that Toke is working on. We have a similar scenario there, whenxdp_md(aliasingxdp_frame) is dequeued from the PIFO map, andPTR_TO_PACKETis obtained by readingxdp_md->data. Thexdp_mdis referenced, so we need to invalidate these pkt pointers as well, in addition to killingxdp_mdcopies. Also thisparent_ref_obj_idstate allows us to reject comparisons between pkt pointers pointing into different xdp_md's (when you dequeue more than one at once and form multiple pkt pointers pointing into differentxdp_mds).
I cannot quite grasp the issue. Sounds orthogonal. The pkt pointers are not ptr_to_btf_id like. There is no PROBE_MEM there.
Quoted 15 lines
Quoted 15 lines
struct
nf_conn*ct =bpf_xdp_ct_lookup(...); structnf_conn*master =ct->master;bpf_ct_release(master);
definitely has to be prevented, since it will cause a crash.As a follow up to this set would be great to allow
ptr_to_btf_idpointers persist longer than program execution.
Users already asked to allow the following:
map_value=bpf_map_lookup_elem(...);
structnf_conn*ct =bpf_xdp_ct_lookup(...);map_value->saved_ct= ct;
and some time later in a different or the same program:
map_value=bpf_map_lookup_elem(...);bpf_ct_release(map_value->saved_ct);
Quoted 7 lines
Quoted 3 lines
Currently folks work around this deficiency by storing some sort of id and doing extra lookups while performance is suffering. wdyt?
Very interesting idea! I'm guessing we'll need something akin to
bpf_timersupport, i.e. a dedicated type verified using BTF which can be embedded inmap_value? I'll be happy to work on enabling this.
Thanks! Would be awesome.
Quoted 4 lines
One thought though (just confirming):
If user doesmap_value->saved_ct= ct, we have to ignore reference leak check for ct'sref_id, but if they rewritesaved_ct, we would also have to unignore it, correct?
We cannot just ignore it :)
I was thinking to borrow std::unique_ptr like semanitcs.
struct nf_conn *ct = bpf_xdp_ct_lookup(...); // here ref checking logic tracks it as normal map_value->saved_ct = ct; // here it trasnfers the ref from Rx into map_value ct->status; // cannot be access here.
It could look unnatural to typical C programmer, so we might need
explicit std::move-like helper, so the assignment will be:bpf_move_ptr(&map_value->saved_ct, &ct); // same as map_value->saved_ct = ct; ct = NULL;
...bpf_move_ptr(&ct, &map_value->saved_ct); // would take the ownership back from the map // and the ref checking logic tracks 'ct' again as normal
Quoted 2 lines
I think we can make this tracking easier by limiting to one
bpf_ptr_to_btfstruct inmap_value, then it can simply be part of ptr_to_map_value'sreg_state.
Possible. Hopefully such limitiation will not be needed.
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 06/10] bpf: Track provenance for pointers formed from referenced PTR_TO_BTF_ID
Quoted 12 lines · Alexei Starovoitov
Quoted 11 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines
The goal is clear now, but look at it differently:
structnf_conn*ct =bpf_xdp_ct_lookup(...); if (ct) {
structnf_conn*master =ct->master; struct net *net =ct->ct_net.net;
bpf_ct_release(ct);
master->status; // prevent this ?
net->ifindex; // but allow this ?
Quoted 9 lines
Quoted 5 lines
I think both will be prevented with the current logic, no?
net will be ct + offset, so ifmark_btf_ld_regwritesPTR_TO_BTF_IDtodst_regfor net, it will copy ct's reg'sref_obj_idtoparent_ref_obj_idofdst_reg(net). Then on release of ct, net's reg gets killed too since reg[ct]->ref_obj_id matches itsparent_ref_obj_id.Excatly, but it should be allowed.
There is nothing wrong with 'net' access afterct_release.
Ok, I see your point. I'll just drop this patch in v5, and we'll revisit the other pkt pointer thing when the patch is posted.
Quoted 19 lines
[...]
Quoted 3 lines
Very interesting idea! I'm guessing we'll need something akin to
bpf_timersupport, i.e. a dedicated type verified using BTF which can be embedded inmap_value? I'll be happy to work on enabling this.Thanks! Would be awesome.
Quoted 4 lines
One thought though (just confirming):
If user doesmap_value->saved_ct= ct, we have to ignore reference leak check for ct'sref_id, but if they rewritesaved_ct, we would also have to unignore it, correct?We cannot just ignore it :)
I was thinking to borrowstd::unique_ptrlike semanitcs.struct
nf_conn*ct =bpf_xdp_ct_lookup(...); // here ref checking logic tracks it as normalmap_value->saved_ct= ct; // here it trasnfers the ref from Rx intomap_valuect->status; // cannot be access here.
Quoted 7 lines
It could look unnatural to typical C programmer, so we might need
explicit std::move-like helper, so the assignment will be:bpf_move_ptr(&map_value->saved_ct, &ct); // same asmap_value->saved_ct= ct; ct = NULL;
...bpf_move_ptr(&ct, &map_value->saved_ct); // would take the ownership back from the map // and the ref checking logic tracks 'ct' again as normal
Agreed, normal assignment syntax having those side effects is indeed awkward.
Quoted 4 lines
Quoted 2 lines
I think we can make this tracking easier by limiting to one
bpf_ptr_to_btfstruct inmap_value, then it can simply be part of ptr_to_map_value'sreg_state.Possible. Hopefully such limitiation will not be needed.
Thanks for your review and feedback, Alexei! I'll address all points.
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 7/10 net/netfilter: Add unstable CT lookup helpers for XDP and TC-BPF
In reply to Kumar Kartikeya DwivediThis change adds conntrack lookup helpers using the unstable kfunc call interface for the XDP and TC-BPF hooks. The primary usecase is implementing a synproxy in XDP, see Maxim's patchset at [0].
Export get_net_ns_by_id as nf_conntrack needs to call it.
Note that we search for acquire, release, and null returning kfuncs in the intersection of those sets and main set.
This implies that the kfunc_btf_id_list acq_set, rel_set, null_set may contain BTF ID not in main set, this is explicitly allowed and recommended (to save on definining more and more sets), since check_kfunc_call verifier operation would filter out the invalid BTF ID fairly early, so later checks for acquire, release, and ret_type_null kfunc will only consider allowed BTF IDs for that program that are allowed in main set. This is why the nf_conntrack_acq_ids set has BTF IDs for both xdp and tc hook kfuncs.
[0]: https://lore.kernel.org/bpf/[email protected]
--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 1 + net/core/filter.c | 24 +++ net/core/net_namespace.c | 1 + net/netfilter/nf_conntrack_core.c | 278 ++++++++++++++++++++++++++++++ 5 files changed, 306 insertions(+)
include/linux/btf.h +2 -0
@@ -363,6 +363,7 @@ bool bpf_is_mod_kfunc_ret_type_null(struct kfunc_btf_id_list *klist,extern struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list;extern struct kfunc_btf_id_list prog_test_kfunc_list; +extern struct kfunc_btf_id_list xdp_kfunc_list;#else static inline void register_kfunc_btf_id_set (struct kfunc_btf_id_list *l,struct kfunc_btf_id_set *s)
@@ -396,6 +397,7 @@ bpf_is_mod_kfunc_ret_type_null(struct kfunc_btf_id_list *klist, u32 kfunc_id,static struct kfunc_btf_id_list bpf_tcp_ca_kfunc_list__maybe_unused ;static struct kfunc_btf_id_list prog_test_kfunc_list__maybe_unused ; +static struct kfunc_btf_id_list xdp_kfunc_list__maybe_unused ;#endif #endif
kernel/bpf/btf.c +1 -0
@@ -6558,6 +6558,7 @@ bool bpf_is_mod_kfunc_ret_type_null(struct kfunc_btf_id_list *klist,DEFINE_KFUNC_BTF_ID_LIST (bpf_tcp_ca_kfunc_list);DEFINE_KFUNC_BTF_ID_LIST (prog_test_kfunc_list); +DEFINE_KFUNC_BTF_ID_LIST (xdp_kfunc_list);#endif
net/core/filter.c +24 -0
@@ -10008,11 +10008,35 @@ const struct bpf_prog_ops tc_cls_act_prog_ops = { .test_run = bpf_prog_test_run_skb, }; +static bool xdp_check_kfunc_call (u32 kfunc_id,struct module *owner) +{ +return bpf_check_mod_kfunc_call (&xdp_kfunc_list, kfunc_id, owner); +} + +static bool xdp_is_acquire_kfunc (u32 kfunc_id,struct module *owner) +{ +return bpf_is_mod_acquire_kfunc (&xdp_kfunc_list, kfunc_id, owner); +} + +static bool xdp_is_release_kfunc (u32 kfunc_id,struct module *owner) +{ +return bpf_is_mod_release_kfunc (&xdp_kfunc_list, kfunc_id, owner); +} + +static bool xdp_is_kfunc_ret_type_null (u32 kfunc_id,struct module *owner) +{ +return bpf_is_mod_kfunc_ret_type_null (&xdp_kfunc_list, kfunc_id, owner); +} +const struct bpf_verifier_ops xdp_verifier_ops = { .get_func_proto = xdp_func_proto, .is_valid_access = xdp_is_valid_access, .convert_ctx_access = xdp_convert_ctx_access, .gen_prologue = bpf_noop_prologue, + .check_kfunc_call = xdp_check_kfunc_call, + .is_acquire_kfunc = xdp_is_acquire_kfunc, + .is_release_kfunc = xdp_is_release_kfunc, + .is_kfunc_ret_type_null = xdp_is_kfunc_ret_type_null, };const struct bpf_prog_ops xdp_prog_ops = {
net/core/net_namespace.c +1 -0
@@ -299,6 +299,7 @@ struct net *get_net_ns_by_id(const struct net *net, int id)return peer; } +EXPORT_SYMBOL_GPL (get_net_ns_by_id);/* * setup_net runs the initializers for the network namespace object.
net/netfilter/nf_conntrack_core.c +278 -0
298 lines not on this page · raw message
Signature · 1 line
2.34.1
Pablo Neira Ayuso
maintainerRe: [PATCH bpf-next v4 07/10] net/netfilter: Add unstable CT lookup helpers for XDP and TC-BPF
Quoted 20 lines · Kumar Kartikeya Dwivedi
This change adds conntrack lookup helpers using the unstable kfunc call interface for the XDP and TC-BPF hooks. The primary usecase is implementing a synproxy in XDP, see Maxim's patchset at [0].
Export
get_net_ns_by_idasnf_conntrackneeds to call it.Note that we search for acquire, release, and null returning kfuncs in the intersection of those sets and main set.
This implies that the
kfunc_btf_id_listacq_set,rel_set,null_setmay contain BTF ID not in main set, this is explicitly allowed and recommended (to save on definining more and more sets), sincecheck_kfunc_callverifier operation would filter out the invalid BTF ID fairly early, so later checks for acquire, release, andret_type_nullkfunc will only consider allowed BTF IDs for that program that are allowed in main set. This is why thenf_conntrack_acq_idsset has BTF IDs for both xdp and tc hook kfuncs.
Quoted 7 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 1 + net/core/filter.c | 24 +++ net/core/net_namespace.c | 1 + net/netfilter/nf_conntrack_core.c | 278 ++++++++++++++++++++++++++++++
Toke proposed to move it to net/netfilter/nf_conntrack_bpf.c
Kumar Kartikeya Dwivedi
maintainerRe: [PATCH bpf-next v4 07/10] net/netfilter: Add unstable CT lookup helpers for XDP and TC-BPF
Quoted 29 lines · Pablo Neira Ayuso
Quoted 20 lines · Kumar Kartikeya Dwivedi
This change adds conntrack lookup helpers using the unstable kfunc call interface for the XDP and TC-BPF hooks. The primary usecase is implementing a synproxy in XDP, see Maxim's patchset at [0].
Export
get_net_ns_by_idasnf_conntrackneeds to call it.Note that we search for acquire, release, and null returning kfuncs in the intersection of those sets and main set.
This implies that the
kfunc_btf_id_listacq_set,rel_set,null_setmay contain BTF ID not in main set, this is explicitly allowed and recommended (to save on definining more and more sets), sincecheck_kfunc_callverifier operation would filter out the invalid BTF ID fairly early, so later checks for acquire, release, andret_type_nullkfunc will only consider allowed BTF IDs for that program that are allowed in main set. This is why thenf_conntrack_acq_idsset has BTF IDs for both xdp and tc hook kfuncs.Quoted 7 lines
Signed-off-by Kumar Kartikeya Dwivedi <[email protected]>--- include/linux/btf.h | 2 + kernel/bpf/btf.c | 1 + net/core/filter.c | 24 +++ net/core/net_namespace.c | 1 + net/netfilter/nf_conntrack_core.c | 278 ++++++++++++++++++++++++++++++
Quoted 1 line
Toke proposed to move it to
net/netfilter/nf_conntrack_bpf.c
Ugh, sorry. I think I completely missed that mail, but I see it now.
I'll wait for this review cycle to conclude, and then put the code in its own file in the next version.
Thanks.
--
Kartikeya
Kumar Kartikeya Dwivedi
maintainerPATCH v4 8/10 selftests/bpf: Add test for unstable CT lookup API
In reply to Kumar Kartikeya DwivediThis tests that we return errors as documented, and also that the kfunc calls work from both XDP and TC hooks.
--- tools/testing/selftests/bpf/config | 4 + .../testing/selftests/bpf/prog_tests/bpf_nf.c | 48 ++++++++ .../testing/selftests/bpf/progs/test_bpf_nf.c | 105 ++++++++++++++++++ 3 files changed, 157 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bpf_nf.c create mode 100644 tools/testing/selftests/bpf/progs/test_bpf_nf.c
tools/testing/selftests/bpf/config +4 -0
@@ -46,3 +46,7 @@ CONFIG_IMA_READ_POLICY=y CONFIG_BLK_DEV_LOOP=y CONFIG_FUNCTION_TRACER=y CONFIG_DYNAMIC_FTRACE=y +CONFIG_NETFILTER=y +CONFIG_NF_DEFRAG_IPV4=y +CONFIG_NF_DEFRAG_IPV6=y +CONFIG_NF_CONNTRACK=y
tools/testing/selftests/bpf/prog_tests/bpf_nf.c new file +48 -0
48 lines not on this page · raw message
tools/testing/selftests/bpf/progs/test_bpf_nf.c new file +105 -0
105 lines not on this page · raw message
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH v4 9/10 selftests/bpf: Add test_verifier support to fixup kfunc call insns
In reply to Kumar Kartikeya DwivediThis allows us to add tests (esp. negative tests) where we only want to ensure the program doesn't pass through the verifier, and also verify the error. The next commit will add the tests making use of this.
--- tools/testing/selftests/bpf/test_verifier.c | 28 +++++++++++++++++++++ 1 file changed, 28 insertions(+)
tools/testing/selftests/bpf/test_verifier.c +28 -0
@@ -31,6 +31,7 @@#include <linux/if_ether.h> #include <linux/btf.h> +#include <bpf/btf.h> #include <bpf/bpf.h> #include <bpf/libbpf.h>
@@ -66,6 +67,11 @@ static bool unpriv_disabled = false;static int skips;static bool verbose = false; +struct kfunc_btf_id_pair { +const char *kfunc; +int insn_idx; +}; +struct bpf_test {const char *descr;struct bpf_insn insns[MAX_INSNS ];
@@ -92,6 +98,7 @@ struct bpf_test {int fixup_map_reuseport_array[MAX_FIXUPS ];int fixup_map_ringbuf[MAX_FIXUPS ];int fixup_map_timer[MAX_FIXUPS ]; +struct kfunc_btf_id_pair fixup_kfunc_btf_id[MAX_FIXUPS ];/* Expected verifier log output for result REJECT or VERBOSE_ACCEPT. * Can be a tab-separated sequence of expected strings. An empty string * means no log verification.
@@ -744,6 +751,7 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type,int *fixup_map_reuseport_array = test->fixup_map_reuseport_array;int *fixup_map_ringbuf = test->fixup_map_ringbuf;int *fixup_map_timer = test->fixup_map_timer; +struct kfunc_btf_id_pair *fixup_kfunc_btf_id = test->fixup_kfunc_btf_id;if (test->fill_helper) { test->fill_insns =calloc (MAX_TEST_INSNS ,sizeof (struct bpf_insn ));
@@ -936,6 +944,26 @@ static void do_test_fixup(struct bpf_test *test, enum bpf_prog_type prog_type, fixup_map_timer++; }while (*fixup_map_timer); } + +/* Patch in kfunc BTF IDs */ +if (fixup_kfunc_btf_id->kfunc) { +struct btf *btf; +int btf_id; + +do { + btf_id =0 ; + btf =btf__load_vmlinux_btf (); +if (btf) { + btf_id =btf__find_by_name_kind (btf, + fixup_kfunc_btf_id->kfunc, +BTF_KIND_FUNC ); + btf_id = btf_id <0 ?0 : btf_id; + } +btf__free (btf); + prog[fixup_kfunc_btf_id->insn_idx].imm = btf_id; + fixup_kfunc_btf_id++; + }while (fixup_kfunc_btf_id->kfunc); + } }struct libcap {
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerPATCH v4 10/10 selftests/bpf: Extend kfunc selftests
In reply to Kumar Kartikeya DwivediUse the prog_test kfuncs to test the referenced PTR_TO_BTF_ID kfunc support, and PTR_TO_CTX, PTR_TO_MEM argument passing support. Also testing the various failure cases for invalid kfunc prototypes.
Also, exercise the two cases being solved by addition of parent_ref_obj_id in bpf_reg_state:
- Invalidation of pointers formed by btf_struct_walk on release_reference
- Not being able to pass same BTF ID's PTR_TO_BTF_ID to the release function, also formed by btf_struct_walk
--- include/linux/bpf.h | 21 +++ net/bpf/test_run.c | 135 ++++++++++++++++++ net/core/filter.c | 3 + .../selftests/bpf/prog_tests/kfunc_call.c | 6 + .../selftests/bpf/progs/kfunc_call_test.c | 52 ++++++- tools/testing/selftests/bpf/verifier/calls.c | 75 ++++++++++ .../selftests/bpf/verifier/ref_tracking.c | 44 ++++++ 7 files changed, 334 insertions(+), 2 deletions(-)
include/linux/bpf.h +21 -0
@@ -1670,6 +1670,9 @@ int bpf_prog_test_run_sk_lookup(struct bpf_prog *prog,const union bpf_attr *kattr, union bpf_attr __user *uattr);bool bpf_prog_test_check_kfunc_call (u32 kfunc_id,struct module *owner); +bool bpf_prog_test_is_acquire_kfunc (u32 kfunc_id,struct module *owner); +bool bpf_prog_test_is_release_kfunc (u32 kfunc_id,struct module *owner); +bool bpf_prog_test_is_kfunc_ret_type_null (u32 kfunc_id,struct module *owner);bool btf_ctx_access (int off,int size,enum bpf_access_type type,const struct bpf_prog *prog,struct bpf_insn_access_aux *info);
@@ -1928,6 +1931,24 @@ static inline bool bpf_prog_test_check_kfunc_call(u32 kfunc_id,return false; } +static inline bool bpf_prog_test_is_acquire_kfunc (u32 kfunc_id, +struct module *owner) +{ +return false; +} + +static inline bool bpf_prog_test_is_release_kfunc (u32 kfunc_id, +struct module *owner) +{ +return false; +} + +static inline bool bpf_prog_test_is_kfunc_ret_type_null (u32 kfunc_id, +struct module *owner) +{ +return false; +} +static inline void bpf_map_put (struct bpf_map *map) { }
net/bpf/test_run.c +135 -0
155 lines not on this page · raw message
net/core/filter.c +3 -0
@@ -10002,6 +10002,9 @@ const struct bpf_verifier_ops tc_cls_act_verifier_ops = { .gen_prologue = tc_cls_act_prologue, .gen_ld_abs = bpf_gen_ld_abs, .check_kfunc_call = bpf_prog_test_check_kfunc_call, + .is_acquire_kfunc = bpf_prog_test_is_acquire_kfunc, + .is_release_kfunc = bpf_prog_test_is_release_kfunc, + .is_kfunc_ret_type_null = bpf_prog_test_is_kfunc_ret_type_null, };const struct bpf_prog_ops tc_cls_act_prog_ops = {
tools/testing/selftests/bpf/prog_tests/kfunc_call.c +6 -0
@@ -27,6 +27,12 @@ static void test_main(void)ASSERT_OK (err,"bpf_prog_test_run(test2)" );ASSERT_EQ (retval,3 ,"test2-retval" ); + prog_fd = skel->progs.kfunc_call_test_ref_btf_id.prog_fd; + err =bpf_prog_test_run (prog_fd,1 , &pkt_v4,sizeof (pkt_v4), +NULL ,NULL , (__u32 *)&retval,NULL ); +ASSERT_OK (err,"bpf_prog_test_run(test_ref_btf_id)" ); +ASSERT_EQ (retval,0 ,"test_ref_btf_id-retval" ); +kfunc_call_test_lskel__destroy (skel); }
tools/testing/selftests/bpf/progs/kfunc_call_test.c +50 -2
67 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/calls.c +75 -0
81 lines not on this page · raw message
tools/testing/selftests/bpf/verifier/ref_tracking.c +44 -0
47 lines not on this page · raw message
Signature · 1 line
2.34.1
Kumar Kartikeya Dwivedi
maintainerQuoted 18 lines · Kumar Kartikeya Dwivedi
This series adds unstable conntrack lookup helpers using BPF kfunc support. The patch adding the lookup helper is based off of Maxim's recent patch to aid in rebasing their series on top of this, all adjusted to work with module kfuncs [0].
[0]: https://lore.kernel.org/bpf/[email protected]
To enable returning a reference to struct
nf_conn, the verifier is extended to support reference tracking forPTR_TO_BTF_ID, and kfunc is extended with support for working as acquire/release functions, similar to existing BPF helpers. kfunc returning pointer (limited toPTR_TO_BTF_IDin the kernel) can also return aPTR_TO_BTF_ID_OR_NULLnow, typically needed when acquiring a resource can fail. kfunc can also receivePTR_TO_CTXandPTR_TO_MEM(with some limitations) as arguments now. There is also support for passing a mem, len pair as argument to kfunc now. In such cases, passing pointer to unsized type (void) is also permitted.Please see individual commits for details.
Quoted 8 lines
Note: BPF CI needs to add the following to config to test the set. I did update the selftests config in patch 8, but not sure if that is enough.
CONFIG_NETFILTER=y
CONFIG_NF_DEFRAG_IPV4=y
CONFIG_NF_DEFRAG_IPV6=y
CONFIG_NF_CONNTRACK=y
Hm, so this is not showing up in BPF CI, is it some mistake from my side? The last couple of versions produced build time warnings in Patchwork, that I fixed, which I suspected was the main cause.
There's still one coming from the last patch, but based on [0], I am not sure whether I should be doing things any differently (and if I do fix it, it also needs to be done for the functions added before). The warnings are from the 11 new kfuncs I added in net/bpf/test_run.c, for their missing declarations.
Comments?
[0]: https://lore.kernel.org/bpf/20200326235426.ei6ae2z5ek6uq3tt@ast-mbp
Quoted 1 line
[...]
--
Kartikeya
Quoted 29 lines · Kumar Kartikeya Dwivedi
Quoted 18 lines · Kumar Kartikeya Dwivedi
This series adds unstable conntrack lookup helpers using BPF kfunc support. The patch adding the lookup helper is based off of Maxim's recent patch to aid in rebasing their series on top of this, all adjusted to work with module kfuncs [0].
[0]: https://lore.kernel.org/bpf/[email protected]
To enable returning a reference to struct
nf_conn, the verifier is extended to support reference tracking forPTR_TO_BTF_ID, and kfunc is extended with support for working as acquire/release functions, similar to existing BPF helpers. kfunc returning pointer (limited toPTR_TO_BTF_IDin the kernel) can also return aPTR_TO_BTF_ID_OR_NULLnow, typically needed when acquiring a resource can fail. kfunc can also receivePTR_TO_CTXandPTR_TO_MEM(with some limitations) as arguments now. There is also support for passing a mem, len pair as argument to kfunc now. In such cases, passing pointer to unsized type (void) is also permitted.Please see individual commits for details.
Quoted 8 lines
Note: BPF CI needs to add the following to config to test the set. I did update the selftests config in patch 8, but not sure if that is enough.
CONFIG_NETFILTER=y
CONFIG_NF_DEFRAG_IPV4=y
CONFIG_NF_DEFRAG_IPV6=y
CONFIG_NF_CONNTRACK=y
Quoted 3 lines
Hm, so this is not showing up in BPF CI, is it some mistake from my side? The last couple of versions produced build time warnings in Patchwork, that I fixed, which I suspected was the main cause.
Not a mistake, for BPF CI there are separate configs that need to be
updated manually:
- https://github.com/kernel-patches/vmtest/blob/master/.github/actions/vmtest/latest.config
for kernel patches CI
- https://github.com/libbpf/libbpf/tree/master/travis-ci/vmtest/configs
(there is x86-64 and s390x configs) for libbpf CI
Quoted 14 lines
There's still one coming from the last patch, but based on [0], I am not sure whether I should be doing things any differently (and if I do fix it, it also needs to be done for the functions added before). The warnings are from the 11 new kfuncs I added in
net/bpf/test_run.c, for their missing declarations.Comments?
[0]: https://lore.kernel.org/bpf/20200326235426.ei6ae2z5ek6uq3tt@ast-mbp
Quoted 1 line
[...]
--
Kartikeya