From patchwork Thu Dec 5 19:46:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Leroy X-Patchwork-Id: 13895960 Received: from pegase2.c-s.fr (pegase2.c-s.fr [93.17.235.10]) (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 B07FE19E961; Thu, 5 Dec 2024 19:46:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.235.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733427992; cv=none; b=rc3R9ZTIojmtUMdKxbQWU3B5P6rX8eokQUV55AV60WyldkHLS4RM167YPC7JYIs3+xL6T5waEZHJj1o1i4RW1mBtKCyIkMYnbc/9axshic2u6x4vsH7rha+DWhKX63wIOvjJr5DDhn9zw3kpCuVFUyplwRIKt0FzWgqdsitDjBU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733427992; c=relaxed/simple; bh=6Fm0gx1a6AkZGf3Kqs5LhL/NAVxTyKNyuj72or1ZIYY=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=YkwiykCsfi8BPKOWXZuTOQeagFjRDmMEnY+FA2QkewK0Zi10f5DYSmHYDN005O0n0o4e2DPb4H9aOFoHTyybi2jSEfIF7/hWWt+KdPGNmthk+aNE2Ta6BuUs1ThnTI3vDV0XxXCrkHqtcaqqXCZdGIn8lPx0qK8rg40XdTiqLGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.235.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [172.26.127.67]) by localhost (Postfix) with ESMTP id 4Y44dT0nnHz9svj; Thu, 5 Dec 2024 20:46:21 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase2.c-s.fr ([172.26.127.65]) by localhost (pegase2.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ZR9QcpiaDyPm; Thu, 5 Dec 2024 20:46:21 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase2.c-s.fr (Postfix) with ESMTP id 4Y44dS6dPvz9svg; Thu, 5 Dec 2024 20:46:20 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id CF9BD8B76E; Thu, 5 Dec 2024 20:46:20 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id jt0TSl1Xf3E8; Thu, 5 Dec 2024 20:46:20 +0100 (CET) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.232.97]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 53F298B770; Thu, 5 Dec 2024 20:46:20 +0100 (CET) From: Christophe Leroy To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Kees Cook , linux-modules@vger.kernel.org Cc: Christophe Leroy , linux-kernel@vger.kernel.org, Thomas Gleixner Subject: [PATCH v1 1/3] module: Split module_enable_rodata_ro() Date: Thu, 5 Dec 2024 20:46:15 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1733427977; l=3253; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=6Fm0gx1a6AkZGf3Kqs5LhL/NAVxTyKNyuj72or1ZIYY=; b=rX1PuStiWXqDboVJ/ZxLvRqUML1O9fmBQJJWxXgBi+Gebgl5x2ONREaqOmaU0VYrBcmavVlSU nnCK+wQiW6CAJteuuuteOgNMszKvle5cXb/GWuYMQNEGV+iAWLBdTP8 X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= module_enable_rodata_ro() is called twice, once before module init to set rodata sections readonly and once after module init to set rodata_after_init section readonly. The second time, only the rodata_after_init section needs to be set to read-only, no need to re-apply it to already set rodata. Split module_enable_rodata_ro() in two. Signed-off-by: Christophe Leroy Tested-by: Daniel Gomez --- kernel/module/internal.h | 3 ++- kernel/module/main.c | 4 ++-- kernel/module/strict_rwx.c | 13 +++++++++---- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2ebece8a789f..994f35a779dc 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -322,7 +322,8 @@ static inline struct module *mod_find(unsigned long addr, struct mod_tree_root * } #endif /* CONFIG_MODULES_TREE_LOOKUP */ -int module_enable_rodata_ro(const struct module *mod, bool after_init); +int module_enable_rodata_ro(const struct module *mod); +int module_enable_rodata_ro_after_init(const struct module *mod); int module_enable_data_nx(const struct module *mod); int module_enable_text_rox(const struct module *mod); int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, diff --git a/kernel/module/main.c b/kernel/module/main.c index d2e1b8976c7b..39fe5d81b6a8 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2673,7 +2673,7 @@ static noinline int do_init_module(struct module *mod) /* Switch to core kallsyms now init is done: kallsyms may be walking! */ rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms); #endif - ret = module_enable_rodata_ro(mod, true); + ret = module_enable_rodata_ro_after_init(mod); if (ret) goto fail_mutex_unlock; mod_tree_remove_init(mod); @@ -2843,7 +2843,7 @@ static int complete_formation(struct module *mod, struct load_info *info) module_bug_finalize(info->hdr, info->sechdrs, mod); module_cfi_finalize(info->hdr, info->sechdrs, mod); - err = module_enable_rodata_ro(mod, false); + err = module_enable_rodata_ro(mod); if (err) goto out_strict_rwx; err = module_enable_data_nx(mod); diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c index 239e5013359d..74834ba15615 100644 --- a/kernel/module/strict_rwx.c +++ b/kernel/module/strict_rwx.c @@ -47,7 +47,7 @@ int module_enable_text_rox(const struct module *mod) return 0; } -int module_enable_rodata_ro(const struct module *mod, bool after_init) +int module_enable_rodata_ro(const struct module *mod) { int ret; @@ -61,12 +61,17 @@ int module_enable_rodata_ro(const struct module *mod, bool after_init) if (ret) return ret; - if (after_init) - return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro); - return 0; } +int module_enable_rodata_ro_after_init(const struct module *mod) +{ + if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX) || !rodata_enabled) + return 0; + + return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro); +} + int module_enable_data_nx(const struct module *mod) { if (!IS_ENABLED(CONFIG_STRICT_MODULE_RWX)) From patchwork Thu Dec 5 19:46:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Leroy X-Patchwork-Id: 13895961 Received: from pegase2.c-s.fr (pegase2.c-s.fr [93.17.235.10]) (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 CC18722E419; Thu, 5 Dec 2024 19:46:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.235.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733427997; cv=none; b=hwLNhyTZ468ra9id95g1flcLpiSnIJ38GsS8qJT04mPCcswAW4TGCdSP75EmN1eeRjizr6jYZ3S4JMR91EJTDE3bKTipUc6KejLX4TYhYo6ldIik/eBjISqAm7lftfxYteJ+vBQghy72gVnyOMOrRggn9r3EZ4boKy5J1wYaTTQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733427997; c=relaxed/simple; bh=8q2j6oY2j7jBVhN9HdnphDr7Cw59vtHJ7ZgyENJ4vY8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Hy7NYko6ZhjiEs5qpsXziuePLJFcl4tqRQ7gCXyrGH9jBpxYGPMFU33BzRHmtY4cnivP3P6uLZzmp8uQ6p7OI4TJwg/dYvyG7CL7o7x7xemX8W7W6vUFhA45mdgeHxnFHi9pdl2niJtRHrYnr0Kt/tEbWx4o7B0ozpez3RRsSdk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.235.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [172.26.127.67]) by localhost (Postfix) with ESMTP id 4Y44dV75Kzz9svl; Thu, 5 Dec 2024 20:46:22 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase2.c-s.fr ([172.26.127.65]) by localhost (pegase2.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id ODfdm_7cbVPI; Thu, 5 Dec 2024 20:46:22 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase2.c-s.fr (Postfix) with ESMTP id 4Y44dT2SbZz9svg; Thu, 5 Dec 2024 20:46:21 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 4363C8B76E; Thu, 5 Dec 2024 20:46:21 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id oX7KGUuo9kQt; Thu, 5 Dec 2024 20:46:21 +0100 (CET) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.232.97]) by messagerie.si.c-s.fr (Postfix) with ESMTP id C98EA8B763; Thu, 5 Dec 2024 20:46:20 +0100 (CET) From: Christophe Leroy To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Kees Cook , linux-modules@vger.kernel.org Cc: Christophe Leroy , linux-kernel@vger.kernel.org, Thomas Gleixner Subject: [PATCH v1 2/3] module: Don't fail module loading when setting ro_after_init section RO failed Date: Thu, 5 Dec 2024 20:46:16 +0100 Message-ID: X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1733427977; l=1393; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=8q2j6oY2j7jBVhN9HdnphDr7Cw59vtHJ7ZgyENJ4vY8=; b=UEZ+N40ZDpO5GVZVXdCUO5lkkuhyK1548+DIf0nFX4s0jTmwRJ8aLNCoeGDi6LaePdf/ylwVC iMFKHYAxRk+CEnR0tmf/MLAu8OWvklLq2qovuiENuo7URtgxEsuDtbD X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= Once module init has succeded it is too late to cancel loading. If setting ro_after_init data section to read-only fails, all we can do is to inform the user through a warning. Reported-by: Thomas Gleixner Closes: https://lore.kernel.org/all/20230915082126.4187913-1-ruanjinjie@huawei.com/ Fixes: d1909c022173 ("module: Don't ignore errors from set_memory_XX()") Signed-off-by: Christophe Leroy --- v1: Fixed comment from Petr about __func__ --- kernel/module/main.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 39fe5d81b6a8..5f922e563fc3 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2675,7 +2675,10 @@ static noinline int do_init_module(struct module *mod) #endif ret = module_enable_rodata_ro_after_init(mod); if (ret) - goto fail_mutex_unlock; + pr_warn("%s: module_enable_rodata_ro_after_init() returned %d, " + "ro_after_init data might still be writable\n", + mod->name, ret); + mod_tree_remove_init(mod); module_arch_freeing_init(mod); for_class_mod_mem_type(type, init) { @@ -2714,8 +2717,6 @@ static noinline int do_init_module(struct module *mod) return 0; -fail_mutex_unlock: - mutex_unlock(&module_mutex); fail_free_freeinit: kfree(freeinit); fail: From patchwork Thu Dec 5 19:46:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Leroy X-Patchwork-Id: 13895962 Received: from pegase2.c-s.fr (pegase2.c-s.fr [93.17.235.10]) (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 4C43622B8A2; Thu, 5 Dec 2024 19:46:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=93.17.235.10 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733428002; cv=none; b=HdgZt/k/ke/KgmoV5OboSOCogsg24tZBx86qKEb9oRpr9BLpmsXWEttLwuXb5JaBdu+fYX3BqTqIA2YZCeAbNzkzbR90y2Okze9dVDxK3zhliup3huW0FB3JCAGUvJ6hMq/BG/JlTPZkW9eMV/PMyMRgRQY6UW5oeHiUbxjB2EM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1733428002; c=relaxed/simple; bh=+a76SSSyXo/Z4P5dmmrzicA4u00ZpV3dbYc40THbTmM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=X40SbjqgnAIPiPMm5zDj3qZ/QVfzDBZpGomPY5t5D+BPgcmSUmxrnocLrOhwRAGggstXCJvr5rYRVF0XkTtIWSjApUBDwsZu0QyRnkE7M4K1mxq9IHgm1gQs55fKLhxhnkqwTHO9J5wXncGxNv2urIJLS5qxhLb+zmb6IzVtDGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu; spf=pass smtp.mailfrom=csgroup.eu; arc=none smtp.client-ip=93.17.235.10 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=csgroup.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=csgroup.eu Received: from localhost (mailhub3.si.c-s.fr [172.26.127.67]) by localhost (Postfix) with ESMTP id 4Y44dX6p4Yz9svg; Thu, 5 Dec 2024 20:46:24 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from pegase2.c-s.fr ([172.26.127.65]) by localhost (pegase2.c-s.fr [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id SAf4glO79EIQ; Thu, 5 Dec 2024 20:46:24 +0100 (CET) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase2.c-s.fr (Postfix) with ESMTP id 4Y44dT5kV4z9svk; Thu, 5 Dec 2024 20:46:21 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id B52DE8B763; Thu, 5 Dec 2024 20:46:21 +0100 (CET) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id Ox0P3sLaevPw; Thu, 5 Dec 2024 20:46:21 +0100 (CET) Received: from PO20335.idsi0.si.c-s.fr (unknown [192.168.232.97]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 44CD28B770; Thu, 5 Dec 2024 20:46:21 +0100 (CET) From: Christophe Leroy To: Luis Chamberlain , Petr Pavlu , Sami Tolvanen , Daniel Gomez , Kees Cook , linux-modules@vger.kernel.org Cc: Christophe Leroy , linux-kernel@vger.kernel.org, Thomas Gleixner Subject: [PATCH v1 3/3] module: pre-test setting ro_after_init data read-only Date: Thu, 5 Dec 2024 20:46:17 +0100 Message-ID: <84c4caf2fbb71fa1699ee24d5b6df548d7f3bb08.1733427536.git.christophe.leroy@csgroup.eu> X-Mailer: git-send-email 2.47.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-modules@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Developer-Signature: v=1; a=ed25519-sha256; t=1733427977; l=2430; i=christophe.leroy@csgroup.eu; s=20211009; h=from:subject:message-id; bh=+a76SSSyXo/Z4P5dmmrzicA4u00ZpV3dbYc40THbTmM=; b=EUWw73At6FPbHZpOKK8cpi+ddGAVKTmAOhVh3Lnrz05hNjyyy9LkMf8TWyBFKZHwZKoYqO3qX EYQLNVthm0ECcsJczhvY3ZYtcds0BdzIz+9mS14OBVvwYasfJnmgLQX X-Developer-Key: i=christophe.leroy@csgroup.eu; a=ed25519; pk=HIzTzUj91asvincQGOFx6+ZF5AoUuP9GdOtQChs7Mm0= To be on the safe side, try to set ro_after_init data section readonly at the same time as rodata. If it fails it will likely fail again later so let's cancel module loading while we still can do it. If it doesn't fail, put it back to read-write, continue module loading and cross fingers so that it still works after module init. In practice, if it worked once it will work twice: - On some architecture like powerpc it works on some memory areas and works on others. If you apply it several times to the same area, either it always works or it always fails - On some architecture like ARM, that may apply splitting big pages into smaller ones, that is what can fails, but once it successed the pages will remain split so there's no reason to fail on pass two if it worked on pass one. Then it should in principle never fail so add a WARN_ON_ONCE() to get a big fat warning in case it happens anyway. For systems that sets panic-on-warn, such systems usely care about security and don't want vulnerable systems, so an implied panic is worth it in that case. Signed-off-by: Christophe Leroy --- v1: Expanded the commit message based on feedback from RFC series --- kernel/module/main.c | 2 +- kernel/module/strict_rwx.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/kernel/module/main.c b/kernel/module/main.c index 5f922e563fc3..634a4a2aaf8c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2674,7 +2674,7 @@ static noinline int do_init_module(struct module *mod) rcu_assign_pointer(mod->kallsyms, &mod->core_kallsyms); #endif ret = module_enable_rodata_ro_after_init(mod); - if (ret) + if (WARN_ON_ONCE(ret)) pr_warn("%s: module_enable_rodata_ro_after_init() returned %d, " "ro_after_init data might still be writable\n", mod->name, ret); diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c index 74834ba15615..1434c48c52ab 100644 --- a/kernel/module/strict_rwx.c +++ b/kernel/module/strict_rwx.c @@ -61,7 +61,10 @@ int module_enable_rodata_ro(const struct module *mod) if (ret) return ret; - return 0; + ret = module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro); + if (ret) + return ret; + return module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_rw); } int module_enable_rodata_ro_after_init(const struct module *mod)