Skip to contentSkip to conversation

bpf: Reject bpf_skb_output() from return-side tracing

PATCH In review Feng Yang 3 participants 5 messages Sep 22

Subsystems (6)
Kselftest BPF Tracing BPF selftests +2 more subsystems
lore
Share & tools
lore

Feng Yang

author
#0 lore raw

PATCH v2 1/2 bpf: Reject bpf_skb_output() from return-side tracing

From: Feng Yang <[email protected]>

BPF fexit programs run after the traced function returns, while their context still contains the original function argument values. A traced function is free to consume an skb argument before returning, so the pointer seen by fexit can already be stale.

The verifier checks that the first argument to bpf_skb_output() has the BTF type of struct sk_buff, but that does not establish its lifetime. bpf_skb_event_output() then dereferences skb->len and can trigger a use-after-free.

Do not expose bpf_skb_output() to tracing programs which can run after the target: fexit, fexit.multi, fsession and fsession.multi. Keep it available to fentry and other tracing attach types where it is already supported. fsession must be rejected because the same program runs on both entry and return and the verifier cannot prove that a helper call is entry-only.

---
v2: Add inline.

v1: https://lore.kernel.org/all/[email protected]/

---
 kernel/trace/bpf_trace.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
kernel/trace/bpf_trace.c +16 -0
index 29260951aa87..29c83e2938cd 100644
@@ -1339,6 +1339,20 @@ static inline bool is_trace_fsession(const struct bpf_prog *prog)
 		prog->expected_attach_type == BPF_TRACE_FSESSION_MULTI);
 }
 
+static inline bool tracing_prog_may_run_after_target(const struct bpf_prog *prog)
+{
+	/* The target may consume pointer arguments before these programs run. */
+	switch (prog->expected_attach_type) {
+	case BPF_TRACE_FEXIT:
+	case BPF_TRACE_FEXIT_MULTI:
+	case BPF_TRACE_FSESSION:
+	case BPF_TRACE_FSESSION_MULTI:
+		return true;
+	default:
+		return false;
+	}
+}
+
 static const struct bpf_func_proto *
 kprobe_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 {
@@ -1730,6 +1744,8 @@ tracing_prog_func_proto(enum bpf_func_id func_id, const struct bpf_prog *prog)
 	switch (func_id) {
 #ifdef CONFIG_NET
 	case BPF_FUNC_skb_output:
+		if (tracing_prog_may_run_after_target(prog))
+			return NULL;
 		return &bpf_skb_output_proto;
 	case BPF_FUNC_xdp_output:
 		return &bpf_xdp_output_proto;
Signature · 1 line
2.27.0
author
#1 lore raw

PATCH v2 2/2 selftests/bpf: Check bpf_skb_output() tracing restrictions

From: Feng Yang <[email protected]>

Add verifier coverage for the bpf_skb_output() tracing policy. Verify that an fentry program can still use the helper, while the equivalent fexit program is rejected at load time.

---
 .../bpf/progs/verifier_helper_restricted.c    | 32 +++++++++++++++++++
 1 file changed, 32 insertions(+)
tools/testing/selftests/bpf/progs/verifier_helper_restricted.c +32 -0
index 889c9b78b912..058e71927cd5 100644
@@ -3,6 +3,7 @@
 
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
 
 struct val {
@@ -17,6 +18,13 @@ struct {
 	__type(value, struct val);
 } map_spin_lock SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} perf_event_map SEC(".maps");
+
 SEC("kprobe")
 __description("bpf_ktime_get_coarse_ns is forbidden in BPF_PROG_TYPE_KPROBE")
 __failure __msg("program of this type cannot use helper bpf_ktime_get_coarse_ns")
@@ -165,4 +173,28 @@ l0_%=:	exit;						\
 	: __clobber_all);
 }
 
