Skip to contentSkip to conversation

bpf: Check cfi_stubs before registering a struct_ops type.

PATCH Superseded [email protected] 3 participants 4 messages Feb 15, 2024 BPF

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: Check cfi_stubs before registering a struct_ops type.

From: Kui-Feng Lee <[email protected]>

Recently, cfi_stubs were introduced. However, existing struct_ops types that are not in the upstream may not be aware of this, resulting in kernel crashes. By rejecting struct_ops types that do not provide cfi_stubs during registration, these crashes can be avoided.

---
 kernel/bpf/bpf_struct_ops.c | 5 +++++
 1 file changed, 5 insertions(+)
kernel/bpf/bpf_struct_ops.c +5 -0
index 0d7be97a2411..e35958142dce 100644
@@ -302,6 +302,11 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
 	}
 	sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
 
+	if (!st_ops->cfi_stubs) {
+		pr_warn("The struct_ops %s has no cfi_stubs\n", st_ops->name);
+		return -EINVAL;
+	}
+
 	type_id = btf_find_by_name_kind(btf, st_ops->name,
 					BTF_KIND_STRUCT);
 	if (type_id < 0) {
Signature · 1 line
2.34.1
#1 lore raw
Quoted 17 lines · On 2/14/24 6:24 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Recently, cfi_stubs were introduced. However, existing struct_ops types that are not in the upstream may not be aware of this, resulting in kernel crashes. By rejecting struct_ops types that do not provide cfi_stubs during registration, these crashes can be avoided.

---
  kernel/bpf/bpf_struct_ops.c | 5 +++++
  1 file changed, 5 insertions(+)
kernel/bpf/bpf_struct_ops.c +0 -0
index 0d7be97a2411..e35958142dce 100644

kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc, · line 305

Quoted 4 lines
kernel/bpf/bpf_struct_ops.c +1 -0
@@ -302,6 +302,11 @@ int bpf_struct_ops_desc_init(struct bpf_struct_ops_desc *st_ops_desc,
  	}
  	sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
 
+	if (!st_ops->cfi_stubs) {

How about *(void **)(st_ops->cfi_stubs + moff) ? Does it need a NULL check?

Please add a test.

Quoted 7 lines
kernel/bpf/bpf_struct_ops.c +4 -0
+		pr_warn("The struct_ops %s has no cfi_stubs\n", st_ops->name);
+		return -EINVAL;
+	}
+
  	type_id = btf_find_by_name_kind(btf, st_ops->name,
  					BTF_KIND_STRUCT);
  	if (type_id < 0) {
#2 lore raw
Quoted 13 lines · Martin KaFai Lau
Quoted 12 lines · On 2/14/24 6:24 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Recently, cfi_stubs were introduced. However, existing struct_ops types that are not in the upstream may not be aware of this, resulting in kernel crashes. By rejecting struct_ops types that do not provide cfi_stubs during registration, these crashes can be avoided.

---
  kernel/bpf/bpf_struct_ops.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 11 lines

kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_desc_init(struct · line 305

Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +1 -0
index 0d7be97a2411..e35958142dce 100644
@@ -302,6 +302,11 @@ int bpf_struct_ops_desc_init(struct 
 bpf_struct_ops_desc *st_ops_desc,
       }
       sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
+    if (!st_ops->cfi_stubs) {

How about *(void **)(st_ops->cfi_stubs + moff) ? Does it need a NULL check?

This NULL check is necessary to prevent the crash but good to have.

Quoted 2 lines

Please add a test.

Got it!

Quoted 9 lines
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +4 -0
+        pr_warn("The struct_ops %s has no cfi_stubs\n", st_ops->name);
+        return -EINVAL;
+    }
+
       type_id = btf_find_by_name_kind(btf, st_ops->name,
                       BTF_KIND_STRUCT);
       if (type_id < 0) {
#3 lore raw
Quoted 28 lines · Kui-Feng Lee
Quoted 13 lines · Martin KaFai Lau
Quoted 12 lines · On 2/14/24 6:24 PM, [email protected] wrote:

From: Kui-Feng Lee <[email protected]>

Recently, cfi_stubs were introduced. However, existing struct_ops types that are not in the upstream may not be aware of this, resulting in kernel crashes. By rejecting struct_ops types that do not provide cfi_stubs during registration, these crashes can be avoided.

---
  kernel/bpf/bpf_struct_ops.c | 5 +++++

  1 file changed, 5 insertions(+)
Quoted 11 lines

kernel/bpf/bpf_struct_ops.c int bpf_struct_ops_desc_init(struct · line 305

Quoted 9 lines
kernel/bpf/bpf_struct_ops.c +1 -0
index 0d7be97a2411..e35958142dce 100644
@@ -302,6 +302,11 @@ int bpf_struct_ops_desc_init(struct 
 bpf_struct_ops_desc *st_ops_desc,
       }
       sprintf(value_name, "%s%s", VALUE_PREFIX, st_ops->name);
+    if (!st_ops->cfi_stubs) {

How about *(void **)(st_ops->cfi_stubs + moff) ? Does it need a NULL check?

Quoted 1 line

This NULL check is necessary to prevent the crash but good to have.

^^^ I mean "not necessary"

Quoted 15 lines
Quoted 3 lines

Please add a test.

Got it!

Quoted 9 lines
Quoted 8 lines
kernel/bpf/bpf_struct_ops.c +4 -0
+        pr_warn("The struct_ops %s has no cfi_stubs\n", st_ops->name);
+        return -EINVAL;
+    }
+
       type_id = btf_find_by_name_kind(btf, st_ops->name,
                       BTF_KIND_STRUCT);
       if (type_id < 0) {