Skip to content

Commit eb0d6d9

Browse files
committed
Merge tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf
Pull bpf fixes from Alexei Starovoitov: "Most of the diff stat comes from Xu Kuohai's fix to emit ENDBR/BTI, since all JITs had to be touched to move constant blinding out and pass bpf_verifier_env in. - Fix use-after-free in arena_vm_close on fork (Alexei Starovoitov) - Dissociate struct_ops program with map if map_update fails (Amery Hung) - Fix out-of-range and off-by-one bugs in arm64 JIT (Daniel Borkmann) - Fix precedence bug in convert_bpf_ld_abs alignment check (Daniel Borkmann) - Fix arg tracking for imprecise/multi-offset in BPF_ST/STX insns (Eduard Zingerman) - Copy token from main to subprogs to fix missing kallsyms (Eduard Zingerman) - Prevent double close and leak of btf objects in libbpf (Jiri Olsa) - Fix af_unix null-ptr-deref in sockmap (Michal Luczaj) - Fix NULL deref in map_kptr_match_type for scalar regs (Mykyta Yatsenko) - Avoid unnecessary IPIs. Remove redundant bpf_flush_icache() in arm64 and riscv JITs (Puranjay Mohan) - Fix out of bounds access. Validate node_id in arena_alloc_pages() (Puranjay Mohan) - Reject BPF-to-BPF calls and callbacks in arm32 JIT (Puranjay Mohan) - Refactor all JITs to pass bpf_verifier_env to emit ENDBR/BTI for indirect jump targets on x86-64, arm64 JITs (Xu Kuohai) - Allow UTF-8 literals in bpf_bprintf_prepare() (Yihan Ding)" * tag 'bpf-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf: (32 commits) bpf, arm32: Reject BPF-to-BPF calls and callbacks in the JIT bpf: Dissociate struct_ops program with map if map_update fails bpf: Validate node_id in arena_alloc_pages() libbpf: Prevent double close and leak of btf objects selftests/bpf: cover UTF-8 trace_printk output bpf: allow UTF-8 literals in bpf_bprintf_prepare() selftests/bpf: Reject scalar store into kptr slot bpf: Fix NULL deref in map_kptr_match_type for scalar regs bpf: Fix precedence bug in convert_bpf_ld_abs alignment check bpf, arm64: Emit BTI for indirect jump target bpf, x86: Emit ENDBR for indirect jump targets bpf: Add helper to detect indirect jump targets bpf: Pass bpf_verifier_env to JIT bpf: Move constants blinding out of arch-specific JITs bpf, sockmap: Take state lock for af_unix iter bpf, sockmap: Fix af_unix null-ptr-deref in proto update selftests/bpf: Extend bpf_iter_unix to attempt deadlocking bpf, sockmap: Fix af_unix iter deadlock bpf, sockmap: Annotate af_unix sock:: Sk_state data-races selftests/bpf: verify kallsyms entries for token-loaded subprograms ...
2 parents 12bffae + e1d4864 commit eb0d6d9

46 files changed

Lines changed: 1173 additions & 707 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

