Skip to content

Commit 772b9b1

Browse files
author
Alexei Starovoitov
committed
Merge branch 'bpf-fix-array-bounds-error-with-may_goto-and-add-selftest'
Jiayuan Chen says: ==================== bpf: Fix array bounds error with may_goto and add selftest Syzbot caught an array out-of-bounds bug [1]. It turns out that when the BPF program runs through do_misc_fixups(), it allocates an extra 8 bytes on the call stack, which eventually causes stack_depth to exceed 512. I was able to reproduce this issue probabilistically by enabling CONFIG_UBSAN=y and disabling CONFIG_BPF_JIT_ALWAYS_ON with the selfttest I provide in second patch(although it doesn't happen every time - I didn't dig deeper into why UBSAN behaves this way). Furthermore, if I set /proc/sys/net/core/bpf_jit_enable to 0 to disable the jit, a panic occurs, and the reason is the same, that bpf_func is assigned an incorrect address. [---[ end trace ]--- [Oops: general protection fault, probably for non-canonical address 0x100f0e0e0d090808: 0000 [#1] PREEMPT SMP NOPTI [Tainted: [W]=WARN, [O]=OOT_MODULE [RIP: 0010:bpf_test_run+0x1d2/0x360 [RSP: 0018:ffffafc7955178a0 EFLAGS: 00010246 [RAX: 100f0e0e0d090808 RBX: ffff8e9fdb2c4100 RCX: 0000000000000018 [RDX: 00000000002b5b18 RSI: ffffafc780497048 RDI: ffff8ea04d601700 [RBP: ffffafc780497000 R08: ffffafc795517a0c R09: 0000000000000000 [R10: 0000000000000000 R11: fefefefefefefeff R12: ffff8ea04d601700 [R13: ffffafc795517928 R14: ffffafc795517928 R15: 0000000000000000 [CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [CR2: 00007f181c064648 CR3: 00000001aa2be003 CR4: 0000000000770ef0 [DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 [DR3: 0000000000000000 DR6: 00000000fffe07f0 DR7: 0000000000000400 [PKRU: 55555554 [Call Trace: [ <TASK> [ ? die_addr+0x36/0x90 [ ? exc_general_protection+0x237/0x430 [ ? asm_exc_general_protection+0x26/0x30 [ ? bpf_test_run+0x1d2/0x360 [ ? bpf_test_run+0x10d/0x360 [ ? __link_object+0x12a/0x1e0 [ ? slab_build_skb+0x23/0x130 [ ? kmem_cache_alloc_noprof+0x2ea/0x3f0 [ ? sk_prot_alloc+0xc2/0x120 [ bpf_prog_test_run_skb+0x21b/0x590 [ __sys_bpf+0x340/0xa80 [ __x64_sys_bpf+0x1e/0x30 --- v2 -> v3: Optimized some code naming and conditional judgment logic. https://lore.kernel.org/bpf/[email protected]/T/ v1 -> v2: Directly reject loading programs with a stack size greater than 512 when jit disabled.(Suggested by Alexei Starovoitov) https://lore.kernel.org/bpf/[email protected]/T/ ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents a458544 + 72266ee commit 772b9b1

File tree

5 files changed

+102
-4
lines changed

5 files changed

+102
-4
lines changed

kernel/bpf/core.c

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,17 +2290,18 @@ void bpf_patch_call_args(struct bpf_insn *insn, u32 stack_depth)
22902290
insn->code = BPF_JMP | BPF_CALL_ARGS;
22912291
}
22922292
#endif
2293-
#else
2293+
#endif
2294+
22942295
static unsigned int __bpf_prog_ret0_warn(const void *ctx,
22952296
const struct bpf_insn *insn)
22962297
{
22972298
/* If this handler ever gets executed, then BPF_JIT_ALWAYS_ON
2298-
* is not working properly, so warn about it!
2299+
* is not working properly, or interpreter is being used when
2300+
* prog->jit_requested is not 0, so warn about it!
22992301
*/
23002302
WARN_ON_ONCE(1);
23012303
return 0;
23022304
}
2303-
#endif
23042305

23052306
bool bpf_prog_map_compatible(struct bpf_map *map,
23062307
const struct bpf_prog *fp)
@@ -2380,8 +2381,18 @@ static void bpf_prog_select_func(struct bpf_prog *fp)
23802381
{
23812382
#ifndef CONFIG_BPF_JIT_ALWAYS_ON
23822383
u32 stack_depth = max_t(u32, fp->aux->stack_depth, 1);
2384+
u32 idx = (round_up(stack_depth, 32) / 32) - 1;
23832385

2384-
fp->bpf_func = interpreters[(round_up(stack_depth, 32) / 32) - 1];
2386+
/* may_goto may cause stack size > 512, leading to idx out-of-bounds.
2387+
* But for non-JITed programs, we don't need bpf_func, so no bounds
2388+
* check needed.
2389+
*/
2390+
if (!fp->jit_requested &&
2391+
!WARN_ON_ONCE(idx >= ARRAY_SIZE(interpreters))) {
2392+
fp->bpf_func = interpreters[idx];
2393+
} else {
2394+
fp->bpf_func = __bpf_prog_ret0_warn;
2395+
}
23852396
#else
23862397
fp->bpf_func = __bpf_prog_ret0_warn;
23872398
#endif

kernel/bpf/verifier.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21903,6 +21903,13 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
2190321903
if (subprogs[cur_subprog + 1].start == i + delta + 1) {
2190421904
subprogs[cur_subprog].stack_depth += stack_depth_extra;
2190521905
subprogs[cur_subprog].stack_extra = stack_depth_extra;
21906+
21907+
stack_depth = subprogs[cur_subprog].stack_depth;
21908+
if (stack_depth > MAX_BPF_STACK && !prog->jit_requested) {
21909+
verbose(env, "stack size %d(extra %d) is too large\n",
21910+
stack_depth, stack_depth_extra);
21911+
return -EINVAL;
21912+
}
2190621913
cur_subprog++;
2190721914
stack_depth = subprogs[cur_subprog].stack_depth;
2190821915
stack_depth_extra = 0;

tools/testing/selftests/bpf/progs/bpf_misc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@
135135
#define __arch_arm64 __arch("ARM64")
136136
#define __arch_riscv64 __arch("RISCV64")
137137
#define __caps_unpriv(caps) __attribute__((btf_decl_tag("comment:test_caps_unpriv=" EXPAND_QUOTE(caps))))
138+
#define __load_if_JITed() __attribute__((btf_decl_tag("comment:load_mode=jited")))
139+
#define __load_if_no_JITed() __attribute__((btf_decl_tag("comment:load_mode=no_jited")))
138140

139141
/* Define common capabilities tested using __caps_unpriv */
140142
#define CAP_NET_ADMIN 12

tools/testing/selftests/bpf/progs/verifier_stack_ptr.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,4 +481,56 @@ l1_%=: r0 = 42; \
481481
: __clobber_all);
482482
}
483483

484+
SEC("socket")
485+
__description("PTR_TO_STACK stack size > 512")
486+
__failure __msg("invalid write to stack R1 off=-520 size=8")
487+
__naked void stack_check_size_gt_512(void)
488+
{
489+
asm volatile (" \
490+
r1 = r10; \
491+
r1 += -520; \
492+
r0 = 42; \
493+
*(u64*)(r1 + 0) = r0; \
494+
exit; \
495+
" ::: __clobber_all);
496+
}
497+
498+
#ifdef __BPF_FEATURE_MAY_GOTO
499+
SEC("socket")
500+
__description("PTR_TO_STACK stack size 512 with may_goto with jit")
501+
__load_if_JITed()
502+
__success __retval(42)
503+
__naked void stack_check_size_512_with_may_goto_jit(void)
504+
{
505+
asm volatile (" \
506+
r1 = r10; \
507+
r1 += -512; \
508+
r0 = 42; \
509+
*(u32*)(r1 + 0) = r0; \
510+
may_goto l0_%=; \
511+
r2 = 100; \
512+
l0_%=: \
513+
exit; \
514+
" ::: __clobber_all);
515+
}
516+
517+
SEC("socket")
518+
__description("PTR_TO_STACK stack size 512 with may_goto without jit")
519+
__load_if_no_JITed()
520+
__failure __msg("stack size 520(extra 8) is too large")
521+
__naked void stack_check_size_512_with_may_goto(void)
522+
{
523+
asm volatile (" \
524+
r1 = r10; \
525+
r1 += -512; \
526+
r0 = 42; \
527+
*(u32*)(r1 + 0) = r0; \
528+
may_goto l0_%=; \
529+
r2 = 100; \
530+
l0_%=: \
531+
exit; \
532+
" ::: __clobber_all);
533+
}
534+
#endif
535+
484536
char _license[] SEC("license") = "GPL";

tools/testing/selftests/bpf/test_loader.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#define TEST_TAG_JITED_PFX "comment:test_jited="
3838
#define TEST_TAG_JITED_PFX_UNPRIV "comment:test_jited_unpriv="
3939
#define TEST_TAG_CAPS_UNPRIV "comment:test_caps_unpriv="
40+
#define TEST_TAG_LOAD_MODE_PFX "comment:load_mode="
4041

4142
/* Warning: duplicated in bpf_misc.h */
4243
#define POINTER_VALUE 0xcafe4all
@@ -55,6 +56,11 @@ enum mode {
5556
UNPRIV = 2
5657
};
5758

59+
enum load_mode {
60+
JITED = 1 << 0,
61+
NO_JITED = 1 << 1,
62+
};
63+
5864
struct expect_msg {
5965
const char *substr; /* substring match */
6066
regex_t regex;
@@ -87,6 +93,7 @@ struct test_spec {
8793
int prog_flags;
8894
int mode_mask;
8995
int arch_mask;
96+
int load_mask;
9097
bool auxiliary;
9198
bool valid;
9299
};
@@ -406,6 +413,7 @@ static int parse_test_spec(struct test_loader *tester,
406413
bool collect_jit = false;
407414
int func_id, i, err = 0;
408415
u32 arch_mask = 0;
416+
u32 load_mask = 0;
409417
struct btf *btf;
410418
enum arch arch;
411419

@@ -580,10 +588,22 @@ static int parse_test_spec(struct test_loader *tester,
580588
if (err)
581589
goto cleanup;
582590
spec->mode_mask |= UNPRIV;
591+
} else if (str_has_pfx(s, TEST_TAG_LOAD_MODE_PFX)) {
592+
val = s + sizeof(TEST_TAG_LOAD_MODE_PFX) - 1;
593+
if (strcmp(val, "jited") == 0) {
594+
load_mask = JITED;
595+
} else if (strcmp(val, "no_jited") == 0) {
596+
load_mask = NO_JITED;
597+
} else {
598+
PRINT_FAIL("bad load spec: '%s'", val);
599+
err = -EINVAL;
600+
goto cleanup;
601+
}
583602
}
584603
}
585604

586605
spec->arch_mask = arch_mask ?: -1;
606+
spec->load_mask = load_mask ?: (JITED | NO_JITED);
587607

588608
if (spec->mode_mask == 0)
589609
spec->mode_mask = PRIV;
@@ -928,6 +948,7 @@ void run_subtest(struct test_loader *tester,
928948
bool unpriv)
929949
{
930950
struct test_subspec *subspec = unpriv ? &spec->unpriv : &spec->priv;
951+
int current_runtime = is_jit_enabled() ? JITED : NO_JITED;
931952
struct bpf_program *tprog = NULL, *tprog_iter;
932953
struct bpf_link *link, *links[32] = {};
933954
struct test_spec *spec_iter;
@@ -946,6 +967,11 @@ void run_subtest(struct test_loader *tester,
946967
return;
947968
}
948969

970+
if ((current_runtime & spec->load_mask) == 0) {
971+
test__skip();
972+
return;
973+
}
974+
949975
if (unpriv) {
950976
if (!can_execute_unpriv(tester, spec)) {
951977
test__skip();

0 commit comments

Comments
 (0)