Received: by 2002:a5d:9c59:0:0:0:0:0 with SMTP id 25csp116171iof; Sun, 5 Jun 2022 22:51:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1gmTpi2remX/yOQOoZcg/EY3HXj0ADlzLqJjx6Y0KLP+h0fe/reAm//V8wrXP7ENJo71m X-Received: by 2002:a17:903:1212:b0:15e:7d94:e21d with SMTP id l18-20020a170903121200b0015e7d94e21dmr22650373plh.92.1654494697853; Sun, 05 Jun 2022 22:51:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1654494697; cv=none; d=google.com; s=arc-20160816; b=AndnAUxUPpBePZy3Wx+Es+NK3bqK3TrOkAhRN0LZ58xpjxQEt2Qzqv5ius0Gxq24// rdkedcKAc8s4Cur3csVR2auk77ecuBB5sU4HF2Vxs7bxOhgogjl3PC6Di1APoHS+JzHb d1nTLO87zwjp8siu69K9a/mO1l16g+b20Y3LHK/5isQwKrEo42TD+LR5WRjNcD5XVXUk S5ZqnfAh+u4/E0CZjNqr6XbXLrJitkotyR+fkNX4Wnq9uvprSuSBSnqvXldFu6bhgX/I xr4pKlIboLGr82vRnPhASmsAODsgvGjQIBLKZ+jR+hNFtk7pHkgD646SeS+nyOohd0GU Th+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=WFvKoEfwrcwk1nWb6KCJTXsSYwP4hTrjoKk55OOy3uY=; b=SCX5VhETFLLzcgrbG6JK7GRC3awB08uDzHlltqszvk4Qti+hJwZmqJFlEmL4WEoPVa E8/AEliWWX15zuttSkEvSJM/tbgzFHajPrk/PpvBXc6wZO9Tmnrqg8brCryXlwST7Eu1 81hMVzxPzBpKgNHUyCP0AlI/B80QCJ9hTLFTezeNmu1DwtcnWaWjKM4BMYfbiwne+0Tj U1FfY7pDYvJ0BZpR08WPUrEeJWe3nf40zSk++wkNglHXqjfciCFsbl98/082o5hDzcDM VpU8mP1cOXardr6sB8se2ZJa+MvLl3PhuuBeudBOCrsaoHBp81Na2gPd/zbDvyJb9hNs kq/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uAK016By; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id gc24-20020a17090b311800b001c6edf679b4si21293420pjb.46.2022.06.05.22.51.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 05 Jun 2022 22:51:37 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uAK016By; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 168CC2A1408; Sun, 5 Jun 2022 21:40:38 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347211AbiFCSUH (ORCPT + 99 others); Fri, 3 Jun 2022 14:20:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40992 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347801AbiFCSGS (ORCPT ); Fri, 3 Jun 2022 14:06:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2BF3D5D5CD; Fri, 3 Jun 2022 10:59:24 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 68E21615E5; Fri, 3 Jun 2022 17:58:47 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 642E2C385A9; Fri, 3 Jun 2022 17:58:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1654279126; bh=4qTz1o5Y1+SqJrvnr8q5XVJw1rkEY0oLUtfdFOURr8A=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uAK016Byp+ScOQuO2eal9dYnqHNZ+DtYDKWSi7SINslFOxQfjf1BelEy4ZWVTkgvI B7XumuRK+EuGfEucL8IG9+I7mBECFskSM8YYkzqebzagv/djW2bWf9HT73dqbroZDb ffvwBRxbB+PUBqiCA1Sw0iRbm8VGi7u0Kt2aZOhY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Andrii Nakryiko , Alexei Starovoitov , Daniel Borkmann , Martin KaFai Lau Subject: [PATCH 5.18 61/67] bpf: Fix combination of jit blinding and pointers to bpf subprogs. Date: Fri, 3 Jun 2022 19:44:02 +0200 Message-Id: <20220603173822.476439161@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220603173820.731531504@linuxfoundation.org> References: <20220603173820.731531504@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-3.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alexei Starovoitov commit 4b6313cf99b0d51b49aeaea98ec76ca8161ecb80 upstream. The combination of jit blinding and pointers to bpf subprogs causes: [ 36.989548] BUG: unable to handle page fault for address: 0000000100000001 [ 36.990342] #PF: supervisor instruction fetch in kernel mode [ 36.990968] #PF: error_code(0x0010) - not-present page [ 36.994859] RIP: 0010:0x100000001 [ 36.995209] Code: Unable to access opcode bytes at RIP 0xffffffd7. [ 37.004091] Call Trace: [ 37.004351] [ 37.004576] ? bpf_loop+0x4d/0x70 [ 37.004932] ? bpf_prog_3899083f75e4c5de_F+0xe3/0x13b The jit blinding logic didn't recognize that ld_imm64 with an address of bpf subprogram is a special instruction and proceeded to randomize it. By itself it wouldn't have been an issue, but jit_subprogs() logic relies on two step process to JIT all subprogs and then JIT them again when addresses of all subprogs are known. Blinding process in the first JIT phase caused second JIT to miss adjustment of special ld_imm64. Fix this issue by ignoring special ld_imm64 instructions that don't have user controlled constants and shouldn't be blinded. Fixes: 69c087ba6225 ("bpf: Add bpf_for_each_map_elem() helper") Reported-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Signed-off-by: Daniel Borkmann Acked-by: Andrii Nakryiko Acked-by: Martin KaFai Lau Link: https://lore.kernel.org/bpf/20220513011025.13344-1-alexei.starovoitov@gmail.com Signed-off-by: Greg Kroah-Hartman --- kernel/bpf/core.c | 10 ++++++++++ 1 file changed, 10 insertions(+) --- a/kernel/bpf/core.c +++ b/kernel/bpf/core.c @@ -1436,6 +1436,16 @@ struct bpf_prog *bpf_jit_blind_constants insn = clone->insnsi; for (i = 0; i < insn_cnt; i++, insn++) { + if (bpf_pseudo_func(insn)) { + /* ld_imm64 with an address of bpf subprog is not + * a user controlled constant. Don't randomize it, + * since it will conflict with jit_subprogs() logic. + */ + insn++; + i++; + continue; + } + /* We temporarily need to hold the original ld64 insn * so that we can still access the first part in the * second blinding run.