Skip to contentSkip to conversation

bpf: Fix stream capacity leak and spurious -ENOSPC in bpf_stream_stage_commit

PATCH In review Khawar Ahemad 3 participants 4 messages Aug 25 BPF

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Fix stream capacity leak and spurious -ENOSPC in bpf_stream_stage_commit

bpf_stream_stage_commit() accounts the staged string length against the stream capacity via bpf_stream_consume_capacity() before detaching the queued elements with llist_del_all().

If no elements are returned by llist_del_all() (for instance, when the staging list was empty or failed to allocate elements), the function returns early without releasing the consumed capacity. Because no elements are queued into stream->log, no reader will ever pop or release this capacity, leading to a permanent capacity leak.

Furthermore, if ss->len is 0, bpf_stream_consume_capacity() is invoked needlessly and fails with -ENOSPC if the stream is currently at capacity, spuriously rejecting a zero-byte commit.

Fix both issues by:
1. Returning early if ss->len is 0.
2. Rolling back the consumed capacity with bpf_stream_release_capacity() if llist_del_all() returns NULL.

Fixes 5ab154f1463a ("bpf: Introduce BPF standard streams")
---
 kernel/bpf/stream.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
kernel/bpf/stream.c +9 -3
index 7a5c3ac867..3ad58c8284 100644
@@ -350,15 +350,21 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
 	if (!stream)
 		return -EINVAL;
 
+	if (!ss->len)
+		return 0;
+
 	ret = bpf_stream_consume_capacity(stream, ss->len);
 	if (ret)
 		return ret;
 
 	list = llist_del_all(&ss->log);
