Skip to content

Commit 7c81ad5

Browse files
tohojogregkh
authored andcommitted
bpf, test_run: Subtract size of xdp_frame from allowed metadata size
[ Upstream commit e558cca217790286e799a8baacd1610bda31b261 ] The xdp_frame structure takes up part of the XDP frame headroom, limiting the size of the metadata. However, in bpf_test_run, we don't take this into account, which makes it possible for userspace to supply a metadata size that is too large (taking up the entire headroom). If userspace supplies such a large metadata size in live packet mode, the xdp_update_frame_from_buff() call in xdp_test_run_init_page() call will fail, after which packet transmission proceeds with an uninitialised frame structure, leading to the usual Bad Stuff. The commit in the Fixes tag fixed a related bug where the second check in xdp_update_frame_from_buff() could fail, but did not add any additional constraints on the metadata size. Complete the fix by adding an additional check on the metadata size. Reorder the checks slightly to make the logic clearer and add a comment. Link: https://lore.kernel.org/r/fa2be179-bad7-4ee3-8668-4903d1853461@hust.edu.cn Fixes: b6f1f78 ("bpf, test_run: Fix packet size check for live packet mode") Reported-by: Yinhao Hu <dddddd@hust.edu.cn> Reported-by: Kaiyan Mei <M202472210@hust.edu.cn> Signed-off-by: Toke Høiland-Jørgensen <toke@redhat.com> Reviewed-by: Amery Hung <ameryhung@gmail.com> Link: https://lore.kernel.org/r/20260105114747.1358750-1-toke@redhat.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent d08b419 commit 7c81ad5

1 file changed

Lines changed: 13 additions & 5 deletions

File tree

net/bpf/test_run.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,8 +1174,6 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
11741174
batch_size = NAPI_POLL_WEIGHT;
11751175
else if (batch_size > TEST_XDP_MAX_BATCH)
11761176
return -E2BIG;
1177-
1178-
headroom += sizeof(struct xdp_page_head);
11791177
} else if (batch_size) {
11801178
return -EINVAL;
11811179
}
@@ -1188,16 +1186,26 @@ int bpf_prog_test_run_xdp(struct bpf_prog *prog, const union bpf_attr *kattr,
11881186
/* There can't be user provided data before the meta data */
11891187
if (ctx->data_meta || ctx->data_end > kattr->test.data_size_in ||
11901188
ctx->data > ctx->data_end ||
1191-
unlikely(xdp_metalen_invalid(ctx->data)) ||
11921189
(do_live && (kattr->test.data_out || kattr->test.ctx_out)))
11931190
goto free_ctx;
1194-
/* Meta data is allocated from the headroom */
1195-
headroom -= ctx->data;
11961191

11971192
meta_sz = ctx->data;
1193+
if (xdp_metalen_invalid(meta_sz) || meta_sz > headroom - sizeof(struct xdp_frame))
1194+
goto free_ctx;
1195+
1196+
/* Meta data is allocated from the headroom */
1197+
headroom -= meta_sz;
11981198
linear_sz = ctx->data_end;
11991199
}
12001200

1201+
/* The xdp_page_head structure takes up space in each page, limiting the
1202+
* size of the packet data; add the extra size to headroom here to make
1203+
* sure it's accounted in the length checks below, but not in the
1204+
* metadata size check above.
1205+
*/
1206+
if (do_live)
1207+
headroom += sizeof(struct xdp_page_head);
1208+
12011209
max_linear_sz = PAGE_SIZE - headroom - tailroom;
12021210
linear_sz = min_t(u32, linear_sz, max_linear_sz);
12031211

0 commit comments

Comments
 (0)