bpf: fix a btf decl_tag bug when tagging a function
Subsystems (3)
PATCH 1/2 bpf: fix a btf decl_tag bug when tagging a function
syzbot reported a btf decl_tag bug with stack trace below:
general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] CPU: 0 PID: 3592 Comm: syz-executor914 Not tainted 5.16.0-syzkaller-11424-gb7892f7d5cb2 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:btf_type_vlen include/linux/btf.h:231 [inline] RIP: 0010:btf_decl_tag_resolve+0x83e/0xaa0 kernel/bpf/btf.c:3910 ... Call Trace: <TASK> btf_resolve+0x251/0x1020 kernel/bpf/btf.c:4198 btf_check_all_types kernel/bpf/btf.c:4239 [inline] btf_parse_type_sec kernel/bpf/btf.c:4280 [inline] btf_parse kernel/bpf/btf.c:4513 [inline] btf_new_fd+0x19fe/0x2370 kernel/bpf/btf.c:6047 bpf_btf_load kernel/bpf/syscall.c:4039 [inline] __sys_bpf+0x1cbb/0x5970 kernel/bpf/syscall.c:4679 __do_sys_bpf kernel/bpf/syscall.c:4738 [inline] __se_sys_bpf kernel/bpf/syscall.c:4736 [inline] __x64_sys_bpf+0x75/0xb0 kernel/bpf/syscall.c:4736 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae
The kasan error is triggered with an illegal BTF like below:
type 0: void
type 1: int
type 2: decl_tag to func type 3
type 3: func to func_proto type 8
The total number of types is 4 and the type 3 is illegal since its func_proto type is out of range.
Currently, the target type of decl_tag can be struct/union, var or func. Both struct/union and var implemented their own 'resolve' callback functions and hence handled properly in kernel.
But func type doesn't have 'resolve' callback function. When btf_decl_tag_resolve() tries to check func type, it tries to get vlen of its func_proto type, which triggered the above kasan error.
To fix the issue, btf_decl_tag_resolve() needs to do btf_func_check() before trying to accessing func_proto type.
In the current implementation, func type is checked with btf_func_check() in the main checking function btf_check_all_types(). To fix the above kasan issue, let us implement 'resolve' callback func type properly. The 'resolve' callback will be also called in btf_check_all_types() for func types.
--- kernel/bpf/btf.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-)
kernel/bpf/btf.c +21 -8
@@ -403,6 +403,9 @@ static struct btf_type btf_void;static int btf_resolve (struct btf_verifier_env *env,const struct btf_type *t,u32 type_id); +static int btf_func_check (struct btf_verifier_env *env, +const struct btf_type *t); +static bool btf_type_is_modifier (const struct btf_type *t) {/* Some of them is not strictly a C modifier
@@ -579,6 +582,7 @@ static bool btf_type_needs_resolve(const struct btf_type *t)btf_type_is_struct (t) ||btf_type_is_array (t) ||btf_type_is_var (t) || +btf_type_is_func (t) ||btf_type_is_decl_tag (t) ||btf_type_is_datasec (t); }
@@ -3533,9 +3537,24 @@ static s32 btf_func_check_meta(struct btf_verifier_env *env,return 0 ; } +static int btf_func_resolve (struct btf_verifier_env *env, +const struct resolve_vertex *v) +{ +const struct btf_type *t = v->t; +u32 next_type_id = t->type; +int err; + + err =btf_func_check (env, t); +if (err) +return err; + +env_stack_pop_resolved (env, next_type_id,0 ); +return 0 ; +} +static struct btf_kind_operations func_ops = { .check_meta = btf_func_check_meta, - .resolve = btf_df_resolve, + .resolve = btf_func_resolve, .check_member = btf_df_check_member, .check_kflag_member = btf_df_check_kflag_member, .log_details = btf_ref_type_log,
@@ -4156,7 +4175,7 @@ static bool btf_resolve_valid(struct btf_verifier_env *env,return !btf_resolved_type_id (btf, type_id) && !btf_resolved_type_size (btf, type_id); -if (btf_type_is_decl_tag (t)) +if (btf_type_is_decl_tag (t) ||btf_type_is_func (t))return btf_resolved_type_id (btf, type_id) && !btf_resolved_type_size (btf, type_id);
@@ -4246,12 +4265,6 @@ static int btf_check_all_types(struct btf_verifier_env *env)if (err)return err; } - -if (btf_type_is_func (t)) { - err =btf_func_check (env, t); -if (err) -return err; - } }return 0 ;
Signature · 1 line
2.30.2
PATCH 2/2 selftests/bpf: add a selftest for invalid func btf with btf decl_tag
Added a selftest similar to [1] which exposed a kernel bug. Without the fix in the previous patch, the similar kasan error will appear.
[1] https://lore.kernel.org/bpf/[email protected]/
--- tools/testing/selftests/bpf/prog_tests/btf.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+)
tools/testing/selftests/bpf/prog_tests/btf.c +19 -0
@@ -3938,6 +3938,25 @@ static struct btf_raw_test raw_tests[] = { .btf_load_err = true, .err_str = "Invalid component_idx", }, +{ + .descr = "decl_tag test #15, func, invalid func proto", + .raw_types = { +BTF_TYPE_INT_ENC (0 ,BTF_INT_SIGNED ,0 ,32 ,4 ),/* [1] */ +BTF_DECL_TAG_ENC (NAME_TBD ,3 ,0 ),/* [2] */ +BTF_FUNC_ENC (NAME_TBD ,8 ),/* [3] */ +BTF_END_RAW , + }, +BTF_STR_SEC ("\0tag\0func" ), + .map_type = BPF_MAP_TYPE_ARRAY, + .map_name = "tag_type_check_btf", + .key_size = sizeof(int), + .value_size = 4, + .key_type_id = 1, + .value_type_id = 1, + .max_entries = 1, + .btf_load_err = true, + .err_str = "Invalid type_id", +}, { .descr ="type_tag test #1" , .raw_types = {
Signature · 1 line
2.30.2
Re: [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag
Quoted 2 lines · Yonghong Song
Added a selftest similar to [1] which exposed a kernel bug. Without the fix in the previous patch, the similar kasan error will appear.
Quoted 40 lines · Yonghong Song
syzbot reported a btf
decl_tagbug with stack trace below:general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] CPU: 0 PID: 3592 Comm: syz-executor914 Not tainted 5.16.0-syzkaller-11424-gb7892f7d5cb2 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:btf_type_vlen include/linux/btf.h:231 [inline] RIP: 0010:btf_decl_tag_resolve+0x83e/0xaa0 kernel/bpf/btf.c:3910 ... Call Trace: <TASK> btf_resolve+0x251/0x1020 kernel/bpf/btf.c:4198 btf_check_all_types kernel/bpf/btf.c:4239 [inline] btf_parse_type_sec kernel/bpf/btf.c:4280 [inline] btf_parse kernel/bpf/btf.c:4513 [inline] btf_new_fd+0x19fe/0x2370 kernel/bpf/btf.c:6047 bpf_btf_load kernel/bpf/syscall.c:4039 [inline] __sys_bpf+0x1cbb/0x5970 kernel/bpf/syscall.c:4679 __do_sys_bpf kernel/bpf/syscall.c:4738 [inline] __se_sys_bpf kernel/bpf/syscall.c:4736 [inline] __x64_sys_bpf+0x75/0xb0 kernel/bpf/syscall.c:4736 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xaeThe kasan error is triggered with an illegal BTF like below:
type 0: void
type 1: int
type 2:decl_tagto func type 3
type 3: func tofunc_prototype 8
The total number of types is 4 and the type 3 is illegal since itsfunc_prototype is out of range.Currently, the target type of
decl_tagcan be struct/union, var or func. Both struct/union and var implemented their own 'resolve' callback functions and hence handled properly in kernel.
But func type doesn't have 'resolve' callback function. Whenbtf_decl_tag_resolve()tries to check func type, it tries to get vlen of itsfunc_prototype, which triggered the above kasan error.
Quoted 7 lines
To fix the issue,
btf_decl_tag_resolve()needs to dobtf_func_check()before trying to accessingfunc_prototype.
In the current implementation, func type is checked withbtf_func_check()in the main checking functionbtf_check_all_types(). To fix the above kasan issue, let us implement 'resolve' callback func type properly. The 'resolve' callback will be also called inbtf_check_all_types()for func types.
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf-next.git (master) by Alexei Starovoitov <[email protected]>:
Quoted 25 lines · On Thu, 3 Feb 2022 11:17:27 -0800 you wrote:
syzbot reported a btf
decl_tagbug with stack trace below:general protection fault, probably for non-canonical address 0xdffffc0000000000: 0000 [#1] PREEMPT SMP KASAN KASAN: null-ptr-deref in range [0x0000000000000000-0x0000000000000007] CPU: 0 PID: 3592 Comm: syz-executor914 Not tainted 5.16.0-syzkaller-11424-gb7892f7d5cb2 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 01/01/2011 RIP: 0010:btf_type_vlen include/linux/btf.h:231 [inline] RIP: 0010:btf_decl_tag_resolve+0x83e/0xaa0 kernel/bpf/btf.c:3910 ... Call Trace: <TASK> btf_resolve+0x251/0x1020 kernel/bpf/btf.c:4198 btf_check_all_types kernel/bpf/btf.c:4239 [inline] btf_parse_type_sec kernel/bpf/btf.c:4280 [inline] btf_parse kernel/bpf/btf.c:4513 [inline] btf_new_fd+0x19fe/0x2370 kernel/bpf/btf.c:6047 bpf_btf_load kernel/bpf/syscall.c:4039 [inline] __sys_bpf+0x1cbb/0x5970 kernel/bpf/syscall.c:4679 __do_sys_bpf kernel/bpf/syscall.c:4738 [inline] __se_sys_bpf kernel/bpf/syscall.c:4736 [inline] __x64_sys_bpf+0x75/0xb0 kernel/bpf/syscall.c:4736 do_syscall_x64 arch/x86/entry/common.c:50 [inline] do_syscall_64+0x35/0xb0 arch/x86/entry/common.c:80 entry_SYSCALL_64_after_hwframe+0x44/0xae
Quoted 1 line
[...]
Here is the summary with links:
- [bpf,1/2] bpf: fix a btf decl_tag bug when tagging a function
https://git.kernel.org/bpf/bpf-next/c/d7e7b42f4f95 - [bpf,2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag https://git.kernel.org/bpf/bpf-next/c/cf1a4cbce63b
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html