Skip to content

Commit 7105e82

Browse files
borkmannAlexei Starovoitov
authored andcommitted
bpf: allow for correlation of maps and helpers in dump
Currently a dump of an xlated prog (post verifier stage) doesn't correlate used helpers as well as maps. The prog info lists involved map ids, however there's no correlation of where in the program they are used as of today. Likewise, bpftool does not correlate helper calls with the target functions. The latter can be done w/o any kernel changes through kallsyms, and also has the advantage that this works with inlined helpers and BPF calls. Example, via interpreter: # tc filter show dev foo ingress filter protocol all pref 49152 bpf chain 0 filter protocol all pref 49152 bpf chain 0 handle 0x1 foo.o:[ingress] \ direct-action not_in_hw id 1 tag c74773051b364165 <-- prog id:1 * Output before patch (calls/maps remain unclear): # bpftool prog dump xlated id 1 <-- dump prog id:1 0: (b7) r1 = 2 1: (63) *(u32 *)(r10 -4) = r1 2: (bf) r2 = r10 3: (07) r2 += -4 4: (18) r1 = 0xffff95c47a8d4800 6: (85) call unknown#73040 7: (15) if r0 == 0x0 goto pc+18 8: (bf) r2 = r10 9: (07) r2 += -4 10: (bf) r1 = r0 11: (85) call unknown#73040 12: (15) if r0 == 0x0 goto pc+23 [...] * Output after patch: # bpftool prog dump xlated id 1 0: (b7) r1 = 2 1: (63) *(u32 *)(r10 -4) = r1 2: (bf) r2 = r10 3: (07) r2 += -4 4: (18) r1 = map[id:2] <-- map id:2 6: (85) call bpf_map_lookup_elem#73424 <-- helper call 7: (15) if r0 == 0x0 goto pc+18 8: (bf) r2 = r10 9: (07) r2 += -4 10: (bf) r1 = r0 11: (85) call bpf_map_lookup_elem#73424 12: (15) if r0 == 0x0 goto pc+23 [...] # bpftool map show id 2 <-- show/dump/etc map id:2 2: hash_of_maps flags 0x0 key 4B value 4B max_entries 3 memlock 4096B Example, JITed, same prog: # tc filter show dev foo ingress filter protocol all pref 49152 bpf chain 0 filter protocol all pref 49152 bpf chain 0 handle 0x1 foo.o:[ingress] \ direct-action not_in_hw id 3 tag c74773051b364165 jited # bpftool prog show id 3 3: sched_cls tag c74773051b364165 loaded_at Dec 19/13:48 uid 0 xlated 384B jited 257B memlock 4096B map_ids 2 # bpftool prog dump xlated id 3 0: (b7) r1 = 2 1: (63) *(u32 *)(r10 -4) = r1 2: (bf) r2 = r10 3: (07) r2 += -4 4: (18) r1 = map[id:2] <-- map id:2 6: (85) call __htab_map_lookup_elem#77408 <-+ inlined rewrite 7: (15) if r0 == 0x0 goto pc+2 | 8: (07) r0 += 56 | 9: (79) r0 = *(u64 *)(r0 +0) <-+ 10: (15) if r0 == 0x0 goto pc+24 11: (bf) r2 = r10 12: (07) r2 += -4 [...] Example, same prog, but kallsyms disabled (in that case we are also not allowed to pass any relative offsets, etc, so prog becomes pointer sanitized on dump): # sysctl kernel.kptr_restrict=2 kernel.kptr_restrict = 2 # bpftool prog dump xlated id 3 0: (b7) r1 = 2 1: (63) *(u32 *)(r10 -4) = r1 2: (bf) r2 = r10 3: (07) r2 += -4 4: (18) r1 = map[id:2] 6: (85) call bpf_unspec#0 7: (15) if r0 == 0x0 goto pc+2 [...] Example, BPF calls via interpreter: # bpftool prog dump xlated id 1 0: (85) call pc+2#__bpf_prog_run_args32 1: (b7) r0 = 1 2: (95) exit 3: (b7) r0 = 2 4: (95) exit Example, BPF calls via JIT: # sysctl net.core.bpf_jit_enable=1 net.core.bpf_jit_enable = 1 # sysctl net.core.bpf_jit_kallsyms=1 net.core.bpf_jit_kallsyms = 1 # bpftool prog dump xlated id 1 0: (85) call pc+2#bpf_prog_3b185187f1855c4c_F 1: (b7) r0 = 1 2: (95) exit 3: (b7) r0 = 2 4: (95) exit And finally, an example for tail calls that is now working as well wrt correlation: # bpftool prog dump xlated id 2 [...] 10: (b7) r2 = 8 11: (85) call bpf_trace_printk#-41312 12: (bf) r1 = r6 13: (18) r2 = map[id:1] 15: (b7) r3 = 0 16: (85) call bpf_tail_call#12 17: (b7) r1 = 42 18: (6b) *(u16 *)(r6 +46) = r1 19: (b7) r0 = 0 20: (95) exit # bpftool map show id 1 1: prog_array flags 0x0 key 4B value 4B max_entries 1 memlock 4096B Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
1 parent 4f74d80 commit 7105e82

