From nobody Thu Feb 12 04:51:37 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2CB5AC77B60 for ; Fri, 28 Apr 2023 09:55:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345827AbjD1JzX (ORCPT ); Fri, 28 Apr 2023 05:55:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345873AbjD1JzH (ORCPT ); Fri, 28 Apr 2023 05:55:07 -0400 Received: from out187-22.us.a.mail.aliyun.com (out187-22.us.a.mail.aliyun.com [47.90.187.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F4D15FFA; Fri, 28 Apr 2023 02:54:42 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R191e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047188;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=26;SR=0;TI=SMTPD_---.STDfs9K_1682675604; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.STDfs9K_1682675604) by smtp.aliyun-inc.com; Fri, 28 Apr 2023 17:53:25 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Thomas Garnier" , "Lai Jiangshan" , "Kees Cook" , "Hou Wenlong" , "Alexei Starovoitov" , "Daniel Borkmann" , "Andrii Nakryiko" , "Martin KaFai Lau" , "Song Liu" , "Yonghong Song" , "John Fastabend" , "KP Singh" , "Stanislav Fomichev" , "Hao Luo" , "Jiri Olsa" , "David S. Miller" , "David Ahern" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , , Subject: [PATCH RFC 30/43] x86/bpf: Adapt BPF_CALL JIT codegen for PIE support Date: Fri, 28 Apr 2023 17:51:10 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" If image is NULL, ip calculated is in bottom address and func is in kernel image address, then the offset is valid when the kernel stays in the top 2G address space. However, PIE kernel image could be below top 2G, which makes the offset out of range. Since the length of PC-relative call instruction is fixed, it's pointless to calculate the offset without the proper image base (it has been zero until the last pass). Use 1 as the dummy offset to generate the instruction in the first pass. Signed-off-by: Hou Wenlong Cc: Thomas Garnier Cc: Lai Jiangshan Cc: Kees Cook --- arch/x86/net/bpf_jit_comp.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/arch/x86/net/bpf_jit_comp.c b/arch/x86/net/bpf_jit_comp.c index 1056bbf55b17..0da41833e426 100644 --- a/arch/x86/net/bpf_jit_comp.c +++ b/arch/x86/net/bpf_jit_comp.c @@ -1549,8 +1549,21 @@ st: if (is_imm8(insn->off)) return -EINVAL; offs =3D x86_call_depth_emit_accounting(&prog, func); } - if (emit_call(&prog, func, image + addrs[i - 1] + offs)) - return -EINVAL; + /* + * If image is NULL, ip is in bottom address and func + * is in kernel image address (top 2G), so the offset + * is valid. However, PIE kernel image could be below + * top 2G, then the offset would be out of range. Since + * the length of PC-relative call(0xe8) is fixed, so it's + * pointless to calculate the offset until the last pass. + * Use 1 as the dummy offset if image is NULL. + */ + if (image) + err =3D emit_call(&prog, func, image + addrs[i - 1] + offs); + else + err =3D emit_call(&prog, (void *)(X86_PATCH_SIZE + 1UL), 0); + if (err) + return err; break; } =20 --=20 2.31.1