Skip to content

Commit a481649

Browse files
committed
Merge branch 'bpf-test-prog-fixes'
I say: ==================== Fix some bpf program testing framework bugs This series fixes two issue: 1) Accidental user pointer dereference in bpf_test_finish() 2) The packet data given to the test programs is not aligned correctly The first issue is fixed simply because we have a kernel side copy of the datastructure in question already. And the second bug is a simple matter of applying NET_IP_ALIGN where needed. ==================== Signed-off-by: David S. Miller <[email protected]>
2 parents 4e9c3a6 + 586f852 commit a481649

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

net/bpf/test_run.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ static u32 bpf_test_run(struct bpf_prog *prog, void *ctx, u32 repeat, u32 *time)
4949
return ret;
5050
}
5151

52-
static int bpf_test_finish(union bpf_attr __user *uattr, const void *data,
52+
static int bpf_test_finish(const union bpf_attr *kattr,
53+
union bpf_attr __user *uattr, const void *data,
5354
u32 size, u32 retval, u32 duration)
5455
{
55-
void __user *data_out = u64_to_user_ptr(uattr->test.data_out);
56+
void __user *data_out = u64_to_user_ptr(kattr->test.data_out);
5657
int err = -EFAULT;
5758

5859
if (data_out && copy_to_user(data_out, data, size))
@@ -99,7 +100,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
99100
void *data;
100101
int ret;
101102

102-
data = bpf_test_init(kattr, size, NET_SKB_PAD,
103+
data = bpf_test_init(kattr, size, NET_SKB_PAD + NET_IP_ALIGN,
103104
SKB_DATA_ALIGN(sizeof(struct skb_shared_info)));
104105
if (IS_ERR(data))
105106
return PTR_ERR(data);
@@ -124,7 +125,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
124125
return -ENOMEM;
125126
}
126127

127-
skb_reserve(skb, NET_SKB_PAD);
128+
skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
128129
__skb_put(skb, size);
129130
skb->protocol = eth_type_trans(skb, current->nsproxy->net_ns->loopback_dev);
130131
skb_reset_network_header(skb);
@@ -140,7 +141,7 @@ int bpf_prog_test_run_skb(struct bpf_prog *prog, const union bpf_attr *kattr,
140141
/* bpf program can never convert linear skb to non-linear */
141142
if (WARN_ON_ONCE(skb_is_nonlinear(skb)))
142143
size = skb_headlen(skb);
143-
ret = bpf_test_finish(uattr, skb->data, size, retval, duration);
144+
ret = bpf_test_finish(kattr, uattr, skb->data, size, retval, duration);
144145
kfree_skb(skb);
145146
return ret;
146147
}
@@ -155,18 +156,18 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
155156
void *data;
156157
int ret;
157158

158-
data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM, 0);
159+
data = bpf_test_init(kattr, size, XDP_PACKET_HEADROOM + NET_IP_ALIGN, 0);
159160
if (IS_ERR(data))
160161
return PTR_ERR(data);
161162

162163
xdp.data_hard_start = data;
163-
xdp.data = data + XDP_PACKET_HEADROOM;
164+
xdp.data = data + XDP_PACKET_HEADROOM + NET_IP_ALIGN;
164165
xdp.data_end = xdp.data + size;
165166

166167
retval = bpf_test_run(prog, &xdp, repeat, &duration);
167-
if (xdp.data != data + XDP_PACKET_HEADROOM)
168+
if (xdp.data != data + XDP_PACKET_HEADROOM + NET_IP_ALIGN)
168169
size = xdp.data_end - xdp.data;
169-
ret = bpf_test_finish(uattr, xdp.data, size, retval, duration);
170+
ret = bpf_test_finish(kattr, uattr, xdp.data, size, retval, duration);
170171
kfree(data);
171172
return ret;
172173
}

0 commit comments

Comments
 (0)