Skip to content

Commit d93f655

Browse files
Hou Taogregkh
authored andcommitted
bpf: Handle return value of BPF_PROG_TYPE_STRUCT_OPS prog
[ Upstream commit 356ed64 ] Currently if a function ptr in struct_ops has a return value, its caller will get a random return value from it, because the return value of related BPF_PROG_TYPE_STRUCT_OPS prog is just dropped. So adding a new flag BPF_TRAMP_F_RET_FENTRY_RET to tell bpf trampoline to save and return the return value of struct_ops prog if ret_size of the function ptr is greater than 0. Also restricting the flag to be used alone. Fixes: 85d33df ("bpf: Introduce BPF_MAP_TYPE_STRUCT_OPS") Signed-off-by: Hou Tao <houtao1@huawei.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: Martin KaFai Lau <kafai@fb.com> Link: https://lore.kernel.org/bpf/20210914023351.3664499-1-houtao1@huawei.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 12cbdae commit d93f655

3 files changed

Lines changed: 47 additions & 15 deletions

File tree

‎arch/x86/net/bpf_jit_comp.c‎

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ static void restore_regs(const struct btf_func_model *m, u8 **prog, int nr_args,
15471547
}
15481548

15491549
static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
1550-
struct bpf_prog *p, int stack_size, bool mod_ret)
1550+
struct bpf_prog *p, int stack_size, bool save_ret)
15511551
{
15521552
u8 *prog = *pprog;
15531553
int cnt = 0;
@@ -1573,11 +1573,15 @@ static int invoke_bpf_prog(const struct btf_func_model *m, u8 **pprog,
15731573
if (emit_call(&prog, p->bpf_func, prog))
15741574
return -EINVAL;
15751575

1576-
/* BPF_TRAMP_MODIFY_RETURN trampolines can modify the return
1576+
/*
1577+
* BPF_TRAMP_MODIFY_RETURN trampolines can modify the return
15771578
* of the previous call which is then passed on the stack to
15781579
* the next BPF program.
1580+
*
1581+
* BPF_TRAMP_FENTRY trampoline may need to return the return
1582+
* value of BPF_PROG_TYPE_STRUCT_OPS prog.
15791583
*/
1580-
if (mod_ret)
1584+
if (save_ret)
15811585
emit_stx(&prog, BPF_DW, BPF_REG_FP, BPF_REG_0, -8);
15821586

15831587
if (p->aux->sleepable) {
@@ -1645,13 +1649,15 @@ static int emit_cond_near_jump(u8 **pprog, void *func, void *ip, u8 jmp_cond)
16451649
}
16461650

16471651
static int invoke_bpf(const struct btf_func_model *m, u8 **pprog,
1648-
struct bpf_tramp_progs *tp, int stack_size)
1652+
struct bpf_tramp_progs *tp, int stack_size,
1653+
bool save_ret)
16491654
{
16501655
int i;
16511656
u8 *prog = *pprog;
16521657

16531658
for (i = 0; i < tp->nr_progs; i++) {
1654-
if (invoke_bpf_prog(m, &prog, tp->progs[i], stack_size, false))
1659+
if (invoke_bpf_prog(m, &prog, tp->progs[i], stack_size,
1660+
save_ret))
16551661
return -EINVAL;
16561662
}
16571663
*pprog = prog;
@@ -1694,6 +1700,23 @@ static int invoke_bpf_mod_ret(const struct btf_func_model *m, u8 **pprog,
16941700
return 0;
16951701
}
16961702

1703+
static bool is_valid_bpf_tramp_flags(unsigned int flags)
1704+
{
1705+
if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
1706+
(flags & BPF_TRAMP_F_SKIP_FRAME))
1707+
return false;
1708+
1709+
/*
1710+
* BPF_TRAMP_F_RET_FENTRY_RET is only used by bpf_struct_ops,
1711+
* and it must be used alone.
1712+
*/
1713+
if ((flags & BPF_TRAMP_F_RET_FENTRY_RET) &&
1714+
(flags & ~BPF_TRAMP_F_RET_FENTRY_RET))
1715+
return false;
1716+
1717+
return true;
1718+
}
1719+
16971720
/* Example:
16981721
* __be16 eth_type_trans(struct sk_buff *skb, struct net_device *dev);
16991722
* its 'struct btf_func_model' will be nr_args=2
@@ -1766,17 +1789,19 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
17661789
struct bpf_tramp_progs *fmod_ret = &tprogs[BPF_TRAMP_MODIFY_RETURN];
17671790
u8 **branches = NULL;
17681791
u8 *prog;
1792+
bool save_ret;
17691793

17701794
/* x86-64 supports up to 6 arguments. 7+ can be added in the future */
17711795
if (nr_args > 6)
17721796
return -ENOTSUPP;
17731797

1774-
if ((flags & BPF_TRAMP_F_RESTORE_REGS) &&
1775-
(flags & BPF_TRAMP_F_SKIP_FRAME))
1798+
if (!is_valid_bpf_tramp_flags(flags))
17761799
return -EINVAL;
17771800

1778-
if (flags & BPF_TRAMP_F_CALL_ORIG)
1779-
stack_size += 8; /* room for return value of orig_call */
1801+
/* room for return value of orig_call or fentry prog */
1802+
save_ret = flags & (BPF_TRAMP_F_CALL_ORIG | BPF_TRAMP_F_RET_FENTRY_RET);
1803+
if (save_ret)
1804+
stack_size += 8;
17801805

17811806
if (flags & BPF_TRAMP_F_SKIP_FRAME)
17821807
/* skip patched call instruction and point orig_call to actual
@@ -1803,7 +1828,8 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
18031828
}
18041829

18051830
if (fentry->nr_progs)
1806-
if (invoke_bpf(m, &prog, fentry, stack_size))
1831+
if (invoke_bpf(m, &prog, fentry, stack_size,
1832+
flags & BPF_TRAMP_F_RET_FENTRY_RET))
18071833
return -EINVAL;
18081834

18091835
if (fmod_ret->nr_progs) {
@@ -1850,7 +1876,7 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
18501876
}
18511877

18521878
if (fexit->nr_progs)
1853-
if (invoke_bpf(m, &prog, fexit, stack_size)) {
1879+
if (invoke_bpf(m, &prog, fexit, stack_size, false)) {
18541880
ret = -EINVAL;
18551881
goto cleanup;
18561882
}
@@ -1870,9 +1896,10 @@ int arch_prepare_bpf_trampoline(struct bpf_tramp_image *im, void *image, void *i
18701896
ret = -EINVAL;
18711897
goto cleanup;
18721898
}
1873-
/* restore original return value back into RAX */
1874-
emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, -8);
18751899
}
1900+
/* restore return value of orig_call or fentry prog back into RAX */
1901+
if (save_ret)
1902+
emit_ldx(&prog, BPF_DW, BPF_REG_0, BPF_REG_FP, -8);
18761903

18771904
EMIT1(0x5B); /* pop rbx */
18781905
EMIT1(0xC9); /* leave */

‎include/linux/bpf.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,8 @@ struct btf_func_model {
526526
* programs only. Should not be used with normal calls and indirect calls.
527527
*/
528528
#define BPF_TRAMP_F_SKIP_FRAME BIT(2)
529+
/* Return the return value of fentry prog. Only used by bpf_struct_ops. */
530+
#define BPF_TRAMP_F_RET_FENTRY_RET BIT(4)
529531

530532
/* Each call __bpf_prog_enter + call bpf_func + call __bpf_prog_exit is ~50
531533
* bytes on x86. Pick a number to fit into BPF_IMAGE_SIZE / 2

‎kernel/bpf/bpf_struct_ops.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
367367
const struct btf_type *mtype, *ptype;
368368
struct bpf_prog *prog;
369369
u32 moff;
370+
u32 flags;
370371

371372
moff = btf_member_bit_offset(t, member) / 8;
372373
ptype = btf_type_resolve_ptr(btf_vmlinux, member->type, NULL);
@@ -430,10 +431,12 @@ static int bpf_struct_ops_map_update_elem(struct bpf_map *map, void *key,
430431

431432
tprogs[BPF_TRAMP_FENTRY].progs[0] = prog;
432433
tprogs[BPF_TRAMP_FENTRY].nr_progs = 1;
434+
flags = st_ops->func_models[i].ret_size > 0 ?
435+
BPF_TRAMP_F_RET_FENTRY_RET : 0;
433436
err = arch_prepare_bpf_trampoline(NULL, image,
434437
st_map->image + PAGE_SIZE,
435-
&st_ops->func_models[i], 0,
436-
tprogs, NULL);
438+
&st_ops->func_models[i],
439+
flags, tprogs, NULL);
437440
if (err < 0)
438441
goto reset_unlock;
439442

0 commit comments

Comments
 (0)