libbpf: Report error when a negative kprobe offset is specified
PATCH v3 1/1 libbpf: Report error when a negative kprobe offset is specified
In attach_kprobe(), the parsing logic uses sscanf() to extract the target function name and offset from the section definition. Currently, if a user specifies a negative offset (e.g., SEC("kprobe/func+-100")), the input is not explicitly caught and reported as an error.
This commit updates the logic to explicitly notify the user when a negative integer is provided. To facilitate this check, the offset variable is changed from unsigned long to long so that sscanf() can accurately capture a negative input for evaluation.
If a negative offset is detected, the loader will now print an informative warning stating that the offset must be non-negative, and return -EINVAL.
Additionally, free(func) is called in this new error path to prevent a memory leak, as the function name string is dynamically allocated by sscanf().
---
Changes in v2 [1]:
- Corrected subject prefix to target "bpf-next" (Mykyta Yatsenko)
- Added "Fixes:" tag (Mykyta Yatsenko)
- Added "Acked-by:" tag from Mykyta Yatsenko
Changes in v1 [2]:
- Added omitted mailing list entries
[1]: https://lore.kernel.org/lkml/[email protected]/
[2]: https://lore.kernel.org/bpf/[email protected]/T/#t
tools/lib/bpf/libbpf.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +8 -1
@@ -12279,7 +12279,7 @@ bpf_program__attach_kprobe_multi_opts(const struct bpf_program *prog,static int attach_kprobe (const struct bpf_program *prog,long cookie,struct bpf_link **link) {DECLARE_LIBBPF_OPTS (bpf_kprobe_opts, opts); -unsigned long offset =0 ; +long offset =0 ;const char *func_name;char *func;int n;
@@ -12301,6 +12301,13 @@ static int attach_kprobe(const struct bpf_program *prog, long cookie, struct bpfpr_warn ("kprobe name is invalid: %s\n" , func_name);return -EINVAL ; } + +if (offset <0 ) { +free (func); +pr_warn ("kprobe offset must be a non-negative integer: %li\n" , offset); +return -EINVAL ; + } +if (opts.retprobe && offset !=0 ) {free (func);pr_warn ("kretprobes do not support offset specification\n" );
Signature · 1 line
2.51.0
This patch was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Hello:
This patch was applied to bpf/bpf-next.git (master) by Kumar Kartikeya Dwivedi <[email protected]>:
Quoted 11 lines · On Sat, 18 Apr 2026 23:09:44 -0400 you wrote:
In
attach_kprobe(), the parsing logic usessscanf()to extract the target function name and offset from the section definition. Currently, if a user specifies a negative offset (e.g.,SEC("kprobe/func+-100")), the input is not explicitly caught and reported as an error.This commit updates the logic to explicitly notify the user when a negative integer is provided. To facilitate this check, the offset variable is changed from unsigned long to long so that
sscanf()can accurately capture a negative input for evaluation.[...]
Here is the summary with links:
- [bpf-next,v3] libbpf: Report error when a negative kprobe offset is specified
https://git.kernel.org/bpf/bpf-next/c/ad35d8018669
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html