Skip to content

Commit 4f32ea1

Browse files
Hou TaoNobody
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]>
1 parent f799243 commit 4f32ea1

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;
@@ -13149,6 +13150,7 @@ static int jit_subprogs(struct bpf_verifier_env *env)
1314913150
out_undo_insn:
1315013151
/* cleanup main prog to be interpreted */
1315113152
prog->jit_requested = 0;
13153+
prog->blinding_requested = 0;
1315213154
for (i = 0, insn = prog->insnsi; i < prog->len; i++, insn++) {
1315313155
if (!bpf_pseudo_call(insn))
1315413156
continue;
@@ -13242,7 +13244,6 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
1324213244
{
1324313245
struct bpf_prog *prog = env->prog;
1324413246
enum bpf_attach_type eatype = prog->expected_attach_type;
13245-
bool expect_blinding = bpf_jit_blinding_enabled(prog);
1324613247
enum bpf_prog_type prog_type = resolve_prog_type(prog);
1324713248
struct bpf_insn *insn = prog->insnsi;
1324813249
const struct bpf_func_proto *fn;
@@ -13406,7 +13407,7 @@ static int do_misc_fixups(struct bpf_verifier_env *env)
1340613407
insn->code = BPF_JMP | BPF_TAIL_CALL;
1340713408

1340813409
aux = &env->insn_aux_data[i + delta];
13409-
if (env->bpf_capable && !expect_blinding &&
13410+
if (env->bpf_capable && !prog->blinding_requested &&
1341013411
prog->jit_requested &&
1341113412
!bpf_map_key_poisoned(aux) &&
1341213413
!bpf_map_ptr_poisoned(aux) &&

0 commit comments

Comments
 (0)