Skip to content

Commit 5c5240d

Browse files
puranjaymohanAlexei Starovoitov
authored andcommitted
bpf: Report arena faults to BPF stderr
Begin reporting arena page faults and the faulting address to BPF program's stderr, this patch adds support in the arm64 and x86-64 JITs, support for other archs can be added later. The fault handlers receive the 32 bit address in the arena region so the upper 32 bits of user_vm_start is added to it before printing the address. This is what the user would expect to see as this is what is printed by bpf_printk() is you pass it an address returned by bpf_arena_alloc_pages(); Signed-off-by: Puranjay Mohan <[email protected]> Acked-by: Yonghong Song <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 70f2354 commit 5c5240d

File tree

4 files changed

+176
-5
lines changed

4 files changed

+176
-5
lines changed

arch/arm64/net/bpf_jit_comp.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,18 +1066,53 @@ static void build_epilogue(struct jit_ctx *ctx, bool was_classic)
10661066
emit(A64_RET(A64_LR), ctx);
10671067
}
10681068

1069+
/*
1070+
* Metadata encoding for exception handling in JITed code.
1071+
*
1072+
* Format of `fixup` field in `struct exception_table_entry`:
1073+
*
1074+
* Bit layout of `fixup` (32-bit):
1075+
*
1076+
* +-----------+--------+-----------+-----------+----------+
1077+
* | 31-27 | 26-22 | 21 | 20-16 | 15-0 |
1078+
* | | | | | |
1079+
* | FIXUP_REG | Unused | ARENA_ACC | ARENA_REG | OFFSET |
1080+
* +-----------+--------+-----------+-----------+----------+
1081+
*
1082+
* - OFFSET (16 bits): Offset used to compute address for Load/Store instruction.
1083+
* - ARENA_REG (5 bits): Register that is used to calculate the address for load/store when
1084+
* accessing the arena region.
1085+
* - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region.
1086+
* - FIXUP_REG (5 bits): Destination register for the load instruction (cleared on fault) or set to
1087+
* DONT_CLEAR if it is a store instruction.
1088+
*/
1089+
1090+
#define BPF_FIXUP_OFFSET_MASK GENMASK(15, 0)
1091+
#define BPF_FIXUP_ARENA_REG_MASK GENMASK(20, 16)
1092+
#define BPF_ARENA_ACCESS BIT(21)
10691093
#define BPF_FIXUP_REG_MASK GENMASK(31, 27)
10701094
#define DONT_CLEAR 5 /* Unused ARM64 register from BPF's POV */
10711095

10721096
bool ex_handler_bpf(const struct exception_table_entry *ex,
10731097
struct pt_regs *regs)
10741098
{
10751099
int dst_reg = FIELD_GET(BPF_FIXUP_REG_MASK, ex->fixup);
1100+
s16 off = FIELD_GET(BPF_FIXUP_OFFSET_MASK, ex->fixup);
1101+
int arena_reg = FIELD_GET(BPF_FIXUP_ARENA_REG_MASK, ex->fixup);
1102+
bool is_arena = !!(ex->fixup & BPF_ARENA_ACCESS);
1103+
bool is_write = (dst_reg == DONT_CLEAR);
1104+
unsigned long addr;
1105+
1106+
if (is_arena) {
1107+
addr = regs->regs[arena_reg] + off;
1108+
bpf_prog_report_arena_violation(is_write, addr, regs->pc);
1109+
}
10761110

10771111
if (dst_reg != DONT_CLEAR)
10781112
regs->regs[dst_reg] = 0;
10791113
/* Skip the faulting instruction */
10801114
regs->pc += AARCH64_INSN_SIZE;
1115+
10811116
return true;
10821117
}
10831118

