All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function
@ 2022-02-03 19:17 Yonghong Song
  2022-02-03 19:17 ` [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag Yonghong Song
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Yonghong Song @ 2022-02-03 19:17 UTC (permalink / raw)
  To: bpf
  Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	kernel-team, syzbot+53619be9444215e785ed

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.

Reported-by: syzbot+53619be9444215e785ed@syzkaller.appspotmail.com
Fixes: b5ea834dde6b ("bpf: Support for new btf kind BTF_KIND_TAG")
Signed-off-by: Yonghong Song <yhs@fb.com>
---
 kernel/bpf/btf.c | 29 +++++++++++++++++++++--------
 1 file changed, 21 insertions(+), 8 deletions(-)

diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c
index e16dafeb2450..cf76e32a00da 100644
--- a/kernel/bpf/btf.c
+++ b/kernel/bpf/btf.c
@@ -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;
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag
  2022-02-03 19:17 [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Yonghong Song
@ 2022-02-03 19:17 ` Yonghong Song
  2022-02-03 20:52   ` Martin KaFai Lau
  2022-02-03 20:51 ` [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Martin KaFai Lau
  2022-02-03 21:20 ` patchwork-bot+netdevbpf
  2 siblings, 1 reply; 5+ messages in thread
From: Yonghong Song @ 2022-02-03 19:17 UTC (permalink / raw)
  To: bpf; +Cc: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

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/0000000000009b6eaa05d71a8c06@google.com/

Signed-off-by: Yonghong Song <yhs@fb.com>
---
 tools/testing/selftests/bpf/prog_tests/btf.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/tools/testing/selftests/bpf/prog_tests/btf.c b/tools/testing/selftests/bpf/prog_tests/btf.c
index 8ba53acf9eb4..1f20a27e8210 100644
--- a/tools/testing/selftests/bpf/prog_tests/btf.c
+++ b/tools/testing/selftests/bpf/prog_tests/btf.c
@@ -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 = {
-- 
2.30.2


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function
  2022-02-03 19:17 [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Yonghong Song
  2022-02-03 19:17 ` [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag Yonghong Song
@ 2022-02-03 20:51 ` Martin KaFai Lau
  2022-02-03 21:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: Martin KaFai Lau @ 2022-02-03 20:51 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann,
	kernel-team, syzbot+53619be9444215e785ed

On Thu, Feb 03, 2022 at 11:17:27AM -0800, Yonghong Song wrote:
> 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.
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag
  2022-02-03 19:17 ` [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag Yonghong Song
@ 2022-02-03 20:52   ` Martin KaFai Lau
  0 siblings, 0 replies; 5+ messages in thread
From: Martin KaFai Lau @ 2022-02-03 20:52 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann, kernel-team

On Thu, Feb 03, 2022 at 11:17:32AM -0800, Yonghong Song wrote:
> Added a selftest similar to [1] which exposed a kernel bug.
> Without the fix in the previous patch, the similar kasan error will appear.
Acked-by: Martin KaFai Lau <kafai@fb.com>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function
  2022-02-03 19:17 [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Yonghong Song
  2022-02-03 19:17 ` [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag Yonghong Song
  2022-02-03 20:51 ` [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Martin KaFai Lau
@ 2022-02-03 21:20 ` patchwork-bot+netdevbpf
  2 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+netdevbpf @ 2022-02-03 21:20 UTC (permalink / raw)
  To: Yonghong Song
  Cc: bpf, ast, andrii, daniel, kernel-team, syzbot+53619be9444215e785ed

Hello:

This series was applied to bpf/bpf-next.git (master)
by Alexei Starovoitov <ast@kernel.org>:

On Thu, 3 Feb 2022 11:17:27 -0800 you wrote:
> 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
> 
> [...]

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!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-02-03 21:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-03 19:17 [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Yonghong Song
2022-02-03 19:17 ` [PATCH bpf 2/2] selftests/bpf: add a selftest for invalid func btf with btf decl_tag Yonghong Song
2022-02-03 20:52   ` Martin KaFai Lau
2022-02-03 20:51 ` [PATCH bpf 1/2] bpf: fix a btf decl_tag bug when tagging a function Martin KaFai Lau
2022-02-03 21:20 ` patchwork-bot+netdevbpf

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.