mirror of
https://github.com/nxp-imx/linux-imx.git
synced 2025-07-14 21:29:37 +02:00
bpf: Take return from set_memory_rox() into account with bpf_jit_binary_lock_ro()
[ Upstream commit e60adf5132
]
set_memory_rox() can fail, leaving memory unprotected.
Check return and bail out when bpf_jit_binary_lock_ro() returns
an error.
Link: https://github.com/KSPP/linux/issues/7
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: linux-hardening@vger.kernel.org <linux-hardening@vger.kernel.org>
Reviewed-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Puranjay Mohan <puranjay12@gmail.com>
Reviewed-by: Ilya Leoshkevich <iii@linux.ibm.com> # s390x
Acked-by: Tiezhu Yang <yangtiezhu@loongson.cn> # LoongArch
Reviewed-by: Johan Almbladh <johan.almbladh@anyfinetworks.com> # MIPS Part
Message-ID: <036b6393f23a2032ce75a1c92220b2afcb798d5d.1709850515.git.christophe.leroy@csgroup.eu>
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:
parent
f99feda568
commit
08f6c05feb
|
@ -1982,28 +1982,21 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
|
||||||
/* If building the body of the JITed code fails somehow,
|
/* If building the body of the JITed code fails somehow,
|
||||||
* we fall back to the interpretation.
|
* we fall back to the interpretation.
|
||||||
*/
|
*/
|
||||||
if (build_body(&ctx) < 0) {
|
if (build_body(&ctx) < 0)
|
||||||
image_ptr = NULL;
|
goto out_free;
|
||||||
bpf_jit_binary_free(header);
|
|
||||||
prog = orig_prog;
|
|
||||||
goto out_imms;
|
|
||||||
}
|
|
||||||
build_epilogue(&ctx);
|
build_epilogue(&ctx);
|
||||||
|
|
||||||
/* 3.) Extra pass to validate JITed Code */
|
/* 3.) Extra pass to validate JITed Code */
|
||||||
if (validate_code(&ctx)) {
|
if (validate_code(&ctx))
|
||||||
image_ptr = NULL;
|
goto out_free;
|
||||||
bpf_jit_binary_free(header);
|
|
||||||
prog = orig_prog;
|
|
||||||
goto out_imms;
|
|
||||||
}
|
|
||||||
flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
|
flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
|
||||||
|
|
||||||
if (bpf_jit_enable > 1)
|
if (bpf_jit_enable > 1)
|
||||||
/* there are 2 passes here */
|
/* there are 2 passes here */
|
||||||
bpf_jit_dump(prog->len, image_size, 2, ctx.target);
|
bpf_jit_dump(prog->len, image_size, 2, ctx.target);
|
||||||
|
|
||||||
bpf_jit_binary_lock_ro(header);
|
if (bpf_jit_binary_lock_ro(header))
|
||||||
|
goto out_free;
|
||||||
prog->bpf_func = (void *)ctx.target;
|
prog->bpf_func = (void *)ctx.target;
|
||||||
prog->jited = 1;
|
prog->jited = 1;
|
||||||
prog->jited_len = image_size;
|
prog->jited_len = image_size;
|
||||||
|
@ -2020,5 +2013,11 @@ out:
|
||||||
bpf_jit_prog_release_other(prog, prog == orig_prog ?
|
bpf_jit_prog_release_other(prog, prog == orig_prog ?
|
||||||
tmp : orig_prog);
|
tmp : orig_prog);
|
||||||
return prog;
|
return prog;
|
||||||
|
|
||||||
|
out_free:
|
||||||
|
image_ptr = NULL;
|
||||||
|
bpf_jit_binary_free(header);
|
||||||
|
prog = orig_prog;
|
||||||
|
goto out_imms;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1206,16 +1206,19 @@ skip_init_ctx:
|
||||||
flush_icache_range((unsigned long)header, (unsigned long)(ctx.image + ctx.idx));
|
flush_icache_range((unsigned long)header, (unsigned long)(ctx.image + ctx.idx));
|
||||||
|
|
||||||
if (!prog->is_func || extra_pass) {
|
if (!prog->is_func || extra_pass) {
|
||||||
|
int err;
|
||||||
|
|
||||||
if (extra_pass && ctx.idx != jit_data->ctx.idx) {
|
if (extra_pass && ctx.idx != jit_data->ctx.idx) {
|
||||||
pr_err_once("multi-func JIT bug %d != %d\n",
|
pr_err_once("multi-func JIT bug %d != %d\n",
|
||||||
ctx.idx, jit_data->ctx.idx);
|
ctx.idx, jit_data->ctx.idx);
|
||||||
bpf_jit_binary_free(header);
|
goto out_free;
|
||||||
prog->bpf_func = NULL;
|
}
|
||||||
prog->jited = 0;
|
err = bpf_jit_binary_lock_ro(header);
|
||||||
prog->jited_len = 0;
|
if (err) {
|
||||||
goto out_offset;
|
pr_err_once("bpf_jit_binary_lock_ro() returned %d\n",
|
||||||
|
err);
|
||||||
|
goto out_free;
|
||||||
}
|
}
|
||||||
bpf_jit_binary_lock_ro(header);
|
|
||||||
} else {
|
} else {
|
||||||
jit_data->ctx = ctx;
|
jit_data->ctx = ctx;
|
||||||
jit_data->image = image_ptr;
|
jit_data->image = image_ptr;
|
||||||
|
@ -1246,6 +1249,13 @@ out:
|
||||||
out_offset = -1;
|
out_offset = -1;
|
||||||
|
|
||||||
return prog;
|
return prog;
|
||||||
|
|
||||||
|
out_free:
|
||||||
|
bpf_jit_binary_free(header);
|
||||||
|
prog->bpf_func = NULL;
|
||||||
|
prog->jited = 0;
|
||||||
|
prog->jited_len = 0;
|
||||||
|
goto out_offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
|
/* Indicate the JIT backend supports mixing bpf2bpf and tailcalls. */
|
||||||
|
|
|
@ -1012,7 +1012,8 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
|
||||||
bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]);
|
bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]);
|
||||||
|
|
||||||
/* Set as read-only exec and flush instruction cache */
|
/* Set as read-only exec and flush instruction cache */
|
||||||
bpf_jit_binary_lock_ro(header);
|
if (bpf_jit_binary_lock_ro(header))
|
||||||
|
goto out_err;
|
||||||
flush_icache_range((unsigned long)header,
|
flush_icache_range((unsigned long)header,
|
||||||
(unsigned long)&ctx.target[ctx.jit_index]);
|
(unsigned long)&ctx.target[ctx.jit_index]);
|
||||||
|
|
||||||
|
|
|
@ -167,7 +167,13 @@ skip_init_ctx:
|
||||||
bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
|
bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
|
||||||
|
|
||||||
if (!prog->is_func || extra_pass) {
|
if (!prog->is_func || extra_pass) {
|
||||||
bpf_jit_binary_lock_ro(jit_data->header);
|
if (bpf_jit_binary_lock_ro(jit_data->header)) {
|
||||||
|
bpf_jit_binary_free(jit_data->header);
|
||||||
|
prog->bpf_func = NULL;
|
||||||
|
prog->jited = 0;
|
||||||
|
prog->jited_len = 0;
|
||||||
|
goto out_offset;
|
||||||
|
}
|
||||||
prologue_len = ctx->epilogue_offset - ctx->body_len;
|
prologue_len = ctx->epilogue_offset - ctx->body_len;
|
||||||
for (i = 0; i < prog->len; i++)
|
for (i = 0; i < prog->len; i++)
|
||||||
ctx->offset[i] += prologue_len;
|
ctx->offset[i] += prologue_len;
|
||||||
|
|
|
@ -1973,7 +1973,11 @@ skip_init_ctx:
|
||||||
print_fn_code(jit.prg_buf, jit.size_prg);
|
print_fn_code(jit.prg_buf, jit.size_prg);
|
||||||
}
|
}
|
||||||
if (!fp->is_func || extra_pass) {
|
if (!fp->is_func || extra_pass) {
|
||||||
bpf_jit_binary_lock_ro(header);
|
if (bpf_jit_binary_lock_ro(header)) {
|
||||||
|
bpf_jit_binary_free(header);
|
||||||
|
fp = orig_fp;
|
||||||
|
goto free_addrs;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
jit_data->header = header;
|
jit_data->header = header;
|
||||||
jit_data->ctx = jit;
|
jit_data->ctx = jit;
|
||||||
|
|
|
@ -1602,7 +1602,11 @@ skip_init_ctx:
|
||||||
bpf_flush_icache(header, (u8 *)header + header->size);
|
bpf_flush_icache(header, (u8 *)header + header->size);
|
||||||
|
|
||||||
if (!prog->is_func || extra_pass) {
|
if (!prog->is_func || extra_pass) {
|
||||||
bpf_jit_binary_lock_ro(header);
|
if (bpf_jit_binary_lock_ro(header)) {
|
||||||
|
bpf_jit_binary_free(header);
|
||||||
|
prog = orig_prog;
|
||||||
|
goto out_off;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
jit_data->ctx = ctx;
|
jit_data->ctx = ctx;
|
||||||
jit_data->image = image_ptr;
|
jit_data->image = image_ptr;
|
||||||
|
|
|
@ -2600,8 +2600,7 @@ out_image:
|
||||||
if (bpf_jit_enable > 1)
|
if (bpf_jit_enable > 1)
|
||||||
bpf_jit_dump(prog->len, proglen, pass + 1, image);
|
bpf_jit_dump(prog->len, proglen, pass + 1, image);
|
||||||
|
|
||||||
if (image) {
|
if (image && !bpf_jit_binary_lock_ro(header)) {
|
||||||
bpf_jit_binary_lock_ro(header);
|
|
||||||
prog->bpf_func = (void *)image;
|
prog->bpf_func = (void *)image;
|
||||||
prog->jited = 1;
|
prog->jited = 1;
|
||||||
prog->jited_len = proglen;
|
prog->jited_len = proglen;
|
||||||
|
|
|
@ -853,10 +853,11 @@ static inline int __must_check bpf_prog_lock_ro(struct bpf_prog *fp)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
|
static inline int __must_check
|
||||||
|
bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
|
||||||
{
|
{
|
||||||
set_vm_flush_reset_perms(hdr);
|
set_vm_flush_reset_perms(hdr);
|
||||||
set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
|
return set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
|
int sk_filter_trim_cap(struct sock *sk, struct sk_buff *skb, unsigned int cap);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user