7 files changed

Lines changed: 370 additions & 35 deletions

File tree

‎include/linux/filter.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <linux/capability.h>
1919
#include <linux/cryptohash.h>
2020
#include <linux/set_memory.h>
21+
#include <linux/kallsyms.h>
2122

2223
#include <net/sch_generic.h>
2324

@@ -724,6 +725,14 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog);
724725
void bpf_jit_compile(struct bpf_prog *prog);
725726
bool bpf_helper_changes_pkt_data(void *func);
726727

728+
static inline bool bpf_dump_raw_ok(void)
729+
{
730+
/* Reconstruction of call-sites is dependent on kallsyms,
731+
* thus make dump the same restriction.
732+
*/
733+
return kallsyms_show_value() == 1;
734+
}
735+
727736
struct bpf_prog *bpf_patch_insn_single(struct bpf_prog *prog, u32 off,
728737
const struct bpf_insn *patch, u32 len);
729738

‎kernel/bpf/core.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,9 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
771771

772772
/* Base function for offset calculation. Needs to go into .text section,
773773
* therefore keeping it non-static as well; will also be used by JITs
774-
* anyway later on, so do not let the compiler omit it.
774+
* anyway later on, so do not let the compiler omit it. This also needs
775+
* to go into kallsyms for correlation from e.g. bpftool, so naming
776+
* must not change.
775777
*/
776778
noinline u64 __bpf_call_base(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
777779
{

‎kernel/bpf/disasm.c‎

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,39 @@ static const char * const func_id_str[] = {
2121
};
2222
#undef __BPF_FUNC_STR_FN
2323

24-
const char *func_id_name(int id)
24+
static const char *__func_get_name(const struct bpf_insn_cbs *cbs,
25+
const struct bpf_insn *insn,
26+
char *buff, size_t len)
2527
{
2628
BUILD_BUG_ON(ARRAY_SIZE(func_id_str) != __BPF_FUNC_MAX_ID);
2729

30+
if (insn->src_reg != BPF_PSEUDO_CALL &&
31+
insn->imm >= 0 && insn->imm < __BPF_FUNC_MAX_ID &&
32+
func_id_str[insn->imm])
33+
return func_id_str[insn->imm];
34+
35+
if (cbs && cbs->cb_call)
36+
return cbs->cb_call(cbs->private_data, insn);
37+
38+
if (insn->src_reg == BPF_PSEUDO_CALL)
39+
snprintf(buff, len, "%+d", insn->imm);
40+
41+
return buff;
42+
}
43+
44+
static const char *__func_imm_name(const struct bpf_insn_cbs *cbs,
45+
const struct bpf_insn *insn,
46+
u64 full_imm, char *buff, size_t len)
47+
{
48+
if (cbs && cbs->cb_imm)
49+
return cbs->cb_imm(cbs->private_data, insn, full_imm);
50+
51+
snprintf(buff, len, "0x%llx", (unsigned long long)full_imm);
52+
return buff;
53+
}
54+
55+
const char *func_id_name(int id)
56+
{
2857
if (id >= 0 && id < __BPF_FUNC_MAX_ID && func_id_str[id])
2958
return func_id_str[id];
3059
else
@@ -83,7 +112,7 @@ static const char *const bpf_jmp_string[16] = {
83112
[BPF_EXIT >> 4] = "exit",
84113
};
85114

86-
static void print_bpf_end_insn(bpf_insn_print_cb verbose,
115+
static void print_bpf_end_insn(bpf_insn_print_t verbose,
87116
struct bpf_verifier_env *env,
88117
const struct bpf_insn *insn)
89118
{
@@ -92,9 +121,12 @@ static void print_bpf_end_insn(bpf_insn_print_cb verbose,
92121
insn->imm, insn->dst_reg);
93122
}
94123

95-
void print_bpf_insn(bpf_insn_print_cb verbose, struct bpf_verifier_env *env,
96-
const struct bpf_insn *insn, bool allow_ptr_leaks)
124+
void print_bpf_insn(const struct bpf_insn_cbs *cbs,
125+
struct bpf_verifier_env *env,
126+
const struct bpf_insn *insn,
127+
bool allow_ptr_leaks)
97128
{
129+
const bpf_insn_print_t verbose = cbs->cb_print;
98130
u8 class = BPF_CLASS(insn->code);
99131

100132
if (class == BPF_ALU || class == BPF_ALU64) {
@@ -175,12 +207,15 @@ void print_bpf_insn(bpf_insn_print_cb verbose, struct bpf_verifier_env *env,
175207
*/
176208
u64 imm = ((u64)(insn + 1)->imm << 32) | (u32)insn->imm;
177209
bool map_ptr = insn->src_reg == BPF_PSEUDO_MAP_FD;
210+
char tmp[64];
178211

179212
if (map_ptr && !allow_ptr_leaks)
180213
imm = 0;
181214

182-
verbose(env, "(%02x) r%d = 0x%llx\n", insn->code,
183-
insn->dst_reg, (unsigned long long)imm);
215+
verbose(env, "(%02x) r%d = %s\n",
216+
insn->code, insn->dst_reg,
217+
__func_imm_name(cbs, insn, imm,
218+
tmp, sizeof(tmp)));
184219
} else {
185220
verbose(env, "BUG_ld_%02x\n", insn->code);
186221
return;
@@ -189,12 +224,20 @@ void print_bpf_insn(bpf_insn_print_cb verbose, struct bpf_verifier_env *env,
189224
u8 opcode = BPF_OP(insn->code);
190225

191226
if (opcode == BPF_CALL) {
192-
if (insn->src_reg == BPF_PSEUDO_CALL)
193-
verbose(env, "(%02x) call pc%+d\n", insn->code,
194-
insn->imm);
195-
else
227+
char tmp[64];
228+
229+
if (insn->src_reg == BPF_PSEUDO_CALL) {
230+
verbose(env, "(%02x) call pc%s\n",
231+
insn->code,
232+
__func_get_name(cbs, insn,
233+
tmp, sizeof(tmp)));
234+
} else {
235+
strcpy(tmp, "unknown");
196236
verbose(env, "(%02x) call %s#%d\n", insn->code,
197-
func_id_name(insn->imm), insn->imm);
237+
__func_get_name(cbs, insn,
238+
tmp, sizeof(tmp)),
239+
insn->imm);
240+
}
198241
} else if (insn->code == (BPF_JMP | BPF_JA)) {
199242
verbose(env, "(%02x) goto pc%+d\n",
200243
insn->code, insn->off);

‎kernel/bpf/disasm.h‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,35 @@
1717
#include <linux/bpf.h>
1818
#include <linux/kernel.h>
1919
#include <linux/stringify.h>
20+
#ifndef __KERNEL__
21+
#include <stdio.h>
22+
#include <string.h>
23+
#endif
24+
25+
struct bpf_verifier_env;
2026

2127
extern const char *const bpf_alu_string[16];
2228
extern const char *const bpf_class_string[8];
2329

2430
const char *func_id_name(int id);
2531

26-
struct bpf_verifier_env;
27-
typedef void (*bpf_insn_print_cb)(struct bpf_verifier_env *env,
28-
const char *, ...);
29-
void print_bpf_insn(bpf_insn_print_cb verbose, struct bpf_verifier_env *env,
30-
const struct bpf_insn *insn, bool allow_ptr_leaks);
32+
typedef void (*bpf_insn_print_t)(struct bpf_verifier_env *env,
33+
const char *, ...);
34+
typedef const char *(*bpf_insn_revmap_call_t)(void *private_data,
35+
const struct bpf_insn *insn);
36+
typedef const char *(*bpf_insn_print_imm_t)(void *private_data,
37+
const struct bpf_insn *insn,
38+
__u64 full_imm);
39+
40+
struct bpf_insn_cbs {
41+
bpf_insn_print_t cb_print;
42+
bpf_insn_revmap_call_t cb_call;
43+
bpf_insn_print_imm_t cb_imm;
44+
void *private_data;
45+
};
3146

47+
void print_bpf_insn(const struct bpf_insn_cbs *cbs,
48+
struct bpf_verifier_env *env,
49+
const struct bpf_insn *insn,
50+
bool allow_ptr_leaks);
3251
#endif

‎kernel/bpf/syscall.c‎

Lines changed: 82 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,6 +1558,67 @@ static int bpf_map_get_fd_by_id(const union bpf_attr *attr)
15581558
return fd;
15591559
}
15601560

1561+
static const struct bpf_map *bpf_map_from_imm(const struct bpf_prog *prog,
1562+
unsigned long addr)
1563+
{
1564+
int i;
1565+
1566+
for (i = 0; i < prog->aux->used_map_cnt; i++)
1567+
if (prog->aux->used_maps[i] == (void *)addr)
1568+
return prog->aux->used_maps[i];
1569+
return NULL;
1570+
}
1571+
1572+
static struct bpf_insn *bpf_insn_prepare_dump(const struct bpf_prog *prog)
1573+
{
1574+
const struct bpf_map *map;
1575+
struct bpf_insn *insns;
1576+
u64 imm;
1577+
int i;
1578+
1579+
insns = kmemdup(prog->insnsi, bpf_prog_insn_size(prog),
1580+
GFP_USER);
1581+
if (!insns)
1582+
return insns;
1583+
1584+
for (i = 0; i < prog->len; i++) {
1585+
if (insns[i].code == (BPF_JMP | BPF_TAIL_CALL)) {
1586+
insns[i].code = BPF_JMP | BPF_CALL;
1587+
insns[i].imm = BPF_FUNC_tail_call;
1588+
/* fall-through */
1589+
}
1590+
if (insns[i].code == (BPF_JMP | BPF_CALL) ||
1591+
insns[i].code == (BPF_JMP | BPF_CALL_ARGS)) {
1592+
if (insns[i].code == (BPF_JMP | BPF_CALL_ARGS))
1593+
insns[i].code = BPF_JMP | BPF_CALL;
1594+
if (!bpf_dump_raw_ok())
1595+
insns[i].imm = 0;
1596+
continue;
1597+
}
1598+
1599+
if (insns[i].code != (BPF_LD | BPF_IMM | BPF_DW))
1600+
continue;
1601+
1602+
imm = ((u64)insns[i + 1].imm << 32) | (u32)insns[i].imm;
1603+
map = bpf_map_from_imm(prog, imm);
1604+
if (map) {
1605+
insns[i].src_reg = BPF_PSEUDO_MAP_FD;
1606+
insns[i].imm = map->id;
1607+
insns[i + 1].imm = 0;
1608+
continue;
1609+
}
1610+
1611+
if (!bpf_dump_raw_ok() &&
1612+
imm == (unsigned long)prog->aux) {
1613+
insns[i].imm = 0;
1614+
insns[i + 1].imm = 0;
1615+
continue;
1616+
}
1617+
}
1618+
1619+
return insns;
1620+
}
1621+
15611622
static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
15621623
const union bpf_attr *attr,
15631624
union bpf_attr __user *uattr)
@@ -1608,18 +1669,34 @@ static int bpf_prog_get_info_by_fd(struct bpf_prog *prog,
16081669
ulen = info.jited_prog_len;
16091670
info.jited_prog_len = prog->jited_len;
16101671
if (info.jited_prog_len && ulen) {
1611-
uinsns = u64_to_user_ptr(info.jited_prog_insns);
1612-
ulen = min_t(u32, info.jited_prog_len, ulen);
1613-
if (copy_to_user(uinsns, prog->bpf_func, ulen))
1614-
return -EFAULT;
1672+
if (bpf_dump_raw_ok()) {
1673+
uinsns = u64_to_user_ptr(info.jited_prog_insns);
1674+
ulen = min_t(u32, info.jited_prog_len, ulen);
1675+
if (copy_to_user(uinsns, prog->bpf_func, ulen))
1676+
return -EFAULT;
1677+
} else {
1678+
info.jited_prog_insns = 0;
1679+
}
16151680
}
16161681

16171682
ulen = info.xlated_prog_len;
16181683
info.xlated_prog_len = bpf_prog_insn_size(prog);
16191684
if (info.xlated_prog_len && ulen) {
1685+
struct bpf_insn *insns_sanitized;
1686+
bool fault;
1687+
1688+
if (prog->blinded && !bpf_dump_raw_ok()) {
1689+
info.xlated_prog_insns = 0;
1690+
goto done;
1691+
}
1692+
insns_sanitized = bpf_insn_prepare_dump(prog);
1693+
if (!insns_sanitized)
1694+
return -ENOMEM;
16201695
uinsns = u64_to_user_ptr(info.xlated_prog_insns);
16211696
ulen = min_t(u32, info.xlated_prog_len, ulen);
1622-
if (copy_to_user(uinsns, prog->insnsi, ulen))
1697+
fault = copy_to_user(uinsns, insns_sanitized, ulen);
1698+
kfree(insns_sanitized);
1699+
if (fault)
16231700
return -EFAULT;
16241701
}
16251702

‎kernel/bpf/verifier.c‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4427,9 +4427,12 @@ static int do_check(struct bpf_verifier_env *env)
44274427
}
44284428

44294429
if (env->log.level) {
4430+
const struct bpf_insn_cbs cbs = {
4431+
.cb_print = verbose,
4432+
};
4433+
44304434
verbose(env, "%d: ", insn_idx);
4431-
print_bpf_insn(verbose, env, insn,
4432-
env->allow_ptr_leaks);
4435+
print_bpf_insn(&cbs, env, insn, env->allow_ptr_leaks);
44334436
}
44344437

44354438
err = ext_analyzer_insn_hook(env, insn_idx, prev_insn_idx);
@@ -5017,14 +5020,14 @@ static int jit_subprogs(struct bpf_verifier_env *env)
50175020
{
50185021
struct bpf_prog *prog = env->prog, **func, *tmp;
50195022
int i, j, subprog_start, subprog_end = 0, len, subprog;
5020-
struct bpf_insn *insn = prog->insnsi;
5023+
struct bpf_insn *insn;
50215024
void *old_bpf_func;
50225025
int err = -ENOMEM;
50235026

50245027
if (env->subprog_cnt == 0)
50255028
return 0;
50265029

5027-
for (i = 0; i < prog->len; i++, insn++) {
5030+
for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
50285031
if (insn->code != (BPF_JMP | BPF_CALL) ||
50295032
insn->src_reg != BPF_PSEUDO_CALL)
50305033
continue;
@@ -5116,6 +5119,25 @@ static int jit_subprogs(struct bpf_verifier_env *env)
51165119
bpf_prog_lock_ro(func[i]);
51175120
bpf_prog_kallsyms_add(func[i]);
51185121
}
5122+
5123+
/* Last step: make now unused interpreter insns from main
5124+
* prog consistent for later dump requests, so they can
5125+
* later look the same as if they were interpreted only.
5126+
*/
5127+
for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
5128+
unsigned long addr;
5129+
5130+
if (insn->code != (BPF_JMP | BPF_CALL) ||
5131+
insn->src_reg != BPF_PSEUDO_CALL)
5132+
continue;
5133+
insn->off = env->insn_aux_data[i].call_imm;
5134+
subprog = find_subprog(env, i + insn->off + 1);
5135+
addr = (unsigned long)func[subprog + 1]->bpf_func;
5136+
addr &= PAGE_MASK;
5137+
insn->imm = (u64 (*)(u64, u64, u64, u64, u64))
5138+
addr - __bpf_call_base;
5139+
}
5140+
51195141
prog->jited = 1;
51205142
prog->bpf_func = func[0]->bpf_func;
51215143
prog->aux->func = func;

0 commit comments

Comments
 (0)