bpf, libbpf, smatch: Fix potential NULL pointer dereference
authorLeo Yan <leo.yan@linaro.org>
Tue, 2 Jul 2019 10:25:31 +0000 (18:25 +0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Wed, 3 Jul 2019 10:17:29 +0000 (12:17 +0200)
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: Leo Yan <leo.yan@linaro.org>
Acked-by: Yonghong Song <yhs@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
tools/lib/bpf/libbpf.c

index 6e6ebef11ba3492c25676231a2e6d5a2bfb2cd55..4907997289e9959632df40f51f507fcfe9d1a85c 100644 (file)
@@ -3872,10 +3872,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,
                        struct bpf_object **pobj, int *prog_fd)
 {
-       struct bpf_object_open_attr open_attr = {
-               .file           = attr->file,
-               .prog_type      = attr->prog_type,
-       };
+       struct bpf_object_open_attr open_attr = {};
        struct bpf_program *prog, *first_prog = NULL;
        enum bpf_attach_type expected_attach_type;
        enum bpf_prog_type prog_type;
@@ -3888,6 +3885,9 @@ int bpf_prog_load_xattr(const struct bpf_prog_load_attr *attr,
        if (!attr->file)
                return -EINVAL;
 
+       open_attr.file = attr->file;
+       open_attr.prog_type = attr->prog_type;
+
        obj = bpf_object__open_xattr(&open_attr);
        if (IS_ERR_OR_NULL(obj))
                return -ENOENT;