bpf: Fix overlapping copies in xdp and dynptr helpers
Subsystems (6)
cover 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
PATCH 1/3 bpf: Fix bpf_xdp_copy_buf() when the buffer overlaps the packet
bpf_xdp_copy_buf() moves data between a caller buffer and an xdp_buff one segment at a time (the linear part, then each frag) with memcpy(). bpf_xdp_load_bytes(), bpf_xdp_store_bytes() and bpf_dynptr_slice() with a buffer all use it.
The buffer does not have to be outside the packet. A program can pass a slice of the same packet, for example one from bpf_dynptr_slice_rdwr(). Then a piece that is copied early can overwrite bytes that a later piece still has to read. memcpy() is also not valid for overlapping memory.
Example: a 100 byte linear part, a 4096 byte frag0, a buffer at frag0 + 10 and a 64 byte load from offset 90. The first piece copies linear[90..100) to frag0[10..20), which destroys data that the second piece needs from frag0[0..54). A store fails in the same way.
Commit ab08863c58b1 ("bpf: Use memmove() to store bytes in an xdp_buff") fixed the case where the range is inside one segment. A range that crosses from the linear part into a frag still goes through here and is not covered.
Use memmove() for every piece. For a load, copy the piece that is in the same segment as the buffer first. For a store, copy it last. This assumes that the frags do not overlap each other.
The selftests added later in this series fail without this patch.
--- net/core/filter.c | 59 ++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 24 deletions(-)
net/core/filter.c +35 -24
@@ -4210,47 +4210,58 @@ static const struct bpf_func_proto bpf_xdp_adjust_head_proto = {void bpf_xdp_copy_buf (struct xdp_buff *xdp,unsigned long off,void *buf,unsigned long len,bool flush) { -unsigned long ptr_len, ptr_off =0 ; +unsigned long ptr_len, ptr_off, o, l;skb_frag_t *next_frag, *end_frag;struct skb_shared_info *sinfo; +u8 *ptr_buf, *b, *orig = buf;void *src, *dst; -u8 *ptr_buf; +int pass;if (likely (xdp->data_end - xdp->data >= off + len)) { src = flush ? buf : xdp->data + off; dst = flush ? xdp->data + off : buf; -memcpy (dst, src, len); +memmove (dst, src, len);return ; } sinfo =xdp_get_shared_info_from_buff (xdp); end_frag = &sinfo->frags[sinfo->nr_frags]; - next_frag = &sinfo->frags[0 ]; - ptr_len = xdp->data_end - xdp->data; - ptr_buf = xdp->data; +for (pass =0 ; pass <2 ; pass++) { + o = off; + l = len; + b = buf; + ptr_off =0 ; + ptr_len = xdp->data_end - xdp->data; + ptr_buf = xdp->data; + next_frag = &sinfo->frags[0 ]; + +while (true) { +if (o < ptr_off + ptr_len) { +unsigned long copy_off = o - ptr_off; +unsigned long copy_len =min (l, ptr_len - copy_off); +bool self = orig >= ptr_buf && orig < ptr_buf + ptr_len; +bool turn = pass == flush; + +if (self == turn) { + src = flush ? b : ptr_buf + copy_off; + dst = flush ? ptr_buf + copy_off : b; +memmove (dst, src, copy_len); + } -while (true) { -if (off < ptr_off + ptr_len) { -unsigned long copy_off = off - ptr_off; -unsigned long copy_len =min (len, ptr_len - copy_off); + o += copy_len; + l -= copy_len; + b += copy_len; + } - src = flush ? buf : ptr_buf + copy_off; - dst = flush ? ptr_buf + copy_off : buf; -memcpy (dst, src, copy_len); +if (!l || next_frag == end_frag) +break ; - off += copy_len; - len -= copy_len; - buf += copy_len; + ptr_off += ptr_len; + ptr_buf =skb_frag_address (next_frag); + ptr_len =skb_frag_size (next_frag); + next_frag++; } - -if (!len || next_frag == end_frag) -break ; - - ptr_off += ptr_len; - ptr_buf =skb_frag_address (next_frag); - ptr_len =skb_frag_size (next_frag); - next_frag++; } }
Signature · 1 line
2.54.0
Re: [PATCH bpf 1/3] bpf: Fix bpf_xdp_copy_buf() when the buffer overlaps the packet
Quoted 1 line · Ayushman Rout
(quoted diff) +1
-0+ for (pass = 0; pass < 2; pass++) {
[...]
Review
Quoted 4 lines
(quoted diff) +4
-0+ bool self = orig >= ptr_buf && orig < ptr_buf + ptr_len; + bool turn = pass == flush; + + if (self == turn) {
This is too clever for what it fixes.
Every load, store and bpf_xdp_output() that crosses into a frag now walks the frags twice, to look for a buf that is on the stack, in a map or in the perf buffer.
memmove() in bpf_xdp_load_bytes() and bpf_xdp_store_bytes() was a one liner. This is not.bpf_dynptr_copy() is there to move bytes within a packet, and it's the only realistic way to get here with a slice: when one range fits into a segment and the other one doesn't, it passes the slice of the first as a buffer to __bpf_dynptr_read() or __bpf_dynptr_write(). With patch 2 the chunk loop copes with any overlap. Use it for every copy within one packet and leave bpf_xdp_copy_buf() alone. bpf_dynptr_read() and bpf_dynptr_write() with a slice as a buffer and a range that crosses into a frag stay as they are. I don't think anyone needs that.
The read and write tests of patch 3 become bpf_dynptr_copy() tests where only one of the ranges crosses into frag0.
Something like this instead of patches 1 and 2 (compile tested only):
kernel/bpf/helpers.c +12 -4
@@ -3291,8 +3291,10 @@ __bpf_kfunc int bpf_dynptr_copy(const struct bpf_dynptr *dst_ptr, u64 dst_off, {const struct bpf_dynptr_kern *dst = (struct bpf_dynptr_kern *)dst_ptr;const struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr; +bool same = src->data == dst->data;void *src_slice, *dst_slice;char buf[256 ]; +bool rev;u64 off; src_slice =bpf_dynptr_slice (src_ptr, src_off,NULL , size);
@@ -3303,25 +3305,31 @@ __bpf_kfunc int bpf_dynptr_copy(const struct bpf_dynptr *dst_ptr, u64 dst_off,return 0 ; } -if (src_slice) +/* The other range is copied piece by piece. A slice of the same packet +* may overlap with it, so it cannot be the buffer for that. +*/ +if (src_slice && !same)return __bpf_dynptr_write (dst, dst_off, src_slice, size,0 ); -if (dst_slice) +if (dst_slice && !same)return __bpf_dynptr_read (dst_slice, size, src, src_off,0 );if (bpf_dynptr_check_off_len (dst, dst_off, size) ||bpf_dynptr_check_off_len (src, src_off, size))return -E2BIG ; +/* like memmove(): from the end if the destination is above the source */ + rev = same && dst->offset + dst_off > src->offset + src_off; off =0 ;while (off < size) {u64 chunk_sz =min_t (u64,sizeof (buf), size - off); +u64 pos = rev ? size - off - chunk_sz : off;int err; - err =__bpf_dynptr_read (buf, chunk_sz, src, src_off + off,0 ); + err =__bpf_dynptr_read (buf, chunk_sz, src, src_off + pos,0 );if (err)return err; - err =__bpf_dynptr_write (dst, dst_off + off, buf, chunk_sz,0 ); + err =__bpf_dynptr_write (dst, dst_off + pos, buf, chunk_sz,0 );if (err)return err;
pw-bot: cr
PATCH 2/3 bpf: Fix bpf_dynptr_copy() for overlapping ranges in one packet
In reply to Ayushman RoutWhen neither dynptr can be sliced, which happens when both ranges cross a segment boundary, bpf_dynptr_copy() copies through a 256 byte stack buffer. It copies the chunks from the start to the end.
If both dynptrs point into the same packet, the ranges overlap and the destination is above the source, a chunk written to the destination can overwrite source bytes that a later chunk has not read yet.
Example: a 1200 byte copy with the destination 100 bytes above the source. The first chunk writes source bytes 100..355, so the second chunk reads bytes 256..355 after they were overwritten.
In that case, copy the chunks from the end to the start. This only applies when src->data == dst->data and the destination is above the source. All other copies stay as they are.
The selftests added later in this series fail without this patch.
--- kernel/bpf/helpers.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
kernel/bpf/helpers.c +5 -2
@@ -3293,6 +3293,7 @@ __bpf_kfunc int bpf_dynptr_copy(const struct bpf_dynptr *dst_ptr, u64 dst_off,const struct bpf_dynptr_kern *src = (struct bpf_dynptr_kern *)src_ptr;void *src_slice, *dst_slice;char buf[256 ]; +bool rev;u64 off; src_slice =bpf_dynptr_slice (src_ptr, src_off,NULL , size);
@@ -3313,15 +3314,17 @@ __bpf_kfunc int bpf_dynptr_copy(const struct bpf_dynptr *dst_ptr, u64 dst_off, bpf_dynptr_check_off_len(src, src_off, size))return -E2BIG ; + rev = src->data == dst->data && dst->offset + dst_off > src->offset + src_off; off =0 ;while (off < size) {u64 chunk_sz =min_t (u64,sizeof (buf), size - off); +u64 pos = rev ? size - off - chunk_sz : off;int err; - err =__bpf_dynptr_read (buf, chunk_sz, src, src_off + off,0 ); + err =__bpf_dynptr_read (buf, chunk_sz, src, src_off + pos,0 );if (err)return err; - err =__bpf_dynptr_write (dst, dst_off + off, buf, chunk_sz,0 ); + err =__bpf_dynptr_write (dst, dst_off + pos, buf, chunk_sz,0 );if (err)return err;
Signature · 1 line
2.54.0
PATCH 3/3 selftests/bpf: Test overlapping dynptr copies on xdp frags
In reply to Ayushman RoutAdd four dynptr tests. Three use an xdp packet with frags, and in each the range goes from the linear part into frag0:
- test_dynptr_copy_xdp_overlap: bpf_dynptr_copy() of 1200 bytes with the destination 100 bytes above the source.
- test_dynptr_read_xdp_overlap: bpf_dynptr_read() into a slice of the same packet.
- test_dynptr_write_xdp_overlap: bpf_dynptr_write() from a slice of the same packet.
The fourth, test_dynptr_skb_load_overlap, reads a range that spans the head and the frags of an skb. It passes with and without the fixes and checks that skbs are not affected.
The new tests set err when the data is wrong. The existing test_dynptr_copy_xdp does not, so it can pass on corrupted data.
Without the fixes to bpf_xdp_copy_buf() and bpf_dynptr_copy(), the three xdp tests fail. With them, all four tests pass.
--- .../testing/selftests/bpf/prog_tests/dynptr.c | 4 + .../selftests/bpf/progs/dynptr_success.c | 128 ++++++++++++++++++ 2 files changed, 132 insertions(+)
tools/testing/selftests/bpf/prog_tests/dynptr.c +4 -0
@@ -22,6 +22,10 @@ static struct { {"test_dynptr_data", SETUP_SYSCALL_SLEEP}, {"test_dynptr_copy", SETUP_SYSCALL_SLEEP}, {"test_dynptr_copy_xdp", SETUP_XDP_PROG}, + {"test_dynptr_copy_xdp_overlap", SETUP_XDP_PROG}, + {"test_dynptr_read_xdp_overlap", SETUP_XDP_PROG}, + {"test_dynptr_write_xdp_overlap", SETUP_XDP_PROG}, + {"test_dynptr_skb_load_overlap", SETUP_SKB_PROG_NONLINEAR}, {"test_dynptr_memset_zero", SETUP_SYSCALL_SLEEP}, {"test_dynptr_memset_notzero", SETUP_SYSCALL_SLEEP}, {"test_dynptr_memset_zero_offset", SETUP_SYSCALL_SLEEP},
tools/testing/selftests/bpf/progs/dynptr_success.c +128 -0
134 lines not on this page · raw message
Signature · 1 line
2.54.0