From patchwork Tue Dec 28 21:30:35 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aaron Tomlin X-Patchwork-Id: 12700633 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 59E04C433FE for ; Tue, 28 Dec 2021 21:30:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237577AbhL1Vay (ORCPT ); Tue, 28 Dec 2021 16:30:54 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:39659 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237496AbhL1Vav (ORCPT ); Tue, 28 Dec 2021 16:30:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1640727050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uueBGakT/gT7TUjBGwoofDgqQKuu/ItyPgp72/wDjB0=; b=UV64UiE0cenDMbREPH7RzEW1hviqMgY63FEvojA3HethXh0+ST59pczzjPQYIv6vMuk7+i Nt0M73TFKjEoCMcNYVaBBVk0Bm4KMqF/j5cFq0vdBtgj7LrubTPi16So8Yn/4Aszph29CD /2z927tkqo76COFXs+LBvH4RjTpmgcs= Received: from mail-wr1-f70.google.com (mail-wr1-f70.google.com [209.85.221.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-150-cXrec3B9N8ifPhrggc1qWQ-1; Tue, 28 Dec 2021 16:30:49 -0500 X-MC-Unique: cXrec3B9N8ifPhrggc1qWQ-1 Received: by mail-wr1-f70.google.com with SMTP id j26-20020adfb31a000000b001a2356afd4fso4293930wrd.21 for ; Tue, 28 Dec 2021 13:30:49 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=uueBGakT/gT7TUjBGwoofDgqQKuu/ItyPgp72/wDjB0=; b=zFVbdURCvad993xptPdraWYHHpJXbvxDBR+NnIFkG58gZcsyc756OkhXtYQIr1ym5x 85Yh1U/DPzqT+sS9H3HGVOxqxczVroJ8QDremXMjIOtBCdZ/9TR0mzypKOdwpbFOR2BI KLEZp+5hygplITEph/zK62Y9z1q19t9oTDhyNp1QqGd1DtKPlelmy13eX+RKVGbOFyj8 5fwdAo3gYbaiTy+PREqTIHKqfiuFtcRI8Qez4NVTP820tmmpXd3+SxdaaNPtiu+VqysL OH4bLLtO19TgPXr4/2tKLp9eRpBPt3UDjAPUg/FjRvwz38Pcl6Vl4MP4ww/51DgLZ6Ap 2akw== X-Gm-Message-State: AOAM532PuBFFUuecmS2bdTwf2lqd6/ERKK1U0om9VF0UoVCpUNO+Cgh9 hZ0QdpumSInQ3zOAjZUXVIg4Xc7qh9abzBTinkvDOpDU7dz/bmE2IAMlKWRiSqQlRB9mBrvoCy2 CXBvwWz7YlE0uTBiTes5IJ3fE X-Received: by 2002:a5d:64e7:: with SMTP id g7mr17559919wri.203.1640727048350; Tue, 28 Dec 2021 13:30:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJws6dB1esH1e3GaF+mmVl28v1qQERbn6j07cqiDgOXtM4kgKG6I+CwoPZ6kiT0DmOhpvOraGg== X-Received: by 2002:a5d:64e7:: with SMTP id g7mr17559910wri.203.1640727048120; Tue, 28 Dec 2021 13:30:48 -0800 (PST) Received: from localhost (cpc111743-lutn13-2-0-cust979.9-3.cable.virginm.net. [82.17.115.212]) by smtp.gmail.com with ESMTPSA id c2sm21117243wri.50.2021.12.28.13.30.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 Dec 2021 13:30:47 -0800 (PST) From: Aaron Tomlin To: mcgrof@kernel.org Cc: cl@linux.com, pmladek@suse.com, mbenes@suse.cz, akpm@linux-foundation.org, jeyu@kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, atomlin@atomlin.com, ghalat@redhat.com Subject: [RFC PATCH 06/12] module: Move strict rwx support to a separate file Date: Tue, 28 Dec 2021 21:30:35 +0000 Message-Id: <20211228213041.1356334-7-atomlin@redhat.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20211228213041.1356334-1-atomlin@redhat.com> References: <20211228213041.1356334-1-atomlin@redhat.com> MIME-Version: 1.0 Precedence: bulk List-ID: No functional change. This patch migrates code that makes module text and rodata memory read-only and non-text memory non-executable from core module code into kernel/module/strict_rwx.c. Signed-off-by: Aaron Tomlin --- include/linux/module.h | 18 ++++++++ kernel/module/Makefile | 1 + kernel/module/main.c | 84 -------------------------------------- kernel/module/strict_rwx.c | 83 +++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 84 deletions(-) create mode 100644 kernel/module/strict_rwx.c diff --git a/include/linux/module.h b/include/linux/module.h index caa7212cf754..218ac6768433 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -354,6 +354,24 @@ extern void module_enable_x(const struct module *mod); static void module_enable_x(const struct module *mod) { } #endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ +#ifdef CONFIG_STRICT_MODULE_RWX +extern void frob_rodata(const struct module_layout *layout, int (*set_memory)(unsigned long start, int num_pages)); +extern void frob_ro_after_init(const struct module_layout *layout, int (*set_memory)(unsigned long start, int num_pages)); +extern void frob_writable_data(const struct module_layout *layout, int (*set_memory)(unsigned long start, int num_pages)); +extern void module_enable_ro(const struct module *mod, bool after_init); +extern void module_enable_nx(const struct module *mod); +extern int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, char *secstrings, struct module *mod); + +#else /* !CONFIG_STRICT_MODULE_RWX */ +static void module_enable_nx(const struct module *mod) { } +static void module_enable_ro(const struct module *mod, bool after_init) {} +static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, + char *secstrings, struct module *mod) +{ + return 0; +} +#endif /* CONFIG_STRICT_MODULE_RWX */ + #ifdef CONFIG_MODULES_TREE_LOOKUP struct mod_tree_root { struct latch_tree_root root; diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 95fad95a0549..795fe10ac530 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -9,3 +9,4 @@ obj-$(CONFIG_MODULE_SIG_FORMAT) += signature.o obj-$(CONFIG_LIVEPATCH) += livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) += tree_lookup.o obj-$(CONFIG_ARCH_HAS_STRICT_MODULE_RWX) += arch_strict_rwx.o +obj-$(CONFIG_STRICT_MODULE_RWX) += strict_rwx.o diff --git a/kernel/module/main.c b/kernel/module/main.c index a0619256b343..c404d00f7958 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1774,90 +1774,6 @@ static void mod_sysfs_teardown(struct module *mod) mod_sysfs_fini(mod); } -#ifdef CONFIG_STRICT_MODULE_RWX -static void frob_rodata(const struct module_layout *layout, - int (*set_memory)(unsigned long start, int num_pages)) -{ - BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); - BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1)); - BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1)); - set_memory((unsigned long)layout->base + layout->text_size, - (layout->ro_size - layout->text_size) >> PAGE_SHIFT); -} - -static void frob_ro_after_init(const struct module_layout *layout, - int (*set_memory)(unsigned long start, int num_pages)) -{ - BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); - BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1)); - BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1)); - set_memory((unsigned long)layout->base + layout->ro_size, - (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT); -} - -static void frob_writable_data(const struct module_layout *layout, - int (*set_memory)(unsigned long start, int num_pages)) -{ - BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); - BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1)); - BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1)); - set_memory((unsigned long)layout->base + layout->ro_after_init_size, - (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT); -} - -static void module_enable_ro(const struct module *mod, bool after_init) -{ - if (!rodata_enabled) - return; - - set_vm_flush_reset_perms(mod->core_layout.base); - set_vm_flush_reset_perms(mod->init_layout.base); - frob_text(&mod->core_layout, set_memory_ro); - - frob_rodata(&mod->core_layout, set_memory_ro); - frob_text(&mod->init_layout, set_memory_ro); - frob_rodata(&mod->init_layout, set_memory_ro); - - if (after_init) - frob_ro_after_init(&mod->core_layout, set_memory_ro); -} - -static void module_enable_nx(const struct module *mod) -{ - frob_rodata(&mod->core_layout, set_memory_nx); - frob_ro_after_init(&mod->core_layout, set_memory_nx); - frob_writable_data(&mod->core_layout, set_memory_nx); - frob_rodata(&mod->init_layout, set_memory_nx); - frob_writable_data(&mod->init_layout, set_memory_nx); -} - -static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, - char *secstrings, struct module *mod) -{ - const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR; - int i; - - for (i = 0; i < hdr->e_shnum; i++) { - if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) { - pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n", - mod->name, secstrings + sechdrs[i].sh_name, i); - return -ENOEXEC; - } - } - - return 0; -} - -#else /* !CONFIG_STRICT_MODULE_RWX */ -static void module_enable_nx(const struct module *mod) { } -static void module_enable_ro(const struct module *mod, bool after_init) {} -static int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, - char *secstrings, struct module *mod) -{ - return 0; -} -#endif /* CONFIG_STRICT_MODULE_RWX */ - void __weak module_memfree(void *module_region) { /* diff --git a/kernel/module/strict_rwx.c b/kernel/module/strict_rwx.c new file mode 100644 index 000000000000..8a513ced02c6 --- /dev/null +++ b/kernel/module/strict_rwx.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * kernel/module/strict_rwx.c - module strict rwx + * + * Copyright (C) 2015 Rusty Russell + */ + +#include +#include +#include + +void frob_rodata(const struct module_layout *layout, + int (*set_memory)(unsigned long start, int num_pages)) +{ + BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); + BUG_ON((unsigned long)layout->text_size & (PAGE_SIZE-1)); + BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1)); + set_memory((unsigned long)layout->base + layout->text_size, + (layout->ro_size - layout->text_size) >> PAGE_SHIFT); +} + +void frob_ro_after_init(const struct module_layout *layout, + int (*set_memory)(unsigned long start, int num_pages)) +{ + BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); + BUG_ON((unsigned long)layout->ro_size & (PAGE_SIZE-1)); + BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1)); + set_memory((unsigned long)layout->base + layout->ro_size, + (layout->ro_after_init_size - layout->ro_size) >> PAGE_SHIFT); +} + +void frob_writable_data(const struct module_layout *layout, + int (*set_memory)(unsigned long start, int num_pages)) +{ + BUG_ON((unsigned long)layout->base & (PAGE_SIZE-1)); + BUG_ON((unsigned long)layout->ro_after_init_size & (PAGE_SIZE-1)); + BUG_ON((unsigned long)layout->size & (PAGE_SIZE-1)); + set_memory((unsigned long)layout->base + layout->ro_after_init_size, + (layout->size - layout->ro_after_init_size) >> PAGE_SHIFT); +} + +void module_enable_ro(const struct module *mod, bool after_init) +{ + if (!rodata_enabled) + return; + + set_vm_flush_reset_perms(mod->core_layout.base); + set_vm_flush_reset_perms(mod->init_layout.base); + frob_text(&mod->core_layout, set_memory_ro); + + frob_rodata(&mod->core_layout, set_memory_ro); + frob_text(&mod->init_layout, set_memory_ro); + frob_rodata(&mod->init_layout, set_memory_ro); + + if (after_init) + frob_ro_after_init(&mod->core_layout, set_memory_ro); +} + +void module_enable_nx(const struct module *mod) +{ + frob_rodata(&mod->core_layout, set_memory_nx); + frob_ro_after_init(&mod->core_layout, set_memory_nx); + frob_writable_data(&mod->core_layout, set_memory_nx); + frob_rodata(&mod->init_layout, set_memory_nx); + frob_writable_data(&mod->init_layout, set_memory_nx); +} + +int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs, + char *secstrings, struct module *mod) +{ + const unsigned long shf_wx = SHF_WRITE|SHF_EXECINSTR; + int i; + + for (i = 0; i < hdr->e_shnum; i++) { + if ((sechdrs[i].sh_flags & shf_wx) == shf_wx) { + pr_err("%s: section %s (index %d) has invalid WRITE|EXEC flags\n", + mod->name, secstrings + sechdrs[i].sh_name, i); + return -ENOEXEC; + } + } + + return 0; +}