]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
Revert "bpf: Take return from set_memory_rox() into account with bpf_jit_binary_lock_...
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 9 Jul 2024 09:14:05 +0000 (11:14 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 9 Jul 2024 09:44:29 +0000 (11:44 +0200)
This reverts commit 08f6c05feb1db21653e98ca84ea04ca032d014c7 which is
commit e60adf513275c3a38e5cb67f7fd12387e43a3ff5 upstream.

It is part of a series that is reported to both break the arm64 builds
and instantly crashes the powerpc systems at the first load of a bpf
program.  So revert it for now until it can come back in a safe way.

Reported-by: matoro <matoro_mailinglist_kernel@matoro.tk>
Reported-by: Vitaly Chikunov <vt@altlinux.org>
Reported-by: WangYuli <wangyuli@uniontech.com>
Link: https://lore.kernel.org/r/5A29E00D83AB84E3+20240706031101.637601-1-wangyuli@uniontech.com
Link: https://lore.kernel.org/r/cf736c5e37489e7dc7ffd67b9de2ab47@matoro.tk
Cc: Hari Bathini <hbathini@linux.ibm.com>
Cc: Song Liu <song@kernel.org>
Cc: Michael Ellerman <mpe@ellerman.id.au>
Cc: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Kees Cook <keescook@chromium.org>
Cc: Puranjay Mohan <puranjay12@gmail.com>
Cc: Ilya Leoshkevich <iii@linux.ibm.com> # s390x
Cc: Tiezhu Yang <yangtiezhu@loongson.cn> # LoongArch
Cc: Johan Almbladh <johan.almbladh@anyfinetworks.com> # MIPS Part
Cc: Alexei Starovoitov <ast@kernel.org>
Cc: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/arm/net/bpf_jit_32.c
arch/loongarch/net/bpf_jit.c
arch/mips/net/bpf_jit_comp.c
arch/parisc/net/bpf_jit_core.c
arch/s390/net/bpf_jit_comp.c
arch/sparc/net/bpf_jit_comp_64.c
arch/x86/net/bpf_jit_comp32.c
include/linux/filter.h

index ac8e4d9bf95442a83011f11590a0f94a190872c8..6a1c9fca5260b228b01010d77d1496f260a7bf96 100644 (file)
@@ -1982,21 +1982,28 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
        /* If building the body of the JITed code fails somehow,
         * we fall back to the interpretation.
         */
-       if (build_body(&ctx) < 0)
-               goto out_free;
+       if (build_body(&ctx) < 0) {
+               image_ptr = NULL;
+               bpf_jit_binary_free(header);
+               prog = orig_prog;
+               goto out_imms;
+       }
        build_epilogue(&ctx);
 
        /* 3.) Extra pass to validate JITed Code */
-       if (validate_code(&ctx))
-               goto out_free;
+       if (validate_code(&ctx)) {
+               image_ptr = NULL;
+               bpf_jit_binary_free(header);
+               prog = orig_prog;
+               goto out_imms;
+       }
        flush_icache_range((u32)header, (u32)(ctx.target + ctx.idx));
 
        if (bpf_jit_enable > 1)
                /* there are 2 passes here */
                bpf_jit_dump(prog->len, image_size, 2, ctx.target);
 
-       if (bpf_jit_binary_lock_ro(header))
-               goto out_free;
+       bpf_jit_binary_lock_ro(header);
        prog->bpf_func = (void *)ctx.target;
        prog->jited = 1;
        prog->jited_len = image_size;
@@ -2013,11 +2020,5 @@ out:
                bpf_jit_prog_release_other(prog, prog == orig_prog ?
                                           tmp : orig_prog);
        return prog;
-
-out_free:
-       image_ptr = NULL;
-       bpf_jit_binary_free(header);
-       prog = orig_prog;
-       goto out_imms;
 }
 