@@ -1087,6 +1122,9 @@ static int add_exception_handler(const struct bpf_insn *insn,
10871122
int dst_reg)
10881123
{
10891124
off_t ins_offset;
1125+
s16 off = insn->off;
1126+
bool is_arena;
1127+
int arena_reg;
10901128
unsigned long pc;
10911129
struct exception_table_entry *ex;
10921130

@@ -1100,6 +1138,9 @@ static int add_exception_handler(const struct bpf_insn *insn,
11001138
BPF_MODE(insn->code) != BPF_PROBE_ATOMIC)
11011139
return 0;
11021140

1141+
is_arena = (BPF_MODE(insn->code) == BPF_PROBE_MEM32) ||
1142+
(BPF_MODE(insn->code) == BPF_PROBE_ATOMIC);
1143+
11031144
if (!ctx->prog->aux->extable ||
11041145
WARN_ON_ONCE(ctx->exentry_idx >= ctx->prog->aux->num_exentries))
11051146
return -EINVAL;
@@ -1131,6 +1172,25 @@ static int add_exception_handler(const struct bpf_insn *insn,
11311172

11321173
ex->fixup = FIELD_PREP(BPF_FIXUP_REG_MASK, dst_reg);
11331174

1175+
if (is_arena) {
1176+
ex->fixup |= BPF_ARENA_ACCESS;
1177+
/*
1178+
* insn->src_reg/dst_reg holds the address in the arena region with upper 32-bits
1179+
* being zero because of a preceding addr_space_cast(r<n>, 0x0, 0x1) instruction.
1180+
* This address is adjusted with the addition of arena_vm_start (see the
1181+
* implementation of BPF_PROBE_MEM32 and BPF_PROBE_ATOMIC) before being used for the
1182+
* memory access. Pass the reg holding the unmodified 32-bit address to
1183+
* ex_handler_bpf.
1184+
*/
1185+
if (BPF_CLASS(insn->code) == BPF_LDX)
1186+
arena_reg = bpf2a64[insn->src_reg];
1187+
else
1188+
arena_reg = bpf2a64[insn->dst_reg];
1189+
1190+
ex->fixup |= FIELD_PREP(BPF_FIXUP_OFFSET_MASK, off) |
1191+
FIELD_PREP(BPF_FIXUP_ARENA_REG_MASK, arena_reg);
1192+
}
1193+
11341194
ex->type = EX_TYPE_BPF;
11351195

11361196
ctx->exentry_idx++;

arch/x86/net/bpf_jit_comp.c

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <linux/netdevice.h>
99
#include <linux/filter.h>
1010
#include <linux/if_vlan.h>
11+
#include <linux/bitfield.h>
1112
#include <linux/bpf.h>
1213
#include <linux/memory.h>
1314
#include <linux/sort.h>
@@ -1388,16 +1389,67 @@ static int emit_atomic_ld_st_index(u8 **pprog, u32 atomic_op, u32 size,
13881389
return 0;
13891390
}
13901391

1392+
/*
1393+
* Metadata encoding for exception handling in JITed code.
1394+
*
1395+
* Format of `fixup` and `data` fields in `struct exception_table_entry`:
1396+
*
1397+
* Bit layout of `fixup` (32-bit):
1398+
*
1399+
* +-----------+--------+-----------+---------+----------+
1400+
* | 31 | 30-24 | 23-16 | 15-8 | 7-0 |
1401+
* | | | | | |
1402+
* | ARENA_ACC | Unused | ARENA_REG | DST_REG | INSN_LEN |
1403+
* +-----------+--------+-----------+---------+----------+
1404+
*
1405+
* - INSN_LEN (8 bits): Length of faulting insn (max x86 insn = 15 bytes (fits in 8 bits)).
1406+
* - DST_REG (8 bits): Offset of dst_reg from reg2pt_regs[] (max offset = 112 (fits in 8 bits)).
1407+
* This is set to DONT_CLEAR if the insn is a store.
1408+
* - ARENA_REG (8 bits): Offset of the register that is used to calculate the
1409+
* address for load/store when accessing the arena region.
1410+
* - ARENA_ACCESS (1 bit): This bit is set when the faulting instruction accessed the arena region.
1411+
*
1412+
* Bit layout of `data` (32-bit):
1413+
*
1414+
* +--------------+--------+--------------+
1415+
* | 31-16 | 15-8 | 7-0 |
1416+
* | | | |
1417+
* | ARENA_OFFSET | Unused | EX_TYPE_BPF |
1418+
* +--------------+--------+--------------+
1419+
*
1420+
* - ARENA_OFFSET (16 bits): Offset used to calculate the address for load/store when
1421+
* accessing the arena region.
1422+
*/
1423+
13911424
#define DONT_CLEAR 1
1425+
#define FIXUP_INSN_LEN_MASK GENMASK(7, 0)
1426+
#define FIXUP_REG_MASK GENMASK(15, 8)
1427+
#define FIXUP_ARENA_REG_MASK GENMASK(23, 16)
1428+
#define FIXUP_ARENA_ACCESS BIT(31)
1429+
#define DATA_ARENA_OFFSET_MASK GENMASK(31, 16)
13921430

13931431
bool ex_handler_bpf(const struct exception_table_entry *x, struct pt_regs *regs)
13941432
{
1395-
u32 reg = x->fixup >> 8;
1433+
u32 reg = FIELD_GET(FIXUP_REG_MASK, x->fixup);
1434+
u32 insn_len = FIELD_GET(FIXUP_INSN_LEN_MASK, x->fixup);
1435+
bool is_arena = !!(x->fixup & FIXUP_ARENA_ACCESS);
1436+
bool is_write = (reg == DONT_CLEAR);
1437+
unsigned long addr;
1438+
s16 off;
1439+
u32 arena_reg;
1440+
1441+
if (is_arena) {
1442+
arena_reg = FIELD_GET(FIXUP_ARENA_REG_MASK, x->fixup);
1443+
off = FIELD_GET(DATA_ARENA_OFFSET_MASK, x->data);
1444+
addr = *(unsigned long *)((void *)regs + arena_reg) + off;
1445+
bpf_prog_report_arena_violation(is_write, addr, regs->ip);
1446+
}
13961447

13971448
/* jump over faulting load and clear dest register */
13981449
if (reg != DONT_CLEAR)
13991450
*(unsigned long *)((void *)regs + reg) = 0;
1400-
regs->ip += x->fixup & 0xff;
1451+
regs->ip += insn_len;
1452+
14011453
return true;
14021454
}
14031455

@@ -2070,6 +2122,7 @@ st: if (is_imm8(insn->off))
20702122
{
20712123
struct exception_table_entry *ex;
20722124
u8 *_insn = image + proglen + (start_of_ldx - temp);
2125+
u32 arena_reg, fixup_reg;
20732126
s64 delta;
20742127

20752128
if (!bpf_prog->aux->extable)
@@ -2089,8 +2142,29 @@ st: if (is_imm8(insn->off))
20892142

20902143
ex->data = EX_TYPE_BPF;
20912144

2092-
ex->fixup = (prog - start_of_ldx) |
2093-
((BPF_CLASS(insn->code) == BPF_LDX ? reg2pt_regs[dst_reg] : DONT_CLEAR) << 8);
2145+
/*
2146+
* src_reg/dst_reg holds the address in the arena region with upper
2147+
* 32-bits being zero because of a preceding addr_space_cast(r<n>,
2148+
* 0x0, 0x1) instruction. This address is adjusted with the addition
2149+
* of arena_vm_start (see the implementation of BPF_PROBE_MEM32 and
2150+
* BPF_PROBE_ATOMIC) before being used for the memory access. Pass
2151+
* the reg holding the unmodified 32-bit address to
2152+
* ex_handler_bpf().
2153+
*/
2154+
if (BPF_CLASS(insn->code) == BPF_LDX) {
2155+
arena_reg = reg2pt_regs[src_reg];
2156+
fixup_reg = reg2pt_regs[dst_reg];
2157+
} else {
2158+
arena_reg = reg2pt_regs[dst_reg];
2159+
fixup_reg = DONT_CLEAR;
2160+
}
2161+
2162+
ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, prog - start_of_ldx) |
2163+
FIELD_PREP(FIXUP_ARENA_REG_MASK, arena_reg) |
2164+
FIELD_PREP(FIXUP_REG_MASK, fixup_reg);
2165+
ex->fixup |= FIXUP_ARENA_ACCESS;
2166+
2167+
ex->data |= FIELD_PREP(DATA_ARENA_OFFSET_MASK, insn->off);
20942168
}
20952169
break;
20962170

@@ -2208,7 +2282,8 @@ st: if (is_imm8(insn->off))
22082282
* End result: x86 insn "mov rbx, qword ptr [rax+0x14]"
22092283
* of 4 bytes will be ignored and rbx will be zero inited.
22102284
*/
2211-
ex->fixup = (prog - start_of_ldx) | (reg2pt_regs[dst_reg] << 8);
2285+
ex->fixup = FIELD_PREP(FIXUP_INSN_LEN_MASK, prog - start_of_ldx) |
2286+
FIELD_PREP(FIXUP_REG_MASK, reg2pt_regs[dst_reg]);
22122287
}
22132288
break;
22142289

