diff mbox series

[bpf] bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE

Message ID 20221102084034.3342995-1-pulehui@huaweicloud.com (mailing list archive)
State Accepted
Commit 34de8e6e0e1f66e431abf4123934a2581cb5f133
Delegated to: BPF
Headers show
Series [bpf] bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE | expand

Checks

Context Check Description
bpf/vmtest-bpf-PR pending PR summary
netdev/tree_selection success Clearly marked for bpf
netdev/fixes_present success Fixes tag present in non-next series
netdev/subject_prefix success Link
netdev/cover_letter success Single patches do not need cover letters
netdev/patch_count success Link
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 0 this patch: 0
netdev/cc_maintainers success CCed 13 of 13 maintainers
netdev/build_clang success Errors and warnings before: 0 this patch: 0
netdev/module_param success Was 0 now: 0
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success Fixes tag looks correct
netdev/build_allmodconfig_warn success Errors and warnings before: 0 this patch: 0
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/source_inline success Was 0 now: 0
bpf/vmtest-bpf-VM_Test-1 pending Logs for ${{ matrix.test }} on ${{ matrix.arch }} with ${{ matrix.toolchain }}
bpf/vmtest-bpf-VM_Test-2 pending Logs for ShellCheck
bpf/vmtest-bpf-VM_Test-3 fail Logs for build for s390x with gcc
bpf/vmtest-bpf-VM_Test-4 fail Logs for build for x86_64 with gcc
bpf/vmtest-bpf-VM_Test-5 fail Logs for build for x86_64 with llvm-16
bpf/vmtest-bpf-VM_Test-6 success Logs for llvm-toolchain
bpf/vmtest-bpf-VM_Test-7 success Logs for set-matrix

Commit Message

Pu Lehui Nov. 2, 2022, 8:40 a.m. UTC
From: Pu Lehui <pulehui@huawei.com>

When using bpftool to pin {PROG, MAP, LINK} without FILE,
segmentation fault will occur. The reson is that the lack
of FILE will cause strlen to trigger NULL pointer dereference.
The corresponding stacktrace is shown below:

do_pin
  do_pin_any
    do_pin_fd
      mount_bpffs_for_pin
        strlen(name) <- NULL pointer dereference

Fix it by adding validation to the common process.

Fixes: 75a1e792c335 ("tools: bpftool: Allow all prog/map handles for pinning objects")
Signed-off-by: Pu Lehui <pulehui@huawei.com>
---
 tools/bpf/bpftool/common.c | 3 +++
 1 file changed, 3 insertions(+)

Comments

Quentin Monnet Nov. 2, 2022, 10:51 a.m. UTC | #1
2022-11-02 16:40 UTC+0800 ~ Pu Lehui <pulehui@huaweicloud.com>
> From: Pu Lehui <pulehui@huawei.com>
> 
> When using bpftool to pin {PROG, MAP, LINK} without FILE,
> segmentation fault will occur. The reson is that the lack
> of FILE will cause strlen to trigger NULL pointer dereference.
> The corresponding stacktrace is shown below:
> 
> do_pin
>   do_pin_any
>     do_pin_fd
>       mount_bpffs_for_pin
>         strlen(name) <- NULL pointer dereference
> 
> Fix it by adding validation to the common process.
> 
> Fixes: 75a1e792c335 ("tools: bpftool: Allow all prog/map handles for pinning objects")
> Signed-off-by: Pu Lehui <pulehui@huawei.com>
> ---
>  tools/bpf/bpftool/common.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
> index e4d33bc8bbbf..653c130a0aaa 100644
> --- a/tools/bpf/bpftool/common.c
> +++ b/tools/bpf/bpftool/common.c
> @@ -302,6 +302,9 @@ int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
>  	int err;
>  	int fd;
>  
> +	if (!REQ_ARGS(3))
> +		return -EINVAL;
> +
>  	fd = get_fd(&argc, &argv);
>  	if (fd < 0)
>  		return fd;

Good catch, and thanks for the fix!

Reviewed-by: Quentin Monnet <quentin@isovalent.com>
patchwork-bot+netdevbpf@kernel.org Nov. 2, 2022, 11:10 a.m. UTC | #2
Hello:

This patch was applied to bpf/bpf.git (master)
by Daniel Borkmann <daniel@iogearbox.net>:

On Wed,  2 Nov 2022 16:40:34 +0800 you wrote:
> From: Pu Lehui <pulehui@huawei.com>
> 
> When using bpftool to pin {PROG, MAP, LINK} without FILE,
> segmentation fault will occur. The reson is that the lack
> of FILE will cause strlen to trigger NULL pointer dereference.
> The corresponding stacktrace is shown below:
> 
> [...]

Here is the summary with links:
  - [bpf] bpftool: Fix NULL pointer dereference when pin {PROG, MAP, LINK} without FILE
    https://git.kernel.org/bpf/bpf/c/34de8e6e0e1f

You are awesome, thank you!
diff mbox series

Patch

diff --git a/tools/bpf/bpftool/common.c b/tools/bpf/bpftool/common.c
index e4d33bc8bbbf..653c130a0aaa 100644
--- a/tools/bpf/bpftool/common.c
+++ b/tools/bpf/bpftool/common.c
@@ -302,6 +302,9 @@  int do_pin_any(int argc, char **argv, int (*get_fd)(int *, char ***))
 	int err;
 	int fd;
 
+	if (!REQ_ARGS(3))
+		return -EINVAL;
+
 	fd = get_fd(&argc, &argv);
 	if (fd < 0)
 		return fd;