Skip to contentSkip to conversation

bpf: fix bpf_attr.attach_btf_id check

PATCH Applied Alexei Starovoitov 4 participants 4 messages Oct 18, 2019

Subsystems (4)
lore
Share & tools
lore

Alexei Starovoitov

author maintainer
#0 lore raw

PATCH 1/1 bpf: fix bpf_attr.attach_btf_id check

Only raw_tracepoint program type can have bpf_attr.attach_btf_id >= 0. Make sure to reject other program types that accidentally set it to non-zero.

Fixes ccfe29eb29c2 ("bpf: Add attach_btf_id attribute to program load")
---
 kernel/bpf/syscall.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
kernel/bpf/syscall.c +11 -6
index 523e3ac15a08..16ea3c0db4f6 100644
@@ -1570,6 +1570,17 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
 			   enum bpf_attach_type expected_attach_type,
 			   u32 btf_id)
 {
+	switch (prog_type) {
+	case BPF_PROG_TYPE_RAW_TRACEPOINT:
+		if (btf_id > BTF_MAX_TYPE)
+			return -EINVAL;
+		break;
+	default:
+		if (btf_id)
+			return -EINVAL;
+		break;
+	}
+
 	switch (prog_type) {
 	case BPF_PROG_TYPE_CGROUP_SOCK:
 		switch (expected_attach_type) {
@@ -1610,13 +1621,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
 		default:
 			return -EINVAL;
 		}
-	case BPF_PROG_TYPE_RAW_TRACEPOINT:
-		if (btf_id > BTF_MAX_TYPE)
-			return -EINVAL;
-		return 0;
 	default:
-		if (btf_id)
-			return -EINVAL;
 		return 0;
 	}
 }
Signature · 1 line
2.17.1
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Alexei Starovoitov

Only raw_tracepoint program type can have bpf_attr.attach_btf_id >= 0. Make sure to reject other program types that accidentally set it to non-zero.

Fixes ccfe29eb29c2 ("bpf: Add attach_btf_id attribute to program load")
Quoted 41 lines
---
  kernel/bpf/syscall.c | 17 +++++++++++------
  1 file changed, 11 insertions(+), 6 deletions(-)
kernel/bpf/syscall.c +11 -6
index 523e3ac15a08..16ea3c0db4f6 100644
@@ -1570,6 +1570,17 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
  			   enum bpf_attach_type expected_attach_type,
  			   u32 btf_id)
  {
+	switch (prog_type) {
+	case BPF_PROG_TYPE_RAW_TRACEPOINT:
+		if (btf_id > BTF_MAX_TYPE)
+			return -EINVAL;
+		break;
+	default:
+		if (btf_id)
+			return -EINVAL;
+		break;
+	}
+
  	switch (prog_type) {
  	case BPF_PROG_TYPE_CGROUP_SOCK:
  		switch (expected_attach_type) {
@@ -1610,13 +1621,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
  		default:
  			return -EINVAL;
  		}
-	case BPF_PROG_TYPE_RAW_TRACEPOINT:
-		if (btf_id > BTF_MAX_TYPE)
-			return -EINVAL;
-		return 0;
  	default:
-		if (btf_id)
-			return -EINVAL;
  		return 0;
  	}
  }
Acked-by on v1 1/1
#3 lore raw
In reply to Alexei Starovoitov
Quoted 2 lines · Alexei Starovoitov

Only raw_tracepoint program type can have bpf_attr.attach_btf_id >= 0.

typo: just > (code is actually correct, though)

Quoted 6 lines

Make sure to reject other program types that accidentally set it to non-zero.

Fixes ccfe29eb29c2 ("bpf: Add attach_btf_id attribute to program load")

---

Quoted 42 lines
 kernel/bpf/syscall.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
kernel/bpf/syscall.c +11 -7
index 523e3ac15a08..16ea3c0db4f6 100644
@@ -1570,6 +1570,17 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
                           enum bpf_attach_type expected_attach_type,
                           u32 btf_id)
 {
+       switch (prog_type) {
+       case BPF_PROG_TYPE_RAW_TRACEPOINT:
+               if (btf_id > BTF_MAX_TYPE)
+                       return -EINVAL;
+               break;
+       default:
+               if (btf_id)
+                       return -EINVAL;
+               break;
+       }
+
        switch (prog_type) {
        case BPF_PROG_TYPE_CGROUP_SOCK:
                switch (expected_attach_type) {
@@ -1610,13 +1621,7 @@ bpf_prog_load_check_attach(enum bpf_prog_type prog_type,
                default:
                        return -EINVAL;
                }
-       case BPF_PROG_TYPE_RAW_TRACEPOINT:
-               if (btf_id > BTF_MAX_TYPE)
-                       return -EINVAL;
-               return 0;
        default:
-               if (btf_id)
-                       return -EINVAL;
                return 0;
        }
 }
--

2.17.1