bpf: Fix NULL pointer dereference in bpf_sock_from_file
Subsystems (4)
PATCH v2 1/1 bpf: Fix NULL pointer dereference in bpf_sock_from_file
bpf_sock_from_file should not dereference a NULL file pointer. KASAN detects a null-ptr-deref when eBPF tracing fentry/fexit programs are attached to points such as __mmap_region and file_map_prot_check kernel functions. This can result in a NULL file pointer flowing from context to the helper.
A minimal check before dereferencing can fix this.
---
v2:
- Added Reviewed-by from Emil Tsalapatis
- Fixed name in Signed-off-by
net/core/filter.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
net/core/filter.c +4 -1
@@ -12182,7 +12182,10 @@ const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { BPF_CALL_1(bpf_sock_from_file, struct file *, file) { -return (unsigned long )sock_from_file (file); +if (file) +return (unsigned long )sock_from_file (file); + +return (unsigned long )NULL ; }BTF_ID_LIST (bpf_sock_from_file_btf_ids)
Signature · 1 line
2.53.0
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
Pre-existing issues:
- [High] Incomplete validation allows ERR_PTR to cause kernel panic in bpf_sock_from_file.
- [High] Missing NULL pointer checks in BPF helpers taking ARG_PTR_TO_BTF_ID lead to kernel panics. --
--- Patch [1]: [PATCH bpf v2] bpf: Fix NULL pointer dereference in bpf_sock_from_file --- commit a54db842fd2086671fb2b904beab51f691e8cc8b
Author: Syeda Mahnur Asif <[email protected]>
bpf: Fix NULL pointer dereference in bpf_sock_from_file
Adds a NULL check to bpf_sock_from_file to prevent kernel panics when fentry/fexit programs pass a NULL file pointer.
net/core/filter.c const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { · line 12186
Quoted 11 lines
net/core/filter.c +2
-1@@ -12182,7 +12182,10 @@ const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { BPF_CALL_1(bpf_sock_from_file, struct file *, file) { -return (unsigned long )sock_from_file (file); +if (file) +return (unsigned long )sock_from_file (file);
[Severity: High]
This isn't a bug introduced by this patch, but does this check adequately handle error pointers?
If an eBPF fexit program attaches to a kernel function returning a struct file pointer that fails (e.g., returning ERR_PTR(-ENOENT)), the BPF verifier treats this as a valid PTR_TO_BTF_ID.
Since an error pointer is not NULL, this condition will pass, and sock_from_file() will attempt to dereference it, leading to a crash.
Could we use IS_ERR_OR_NULL(file) instead to catch these cases as well?
Also, while not introduced by this patch, are there similar missing NULL pointer checks in other BPF helpers taking ARG_PTR_TO_BTF_ID?
When fentry/fexit programs pass context arguments, the verifier lacks BTF nullability annotations for most parameters. This means helpers can receive and blindly dereference NULL pointers.
For instance, bpf_task_pt_regs() directly dereferences the task without checking:
kernel/trace/bpf_trace.c:bpf_task_pt_regs() {
return (unsigned long) task_pt_regs(task);
}Similarly, __bpf_get_task_stack() calls try_get_task_stack() which will dereference the task:
kernel/bpf/stackmap.c:__bpf_get_task_stack() {
if (!try_get_task_stack(task))
return -EFAULT;
}And bpf_xdp_get_buff_len() accesses the xdp buffer directly:
net/core/filter.c:bpf_xdp_get_buff_len() {
return xdp_get_buff_len(xdp);
}Do these helpers need similar runtime validation to prevent panics in the same execution context?
Quoted 5 lines
net/core/filter.c +2
-0+ +return (unsigned long )NULL ; }BTF_ID_LIST (bpf_sock_from_file_btf_ids)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 25 lines · Syeda Mahnur Asif
bpf_sock_from_fileshould not dereference a NULL file pointer. KASAN detects a null-ptr-deref when eBPF tracing fentry/fexit programs are attached to points such as__mmap_regionandfile_map_prot_checkkernel functions. This can result in a NULL file pointer flowing from context to the helper.A minimal check before dereferencing can fix this.
Fixes b60da4955f53 ("bpf: Only provide bpf_sock_from_file with CONFIG_NET")Reviewed-by Emil Tsalapatis <[email protected]>Signed-off-by Syeda Mahnur Asif <[email protected]>---
v2:
- Added Reviewed-by from Emil Tsalapatis
- Fixed name in Signed-off-bynet/core/filter.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/core/filter.c +0
-0
net/core/filter.c const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { · line 12186
Quoted 6 lines
net/core/filter.c +2
-1@@ -12182,7 +12182,10 @@ const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { BPF_CALL_1(bpf_sock_from_file, struct file *, file) { -return (unsigned long )sock_from_file (file); +if (file) +return (unsigned long )sock_from_file (file);
given this is a legacy ARG_PTR_TO_BTF_ID, it can be not just NULL, but also a) small value bogus "pointer" resulting from NULL + offset calculation and/or b) just random garbage casted to struct file pointer with bpf_core_cast() (maybe this hole we've closed with explicitly untrusted, not sure).
Either way, this is not a sufficient fix at least.
But I wonder if the proper fix is actually to mark this (and other similar) argument as explicitly requiring trusted PTR_TO_BTF_ID?
Thoughts?
Quoted 8 lines
net/core/filter.c +2
-1+ +return (unsigned long )NULL ; }BTF_ID_LIST (bpf_sock_from_file_btf_ids) --2.53.0
Kumar Kartikeya Dwivedi
maintainerQuoted 39 lines · Andrii Nakryiko
Quoted 25 lines · Syeda Mahnur Asif
bpf_sock_from_fileshould not dereference a NULL file pointer. KASAN detects a null-ptr-deref when eBPF tracing fentry/fexit programs are attached to points such as__mmap_regionandfile_map_prot_checkkernel functions. This can result in a NULL file pointer flowing from context to the helper.A minimal check before dereferencing can fix this.
Fixes b60da4955f53 ("bpf: Only provide bpf_sock_from_file with CONFIG_NET")Reviewed-by Emil Tsalapatis <[email protected]>Signed-off-by Syeda Mahnur Asif <[email protected]>---
v2:
- Added Reviewed-by from Emil Tsalapatis
- Fixed name in Signed-off-bynet/core/filter.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)net/core/filter.c +0
-0net/core/filter.c const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { · line 12186
Quoted 6 lines
net/core/filter.c +2
-1@@ -12182,7 +12182,10 @@ const struct bpf_func_proto bpf_skc_to_mptcp_sock_proto = { BPF_CALL_1(bpf_sock_from_file, struct file *, file) { -return (unsigned long )sock_from_file (file); +if (file) +return (unsigned long )sock_from_file (file);given this is a legacy
ARG_PTR_TO_BTF_ID, it can be not just NULL, but also a) small value bogus "pointer" resulting from NULL + offset calculation and/or b) just random garbage casted to struct file pointer withbpf_core_cast()(maybe this hole we've closed with explicitly untrusted, not sure).
Quoted 7 lines
Either way, this is not a sufficient fix at least.
But I wonder if the proper fix is actually to mark this (and other similar) argument as explicitly requiring trusted
PTR_TO_BTF_ID?Thoughts?
+1, should bite the bullet and accelerate deprecation of legacy PTR_TO_BTF_ID. We can mark specific arguments / fields in structs as trusted where there is a legitimate use case and the kernel guarantees their lifetime appropriately.
Quoted 8 lines
Quoted 8 lines
net/core/filter.c +2
-1+ +return (unsigned long )NULL ; }BTF_ID_LIST (bpf_sock_from_file_btf_ids) --2.53.0