include/linux/bpf.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,7 @@ void bpf_dynptr_init(struct bpf_dynptr_kern *ptr, void *data,
28812881
enum bpf_dynptr_type type, u32 offset, u32 size);
28822882
void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr);
28832883
void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr);
2884+
void bpf_prog_report_arena_violation(bool write, unsigned long addr, unsigned long fault_ip);
28842885

28852886
#else /* !CONFIG_BPF_SYSCALL */
28862887
static inline struct bpf_prog *bpf_prog_get(u32 ufd)
@@ -3168,6 +3169,11 @@ static inline void bpf_dynptr_set_null(struct bpf_dynptr_kern *ptr)
31683169
static inline void bpf_dynptr_set_rdonly(struct bpf_dynptr_kern *ptr)
31693170
{
31703171
}
3172+
3173+
static inline void bpf_prog_report_arena_violation(bool write, unsigned long addr,
3174+
unsigned long fault_ip)
3175+
{
3176+
}
31713177
#endif /* CONFIG_BPF_SYSCALL */
31723178

31733179
static __always_inline int

kernel/bpf/arena.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,33 @@ static int __init kfunc_init(void)
633633
return register_btf_kfunc_id_set(BPF_PROG_TYPE_UNSPEC, &common_kfunc_set);
634634
}
635635
late_initcall(kfunc_init);
636+
637+
void bpf_prog_report_arena_violation(bool write, unsigned long addr, unsigned long fault_ip)
638+
{
639+
struct bpf_stream_stage ss;
640+
struct bpf_prog *prog;
641+
u64 user_vm_start;
642+
643+
/*
644+
* The RCU read lock is held to safely traverse the latch tree, but we
645+
* don't need its protection when accessing the prog, since it will not
646+
* disappear while we are handling the fault.
647+
*/
648+
rcu_read_lock();
649+
prog = bpf_prog_ksym_find(fault_ip);
650+
rcu_read_unlock();
651+
if (!prog)
652+
return;
653+
654+
/* Use main prog for stream access */
655+
prog = prog->aux->main_prog_aux->prog;
656+
657+
user_vm_start = bpf_arena_get_user_vm_start(prog->aux->arena);
658+
addr += clear_lo32(user_vm_start);
659+
660+
bpf_stream_stage(ss, prog, BPF_STDERR, ({
661+
bpf_stream_printk(ss, "ERROR: Arena %s access at unmapped address 0x%lx\n",
662+
write ? "WRITE" : "READ", addr);
663+
bpf_stream_dump_stack(ss);
664+
}));
665+
}

0 commit comments

Comments
 (0)