+SEC("fentry/skb_tx_error")
+__description("bpf_skb_output is allowed in BPF_TRACE_FENTRY")
+__success
+int BPF_PROG(skb_output_fentry, void *skb)
+{
+	__u64 meta = 0;
+
+	bpf_skb_output(skb, &perf_event_map, BPF_F_CURRENT_CPU,
+		       &meta, sizeof(meta));
+	return 0;
+}
+
+SEC("fexit/skb_tx_error")
+__description("bpf_skb_output is forbidden in BPF_TRACE_FEXIT")
+__failure __msg("program of this type cannot use helper bpf_skb_output")
+int BPF_PROG(skb_output_fexit, void *skb)
+{
+	__u64 meta = 0;
+
+	bpf_skb_output(skb, &perf_event_map, BPF_F_CURRENT_CPU,
+		       &meta, sizeof(meta));
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";
Signature · 1 line
2.27.0
#2 lore raw

Re: [PATCH v2 bpf-next 2/2] selftests/bpf: Check bpf_skb_output() tracing restrictions

There's a formatting issue in the second Signed-off-by line - it's missing the space between the name and email address. Should this be:
Quoted 6 lines

commit 586c31fe079529e232f3760545b476754b91501b

Author:     Feng Yang <[email protected]>
AuthorDate: Tue Sep 22 09:54:17 2026 +0800
Commit:     Kernel Patches Daemon <[email protected]>
CommitDate: Mon Sep 21 19:02:48 2026 -0700
Quoted 8 lines

selftests/bpf: Check bpf_skb_output() tracing restrictions

Add verifier coverage for the bpf_skb_output() tracing policy. Verify that an fentry program can still use the helper, while the equivalent fexit program is rejected at load time.

There's a formatting issue in the second Signed-off-by line - it's missing the space between the name and email address. Should this be:

instead of "Feng Yang<[email protected]>"?

Quoted 23 lines
tools/testing/selftests/bpf/progs/verifier_helper_restricted.c +8 -0
index 889c9b78b912e..058e71927cd5c 100644
@@ -3,6 +3,7 @@
 
 #include <linux/bpf.h>
 #include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
 #include "bpf_misc.h"
 
 struct val {
@@ -17,6 +18,13 @@ struct {
 	__type(value, struct val);
 } map_spin_lock SEC(".maps");
 
+struct {
+	__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY);
+	__uint(max_entries, 1);
+	__type(key, __u32);
+	__type(value, __u32);
+} perf_event_map SEC(".maps");
+

[ ... ]

Quoted 19 lines
tools/testing/selftests/bpf/progs/verifier_helper_restricted.c +15 -0
@@ -165,4 +173,28 @@ l0_%=:	exit;						\
 	: __clobber_all);
 }
 
+SEC("fentry/skb_tx_error")
+__description("bpf_skb_output is allowed in BPF_TRACE_FENTRY")
+__success
+int BPF_PROG(skb_output_fentry, void *skb)
+{
+	__u64 meta = 0;
+
+	bpf_skb_output(skb, &perf_event_map, BPF_F_CURRENT_CPU,
+		       &meta, sizeof(meta));
+	return 0;
+}
+
+SEC("fexit/skb_tx_error")
+__description("bpf_skb_output is forbidden in BPF_TRACE_FEXIT")
+__failure __msg("program of this type cannot use helper bpf_skb_output")

tools/testing/selftests/bpf/progs/verifier_helper_restricted.c l0_%=: exit; \ · line 200

Quoted 10 lines
tools/testing/selftests/bpf/progs/verifier_helper_restricted.c +9 -0
+int BPF_PROG(skb_output_fexit, void *skb)
+{
+	__u64 meta = 0;
+
+	bpf_skb_output(skb, &perf_event_map, BPF_F_CURRENT_CPU,
+		       &meta, sizeof(meta));
+	return 0;
+}
+
 char _license[] SEC("license") = "GPL";

The kernel change this tests (538d3dadf8d0e) gates bpf_skb_output() on four attach types in tracing_prog_may_run_after_target():