-	head = tail = list;
-
-	if (!list)
+	if (!list) {
+		bpf_stream_release_capacity(stream, ss->len);
 		return 0;
+	}
+
+	head = list;
+	tail = list;
 	while (llist_next(list)) {
 		tail = llist_next(list);
 		list = tail;
Signature · 1 line
2.54.0 (Apple Git-157)
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Dead code and commit message mismatch regarding a structurally impossible capacity leak. - [Low]…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Dead code and commit message mismatch regarding a structurally impossible capacity leak.
- [Low] The patch inconsistently drops 0-byte stream commits when ss->len == 0. --

--- Patch [1]: [PATCH bpf-next] bpf: Fix stream capacity leak and spurious -ENOSPC in bpf_stream_stage_commit --- commit 55dd4746dd7b2e3d562a36b8b50cd0857f9d9bcc
Author: Khawar Ahemad <[email protected]>

bpf: Fix stream capacity leak and spurious -ENOSPC in bpf_stream_stage_commit

This patch updates bpf_stream_stage_commit() to return early if the staged length is 0, avoiding a spurious -ENOSPC. It also adds a rollback to release consumed capacity if the staging list is empty when detached.

kernel/bpf/stream.c int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, · line 354

Quoted 10 lines
kernel/bpf/stream.c +2 -0
index 7a5c3ac8676b9..3ad58c82846ba 100644
@@ -350,15 +350,21 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
 	if (!stream)
 		return -EINVAL;
 
+	if (!ss->len)
+		return 0;

[Severity: Low]
Does this early return introduce an inconsistency with 0-byte elements?

If a BPF program stages only 0-byte elements, this early return discards them entirely without transferring them to the stream. However, if a 0-byte element is staged alongside a >0-byte element, ss->len will be > 0, and the 0-byte element will be successfully pushed to the stream.

kernel/bpf/stream.c int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog, · line 364

Quoted 13 lines
kernel/bpf/stream.c +4 -3
+
 	ret = bpf_stream_consume_capacity(stream, ss->len);
 	if (ret)
 		return ret;
 
 	list = llist_del_all(&ss->log);
-	head = tail = list;
-
-	if (!list)
+	if (!list) {
+		bpf_stream_release_capacity(stream, ss->len);
 		return 0;
+	}

[Severity: Medium]
Is this rollback path actually reachable?

The commit message states this fixes a capacity leak if llist_del_all() returns NULL. But because of the new if (!ss->len) check above, execution only reaches here when ss->len > 0. Since ss->len is the sum of the lengths of elements successfully added to ss->log, having ss->len > 0 seems to guarantee that ss->log is not empty, meaning list will never be NULL.

Additionally, in the unpatched code, if ss->len was 0, bpf_stream_consume_capacity() consumed exactly 0 capacity. Returning early without releasing 0 capacity wouldn't have actually leaked anything.

Could the commit message be clarified, or is this dead code?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Khawar Ahemad

Hi Sashiko reviewer,

Thanks for the thoughtful analysis. Addressing both points:

1. Regarding 0-byte elements (Low):
A 0-byte element in BPF streams carries no payload and no delimiter (unlike NUL-terminated strings in userspace, streams are raw byte logs). When bpf_stream_read() encounters a 0-byte element, min(0, rem_len) is 0, copy_to_user() copies 0 bytes, and the element is immediately popped and freed. Staging and committing a purely 0-byte stage transfers zero bytes of data to readers; returning early when ss->len == 0 safely avoids unnecessary atomic operations and lock contention on stream->log.

2. Regarding the rollback path and commit description (Medium):
In normal sequential execution, ss->len > 0 implies ss->log is non-empty because ss->len is only incremented upon successful element enqueue. The if (!list) rollback check is defensive programming: in the unpatched code, if list was NULL, the function returned 0 without considering any capacity consumed. Adding bpf_stream_release_capacity() ensures that even under abnormal or future decoupled states, capacity accounting remains strictly symmetric with queue state.

The primary immediate functional fix is preventing spurious -ENOSPC:
when stream->capacity is at BPF_STREAM_MAX_CAPACITY, calling bpf_stream_consume_capacity(stream, 0) returned -ENOSPC due to the atomic_read(&stream->capacity) >= BPF_STREAM_MAX_CAPACITY check, incorrectly failing zero-byte stage commits.

Thanks,
Khawar Ahemad <[email protected]>

#3 lore raw
In reply to Khawar Ahemad
Quoted 14 lines · Khawar Ahemad

bpf_stream_stage_commit() accounts the staged string length against the stream capacity via bpf_stream_consume_capacity() before detaching the queued elements with llist_del_all().

If no elements are returned by llist_del_all() (for instance, when the staging list was empty or failed to allocate elements), the function returns early without releasing the consumed capacity. Because no elements are queued into stream->log, no reader will ever pop or release this capacity, leading to a permanent capacity leak.

Furthermore, if ss->len is 0, bpf_stream_consume_capacity() is invoked needlessly and fails with -ENOSPC if the stream is currently at capacity, spuriously rejecting a zero-byte commit.

Quoted 8 lines

Fix both issues by:
1. Returning early if ss->len is 0.
2. Rolling back the consumed capacity with bpf_stream_release_capacity() if llist_del_all() returns NULL.

Fixes 5ab154f1463a ("bpf: Introduce BPF standard streams")

---

Completely unnecessary.

pw-bot: cr

Quoted 32 lines
 kernel/bpf/stream.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
kernel/bpf/stream.c +9 -3
index 7a5c3ac867..3ad58c8284 100644
@@ -350,15 +350,21 @@ int bpf_stream_stage_commit(struct bpf_stream_stage *ss, struct bpf_prog *prog,
 	if (!stream)
 		return -EINVAL;
 
+	if (!ss->len)
+		return 0;
+
 	ret = bpf_stream_consume_capacity(stream, ss->len);
 	if (ret)
 		return ret;
 
 	list = llist_del_all(&ss->log);
-	head = tail = list;
-
-	if (!list)
+	if (!list) {
+		bpf_stream_release_capacity(stream, ss->len);
 		return 0;
+	}
+
+	head = list;
+	tail = list;
 	while (llist_next(list)) {
 		tail = llist_next(list);
 		list = tail;