Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp2751781pxb; Mon, 31 Jan 2022 03:36:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJyum/+JPakvvrAzY5l26uoQpmerOMIMYsCCS5DrylzDKIiZ0JADpjrQVT/ivIPK4I92wQLg X-Received: by 2002:a17:902:d34c:: with SMTP id l12mr20109156plk.106.1643628989096; Mon, 31 Jan 2022 03:36:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643628989; cv=none; d=google.com; s=arc-20160816; b=z1XVD/sp7GTy6raQkcna6uCCgwQZQgac+Ctf1CoPG7KxVPIvgtyvzZ/bZOsQJTxSi5 UWKunlhjMW1x7+dDGWbs8LQescjUeM/t9vuNIQ7OKfnMJKiIfDGnXGwM9WErof3PMqTW fAUquo3t0bss8W2LLVFh8LmBWunM9P6NLF1fC/tohla5zNEFhBMuZaCTQE6sALbzTjP3 DYF0VqaXn+Ly2AVDCICCF2qoWfiFC4RynFnzMOGPEyOR+yXspoEHFgmSQJy0QV01qx8D p9OkY5Va9kdG+K6jtsVE182G8NHFsxkw0TiHS2tnhckuKDY75QD5NY+INtJ895bPcDuh dm+Q== 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 :dkim-signature; bh=Xbb1waqP0kwmR38UUX09e82b0OEVG9z0fGYLb99+jx8=; b=RpR8RXEgXMpOI6cSUrfaepv3DuegE2nti50fMmTlvyCUZUkHomI3D8JBzNAc8qZD3X ShvMnsapuCEcTzXMapVqtuZq80oNaCoYnN49fIx5oOPak2HJFfgWs8bXvWSxbDU07aBI FdYN/3xarFQ06ozll83PmxDFybeXAe9TVmo+j8OWMbvXVgsYmMrpmS7bYwz1vaN2kVbD jHqkjuT95A1v62JTiSyXpSQPYy1e6AhgEdHYz5ZeTytRcccn5XBTY5p7LhNDwz4j+Sw3 ZFuNt8TSLRhryJ54dTAdtH5sWOGqs5/y0VrhKi4pydSznnUJbxC1N/Ldxw6SLOt0M8tQ UHAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ti+BnmvY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h191si6310218pge.299.2022.01.31.03.36.18; Mon, 31 Jan 2022 03:36:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=Ti+BnmvY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1351410AbiA1UkG (ORCPT + 99 others); Fri, 28 Jan 2022 15:40:06 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:33124 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343615AbiA1Ujq (ORCPT ); Fri, 28 Jan 2022 15:39:46 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643402386; 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=Xbb1waqP0kwmR38UUX09e82b0OEVG9z0fGYLb99+jx8=; b=Ti+BnmvYkizeh9Rc5WgOwBOhNsdVBSTALXYdn4wt//RpMbZWNZgq+hzsFcLOCQD+M3RVi2 EhzKnL4ZYd0ccD9FmoblLjxHqZxrzAHIHh5tHfb9IeSVzZKQIIlvDtxwUWnZqPeLu+LBjd MTuicCT0btMxYWpidxbeBdyPWV3/Bb8= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-440-iFc0p9SMN0SFf7gVwe1z5g-1; Fri, 28 Jan 2022 15:39:45 -0500 X-MC-Unique: iFc0p9SMN0SFf7gVwe1z5g-1 Received: by mail-wm1-f72.google.com with SMTP id v185-20020a1cacc2000000b0034906580813so6444352wme.1 for ; Fri, 28 Jan 2022 12:39:45 -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=Xbb1waqP0kwmR38UUX09e82b0OEVG9z0fGYLb99+jx8=; b=Hh6V2jrzLVWRxsgj4bkkATCE6vJagoHYONyHW7wI2r/yBXYafv8asr361XCl+CKyJx Xsjko5FT1n+bkkex9X4bCHtQIu4wbkcuOf5jRQfYFnnAzS3DEAexWHsro0sOaq3XTZsV cbQNorcTr7NVhVlBz1zlxvfj6mGWZCeMYs/poV3qujj7gRCl5Z+rV/IL+SsYiAsV0pI7 cOJbw2/oz8AGv0WvIWf+XnpmNabFtcsemJdh5mrk4ADmo0XFLIkHnE62hrl/D26Ykoj8 m9zwz9DxGhD9tnVimBelGg/2wg1uoWrenh0Zo45trl6HCdACCg/TFS92qjxFAJvbTMRI abxw== X-Gm-Message-State: AOAM530O/B7dtJhKTHERIf7apX3HC4M27yKRWzpXnLP7vYBbUzdJ9wkm sPE/bdFGxsDYQTeCl8wI3xsK0LHN5egSmrircOACjpbEqLQ8yhR5/2IS0hFmR+1FH0IF6iWlp/1 19kvKBGx6F6AGJFX6ZVxnlEM= X-Received: by 2002:a05:600c:410f:: with SMTP id j15mr16876879wmi.161.1643402383745; Fri, 28 Jan 2022 12:39:43 -0800 (PST) X-Received: by 2002:a05:600c:410f:: with SMTP id j15mr16876869wmi.161.1643402383571; Fri, 28 Jan 2022 12:39:43 -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 f16sm1472309wmg.28.2022.01.28.12.39.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 12:39:43 -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, live-patching@vger.kernel.org, atomlin@atomlin.com, ghalat@redhat.com, allen.lkml@gmail.com Subject: [RFC PATCH v3 06/13] module: Move strict rwx support to a separate file Date: Fri, 28 Jan 2022 20:39:27 +0000 Message-Id: <20220128203934.600247-7-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220128203934.600247-1-atomlin@redhat.com> References: <20220128203934.600247-1-atomlin@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 | 22 ++++++++++ kernel/module/Makefile | 1 + kernel/module/main.c | 84 -------------------------------------- kernel/module/strict_rwx.c | 83 +++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+), 84 deletions(-) create mode 100644 kernel/module/strict_rwx.c diff --git a/include/linux/module.h b/include/linux/module.h index 35552164830d..7bf93c0d87e6 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -356,6 +356,28 @@ 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 71341f30c3f2..c4198fcf4772 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -10,3 +10,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 fbac043b07d7..4f590b0c7aca 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1773,90 +1773,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..39060021755a --- /dev/null +++ b/kernel/module/strict_rwx.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * 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; +} -- 2.34.1