Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp623280pxb; Tue, 1 Feb 2022 07:11:32 -0800 (PST) X-Google-Smtp-Source: ABdhPJx0f2zmOqni1TcaiPMTlUPZNF3U5cjP9e9wU6kjisI5ncmBf4XViczaRCw9LuUtRfFOCj1N X-Received: by 2002:a05:6402:27c8:: with SMTP id c8mr25213284ede.87.1643728292462; Tue, 01 Feb 2022 07:11:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643728292; cv=none; d=google.com; s=arc-20160816; b=taC+nbFNscl85x1ZjeGUSkBSBhPAQpkk60ZmeOAl5Tj7sjpDUMDVr/kyIAazNoj68H n9pVHFlE1q0E1V15Fn8txsM4e1my7XqtmDDvI1q+gXxwIXZk1m20nd92lRJPxHCQ8Gl3 AJyYJKfUp+zQY3YvYreQKThTwKs8xxFGT8HxxzXTX8ioUObIk8fVfqonogR+F4TAxrvX d7OjAjiCbnvTOYepEItifTwaUoramRP7x4q1YZiZtfvxVQFddDhUQFcaV7i0cW8Kg1zy kv2P5FuaLXpTs7cPfWuSGEUM/2NMvrMveR5dUm5Eb44MmrXoBG14fFIKo7WIIpliF7aW BjdA== 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=+8jA7bYfQPshsiLx0pkdrriw91RK3YxQWB2UbWGtHA0=; b=xlwMwry4E/ipfe8iucaU+6rPJ8+lrIwOifcrvI2PgpSxwJHOBH+RDu5P5BmYV/XyyJ PMrMDiaRonCUzfUV7F688YDvL+noyFKHSyFLpa5Uq4lGMdLQyEImnwDf250jofCRbs7R tFDAobUwGZBfDDtK4G1aRBGm9fhsPfT9uy4sa+efFItYTtZ6mMzOSYtYfPCUFcuBLWJ5 kDS+/7snFj5lRfwmtvhZY/kq1ZzT+NOJaO9iUdZ83pU0Oq2Hu0Y0x5MIuLUaZL/DnSff dDZEjM1DXNaP+xLIonyY3L5e/Y8mB0du7Q/sW+qj0PaLEZqbD+X3c/Le2Sw/XU0Z/Ojy 2S/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=V5csWtGy; 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 ht11si5678477ejc.275.2022.02.01.07.11.05; Tue, 01 Feb 2022 07:11:32 -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=V5csWtGy; 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 S1356779AbiA3Vd3 (ORCPT + 99 others); Sun, 30 Jan 2022 16:33:29 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:30222 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356566AbiA3Vc2 (ORCPT ); Sun, 30 Jan 2022 16:32:28 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578347; 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=+8jA7bYfQPshsiLx0pkdrriw91RK3YxQWB2UbWGtHA0=; b=V5csWtGy+CBHW8A+iwxhbdEsUxhJOrtFpc1/ABlAYiAcqRpNpBdmpzfYItv13qg5ZTns6J 3TgYdoLTMn+b6uwZwzDrRTsJgwsfsoHh2KdkoCSkDdWwrY/xinawRH6VpF8qlakf4gqMpB yiyVpj7rhxH9IFHGfiQ6NQKvQchXF70= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-399-avyhR6hDM7qUJx5zkeYeXw-1; Sun, 30 Jan 2022 16:32:26 -0500 X-MC-Unique: avyhR6hDM7qUJx5zkeYeXw-1 Received: by mail-wm1-f71.google.com with SMTP id d184-20020a1c1dc1000000b00350769d4bcfso2535354wmd.4 for ; Sun, 30 Jan 2022 13:32:26 -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=+8jA7bYfQPshsiLx0pkdrriw91RK3YxQWB2UbWGtHA0=; b=ZzGEe6m1GXUhHaCq+/V2D74/epDWgfxe7IfXjqp/nqZLCbDYcrJsliqgttqgOs1tBG tg2Am1mLNgJeuwuAFfYyk2qKx6/w9phORzWtzdmYOFQ6cYCDyZ/wqNm9guMznrAw91/I YxLiSYI7z7NTHbIppEs0n2eB9CEORf1bPmoPvMogOVCKd9Lu5SQwFznalArNcLeKk1kS P5RkuZkSkdjpybiPpzaOdAjlSJkaGyz+wDuXTI9vconXnxW3d6/4Yme47eyNQc2eq4zY +QfuJzDugcoJ4BOims1N/jaXn2XEfL4q2XT2nSyVYvmw8yMwv59MMIVD5jCNVLoPpMyj IVYg== X-Gm-Message-State: AOAM530XZW836+WY76uN9vUHgQVcbDiSym1JtDXNuNVepXaXZ+KLvtP8 o7W3N4tirCK4n9vrFPDJmr4Rg5ZaKdIVgV6jzQjAp7LUAFiCaAtgetWoCTY07VUZMK+ZYMaQ/aU +2gpXS7MqdJ6gPE8CNwsbJT0= X-Received: by 2002:a5d:550f:: with SMTP id b15mr5171590wrv.384.1643578344986; Sun, 30 Jan 2022 13:32:24 -0800 (PST) X-Received: by 2002:a5d:550f:: with SMTP id b15mr5171573wrv.384.1643578344807; Sun, 30 Jan 2022 13:32:24 -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 n13sm10203712wrm.68.2022.01.30.13.32.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:24 -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, void@manifault.com, joe@perches.com Subject: [RFC PATCH v4 06/13] module: Move strict rwx support to a separate file Date: Sun, 30 Jan 2022 21:32:07 +0000 Message-Id: <20220130213214.1042497-7-atomlin@redhat.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220130213214.1042497-1-atomlin@redhat.com> References: <20220130213214.1042497-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 8b75380bc340..520c0f4bb968 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 /* Only touch one cacheline for common rbtree-for-core-layout case. */ #define __module_layout_align ____cacheline_aligned diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 146509978708..d1dada641ad6 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -11,4 +11,5 @@ ifdef CONFIG_MODULES 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 endif diff --git a/kernel/module/main.c b/kernel/module/main.c index 7e98ea12b146..1a0e659a27bc 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1782,90 +1782,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