Skip to contentSkip to conversation

bpf: btf: Fix a var size check in validator

PATCH In review Yichun Zhang (agentzh) 2 participants 2 messages Jan 8, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: btf: Fix a var size check in validator

The btf validator should croak when the variable size is larger than its type size, not less. The LLVM optimizer may use smaller sizes for the C type.

We ran into this issue with real-world BPF programs emitted by the latest version of Clang/LLVM.

Fixes 1dc92851849cc ("bpf: kernel side support for BTF Var and DataSec")
---
 kernel/bpf/btf.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +1 -1
index 9bdb03767db5..2a6967b13ce1 100644
@@ -3696,7 +3696,7 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,
 			return -EINVAL;
 		}
 
-		if (vsi->size < type_size) {
+		if (vsi->size > type_size) {
 			btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
 			return -EINVAL;
 		}
Signature · 1 line
2.17.2
#1 lore raw
Quoted 6 lines · Yichun Zhang (agentzh)

The btf validator should croak when the variable size is larger than its type size, not less. The LLVM optimizer may use smaller sizes for the C type.

We ran into this issue with real-world BPF programs emitted by the latest version of Clang/LLVM.

Could you give an example to show we have variable size is less than type_size?

Quoted 12 lines
Fixes 1dc92851849cc ("bpf: kernel side support for BTF Var and DataSec")
---
  kernel/bpf/btf.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/btf.c +0 -0
index 9bdb03767db5..2a6967b13ce1 100644

kernel/bpf/btf.c static int btf_datasec_resolve(struct btf_verifier_env *env, · line 3702

Quoted 8 lines
kernel/bpf/btf.c +1 -1
@@ -3696,7 +3696,7 @@ static int btf_datasec_resolve(struct btf_verifier_env *env,
  			return -EINVAL;
  		}
 
-		if (vsi->size < type_size) {
+		if (vsi->size > type_size) {
  			btf_verifier_log_vsi(env, v->t, vsi, "Invalid size");
  			return -EINVAL;
  		}

The fix is incorrect. We do have cases where variable size greater than type_size. For example,

$ cat t.c
struct t {
  int  g;
  char a[];
} v = {1, {'a', 'b', 'c', 0}};

The type (struct t) size is 4, but the variable size is 8.