case BPF_TRACE_FEXIT:
case BPF_TRACE_FEXIT_MULTI:
case BPF_TRACE_FSESSION:
case BPF_TRACE_FSESSION_MULTI:

          return true;

Only BPF_TRACE_FEXIT gets a test case here. Would it make sense to add coverage for BPF_TRACE_FEXIT_MULTI, BPF_TRACE_FSESSION and BPF_TRACE_FSESSION_MULTI as well?

The fsession case in particular is the one whose rationale the parent commit message spends a paragraph on - that fsession must be rejected because the same program runs on both entry and return and the verifier cannot prove that a helper call is entry-only.

All three have libbpf section prefixes available (tools/lib/bpf/libbpf.c has "fsession+", "fsession.multi+", "fexit.multi+"), so the extra test cases would be copies of the existing fexit block with different SEC() strings.

Kumar Kartikeya Dwivedi raised a design concern about the overall approach in v2:

https://lore.kernel.org/all/[email protected]/

"Sorry, this is not an acceptable fix. This negates usage of the function in all fexit and fession attach points. Unless there is a simpler way to enumerate in which attach points this helper should be disabled, it might not be worth doing and leaving this be as is."

The concern is that disabling bpf_skb_output() in all fexit/fsession programs may be too broad. Since this test verifies the blanket restriction that was rejected, has this feedback been addressed?

---
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/35678199634

#3 lore raw
In reply to Feng Yang
Quoted 19 lines · Feng Yang

From: Feng Yang <[email protected]>

BPF fexit programs run after the traced function returns, while their context still contains the original function argument values. A traced function is free to consume an skb argument before returning, so the pointer seen by fexit can already be stale.

The verifier checks that the first argument to bpf_skb_output() has the BTF type of struct sk_buff, but that does not establish its lifetime. bpf_skb_event_output() then dereferences skb->len and can trigger a use-after-free.

Do not expose bpf_skb_output() to tracing programs which can run after the target: fexit, fexit.multi, fsession and fsession.multi. Keep it available to fentry and other tracing attach types where it is already supported. fsession must be rejected because the same program runs on both entry and return and the verifier cannot prove that a helper call is entry-only.

Quoted 8 lines

---

Sorry, this is not an acceptable fix. This negates usage of the function in all fexit and fession attach points. Unless there is a simpler way to enumerate in which attach points this helper should be disabled, it might not be worth doing and leaving this be as is.

pw-bot: cr

Quoted 1 line

[...]

author
#4 lore raw
Quoted 29 lines · Kumar Kartikeya Dwivedi
Quoted 19 lines · Feng Yang

From: Feng Yang <[email protected]>

BPF fexit programs run after the traced function returns, while their context still contains the original function argument values. A traced function is free to consume an skb argument before returning, so the pointer seen by fexit can already be stale.

The verifier checks that the first argument to bpf_skb_output() has the BTF type of struct sk_buff, but that does not establish its lifetime. bpf_skb_event_output() then dereferences skb->len and can trigger a use-after-free.

Do not expose bpf_skb_output() to tracing programs which can run after the target: fexit, fexit.multi, fsession and fsession.multi. Keep it available to fentry and other tracing attach types where it is already supported. fsession must be rejected because the same program runs on both entry and return and the verifier cannot prove that a helper call is entry-only.

Quoted 8 lines

---

Quoted 5 lines

Sorry, this is not an acceptable fix. This negates usage of the function in all fexit and fession attach points. Unless there is a simpler way to enumerate in which attach points this helper should be disabled, it might not be worth doing and leaving this be as is.

Thanks for your reply. That is indeed correct.

Moreover, the comment for bpf_skb_output in include/uapi/linux/bpf.h
reads: "This helper is similar to bpf_perf_event_output() but restricted to raw_tracepoint bpf programs.", yet this comment is actually outdated.

Quoted 1 line

pw-bot: cr