Skip to content

Commit d2a3b7c

Browse files
Hou TaoAlexei Starovoitov
authored andcommitted
bpf: Fix net.core.bpf_jit_harden race
It is the bpf_jit_harden counterpart to commit 60b58af ("bpf: fix net.core.bpf_jit_enable race"). bpf_jit_harden will be tested twice for each subprog if there are subprogs in bpf program and constant blinding may increase the length of program, so when running "./test_progs -t subprogs" and toggling bpf_jit_harden between 0 and 2, jit_subprogs may fail because constant blinding increases the length of subprog instructions during extra passs. So cache the value of bpf_jit_blinding_enabled() during program allocation, and use the cached value during constant blinding, subprog JITing and args tracking of tail call. Signed-off-by: Hou Tao <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 73e1445 commit d2a3b7c

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

include/linux/filter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ struct bpf_prog {
566566
gpl_compatible:1, /* Is filter GPL compatible? */
567567
cb_access:1, /* Is control block accessed? */
568568
dst_needed:1, /* Do we need dst entry? */
569+
blinding_requested:1, /* needs constant blinding */
569570
blinded:1, /* Was blinded */
570571
is_func:1, /* program is a bpf function */
571572
kprobe_override:1, /* Do we override a kprobe? */

kernel/bpf/core.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ struct bpf_prog *bpf_prog_alloc_no_stats(unsigned int size, gfp_t gfp_extra_flag
105105
fp->aux = aux;
106106
fp->aux->prog = fp;
107107
fp->jit_requested = ebpf_jit_enabled();
108+
fp->blinding_requested = bpf_jit_blinding_enabled(fp);
108109

109110
INIT_LIST_HEAD_RCU(&fp->aux->ksym.lnode);
110111
mutex_init(&fp->aux->used_maps_mutex);
@@ -1382,7 +1383,7 @@ struct bpf_prog *bpf_jit_blind_constants(struct bpf_prog *prog)
13821383
struct bpf_insn *insn;
13831384
int i, rewritten;
13841385

1385-
if (!bpf_jit_blinding_enabled(prog) || prog->blinded)
1386+
if (!prog->blinding_requested || prog->blinded)
13861387
return prog;
13871388

13881389
clone = bpf_prog_clone_create(prog, GFP_USER);

kernel/bpf/verifier.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13023,6 +13023,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1302313023
func[i]->aux->name[0] = 'F';
1302413024
func[i]->aux->stack_depth = env->subprog_info[i].stack_depth;
1302513025
func[i]->jit_requested = 1;
13026+
func[i]->blinding_requested = prog->blinding_requested;
1302613027
func[i]->aux->kfunc_tab = prog->aux->kfunc_tab;
1302713028
func[i]->aux->kfunc_btf_tab = prog->aux->kfunc_btf_tab;
1302813029
func[i]->aux->linfo = prog->aux->linfo;
@@ -13146,6 +13147,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1314613147
out_undo_insn:
1314713148
/* cleanup main prog to be interpreted */
1314813149
prog->jit_requested = 0;
13150+
prog->blinding_requested = 0;
1314913151
for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
1315013152
if (!bpf_pseudo_call(insn))
1315113153
continue;
@@ -13239,7 +13241,6 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
1323913241
{
1324013242
struct bpf_prog *prog = env->prog;
1324113243
enum bpf_attach_type eatype = prog->expected_attach_type;
13242-
bool expect_blinding = bpf_jit_blinding_enabled(prog);
1324313244
enum bpf_prog_type prog_type = resolve_prog_type(prog);
1324413245
struct bpf_insn *insn = prog->insnsi;
1324513246
const struct bpf_func_proto *fn;
@@ -13403,7 +13404,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
1340313404
insn->code = BPF_JMP | BPF_TAIL_CALL;
1340413405

1340513406
aux = &env->insn_aux_data[i + delta];
13406-
if (env->bpf_capable && !expect_blinding &&
13407+
if (env->bpf_capable && !prog->blinding_requested &&
1340713408
prog->jit_requested &&
1340813409
!bpf_map_key_poisoned(aux) &&
1340913410
!bpf_map_ptr_poisoned(aux) &&

0 commit comments

Comments
 (0)