Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp1860993rwr; Fri, 28 Apr 2023 02:55:35 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6GgskWipJ7jQAEtT4s7gWhfeS43ajYppYHcP8oqPo0aawh5Q+hps3LWCCaKaFd2SbseC5O X-Received: by 2002:a17:902:e848:b0:1a0:450d:a481 with SMTP id t8-20020a170902e84800b001a0450da481mr5656561plg.35.1682675734863; Fri, 28 Apr 2023 02:55:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682675734; cv=none; d=google.com; s=arc-20160816; b=Ig6tDJbZs5Aw0s5yrZFjdfpM6zTr/YT58l9e/hvj5kaG/aAB39FXqYVbVU9IXraXz0 kFPw8LgR218bnAQOnwncMu+QwzZ9toV+MbTDOYJXGgWEfdBpgrN4KsdG0hbXtve3LoGq bUk4CVFBAAHIONsCFSqtgrKE/NVPmoew2Vmpl12eyqHgvqYvsSdU50Tk81Lke51OV1P0 tCTS57mr6pPzuAgVP/0DVeHX5Ctv6+pal1EntQHy4ukxiHvViJz7nNaodKRKG5HZzaxr S2aMWghrCwI0zxh2fCfuEgPS5IjUYE5Zj3Qw+HjGYL6sd4/SodkvbFmaURECYkiv+CkZ DtOQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=Mw/oMadipzUfVubb09fVpf+MLmYXFQrY6rQ3ZBAlysU=; b=A6m+UXWv7Eq5qPWIPcpmAMD6WPjFhS2O9v6vVDZwTbnZ34cbGTU6rC6lx6an+sNm1I FOnR6O9XXYeO90z2H6kzxCkwWydGVkzNs2nqaVWWV4WtbwFRUZCt5QI46jHVhXZbX3YZ iNDvpxahjLN+0WFXOsY03ObIaf7ya9XNlcRW+zzjsWm9HtMFsospGP3KwiNOh9fltFFE kTZEfJd6Qy3dM6zmV+35e/YhEvM/L/4UVBB9M6BWc3WRQqboNQBbS46jPxmbtThkPJR+ Yt9PzvCHoinhYe48ot3gvjs2HDkav9wFc8n+Jx4Cz41sHfExZfv5crnyQTv3eUDon4b8 S1qQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id i1-20020a170902cf0100b001a986e5bd92si10191610plg.83.2023.04.28.02.55.23; Fri, 28 Apr 2023 02:55:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345839AbjD1JzP (ORCPT + 99 others); Fri, 28 Apr 2023 05:55:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345706AbjD1JzF (ORCPT ); Fri, 28 Apr 2023 05:55:05 -0400 Received: from out0-201.mail.aliyun.com (out0-201.mail.aliyun.com [140.205.0.201]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 35C344EE2 for ; Fri, 28 Apr 2023 02:54:41 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R171e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047205;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=18;SR=0;TI=SMTPD_---.STFQGTg_1682675609; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.STFQGTg_1682675609) by smtp.aliyun-inc.com; Fri, 28 Apr 2023 17:53:30 +0800 From: "Hou Wenlong" To: linux-kernel@vger.kernel.org Cc: "Thomas Garnier" , "Lai Jiangshan" , "Kees Cook" , "Hou Wenlong" , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , "Peter Zijlstra" , "Petr Mladek" , "Greg Kroah-Hartman" , "Jason A. Donenfeld" , "Song Liu" , "Julian Pidancet" , "Ard Biesheuvel" Subject: [PATCH RFC 31/43] x86/modules: Adapt module loading for PIE support Date: Fri, 28 Apr 2023 17:51:11 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE,UNPARSEABLE_RELAY autolearn=ham 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 Adapt module loading to support PIE relocations. No GOT is generared for module, all the GOT entry of got references in module should exist in kernel GOT. Currently, there is only one usable got reference for __fentry__(). Signed-off-by: Hou Wenlong Cc: Thomas Garnier Cc: Lai Jiangshan Cc: Kees Cook --- arch/x86/include/asm/sections.h | 5 +++++ arch/x86/kernel/module.c | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/arch/x86/include/asm/sections.h b/arch/x86/include/asm/sections.h index a6e8373a5170..dc1c2b08ec48 100644 --- a/arch/x86/include/asm/sections.h +++ b/arch/x86/include/asm/sections.h @@ -12,6 +12,11 @@ extern char __end_rodata_aligned[]; #if defined(CONFIG_X86_64) extern char __end_rodata_hpage_align[]; + +#ifdef CONFIG_X86_PIE +extern char __start_got[], __end_got[]; +#endif + #endif extern char __end_of_kernel_reserve[]; diff --git a/arch/x86/kernel/module.c b/arch/x86/kernel/module.c index 84ad0e61ba6e..051f88e6884e 100644 --- a/arch/x86/kernel/module.c +++ b/arch/x86/kernel/module.c @@ -129,6 +129,18 @@ int apply_relocate(Elf32_Shdr *sechdrs, return 0; } #else /*X86_64*/ +#ifdef CONFIG_X86_PIE +static u64 find_got_kernel_entry(Elf64_Sym *sym, const Elf64_Rela *rela) +{ + u64 *pos; + + for (pos = (u64 *)__start_got; pos < (u64 *)__end_got; pos++) + if (*pos == sym->st_value) + return (u64)pos + rela->r_addend; + return 0; +} +#endif + static int __write_relocate_add(Elf64_Shdr *sechdrs, const char *strtab, unsigned int symindex, @@ -171,6 +183,7 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs, case R_X86_64_64: size = 8; break; +#ifndef CONFIG_X86_PIE case R_X86_64_32: if (val != *(u32 *)&val) goto overflow; @@ -181,6 +194,13 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs, goto overflow; size = 4; break; +#else + case R_X86_64_GOTPCREL: + val = find_got_kernel_entry(sym, rel); + if (!val) + goto unexpected_got_reference; + fallthrough; +#endif case R_X86_64_PC32: case R_X86_64_PLT32: val -= (u64)loc; @@ -214,11 +234,18 @@ static int __write_relocate_add(Elf64_Shdr *sechdrs, } return 0; +#ifdef CONFIG_X86_PIE +unexpected_got_reference: + pr_err("Target got entry doesn't exist in kernel got, loc %p\n", loc); + return -ENOEXEC; +#else overflow: pr_err("overflow in relocation type %d val %Lx\n", (int)ELF64_R_TYPE(rel[i].r_info), val); pr_err("`%s' likely not compiled with -mcmodel=kernel\n", me->name); +#endif + return -ENOEXEC; } -- 2.31.1