Skip to contentSkip to conversation

bpf: check attach_func_proto return type more carefully

PATCH In review Stanislav Fomichev 2 participants 3 messages Jul 6, 2022

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

PATCH 1/1 bpf: check attach_func_proto return type more carefully

Syzkaller reports the following crash:

RIP: 0010:check_return_code kernel/bpf/verifier.c:10575 [inline]
RIP: 0010:do_check kernel/bpf/verifier.c:12346 [inline]
RIP: 0010:do_check_common+0xb3d2/0xd250 kernel/bpf/verifier.c:14610

With the following reproducer:
bpf$PROG_LOAD_XDP(0x5, &(0x7f00000004c0)={0xd, 0x3, &(0x7f0000000000)=ANY=[@ANYBLOB="1800000000000019000000000000000095"], &(0x7f0000000300)='GPL\x00', 0x0, 0x0, 0x0, 0x0, 0x0, '\x00', 0x0, 0x2b, 0xffffffffffffffff, 0x8, 0x0, 0x0, 0x10, 0x0}, 0x80)

Because we don't enforce expected_attach_type for XDP programs, we end up in hitting 'if (prog->expected_attach_type == BPF_LSM_CGROUP' part in check_return_code and follow up with testing prog->aux->attach_func_proto->type, but prog->aux->attach_func_proto is NULL.

Let's add a new btf_func_returns_void() wrapper which is more defensive and use it in the places where we currently do '!->type' check.

Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")
Reported-by [email protected]
---
 include/linux/btf.h     | 5 +++++
 kernel/bpf/trampoline.c | 2 +-
 kernel/bpf/verifier.c   | 8 ++++----
 3 files changed, 10 insertions(+), 5 deletions(-)
include/linux/btf.h +5 -0
index 1bfed7fa0428..17ba7d27a8ad 100644
@@ -302,6 +302,11 @@ static inline u16 btf_func_linkage(const struct btf_type *t)
 	return BTF_INFO_VLEN(t->info);
 }
 
