Skip to contentSkip to conversation

bpf: Fix NULL pointer dereference in __btf_resolve_helper_id()

PATCH Superseded Peilin Ye 4 participants 7 messages Jul 14, 2020 – Jul 15, 2020

Subsystems (3)
lore
Share & tools
lore

Peilin Ye

author
#0 lore raw

PATCH 1/1 bpf: Fix NULL pointer dereference in __btf_resolve_helper_id()

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=5edd146856fd513747c1992442732e5a0e9ba355
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
index 30721f2c2d10..3e981b183fa4 100644
@@ -4088,7 +4088,7 @@ static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
 	const char *tname, *sym;
 	u32 btf_id, i;
 
-	if (IS_ERR(btf_vmlinux)) {
+	if (IS_ERR_OR_NULL(btf_vmlinux)) {
 		bpf_log(log, "btf_vmlinux is malformed\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.25.1
#1 lore raw
Quoted 17 lines · Peilin Ye

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=5edd146856fd513747c1992442732e5a0e9ba355
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +0 -0
index 30721f2c2d10..3e981b183fa4 100644

kernel/bpf/btf.c static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn, · line 4092

Quoted 6 lines
kernel/bpf/btf.c +1 -1
@@ -4088,7 +4088,7 @@ static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
        const char *tname, *sym;
        u32 btf_id, i;
 
-       if (IS_ERR(btf_vmlinux)) {
+       if (IS_ERR_OR_NULL(btf_vmlinux)) {
                bpf_log(log, "btf_vmlinux is malformed\n");

Can you please split IS_ERR and NULL cases and emit different messages to log? If the kernel is not built with btf_vmlinux, saying that it's "malformed" will confuse people. Thanks.

Quoted 5 lines
kernel/bpf/btf.c +0 -1
                return -EINVAL;
        }
--

2.25.1

author
#2 lore raw

PATCH v2 1/1 bpf: Fix NULL pointer dereference in __btf_resolve_helper_id()

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=5edd146856fd513747c1992442732e5a0e9ba355

---
Thank you for reviewing my patch! I am new to Linux kernel development; would the log message and errno be appropriate for this case?

Change in v2:
- Split NULL and IS_ERR cases.

 kernel/bpf/btf.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/btf.c +5 -0
index 30721f2c2d10..092116a311f4 100644
@@ -4088,6 +4088,11 @@ static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
 	const char *tname, *sym;
 	u32 btf_id, i;
 
+	if (!btf_vmlinux) {
+		bpf_log(log, "btf_vmlinux doesn't exist\n");
+		return -EINVAL;
+	}
+
 	if (IS_ERR(btf_vmlinux)) {
 		bpf_log(log, "btf_vmlinux is malformed\n");
 		return -EINVAL;
Signature · 1 line
2.25.1
Acked-by on v2 1/1
#3 lore raw
Quoted 11 lines · Peilin Ye

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=5edd146856fd513747c1992442732e5a0e9ba355

---
Thank you for reviewing my patch! I am new to Linux kernel development; would the log message and errno be appropriate for this case?

I think it's good enough, thanks for the fix.

Quoted 7 lines

Change in v2:
- Split NULL and IS_ERR cases.

 kernel/bpf/btf.c | 5 +++++
 1 file changed, 5 insertions(+)

[...]

#4 lore raw
Quoted 7 lines · Andrii Nakryiko
Quoted 5 lines · Peilin Ye

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=5edd146856fd513747c1992442732e5a0e9ba355

The link looks wrong?
Nothing in the stack trace indicates this issue.

Quoted 19 lines
Quoted 5 lines

---
Thank you for reviewing my patch! I am new to Linux kernel development; would the log message and errno be appropriate for this case?

I think it's good enough, thanks for the fix.

Quoted 7 lines

Change in v2:
- Split NULL and IS_ERR cases.

 kernel/bpf/btf.c | 5 +++++
 1 file changed, 5 insertions(+)

[...]

author
#5 lore raw

PATCH v3 1/1 bpf: Fix NULL pointer dereference in __btf_resolve_helper_id()

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

    https://syzkaller.appspot.com/bug?id=f823224ada908fa5c207902a5a62065e53ca0fcc

---
Sorry, I got the link wrong. Thank you for pointing that out.

Change in v3:
- Fix incorrect syzbot dashboard link.

Change in v2:
- Split NULL and IS_ERR cases.

 kernel/bpf/btf.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/btf.c +5 -0
index 30721f2c2d10..092116a311f4 100644
@@ -4088,6 +4088,11 @@ static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
 	const char *tname, *sym;
 	u32 btf_id, i;
 
+	if (!btf_vmlinux) {
+		bpf_log(log, "btf_vmlinux doesn't exist\n");
+		return -EINVAL;
+	}
+
 	if (IS_ERR(btf_vmlinux)) {
 		bpf_log(log, "btf_vmlinux is malformed\n");
 		return -EINVAL;
Signature · 6 lines
2.25.1

_______________________________________________
Linux-kernel-mentees mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees
maintainer
#6 lore raw
Quoted 7 lines · Peilin Ye

Prevent __btf_resolve_helper_id() from dereferencing btf_vmlinux as NULL. This patch fixes the following syzbot bug:

     https://syzkaller.appspot.com/bug?id=f823224ada908fa5c207902a5a62065e53ca0fcc

Looks good, applied, thanks! As far as I can tell all the other occurrences are gated behind btf_parse_vmlinux() where we also init struct_opts, etc.

So for bpf-next this would then end up looking like ...

int btf_resolve_helper_id(struct bpf_verifier_log *log,
const struct bpf_func_proto *fn, int arg)

{
        int id;

        if (fn->arg_type[arg] != ARG_PTR_TO_BTF_ID)
                return -EINVAL;
        id = fn->btf_id[arg];
        if (!id || !btf_vmlinux || id > btf_vmlinux->nr_types)
                return -EINVAL;
        return id;
}
Quoted 29 lines

---
Sorry, I got the link wrong. Thank you for pointing that out.

Change in v3:
- Fix incorrect syzbot dashboard link.

Change in v2:
- Split NULL and IS_ERR cases.

  kernel/bpf/btf.c | 5 +++++
  1 file changed, 5 insertions(+)
kernel/bpf/btf.c +5 -0
index 30721f2c2d10..092116a311f4 100644
@@ -4088,6 +4088,11 @@ static int __btf_resolve_helper_id(struct bpf_verifier_log *log, void *fn,
  	const char *tname, *sym;
  	u32 btf_id, i;
 
+	if (!btf_vmlinux) {
+		bpf_log(log, "btf_vmlinux doesn't exist\n");
+		return -EINVAL;
+	}
+
  	if (IS_ERR(btf_vmlinux)) {
  		bpf_log(log, "btf_vmlinux is malformed\n");
  		return -EINVAL;