Skip to content

bpf: Fix overlapping copies in xdp and dynptr helpers

Ayushman Rout bpf linux-kselftest lkml netdev 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 Latest3 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/3bpf: Fix bpf_xdp_copy_buf() when the buffer overlaps the packetnet/core/filter.c+35 -241 reply
2/3bpf: Fix bpf_dynptr_copy() for overlapping ranges in one packetkernel/bpf/helpers.c+5 -20 replies
3/3selftests/bpf: Test overlapping dynptr copies on xdp fragstools/testing/selftests/bpf/prog_tests/dynptr.c tools/testing/selftests/bpf/progs/dynptr_success.c+132 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [PATCH bpf 0/3] bpf: Fix overlapping copies in xdp and dynptr helpers

My patch "bpf: Use memmove() to load bytes from an skb and an xdp_buff" was applied as b93cf2c8a055. The AI review on it noted that the multi-frag path of bpf_xdp_copy_buf() still copies forward with memcpy() and breaks when the buffer is a slice of the same packet. This series fixes that.

Commit ab08863c58b1 ("bpf: Use memmove() to store bytes in an xdp_buff") fixed the single-segment store path and left the cross-fragment case open. Patch 1 fixes that case for both load and store.

Patch 1 fixes bpf_xdp_copy_buf().
Patch 2 fixes bpf_dynptr_copy(), which has the same problem when both ranges overlap in one packet and cross a segment boundary. Patch 3 adds selftests.

Testing: x86_64 QEMU VM, ./test_progs -t dynptr. With the fixes all four new tests pass. Without them the three xdp tests fail and the skb test passes.

I also read the other memcpy() calls that could touch packet memory. I found no overlap path, but this is not proven, so I left them
unchanged: the sock_ops header option copies, bpf_file_fetch_bytes(), bpf_flow_dissector_load_bytes() and bpf_lwt_seg6_store_bytes().

v2 of the earlier patch:
https://patch.msgid.link/[email protected]

Ayushman Rout (3):
bpf: Fix bpf_xdp_copy_buf() when the buffer overlaps the packet
bpf: Fix bpf_dynptr_copy() for overlapping ranges in one packet selftests/bpf: Test overlapping dynptr copies on xdp frags

 kernel/bpf/helpers.c                          |   7 +-
 net/core/filter.c                             |  59 ++++----
 .../testing/selftests/bpf/prog_tests/dynptr.c |   4 +
 .../selftests/bpf/progs/dynptr_success.c      | 128 ++++++++++++++++++
 4 files changed, 172 insertions(+), 26 deletions(-)
Signature · 1 line
2.54.0