arch/arc/net/bpf_jit_core.c

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ struct arc_jit_data {
7979
* The JIT pertinent context that is used by different functions.
8080
*
8181
* prog: The current eBPF program being handled.
82-
* orig_prog: The original eBPF program before any possible change.
8382
* jit: The JIT buffer and its length.
8483
* bpf_header: The JITed program header. "jit.buf" points inside it.
8584
* emit: If set, opcodes are written to memory; else, a dry-run.
@@ -94,12 +93,10 @@ struct arc_jit_data {
9493
* need_extra_pass: A forecast if an "extra_pass" will occur.
9594
* is_extra_pass: Indicates if the current pass is an extra pass.
9695
* user_bpf_prog: True, if VM opcodes come from a real program.
97-
* blinded: True if "constant blinding" step returned a new "prog".
9896
* success: Indicates if the whole JIT went OK.
9997
*/
10098
struct jit_context {
10199
struct bpf_prog *prog;
102-
struct bpf_prog *orig_prog;
103100
struct jit_buffer jit;
104101
struct bpf_binary_header *bpf_header;
105102
bool emit;
@@ -114,7 +111,6 @@ struct jit_context {
114111
bool need_extra_pass;
115112
bool is_extra_pass;
116113
bool user_bpf_prog;
117-
bool blinded;
118114
bool success;
119115
};
120116

@@ -161,13 +157,7 @@ static int jit_ctx_init(struct jit_context *ctx, struct bpf_prog *prog)
161157
{
162158
memset(ctx, 0, sizeof(*ctx));
163159

164-
ctx->orig_prog = prog;
165-
166-
/* If constant blinding was requested but failed, scram. */
167-
ctx->prog = bpf_jit_blind_constants(prog);
168-
if (IS_ERR(ctx->prog))
169-
return PTR_ERR(ctx->prog);
170-
ctx->blinded = (ctx->prog != ctx->orig_prog);
160+
ctx->prog = prog;
171161

172162
/* If the verifier doesn't zero-extend, then we have to do it. */
173163
ctx->do_zext = !ctx->prog->aux->verifier_zext;
@@ -214,27 +204,26 @@ static inline void maybe_free(struct jit_context *ctx, void **mem)
214204
*/
215205
static void jit_ctx_cleanup(struct jit_context *ctx)
216206
{
217-
if (ctx->blinded) {
218-
/* if all went well, release the orig_prog. */
219-
if (ctx->success)
220-
bpf_jit_prog_release_other(ctx->prog, ctx->orig_prog);
221-
else
222-
bpf_jit_prog_release_other(ctx->orig_prog, ctx->prog);
223-
}
224-
225207
maybe_free(ctx, (void **)&ctx->bpf2insn);
226208
maybe_free(ctx, (void **)&ctx->jit_data);
227209

228210
if (!ctx->bpf2insn)
229211
ctx->bpf2insn_valid = false;
230212

231213
/* Freeing "bpf_header" is enough. "jit.buf" is a sub-array of it. */
232-
if (!ctx->success && ctx->bpf_header) {
233-
bpf_jit_binary_free(ctx->bpf_header);
234-
ctx->bpf_header = NULL;
235-
ctx->jit.buf = NULL;
236-
ctx->jit.index = 0;
237-
ctx->jit.len = 0;
214+
if (!ctx->success) {
215+
if (ctx->bpf_header) {
216+
bpf_jit_binary_free(ctx->bpf_header);
217+
ctx->bpf_header = NULL;
218+
ctx->jit.buf = NULL;
219+
ctx->jit.index = 0;
220+
ctx->jit.len = 0;
221+
}
222+
if (ctx->is_extra_pass) {
223+
ctx->prog->bpf_func = NULL;
224+
ctx->prog->jited = 0;
225+
ctx->prog->jited_len = 0;
226+
}
238227
}
239228

240229
ctx->emit = false;
@@ -1411,7 +1400,7 @@ static struct bpf_prog *do_extra_pass(struct bpf_prog *prog)
14111400
* (re)locations involved that their addresses are not known
14121401
* during the first run.
14131402
*/
1414-
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
1403+
struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
14151404
{
14161405
vm_dump(prog);
14171406

arch/arm/net/bpf_jit_32.c

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
18521852
{
18531853
u64 val = (u32)imm | (u64)insn[1].imm << 32;
18541854

1855+
if (insn->src_reg == BPF_PSEUDO_FUNC)
1856+
goto notyet;
1857+
18551858
emit_a32_mov_i64(dst, val, ctx);
18561859

18571860
return 1;
@@ -2055,6 +2058,9 @@ static int build_insn(const struct bpf_insn *insn, struct jit_ctx *ctx)
20552058
const s8 *r5 = bpf2a32[BPF_REG_5];
20562059
const u32 func = (u32)__bpf_call_base + (u32)imm;
20572060

2061+
if (insn->src_reg == BPF_PSEUDO_CALL)
2062+
goto notyet;
2063+
20582064
emit_a32_mov_r64(true, r0, r1, ctx);
20592065
emit_a32_mov_r64(true, r1, r2, ctx);
20602066
emit_push_r64(r5, ctx);
@@ -2142,11 +2148,9 @@ bool bpf_jit_needs_zext(void)
21422148
return true;
21432149
}
21442150

2145-
struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
2151+
struct bpf_prog *bpf_int_jit_compile(struct bpf_verifier_env *env, struct bpf_prog *prog)
21462152
{
2147-
struct bpf_prog *tmp, *orig_prog = prog;
21482153
struct bpf_binary_header *header;
2149-
bool tmp_blinded = false;
21502154
struct jit_ctx ctx;
21512155
unsigned int tmp_idx;
21522156
unsigned int image_size;
@@ -2156,20 +2160,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21562160
* the interpreter.
21572161
*/
21582162
if (!prog->jit_requested)
2159-
return orig_prog;
2160-
2161-
/* If constant blinding was enabled and we failed during blinding
2162-
* then we must fall back to the interpreter. Otherwise, we save
2163-
* the new JITed code.
2164-
*/
2165-
tmp = bpf_jit_blind_constants(prog);
2166-
2167-
if (IS_ERR(tmp))
2168-
return orig_prog;
2169-
if (tmp != prog) {
2170-
tmp_blinded = true;
2171-
prog = tmp;
2172-
}
2163+
return prog;
21732164

21742165
memset(&ctx, 0, sizeof(ctx));
21752166
ctx.prog = prog;
@@ -2179,10 +2170,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21792170
* we must fall back to the interpreter
21802171
*/
21812172
ctx.offsets = kcalloc(prog->len, sizeof(int), GFP_KERNEL);
2182-
if (ctx.offsets == NULL) {
2183-
prog = orig_prog;
2184-
goto out;
2185-
}
2173+
if (ctx.offsets == NULL)
2174+
return prog;
21862175

21872176
/* 1) fake pass to find in the length of the JITed code,
21882177
* to compute ctx->offsets and other context variables
@@ -2194,10 +2183,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
21942183
* being successful in the second pass, so just fall back
21952184
* to the interpreter.
21962185
*/
2197-
if (build_body(&ctx)) {
2198-
prog = orig_prog;
2186+
if (build_body(&ctx))
21992187
goto out_off;
2200-
}
22012188

22022189
tmp_idx = ctx.idx;
22032190
build_prologue(&ctx);
@@ -2213,10 +2200,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
22132200
ctx.idx += ctx.imm_count;
22142201
if (ctx.imm_count) {
22152202
ctx.imms = kcalloc(ctx.imm_count, sizeof(u32), GFP_KERNEL);
2216-
if (ctx.imms == NULL) {
2217-
prog = orig_prog;
2203+
if (ctx.imms == NULL)
22182204
goto out_off;
2219-
}
22202205
}
22212206
#else
22222207
/* there's nothing about the epilogue on ARMv7 */
@@ -2238,10 +2223,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
22382223
/* Not able to allocate memory for the structure then
22392224
* we must fall back to the interpretation
22402225
*/
2241-
if (header == NULL) {
2242-
prog = orig_prog;
2226+
if (header == NULL)
22432227
goto out_imms;
2244-
}
22452228

22462229
/* 2.) Actual pass to generate final JIT code */
22472230
ctx.target = (u32 *) image_ptr;
@@ -2278,16 +2261,12 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
22782261
#endif
22792262
out_off:
22802263
kfree(ctx.offsets);
2281-
out:
2282-
if (tmp_blinded)
2283-
bpf_jit_prog_release_other(prog, prog == orig_prog ?
2284-
tmp : orig_prog);
2264+
22852265
return prog;
22862266

22872267
out_free:
22882268
image_ptr = NULL;
22892269
bpf_jit_binary_free(header);
2290-
prog = orig_prog;
22912270
goto out_imms;
22922271
}
22932272

arch/arm64/lib/insn.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,8 @@ u32 aarch64_insn_gen_cond_branch_imm(unsigned long pc, unsigned long addr,
338338
long offset;
339339

340340
offset = label_imm_common(pc, addr, SZ_1M);
341+
if (offset >= SZ_1M)
342+
return AARCH64_BREAK_FAULT;
341343

342344
insn = aarch64_insn_get_bcond_value();
343345

0 commit comments

Comments
 (0)