From patchwork Mon Aug 26 06:55:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mike Rapoport X-Patchwork-Id: 13777232 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 60032C5472D for ; Mon, 26 Aug 2024 07:02:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A0558D0040; Mon, 26 Aug 2024 03:02:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0015F8D0047; Mon, 26 Aug 2024 03:02:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B907F8D0040; Mon, 26 Aug 2024 03:02:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 78E1D8D0029 for ; Mon, 26 Aug 2024 03:02:23 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2F0691A0C7A for ; Mon, 26 Aug 2024 07:02:23 +0000 (UTC) X-FDA: 82493502966.21.5B2A936 Received: from sin.source.kernel.org (sin.source.kernel.org [145.40.73.55]) by imf06.hostedemail.com (Postfix) with ESMTP id DDDAE180009 for ; Mon, 26 Aug 2024 07:02:20 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DYK9oNeM; spf=pass (imf06.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724655656; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0ipojPJka61m/dDVaL1rj5Xz8WsXALdkbVZGJd5RdxE=; b=Sos/TXWB56MHet6y2AjHlNisA8t2NgrP5oKb6kBrTEzXMKLIHaIaykE+EE/JuKcD8tCsG5 JmNI+4O/SY9nS/1nky6AWcoB9TV+7j9vjhhGNM1Hvmm7W7K/6BL9euPB0uQLc6iFaNkbUc y6lqi4bbTs/JEE8oSSZ5O5A9LaE0CQY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724655656; a=rsa-sha256; cv=none; b=YICCnByuVL5vMgZEdCQMj1l0fNDhfVqX47zAk4gweQBsYRIaRIYEEm68hFcq/WmX3G/c5m nFz2mep8Mqh/6DPtGL9AiGYkJfACE8OTEIhvdSWKz+Zdhz3YXmMK0ofWuGx5qY+W698vnl uk3lsaEvDTRN0FFaRVa8uIbgczyIgeo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=DYK9oNeM; spf=pass (imf06.hostedemail.com: domain of rppt@kernel.org designates 145.40.73.55 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 4AD9ACE0B9F; Mon, 26 Aug 2024 07:02:17 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3E073C521E0; Mon, 26 Aug 2024 06:56:42 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1724655416; bh=r822LaZRSo48kh1hH5WoG20uCbxKP6lu+ngNzNWB1aE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DYK9oNeMv3/VMADQKPjCojtZBfz9RuwZwds9R0avsETK/hiJ48/NmAdPUuLyylz7w 6+d1+Viqdarxcw2qygDqcIzxOB89mv22pkZ7jmQeRBIjFdFL+sPkAcsK75SA7Cl68K LWspJaAQiUlCQCTId5Fm/c9iHwvxY2+F69No7D2KvwHQOERk0yaCx8qDPsl+9qvsAk iILGxprzy+kNT7Efacq4WICC1ssWjOLPoCc+l6nwAV+IC0eH4bzt2soEhB5CaZIXke 5y9fZ7a+Ox2xqigNBk/V7YAZFdsD4Othq+DHAQY8jfW1gNwY201ZJT08FIZQRS56Vp gUFbTLvgCXNcA== From: Mike Rapoport To: Andrew Morton Cc: Andreas Larsson , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Brian Cain , Catalin Marinas , Christoph Hellwig , Christophe Leroy , Dave Hansen , Dinh Nguyen , Geert Uytterhoeven , Guo Ren , Helge Deller , Huacai Chen , Ingo Molnar , Johannes Berg , John Paul Adrian Glaubitz , Kent Overstreet , "Liam R. Howlett" , Luis Chamberlain , Mark Rutland , Masami Hiramatsu , Matt Turner , Max Filippov , Michael Ellerman , Michal Simek , Mike Rapoport , Oleg Nesterov , Palmer Dabbelt , Peter Zijlstra , Richard Weinberger , Russell King , Song Liu , Stafford Horne , Steven Rostedt , Thomas Bogendoerfer , Thomas Gleixner , Uladzislau Rezki , Vineet Gupta , Will Deacon , bpf@vger.kernel.org, linux-alpha@vger.kernel.org, linux-arch@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-csky@vger.kernel.org, linux-hexagon@vger.kernel.org, linux-kernel@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-mips@vger.kernel.org, linux-mm@kvack.org, linux-modules@vger.kernel.org, linux-openrisc@vger.kernel.org, linux-parisc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-sh@vger.kernel.org, linux-snps-arc@lists.infradead.org, linux-trace-kernel@vger.kernel.org, linux-um@lists.infradead.org, linuxppc-dev@lists.ozlabs.org, loongarch@lists.linux.dev, sparclinux@vger.kernel.org, x86@kernel.org Subject: [PATCH v2 4/8] module: prepare to handle ROX allocations for text Date: Mon, 26 Aug 2024 09:55:28 +0300 Message-ID: <20240826065532.2618273-5-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240826065532.2618273-1-rppt@kernel.org> References: <20240826065532.2618273-1-rppt@kernel.org> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DDDAE180009 X-Stat-Signature: 1xnjw9ekz4rafjuhpqpiwmte9frw39tb X-HE-Tag: 1724655740-834760 X-HE-Meta: U2FsdGVkX196ECnw5958E00sEyRHMfjjWecsCPmn4QUsC1g7OQo0jBqcKThG2HiICI5HkGzaExmAQBV3co0KlLa0M4xeELNl5ObX18rrOhUYjvavHtvZnDcrXl4/teeBzdih3H+w20uCtxK8k46JlFTDaMREA4nyvUUN9S+SW49AZ5cRpzbnqJinnezwngZ+fPEP4tEO+IANtzOhBkxZpIt25q4jVrHQwNtYuTF8E/3ifR7LW7KmjedVrhAhoyIZ70itZ5ujsXSTSRSw6B/HVXpYhW2vd7HoB81X3EzjYj8EtXkwOsfJptOetNbIZEVOw0yPlrpbr/KC61BWJ66WJPPwRsRdKYvWlSdAqUswQdo7MNjC/cvNwNoIWcIoGsacabjgVqJKlqwpDmtoa/kKjSQIquesJVcs8t8x2lVjVGT7rbZnWiEIoSFON2Iy0JJ4UqaisIF6TLAtI6qtAW2Ct8lyH0G5dbZN7cASa1Pex5M6WLRMmCssAzRV5XLiIAwAId3zVPMXX3KI/sqXCstWhkjrD45evzXG47JL1uYWQXUBVe4RbkmpNLBOmw6KtFD414q8xCl8UNosth2Il4RMDD28ird974C9WeTztEWP/8CIN6QUUeBXVVAV90GQfX9Ch9pf1RYXiIo4Pe7ueToFv/0NYcj8kNOaBsYzHjwMMHavA4C4xVm/U6IRL/VCeAYNl6OwG+KlLD/A3W8m/L3CSgeQ112QaKFXBTzfKLhnQ4kSdrOfJK+KsS2GivQr/Q540OpK/LK62qLgpRjr/lb12gKWhiU4+J3ExtrUNGQspEYGxLpsnOaRcWu93Lb9RkDKFL9i0YBoUzXTSv1jc+NdBTOsdi7v79syspn9O0iYyImybP+7lpqNKHm5UJS4UPAl4shsfo48RBZ0hl/I/9v0YmQi/O2IjhBgcGY2k2QIaYuW870Ven/RTb3dw0CiALk1gvhhNoZSfGXgmbdyWPW njpc/ZW0 ubkLGdaOyhbvtROpOpBTLQnEPVzlgNqVLIZvCflNDLfIW9q/fL4+1eCH2IGSZGU8hHKqII7zFk680CHjI3e36dDXjb0csqZqGNQm/KebnHssHwW6xWYczGtMHJRIL29KChbldJQmDLBSPFGvAx12c8lzf/WaVHn77gv4LJbmtnJdzlq2i6ZqliX7Bwp7ya0jhG8xFtZ450fSSYIzdnPmlPq8Js6QEskl+UCg8Gysx+pHeelF0l8Mog5qBHg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" In order to support ROX allocations for module text, it is necessary to handle modifications to the code, such as relocations and alternatives patching, without write access to that memory. One option is to use text patching, but this would make module loading extremely slow and will expose executable code that is not finally formed. A better way is to have memory allocated with ROX permissions contain invalid instructions and keep a writable, but not executable copy of the module text. The relocations and alternative patches would be done on the writable copy using the addresses of the ROX memory. Once the module is completely ready, the updated text will be copied to ROX memory using text patching in one go and the writable copy will be freed. Add support for that to module initialization code and provide necessary interfaces in execmem. Signed-off-by: Mike Rapoport (Microsoft) --- include/linux/execmem.h | 23 +++++++++++ include/linux/module.h | 11 ++++++ include/linux/moduleloader.h | 4 ++ kernel/module/main.c | 77 ++++++++++++++++++++++++++++++++---- kernel/module/strict_rwx.c | 3 ++ mm/execmem.c | 11 ++++++ 6 files changed, 122 insertions(+), 7 deletions(-) diff --git a/include/linux/execmem.h b/include/linux/execmem.h index 32cef1144117..dfdf19f8a5e8 100644 --- a/include/linux/execmem.h +++ b/include/linux/execmem.h @@ -46,9 +46,11 @@ enum execmem_type { /** * enum execmem_range_flags - options for executable memory allocations * @EXECMEM_KASAN_SHADOW: allocate kasan shadow + * @EXECMEM_ROX_CACHE: allocations should use ROX cache of huge pages */ enum execmem_range_flags { EXECMEM_KASAN_SHADOW = (1 << 0), + EXECMEM_ROX_CACHE = (1 << 1), }; /** @@ -123,6 +125,27 @@ void *execmem_alloc(enum execmem_type type, size_t size); */ void execmem_free(void *ptr); +/** + * execmem_update_copy - copy an update to executable memory + * @dst: destination address to update + * @src: source address containing the data + * @size: how many bytes of memory shold be copied + * + * Copy @size bytes from @src to @dst using text poking if the memory at + * @dst is read-only. + * + * Return: a pointer to @dst or NULL on error + */ +void *execmem_update_copy(void *dst, const void *src, size_t size); + +/** + * execmem_is_rox - check if execmem is read-only + * @type - the execmem type to check + * + * Return: %true if the @type is read-only, %false if it's writable + */ +bool execmem_is_rox(enum execmem_type type); + #if defined(CONFIG_EXECMEM) && !defined(CONFIG_ARCH_WANTS_EXECMEM_LATE) void execmem_init(void); #else diff --git a/include/linux/module.h b/include/linux/module.h index 88ecc5e9f523..7039f609c6ef 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -367,6 +367,8 @@ enum mod_mem_type { struct module_memory { void *base; + void *rw_copy; + bool is_rox; unsigned int size; #ifdef CONFIG_MODULES_TREE_LOOKUP @@ -374,6 +376,15 @@ struct module_memory { #endif }; +#ifdef CONFIG_MODULES +void *module_writable_address(struct module *mod, void *loc); +#else +static inline void *module_writable_address(struct module *mod, void *loc) +{ + return loc; +} +#endif + #ifdef CONFIG_MODULES_TREE_LOOKUP /* Only touch one cacheline for common rbtree-for-core-layout case. */ #define __module_memory_align ____cacheline_aligned diff --git a/include/linux/moduleloader.h b/include/linux/moduleloader.h index e395461d59e5..1f5507ba5a12 100644 --- a/include/linux/moduleloader.h +++ b/include/linux/moduleloader.h @@ -108,6 +108,10 @@ int module_finalize(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs, struct module *mod); +int module_post_finalize(const Elf_Ehdr *hdr, + const Elf_Shdr *sechdrs, + struct module *mod); + #ifdef CONFIG_MODULES void flush_module_init_free_work(void); #else diff --git a/kernel/module/main.c b/kernel/module/main.c index 71396e297499..55f53f0095d4 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1189,6 +1189,21 @@ void __weak module_arch_freeing_init(struct module *mod) { } +void *module_writable_address(struct module *mod, void *loc) +{ + if (!mod) + return loc; + + for_class_mod_mem_type(type, text) { + struct module_memory *mem = &mod->mem[type]; + + if (loc >= mem->base && loc < mem->base + mem->size) + return loc + (mem->rw_copy - mem->base); + } + + return loc; +} + static int module_memory_alloc(struct module *mod, enum mod_mem_type type) { unsigned int size = PAGE_ALIGN(mod->mem[type].size); @@ -1206,6 +1221,23 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) if (!ptr) return -ENOMEM; + mod->mem[type].base = ptr; + + if (execmem_is_rox(execmem_type)) { + ptr = vzalloc(size); + + if (!ptr) { + execmem_free(mod->mem[type].base); + return -ENOMEM; + } + + mod->mem[type].rw_copy = ptr; + mod->mem[type].is_rox = true; + } else { + mod->mem[type].rw_copy = mod->mem[type].base; + memset(mod->mem[type].base, 0, size); + } + /* * The pointer to these blocks of memory are stored on the module * structure and we keep that around so long as the module is @@ -1219,16 +1251,17 @@ static int module_memory_alloc(struct module *mod, enum mod_mem_type type) */ kmemleak_not_leak(ptr); - memset(ptr, 0, size); - mod->mem[type].base = ptr; - return 0; } static void module_memory_free(struct module *mod, enum mod_mem_type type, bool unload_codetags) { - void *ptr = mod->mem[type].base; + struct module_memory *mem = &mod->mem[type]; + void *ptr = mem->base; + + if (mem->is_rox) + vfree(mem->rw_copy); if (!unload_codetags && mod_mem_type_is_core_data(type)) return; @@ -2251,6 +2284,7 @@ static int move_module(struct module *mod, struct load_info *info) for_each_mod_mem_type(type) { if (!mod->mem[type].size) { mod->mem[type].base = NULL; + mod->mem[type].rw_copy = NULL; continue; } @@ -2267,11 +2301,14 @@ static int move_module(struct module *mod, struct load_info *info) void *dest; Elf_Shdr *shdr = &info->sechdrs[i]; enum mod_mem_type type = shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; + unsigned long offset = shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK; + unsigned long addr; if (!(shdr->sh_flags & SHF_ALLOC)) continue; - dest = mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK); + addr = (unsigned long)mod->mem[type].base + offset; + dest = mod->mem[type].rw_copy + offset; if (shdr->sh_type != SHT_NOBITS) { /* @@ -2293,7 +2330,7 @@ static int move_module(struct module *mod, struct load_info *info) * users of info can keep taking advantage and using the newly * minted official memory area. */ - shdr->sh_addr = (unsigned long)dest; + shdr->sh_addr = addr; pr_debug("\t0x%lx 0x%.8lx %s\n", (long)shdr->sh_addr, (long)shdr->sh_size, info->secstrings + shdr->sh_name); } @@ -2441,8 +2478,17 @@ int __weak module_finalize(const Elf_Ehdr *hdr, return 0; } +int __weak module_post_finalize(const Elf_Ehdr *hdr, + const Elf_Shdr *sechdrs, + struct module *me) +{ + return 0; +} + static int post_relocation(struct module *mod, const struct load_info *info) { + int ret; + /* Sort exception table now relocations are done. */ sort_extable(mod->extable, mod->extable + mod->num_exentries); @@ -2454,7 +2500,24 @@ static int post_relocation(struct module *mod, const struct load_info *info) add_kallsyms(mod, info); /* Arch-specific module finalizing. */ - return module_finalize(info->hdr, info->sechdrs, mod); + ret = module_finalize(info->hdr, info->sechdrs, mod); + if (ret) + return ret; + + for_each_mod_mem_type(type) { + struct module_memory *mem = &mod->mem[type]; + + if (mem->is_rox) { + if (!execmem_update_copy(mem->base, mem->rw_copy, + mem->size)) + return -ENOMEM; + + vfree(mem->rw_copy); + mem->rw_copy = NULL; + } + } + + return module_post_finalize(info->hdr, info->sechdrs, mod); } /* Call module constructors. */ diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c index c45caa4690e5..239e5013359d 100644 --- a/kernel/module/strict_rwx.c +++ b/kernel/module/strict_rwx.c @@ -34,6 +34,9 @@ int module_enable_text_rox(const struct module *mod) for_class_mod_mem_type(type, text) { int ret; + if (mod->mem[type].is_rox) + continue; + if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) ret = module_set_memory(mod, type, set_memory_rox); else diff --git a/mm/execmem.c b/mm/execmem.c index 0c4b36bc6d10..0f6691e9ffe6 100644 --- a/mm/execmem.c +++ b/mm/execmem.c @@ -10,6 +10,7 @@ #include #include #include +#include static struct execmem_info *execmem_info __ro_after_init; static struct execmem_info default_execmem_info __ro_after_init; @@ -69,6 +70,16 @@ void execmem_free(void *ptr) vfree(ptr); } +void *execmem_update_copy(void *dst, const void *src, size_t size) +{ + return text_poke_copy(dst, src, size); +} + +bool execmem_is_rox(enum execmem_type type) +{ + return !!(execmem_info->ranges[type].flags & EXECMEM_ROX_CACHE); +} + static bool execmem_validate(struct execmem_info *info) { struct execmem_range *r = &info->ranges[EXECMEM_DEFAULT];