Skip to contentSkip to conversation

libbpf: Fix program log buffer size validation

PATCH Applied Luis Vieira 4 participants 4 messages Sep 15 – Sep 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 libbpf: Fix program log buffer size validation

bpf_program__set_log_buf() checks the existing prog->log_size instead of the incoming log_size argument when enforcing the UINT_MAX limit. As a result, an oversized value can be accepted.

The setter also accepts a non-NULL log_buf with a zero log_size, while bpf_prog_load() rejects mismatched log buffer and size values.

Validate the log buffer and size pair consistently with bpf_prog_load(), and check the supplied log_size against UINT_MAX.

---
Changes in v2:
- Target bpf-next instead of bpf and drop the Fixes tag.
- Drop the regression selftest.
- Validate log_buf and log_size consistently with bpf_prog_load().
- Link to v1: https://patch.msgid.link/[email protected]

To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Ihor Solodrai <[email protected]>
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
Cc: [email protected]
Cc: [email protected]

---
 tools/lib/bpf/libbpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +2 -2
index 613afae26519..f7a0517d8fa8 100644
@@ -9966,9 +9966,9 @@ const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_siz
 
 int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size)
 {
-	if (log_size && !log_buf)
+	if (!!log_buf != !!log_size)
 		return libbpf_err(-EINVAL);
-	if (prog->log_size > UINT_MAX)
+	if (log_size > UINT_MAX)
 		return libbpf_err(-EINVAL);
 	if (prog->obj->state >= OBJ_LOADED)
 		return libbpf_err(-EBUSY);

---
base-commit: 5ef40d69b38a93bc9951dadb1a15c85c597e1a40
change-id: 20260913-libbpf-log-buf-fix-49f5038caa5d

Best regards,

Signature · 1 line
Luis Vieira <[email protected]>
#1 lore raw
This looks like a bug fix. Should this include:
Quoted 6 lines

libbpf: Fix program log buffer size validation

bpf_program__set_log_buf() checks the existing prog->log_size instead of the incoming log_size argument when enforcing the UINT_MAX limit. As a result, an oversized value can be accepted.

Quoted 7 lines

The setter also accepts a non-NULL log_buf with a zero log_size, while bpf_prog_load() rejects mismatched log buffer and size values.

Validate the log buffer and size pair consistently with bpf_prog_load(), and check the supplied log_size against UINT_MAX.

This looks like a bug fix. Should this include:

Fixes 847b98421362 ("adding ci files")

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

Acked-by on v2 1/1
#2 lore raw
In reply to Luis Vieira
Quoted 18 lines · Luis Vieira

bpf_program__set_log_buf() checks the existing prog->log_size instead of the incoming log_size argument when enforcing the UINT_MAX limit. As a result, an oversized value can be accepted.

The setter also accepts a non-NULL log_buf with a zero log_size, while bpf_prog_load() rejects mismatched log buffer and size values.

Validate the log buffer and size pair consistently with bpf_prog_load(), and check the supplied log_size against UINT_MAX.

---
Changes in v2:
- Target bpf-next instead of bpf and drop the Fixes tag.
- Drop the regression selftest.
- Validate log_buf and log_size consistently with bpf_prog_load().
- Link to v1: https://patch.msgid.link/[email protected]

Quoted 14 lines

To: Andrii Nakryiko <[email protected]>
To: Eduard Zingerman <[email protected]>
To: Ihor Solodrai <[email protected]>
To: Alexei Starovoitov <[email protected]>
To: Daniel Borkmann <[email protected]>
To: Kumar Kartikeya Dwivedi <[email protected]>
To: Martin KaFai Lau <[email protected]>
To: Song Liu <[email protected]>
To: Yonghong Song <[email protected]>
To: Jiri Olsa <[email protected]>
To: Emil Tsalapatis <[email protected]>
Cc: [email protected]
Cc: [email protected]
---

Quoted 28 lines
 tools/lib/bpf/libbpf.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
tools/lib/bpf/libbpf.c +2 -3
index 613afae26519..f7a0517d8fa8 100644
@@ -9966,9 +9966,9 @@ const char *bpf_program__log_buf(const struct bpf_program *prog, size_t *log_siz
 
 int bpf_program__set_log_buf(struct bpf_program *prog, char *log_buf, size_t log_size)
 {
-	if (log_size && !log_buf)
+	if (!!log_buf != !!log_size)
 		return libbpf_err(-EINVAL);
-	if (prog->log_size > UINT_MAX)
+	if (log_size > UINT_MAX)
 		return libbpf_err(-EINVAL);
 	if (prog->obj->state >= OBJ_LOADED)
 		return libbpf_err(-EBUSY);
 
---

base-commit: 5ef40d69b38a93bc9951dadb1a15c85c597e1a40
change-id: 20260913-libbpf-log-buf-fix-49f5038caa5d

Best regards,

Signature · 1 line
Luis Vieira <[email protected]>
#3 lore raw
In reply to Luis Vieira
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Tue, 15 Sep 2026 15:13:35 -0300 you wrote:

bpf_program__set_log_buf() checks the existing prog->log_size instead of the incoming log_size argument when enforcing the UINT_MAX limit. As a result, an oversized value can be accepted.

The setter also accepts a non-NULL log_buf with a zero log_size, while bpf_prog_load() rejects mismatched log buffer and size values.

[...]

Here is the summary with links:
- [bpf-next,v2] libbpf: Fix program log buffer size validation

    https://git.kernel.org/bpf/bpf-next/c/8cca9a6bb681

You are awesome, thank you!

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