Support direct writes to nf_conn:mark
Subsystems (8)
v5 cover Support direct writes to nf_conn:mark
Support direct writes to nf_conn:mark from TC and XDP prog types. This is useful when applications want to store per-connection metadata. This is also particularly useful for applications that run both bpf and iptables/nftables because the latter can trivially access this metadata.
One example use case would be if a bpf prog is responsible for advanced packet classification and iptables/nftables is later used for routing due to pre-existing/legacy code.
Past discussion:
- v4: https://lore.kernel.org/bpf/[email protected]/
- v3: https://lore.kernel.org/bpf/[email protected]/
- v2: https://lore.kernel.org/bpf/CAP01T74Sgn354dXGiFWFryu4vg+o8b9s9La1d9zEbC4LGvH4qg@mail.gmail.com/T/
- v1: https://lore.kernel.org/bpf/[email protected]/
Changes since v4:
- Use exported function pointer + mutex to handle CONFIG_NF_CONNTRACK=m case
Changes since v3:
- Use a mutex to protect module load/unload critical section
Changes since v2:
- Remove use of NOT_INIT for btf_struct_access write path
- Disallow nf_conn writing when nf_conntrack module not loaded
- Support writing to nf_conn___init:mark
Changes since v1:
- Add unimplemented stub for when !CONFIG_BPF_SYSCALL
Daniel Xu (6):
bpf: Remove duplicate PTR_TO_BTF_ID RO check
bpf: Add stub for btf_struct_access()
bpf: Use 0 instead of NOT_INIT for btf_struct_access() writes
bpf: Export btf_type_by_id() and bpf_log()
bpf: Add support for writing to nf_conn:mark selftests/bpf: Add tests for writing to nf_conn:mark
include/linux/bpf.h | 9 +++ include/net/netfilter/nf_conntrack_bpf.h | 23 +++++++ kernel/bpf/btf.c | 1 + kernel/bpf/verifier.c | 4 +- net/core/filter.c | 54 +++++++++++++++ net/ipv4/bpf_tcp_ca.c | 2 +- net/netfilter/nf_conntrack_bpf.c | 66 ++++++++++++++++++- net/netfilter/nf_conntrack_core.c | 1 + .../testing/selftests/bpf/prog_tests/bpf_nf.c | 2 + .../testing/selftests/bpf/progs/test_bpf_nf.c | 9 ++- .../selftests/bpf/progs/test_bpf_nf_fail.c | 14 ++++ 11 files changed, 178 insertions(+), 7 deletions(-)
Signature · 1 line
2.37.1
PATCH v5 1/6 bpf: Remove duplicate PTR_TO_BTF_ID RO check
Since commit 27ae7997a661 ("bpf: Introduce BPF_PROG_TYPE_STRUCT_OPS") there has existed bpf_verifier_ops:btf_struct_access. When btf_struct_access is _unset_ for a prog type, the verifier runs the default implementation, which is to enforce read only:
if (env->ops->btf_struct_access) {
[...]
} else {
if (atype != BPF_READ) {
verbose(env, "only read is supported\n");
return -EACCES;
}
[...]
}When btf_struct_access is _set_, the expectation is that btf_struct_access has full control over accesses, including if writes are allowed.
Rather than carve out an exception for each prog type that may write to BTF ptrs, delete the redundant check and give full control to btf_struct_access.
--- kernel/bpf/verifier.c | 3 --- 1 file changed, 3 deletions(-)
kernel/bpf/verifier.c +0 -3
@@ -13447,9 +13447,6 @@ static int convert_ctx_accesses(struct bpf_verifier_env *env) insn->code =BPF_LDX |BPF_PROBE_MEM |BPF_SIZE ((insn)->code); env->prog->aux->num_exentries++; - }else if (resolve_prog_type (env->prog) !=BPF_PROG_TYPE_STRUCT_OPS ) { -verbose (env,"Writes through BTF pointers are not allowed\n" ); -return -EINVAL ; }continue ;default :
Signature · 1 line
2.37.1
PATCH v5 2/6 bpf: Add stub for btf_struct_access()
In reply to Daniel XuAdd corresponding unimplemented stub for when CONFIG_BPF_SYSCALL=n
--- include/linux/bpf.h | 9 +++++++++ 1 file changed, 9 insertions(+)
include/linux/bpf.h +9 -0
@@ -2169,6 +2169,15 @@ static inline struct bpf_prog *bpf_prog_by_id(u32 id)return ERR_PTR (-ENOTSUPP ); } +static inline int btf_struct_access (struct bpf_verifier_log *log, +const struct btf *btf, +const struct btf_type *t,int off,int size, +enum bpf_access_type atype, +u32 *next_btf_id,enum bpf_type_flag *flag) +{ +return -EACCES ; +} +static inline const struct bpf_func_proto *bpf_base_func_proto (enum bpf_func_id func_id) {
Signature · 1 line
2.37.1
PATCH v5 3/6 bpf: Use 0 instead of NOT_INIT for btf_struct_access() writes
In reply to Daniel XuReturning a bpf_reg_type only makes sense in the context of a BPF_READ. For writes, prefer to explicitly return 0 for clarity.
Note that is non-functional change as it just so happened that NOT_INIT == 0.
--- net/ipv4/bpf_tcp_ca.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/bpf_tcp_ca.c +1 -1
@@ -124,7 +124,7 @@ static int bpf_tcp_ca_btf_struct_access(struct bpf_verifier_log *log,return -EACCES ; } -return NOT_INIT ; +return 0 ; } BPF_CALL_2(bpf_tcp_send_ack, struct tcp_sock *, tp, u32, rcv_nxt)
Signature · 1 line
2.37.1
PATCH v5 4/6 bpf: Export btf_type_by_id() and bpf_log()
In reply to Daniel XuThese symbols will be used in nf_conntrack.ko to support direct writes to nf_conn.
--- kernel/bpf/btf.c | 1 + kernel/bpf/verifier.c | 1 + 2 files changed, 2 insertions(+)
kernel/bpf/btf.c +1 -0
@@ -818,6 +818,7 @@ const struct btf_type *btf_type_by_id(const struct btf *btf, u32 type_id)return NULL ;return btf->types[type_id]; } +EXPORT_SYMBOL_GPL (btf_type_by_id);/* * Regular int is not a bit field and it must be either
kernel/bpf/verifier.c +1 -0
@@ -370,6 +370,7 @@ __printf(2, 3) void bpf_log(struct bpf_verifier_log *log,bpf_verifier_vlog (log, fmt, args);va_end (args); } +EXPORT_SYMBOL_GPL (bpf_log);static const char *ltrim (const char *s) {
Signature · 1 line
2.37.1
PATCH v5 5/6 bpf: Add support for writing to nf_conn:mark
In reply to Daniel XuSupport direct writes to nf_conn:mark from TC and XDP prog types. This is useful when applications want to store per-connection metadata. This is also particularly useful for applications that run both bpf and iptables/nftables because the latter can trivially access this metadata.
One example use case would be if a bpf prog is responsible for advanced packet classification and iptables/nftables is later used for routing due to pre-existing/legacy code.
--- include/net/netfilter/nf_conntrack_bpf.h | 23 +++++++++ net/core/filter.c | 54 +++++++++++++++++++ net/netfilter/nf_conntrack_bpf.c | 66 +++++++++++++++++++++++- net/netfilter/nf_conntrack_core.c | 1 + 4 files changed, 143 insertions(+), 1 deletion(-)
include/net/netfilter/nf_conntrack_bpf.h +23 -0
@@ -3,13 +3,22 @@#ifndef _NF_CONNTRACK_BPF_H#define _NF_CONNTRACK_BPF_H +#include <linux/bpf.h> #include <linux/btf.h> #include <linux/kconfig.h> +#include <linux/mutex.h> #if (IS_BUILTIN (CONFIG_NF_CONNTRACK ) &&IS_ENABLED (CONFIG_DEBUG_INFO_BTF )) || \ (IS_MODULE (CONFIG_NF_CONNTRACK ) &&IS_ENABLED (CONFIG_DEBUG_INFO_BTF_MODULES ))extern int register_nf_conntrack_bpf (void ); +extern void cleanup_nf_conntrack_bpf (void ); + +extern struct mutex nf_conn_btf_access_lock; +extern int (*nfct_bsa)(struct bpf_verifier_log *log,const struct btf *btf, +const struct btf_type *t,int off,int size, +enum bpf_access_type atype,u32 *next_btf_id, +enum bpf_type_flag *flag);#else
@@ -18,6 +27,20 @@ static inline int register_nf_conntrack_bpf(void)return 0 ; } +static inline void cleanup_nf_conntrack_bpf (void ) +{ +} + +static inline int nf_conntrack_btf_struct_access (struct bpf_verifier_log *log, +const struct btf *btf, +const struct btf_type *t,int off, +int size,enum bpf_access_type atype, +u32 *next_btf_id, +enum bpf_type_flag *flag) +{ +return -EACCES ; +} +#endif #endif /* _NF_CONNTRACK_BPF_H */
net/core/filter.c +54 -0
84 lines not on this page · raw message
net/netfilter/nf_conntrack_bpf.c +65 -1
84 lines not on this page · raw message
net/netfilter/nf_conntrack_core.c +1 -0
@@ -2512,6 +2512,7 @@ static int kill_all(struct nf_conn *i, void *data)void nf_conntrack_cleanup_start (void ) { +cleanup_nf_conntrack_bpf (); conntrack_gc_work.exiting = true; }
Signature · 1 line
2.37.1
PATCH v5 6/6 selftests/bpf: Add tests for writing to nf_conn:mark
In reply to Daniel XuAdd a simple extension to the existing selftest to write to nf_conn:mark. Also add a failure test for writing to unsupported field.
--- tools/testing/selftests/bpf/prog_tests/bpf_nf.c | 2 ++ tools/testing/selftests/bpf/progs/test_bpf_nf.c | 9 +++++++-- .../testing/selftests/bpf/progs/test_bpf_nf_fail.c | 14 ++++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-)
tools/testing/selftests/bpf/prog_tests/bpf_nf.c +2 -0
@@ -17,6 +17,7 @@ struct { { "set_status_after_insert", "kernel function bpf_ct_set_status args#0 expected pointer to STRUCT nf_conn___init but" }, { "change_timeout_after_alloc", "kernel function bpf_ct_change_timeout args#0 expected pointer to STRUCT nf_conn but" }, { "change_status_after_alloc", "kernel function bpf_ct_change_status args#0 expected pointer to STRUCT nf_conn but" }, + { "write_not_allowlisted_field", "no write support to nf_conn at off" }, }; enum {
@@ -113,6 +114,7 @@ static void test_bpf_nf_ct(int mode)ASSERT_LE (skel->bss->test_delta_timeout,10 ,"Test for max ct timeout update" );/* expected status is IPS_SEEN_REPLY */ ASSERT_EQ (skel->bss->test_status,2 ,"Test for ct status update " ); +ASSERT_EQ (skel->bss->test_insert_lookup_mark,77 ,"Test for insert and lookup mark value" );ASSERT_EQ (skel->data->test_exist_lookup,0 ,"Test existing connection lookup" );ASSERT_EQ (skel->bss->test_exist_lookup_mark,43 ,"Test existing connection lookup ctmark" ); end:
tools/testing/selftests/bpf/progs/test_bpf_nf.c +7 -2
@@ -23,6 +23,7 @@ int test_insert_entry = -EAFNOSUPPORT;int test_succ_lookup = -ENOENT ;u32 test_delta_timeout =0 ;u32 test_status =0 ; +u32 test_insert_lookup_mark =0 ;__be32 saddr =0 ;__be16 sport =0 ;__be32 daddr =0 ;
@@ -144,6 +145,7 @@ nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32,bpf_ct_set_timeout (ct,10000 );bpf_ct_set_status (ct,IPS_CONFIRMED ); + ct->mark =77 ; ct_ins =bpf_ct_insert_entry (ct);if (ct_ins) {
@@ -157,6 +159,7 @@ nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32, test_delta_timeout = ct_lk->timeout -bpf_jiffies64 (); test_delta_timeout /=CONFIG_HZ ; test_status =IPS_SEEN_REPLY ; + test_insert_lookup_mark = ct_lk->mark;bpf_ct_change_status (ct_lk,IPS_SEEN_REPLY );bpf_ct_release (ct_lk); test_succ_lookup =0 ;
@@ -175,8 +178,10 @@ nf_ct_test(struct nf_conn *(*lookup_fn)(void *, struct bpf_sock_tuple *, u32, sizeof(opts_def));if (ct) { test_exist_lookup =0 ; -if (ct->mark ==42 ) - test_exist_lookup_mark =43 ; +if (ct->mark ==42 ) { + ct->mark++; + test_exist_lookup_mark = ct->mark; + }bpf_ct_release (ct); }else { test_exist_lookup = opts_def.error;
tools/testing/selftests/bpf/progs/test_bpf_nf_fail.c +14 -0
@@ -69,6 +69,20 @@ int lookup_insert(struct __sk_buff *ctx)return 0 ; } +SEC("?tc") +int write_not_allowlisted_field (struct __sk_buff *ctx) +{ +struct bpf_ct_opts___local opts = {}; +struct bpf_sock_tuple tup = {}; +struct nf_conn *ct; + + ct =bpf_skb_ct_lookup (ctx, &tup,sizeof (tup.ipv4), &opts,sizeof (opts)); +if (!ct) +return 0 ; + ct->status =0xF00 ; +return 0 ; +} + SEC("?tc")int set_timeout_after_insert (struct __sk_buff *ctx) {
Signature · 1 line
2.37.1
Quoted 10 lines · Daniel Xu
Support direct writes to nf_conn:mark from TC and XDP prog types. This is useful when applications want to store per-connection metadata. This is also particularly useful for applications that run both bpf and iptables/nftables because the latter can trivially access this metadata.
One example use case would be if a bpf prog is responsible for advanced packet classification and iptables/nftables is later used for routing due to pre-existing/legacy code.
There are a couple of compile time warnings when conntrack is disabled,
../net/core/filter.c:8608:1: warning: symbol 'nf_conn_btf_access_lock' was not declared. Should it be static? ../net/core/filter.c:8611:5: warning: symbol 'nfct_bsa' was not declared. Should it be static?
Most likely because extern declaration is guarded by ifdefs. So just moving those out of ifdef should work.
I guess you can send that as a follow up fix, or roll it in if you end up respinning.
Otherwise, for the series:
Hi Kumar,
Quoted 19 lines · Kumar Kartikeya Dwivedi
Quoted 10 lines · Daniel Xu
Support direct writes to nf_conn:mark from TC and XDP prog types. This is useful when applications want to store per-connection metadata. This is also particularly useful for applications that run both bpf and iptables/nftables because the latter can trivially access this metadata.
One example use case would be if a bpf prog is responsible for advanced packet classification and iptables/nftables is later used for routing due to pre-existing/legacy code.
There are a couple of compile time warnings when conntrack is disabled,
../net/core/filter.c:8608:1: warning: symbol 'nf_conn_btf_access_lock' was not declared. Should it be static?../net/core/filter.c:8611:5: warning: symbol 'nfct_bsa' was not declared. Should it be static?
Quoted 4 lines
Most likely because extern declaration is guarded by ifdefs. So just moving those out of ifdef should work.
I guess you can send that as a follow up fix, or roll it in if you end up respinning.
Hmm, I don't see how filter.c ever #include's nf_conntrack_bpf.h. So you'd think that the warning would always be present regardless of CONFIG_NF_CONNTRACK setting.
FWIW I can't reproduce the warning even with CONFIG_NF_CONNTRACK=n.
Maybe the extern declarations should be in include/linux/filter.h anyways? Might be cleaner. WDYT?
Quoted 2 lines
Otherwise, for the series:
Thanks!
Daniel
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 10 lines · On Wed, 7 Sep 2022 10:40:35 -0600 you wrote:
Support direct writes to nf_conn:mark from TC and XDP prog types. This is useful when applications want to store per-connection metadata. This is also particularly useful for applications that run both bpf and iptables/nftables because the latter can trivially access this metadata.
One example use case would be if a bpf prog is responsible for advanced packet classification and iptables/nftables is later used for routing due to pre-existing/legacy code.
[...]
Here is the summary with links:
- [bpf-next,v5,1/6] bpf: Remove duplicate PTR_TO_BTF_ID RO check
https://git.kernel.org/bpf/bpf-next/c/65269888c695 - [bpf-next,v5,2/6] bpf: Add stub for btf_struct_access() https://git.kernel.org/bpf/bpf-next/c/d4f7bdb2ed7b - [bpf-next,v5,3/6] bpf: Use 0 instead of NOT_INIT for btf_struct_access() writes https://git.kernel.org/bpf/bpf-next/c/896f07c07da0 - [bpf-next,v5,4/6] bpf: Export btf_type_by_id() and bpf_log() https://git.kernel.org/bpf/bpf-next/c/84c6ac417cea - [bpf-next,v5,5/6] bpf: Add support for writing to nf_conn:mark https://git.kernel.org/bpf/bpf-next/c/864b656f82cc - [bpf-next,v5,6/6] selftests/bpf: Add tests for writing to nf_conn:mark https://git.kernel.org/bpf/bpf-next/c/e2d75e954c0a
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html