Commit b8bf2e82 authored by Leo Yan's avatar Leo Yan Committed by Greg Kroah-Hartman

bpf, libbpf, smatch: Fix potential NULL pointer dereference

[ Upstream commit 33bae185 ]

Based on the following report from Smatch, fix the potential NULL
pointer dereference check:

  tools/lib/bpf/libbpf.c:3493
  bpf_prog_load_xattr() warn: variable dereferenced before check 'attr'
  (see line 3483)

  3479 int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
  3480                         struct bpf_object **pobj, int *prog_fd)
  3481 {
  3482         struct bpf_object_open_attr open_attr = {
  3483                 .file           = attr->file,
  3484                 .prog_type      = attr->prog_type,
                                         ^^^^^^
  3485         };

At the head of function, it directly access 'attr' without checking
if it's NULL pointer. This patch moves the values assignment after
validating 'attr' and 'attr->file'.
Signed-off-by: default avatarLeo Yan <leo.yan@linaro.org>
Acked-by: default avatarYonghong Song <yhs@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
Signed-off-by: default avatarSasha Levin <sashal@kernel.org>
parent 925df798
...@@ -2897,10 +2897,7 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type, ...@@ -2897,10 +2897,7 @@ int bpf_prog_load(const char *file, enum bpf_prog_type type,
int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
struct bpf_object **pobj, int *prog_fd) struct bpf_object **pobj, int *prog_fd)
{ {
struct bpf_object_open_attr open_attr = { struct bpf_object_open_attr open_attr = {};
.file = attr->file,
.prog_type = attr->prog_type,
};
struct bpf_program *prog, *first_prog = NULL; struct bpf_program *prog, *first_prog = NULL;
enum bpf_attach_type expected_attach_type; enum bpf_attach_type expected_attach_type;
enum bpf_prog_type prog_type; enum bpf_prog_type prog_type;
...@@ -2913,6 +2910,9 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr, ...@@ -2913,6 +2910,9 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
if (!attr->file) if (!attr->file)
return -EINVAL; return -EINVAL;
open_attr.file = attr->file;
open_attr.prog_type = attr->prog_type;
obj = bpf_object__open_xattr(&open_attr); obj = bpf_object__open_xattr(&open_attr);
if (IS_ERR_OR_NULL(obj)) if (IS_ERR_OR_NULL(obj))
return -ENOENT; return -ENOENT;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment