Skip to content

Commit 6504661

Browse files
mykyta5Kernel Patches Daemon
authored andcommitted
bpf: verifier: refactor kfunc specialization
Move kfunc specialization (function address substitution) to later stage of verification to support a new use case, where we need to take into consideration whether kfunc is called in sleepable context. Minor refactoring in add_kfunc_call(), making sure that if function fails, kfunc desc is not added to tab->descs (previously it could be added or not, depending on what failed). Signed-off-by: Mykyta Yatsenko <[email protected]>
1 parent 48a02c2 commit 6504661

File tree

1 file changed

+73
-44
lines changed

1 file changed

+73
-44
lines changed

kernel/bpf/verifier.c

Lines changed: 73 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -209,8 +209,6 @@ static void invalidate_non_owning_refs(struct bpf_verifier_env *env);
209209
static bool in_rbtree_lock_required_cb(struct bpf_verifier_env *env);
210210
static int ref_set_non_owning(struct bpf_verifier_env *env,
211211
struct bpf_reg_state *reg);
212-
static void specialize_kfunc(struct bpf_verifier_env *env,
213-
u32 func_id, u16 offset, unsigned long *addr);
214212
static bool is_trusted_reg(const struct bpf_reg_state *reg);
215213

216214
static bool bpf_map_ptr_poisoned(const struct bpf_insn_aux_data *aux)
@@ -3126,6 +3124,11 @@ struct bpf_kfunc_btf_tab {
31263124
u32 nr_descs;
31273125
};
31283126

3127+
static int kfunc_call_imm(struct bpf_verifier_env *env, unsigned long func_addr, u32 func_id,
3128+
s32 *imm);
3129+
3130+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc);
3131+
31293132
static int kfunc_desc_cmp_by_id_off(const void *a, const void *b)
31303133
{
31313134
const struct bpf_kfunc_desc *d0 = a;
@@ -3143,7 +3146,7 @@ static int kfunc_btf_cmp_by_off(const void *a, const void *b)
31433146
return d0->offset - d1->offset;
31443147
}
31453148

3146-
static const struct bpf_kfunc_desc *
3149+
static struct bpf_kfunc_desc *
31473150
find_kfunc_desc(const struct bpf_prog *prog, u32 func_id, u16 offset)
31483151
{
31493152
struct bpf_kfunc_desc desc = {
@@ -3266,12 +3269,13 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)
32663269
{
32673270
const struct btf_type *func, *func_proto;
32683271
struct bpf_kfunc_btf_tab *btf_tab;
3272+
struct btf_func_model func_model;
32693273
struct bpf_kfunc_desc_tab *tab;
32703274
struct bpf_prog_aux *prog_aux;
32713275
struct bpf_kfunc_desc *desc;
32723276
const char *func_name;
32733277
struct btf *desc_btf;
3274-
unsigned long call_imm;
3278+
s32 call_imm;
32753279
unsigned long addr;
32763280
int err;
32773281

@@ -3355,38 +3359,32 @@ static int add_kfunc_call(struct bpf_verifier_env *env, u32 func_id, s16 offset)
33553359
func_name);
33563360
return -EINVAL;
33573361
}
3358-
specialize_kfunc(env, func_id, offset, &addr);
3359-
3360-
if (bpf_jit_supports_far_kfunc_call()) {
3361-
call_imm = func_id;
3362-
} else {
3363-
call_imm = BPF_CALL_IMM(addr);
3364-
/* Check whether the relative offset overflows desc->imm */
3365-
if ((unsigned long)(s32)call_imm != call_imm) {
3366-
verbose(env, "address of kernel function %s is out of range\n",
3367-
func_name);
3368-
return -EINVAL;
3369-
}
3370-
}
33713362

33723363
if (bpf_dev_bound_kfunc_id(func_id)) {
33733364
err = bpf_dev_bound_kfunc_check(&env->log, prog_aux);
33743365
if (err)
33753366
return err;
33763367
}
33773368

3369+
err = btf_distill_func_proto(&env->log, desc_btf,
3370+
func_proto, func_name,
3371+
&func_model);
3372+
if (err)
3373+
return err;
3374+
3375+
err = kfunc_call_imm(env, addr, func_id, &call_imm);
3376+
if (err)
3377+
return err;
3378+
33783379
desc = &tab->descs[tab->nr_descs++];
33793380
desc->func_id = func_id;
33803381
desc->imm = call_imm;
33813382
desc->offset = offset;
33823383
desc->addr = addr;
3383-
err = btf_distill_func_proto(&env->log, desc_btf,
3384-
func_proto, func_name,
3385-
&desc->func_model);
3386-
if (!err)
3387-
sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]),
3388-
kfunc_desc_cmp_by_id_off, NULL);
3389-
return err;
3384+
desc->func_model = func_model;
3385+
sort(tab->descs, tab->nr_descs, sizeof(tab->descs[0]),
3386+
kfunc_desc_cmp_by_id_off, NULL);
3387+
return 0;
33903388
}
33913389

33923390
static int kfunc_desc_cmp_by_imm_off(const void *a, const void *b)
@@ -21879,47 +21877,73 @@ static int fixup_call_args(struct bpf_verifier_env *env)
2187921877
return err;
2188021878
}
2188121879

21880+
static int kfunc_call_imm(struct bpf_verifier_env *env, unsigned long func_addr, u32 func_id,
21881+
s32 *imm)
21882+
{
21883+
unsigned long call_imm;
21884+
21885+
if (bpf_jit_supports_far_kfunc_call()) {
21886+
*imm = func_id;
21887+
return 0;
21888+
}
21889+
21890+
call_imm = BPF_CALL_IMM(func_addr);
21891+
/* Check whether the relative offset overflows desc->imm */
21892+
if ((unsigned long)(s32)call_imm != call_imm) {
21893+
verbose(env, "address of kernel func_id %u is out of range\n", func_id);
21894+
return -EINVAL;
21895+
}
21896+
*imm = call_imm;
21897+
return 0;
21898+
}
21899+
2188221900
/* replace a generic kfunc with a specialized version if necessary */
21883-
static void specialize_kfunc(struct bpf_verifier_env *env,
21884-
u32 func_id, u16 offset, unsigned long *addr)
21901+
static int specialize_kfunc(struct bpf_verifier_env *env, struct bpf_kfunc_desc *desc)
2188521902
{
2188621903
struct bpf_prog *prog = env->prog;
2188721904
bool seen_direct_write;
2188821905
void *xdp_kfunc;
2188921906
bool is_rdonly;
21907+
u32 func_id = desc->func_id;
21908+
u16 offset = desc->offset;
21909+
unsigned long addr = 0;
21910+
int err;
21911+
21912+
if (offset) /* return if module BTF is used */
21913+
return 0;
2189021914

2189121915
if (bpf_dev_bound_kfunc_id(func_id)) {
2189221916
xdp_kfunc = bpf_dev_bound_resolve_kfunc(prog, func_id);
21893-
if (xdp_kfunc) {
21894-
*addr = (unsigned long)xdp_kfunc;
21895-
return;
21896-
}
21917+
if (xdp_kfunc)
21918+
addr = (unsigned long)xdp_kfunc;
2189721919
/* fallback to default kfunc when not supported by netdev */
21898-
}
21899-
21900-
if (offset)
21901-
return;
21902-
21903-
if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) {
21920+
} else if (func_id == special_kfunc_list[KF_bpf_dynptr_from_skb]) {
2190421921
seen_direct_write = env->seen_direct_write;
2190521922
is_rdonly = !may_access_direct_pkt_data(env, NULL, BPF_WRITE);
2190621923

2190721924
if (is_rdonly)
21908-
*addr = (unsigned long)bpf_dynptr_from_skb_rdonly;
21925+
addr = (unsigned long)bpf_dynptr_from_skb_rdonly;
2190921926

2191021927
/* restore env->seen_direct_write to its original value, since
2191121928
* may_access_direct_pkt_data mutates it
2191221929
*/
2191321930
env->seen_direct_write = seen_direct_write;
21931+
} else if (func_id == special_kfunc_list[KF_bpf_set_dentry_xattr]) {
21932+
if (bpf_lsm_has_d_inode_locked(prog))
21933+
addr = (unsigned long)bpf_set_dentry_xattr_locked;
21934+
} else if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr]) {
21935+
if (bpf_lsm_has_d_inode_locked(prog))
21936+
addr = (unsigned long)bpf_remove_dentry_xattr_locked;
2191421937
}
2191521938

21916-
if (func_id == special_kfunc_list[KF_bpf_set_dentry_xattr] &&
21917-
bpf_lsm_has_d_inode_locked(prog))
21918-
*addr = (unsigned long)bpf_set_dentry_xattr_locked;
21939+
if (!addr) /* Nothing to patch with */
21940+
return 0;
2191921941

21920-
if (func_id == special_kfunc_list[KF_bpf_remove_dentry_xattr] &&
21921-
bpf_lsm_has_d_inode_locked(prog))
21922-
*addr = (unsigned long)bpf_remove_dentry_xattr_locked;
21942+
err = kfunc_call_imm(env, addr, func_id, &desc->imm);
21943+
if (err)
21944+
return err;
21945+
desc->addr = addr;
21946+
return 0;
2192321947
}
2192421948

2192521949
static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,
@@ -21942,7 +21966,8 @@ static void __fixup_collection_insert_kfunc(struct bpf_insn_aux_data *insn_aux,
2194221966
static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2194321967
struct bpf_insn *insn_buf, int insn_idx, int *cnt)
2194421968
{
21945-
const struct bpf_kfunc_desc *desc;
21969+
struct bpf_kfunc_desc *desc;
21970+
int err;
2194621971

2194721972
if (!insn->imm) {
2194821973
verbose(env, "invalid kernel function call not eliminated in verifier pass\n");
@@ -21962,6 +21987,10 @@ static int fixup_kfunc_call(struct bpf_verifier_env *env, struct bpf_insn *insn,
2196221987
return -EFAULT;
2196321988
}
2196421989

21990+
err = specialize_kfunc(env, desc);
21991+
if (err)
21992+
return err;
21993+
2196521994
if (!bpf_jit_supports_far_kfunc_call())
2196621995
insn->imm = BPF_CALL_IMM(desc->addr);
2196721996
if (insn->off)

0 commit comments

Comments
 (0)