Enable socket lookup in SOCKMAP/SOCKHASH from BPF
Subsystems (9)
Jakub Sitnicki
maintainercover Enable socket lookup in SOCKMAP/SOCKHASH from BPF
This series enables BPF programs to fetch sockets from SOCKMAP/SOCKHASH by doing a map lookup, as proposed during virtual BPF conference.
Patch 1 description covers changes on verifier side needed to make it work.
Fetched socket can be inspected or passed to helpers such as bpf_sk_assign, which is demonstrated by the test updated in patch 3.
Thanks,
Jakub
Jakub Sitnicki (3):
bpf: Allow bpf_map_lookup_elem for SOCKMAP and SOCKHASH selftests/bpf: Test that lookup on SOCKMAP/SOCKHASH is allowed selftests/bpf: Use SOCKMAP for server sockets in bpf_sk_assign test
kernel/bpf/verifier.c | 45 +++++++--- net/core/filter.c | 4 + net/core/sock_map.c | 18 +++- tools/testing/selftests/bpf/Makefile | 2 +- .../selftests/bpf/prog_tests/sk_assign.c | 21 ++++- .../selftests/bpf/progs/test_sk_assign.c | 82 ++++++++----------- .../bpf/verifier/prevent_map_lookup.c | 30 ------- tools/testing/selftests/bpf/verifier/sock.c | 70 ++++++++++++++++ 8 files changed, 178 insertions(+), 94 deletions(-)
Signature · 1 line
2.25.3
Jakub Sitnicki
maintainerPATCH 1/3 bpf: Allow bpf_map_lookup_elem for SOCKMAP and SOCKHASH
White-list map lookup for SOCKMAP/SOCKHASH from BPF. Lookup returns a pointer to a full socket and acquires a reference if necessary.
To support it we need to extend the verifier to know that:
(1) register storing the lookup result holds a pointer to socket, if lookup was done on SOCKMAP/SOCKHASH, and that
(2) map lookup on SOCKMAP/SOCKHASH is a reference acquiring operation, which needs a corresponding reference release with bpf_sk_release.
On sock_map side, lookup handlers exposed via bpf_map_ops now bump sk_refcnt if socket is reference counted. In turn, bpf_sk_select_reuseport, the only in-kernel user of SOCKMAP/SOCKHASH ops->map_lookup_elem, was updated to release the reference.
Sockets fetched from a map can be used in the same way as ones returned by BPF socket lookup helpers, such as bpf_sk_lookup_tcp. In particular, they can be used with bpf_sk_assign to direct packets toward a socket on TC ingress path.
--- kernel/bpf/verifier.c | 45 +++++++++++++++++++++++++++++++++---------- net/core/filter.c | 4 ++++ net/core/sock_map.c | 18 +++++++++++++++-- 3 files changed, 55 insertions(+), 12 deletions(-)
kernel/bpf/verifier.c +35 -10
@@ -429,11 +429,30 @@ static bool is_release_function(enum bpf_func_id func_id)return func_id == BPF_FUNC_sk_release; } -static bool is_acquire_function (enum bpf_func_id func_id) +static bool may_be_acquire_function (enum bpf_func_id func_id) {return func_id == BPF_FUNC_sk_lookup_tcp || func_id == BPF_FUNC_sk_lookup_udp || - func_id == BPF_FUNC_skc_lookup_tcp; + func_id == BPF_FUNC_skc_lookup_tcp || + func_id == BPF_FUNC_map_lookup_elem; +} + +static bool is_acquire_function (enum bpf_func_id func_id, +const struct bpf_map *map) +{ +enum bpf_map_type map_type = map ? map->map_type :BPF_MAP_TYPE_UNSPEC ; + +if (func_id == BPF_FUNC_sk_lookup_tcp || + func_id == BPF_FUNC_sk_lookup_udp || + func_id == BPF_FUNC_skc_lookup_tcp) +return true; + +if (func_id == BPF_FUNC_map_lookup_elem && + (map_type ==BPF_MAP_TYPE_SOCKMAP || + map_type ==BPF_MAP_TYPE_SOCKHASH )) +return true; + +return false; }static bool is_ptr_cast_function (enum bpf_func_id func_id)
@@ -3934,7 +3953,8 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_sock_map_update && func_id != BPF_FUNC_map_delete_elem && func_id != BPF_FUNC_msg_redirect_map && - func_id != BPF_FUNC_sk_select_reuseport) + func_id != BPF_FUNC_sk_select_reuseport && + func_id != BPF_FUNC_map_lookup_elem) goto error;break ;case BPF_MAP_TYPE_SOCKHASH :
@@ -3942,7 +3962,8 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_sock_hash_update && func_id != BPF_FUNC_map_delete_elem && func_id != BPF_FUNC_msg_redirect_hash && - func_id != BPF_FUNC_sk_select_reuseport) + func_id != BPF_FUNC_sk_select_reuseport && + func_id != BPF_FUNC_map_lookup_elem) goto error;break ;case BPF_MAP_TYPE_REUSEPORT_SOCKARRAY :
@@ -4112,7 +4133,7 @@ static bool check_refcount_ok(const struct bpf_func_proto *fn, int func_id)/* A reference acquiring function cannot acquire * another refcounted ptr. */ -if (is_acquire_function (func_id) && count) +if (may_be_acquire_function (func_id) && count)return false;/* We only support one arg being unreferenced at the moment,
@@ -4623,7 +4644,7 @@ static int check_helper_call(struct bpf_verifier_env *env, int func_id, int insnif (is_ptr_cast_function (func_id)) {/* For release_reference() */ regs[BPF_REG_0 ].ref_obj_id = meta.ref_obj_id; - }else if (is_acquire_function (func_id)) { + }else if (is_acquire_function (func_id, meta.map_ptr)) {int id =acquire_reference_state (env, insn_idx);if (id <0 )
@@ -6532,12 +6553,16 @@ static void mark_ptr_or_null_reg(struct bpf_func_state *state,if (is_null) { reg->type =SCALAR_VALUE ; }else if (reg->type ==PTR_TO_MAP_VALUE_OR_NULL ) { -if (reg->map_ptr->inner_map_meta) { +const struct bpf_map *map = reg->map_ptr; + +if (map->inner_map_meta) { reg->type =CONST_PTR_TO_MAP ; - reg->map_ptr = reg->map_ptr->inner_map_meta; - }else if (reg->map_ptr->map_type == -BPF_MAP_TYPE_XSKMAP ) { + reg->map_ptr = map->inner_map_meta; + }else if (map->map_type ==BPF_MAP_TYPE_XSKMAP ) { reg->type =PTR_TO_XDP_SOCK ; + }else if (map->map_type ==BPF_MAP_TYPE_SOCKMAP || + map->map_type ==BPF_MAP_TYPE_SOCKHASH ) { + reg->type =PTR_TO_SOCKET ; }else { reg->type =PTR_TO_MAP_VALUE ; }
net/core/filter.c +4 -0
@@ -8712,6 +8712,10 @@ BPF_CALL_4(sk_select_reuseport, struct sk_reuseport_kern *, reuse_kern, reuse =rcu_dereference (selected_sk->sk_reuseport_cb);if (!reuse) { +/* Lookup in sock_map can return TCP ESTABLISHED sockets. */ +if (sk_is_refcounted (selected_sk)) +sock_put (selected_sk); +/* reuseport_array has only sk with non NULL sk_reuseport_cb. * The only (!reuse) case here is - the sk has already been * unhashed (e.g. by close()), so treat it as -ENOENT.
net/core/sock_map.c +16 -2
@@ -343,7 +343,14 @@ static struct sock *__sock_map_lookup_elem(struct bpf_map *map, u32 key)static void *sock_map_lookup (struct bpf_map *map,void *key) { -return __sock_map_lookup_elem (map, *(u32 *)key); +struct sock *sk; + + sk =__sock_map_lookup_elem (map, *(u32 *)key); +if (!sk || !sk_fullsock (sk)) +return NULL ; +if (sk_is_refcounted (sk) && !refcount_inc_not_zero (&sk->sk_refcnt)) +return NULL ; +return sk; }static void *sock_map_lookup_sys (struct bpf_map *map,void *key)
@@ -1051,7 +1058,14 @@ static void *sock_hash_lookup_sys(struct bpf_map *map, void *key)static void *sock_hash_lookup (struct bpf_map *map,void *key) { -return __sock_hash_lookup_elem (map, key); +struct sock *sk; + + sk =__sock_hash_lookup_elem (map, key); +if (!sk || !sk_fullsock (sk)) +return NULL ; +if (sk_is_refcounted (sk) && !refcount_inc_not_zero (&sk->sk_refcnt)) +return NULL ; +return sk; }static void sock_hash_release_progs (struct bpf_map *map)
Signature · 1 line
2.25.3
RE: [PATCH bpf-next 1/3] bpf: Allow bpf_map_lookup_elem for SOCKMAP and SOCKHASH
Quoted 16 lines · Jakub Sitnicki
White-list map lookup for SOCKMAP/SOCKHASH from BPF. Lookup returns a pointer to a full socket and acquires a reference if necessary.
To support it we need to extend the verifier to know that:
(1) register storing the lookup result holds a pointer to socket, if lookup was done on SOCKMAP/SOCKHASH, and that
(2) map lookup on SOCKMAP/SOCKHASH is a reference acquiring operation, which needs a corresponding reference release with
bpf_sk_release.On
sock_mapside, lookup handlers exposed viabpf_map_opsnow bumpsk_refcntif socket is reference counted. In turn,bpf_sk_select_reuseport, the only in-kernel user of SOCKMAP/SOCKHASHops->map_lookup_elem, was updated to release the reference.
Quoted 8 lines
Sockets fetched from a map can be used in the same way as ones returned by BPF socket lookup helpers, such as
bpf_sk_lookup_tcp. In particular, they can be used withbpf_sk_assignto direct packets toward a socket on TC ingress path.Suggested-by Lorenz Bauer <[email protected]>Signed-off-by Jakub Sitnicki <[email protected]>---
LGTM thanks!
Jakub Sitnicki
maintainerPATCH 2/3 selftests/bpf: Test that lookup on SOCKMAP/SOCKHASH is allowed
In reply to Jakub SitnickiNow that bpf_map_lookup_elem() is white-listed for SOCKMAP/SOCKHASH, replace the tests which check that verifier prevents lookup on these map types with ones that ensure that lookup operation is permitted, but only with a release of acquired socket reference.
--- .../bpf/verifier/prevent_map_lookup.c | 30 -------- tools/testing/selftests/bpf/verifier/sock.c | 70 +++++++++++++++++++ 2 files changed, 70 insertions(+), 30 deletions(-)
tools/testing/selftests/bpf/verifier/prevent_map_lookup.c +0 -30
@@ -1,33 +1,3 @@ -{ - "prevent map lookup in sockmap", - .insns = { -BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), -BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), -BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), -BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem), - BPF_EXIT_INSN(), - }, - .fixup_map_sockmap = { 3 }, - .result = REJECT, - .errstr = "cannot pass map_type 15 into func bpf_map_lookup_elem", - .prog_type = BPF_PROG_TYPE_SOCK_OPS, -}, -{ -"prevent map lookup in sockhash" , - .insns = { -BPF_ST_MEM (BPF_DW ,BPF_REG_10 ,-8 ,0 ), -BPF_MOV64_REG (BPF_REG_2 ,BPF_REG_10 ), -BPF_ALU64_IMM (BPF_ADD ,BPF_REG_2 ,-8 ), -BPF_LD_MAP_FD (BPF_REG_1 ,0 ), -BPF_RAW_INSN (BPF_JMP |BPF_CALL ,0 ,0 ,0 , BPF_FUNC_map_lookup_elem), - BPF_EXIT_INSN(), - }, - .fixup_map_sockhash = { 3 }, - .result = REJECT, - .errstr = "cannot pass map_type 18 into func bpf_map_lookup_elem", - .prog_type = BPF_PROG_TYPE_SOCK_OPS, -}, { "prevent map lookup in stack trace", .insns = {
tools/testing/selftests/bpf/verifier/sock.c +70 -0
73 lines not on this page · raw message
Signature · 1 line
2.25.3
RE: [PATCH bpf-next 2/3] selftests/bpf: Test that lookup on SOCKMAP/SOCKHASH is allowed
Quoted 7 lines · Jakub Sitnicki
Now that
bpf_map_lookup_elem()is white-listed for SOCKMAP/SOCKHASH, replace the tests which check that verifier prevents lookup on these map types with ones that ensure that lookup operation is permitted, but only with a release of acquired socket reference.Signed-off-by Jakub Sitnicki <[email protected]>---
For completeness would be nice to add a test passing this into sk_select_reuseport to cover that case as well. Could come as a follow up patch imo.
Jakub Sitnicki
maintainerRe: [PATCH bpf-next 2/3] selftests/bpf: Test that lookup on SOCKMAP/SOCKHASH is allowed
Quoted 6 lines · John Fastabend
Quoted 5 lines · Jakub Sitnicki
Now that
bpf_map_lookup_elem()is white-listed for SOCKMAP/SOCKHASH, replace the tests which check that verifier prevents lookup on these map types with ones that ensure that lookup operation is permitted, but only with a release of acquired socket reference.
Quoted 6 lines
Quoted 2 lines
Signed-off-by Jakub Sitnicki <[email protected]>---
For completeness would be nice to add a test passing this into
sk_select_reuseportto cover that case as well. Could come as a follow up patch imo.
Is this what you had in mind?
https://lore.kernel.org/bpf/[email protected]/
Thanks for reviewing the series.
John Fastabend
maintainerRe: [PATCH bpf-next 2/3] selftests/bpf: Test that lookup on SOCKMAP/SOCKHASH is allowed
Quoted 7 lines · Jakub Sitnicki
Quoted 6 lines · John Fastabend
Quoted 5 lines · Jakub Sitnicki
Now that
bpf_map_lookup_elem()is white-listed for SOCKMAP/SOCKHASH, replace the tests which check that verifier prevents lookup on these map types with ones that ensure that lookup operation is permitted, but only with a release of acquired socket reference.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Signed-off-by Jakub Sitnicki <[email protected]>---
For completeness would be nice to add a test passing this into
sk_select_reuseportto cover that case as well. Could come as a follow up patch imo.Is this what you had in mind?
Yes thanks!
Quoted 4 lines
https://lore.kernel.org/bpf/[email protected]/
Thanks for reviewing the series.
Jakub Sitnicki
maintainerPATCH 3/3 selftests/bpf: Use SOCKMAP for server sockets in bpf_sk_assign test
In reply to Jakub SitnickiUpdate bpf_sk_assign test to fetch the server socket from SOCKMAP, now that map lookup from BPF in SOCKMAP is enabled. This way the test TC BPF program doesn't need to know what address server socket is bound to.
--- tools/testing/selftests/bpf/Makefile | 2 +- .../selftests/bpf/prog_tests/sk_assign.c | 21 ++++- .../selftests/bpf/progs/test_sk_assign.c | 82 ++++++++----------- 3 files changed, 53 insertions(+), 52 deletions(-)
tools/testing/selftests/bpf/Makefile +1 -1
@@ -243,7 +243,7 @@ define GCC_BPF_BUILD_RULE $(BPF_GCC) $3 $4 -O2 -c $1 -o $2 endef -SKEL_BLACKLIST := btf__% test_pinning_invalid.c +SKEL_BLACKLIST := btf__% test_pinning_invalid.c test_sk_assign.c # Set up extra TRUNNER_XXX "temporary" variables in the environment (relies on # $eval()) and pass control to DEFINE_TEST_RUNNER_RULES.
tools/testing/selftests/bpf/prog_tests/sk_assign.c +20 -1
@@ -20,6 +20,7 @@#define CONNECT_PORT 4321#define TEST_DADDR (0xC0A80203)#define NS_SELF "/proc/self/ns/net" +#define SERVER_MAP_PATH "/sys/fs/bpf/tc/globals/server_map"static const struct timeval timeo_sec = { .tv_sec =3 };static const size_t timeo_optlen =sizeof (timeo_sec);
@@ -265,6 +266,7 @@ void test_sk_assign(void) TEST("ipv6 udp addr redir", AF_INET6, SOCK_DGRAM, true), };int server =-1 ; +int server_map;int self_net; self_net =open (NS_SELF ,O_RDONLY );
@@ -278,9 +280,17 @@ void test_sk_assign(void) goto cleanup; } + server_map =bpf_obj_get (SERVER_MAP_PATH ); +if (CHECK_FAIL (server_map <0 )) { +perror ("Unable to open " SERVER_MAP_PATH ); + goto cleanup; + } +for (int i =0 ; i <ARRAY_SIZE (tests) && !READ_ONCE (stop); i++) {struct test_sk_cfg *test = &tests[i];const struct sockaddr *addr; +const int zero =0 ; +int err;if (!test__start_subtest (test->name))continue ;
@@ -288,7 +298,13 @@ void test_sk_assign(void) addr = (const struct sockaddr *)test->addr; server =start_server (addr, test->len, test->type);if (server ==-1 ) - goto cleanup; + goto close; + + err =bpf_map_update_elem (server_map, &zero, &server,BPF_ANY ); +if (CHECK_FAIL (err)) { +perror ("Unable to update server_map" ); + goto close; + }/* connect to unbound ports */ prepare_addr(test->addr, test->family, CONNECT_PORT,
@@ -302,7 +318,10 @@ void test_sk_assign(void) close:close (server); +close (server_map); cleanup: +if (CHECK_FAIL (unlink (SERVER_MAP_PATH ))) +perror ("Unable to unlink " SERVER_MAP_PATH );if (CHECK_FAIL (setns (self_net,CLONE_NEWNET )))perror ("Failed to setns(" NS_SELF ")" );close (self_net);
tools/testing/selftests/bpf/progs/test_sk_assign.c +32 -50
116 lines not on this page · raw message
Signature · 1 line
2.25.3
RE: [PATCH bpf-next 3/3] selftests/bpf: Use SOCKMAP for server sockets in bpf_sk_assign test
Quoted 6 lines · Jakub Sitnicki
Update
bpf_sk_assigntest to fetch the server socket from SOCKMAP, now that map lookup from BPF in SOCKMAP is enabled. This way the test TC BPF program doesn't need to know what address server socket is bound to.Signed-off-by Jakub Sitnicki <[email protected]>---
Quoted 16 lines · Jakub Sitnicki
This series enables BPF programs to fetch sockets from SOCKMAP/SOCKHASH by doing a map lookup, as proposed during virtual BPF conference.
Patch 1 description covers changes on verifier side needed to make it work.
Fetched socket can be inspected or passed to helpers such as
bpf_sk_assign, which is demonstrated by the test updated in patch 3.Thanks,
JakubJakub Sitnicki (3):
bpf: Allowbpf_map_lookup_elemfor SOCKMAP and SOCKHASH selftests/bpf: Test that lookup on SOCKMAP/SOCKHASH is allowed selftests/bpf: Use SOCKMAP for server sockets inbpf_sk_assigntest
Quoted 10 lines
kernel/bpf/verifier.c | 45 +++++++--- net/core/filter.c | 4 + net/core/sock_map.c | 18 +++- tools/testing/selftests/bpf/Makefile | 2 +- .../selftests/bpf/prog_tests/sk_assign.c | 21 ++++- .../selftests/bpf/progs/test_sk_assign.c | 82 ++++++++----------- .../bpf/verifier/prevent_map_lookup.c | 30 ------- tools/testing/selftests/bpf/verifier/sock.c | 70 ++++++++++++++++ 8 files changed, 178 insertions(+), 94 deletions(-)
Applied, thanks!