Skip to contentSkip to conversation

Fix for check_max_stack_depth

PATCH Applied Kumar Kartikeya Dwivedi 2 participants 4 messages Jul 5, 2023 – Jul 6, 2023

Subsystems (4)
lore
Share & tools
lore

Kumar Kartikeya Dwivedi

author maintainer
#0 lore raw

cover Fix for check_max_stack_depth

Fix for a bug in check_max_stack_depth which allows bypassing the 512-byte stack limit.

Kumar Kartikeya Dwivedi (2):
bpf: Fix max stack depth check for async callbacks selftests/bpf: Add selftest for check_stack_max_depth bug

 kernel/bpf/verifier.c                         |  5 ++-
 .../bpf/prog_tests/async_stack_depth.c        |  9 +++++
 .../selftests/bpf/progs/async_stack_depth.c   | 40 +++++++++++++++++++
 3 files changed, 52 insertions(+), 2 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/async_stack_depth.c
 create mode 100644 tools/testing/selftests/bpf/progs/async_stack_depth.c

base-commit: f7306acec9aae9893d15e745c8791124d42ab10a

Signature · 1 line
2.40.1
author maintainer
#1 lore raw

PATCH 1/2 bpf: Fix max stack depth check for async callbacks

The check_max_stack_depth pass happens after the verifier's symbolic execution, and attempts to walk the call graph of the BPF program, ensuring that the stack usage stays within bounds for all possible call chains. There are two cases to consider: bpf_pseudo_func and bpf_pseudo_call. In the former case, the callback pointer is loaded into a register, and is assumed that it is passed to some helper later which calls it (however there is no way to be sure), but the check remains conservative and accounts the stack usage anyway. For this particular case, asynchronous callbacks are skipped as they execute asynchronously when their corresponding event fires.

The case of bpf_pseudo_call is simpler and we know that the call is definitely made, hence the stack depth of the subprog is accounted for.

However, the current check still skips an asynchronous callback even if a bpf_pseudo_call was made for it. This is erroneous, as it will miss accounting for the stack usage of the asynchronous callback, which can be used to breach the maximum stack depth limit.

Fix this by only skipping asynchronous callbacks when the instruction is not a pseudo call to the subprog.

Fixes 7ddc80a476c2 ("bpf: Teach stack depth check about async callbacks.")
---
 kernel/bpf/verifier.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +3 -2
index 11e54dd8b6dd..930b5555cfd3 100644
@@ -5642,8 +5642,9 @@ static int check_max_stack_depth(struct bpf_verifier_env *env)
 				verbose(env, "verifier bug. subprog has tail_call and async cb\n");
 				return -EFAULT;
 			}
-			 /* async callbacks don't increase bpf prog stack size */
-			continue;
+			/* async callbacks don't increase bpf prog stack size unless called directly */
+			if (!bpf_pseudo_call(insn + i))
+				continue;
 		}
 		i = next_insn;
 
Signature · 1 line
2.40.1
author maintainer
#2 lore raw

PATCH 2/2 selftests/bpf: Add selftest for check_stack_max_depth bug

In reply to Kumar Kartikeya Dwivedi

Use the bpf_timer_set_callback helper to mark timer_cb as an async callback, and put a direct call to timer_cb in the main subprog.

As the check_stack_max_depth happens after the do_check pass, the order does not matter. Without the previous fix, the test passes successfully.

---
 .../bpf/prog_tests/async_stack_depth.c        |  9 +++++
 .../selftests/bpf/progs/async_stack_depth.c   | 40 +++++++++++++++++++
 2 files changed, 49 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/async_stack_depth.c
 create mode 100644 tools/testing/selftests/bpf/progs/async_stack_depth.c
tools/testing/selftests/bpf/prog_tests/async_stack_depth.c new file +9 -0
new file mode 100644
index 000000000000..118abc29b236
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <test_progs.h>
+
+#include "async_stack_depth.skel.h"
+
+void test_async_stack_depth(void)
+{
+	RUN_TESTS(async_stack_depth);
+}
tools/testing/selftests/bpf/progs/async_stack_depth.c new file +40 -0
new file mode 100644
index 000000000000..477ba950bb43
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+
+#include "bpf_misc.h"
+
+struct hmap_elem {
+	struct bpf_timer timer;
+};
+
+struct {
+	__uint(type, BPF_MAP_TYPE_HASH);
+	__uint(max_entries, 64);
+	__type(key, int);
+	__type(value, struct hmap_elem);
+} hmap SEC(".maps");
+
+__attribute__((noinline))
+static int timer_cb(void *map, int *key, struct bpf_timer *timer)
+{
+	volatile char buf[256] = {};
+	return buf[69];
+}
+
+SEC("tc")
+__failure __msg("combined stack size of 2 calls")
+int prog(struct __sk_buff *ctx)
+{
+	struct hmap_elem *elem;
+	volatile char buf[256] = {};
+
+	elem = bpf_map_lookup_elem(&hmap, &(int){0});
+	if (!elem)
+		return 0;
+
+	timer_cb(NULL, NULL, NULL);
+	return bpf_timer_set_callback(&elem->timer, timer_cb) + buf[0];
+}
+
+char _license[] SEC("license") = "GPL";
Signature · 1 line
2.40.1