Skip to contentSkip to conversation

bpf, sockmap: Fix ring capacity invariant break in bpf_msg_push_data()

PATCH Superseded bo cao 2 participants 2 messages Nov 12

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

bo cao

author
#0 lore raw

PATCH v2 1/1 bpf, sockmap: Fix ring capacity invariant break in bpf_msg_push_data()

From: Bo Cao <[email protected]>

A push at msg->size on a full ring reads the length of the free slot. It is 0, so nothing is copied and sg.end moves onto sg.start. The ring looks empty and the next push overwrites live elements. The prog needs CAP_BPF and CAP_NET_ADMIN.

Fixes 15ab0548e310 ("bpf, sockmap: Several fixes to bpf_msg_push_data")

---
v1 review: https://lore.kernel.org/bpf/[email protected]

Changes since v1, per the review:
- adopted the minimal diff suggested in the review
- Fixes: corrected to 15ab0548e310, the commit that introduced the append path
- commit message reduced to the essentials

 net/core/filter.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
net/core/filter.c +4 -4
index 1f81a3dd6a5c..a742a4731a4b 100644
@@ -2883,6 +2883,10 @@ BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
 	if (start > offset + l)
 		return -EINVAL;
 
+	/* A push at the end of the msg works on the last element */
+	if (i == msg->sg.end && start != offset)
+		sk_msg_iter_var_prev(i);
+
 	space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
 
 	/* If no space available will fallback to copy, we need at
@@ -2908,8 +2912,6 @@ BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
 
 		raw = page_address(page);
 
-		if (i == msg->sg.end)
-			sk_msg_iter_var_prev(i);
 		psge = sk_msg_elem(msg, i);
 		front = start - offset;
 		back = psge->length - front;
@@ -2931,8 +2933,6 @@ BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
 	}
 
 	if (start - offset) {
-		if (i == msg->sg.end)
-			sk_msg_iter_var_prev(i);
 		psge = sk_msg_elem(msg, i);
 		rsge = sk_msg_elem_cpy(msg, i);
 		rsge_copy = sk_msg_elem_is_copy(msg, i);
Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
TL;DR: In bpf_msg_push_data(), a push at the end of a ring with one free slot may now take the copy path, possibly allocating a large GFP_ATOMIC buffer that can fail with -ENOMEM; behaviour change…
Quoted 1 line

bpf, sockmap: Fix ring capacity invariant break in bpf_msg_push_data()

TL;DR: In bpf_msg_push_data(), a push at the end of a ring with one free slot may now take the copy path, possibly allocating a large GFP_ATOMIC buffer that can fail with -ENOMEM; behaviour change may be unintended.

Quoted 5 lines
net/core/filter.c +0 -0
index d68b52150405..1bd59dbe4d11 100644

net/core/filter.c BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start, · line 2892

Quoted 10 lines
net/core/filter.c +4 -0
@@ -2883,6 +2883,10 @@ BPF_CALL_4(bpf_msg_push_data, struct sk_msg *, msg, u32, start,
 	if (start > offset + l)
 		return -EINVAL;
 
+	/* A push at the end of the msg works on the last element */
+	if (i == msg->sg.end && start != offset)
+		sk_msg_iter_var_prev(i);
+
 	space = MAX_MSG_FRAGS - sk_msg_elem_used(msg);
 
 	/* If no space available will fallback to copy, we need at

[ ... ]

Before this patch, a push at msg->sg.size with space == 1 read copy from the free slot (0) and took the split path. There psge keeps its whole length and rsge.length becomes 0, so the code goes through:

	if (i == msg->sg.end) {
		if (!rsge.length)
			goto place_new;

and only one slot is used.

After this patch, i has been moved to the last element and start != offset, so the existing check:

	if (!space || (space == 1 && start != offset))
		copy = msg->sg.data[i].length;

now sets copy to the full length of the last element. Does this push now take the copy path, allocating get_order(copy + len) with GFP_ATOMIC, memcpy()ing the whole last element and calling put_page() on the original page?

An element built from the socket page_frag can be up to 32KB, so could this turn a zero-copy push into a higher order allocation that can fail with -ENOMEM where it used to succeed?

The block comment says two slots are needed only when the push "falls inside an element", and a push at the end of the last element leaves rsge empty. Would it make sense to require two slots only when the split leaves a non-empty remainder, for example:

	if (!space || (space == 1 && start != offset && start < offset + l))
		copy = msg->sg.data[i].length;

where l is still the length of element i after the first loop? Otherwise, should the commit message say that the space == 1 push-at-end case now takes the copy path on purpose?

---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/43163306786