Skip to contentSkip to conversation

BPF Streams - Fixes

PATCH Applied Kumar Kartikeya Dwivedi 4 participants 6 messages Jul 5, 2025 – Jul 7, 2025

Subsystems (2)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover BPF Streams - Fixes

This set contains some fixes for recently reported issues for BPF streams. Please check individual patches for details.

Kumar Kartikeya Dwivedi (2):
bpf: Fix bounds for bpf_prog_get_file_line linfo loop
bpf: Fix improper int-to-ptr cast in dump_stack_cb

 kernel/bpf/core.c   | 4 +++-
 kernel/bpf/stream.c | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

base-commit: 03fe01ddd1d8be7799419ea5e5f228a0186ae8c2

Signature · 1 line
2.47.1
author maintainer
#1 lore raw

PATCH 1/2 bpf: Fix bounds for bpf_prog_get_file_line linfo loop

We may overrun the bounds because linfo and jited_linfo are already advanced to prog->aux->linfo_idx, hence we must only iterate the remaining elements until we reach prog->aux->nr_linfo. Adjust the nr_linfo calculation to fix this. Reported in [0].

[0]: https://lore.kernel.org/bpf/[email protected]

Fixes 0e521efaf363 ("bpf: Add function to extract program source info")
---
 kernel/bpf/core.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/bpf/core.c +3 -1
index fe8a53f3c5bc..61613785bdd0 100644
@@ -3244,6 +3244,7 @@ int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char *
 	struct bpf_line_info *linfo;
 	void **jited_linfo;
 	struct btf *btf;
+	int nr_linfo;
 
 	btf = prog->aux->btf;
 	linfo = prog->aux->linfo;
@@ -3258,8 +3259,9 @@ int bpf_prog_get_file_line(struct bpf_prog *prog, unsigned long ip, const char *
 
 	insn_start = linfo[0].insn_off;
 	insn_end = insn_start + len;
+	nr_linfo = prog->aux->nr_linfo - prog->aux->linfo_idx;
 
-	for (int i = 0; i < prog->aux->nr_linfo &&
+	for (int i = 0; i < nr_linfo &&
 	     linfo[i].insn_off >= insn_start && linfo[i].insn_off < insn_end; i++) {
 		if (jited_linfo[i] >= (void *)ip)
 			break;
Signature · 1 line
2.47.1
maintainer Acked-by on v1 1/2
#2 lore raw

Re: [PATCH bpf-next v1 1/2] bpf: Fix bounds for bpf_prog_get_file_line linfo loop

Quoted 11 lines · Kumar Kartikeya Dwivedi

We may overrun the bounds because linfo and jited_linfo are already advanced to prog->aux->linfo_idx, hence we must only iterate the remaining elements until we reach prog->aux->nr_linfo. Adjust the nr_linfo calculation to fix this. Reported in [0].

[0]: https://lore.kernel.org/bpf/[email protected]

Fixes 0e521efaf363 ("bpf: Add function to extract program source info")

---

Nit: It would be nice to extend progs/stream.c, so that e.g.
cond_break exhaustion is reported from a subprogram. I checked it locally and everything works as expected.

author maintainer
#3 lore raw

PATCH 2/2 bpf: Fix improper int-to-ptr cast in dump_stack_cb

In reply to Kumar Kartikeya Dwivedi

On 32-bit platforms, we'll try to convert a u64 directly to a pointer type which is 32-bit, which causes the compiler to complain about cast from an integer of a different size to a pointer type. Cast to long before casting to the pointer type to match the pointer width.

Fixes d7c431cafcb4 ("bpf: Add dump_stack() analogue to print to BPF stderr")
---
 kernel/bpf/stream.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/stream.c +2 -2
index 8c842f845245..ab592db4a4bf 100644
@@ -498,11 +498,11 @@ static bool dump_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp)
 		if (ret < 0)
 			goto end;
 		ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n  %s @ %s:%d\n",
-						    (void *)ip, line, file, num);
+						    (void *)(long)ip, line, file, num);
 		return !ctxp->err;
 	}
 end:
-	ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip);
+	ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)(long)ip);
 	return !ctxp->err;
 }
 
Signature · 1 line
2.47.1
Acked-by Tested-by on v1 2/2
#4 lore raw

Re: [PATCH bpf-next v1 2/2] bpf: Fix improper int-to-ptr cast in dump_stack_cb

Quoted 9 lines · Kumar Kartikeya Dwivedi

On 32-bit platforms, we'll try to convert a u64 directly to a pointer type which is 32-bit, which causes the compiler to complain about cast from an integer of a different size to a pointer type. Cast to long before casting to the pointer type to match the pointer width.

Fixes d7c431cafcb4 ("bpf: Add dump_stack() analogue to print to BPF stderr")

Thanks.

Quoted 22 lines
---
 kernel/bpf/stream.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/bpf/stream.c +2 -2
index 8c842f845245..ab592db4a4bf 100644
@@ -498,11 +498,11 @@ static bool dump_stack_cb(void *cookie, u64 ip, u64 sp, u64 bp)
 		if (ret < 0)
 			goto end;
 		ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n  %s @ %s:%d\n",
-						    (void *)ip, line, file, num);
+						    (void *)(long)ip, line, file, num);
 		return !ctxp->err;
 	}
 end:
-	ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)ip);
+	ctxp->err = bpf_stream_stage_printk(ctxp->ss, "%pS\n", (void *)(long)ip);
 	return !ctxp->err;
 }
 
Signature · 1 line
~Randy
#5 lore raw
In reply to Kumar Kartikeya Dwivedi
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Hello:

This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:

Quoted 8 lines · On Fri, 4 Jul 2025 22:30:33 -0700 you wrote:

This set contains some fixes for recently reported issues for BPF streams. Please check individual patches for details.

Kumar Kartikeya Dwivedi (2):
bpf: Fix bounds for bpf_prog_get_file_line linfo loop
bpf: Fix improper int-to-ptr cast in dump_stack_cb

[...]

Here is the summary with links:
- [bpf-next,v1,1/2] bpf: Fix bounds for bpf_prog_get_file_line linfo loop

    https://git.kernel.org/bpf/bpf-next/c/116c8f474722
  - [bpf-next,v1,2/2] bpf: Fix improper int-to-ptr cast in dump_stack_cb
    https://git.kernel.org/bpf/bpf-next/c/bfa2bb9abd99

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html