From nobody Tue Nov 26 00:27:36 2024 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BF0B11CBEB5; Wed, 23 Oct 2024 16:28:42 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700923; cv=none; b=ZaGVt51TykdKL1m0DZ6PiyrEOdTjas8YvDVuVB1A9NskbYH2hEZdwhYfTG6Xkuh3GcVYvy6aSOu7f7Idm5dWrQxpSEcSNB7S+cuRylFGcHyvqjPxdy6bCbN19TR+QTRLEfaR9LMODC65oT/vdaz5stmAMznO95M+2ZRz7kuA02s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729700923; c=relaxed/simple; bh=xb4svDXwFaBxninI5I+0LNtfAySGXZnMRdp5fkel8t8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VV3CAIW6YIjeF2qQKE/a9gyUcR7Y4T9ePjFY6jjCMpp3ltRIkrkjdc76TS4byd52UB9D4o1RuocdlwwMyPaFbvGGDoD66SfTlTXFKPPW1R+rY853I4RKcHwVipVabEXM2wUDe1nTdEb1+9OoPHGKIicwQnB8CShHPkfF/3B4kko= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DWJBcVWM; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DWJBcVWM" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6313BC4CEE6; Wed, 23 Oct 2024 16:28:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1729700922; bh=xb4svDXwFaBxninI5I+0LNtfAySGXZnMRdp5fkel8t8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DWJBcVWMJM+2XvLsPWqvMDvcMLPrCfAisg4mS38/DaGVrHFraG/T0LyRWjmnMW3YQ 6K4yoL+Jy2tksBnKk+p+o1VWMTYELJNvrl4+GQ9xwQbA8dPta2mwhb30Kvftw9Jjc5 q7Xk2Y30pj+8msd4ifQeu69k1ePJV/eOdN2WUUUj7+l13tFClJ7KgfqSJ4iksyWcbE OPOnQct+s1MyXqH8VAEC1U6/GMos1tYg/95JGkKQN20y3FQk8VlXYuYjLngQyklXzA vNZV7bXiVqwH81GW1G22p3XhUzgHyvZq4w/9jYJne6g1s8hRM56G0jlA9cHHjEkzch swuz7EyS0soiA== From: Mike Rapoport To: Andrew Morton , Luis Chamberlain Cc: Andreas Larsson , Andy Lutomirski , Ard Biesheuvel , 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" , 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 , Suren Baghdasaryan , 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 v7 4/8] module: prepare to handle ROX allocations for text Date: Wed, 23 Oct 2024 19:27:07 +0300 Message-ID: <20241023162711.2579610-5-rppt@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241023162711.2579610-1-rppt@kernel.org> References: <20241023162711.2579610-1-rppt@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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) Reviewd-by: Luis Chamberlain Tested-by: kdevops --- include/linux/execmem.h | 23 +++++++++++ include/linux/module.h | 16 ++++++++ include/linux/moduleloader.h | 4 ++ kernel/module/debug_kmemleak.c | 3 +- kernel/module/main.c | 74 ++++++++++++++++++++++++++++++---- kernel/module/strict_rwx.c | 3 ++ mm/execmem.c | 11 +++++ 7 files changed, 126 insertions(+), 8 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 =3D (1 << 0), + EXECMEM_ROX_CACHE =3D (1 << 1), }; =20 /** @@ -123,6 +125,27 @@ void *execmem_alloc(enum execmem_type type, size_t siz= e); */ void execmem_free(void *ptr); =20 +/** + * 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..2a9386cbdf85 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -367,6 +367,8 @@ enum mod_mem_type { =20 struct module_memory { void *base; + void *rw_copy; + bool is_rox; unsigned int size; =20 #ifdef CONFIG_MODULES_TREE_LOOKUP @@ -767,6 +769,15 @@ static inline bool is_livepatch_module(struct module *= mod) =20 void set_module_sig_enforced(void); =20 +void *__module_writable_address(struct module *mod, void *loc); + +static inline void *module_writable_address(struct module *mod, void *loc) +{ + if (!IS_ENABLED(CONFIG_ARCH_HAS_EXECMEM_ROX) || !mod) + return loc; + return __module_writable_address(mod, loc); +} + #else /* !CONFIG_MODULES... */ =20 static inline struct module *__module_address(unsigned long addr) @@ -874,6 +885,11 @@ static inline bool module_is_coming(struct module *mod) { return false; } + +static inline void *module_writable_address(struct module *mod, void *loc) +{ + return loc; +} #endif /* CONFIG_MODULES */ =20 #ifdef CONFIG_SYSFS 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); =20 +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/debug_kmemleak.c b/kernel/module/debug_kmemleak.c index b4cc03842d70..df873dad049d 100644 --- a/kernel/module/debug_kmemleak.c +++ b/kernel/module/debug_kmemleak.c @@ -14,7 +14,8 @@ void kmemleak_load_module(const struct module *mod, { /* only scan writable, non-executable sections */ for_each_mod_mem_type(type) { - if (type !=3D MOD_DATA && type !=3D MOD_INIT_DATA) + if (type !=3D MOD_DATA && type !=3D MOD_INIT_DATA && + !mod->mem[type].is_rox) kmemleak_no_scan(mod->mem[type].base); } } diff --git a/kernel/module/main.c b/kernel/module/main.c index 49b9bca9de12..73b588fe98d4 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1189,6 +1189,18 @@ void __weak module_arch_freeing_init(struct module *= mod) { } =20 +void *__module_writable_address(struct module *mod, void *loc) +{ + for_class_mod_mem_type(type, text) { + struct module_memory *mem =3D &mod->mem[type]; + + if (loc >=3D 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 =3D PAGE_ALIGN(mod->mem[type].size); @@ -1206,6 +1218,23 @@ static int module_memory_alloc(struct module *mod, e= num mod_mem_type type) if (!ptr) return -ENOMEM; =20 + mod->mem[type].base =3D ptr; + + if (execmem_is_rox(execmem_type)) { + ptr =3D vzalloc(size); + + if (!ptr) { + execmem_free(mod->mem[type].base); + return -ENOMEM; + } + + mod->mem[type].rw_copy =3D ptr; + mod->mem[type].is_rox =3D true; + } else { + mod->mem[type].rw_copy =3D 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 +1248,17 @@ static int module_memory_alloc(struct module *mod, = enum mod_mem_type type) */ kmemleak_not_leak(ptr); =20 - memset(ptr, 0, size); - mod->mem[type].base =3D ptr; - return 0; } =20 static void module_memory_free(struct module *mod, enum mod_mem_type type, bool unload_codetags) { - void *ptr =3D mod->mem[type].base; + struct module_memory *mem =3D &mod->mem[type]; + void *ptr =3D mem->base; + + if (mem->is_rox) + vfree(mem->rw_copy); =20 if (!unload_codetags && mod_mem_type_is_core_data(type)) return; @@ -2251,6 +2281,7 @@ static int move_module(struct module *mod, struct loa= d_info *info) for_each_mod_mem_type(type) { if (!mod->mem[type].size) { mod->mem[type].base =3D NULL; + mod->mem[type].rw_copy =3D NULL; continue; } =20 @@ -2267,11 +2298,14 @@ static int move_module(struct module *mod, struct l= oad_info *info) void *dest; Elf_Shdr *shdr =3D &info->sechdrs[i]; enum mod_mem_type type =3D shdr->sh_entsize >> SH_ENTSIZE_TYPE_SHIFT; + unsigned long offset =3D shdr->sh_entsize & SH_ENTSIZE_OFFSET_MASK; + unsigned long addr; =20 if (!(shdr->sh_flags & SHF_ALLOC)) continue; =20 - dest =3D mod->mem[type].base + (shdr->sh_entsize & SH_ENTSIZE_OFFSET_MAS= K); + addr =3D (unsigned long)mod->mem[type].base + offset; + dest =3D mod->mem[type].rw_copy + offset; =20 if (shdr->sh_type !=3D SHT_NOBITS) { /* @@ -2293,7 +2327,7 @@ static int move_module(struct module *mod, struct loa= d_info *info) * users of info can keep taking advantage and using the newly * minted official memory area. */ - shdr->sh_addr =3D (unsigned long)dest; + shdr->sh_addr =3D addr; pr_debug("\t0x%lx 0x%.8lx %s\n", (long)shdr->sh_addr, (long)shdr->sh_size, info->secstrings + shdr->sh_name); } @@ -2441,8 +2475,17 @@ int __weak module_finalize(const Elf_Ehdr *hdr, return 0; } =20 +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 *inf= o) { + int ret; + /* Sort exception table now relocations are done. */ sort_extable(mod->extable, mod->extable + mod->num_exentries); =20 @@ -2454,7 +2497,24 @@ static int post_relocation(struct module *mod, const= struct load_info *info) add_kallsyms(mod, info); =20 /* Arch-specific module finalizing. */ - return module_finalize(info->hdr, info->sechdrs, mod); + ret =3D module_finalize(info->hdr, info->sechdrs, mod); + if (ret) + return ret; + + for_each_mod_mem_type(type) { + struct module_memory *mem =3D &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 =3D NULL; + } + } + + return module_post_finalize(info->hdr, info->sechdrs, mod); } =20 /* 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; =20 + if (mod->mem[type].is_rox) + continue; + if (IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) ret =3D 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 =20 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); } =20 +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 =3D &info->ranges[EXECMEM_DEFAULT]; --=20 2.43.0