Blob Blame History Raw
From: Alexei Starovoitov <ast@kernel.org>
Date: Fri, 31 Jan 2020 16:03:14 -0800
Subject: bpf: Fix modifier skipping logic
Patch-mainline: v5.6-rc1
Git-commit: 257af63d7f84f0672aa6a24b5511871f00741f19
References: bsc#1155518

Fix the way modifiers are skipped while walking pointers. Otherwise second
level dereferences of 'const struct foo *' will be rejected by the verifier.

Fixes: 9e15db66136a ("bpf: Implement accurate raw_tp context access via BTF")
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Yonghong Song <yhs@fb.com>
Link: https://lore.kernel.org/bpf/20200201000314.261392-1-ast@kernel.org
Acked-by: Gary Lin <glin@suse.com>

NOTE: This patch is modified to add the equivalent implementation of
      btf_type_skip_modifiers() in btf_struct_access().
---
 kernel/bpf/btf.c |    8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -3838,6 +3838,7 @@ again:
 
 		if (btf_type_is_ptr(mtype)) {
 			const struct btf_type *stype;
+			u32 id;
 
 			if (msize != size || off != moff) {
 				bpf_log(log,
@@ -3846,12 +3847,15 @@ again:
 				return -EACCES;
 			}
 
+			id = mtype->type;
 			stype = btf_type_by_id(btf_vmlinux, mtype->type);
 			/* skip modifiers */
-			while (btf_type_is_modifier(stype))
+			while (btf_type_is_modifier(stype)) {
+				id = stype->type;
 				stype = btf_type_by_id(btf_vmlinux, stype->type);
+			}
 			if (btf_type_is_struct(stype)) {
-				*next_btf_id = mtype->type;
+				*next_btf_id = id;
 				return PTR_TO_BTF_ID;
 			}
 		}