Skip to content

Commit

Permalink
bpf, test, cgroup: Use sk_{alloc,free} for test cases
Browse files Browse the repository at this point in the history
BPF test infra has some hacks in place which kzalloc() a socket and perform
minimum init via sock_net_set() and sock_init_data(). As a result, the sk's
skcd->cgroup is NULL since it didn't go through proper initialization as it
would have been the case from sk_alloc(). Rather than re-adding a NULL test
in sock_cgroup_ptr() just for this, use sk_{alloc,free}() pair for the test
socket. The latter also allows to get rid of the bpf_sk_storage_free() special
case.

Fixes: 8520e22 ("bpf, cgroups: Fix cgroup v2 fallback on v1/v2 mixed mode")
Fixes: b7a1848 ("bpf: add BPF_PROG_TEST_RUN support for flow dissector")
Fixes: 2cb494a ("bpf: add tests for direct packet access from CGROUP_SKB")
Reported-by: [email protected]
Reported-by: [email protected]
Signed-off-by: Daniel Borkmann <[email protected]>
Signed-off-by: Alexei Starovoitov <[email protected]>
Tested-by: [email protected]
Tested-by: [email protected]
Link: https://lore.kernel.org/bpf/[email protected]
  • Loading branch information
borkmann committed Sep 28, 2021
1 parent 78cc316 commit 435b08e
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions net/bpf/test_run.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ static void convert_skb_to___skb(struct sk_buff *skb, struct __sk_buff *__skb)
__skb->gso_segs = skb_shinfo(skb)->gso_segs;
}

static struct proto bpf_dummy_proto = {
.name = "bpf_dummy",
.owner = THIS_MODULE,
.obj_size = sizeof(struct sock),
};

int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
union bpf_attr __user *uattr)
{
Expand Down Expand Up @@ -596,20 +602,19 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
break;
}

sk = kzalloc(sizeof(struct sock), GFP_USER);
sk = sk_alloc(net, AF_UNSPEC, GFP_USER, &bpf_dummy_proto, 1);
if (!sk) {
kfree(data);
kfree(ctx);
return -ENOMEM;
}
sock_net_set(sk, net);
sock_init_data(NULL, sk);

skb = build_skb(data, 0);
if (!skb) {
kfree(data);
kfree(ctx);
kfree(sk);
sk_free(sk);
return -ENOMEM;
}
skb->sk = sk;
Expand Down Expand Up @@ -682,8 +687,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
if (dev && dev != net->loopback_dev)
dev_put(dev);
kfree_skb(skb);
bpf_sk_storage_free(sk);
kfree(sk);
sk_free(sk);
kfree(ctx);
return ret;
}
Expand Down

0 comments on commit 435b08e

Please sign in to comment.