+static inline bool btf_func_returns_void(const struct btf_type *t)
+{
+	return t && !t->type;
+}
+
 static inline bool btf_type_kflag(const struct btf_type *t)
 {
 	return BTF_INFO_KFLAG(t->info);
kernel/bpf/trampoline.c +1 -1
index 6cd226584c33..9c4cb4c8a5fa 100644
@@ -400,7 +400,7 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
 	case BPF_TRACE_FEXIT:
 		return BPF_TRAMP_FEXIT;
 	case BPF_LSM_MAC:
-		if (!prog->aux->attach_func_proto->type)
+		if (btf_func_returns_void(prog->aux->attach_func_proto))
 			/* The function returns void, we cannot modify its
 			 * return value.
 			 */
kernel/bpf/verifier.c +4 -4
index df3ec6b05f05..e3ee6f70939b 100644
@@ -7325,7 +7325,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		break;
 	case BPF_FUNC_set_retval:
 		if (env->prog->expected_attach_type == BPF_LSM_CGROUP) {
-			if (!env->prog->aux->attach_func_proto->type) {
+			if (btf_func_returns_void(env->prog->aux->attach_func_proto)) {
 				/* Make sure programs that attach to void
 				 * hooks don't try to modify return value.
 				 */
@@ -10447,7 +10447,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 	if (!is_subprog &&
 	    (prog_type == BPF_PROG_TYPE_STRUCT_OPS ||
 	     prog_type == BPF_PROG_TYPE_LSM) &&
-	    !prog->aux->attach_func_proto->type)
+	    btf_func_returns_void(prog->aux->attach_func_proto))
 		return 0;
 
 	/* eBPF calling convention is such that R0 is used
@@ -10547,7 +10547,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 			 */
 			return 0;
 		}
-		if (!env->prog->aux->attach_func_proto->type) {
+		if (btf_func_returns_void(env->prog->aux->attach_func_proto)) {
 			/* Make sure programs that attach to void
 			 * hooks don't try to modify return value.
 			 */
@@ -10572,7 +10572,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 	if (!tnum_in(range, reg->var_off)) {
 		verbose_invalid_scalar(env, reg, &range, "program exit", "R0");
 		if (prog->expected_attach_type == BPF_LSM_CGROUP &&
-		    !prog->aux->attach_func_proto->type)
+		    btf_func_returns_void(prog->aux->attach_func_proto))
 			verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
#1 lore raw
Quoted 69 lines · Stanislav Fomichev

Syzkaller reports the following crash:

RIP: 0010:check_return_code kernel/bpf/verifier.c:10575 [inline]
RIP: 0010:do_check kernel/bpf/verifier.c:12346 [inline]
RIP: 0010:do_check_common+0xb3d2/0xd250 kernel/bpf/verifier.c:14610

With the following reproducer:
bpf$PROG_LOAD_XDP(0x5, &(0x7f00000004c0)={0xd, 0x3, &(0x7f0000000000)=ANY=[@ANYBLOB="1800000000000019000000000000000095"], &(0x7f0000000300)='GPL\x00', 0x0, 0x0, 0x0, 0x0, 0x0, '\x00', 0x0, 0x2b, 0xffffffffffffffff, 0x8, 0x0, 0x0, 0x10, 0x0}, 0x80)

Because we don't enforce expected_attach_type for XDP programs, we end up in hitting 'if (prog->expected_attach_type == BPF_LSM_CGROUP' part in check_return_code and follow up with testing prog->aux->attach_func_proto->type, but prog->aux->attach_func_proto is NULL.

Let's add a new btf_func_returns_void() wrapper which is more defensive and use it in the places where we currently do '!->type' check.

Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")
Reported-by [email protected]
---
 include/linux/btf.h     | 5 +++++
 kernel/bpf/trampoline.c | 2 +-
 kernel/bpf/verifier.c   | 8 ++++----
 3 files changed, 10 insertions(+), 5 deletions(-)
include/linux/btf.h +5 -0
index 1bfed7fa0428..17ba7d27a8ad 100644
@@ -302,6 +302,11 @@ static inline u16 btf_func_linkage(const struct btf_type *t)
 	return BTF_INFO_VLEN(t->info);
 }
 
+static inline bool btf_func_returns_void(const struct btf_type *t)
+{
+	return t && !t->type;
+}
+
 static inline bool btf_type_kflag(const struct btf_type *t)
 {
 	return BTF_INFO_KFLAG(t->info);
kernel/bpf/trampoline.c +1 -1
index 6cd226584c33..9c4cb4c8a5fa 100644
@@ -400,7 +400,7 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
 	case BPF_TRACE_FEXIT:
 		return BPF_TRAMP_FEXIT;
 	case BPF_LSM_MAC:
-		if (!prog->aux->attach_func_proto->type)
+		if (btf_func_returns_void(prog->aux->attach_func_proto))
 			/* The function returns void, we cannot modify its
 			 * return value.
 			 */
kernel/bpf/verifier.c +1 -1
index df3ec6b05f05..e3ee6f70939b 100644
@@ -7325,7 +7325,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
 		break;
 	case BPF_FUNC_set_retval:
 		if (env->prog->expected_attach_type == BPF_LSM_CGROUP) {
-			if (!env->prog->aux->attach_func_proto->type) {
+			if (btf_func_returns_void(env->prog->aux->attach_func_proto)) {
 				/* Make sure programs that attach to void
 				 * hooks don't try to modify return value.
 				 */

kernel/bpf/verifier.c static int check_return_code(struct bpf_verifier_env *env) · line 10451

Quoted 6 lines
kernel/bpf/verifier.c +1 -1
@@ -10447,7 +10447,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 	if (!is_subprog &&
 	    (prog_type == BPF_PROG_TYPE_STRUCT_OPS ||
 	     prog_type == BPF_PROG_TYPE_LSM) &&
-	    !prog->aux->attach_func_proto->type)
+	    btf_func_returns_void(prog->aux->attach_func_proto))
 		return 0;

It seems there is another problem here.
It returns early here for prog_type == BPF_PROG_TYPE_LSM. It should only do that for expected_attach_type != BPF_LSM_CGROUP.

Otherwise, the later verbose(env, "Note, BPF_LSM_CGROUP...") will not get a chance.

Quoted 12 lines
kernel/bpf/verifier.c +1 -1
 	/* eBPF calling convention is such that R0 is used
@@ -10547,7 +10547,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 			 */
 			return 0;
 		}
-		if (!env->prog->aux->attach_func_proto->type) {
+		if (btf_func_returns_void(env->prog->aux->attach_func_proto)) {
 			/* Make sure programs that attach to void
 			 * hooks don't try to modify return value.
 			 */

kernel/bpf/verifier.c static int check_return_code(struct bpf_verifier_env *env) · line 10574

Quoted 3 lines
kernel/bpf/verifier.c +0 -0
@@ -10572,7 +10572,7 @@ static int check_return_code(struct bpf_verifier_env *env)
 	if (!tnum_in(range, reg->var_off)) {
 		verbose_invalid_scalar(env, reg, &range, "program exit", "R0");
 		if (prog->expected_attach_type == BPF_LSM_CGROUP &&

I think the problem is more like missing
prog_type == BPF_PROG_TYPE_LSM [&& expected_attach_type == BPF_LSM_CGROUP] here instead of testing !attach_func_proto in all places.

Quoted 8 lines
kernel/bpf/verifier.c +1 -1
-		    !prog->aux->attach_func_proto->type)
+		    btf_func_returns_void(prog->aux->attach_func_proto))
 			verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.37.0.rc0.161.g10f37bed90-goog
#2 lore raw
Quoted 81 lines · Martin KaFai Lau
Quoted 69 lines · Stanislav Fomichev

Syzkaller reports the following crash:

RIP: 0010:check_return_code kernel/bpf/verifier.c:10575 [inline]
RIP: 0010:do_check kernel/bpf/verifier.c:12346 [inline]
RIP: 0010:do_check_common+0xb3d2/0xd250 kernel/bpf/verifier.c:14610

With the following reproducer:
bpf$PROG_LOAD_XDP(0x5, &(0x7f00000004c0)={0xd, 0x3, &(0x7f0000000000)=ANY=[@ANYBLOB="1800000000000019000000000000000095"], &(0x7f0000000300)='GPL\x00', 0x0, 0x0, 0x0, 0x0, 0x0, '\x00', 0x0, 0x2b, 0xffffffffffffffff, 0x8, 0x0, 0x0, 0x10, 0x0}, 0x80)

Because we don't enforce expected_attach_type for XDP programs, we end up in hitting 'if (prog->expected_attach_type == BPF_LSM_CGROUP' part in check_return_code and follow up with testing prog->aux->attach_func_proto->type, but prog->aux->attach_func_proto is NULL.

Let's add a new btf_func_returns_void() wrapper which is more defensive and use it in the places where we currently do '!->type' check.

Fixes 69fd337a975c ("bpf: per-cgroup lsm flavor")
Reported-by [email protected]
---
 include/linux/btf.h     | 5 +++++
 kernel/bpf/trampoline.c | 2 +-
 kernel/bpf/verifier.c   | 8 ++++----
 3 files changed, 10 insertions(+), 5 deletions(-)
include/linux/btf.h +5 -0
index 1bfed7fa0428..17ba7d27a8ad 100644
@@ -302,6 +302,11 @@ static inline u16 btf_func_linkage(const struct btf_type *t)
      return BTF_INFO_VLEN(t->info);
 }
 
+static inline bool btf_func_returns_void(const struct btf_type *t)
+{
+     return t && !t->type;
+}
+
 static inline bool btf_type_kflag(const struct btf_type *t)
 {
      return BTF_INFO_KFLAG(t->info);
kernel/bpf/trampoline.c +1 -1
index 6cd226584c33..9c4cb4c8a5fa 100644
@@ -400,7 +400,7 @@ static enum bpf_tramp_prog_type bpf_attach_type_to_tramp(struct bpf_prog *prog)
      case BPF_TRACE_FEXIT:
              return BPF_TRAMP_FEXIT;
      case BPF_LSM_MAC:
-             if (!prog->aux->attach_func_proto->type)
+             if (btf_func_returns_void(prog->aux->attach_func_proto))
                      /* The function returns void, we cannot modify its
                       * return value.
                       */
kernel/bpf/verifier.c +1 -1
index df3ec6b05f05..e3ee6f70939b 100644
@@ -7325,7 +7325,7 @@ static int check_helper_call(struct bpf_verifier_env *env, struct bpf_insn *insn
              break;
      case BPF_FUNC_set_retval:
              if (env->prog->expected_attach_type == BPF_LSM_CGROUP) {
-                     if (!env->prog->aux->attach_func_proto->type) {
+                     if (btf_func_returns_void(env->prog->aux->attach_func_proto)) {
                              /* Make sure programs that attach to void
                               * hooks don't try to modify return value.
                               */

kernel/bpf/verifier.c static int check_return_code(struct bpf_verifier_env *env) · line 10451

Quoted 6 lines
kernel/bpf/verifier.c +1 -1
@@ -10447,7 +10447,7 @@ static int check_return_code(struct bpf_verifier_env *env)
      if (!is_subprog &&
          (prog_type == BPF_PROG_TYPE_STRUCT_OPS ||
           prog_type == BPF_PROG_TYPE_LSM) &&
-         !prog->aux->attach_func_proto->type)
+         btf_func_returns_void(prog->aux->attach_func_proto))
              return 0;

It seems there is another problem here.
It returns early here for prog_type == BPF_PROG_TYPE_LSM. It should only do that for expected_attach_type != BPF_LSM_CGROUP.

Quoted 2 lines

Otherwise, the later verbose(env, "Note, BPF_LSM_CGROUP...") will not get a chance.

Ah, true, will add expected_attach_type check here as well, thanks!

Quoted 10 lines
Quoted 10 lines
kernel/bpf/verifier.c +1 -1
      /* eBPF calling convention is such that R0 is used
@@ -10547,7 +10547,7 @@ static int check_return_code(struct bpf_verifier_env *env)
                       */
                      return 0;
              }
-             if (!env->prog->aux->attach_func_proto->type) {
+             if (btf_func_returns_void(env->prog->aux->attach_func_proto)) {
                      /* Make sure programs that attach to void
                       * hooks don't try to modify return value.
Quoted 8 lines

kernel/bpf/verifier.c static int check_return_code(struct bpf_verifier_env *env) · line 10574

Quoted 5 lines
kernel/bpf/verifier.c +0 -0
                       */
@@ -10572,7 +10572,7 @@ static int check_return_code(struct bpf_verifier_env *env)
      if (!tnum_in(range, reg->var_off)) {
              verbose_invalid_scalar(env, reg, &range, "program exit", "R0");
              if (prog->expected_attach_type == BPF_LSM_CGROUP &&

I think the problem is more like missing
prog_type == BPF_PROG_TYPE_LSM [&& expected_attach_type == BPF_LSM_CGROUP] here instead of testing !attach_func_proto in all places.

SG!

Quoted 8 lines
Quoted 8 lines
kernel/bpf/verifier.c +1 -2
-                 !prog->aux->attach_func_proto->type)
+                 btf_func_returns_void(prog->aux->attach_func_proto))
                      verbose(env, "Note, BPF_LSM_CGROUP that attach to void LSM hooks can't modify return value!\n");
              return -EINVAL;
      }
--

2.37.0.rc0.161.g10f37bed90-goog