index 13cd480385ca8271b0e6ff730e4705641222e1b0..9eb7753d117dfba42dc96304de84d59e4e6f3875 100644 (file)
@@ -1206,19 +1206,16 @@ skip_init_ctx:
        flush_icache_range((unsigned long)header, (unsigned long)(ctx.image + ctx.idx));
 
        if (!prog->is_func || extra_pass) {
-               int err;
-
                if (extra_pass && ctx.idx != jit_data->ctx.idx) {
                        pr_err_once("multi-func JIT bug %d != %d\n",
                                    ctx.idx, jit_data->ctx.idx);
-                       goto out_free;
-               }
-               err = bpf_jit_binary_lock_ro(header);
-               if (err) {
-                       pr_err_once("bpf_jit_binary_lock_ro() returned %d\n",
-                                   err);
-                       goto out_free;
+                       bpf_jit_binary_free(header);
+                       prog->bpf_func = NULL;
+                       prog->jited = 0;
+                       prog->jited_len = 0;
+                       goto out_offset;
                }
+               bpf_jit_binary_lock_ro(header);
        } else {
                jit_data->ctx = ctx;
                jit_data->image = image_ptr;
@@ -1249,13 +1246,6 @@ out:
        out_offset = -1;
 
        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. */
index e355dfca440087520182a8a12d5f832ba86f31b9..a40d926b6513984623815b6e566d427260ccce22 100644 (file)
@@ -1012,8 +1012,7 @@ struct bpf_prog *bpf_int_jit_compile(struct bpf_prog *prog)
        bpf_prog_fill_jited_linfo(prog, &ctx.descriptors[1]);
 
        /* Set as read-only exec and flush instruction cache */
-       if (bpf_jit_binary_lock_ro(header))
-               goto out_err;
+       bpf_jit_binary_lock_ro(header);
        flush_icache_range((unsigned long)header,
                           (unsigned long)&ctx.target[ctx.jit_index]);
 
index 979f45d4d1fbeae5a60d92345409d413d352e7e1..d6ee2fd455503795e918c6f7cdd8d197eaca0eee 100644 (file)
@@ -167,13 +167,7 @@ skip_init_ctx:
        bpf_flush_icache(jit_data->header, ctx->insns + ctx->ninsns);
 
        if (!prog->is_func || extra_pass) {
-               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;
-               }
+               bpf_jit_binary_lock_ro(jit_data->header);
                prologue_len = ctx->epilogue_offset - ctx->body_len;
                for (i = 0; i < prog->len; i++)
                        ctx->offset[i] += prologue_len;
index 05746e22fe79c517541032c447d1ce319f3f9229..62ee557d4b4996605dffcc82ea9ebe7413297158 100644 (file)
@@ -1973,11 +1973,7 @@ skip_init_ctx:
                print_fn_code(jit.prg_buf, jit.size_prg);
        }
        if (!fp->is_func || extra_pass) {
-               if (bpf_jit_binary_lock_ro(header)) {
-                       bpf_jit_binary_free(header);
-                       fp = orig_fp;
-                       goto free_addrs;
-               }
+               bpf_jit_binary_lock_ro(header);
        } else {
                jit_data->header = header;
                jit_data->ctx = jit;
index 73bf0aea8baf14711c3d094d93aa782a65b382bf..fa0759bfe498e917b0b215d2a24d1269d645161f 100644 (file)
@@ -1602,11 +1602,7 @@ skip_init_ctx:
        bpf_flush_icache(header, (u8 *)header + header->size);
 
        if (!prog->is_func || extra_pass) {
-               if (bpf_jit_binary_lock_ro(header)) {
-                       bpf_jit_binary_free(header);
-                       prog = orig_prog;
-                       goto out_off;
-               }
+               bpf_jit_binary_lock_ro(header);
        } else {
                jit_data->ctx = ctx;
                jit_data->image = image_ptr;
index f2fc8c38629b56a280c78210eae50d77bafc2bcd..429a89c5468b5748c01728193d7eeb82fadf2376 100644 (file)
@@ -2600,7 +2600,8 @@ out_image:
        if (bpf_jit_enable > 1)
                bpf_jit_dump(prog->len, proglen, pass + 1, image);
 
-       if (image && !bpf_jit_binary_lock_ro(header)) {
+       if (image) {
+               bpf_jit_binary_lock_ro(header);
                prog->bpf_func = (void *)image;
                prog->jited = 1;
                prog->jited_len = proglen;
index a74d97114a5427d07aff4dc9b70b10c715f83c90..5a2800ec94ea6472becfd44711dc9e0b1ea565c6 100644 (file)
@@ -853,11 +853,10 @@ static inline int __must_check bpf_prog_lock_ro(struct bpf_prog *fp)
        return 0;
 }
 
-static inline int __must_check
-bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
+static inline void bpf_jit_binary_lock_ro(struct bpf_binary_header *hdr)
 {
        set_vm_flush_reset_perms(hdr);
-       return set_memory_rox((unsigned long)hdr, hdr->size >> PAGE_SHIFT);
+       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);