Merge branch 'bpf-sockmap-reject-a-packet-modifying-sk_skb-stream-parser'

Sechang Lim says:

====================
bpf, sockmap: reject a packet-modifying SK_SKB stream parser

A BPF_PROG_TYPE_SK_SKB stream parser runs on strparser's message head,
which can chain skbs through frag_list. A parser that resizes the skb
frees the frag_list segments that strparser still tracks through
skb_nextp, leading to a use-after-free.

A stream parser is only meant to measure the next message, not to modify
the packet, so reject a packet-modifying parser at attach time.

v5:
 - target bpf-next instead of bpf
 - add Reviewed-by tag (Jiayuan Chen)

v4:
 - https://lore.kernel.org/all/20260619062959.3277612-1-rhkrqnwk98@gmail.com/

v3:
 - https://lore.kernel.org/all/20260618102718.2331468-1-rhkrqnwk98@gmail.com/

v2:
 - https://lore.kernel.org/all/20260612123553.2724240-1-rhkrqnwk98@gmail.com/

v1:
 - https://lore.kernel.org/all/20260609112316.3685738-1-rhkrqnwk98@gmail.com/
====================

Link: https://patch.msgid.link/20260620024423.4141004-1-rhkrqnwk98@gmail.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
This commit is contained in:
Alexei Starovoitov 2026-06-25 17:42:02 -07:00
commit 66bb952b44
4 changed files with 58 additions and 22 deletions

View File

@ -1515,6 +1515,17 @@ static int sock_map_prog_link_lookup(struct bpf_map *map, struct bpf_prog ***ppr
return 0;
}
static int sock_map_prog_attach_check(enum bpf_attach_type attach_type,
struct bpf_prog *prog)
{
/* A stream parser must not modify the skb, only measure it. */
if (prog && attach_type == BPF_SK_SKB_STREAM_PARSER &&
prog->aux->changes_pkt_data)
return -EINVAL;
return 0;
}
/* Handle the following four cases:
* prog_attach: prog != NULL, old == NULL, link == NULL
* prog_detach: prog == NULL, old != NULL, link == NULL
@ -1533,6 +1544,10 @@ static int sock_map_prog_update(struct bpf_map *map, struct bpf_prog *prog,
if (ret)
return ret;
ret = sock_map_prog_attach_check(which, prog);
if (ret)
return ret;
/* for prog_attach/prog_detach/link_attach, return error if a bpf_link
* exists for that prog.
*/
@ -1776,6 +1791,11 @@ static int sock_map_link_update_prog(struct bpf_link *link,
ret = -EINVAL;
goto out;
}
ret = sock_map_prog_attach_check(link->attach_type, prog);
if (ret)
goto out;
if (!sockmap_link->map) {
ret = -ENOLINK;
goto out;

View File

@ -431,6 +431,35 @@ static void test_sockmap_strp_verdict(int family, int sotype)
test_sockmap_strp__destroy(strp);
}
static void test_sockmap_strp_parser_reject(void)
{
struct test_sockmap_strp *strp = NULL;
int parser_mod, parser_ro, link;
int err, map;
strp = test_sockmap_strp__open_and_load();
if (!ASSERT_OK_PTR(strp, "test_sockmap_strp__open_and_load"))
return;
map = bpf_map__fd(strp->maps.sock_map);
parser_mod = bpf_program__fd(strp->progs.prog_skb_parser_resize);
parser_ro = bpf_program__fd(strp->progs.prog_skb_parser);
err = bpf_prog_attach(parser_mod, map, BPF_SK_SKB_STREAM_PARSER, 0);
ASSERT_ERR(err, "bpf_prog_attach parser_mod");
link = bpf_link_create(parser_ro, map, BPF_SK_SKB_STREAM_PARSER, NULL);
if (!ASSERT_GE(link, 0, "bpf_link_create parser_ro"))
goto out;
err = bpf_link_update(link, parser_mod, NULL);
ASSERT_ERR(err, "bpf_link_update parser_mod");
out:
if (link >= 0)
close(link);
test_sockmap_strp__destroy(strp);
}
void test_sockmap_strp(void)
{
if (test__start_subtest("sockmap strp tcp pass"))
@ -451,4 +480,6 @@ void test_sockmap_strp(void)
test_sockmap_strp_multiple_pkt(AF_INET, SOCK_STREAM);
if (test__start_subtest("sockmap strp tcp dispatch"))
test_sockmap_strp_dispatch_pkt(AF_INET, SOCK_STREAM);
if (test__start_subtest("sockmap strp parser reject pkt mod"))
test_sockmap_strp_parser_reject();
}

View File

@ -5,28 +5,6 @@
SEC("sk_skb1")
int bpf_prog1(struct __sk_buff *skb)
{
void *data_end = (void *)(long) skb->data_end;
void *data = (void *)(long) skb->data;
__u8 *d = data;
int err;
if (data + 10 > data_end) {
err = bpf_skb_pull_data(skb, 10);
if (err)
return SK_DROP;
data_end = (void *)(long)skb->data_end;
data = (void *)(long)skb->data;
if (data + 10 > data_end)
return SK_DROP;
}
/* This write/read is a bit pointless but tests the verifier and
* strparser handler for read/write pkt data and access into sk
* fields.
*/
d = data;
d[7] = 1;
return skb->len;
}

View File

@ -50,4 +50,11 @@ int prog_skb_parser_partial(struct __sk_buff *skb)
return 10;
}
SEC("sk_skb/stream_parser")
int prog_skb_parser_resize(struct __sk_buff *skb)
{
bpf_skb_change_tail(skb, skb->len, 0);
return skb->len;
}
char _license[] SEC("license") = "GPL";