From nobody Tue Jun 30 02:38:54 2026 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 98C98C433F5 for ; Sun, 30 Jan 2022 21:32:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356712AbiA3Vck (ORCPT ); Sun, 30 Jan 2022 16:32:40 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:58208 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356469AbiA3VcV (ORCPT ); Sun, 30 Jan 2022 16:32:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578340; 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=JV4WzeMBxzAOtiGbJsE6/WwOSD8dA4tNDcXqNuD6cVU=; b=cFc87/dDyMg2vgK/RTbWaZE1IsX9RO/dXn9EuOCwA52sK445kZNSUdtpaCmun0/tehZCKz bkfwl10TpLxbPuhF4657nmgTcJG4cnBQYtZ2oyogXjNMQM4Q6dbYbRLLNkVOCPrKS2GNCn Chhef3Ls2rCjbJ5Wk0QrJYLP97D1BJY= 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-262-eVWKuyh1NiqxGRtdYEQGEg-1; Sun, 30 Jan 2022 16:32:19 -0500 X-MC-Unique: eVWKuyh1NiqxGRtdYEQGEg-1 Received: by mail-wm1-f71.google.com with SMTP id c16-20020a1c9a10000000b0034dd409329eso5148199wme.3 for ; Sun, 30 Jan 2022 13:32:19 -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=JV4WzeMBxzAOtiGbJsE6/WwOSD8dA4tNDcXqNuD6cVU=; b=nDLXfOW/7Z4Kd5xGe/40HKdw4c+J348ALIsNfcOEO6QtBJ4qTTDL2Bn4mUyRER0sKX SKHf4FnITi0HmbvNrbS2PbGIvvj+T4wsbZcdZY/zYIu5frJF7FUyDbqa1USaqkaaU+u6 JXmPLWlJyQVzxBXnBd467AlvLGcFuI2uT8Ff0eKCFSuQOwY1G0+K1qxjxa71kGAl0jn6 yX2kK/vIeY40OIvBAh5xrbxwAwbQ3P4yS/C8Zi8kUMst/Rp7eRJIZvpMTICP7f1P+tjO rRMXtZHTtR0Ofjg5Sak51cH6hA8dcecvklrodhXHcQPRCzLdIOVjeZDS+fg4gXm/8Dk6 uemw== X-Gm-Message-State: AOAM532x7LGtoyNEKb5UW9jwxXnTojCmZTg4QDpRLunJq/t4Lb0n9uZI vBGxSRB4RNGvF8DfwINDT4LK/VNtOEf1d2WMkh6bdmesGqV4AsuhD6ww4PF0GzHWSjATHu9q3GF SRVJjHWQ+fj/5Cdf83KzAvx8= X-Received: by 2002:a05:600c:48a:: with SMTP id d10mr16034124wme.100.1643578338239; Sun, 30 Jan 2022 13:32:18 -0800 (PST) X-Google-Smtp-Source: ABdhPJz7jvFvppo9puF0OsfaRfA3N+nCHc74wub5pjdUY1rNAuJO9Wd7ayQBEQZUn7+/vp/YyotidQ== X-Received: by 2002:a05:600c:48a:: with SMTP id d10mr16034110wme.100.1643578338088; Sun, 30 Jan 2022 13:32:18 -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 v8sm7063946wmd.44.2022.01.30.13.32.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:17 -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 01/13] module: Move all into module/ Date: Sun, 30 Jan 2022 21:32:02 +0000 Message-Id: <20220130213214.1042497-2-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional changes. This patch moves all module related code into a separate directory, modifies each file name and creates a new Makefile. Note: this effort is in preparation to refactor core module code. Signed-off-by: Aaron Tomlin --- MAINTAINERS | 2 +- kernel/Makefile | 5 +---- kernel/module/Makefile | 9 +++++++++ kernel/{module_decompress.c =3D> module/decompress.c} | 2 +- kernel/{module-internal.h =3D> module/internal.h} | 0 kernel/{module.c =3D> module/main.c} | 2 +- kernel/{module_signature.c =3D> module/signature.c} | 0 kernel/{module_signing.c =3D> module/signing.c} | 2 +- 8 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 kernel/module/Makefile rename kernel/{module_decompress.c =3D> module/decompress.c} (99%) rename kernel/{module-internal.h =3D> module/internal.h} (100%) rename kernel/{module.c =3D> module/main.c} (99%) rename kernel/{module_signature.c =3D> module/signature.c} (100%) rename kernel/{module_signing.c =3D> module/signing.c} (97%) diff --git a/MAINTAINERS b/MAINTAINERS index acd112c42255..a98f4700acb7 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -12976,7 +12976,7 @@ L: linux-kernel@vger.kernel.org S: Maintained T: git git://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git modu= les-next F: include/linux/module.h -F: kernel/module.c +F: kernel/module/main.c =20 MONOLITHIC POWER SYSTEM PMIC DRIVER M: Saravanan Sekar diff --git a/kernel/Makefile b/kernel/Makefile index 56f4ee97f328..3a6380975c57 100644 --- a/kernel/Makefile +++ b/kernel/Makefile @@ -53,6 +53,7 @@ obj-y +=3D rcu/ obj-y +=3D livepatch/ obj-y +=3D dma/ obj-y +=3D entry/ +obj-y +=3D module/ =20 obj-$(CONFIG_KCMP) +=3D kcmp.o obj-$(CONFIG_FREEZER) +=3D freezer.o @@ -66,10 +67,6 @@ ifneq ($(CONFIG_SMP),y) obj-y +=3D up.o endif obj-$(CONFIG_UID16) +=3D uid16.o -obj-$(CONFIG_MODULES) +=3D module.o -obj-$(CONFIG_MODULE_DECOMPRESS) +=3D module_decompress.o -obj-$(CONFIG_MODULE_SIG) +=3D module_signing.o -obj-$(CONFIG_MODULE_SIG_FORMAT) +=3D module_signature.o obj-$(CONFIG_KALLSYMS) +=3D kallsyms.o obj-$(CONFIG_BSD_PROCESS_ACCT) +=3D acct.o obj-$(CONFIG_CRASH_CORE) +=3D crash_core.o diff --git a/kernel/module/Makefile b/kernel/module/Makefile new file mode 100644 index 000000000000..2902fc7d0ef1 --- /dev/null +++ b/kernel/module/Makefile @@ -0,0 +1,9 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Makefile for linux kernel module support +# + +obj-$(CONFIG_MODULES) +=3D main.o +obj-$(CONFIG_MODULE_DECOMPRESS) +=3D decompress.o +obj-$(CONFIG_MODULE_SIG) +=3D signing.o +obj-$(CONFIG_MODULE_SIG_FORMAT) +=3D signature.o diff --git a/kernel/module_decompress.c b/kernel/module/decompress.c similarity index 99% rename from kernel/module_decompress.c rename to kernel/module/decompress.c index b01c69c2ff99..c153fd8a4444 100644 --- a/kernel/module_decompress.c +++ b/kernel/module/decompress.c @@ -12,7 +12,7 @@ #include #include =20 -#include "module-internal.h" +#include "internal.h" =20 static int module_extend_max_pages(struct load_info *info, unsigned int ex= tent) { diff --git a/kernel/module-internal.h b/kernel/module/internal.h similarity index 100% rename from kernel/module-internal.h rename to kernel/module/internal.h diff --git a/kernel/module.c b/kernel/module/main.c similarity index 99% rename from kernel/module.c rename to kernel/module/main.c index 387ee77bdbd6..d8c6269af6cf 100644 --- a/kernel/module.c +++ b/kernel/module/main.c @@ -58,7 +58,7 @@ #include #include #include -#include "module-internal.h" +#include "internal.h" =20 #define CREATE_TRACE_POINTS #include diff --git a/kernel/module_signature.c b/kernel/module/signature.c similarity index 100% rename from kernel/module_signature.c rename to kernel/module/signature.c diff --git a/kernel/module_signing.c b/kernel/module/signing.c similarity index 97% rename from kernel/module_signing.c rename to kernel/module/signing.c index 8723ae70ea1f..8aeb6d2ee94b 100644 --- a/kernel/module_signing.c +++ b/kernel/module/signing.c @@ -12,7 +12,7 @@ #include #include #include -#include "module-internal.h" +#include "internal.h" =20 /* * Verify the signature on a module. --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 2CC1CC433FE for ; Sun, 30 Jan 2022 21:33:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356747AbiA3Vcn (ORCPT ); Sun, 30 Jan 2022 16:32:43 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:33529 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356487AbiA3VcX (ORCPT ); Sun, 30 Jan 2022 16:32:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578342; 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=CKFHu1mwRyMCd3tE6xfYJyy3RgbH1D0nAbWrHT5RWOU=; b=DCR9t5yrgdZX6WNYuCqMcBXxRvad9/or/d8ffkAF6kvly83Sr0fJEdiPq6B8xM9vonhxc5 JbQ6D53K2+b3s3l8miFl3+Kc+FbToDwVCtq6BL1BofFpl0uBFBrjjkDDnRpzeZSh9+Fisn CwGam0xeAB78xp+RURLUrfV1hpJDH9g= 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-451-4WIY67vkMqqu0IXJPNUKSQ-1; Sun, 30 Jan 2022 16:32:21 -0500 X-MC-Unique: 4WIY67vkMqqu0IXJPNUKSQ-1 Received: by mail-wm1-f72.google.com with SMTP id n7-20020a1c7207000000b0034ec3d8ce0aso5135245wmc.8 for ; Sun, 30 Jan 2022 13:32:20 -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=CKFHu1mwRyMCd3tE6xfYJyy3RgbH1D0nAbWrHT5RWOU=; b=AnVkjqDu6ZgJG9+X6CRe8cGH5J1J4iCVkD1rdo3WT9sGDHi+Xo5L/h3+0t8NzBhPO2 yzxbc3UHCdHsvFFKR20ysZVXXXbkorfa4unklU4R9atAlfLZ85SpobY/JtFmYg4eqxF7 JqVHO4CMCpme6DzpdDfATi2S8GoSbVCJ5CWKompJupuh3ramQZLZPX3uNrQsYxQk8e45 ROYAbKafanQiprsdhfhemvhOWemf3rx4iPQ5L3+Z6YzXLRToh9PMm7+92wxbhnl8KT81 zd4W7LBO494zlc3IrE3fEnxNqSGUKe4sYZxk33KVk+JUphLn+14ZIDfZ61eye0Vmpdpy VshQ== X-Gm-Message-State: AOAM532wctJxMyK4xw9sSIW3Lye2GLOEBS21UN6QlVxeoRpB73/szaW8 cdolaKTbDF3I64SFyO98v7jrda5kTzs6KimqO9AxiSwj421oIYb96woSKpOdSJkJJi4GO8suBgz YHPv+8jybLkFyl9HEYqIVPII= X-Received: by 2002:a05:6000:15ca:: with SMTP id y10mr15316702wry.523.1643578339933; Sun, 30 Jan 2022 13:32:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJw17nLhSQySLDHIo8E6y8aoNvy6mIUxMDuspgSa/9hzx9TeehLqEPnw5LpcjcmIpptujoZxyA== X-Received: by 2002:a05:6000:15ca:: with SMTP id y10mr15316693wry.523.1643578339742; Sun, 30 Jan 2022 13:32:19 -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 l11sm7075887wry.50.2022.01.30.13.32.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:19 -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 02/13] module: Simple refactor in preparation for split Date: Sun, 30 Jan 2022 21:32:03 +0000 Message-Id: <20220130213214.1042497-3-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch makes it possible to move non-essential code out of core module code. Signed-off-by: Aaron Tomlin --- kernel/module/internal.h | 22 ++++++++++++++++++++++ kernel/module/main.c | 23 ++--------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 8c381c99062f..e3c593f8767f 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -7,6 +7,28 @@ =20 #include #include +#include + +#ifndef ARCH_SHF_SMALL +#define ARCH_SHF_SMALL 0 +#endif + +/* If this is set, the section belongs in the init part of the module */ +#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) +/* Maximum number of characters written by module_flags() */ +#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) +#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4)) + +extern struct mutex module_mutex; +extern struct list_head modules; + +/* Provided by the linker */ +extern const struct kernel_symbol __start___ksymtab[]; +extern const struct kernel_symbol __stop___ksymtab[]; +extern const struct kernel_symbol __start___ksymtab_gpl[]; +extern const struct kernel_symbol __stop___ksymtab_gpl[]; +extern const s32 __start___kcrctab[]; +extern const s32 __start___kcrctab_gpl[]; =20 struct load_info { const char *name; diff --git a/kernel/module/main.c b/kernel/module/main.c index d8c6269af6cf..fee64c4957f3 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -63,10 +63,6 @@ #define CREATE_TRACE_POINTS #include =20 -#ifndef ARCH_SHF_SMALL -#define ARCH_SHF_SMALL 0 -#endif - /* * Modules' sections will be aligned on page boundaries * to ensure complete separation of code and data, but @@ -78,9 +74,6 @@ # define debug_align(X) (X) #endif =20 -/* If this is set, the section belongs in the init part of the module */ -#define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1)) - /* * Mutex protects: * 1) List of modules (also safely readable with preempt_disable), @@ -88,8 +81,8 @@ * 3) module_addr_min/module_addr_max. * (delete and add uses RCU list operations). */ -static DEFINE_MUTEX(module_mutex); -static LIST_HEAD(modules); +DEFINE_MUTEX(module_mutex); +LIST_HEAD(modules); =20 /* Work queue for freeing init sections in success case */ static void do_free_init(struct work_struct *w); @@ -408,14 +401,6 @@ static __maybe_unused void *any_section_objs(const str= uct load_info *info, return (void *)info->sechdrs[sec].sh_addr; } =20 -/* Provided by the linker */ -extern const struct kernel_symbol __start___ksymtab[]; -extern const struct kernel_symbol __stop___ksymtab[]; -extern const struct kernel_symbol __start___ksymtab_gpl[]; -extern const struct kernel_symbol __stop___ksymtab_gpl[]; -extern const s32 __start___kcrctab[]; -extern const s32 __start___kcrctab_gpl[]; - #ifndef CONFIG_MODVERSIONS #define symversion(base, idx) NULL #else @@ -1490,7 +1475,6 @@ struct module_sect_attrs { struct module_sect_attr attrs[]; }; =20 -#define MODULE_SECT_READ_SIZE (3 /* "0x", "\n" */ + (BITS_PER_LONG / 4)) static ssize_t module_sect_read(struct file *file, struct kobject *kobj, struct bin_attribute *battr, char *buf, loff_t pos, size_t count) @@ -4555,9 +4539,6 @@ static void cfi_cleanup(struct module *mod) #endif } =20 -/* Maximum number of characters written by module_flags() */ -#define MODULE_FLAGS_BUF_SIZE (TAINT_FLAGS_COUNT + 4) - /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ static char *module_flags(struct module *mod, char *buf) { --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 14862C433F5 for ; Sun, 30 Jan 2022 21:33:15 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356700AbiA3VdL (ORCPT ); Sun, 30 Jan 2022 16:33:11 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:38475 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356489AbiA3VcY (ORCPT ); Sun, 30 Jan 2022 16:32:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578344; 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=QXh589vFvMrnMIUeph5M5xWx3C0pdmawJfK9iOmEPzA=; b=QYgCThfQVOHvPvT0DaOpBH9f/VURFXFHhoRsjb0TP78PA9Wk/k/hN5rHPWL/IGehhpXSrN 0WxYSWgQfUKJPJgmVGHSsDi2RVKNAC/oz+8nxOqws5BwHW89EDBMbFJRJSCxYMIOMbl3Nw aXoJuhIQ6MhavzHkQdk5xDLB1ghqVmI= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-329-KhPWT4GoNHqJXLqSKCsc5g-1; Sun, 30 Jan 2022 16:32:22 -0500 X-MC-Unique: KhPWT4GoNHqJXLqSKCsc5g-1 Received: by mail-wm1-f70.google.com with SMTP id o194-20020a1ca5cb000000b00350b177fb22so8581333wme.3 for ; Sun, 30 Jan 2022 13:32:22 -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=QXh589vFvMrnMIUeph5M5xWx3C0pdmawJfK9iOmEPzA=; b=1Jt3DoLypuUlQYdHbx34Am4Y7bQjAeXg/RT/JM1I3azOoJWGjdfmo3/SbhHzyxk0uM ZuRp6CYqetAlxN5xYbBUt9MFlNcuB273MNqIY2mS+4QDfub7d0er4h/izcEGF2ZdHdF8 eZ6RVK6GJl19D086TZ+t5BPPm32H0FYwCJNVhMT2yC1BMYZc/GOtYEl0TcpaXuxBRnJf 67daO0Jkl4N5unHS7I8lj1N6Pj1dT1GKptVhpYHutYYvulesDF+b48z0yDgsBVGquqnD /Jp5G75qOyM52gu0/bZzXn73rcBPC0pJTMVDfugoJgm8mnUO/J8dMS4VJqOxo7dJYyDc xDEA== X-Gm-Message-State: AOAM530T+aZAhV4vMLLH3TvQ0sx/CllgB2svw550VMs5GVtWeOH3buAZ cehJqmBkk2LR0rNMcUb7qP0da9kcQuqHNn/T6467l6lCC58Wk8y8zz8tqo19S3actVSxg/cSTbp 6sTiBsI1WP3gkKgM2drAuCJo= X-Received: by 2002:a05:600c:510e:: with SMTP id o14mr16205190wms.163.1643578341080; Sun, 30 Jan 2022 13:32:21 -0800 (PST) X-Google-Smtp-Source: ABdhPJzBcnZ7YuyjzrWriFHLlfRfjVX9SJyc6dZ5zw+jYQNPk97ekvd+lPSXxfjFIZ+Ucy2AitFlLQ== X-Received: by 2002:a05:600c:510e:: with SMTP id o14mr16205184wms.163.1643578340898; Sun, 30 Jan 2022 13:32:20 -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 q13sm10301815wrd.78.2022.01.30.13.32.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:20 -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 03/13] module: Move livepatch support to a separate file Date: Sun, 30 Jan 2022 21:32:04 +0000 Message-Id: <20220130213214.1042497-4-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates livepatch support (i.e. used during module add/or load and remove/or deletion) from core module code into kernel/module/livepatch.c. At the moment it contains code to persist Elf information about a given livepatch module, only. Signed-off-by: Aaron Tomlin --- include/linux/module.h | 16 ++++-- kernel/module/Makefile | 3 ++ kernel/module/internal.h | 11 ++++ kernel/module/livepatch.c | 74 +++++++++++++++++++++++++++ kernel/module/main.c | 102 ++++---------------------------------- 5 files changed, 109 insertions(+), 97 deletions(-) create mode 100644 kernel/module/livepatch.c diff --git a/include/linux/module.h b/include/linux/module.h index f4338235ed2c..c7daac4d16ee 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -663,17 +663,23 @@ static inline bool module_requested_async_probing(str= uct module *module) return module && module->async_probe_requested; } =20 -#ifdef CONFIG_LIVEPATCH static inline bool is_livepatch_module(struct module *mod) { - return mod->klp; + if (IS_ENABLED(CONFIG_LIVEPATCH)) { + if (mod->klp) + return true; + } + return false; } -#else /* !CONFIG_LIVEPATCH */ -static inline bool is_livepatch_module(struct module *mod) + +static inline bool set_livepatch_module(struct module *mod) { + if (IS_ENABLED(CONFIG_LIVEPATCH)) { + mod->klp =3D true; + return true; + } return false; } -#endif /* CONFIG_LIVEPATCH */ =20 bool is_module_sig_enforced(void); void set_module_sig_enforced(void); diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 2902fc7d0ef1..ee20d864ad19 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -7,3 +7,6 @@ obj-$(CONFIG_MODULES) +=3D main.o obj-$(CONFIG_MODULE_DECOMPRESS) +=3D decompress.o obj-$(CONFIG_MODULE_SIG) +=3D signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) +=3D signature.o +ifdef CONFIG_MODULES +obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o +endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index e3c593f8767f..6150ec5ae042 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -57,6 +57,17 @@ struct load_info { =20 extern int mod_verify_sig(const void *mod, struct load_info *info); =20 +#ifdef CONFIG_LIVEPATCH +extern int copy_module_elf(struct module *mod, struct load_info *info); +extern void free_module_elf(struct module *mod); +#else /* !CONFIG_LIVEPATCH */ +static inline int copy_module_elf(struct module *mod, struct load_info *in= fo) +{ + return 0; +} +static inline void free_module_elf(struct module *mod) { } +#endif /* CONFIG_LIVEPATCH */ + #ifdef CONFIG_MODULE_DECOMPRESS int module_decompress(struct load_info *info, const void *buf, size_t size= ); void module_decompress_cleanup(struct load_info *info); diff --git a/kernel/module/livepatch.c b/kernel/module/livepatch.c new file mode 100644 index 000000000000..961045d32332 --- /dev/null +++ b/kernel/module/livepatch.c @@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module livepatch support + * + * Copyright (C) 2016 Jessica Yu + */ + +#include +#include +#include +#include "internal.h" + +/* + * Persist Elf information about a module. Copy the Elf header, + * section header table, section string table, and symtab section + * index from info to mod->klp_info. + */ +int copy_module_elf(struct module *mod, struct load_info *info) +{ + unsigned int size, symndx; + int ret; + + size =3D sizeof(*mod->klp_info); + mod->klp_info =3D kmalloc(size, GFP_KERNEL); + if (mod->klp_info =3D=3D NULL) + return -ENOMEM; + + /* Elf header */ + size =3D sizeof(mod->klp_info->hdr); + memcpy(&mod->klp_info->hdr, info->hdr, size); + + /* Elf section header table */ + size =3D sizeof(*info->sechdrs) * info->hdr->e_shnum; + mod->klp_info->sechdrs =3D kmemdup(info->sechdrs, size, GFP_KERNEL); + if (mod->klp_info->sechdrs =3D=3D NULL) { + ret =3D -ENOMEM; + goto free_info; + } + + /* Elf section name string table */ + size =3D info->sechdrs[info->hdr->e_shstrndx].sh_size; + mod->klp_info->secstrings =3D kmemdup(info->secstrings, size, GFP_KERNEL); + if (mod->klp_info->secstrings =3D=3D NULL) { + ret =3D -ENOMEM; + goto free_sechdrs; + } + + /* Elf symbol section index */ + symndx =3D info->index.sym; + mod->klp_info->symndx =3D symndx; + + /* + * For livepatch modules, core_kallsyms.symtab is a complete + * copy of the original symbol table. Adjust sh_addr to point + * to core_kallsyms.symtab since the copy of the symtab in module + * init memory is freed at the end of do_init_module(). + */ + mod->klp_info->sechdrs[symndx].sh_addr =3D (unsigned long) mod->core_kall= syms.symtab; + + return 0; + +free_sechdrs: + kfree(mod->klp_info->sechdrs); +free_info: + kfree(mod->klp_info); + return ret; +} + +void free_module_elf(struct module *mod) +{ + kfree(mod->klp_info->sechdrs); + kfree(mod->klp_info->secstrings); + kfree(mod->klp_info); +} diff --git a/kernel/module/main.c b/kernel/module/main.c index fee64c4957f3..c91c7e57bca7 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2042,81 +2042,6 @@ static int module_enforce_rwx_sections(Elf_Ehdr *hdr= , Elf_Shdr *sechdrs, } #endif /* CONFIG_STRICT_MODULE_RWX */ =20 -#ifdef CONFIG_LIVEPATCH -/* - * Persist Elf information about a module. Copy the Elf header, - * section header table, section string table, and symtab section - * index from info to mod->klp_info. - */ -static int copy_module_elf(struct module *mod, struct load_info *info) -{ - unsigned int size, symndx; - int ret; - - size =3D sizeof(*mod->klp_info); - mod->klp_info =3D kmalloc(size, GFP_KERNEL); - if (mod->klp_info =3D=3D NULL) - return -ENOMEM; - - /* Elf header */ - size =3D sizeof(mod->klp_info->hdr); - memcpy(&mod->klp_info->hdr, info->hdr, size); - - /* Elf section header table */ - size =3D sizeof(*info->sechdrs) * info->hdr->e_shnum; - mod->klp_info->sechdrs =3D kmemdup(info->sechdrs, size, GFP_KERNEL); - if (mod->klp_info->sechdrs =3D=3D NULL) { - ret =3D -ENOMEM; - goto free_info; - } - - /* Elf section name string table */ - size =3D info->sechdrs[info->hdr->e_shstrndx].sh_size; - mod->klp_info->secstrings =3D kmemdup(info->secstrings, size, GFP_KERNEL); - if (mod->klp_info->secstrings =3D=3D NULL) { - ret =3D -ENOMEM; - goto free_sechdrs; - } - - /* Elf symbol section index */ - symndx =3D info->index.sym; - mod->klp_info->symndx =3D symndx; - - /* - * For livepatch modules, core_kallsyms.symtab is a complete - * copy of the original symbol table. Adjust sh_addr to point - * to core_kallsyms.symtab since the copy of the symtab in module - * init memory is freed at the end of do_init_module(). - */ - mod->klp_info->sechdrs[symndx].sh_addr =3D \ - (unsigned long) mod->core_kallsyms.symtab; - - return 0; - -free_sechdrs: - kfree(mod->klp_info->sechdrs); -free_info: - kfree(mod->klp_info); - return ret; -} - -static void free_module_elf(struct module *mod) -{ - kfree(mod->klp_info->sechdrs); - kfree(mod->klp_info->secstrings); - kfree(mod->klp_info); -} -#else /* !CONFIG_LIVEPATCH */ -static int copy_module_elf(struct module *mod, struct load_info *info) -{ - return 0; -} - -static void free_module_elf(struct module *mod) -{ -} -#endif /* CONFIG_LIVEPATCH */ - void __weak module_memfree(void *module_region) { /* @@ -3091,30 +3016,23 @@ static int copy_chunked_from_user(void *dst, const = void __user *usrc, unsigned l return 0; } =20 -#ifdef CONFIG_LIVEPATCH static int check_modinfo_livepatch(struct module *mod, struct load_info *i= nfo) { - if (get_modinfo(info, "livepatch")) { - mod->klp =3D true; + if (!get_modinfo(info, "livepatch")) + /* Nothing more to do */ + return 0; + + if (set_livepatch_module(mod)) { add_taint_module(mod, TAINT_LIVEPATCH, LOCKDEP_STILL_OK); pr_notice_once("%s: tainting kernel with TAINT_LIVEPATCH\n", - mod->name); - } - - return 0; -} -#else /* !CONFIG_LIVEPATCH */ -static int check_modinfo_livepatch(struct module *mod, struct load_info *i= nfo) -{ - if (get_modinfo(info, "livepatch")) { - pr_err("%s: module is marked as livepatch module, but livepatch support = is disabled", - mod->name); - return -ENOEXEC; + mod->name); + return 0; } =20 - return 0; + pr_err("%s: module is marked as livepatch module, but livepatch support i= s disabled", + mod->name); + return -ENOEXEC; } -#endif /* CONFIG_LIVEPATCH */ =20 static void check_modinfo_retpoline(struct module *mod, struct load_info *= info) { --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 367DFC433F5 for ; Sun, 30 Jan 2022 21:33:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356864AbiA3VdX (ORCPT ); Sun, 30 Jan 2022 16:33:23 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:21866 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356528AbiA3VcZ (ORCPT ); Sun, 30 Jan 2022 16:32:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578345; 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=hyabKM8+59CUwT2B0d7qLIZay/RH+beAzDXJmHYL03U=; b=cTNUC0k2uNB2QpVB0SJ6z21f88GmRyl2RdpTDFtBfb6QNmeuzRJB2lzeni0TwJiqPeZfhZ vE+OZw0WZaCewNZ63DvwMNV/7Vk+KWAxDOR0pkIEF/BLq61mBF8Ro8V3Z/QRSPgCz6TbpF C1qBWpfbvRGfk40Y6bX6Ms54AIKoyT0= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-387-t_JzO0XUMGmUfL_CxU3Vxw-1; Sun, 30 Jan 2022 16:32:23 -0500 X-MC-Unique: t_JzO0XUMGmUfL_CxU3Vxw-1 Received: by mail-wm1-f69.google.com with SMTP id l16-20020a1c7910000000b0034e4206ecb7so5135249wme.7 for ; Sun, 30 Jan 2022 13:32:23 -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=hyabKM8+59CUwT2B0d7qLIZay/RH+beAzDXJmHYL03U=; b=Y0wLgfBNk2LDpJejyye21NY+PYRj5FHEB/Ycn1FbeGweMVTG8tgORFVHRs0taCxtJr T0yFmtSlXsCw1bQ/OiQhvBYV/9i7k90CEXFWabAsVqJTqxxch0CUHp87ezY0hHiS1RhY v5RzEIlWA/bBjrJSth97yyMfH9zTYQg7yVfAGOOvg5LIQXObxcvETz2g2Z+k8a0VE7N/ wu9KGspcDecXOLNGg+Lb3Ispn76xkESniIlQeEU6ujhrzq3/F5wu+ymrUJwCCyaE6Tqx OLphHljoIUN1tRjntYC5z2boQmKebz7t69XFTjdxvrGftzev+afHmqFkP0gqIy2XMdvP BMDw== X-Gm-Message-State: AOAM532zAS+di86WPndMmtB9shztihrYrtzy8HksD2FBR4AFa9MltVYm 2kv7531MPNBWkxfcwsr3cQYTqC7UcX3kdYuGhOZttUXa0y+AN0eevHHo2xNCLtRE16OLYKacygj UPHdWoAIzsZV1hQ30RLRz0eU= X-Received: by 2002:a5d:6a8d:: with SMTP id s13mr14618944wru.280.1643578342408; Sun, 30 Jan 2022 13:32:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJy9iOV5OMv/gnyHQLbIxe8/bgndYMZGYUBXI+D8eIusi6O3MhgTP7iKtz7IbLdk3wGD6R+fug== X-Received: by 2002:a5d:6a8d:: with SMTP id s13mr14618933wru.280.1643578342171; Sun, 30 Jan 2022 13:32:22 -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 v5sm11998503wrx.114.2022.01.30.13.32.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:21 -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 04/13] module: Move latched RB-tree support to a separate file Date: Sun, 30 Jan 2022 21:32:05 +0000 Message-Id: <20220130213214.1042497-5-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates module latched RB-tree support (e.g. see __module_address()) from core module code into kernel/module/tree_lookup.c. Signed-off-by: Aaron Tomlin --- include/linux/module.h | 4 +- kernel/module/Makefile | 1 + kernel/module/internal.h | 33 +++++++++ kernel/module/main.c | 129 +----------------------------------- kernel/module/tree_lookup.c | 109 ++++++++++++++++++++++++++++++ 5 files changed, 147 insertions(+), 129 deletions(-) create mode 100644 kernel/module/tree_lookup.c diff --git a/include/linux/module.h b/include/linux/module.h index c7daac4d16ee..faeb7ae49b89 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -342,9 +342,9 @@ struct module_layout { #ifdef CONFIG_MODULES_TREE_LOOKUP /* Only touch one cacheline for common rbtree-for-core-layout case. */ #define __module_layout_align ____cacheline_aligned -#else +#else /* !CONFIG_MODULES_TREE_LOOKUP */ #define __module_layout_align -#endif +#endif /* CONFIG_MODULES_TREE_LOOKUP */ =20 struct mod_kallsyms { Elf_Sym *symtab; diff --git a/kernel/module/Makefile b/kernel/module/Makefile index ee20d864ad19..fc6d7a053a62 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -9,4 +9,5 @@ obj-$(CONFIG_MODULE_SIG) +=3D signing.o obj-$(CONFIG_MODULE_SIG_FORMAT) +=3D signature.o ifdef CONFIG_MODULES obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o +obj-$(CONFIG_MODULES_TREE_LOOKUP) +=3D tree_lookup.o endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 6150ec5ae042..de28d6bb7b5b 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -81,3 +81,36 @@ static inline void module_decompress_cleanup(struct load= _info *info) { } #endif + +#ifdef CONFIG_MODULES_TREE_LOOKUP +struct mod_tree_root { + struct latch_tree_root root; + unsigned long addr_min; + unsigned long addr_max; +}; + +extern struct mod_tree_root mod_tree; + +extern void mod_tree_insert(struct module *mod); +extern void mod_tree_remove_init(struct module *mod); +extern void mod_tree_remove(struct module *mod); +extern struct module *mod_find(unsigned long addr); +#else /* !CONFIG_MODULES_TREE_LOOKUP */ +static unsigned long module_addr_min =3D -1UL, module_addr_max =3D 0; + +static void mod_tree_insert(struct module *mod) { } +static void mod_tree_remove_init(struct module *mod) { } +static void mod_tree_remove(struct module *mod) { } +static struct module *mod_find(unsigned long addr) +{ + struct module *mod; + + list_for_each_entry_rcu(mod, &modules, list, + lockdep_is_held(&module_mutex)) { + if (within_module(addr, mod)) + return mod; + } + + return NULL; +} +#endif /* CONFIG_MODULES_TREE_LOOKUP */ diff --git a/kernel/module/main.c b/kernel/module/main.c index c91c7e57bca7..941be7788626 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -90,138 +90,13 @@ static DECLARE_WORK(init_free_wq, do_free_init); static LLIST_HEAD(init_free_list); =20 #ifdef CONFIG_MODULES_TREE_LOOKUP - -/* - * Use a latched RB-tree for __module_address(); this allows us to use - * RCU-sched lookups of the address from any context. - * - * This is conditional on PERF_EVENTS || TRACING because those can really = hit - * __module_address() hard by doing a lot of stack unwinding; potentially = from - * NMI context. - */ - -static __always_inline unsigned long __mod_tree_val(struct latch_tree_node= *n) -{ - struct module_layout *layout =3D container_of(n, struct module_layout, mt= n.node); - - return (unsigned long)layout->base; -} - -static __always_inline unsigned long __mod_tree_size(struct latch_tree_nod= e *n) -{ - struct module_layout *layout =3D container_of(n, struct module_layout, mt= n.node); - - return (unsigned long)layout->size; -} - -static __always_inline bool -mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b) -{ - return __mod_tree_val(a) < __mod_tree_val(b); -} - -static __always_inline int -mod_tree_comp(void *key, struct latch_tree_node *n) -{ - unsigned long val =3D (unsigned long)key; - unsigned long start, end; - - start =3D __mod_tree_val(n); - if (val < start) - return -1; - - end =3D start + __mod_tree_size(n); - if (val >=3D end) - return 1; - - return 0; -} - -static const struct latch_tree_ops mod_tree_ops =3D { - .less =3D mod_tree_less, - .comp =3D mod_tree_comp, -}; - -static struct mod_tree_root { - struct latch_tree_root root; - unsigned long addr_min; - unsigned long addr_max; -} mod_tree __cacheline_aligned =3D { +struct mod_tree_root mod_tree __cacheline_aligned =3D { .addr_min =3D -1UL, }; =20 #define module_addr_min mod_tree.addr_min #define module_addr_max mod_tree.addr_max - -static noinline void __mod_tree_insert(struct mod_tree_node *node) -{ - latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops); -} - -static void __mod_tree_remove(struct mod_tree_node *node) -{ - latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops); -} - -/* - * These modifications: insert, remove_init and remove; are serialized by = the - * module_mutex. - */ -static void mod_tree_insert(struct module *mod) -{ - mod->core_layout.mtn.mod =3D mod; - mod->init_layout.mtn.mod =3D mod; - - __mod_tree_insert(&mod->core_layout.mtn); - if (mod->init_layout.size) - __mod_tree_insert(&mod->init_layout.mtn); -} - -static void mod_tree_remove_init(struct module *mod) -{ - if (mod->init_layout.size) - __mod_tree_remove(&mod->init_layout.mtn); -} - -static void mod_tree_remove(struct module *mod) -{ - __mod_tree_remove(&mod->core_layout.mtn); - mod_tree_remove_init(mod); -} - -static struct module *mod_find(unsigned long addr) -{ - struct latch_tree_node *ltn; - - ltn =3D latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops); - if (!ltn) - return NULL; - - return container_of(ltn, struct mod_tree_node, node)->mod; -} - -#else /* MODULES_TREE_LOOKUP */ - -static unsigned long module_addr_min =3D -1UL, module_addr_max =3D 0; - -static void mod_tree_insert(struct module *mod) { } -static void mod_tree_remove_init(struct module *mod) { } -static void mod_tree_remove(struct module *mod) { } - -static struct module *mod_find(unsigned long addr) -{ - struct module *mod; - - list_for_each_entry_rcu(mod, &modules, list, - lockdep_is_held(&module_mutex)) { - if (within_module(addr, mod)) - return mod; - } - - return NULL; -} - -#endif /* MODULES_TREE_LOOKUP */ +#endif =20 /* * Bounds of module text, for speeding up __module_address. diff --git a/kernel/module/tree_lookup.c b/kernel/module/tree_lookup.c new file mode 100644 index 000000000000..037d6eb2f56f --- /dev/null +++ b/kernel/module/tree_lookup.c @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Modules tree lookup + * + * Copyright (C) 2015 Peter Zijlstra + * Copyright (C) 2015 Rusty Russell + */ + +#include +#include +#include "internal.h" + +/* + * Use a latched RB-tree for __module_address(); this allows us to use + * RCU-sched lookups of the address from any context. + * + * This is conditional on PERF_EVENTS || TRACING because those can really = hit + * __module_address() hard by doing a lot of stack unwinding; potentially = from + * NMI context. + */ + +__always_inline unsigned long __mod_tree_val(struct latch_tree_node *n) +{ + struct module_layout *layout =3D container_of(n, struct module_layout, mt= n.node); + + return (unsigned long)layout->base; +} + +__always_inline unsigned long __mod_tree_size(struct latch_tree_node *n) +{ + struct module_layout *layout =3D container_of(n, struct module_layout, mt= n.node); + + return (unsigned long)layout->size; +} + +__always_inline bool +mod_tree_less(struct latch_tree_node *a, struct latch_tree_node *b) +{ + return __mod_tree_val(a) < __mod_tree_val(b); +} + +__always_inline int +mod_tree_comp(void *key, struct latch_tree_node *n) +{ + unsigned long val =3D (unsigned long)key; + unsigned long start, end; + + start =3D __mod_tree_val(n); + if (val < start) + return -1; + + end =3D start + __mod_tree_size(n); + if (val >=3D end) + return 1; + + return 0; +} + +const struct latch_tree_ops mod_tree_ops =3D { + .less =3D mod_tree_less, + .comp =3D mod_tree_comp, +}; + +static noinline void __mod_tree_insert(struct mod_tree_node *node) +{ + latch_tree_insert(&node->node, &mod_tree.root, &mod_tree_ops); +} + +static void __mod_tree_remove(struct mod_tree_node *node) +{ + latch_tree_erase(&node->node, &mod_tree.root, &mod_tree_ops); +} + +/* + * These modifications: insert, remove_init and remove; are serialized by = the + * module_mutex. + */ +void mod_tree_insert(struct module *mod) +{ + mod->core_layout.mtn.mod =3D mod; + mod->init_layout.mtn.mod =3D mod; + + __mod_tree_insert(&mod->core_layout.mtn); + if (mod->init_layout.size) + __mod_tree_insert(&mod->init_layout.mtn); +} + +void mod_tree_remove_init(struct module *mod) +{ + if (mod->init_layout.size) + __mod_tree_remove(&mod->init_layout.mtn); +} + +void mod_tree_remove(struct module *mod) +{ + __mod_tree_remove(&mod->core_layout.mtn); + mod_tree_remove_init(mod); +} + +struct module *mod_find(unsigned long addr) +{ + struct latch_tree_node *ltn; + + ltn =3D latch_tree_find((void *)addr, &mod_tree.root, &mod_tree_ops); + if (!ltn) + return NULL; + + return container_of(ltn, struct mod_tree_node, node)->mod; +} --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 B66A0C433F5 for ; Sun, 30 Jan 2022 21:33:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356585AbiA3Vd1 (ORCPT ); Sun, 30 Jan 2022 16:33:27 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:51412 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356537AbiA3Vc1 (ORCPT ); Sun, 30 Jan 2022 16:32:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578346; 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=pUE7TRSTRaoiZ1wlOtXxOCtAqDzvICLX8IMQbGpBGgE=; b=g1cyHKPn9i57svEzHnFHpsG5fTA720dDHRXDVTBGgln+/6sneqS8dwzN5o5QizD6zX0jJB 0+GYSyerH5pIiGdytfFk8u48MImuRVPB61qDAlCIp0ptvP/z7w0QEmnd322R1gslNXdhGJ ke5XQ5oG4L/kwBZ4mzjsnl3ZQr2pYzc= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-583-R0zRtMAOPl-dlrOEzzco0Q-1; Sun, 30 Jan 2022 16:32:25 -0500 X-MC-Unique: R0zRtMAOPl-dlrOEzzco0Q-1 Received: by mail-wm1-f69.google.com with SMTP id f188-20020a1c1fc5000000b0034d79edde84so2542327wmf.0 for ; Sun, 30 Jan 2022 13:32:24 -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=pUE7TRSTRaoiZ1wlOtXxOCtAqDzvICLX8IMQbGpBGgE=; b=sF8rkALOZl6o0JcZeLONtCoqRUOaLheNsHhR1ujZRzB98TQ2CM6+HoxMacqmCR4Ij5 P/z/h+8vNTybnFg5oqcpUHFLWWoPExr7jqh7iAN9moTrpmve/2FzQ8Y9rPbiW3iFE1eU 5sdZzvMp5w+7wuf+A/Piu41Leu5jMy7KxxvOVNTbovdWwTZi652J8mPyhgKhREoM3JOw Eg2GfomlU1mCHygYmAS5KVu8yrjnW2uzR2agADo4gjrbIhBksFcrKDq71OyOgSsOtdXS XJcn1W+yhyvcgXV2p9B311fZh5Nnn2p+tBmmYDzQAsdK+k/bNMIkuUBpzo5NvEZYyFo3 zX6Q== X-Gm-Message-State: AOAM533f+0WLc5H6uL5jTxq5LhZF6K03AA1Ge9WzuULLZ1vgklpyDWU+ mYXs51nn3e3RP8sF5YXBYSCAv+fYtLGrVSEI1w0Oc5eSXCqua557seyc+znbaWVfnYqR0izB0+3 TpQyEgjtYffOKTLcBeBa9LpI= X-Received: by 2002:a05:600c:3d8c:: with SMTP id bi12mr16054804wmb.109.1643578343951; Sun, 30 Jan 2022 13:32:23 -0800 (PST) X-Google-Smtp-Source: ABdhPJwqbRW6GKt1zWz3cu463QUS2dcAe0Zt6f1zElO6WwC0sHHOja9ANvD68R3Z1bShAGRbtS0uXg== X-Received: by 2002:a05:600c:3d8c:: with SMTP id bi12mr16054788wmb.109.1643578343759; Sun, 30 Jan 2022 13:32:23 -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 p190sm7246166wmp.16.2022.01.30.13.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:23 -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 05/13] module: Move arch strict rwx support to a separate file Date: Sun, 30 Jan 2022 21:32:06 +0000 Message-Id: <20220130213214.1042497-6-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates applicable architecture code that support strict module rwx from core module code into kernel/module/arch_strict_rwx.c Signed-off-by: Aaron Tomlin --- include/linux/module.h | 17 +++++++++++ kernel/module/Makefile | 1 + kernel/module/arch_strict_rwx.c | 44 ++++++++++++++++++++++++++++ kernel/module/main.c | 51 --------------------------------- 4 files changed, 62 insertions(+), 51 deletions(-) create mode 100644 kernel/module/arch_strict_rwx.c diff --git a/include/linux/module.h b/include/linux/module.h index faeb7ae49b89..8b75380bc340 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -339,6 +339,23 @@ struct module_layout { #endif }; =20 +/* + * Modules' sections will be aligned on page boundaries + * to ensure complete separation of code and data, but + * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=3Dy + */ +#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX +# define debug_align(X) ALIGN(X, PAGE_SIZE) + +extern void frob_text(const struct module_layout *layout, + int (*set_memory)(unsigned long start, int num_pages)); +extern void module_enable_x(const struct module *mod); +#else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ +# define debug_align(X) (X) + +static void module_enable_x(const struct module *mod) { } +#endif /* CONFIG_ARCH_HAS_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 fc6d7a053a62..146509978708 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -10,4 +10,5 @@ obj-$(CONFIG_MODULE_SIG_FORMAT) +=3D signature.o ifdef CONFIG_MODULES obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) +=3D tree_lookup.o +obj-$(CONFIG_ARCH_HAS_STRICT_MODULE_RWX) +=3D arch_strict_rwx.o endif diff --git a/kernel/module/arch_strict_rwx.c b/kernel/module/arch_strict_rw= x.c new file mode 100644 index 000000000000..9801cb4fef36 --- /dev/null +++ b/kernel/module/arch_strict_rwx.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module arch strict rwx + * + * Copyright (C) 2015 Rusty Russell + */ + +#include +#include + +/* + * LKM RO/NX protection: protect module's text/ro-data + * from modification and any data from execution. + * + * General layout of module is: + * [text] [read-only-data] [ro-after-init] [writable data] + * text_size -----^ ^ ^ ^ + * ro_size ------------------------| | | + * ro_after_init_size -----------------------------| | + * size -----------------------------------------------------------| + * + * These values are always page-aligned (as is base) + */ + +/* + * Since some arches are moving towards PAGE_KERNEL module allocations ins= tead + * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of = the + * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless= of + * whether we are strict. + */ +void frob_text(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)); + set_memory((unsigned long)layout->base, + layout->text_size >> PAGE_SHIFT); +} + +void module_enable_x(const struct module *mod) +{ + frob_text(&mod->core_layout, set_memory_x); + frob_text(&mod->init_layout, set_memory_x); +} diff --git a/kernel/module/main.c b/kernel/module/main.c index 941be7788626..7e98ea12b146 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include @@ -63,17 +62,6 @@ #define CREATE_TRACE_POINTS #include =20 -/* - * Modules' sections will be aligned on page boundaries - * to ensure complete separation of code and data, but - * only when CONFIG_ARCH_HAS_STRICT_MODULE_RWX=3Dy - */ -#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX -# define debug_align(X) ALIGN(X, PAGE_SIZE) -#else -# define debug_align(X) (X) -#endif - /* * Mutex protects: * 1) List of modules (also safely readable with preempt_disable), @@ -1794,45 +1782,6 @@ static void mod_sysfs_teardown(struct module *mod) mod_sysfs_fini(mod); } =20 -/* - * LKM RO/NX protection: protect module's text/ro-data - * from modification and any data from execution. - * - * General layout of module is: - * [text] [read-only-data] [ro-after-init] [writable data] - * text_size -----^ ^ ^ ^ - * ro_size ------------------------| | | - * ro_after_init_size -----------------------------| | - * size -----------------------------------------------------------| - * - * These values are always page-aligned (as is base) - */ - -/* - * Since some arches are moving towards PAGE_KERNEL module allocations ins= tead - * of PAGE_KERNEL_EXEC, keep frob_text() and module_enable_x() outside of = the - * CONFIG_STRICT_MODULE_RWX block below because they are needed regardless= of - * whether we are strict. - */ -#ifdef CONFIG_ARCH_HAS_STRICT_MODULE_RWX -static void frob_text(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)); - set_memory((unsigned long)layout->base, - layout->text_size >> PAGE_SHIFT); -} - -static void module_enable_x(const struct module *mod) -{ - frob_text(&mod->core_layout, set_memory_x); - frob_text(&mod->init_layout, set_memory_x); -} -#else /* !CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ -static void module_enable_x(const struct module *mod) { } -#endif /* CONFIG_ARCH_HAS_STRICT_MODULE_RWX */ - #ifdef CONFIG_STRICT_MODULE_RWX static void frob_rodata(const struct module_layout *layout, int (*set_memory)(unsigned long start, int num_pages)) --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 A1944C433EF for ; Sun, 30 Jan 2022 21:33:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356903AbiA3Vdd (ORCPT ); Sun, 30 Jan 2022 16:33:33 -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-Google-Smtp-Source: ABdhPJyXgF4BvmYNdjyg50g9s9aIyE5u7AofJTtbWr5KA0J7EOgC8xa162nZ0tT185NM2i6SpoEnLg== 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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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 */ =20 +#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) +=3D livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) +=3D tree_lookup.o obj-$(CONFIG_ARCH_HAS_STRICT_MODULE_RWX) +=3D arch_strict_rwx.o +obj-$(CONFIG_STRICT_MODULE_RWX) +=3D 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); } =20 -#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 =3D SHF_WRITE|SHF_EXECINSTR; - int i; - - for (i =3D 0; i < hdr->e_shnum; i++) { - if ((sechdrs[i].sh_flags & shf_wx) =3D=3D 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 =3D SHF_WRITE|SHF_EXECINSTR; + int i; + + for (i =3D 0; i < hdr->e_shnum; i++) { + if ((sechdrs[i].sh_flags & shf_wx) =3D=3D 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; +} --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 A7740C433FE for ; Sun, 30 Jan 2022 21:33:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356788AbiA3Vdj (ORCPT ); Sun, 30 Jan 2022 16:33:39 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:32103 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356574AbiA3Vc3 (ORCPT ); Sun, 30 Jan 2022 16:32:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578349; 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=RJ3wtlLtaJBB+rGMcpTUIkLwDhDnJi3iDHMFvemCFL0=; b=EBdFXK1dO2h0iYMra49eKFx5C00EX6cegbqeUzz/rEua1Sb8/k4MPj38MJD0pN8xuE46u3 +Wa+qDWRUgYmcDjBssIAC1M1F7St5kVOIjOnWTiJw0C3avpbAdf6TPCvPw2iJiv+now+/d hpOwrhDbRFNjo5PePJVjXV4q7b8zepU= 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-102-DW8Bf2W4Mu2_U7n3wDc_zA-1; Sun, 30 Jan 2022 16:32:27 -0500 X-MC-Unique: DW8Bf2W4Mu2_U7n3wDc_zA-1 Received: by mail-wm1-f71.google.com with SMTP id v185-20020a1cacc2000000b0034906580813so9365031wme.1 for ; Sun, 30 Jan 2022 13:32:27 -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=RJ3wtlLtaJBB+rGMcpTUIkLwDhDnJi3iDHMFvemCFL0=; b=5YCdcbyv/hmNo9GX/QNswyTpL7xwua2vbEr/pwMQtVR2SaGFF8RAmaaUzzLYAvgdy2 qiS1SYOcKB76Zry0Q3UMUyWPwQY9lqT/D2bT3mjsnGx4Qk3Q21HFFbVrDzo9EbGW4tOl gAiBwgrplkKtOYsbBcfnMbNLbEanmL5cOxL4+1Agtsb8EL7equFQnhgXVtv4HOpf7bk2 T+2pvCHp3IfHoL8K9rgxeBo0Et+/c/0iwmpa7I44yxKCwyC/H0/iTBwhSyvycb/qUM1U 7BIAEM2SXKTLfcY2ldX23F8yczphXMj+VZniOwjI6cpn9AswBcHyFxKwNtI/AkgJaVbE 5AZg== X-Gm-Message-State: AOAM5321uAWlDHBPS8aoof37qPJG5A7ok4Ao5W1xB2LROZplpDyFment UHOyI5KO+8LK0QMlVDXluUIqUAXKqaFmhfBRBxUGGpU196RvlgB2/vxTZSfvYsXZjPSecHFDnml YEcO6yLtZocXjHBf1qvMZpT0= X-Received: by 2002:a7b:ca55:: with SMTP id m21mr25031015wml.114.1643578346415; Sun, 30 Jan 2022 13:32:26 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8yo00K8umZij8wpGoKor3DR0Q5lrqw7AuO7i/8XAPVAsVqsIdoHyV/SKYbZG/stHDtrR7hw== X-Received: by 2002:a7b:ca55:: with SMTP id m21mr25031003wml.114.1643578346169; Sun, 30 Jan 2022 13:32:26 -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 bg26sm7691200wmb.48.2022.01.30.13.32.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:25 -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 07/13] module: Move extra signature support out of core code Date: Sun, 30 Jan 2022 21:32:08 +0000 Message-Id: <20220130213214.1042497-8-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates additional module signature check code from core module code into kernel/module/signing.c. Signed-off-by: Aaron Tomlin --- include/linux/module.h | 5 ++- kernel/module/internal.h | 9 +++++ kernel/module/main.c | 87 ---------------------------------------- kernel/module/signing.c | 75 ++++++++++++++++++++++++++++++++++ 4 files changed, 87 insertions(+), 89 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index 520c0f4bb968..15ba2ebbca3e 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -720,8 +720,8 @@ static inline bool set_livepatch_module(struct module *= mod) return false; } =20 -bool is_module_sig_enforced(void); -void set_module_sig_enforced(void); +extern bool is_module_sig_enforced(void); +extern void set_module_sig_enforced(void); =20 #else /* !CONFIG_MODULES... */ =20 @@ -911,6 +911,7 @@ static inline bool module_sig_ok(struct module *module) { return true; } +#define sig_enforce false #endif /* CONFIG_MODULE_SIG */ =20 int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, diff --git a/kernel/module/internal.h b/kernel/module/internal.h index de28d6bb7b5b..2ec2a1d9dd9f 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -114,3 +114,12 @@ static struct module *mod_find(unsigned long addr) return NULL; } #endif /* CONFIG_MODULES_TREE_LOOKUP */ + +#ifdef CONFIG_MODULE_SIG +extern int module_sig_check(struct load_info *info, int flags); +#else /* !CONFIG_MODULE_SIG */ +static int module_sig_check(struct load_info *info, int flags) +{ + return 0; +} +#endif /* !CONFIG_MODULE_SIG */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 1a0e659a27bc..90c7266087d7 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -22,7 +22,6 @@ #include #include #include -#include #include #include #include @@ -123,28 +122,6 @@ static void module_assert_mutex_or_preempt(void) #endif } =20 -#ifdef CONFIG_MODULE_SIG -static bool sig_enforce =3D IS_ENABLED(CONFIG_MODULE_SIG_FORCE); -module_param(sig_enforce, bool_enable_only, 0644); - -void set_module_sig_enforced(void) -{ - sig_enforce =3D true; -} -#else -#define sig_enforce false -#endif - -/* - * Export sig_enforce kernel cmdline parameter to allow other subsystems r= ely - * on that instead of directly to CONFIG_MODULE_SIG_FORCE config. - */ -bool is_module_sig_enforced(void) -{ - return sig_enforce; -} -EXPORT_SYMBOL(is_module_sig_enforced); - /* Block module loading/unloading? */ int modules_disabled =3D 0; core_param(nomodule, modules_disabled, bint, 0); @@ -2525,70 +2502,6 @@ static inline void kmemleak_load_module(const struct= module *mod, } #endif =20 -#ifdef CONFIG_MODULE_SIG -static int module_sig_check(struct load_info *info, int flags) -{ - int err =3D -ENODATA; - const unsigned long markerlen =3D sizeof(MODULE_SIG_STRING) - 1; - const char *reason; - const void *mod =3D info->hdr; - bool mangled_module =3D flags & (MODULE_INIT_IGNORE_MODVERSIONS | - MODULE_INIT_IGNORE_VERMAGIC); - /* - * Do not allow mangled modules as a module with version information - * removed is no longer the module that was signed. - */ - if (!mangled_module && - info->len > markerlen && - memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) =3D= =3D 0) { - /* We truncate the module to discard the signature */ - info->len -=3D markerlen; - err =3D mod_verify_sig(mod, info); - if (!err) { - info->sig_ok =3D true; - return 0; - } - } - - /* - * We don't permit modules to be loaded into the trusted kernels - * without a valid signature on them, but if we're not enforcing, - * certain errors are non-fatal. - */ - switch (err) { - case -ENODATA: - reason =3D "unsigned module"; - break; - case -ENOPKG: - reason =3D "module with unsupported crypto"; - break; - case -ENOKEY: - reason =3D "module with unavailable key"; - break; - - default: - /* - * All other errors are fatal, including lack of memory, - * unparseable signatures, and signature check failures -- - * even if signatures aren't required. - */ - return err; - } - - if (is_module_sig_enforced()) { - pr_notice("Loading of %s is rejected\n", reason); - return -EKEYREJECTED; - } - - return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); -} -#else /* !CONFIG_MODULE_SIG */ -static int module_sig_check(struct load_info *info, int flags) -{ - return 0; -} -#endif /* !CONFIG_MODULE_SIG */ - static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) { #if defined(CONFIG_64BIT) diff --git a/kernel/module/signing.c b/kernel/module/signing.c index 8aeb6d2ee94b..ff41541e982a 100644 --- a/kernel/module/signing.c +++ b/kernel/module/signing.c @@ -11,9 +11,28 @@ #include #include #include +#include #include #include "internal.h" =20 +static bool sig_enforce =3D IS_ENABLED(CONFIG_MODULE_SIG_FORCE); +module_param(sig_enforce, bool_enable_only, 0644); + +/* + * Export sig_enforce kernel cmdline parameter to allow other subsystems r= ely + * on that instead of directly to CONFIG_MODULE_SIG_FORCE config. + */ +bool is_module_sig_enforced(void) +{ + return sig_enforce; +} +EXPORT_SYMBOL(is_module_sig_enforced); + +void set_module_sig_enforced(void) +{ + sig_enforce =3D true; +} + /* * Verify the signature on a module. */ @@ -43,3 +62,59 @@ int mod_verify_sig(const void *mod, struct load_info *in= fo) VERIFYING_MODULE_SIGNATURE, NULL, NULL); } + +int module_sig_check(struct load_info *info, int flags) +{ + int err =3D -ENODATA; + const unsigned long markerlen =3D sizeof(MODULE_SIG_STRING) - 1; + const char *reason; + const void *mod =3D info->hdr; + + /* + * Require flags =3D=3D 0, as a module with version information + * removed is no longer the module that was signed + */ + if (flags =3D=3D 0 && + info->len > markerlen && + memcmp(mod + info->len - markerlen, MODULE_SIG_STRING, markerlen) =3D= =3D 0) { + /* We truncate the module to discard the signature */ + info->len -=3D markerlen; + err =3D mod_verify_sig(mod, info); + if (!err) { + info->sig_ok =3D true; + return 0; + } + } + + /* + * We don't permit modules to be loaded into the trusted kernels + * without a valid signature on them, but if we're not enforcing, + * certain errors are non-fatal. + */ + switch (err) { + case -ENODATA: + reason =3D "unsigned module"; + break; + case -ENOPKG: + reason =3D "module with unsupported crypto"; + break; + case -ENOKEY: + reason =3D "module with unavailable key"; + break; + + default: + /* + * All other errors are fatal, including lack of memory, + * unparseable signatures, and signature check failures -- + * even if signatures aren't required. + */ + return err; + } + + if (is_module_sig_enforced()) { + pr_notice("Loading of %s is rejected\n", reason); + return -EKEYREJECTED; + } + + return security_locked_down(LOCKDOWN_MODULE_SIGNATURE); +} --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 EDC62C4167B for ; Sun, 30 Jan 2022 21:33:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356625AbiA3Vdh (ORCPT ); Sun, 30 Jan 2022 16:33:37 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:58488 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356596AbiA3Vcb (ORCPT ); Sun, 30 Jan 2022 16:32:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578350; 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=tQAfe4ObvGQJfJNxu8h5P3w1uhPwPWFzYEYylizISc0=; b=RGJRoZvhTurIR9K3UvOvVsVHxn3Dl5WkKpmJ6jXgu3aFS7yAH/0eVj/qeOxs0soiuaAbVK Uq4QBaEGxFQ3SUSTvzy/iHU5e7obsVixi/71gOyGPil/XDNo1uoxppcTUqBuXm2Bee/vxl bGcbRND3pDDh88AR+AHwyfPIWgUjoAE= 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-211-nnffSMZSP0alAPfsiA-GyA-1; Sun, 30 Jan 2022 16:32:29 -0500 X-MC-Unique: nnffSMZSP0alAPfsiA-GyA-1 Received: by mail-wm1-f71.google.com with SMTP id n22-20020a05600c3b9600b00352d3f6a850so442123wms.3 for ; Sun, 30 Jan 2022 13:32:29 -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=tQAfe4ObvGQJfJNxu8h5P3w1uhPwPWFzYEYylizISc0=; b=w+MImrD1saHos6DDYy4jGwRbKyYX0EspitqerlofVvVYVHBjGvhOMwo84s8pZGZ65/ Ba5K7zIjmocFPOkBhTaLRDP0x37XJQeCdvvfeEFntmiiIYOMPp3YuSit8tXD6rglwOb5 5ns+ybgz42CHjB+lt0BBoadvBv8r9+N1chg9ZykBDhu/XGwijIDpGYCgu7DNAqdBhOpQ 5Cc7H3bhQoCYmaDtXL6rLio+CBWgHZdy7swkm8Axov6svuQjUgjIK6csnZ+87bndocpb ZTXcnlXtaimBUUEHoOD6qzSLuC6XZ2oZLWYcxGiW6iSTHNigs1Q9isjiAH7ln9d93MWZ ri1A== X-Gm-Message-State: AOAM530y17wSrMGYEqNuwWxsixPUa921SDV4QxgR4YZ98d4nUxPZkWDA 2GtNGJPIHfQfH3axShitRAxInRGcOt3hGWWBxMhTtgNvkON4aQ43BvUVr+0WoStnyUgE5s0+Jic Y8xfloxtYTBBeIl9HpoAw8fs= X-Received: by 2002:a7b:cb54:: with SMTP id v20mr24427164wmj.2.1643578347925; Sun, 30 Jan 2022 13:32:27 -0800 (PST) X-Google-Smtp-Source: ABdhPJzjlW03yjRZL5UouuPmS7o2QoqJ7yqYAOCyQcMp512qs+v3prNf6O+tPTA7MOdVlgprpABsyw== X-Received: by 2002:a7b:cb54:: with SMTP id v20mr24427146wmj.2.1643578347723; Sun, 30 Jan 2022 13:32:27 -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 i17sm9896690wru.107.2022.01.30.13.32.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:27 -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 08/13] module: Move kmemleak support to a separate file Date: Sun, 30 Jan 2022 21:32:09 +0000 Message-Id: <20220130213214.1042497-9-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates kmemleak code out of core module code into kernel/module/debug_kmemleak.c Signed-off-by: Aaron Tomlin --- kernel/module/Makefile | 1 + kernel/module/debug_kmemleak.c | 30 ++++++++++++++++++++++++++++++ kernel/module/internal.h | 8 ++++++++ kernel/module/main.c | 27 --------------------------- 4 files changed, 39 insertions(+), 27 deletions(-) create mode 100644 kernel/module/debug_kmemleak.c diff --git a/kernel/module/Makefile b/kernel/module/Makefile index d1dada641ad6..10efb144551b 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -12,4 +12,5 @@ obj-$(CONFIG_LIVEPATCH) +=3D livepatch.o obj-$(CONFIG_MODULES_TREE_LOOKUP) +=3D tree_lookup.o obj-$(CONFIG_ARCH_HAS_STRICT_MODULE_RWX) +=3D arch_strict_rwx.o obj-$(CONFIG_STRICT_MODULE_RWX) +=3D strict_rwx.o +obj-$(CONFIG_DEBUG_KMEMLEAK) +=3D debug_kmemleak.o endif diff --git a/kernel/module/debug_kmemleak.c b/kernel/module/debug_kmemleak.c new file mode 100644 index 000000000000..e896c2268011 --- /dev/null +++ b/kernel/module/debug_kmemleak.c @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module kmemleak support + * + * Copyright (C) 2009 Catalin Marinas + */ + +#include +#include +#include "internal.h" + +void kmemleak_load_module(const struct module *mod, + const struct load_info *info) +{ + unsigned int i; + + /* only scan the sections containing data */ + kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); + + for (i =3D 1; i < info->hdr->e_shnum; i++) { + /* Scan all writable sections that's not executable */ + if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) || + !(info->sechdrs[i].sh_flags & SHF_WRITE) || + (info->sechdrs[i].sh_flags & SHF_EXECINSTR)) + continue; + + kmemleak_scan_area((void *)info->sechdrs[i].sh_addr, + info->sechdrs[i].sh_size, GFP_KERNEL); + } +} diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 2ec2a1d9dd9f..4c2f64a75401 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -6,6 +6,7 @@ */ =20 #include +#include #include #include =20 @@ -123,3 +124,10 @@ static int module_sig_check(struct load_info *info, in= t flags) return 0; } #endif /* !CONFIG_MODULE_SIG */ + +#ifdef CONFIG_DEBUG_KMEMLEAK +extern void kmemleak_load_module(const struct module *mod, const struct lo= ad_info *info); +#else /* !CONFIG_DEBUG_KMEMLEAK */ +static inline void __maybe_unused kmemleak_load_module(const struct module= *mod, + const struct load_info *info) { } +#endif /* CONFIG_DEBUG_KMEMLEAK */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 90c7266087d7..80790d39a6b3 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -2475,33 +2475,6 @@ bool __weak module_exit_section(const char *name) return strstarts(name, ".exit"); } =20 -#ifdef CONFIG_DEBUG_KMEMLEAK -static void kmemleak_load_module(const struct module *mod, - const struct load_info *info) -{ - unsigned int i; - - /* only scan the sections containing data */ - kmemleak_scan_area(mod, sizeof(struct module), GFP_KERNEL); - - for (i =3D 1; i < info->hdr->e_shnum; i++) { - /* Scan all writable sections that's not executable */ - if (!(info->sechdrs[i].sh_flags & SHF_ALLOC) || - !(info->sechdrs[i].sh_flags & SHF_WRITE) || - (info->sechdrs[i].sh_flags & SHF_EXECINSTR)) - continue; - - kmemleak_scan_area((void *)info->sechdrs[i].sh_addr, - info->sechdrs[i].sh_size, GFP_KERNEL); - } -} -#else -static inline void kmemleak_load_module(const struct module *mod, - const struct load_info *info) -{ -} -#endif - static int validate_section_offset(struct load_info *info, Elf_Shdr *shdr) { #if defined(CONFIG_64BIT) --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 7D7F9C433EF for ; Sun, 30 Jan 2022 21:33:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356745AbiA3Vdv (ORCPT ); Sun, 30 Jan 2022 16:33:51 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:48574 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356605AbiA3Vcd (ORCPT ); Sun, 30 Jan 2022 16:32:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578352; 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=PX4H5UxmqUAe2K8VvJ1WLyetaF8zMIVDnnCaQaquytM=; b=Srjo6rPHGEBwXpxD2jk/5VZwQJa0OdgWPgewtmKzWYBeEjnkG3iDFzFzTJa0OThn7Ibqdz ff3hLu1rdGfayLAku7JhYWwb62aVnQh2N/2tiOWr1VO6Eei2AXdPe56S2tbX1Xvn+Pz6wO GWC0bVQhNJ6d8q+Tv/wfbglAeC4MAs4= 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-219-EUJinSpmN02jssvIbdkNwg-1; Sun, 30 Jan 2022 16:32:31 -0500 X-MC-Unique: EUJinSpmN02jssvIbdkNwg-1 Received: by mail-wm1-f72.google.com with SMTP id s1-20020a1ca901000000b0034ece94dd8cso9356338wme.5 for ; Sun, 30 Jan 2022 13:32:30 -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=PX4H5UxmqUAe2K8VvJ1WLyetaF8zMIVDnnCaQaquytM=; b=XZlI4l+65sipLZXi8kriFXK/opNuQ4e3J+ihHD/PQNaYSbqVBFDioWvkxrY+/RF1s3 tSdKg7aabkLraiA0x3iWMD/z4ewRyOqkaZiDKMGMLvQ+Bj1cRVd2y5ooYaHrXUCyKs8h o65Pu+KxncSs1BRXV5yDYtarHBIGB71WjgAkmTEKCXhOIQ7eQeEwwlmlezpGlR9ub8Tp d24bNNwrC26AKCWVHOmTDeTamC3X4l3ypPkBbhd2DKLOIDgXwOOu5eBIFOgGFI0lhngZ sBxDbFQnnDBwyYgjlkn0BffvzChUilkVSz5D7NYfWUryLann8IPp0GLYKbUCpoPmPaIR XAfw== X-Gm-Message-State: AOAM530lrbozB/DAGQ8DLjyrZ7scioct5Uq2VOaGAS7oZQ1rPUUZDzyR 6kRjxlTRrNtOKEoIHuVvJbvhOSW1i7hJ/ufv6PzrKd3Y8S6FDMAvStwlkTZAyl6g4d+wobFwN6w 9Erk/Ngl6GiOO7ImGCANd4cY= X-Received: by 2002:a5d:4390:: with SMTP id i16mr5673694wrq.516.1643578349453; Sun, 30 Jan 2022 13:32:29 -0800 (PST) X-Google-Smtp-Source: ABdhPJzpt9BwdJVQ+2kLhAtXnNfFmUd33dFceyAMVu0RO+lyYHXqOeJCmLi1+YIOL9il9ijMQ1A5Bg== X-Received: by 2002:a5d:4390:: with SMTP id i16mr5673673wrq.516.1643578348930; Sun, 30 Jan 2022 13:32:28 -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 j12sm9994535wru.38.2022.01.30.13.32.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:28 -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 09/13] module: Move kallsyms support into a separate file Date: Sun, 30 Jan 2022 21:32:10 +0000 Message-Id: <20220130213214.1042497-10-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates kallsyms code out of core module code kernel/module/kallsyms.c Signed-off-by: Aaron Tomlin --- kernel/module/Makefile | 1 + kernel/module/internal.h | 27 ++ kernel/module/kallsyms.c | 502 +++++++++++++++++++++++++++++++++++++ kernel/module/main.c | 516 +-------------------------------------- 4 files changed, 534 insertions(+), 512 deletions(-) create mode 100644 kernel/module/kallsyms.c diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 10efb144551b..906f83e1931b 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -13,4 +13,5 @@ obj-$(CONFIG_MODULES_TREE_LOOKUP) +=3D tree_lookup.o obj-$(CONFIG_ARCH_HAS_STRICT_MODULE_RWX) +=3D arch_strict_rwx.o obj-$(CONFIG_STRICT_MODULE_RWX) +=3D strict_rwx.o obj-$(CONFIG_DEBUG_KMEMLEAK) +=3D debug_kmemleak.o +obj-$(CONFIG_KALLSYMS) +=3D kallsyms.o endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 4c2f64a75401..866fce7c1a33 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -57,6 +57,11 @@ struct load_info { }; =20 extern int mod_verify_sig(const void *mod, struct load_info *info); +extern struct module *find_module_all(const char *name, size_t len, bool e= ven_unformed); +extern unsigned long kernel_symbol_value(const struct kernel_symbol *sym); +extern int cmp_name(const void *name, const void *sym); +extern long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *s= echdr, + unsigned int section); =20 #ifdef CONFIG_LIVEPATCH extern int copy_module_elf(struct module *mod, struct load_info *info); @@ -131,3 +136,25 @@ extern void kmemleak_load_module(const struct module *= mod, const struct load_inf static inline void __maybe_unused kmemleak_load_module(const struct module= *mod, const struct load_info *info) { } #endif /* CONFIG_DEBUG_KMEMLEAK */ + +#ifdef CONFIG_KALLSYMS +#ifdef CONFIG_STACKTRACE_BUILD_ID +extern void init_build_id(struct module *mod, const struct load_info *info= ); +#else /* !CONFIG_STACKTRACE_BUILD_ID */ +static inline void init_build_id(struct module *mod, const struct load_inf= o *info) { } + +#endif +extern void layout_symtab(struct module *mod, struct load_info *info); +extern void add_kallsyms(struct module *mod, const struct load_info *info); +extern bool sect_empty(const Elf_Shdr *sect); +extern const char *find_kallsyms_symbol(struct module *mod, unsigned long = addr, + unsigned long *size, unsigned long *offset); +#else /* !CONFIG_KALLSYMS */ +static inline void layout_symtab(struct module *mod, struct load_info *inf= o) { } +static inline void add_kallsyms(struct module *mod, const struct load_info= *info) { } +static inline char *find_kallsyms_symbol(struct module *mod, unsigned long= addr, + unsigned long *size, unsigned long *offset) +{ + return NULL; +} +#endif /* CONFIG_KALLSYMS */ diff --git a/kernel/module/kallsyms.c b/kernel/module/kallsyms.c new file mode 100644 index 000000000000..ed28f6310701 --- /dev/null +++ b/kernel/module/kallsyms.c @@ -0,0 +1,502 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module kallsyms support + * + * Copyright (C) 2010 Rusty Russell + */ + +#include +#include +#include +#include +#include "internal.h" + +/* Lookup exported symbol in given range of kernel_symbols */ +static const struct kernel_symbol *lookup_exported_symbol(const char *name, + const struct kernel_symbol *start, + const struct kernel_symbol *stop) +{ + return bsearch(name, start, stop - start, + sizeof(struct kernel_symbol), cmp_name); +} + +static int is_exported(const char *name, unsigned long value, + const struct module *mod) +{ + const struct kernel_symbol *ks; + + if (!mod) + ks =3D lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab); + else + ks =3D lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms= ); + + return ks !=3D NULL && kernel_symbol_value(ks) =3D=3D value; +} + +/* As per nm */ +static char elf_type(const Elf_Sym *sym, const struct load_info *info) +{ + const Elf_Shdr *sechdrs =3D info->sechdrs; + + if (ELF_ST_BIND(sym->st_info) =3D=3D STB_WEAK) { + if (ELF_ST_TYPE(sym->st_info) =3D=3D STT_OBJECT) + return 'v'; + else + return 'w'; + } + if (sym->st_shndx =3D=3D SHN_UNDEF) + return 'U'; + if (sym->st_shndx =3D=3D SHN_ABS || sym->st_shndx =3D=3D info->index.pcpu) + return 'a'; + if (sym->st_shndx >=3D SHN_LORESERVE) + return '?'; + if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) + return 't'; + if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC + && sechdrs[sym->st_shndx].sh_type !=3D SHT_NOBITS) { + if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) + return 'r'; + else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) + return 'g'; + else + return 'd'; + } + if (sechdrs[sym->st_shndx].sh_type =3D=3D SHT_NOBITS) { + if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) + return 's'; + else + return 'b'; + } + if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name, + ".debug")) { + return 'n'; + } + return '?'; +} + +static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs, + unsigned int shnum, unsigned int pcpundx) +{ + const Elf_Shdr *sec; + + if (src->st_shndx =3D=3D SHN_UNDEF + || src->st_shndx >=3D shnum + || !src->st_name) + return false; + +#ifdef CONFIG_KALLSYMS_ALL + if (src->st_shndx =3D=3D pcpundx) + return true; +#endif + + sec =3D sechdrs + src->st_shndx; + if (!(sec->sh_flags & SHF_ALLOC) +#ifndef CONFIG_KALLSYMS_ALL + || !(sec->sh_flags & SHF_EXECINSTR) +#endif + || (sec->sh_entsize & INIT_OFFSET_MASK)) + return false; + + return true; +} + +/* + * We only allocate and copy the strings needed by the parts of symtab + * we keep. This is simple, but has the effect of making multiple + * copies of duplicates. We could be more sophisticated, see + * linux-kernel thread starting with + * <73defb5e4bca04a6431392cc341112b1@localhost>. + */ +void layout_symtab(struct module *mod, struct load_info *info) +{ + Elf_Shdr *symsect =3D info->sechdrs + info->index.sym; + Elf_Shdr *strsect =3D info->sechdrs + info->index.str; + const Elf_Sym *src; + unsigned int i, nsrc, ndst, strtab_size =3D 0; + + /* Put symbol section at end of init part of module. */ + symsect->sh_flags |=3D SHF_ALLOC; + symsect->sh_entsize =3D get_offset(mod, &mod->init_layout.size, symsect, + info->index.sym) | INIT_OFFSET_MASK; + pr_debug("\t%s\n", info->secstrings + symsect->sh_name); + + src =3D (void *)info->hdr + symsect->sh_offset; + nsrc =3D symsect->sh_size / sizeof(*src); + + /* Compute total space required for the core symbols' strtab. */ + for (ndst =3D i =3D 0; i < nsrc; i++) { + if (i =3D=3D 0 || is_livepatch_module(mod) || + is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum, + info->index.pcpu)) { + strtab_size +=3D strlen(&info->strtab[src[i].st_name])+1; + ndst++; + } + } + + /* Append room for core symbols at end of core part. */ + info->symoffs =3D ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1= ); + info->stroffs =3D mod->core_layout.size =3D info->symoffs + ndst * sizeof= (Elf_Sym); + mod->core_layout.size +=3D strtab_size; + info->core_typeoffs =3D mod->core_layout.size; + mod->core_layout.size +=3D ndst * sizeof(char); + mod->core_layout.size =3D debug_align(mod->core_layout.size); + + /* Put string table section at end of init part of module. */ + strsect->sh_flags |=3D SHF_ALLOC; + strsect->sh_entsize =3D get_offset(mod, &mod->init_layout.size, strsect, + info->index.str) | INIT_OFFSET_MASK; + pr_debug("\t%s\n", info->secstrings + strsect->sh_name); + + /* We'll tack temporary mod_kallsyms on the end. */ + mod->init_layout.size =3D ALIGN(mod->init_layout.size, + __alignof__(struct mod_kallsyms)); + info->mod_kallsyms_init_off =3D mod->init_layout.size; + mod->init_layout.size +=3D sizeof(struct mod_kallsyms); + info->init_typeoffs =3D mod->init_layout.size; + mod->init_layout.size +=3D nsrc * sizeof(char); + mod->init_layout.size =3D debug_align(mod->init_layout.size); +} + +/* + * We use the full symtab and strtab which layout_symtab arranged to + * be appended to the init section. Later we switch to the cut-down + * core-only ones. + */ +void add_kallsyms(struct module *mod, const struct load_info *info) +{ + unsigned int i, ndst; + const Elf_Sym *src; + Elf_Sym *dst; + char *s; + Elf_Shdr *symsec =3D &info->sechdrs[info->index.sym]; + + /* Set up to point into init section. */ + mod->kallsyms =3D mod->init_layout.base + info->mod_kallsyms_init_off; + + mod->kallsyms->symtab =3D (void *)symsec->sh_addr; + mod->kallsyms->num_symtab =3D symsec->sh_size / sizeof(Elf_Sym); + /* Make sure we get permanent strtab: don't use info->strtab. */ + mod->kallsyms->strtab =3D (void *)info->sechdrs[info->index.str].sh_addr; + mod->kallsyms->typetab =3D mod->init_layout.base + info->init_typeoffs; + + /* + * Now populate the cut down core kallsyms for after init + * and set types up while we still have access to sections. + */ + mod->core_kallsyms.symtab =3D dst =3D mod->core_layout.base + info->symof= fs; + mod->core_kallsyms.strtab =3D s =3D mod->core_layout.base + info->stroffs; + mod->core_kallsyms.typetab =3D mod->core_layout.base + info->core_typeoff= s; + src =3D mod->kallsyms->symtab; + for (ndst =3D i =3D 0; i < mod->kallsyms->num_symtab; i++) { + mod->kallsyms->typetab[i] =3D elf_type(src + i, info); + if (i =3D=3D 0 || is_livepatch_module(mod) || + is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum, + info->index.pcpu)) { + mod->core_kallsyms.typetab[ndst] =3D + mod->kallsyms->typetab[i]; + dst[ndst] =3D src[i]; + dst[ndst++].st_name =3D s - mod->core_kallsyms.strtab; + s +=3D strscpy(s, &mod->kallsyms->strtab[src[i].st_name], + KSYM_NAME_LEN) + 1; + } + } + mod->core_kallsyms.num_symtab =3D ndst; +} + +inline bool sect_empty(const Elf_Shdr *sect) +{ + return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size =3D=3D 0; +} + +#ifdef CONFIG_STACKTRACE_BUILD_ID +void init_build_id(struct module *mod, const struct load_info *info) +{ + const Elf_Shdr *sechdr; + unsigned int i; + + for (i =3D 0; i < info->hdr->e_shnum; i++) { + sechdr =3D &info->sechdrs[i]; + if (!sect_empty(sechdr) && sechdr->sh_type =3D=3D SHT_NOTE && + !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id, + sechdr->sh_size)) + break; + } +} +#endif + +/* + * This ignores the intensely annoying "mapping symbols" found + * in ARM ELF files: $a, $t and $d. + */ +static inline int is_arm_mapping_symbol(const char *str) +{ + if (str[0] =3D=3D '.' && str[1] =3D=3D 'L') + return true; + return str[0] =3D=3D '$' && strchr("axtd", str[1]) + && (str[2] =3D=3D '\0' || str[2] =3D=3D '.'); +} + +static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, uns= igned int symnum) +{ + return kallsyms->strtab + kallsyms->symtab[symnum].st_name; +} + +/* + * Given a module and address, find the corresponding symbol and return it= s name + * while providing its size and offset if needed. + */ +const char *find_kallsyms_symbol(struct module *mod, + unsigned long addr, + unsigned long *size, + unsigned long *offset) +{ + unsigned int i, best =3D 0; + unsigned long nextval, bestval; + struct mod_kallsyms *kallsyms =3D rcu_dereference_sched(mod->kallsyms); + + /* At worse, next value is at end of module */ + if (within_module_init(addr, mod)) + nextval =3D (unsigned long)mod->init_layout.base+mod->init_layout.text_s= ize; + else + nextval =3D (unsigned long)mod->core_layout.base+mod->core_layout.text_s= ize; + + bestval =3D kallsyms_symbol_value(&kallsyms->symtab[best]); + + /* + * Scan for closest preceding symbol, and next symbol. (ELF + * starts real symbols at 1). + */ + for (i =3D 1; i < kallsyms->num_symtab; i++) { + const Elf_Sym *sym =3D &kallsyms->symtab[i]; + unsigned long thisval =3D kallsyms_symbol_value(sym); + + if (sym->st_shndx =3D=3D SHN_UNDEF) + continue; + + /* + * We ignore unnamed symbols: they're uninformative + * and inserted at a whim. + */ + if (*kallsyms_symbol_name(kallsyms, i) =3D=3D '\0' + || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i))) + continue; + + if (thisval <=3D addr && thisval > bestval) { + best =3D i; + bestval =3D thisval; + } + if (thisval > addr && thisval < nextval) + nextval =3D thisval; + } + + if (!best) + return NULL; + + if (size) + *size =3D nextval - bestval; + if (offset) + *offset =3D addr - bestval; + + return kallsyms_symbol_name(kallsyms, best); +} + +void * __weak dereference_module_function_descriptor(struct module *mod, + void *ptr) +{ + return ptr; +} + +/* + * For kallsyms to ask for address resolution. NULL means not found. Car= eful + * not to lock to avoid deadlock on oopses, simply disable preemption. + */ +const char *module_address_lookup(unsigned long addr, + unsigned long *size, + unsigned long *offset, + char **modname, + const unsigned char **modbuildid, + char *namebuf) +{ + const char *ret =3D NULL; + struct module *mod; + + preempt_disable(); + mod =3D __module_address(addr); + if (mod) { + if (modname) + *modname =3D mod->name; + if (modbuildid) { +#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) + *modbuildid =3D mod->build_id; +#else + *modbuildid =3D NULL; +#endif + } + + ret =3D find_kallsyms_symbol(mod, addr, size, offset); + } + /* Make a copy in here where it's safe */ + if (ret) { + strncpy(namebuf, ret, KSYM_NAME_LEN - 1); + ret =3D namebuf; + } + preempt_enable(); + + return ret; +} + +int lookup_module_symbol_name(unsigned long addr, char *symname) +{ + struct module *mod; + + preempt_disable(); + list_for_each_entry_rcu(mod, &modules, list) { + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + continue; + if (within_module(addr, mod)) { + const char *sym; + + sym =3D find_kallsyms_symbol(mod, addr, NULL, NULL); + if (!sym) + goto out; + + strscpy(symname, sym, KSYM_NAME_LEN); + preempt_enable(); + return 0; + } + } +out: + preempt_enable(); + return -ERANGE; +} + +int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, + unsigned long *offset, char *modname, char *name) +{ + struct module *mod; + + preempt_disable(); + list_for_each_entry_rcu(mod, &modules, list) { + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + continue; + if (within_module(addr, mod)) { + const char *sym; + + sym =3D find_kallsyms_symbol(mod, addr, size, offset); + if (!sym) + goto out; + if (modname) + strscpy(modname, mod->name, MODULE_NAME_LEN); + if (name) + strscpy(name, sym, KSYM_NAME_LEN); + preempt_enable(); + return 0; + } + } +out: + preempt_enable(); + return -ERANGE; +} + +int module_get_kallsym(unsigned int symnum, unsigned long *value, char *ty= pe, + char *name, char *module_name, int *exported) +{ + struct module *mod; + + preempt_disable(); + list_for_each_entry_rcu(mod, &modules, list) { + struct mod_kallsyms *kallsyms; + + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + continue; + kallsyms =3D rcu_dereference_sched(mod->kallsyms); + if (symnum < kallsyms->num_symtab) { + const Elf_Sym *sym =3D &kallsyms->symtab[symnum]; + + *value =3D kallsyms_symbol_value(sym); + *type =3D kallsyms->typetab[symnum]; + strscpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN); + strscpy(module_name, mod->name, MODULE_NAME_LEN); + *exported =3D is_exported(name, *value, mod); + preempt_enable(); + return 0; + } + symnum -=3D kallsyms->num_symtab; + } + preempt_enable(); + return -ERANGE; +} + +/* Given a module and name of symbol, find and return the symbol's value */ +static unsigned long find_kallsyms_symbol_value(struct module *mod, const = char *name) +{ + unsigned int i; + struct mod_kallsyms *kallsyms =3D rcu_dereference_sched(mod->kallsyms); + + for (i =3D 0; i < kallsyms->num_symtab; i++) { + const Elf_Sym *sym =3D &kallsyms->symtab[i]; + + if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) =3D=3D 0 && + sym->st_shndx !=3D SHN_UNDEF) + return kallsyms_symbol_value(sym); + } + return 0; +} + +/* Look for this name: can be of form module:name. */ +unsigned long module_kallsyms_lookup_name(const char *name) +{ + struct module *mod; + char *colon; + unsigned long ret =3D 0; + + /* Don't lock: we're in enough trouble already. */ + preempt_disable(); + if ((colon =3D strnchr(name, MODULE_NAME_LEN, ':')) !=3D NULL) { + if ((mod =3D find_module_all(name, colon - name, false)) !=3D NULL) + ret =3D find_kallsyms_symbol_value(mod, colon+1); + } else { + list_for_each_entry_rcu(mod, &modules, list) { + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + continue; + if ((ret =3D find_kallsyms_symbol_value(mod, name)) !=3D 0) + break; + } + } + preempt_enable(); + return ret; +} + +#ifdef CONFIG_LIVEPATCH +int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, + struct module *, unsigned long), + void *data) +{ + struct module *mod; + unsigned int i; + int ret =3D 0; + + mutex_lock(&module_mutex); + list_for_each_entry(mod, &modules, list) { + /* We hold module_mutex: no need for rcu_dereference_sched */ + struct mod_kallsyms *kallsyms =3D mod->kallsyms; + + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + continue; + for (i =3D 0; i < kallsyms->num_symtab; i++) { + const Elf_Sym *sym =3D &kallsyms->symtab[i]; + + if (sym->st_shndx =3D=3D SHN_UNDEF) + continue; + + ret =3D fn(data, kallsyms_symbol_name(kallsyms, i), + mod, kallsyms_symbol_value(sym)); + if (ret !=3D 0) + goto out; + } + } +out: + mutex_unlock(&module_mutex); + return ret; +} +#endif /* CONFIG_LIVEPATCH */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 80790d39a6b3..363a54298f7e 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -284,7 +284,7 @@ static bool check_exported_symbol(const struct symsearc= h *syms, return true; } =20 -static unsigned long kernel_symbol_value(const struct kernel_symbol *sym) +unsigned long kernel_symbol_value(const struct kernel_symbol *sym) { #ifdef CONFIG_HAVE_ARCH_PREL32_RELOCATIONS return (unsigned long)offset_to_ptr(&sym->value_offset); @@ -313,7 +313,7 @@ static const char *kernel_symbol_namespace(const struct= kernel_symbol *sym) #endif } =20 -static int cmp_name(const void *name, const void *sym) +int cmp_name(const void *name, const void *sym) { return strcmp(name, kernel_symbol_name(sym)); } @@ -383,7 +383,7 @@ static bool find_symbol(struct find_symbol_arg *fsa) * Search for module by name: must hold module_mutex (or preempt disabled * for read-only access). */ -static struct module *find_module_all(const char *name, size_t len, +struct module *find_module_all(const char *name, size_t len, bool even_unformed) { struct module *mod; @@ -1290,13 +1290,6 @@ resolve_symbol_wait(struct module *mod, return ksym; } =20 -#ifdef CONFIG_KALLSYMS -static inline bool sect_empty(const Elf_Shdr *sect) -{ - return !(sect->sh_flags & SHF_ALLOC) || sect->sh_size =3D=3D 0; -} -#endif - /* * /sys/module/foo/sections stuff * J. Corbet @@ -2021,7 +2014,7 @@ unsigned int __weak arch_mod_section_prepend(struct m= odule *mod, } =20 /* Update size with this section: return offset. */ -static long get_offset(struct module *mod, unsigned int *size, +long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *sechdr, unsigned int section) { long ret; @@ -2223,228 +2216,6 @@ static void free_modinfo(struct module *mod) } } =20 -#ifdef CONFIG_KALLSYMS - -/* Lookup exported symbol in given range of kernel_symbols */ -static const struct kernel_symbol *lookup_exported_symbol(const char *name, - const struct kernel_symbol *start, - const struct kernel_symbol *stop) -{ - return bsearch(name, start, stop - start, - sizeof(struct kernel_symbol), cmp_name); -} - -static int is_exported(const char *name, unsigned long value, - const struct module *mod) -{ - const struct kernel_symbol *ks; - if (!mod) - ks =3D lookup_exported_symbol(name, __start___ksymtab, __stop___ksymtab); - else - ks =3D lookup_exported_symbol(name, mod->syms, mod->syms + mod->num_syms= ); - - return ks !=3D NULL && kernel_symbol_value(ks) =3D=3D value; -} - -/* As per nm */ -static char elf_type(const Elf_Sym *sym, const struct load_info *info) -{ - const Elf_Shdr *sechdrs =3D info->sechdrs; - - if (ELF_ST_BIND(sym->st_info) =3D=3D STB_WEAK) { - if (ELF_ST_TYPE(sym->st_info) =3D=3D STT_OBJECT) - return 'v'; - else - return 'w'; - } - if (sym->st_shndx =3D=3D SHN_UNDEF) - return 'U'; - if (sym->st_shndx =3D=3D SHN_ABS || sym->st_shndx =3D=3D info->index.pcpu) - return 'a'; - if (sym->st_shndx >=3D SHN_LORESERVE) - return '?'; - if (sechdrs[sym->st_shndx].sh_flags & SHF_EXECINSTR) - return 't'; - if (sechdrs[sym->st_shndx].sh_flags & SHF_ALLOC - && sechdrs[sym->st_shndx].sh_type !=3D SHT_NOBITS) { - if (!(sechdrs[sym->st_shndx].sh_flags & SHF_WRITE)) - return 'r'; - else if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) - return 'g'; - else - return 'd'; - } - if (sechdrs[sym->st_shndx].sh_type =3D=3D SHT_NOBITS) { - if (sechdrs[sym->st_shndx].sh_flags & ARCH_SHF_SMALL) - return 's'; - else - return 'b'; - } - if (strstarts(info->secstrings + sechdrs[sym->st_shndx].sh_name, - ".debug")) { - return 'n'; - } - return '?'; -} - -static bool is_core_symbol(const Elf_Sym *src, const Elf_Shdr *sechdrs, - unsigned int shnum, unsigned int pcpundx) -{ - const Elf_Shdr *sec; - - if (src->st_shndx =3D=3D SHN_UNDEF - || src->st_shndx >=3D shnum - || !src->st_name) - return false; - -#ifdef CONFIG_KALLSYMS_ALL - if (src->st_shndx =3D=3D pcpundx) - return true; -#endif - - sec =3D sechdrs + src->st_shndx; - if (!(sec->sh_flags & SHF_ALLOC) -#ifndef CONFIG_KALLSYMS_ALL - || !(sec->sh_flags & SHF_EXECINSTR) -#endif - || (sec->sh_entsize & INIT_OFFSET_MASK)) - return false; - - return true; -} - -/* - * We only allocate and copy the strings needed by the parts of symtab - * we keep. This is simple, but has the effect of making multiple - * copies of duplicates. We could be more sophisticated, see - * linux-kernel thread starting with - * <73defb5e4bca04a6431392cc341112b1@localhost>. - */ -static void layout_symtab(struct module *mod, struct load_info *info) -{ - Elf_Shdr *symsect =3D info->sechdrs + info->index.sym; - Elf_Shdr *strsect =3D info->sechdrs + info->index.str; - const Elf_Sym *src; - unsigned int i, nsrc, ndst, strtab_size =3D 0; - - /* Put symbol section at end of init part of module. */ - symsect->sh_flags |=3D SHF_ALLOC; - symsect->sh_entsize =3D get_offset(mod, &mod->init_layout.size, symsect, - info->index.sym) | INIT_OFFSET_MASK; - pr_debug("\t%s\n", info->secstrings + symsect->sh_name); - - src =3D (void *)info->hdr + symsect->sh_offset; - nsrc =3D symsect->sh_size / sizeof(*src); - - /* Compute total space required for the core symbols' strtab. */ - for (ndst =3D i =3D 0; i < nsrc; i++) { - if (i =3D=3D 0 || is_livepatch_module(mod) || - is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum, - info->index.pcpu)) { - strtab_size +=3D strlen(&info->strtab[src[i].st_name])+1; - ndst++; - } - } - - /* Append room for core symbols at end of core part. */ - info->symoffs =3D ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 1= ); - info->stroffs =3D mod->core_layout.size =3D info->symoffs + ndst * sizeof= (Elf_Sym); - mod->core_layout.size +=3D strtab_size; - info->core_typeoffs =3D mod->core_layout.size; - mod->core_layout.size +=3D ndst * sizeof(char); - mod->core_layout.size =3D debug_align(mod->core_layout.size); - - /* Put string table section at end of init part of module. */ - strsect->sh_flags |=3D SHF_ALLOC; - strsect->sh_entsize =3D get_offset(mod, &mod->init_layout.size, strsect, - info->index.str) | INIT_OFFSET_MASK; - pr_debug("\t%s\n", info->secstrings + strsect->sh_name); - - /* We'll tack temporary mod_kallsyms on the end. */ - mod->init_layout.size =3D ALIGN(mod->init_layout.size, - __alignof__(struct mod_kallsyms)); - info->mod_kallsyms_init_off =3D mod->init_layout.size; - mod->init_layout.size +=3D sizeof(struct mod_kallsyms); - info->init_typeoffs =3D mod->init_layout.size; - mod->init_layout.size +=3D nsrc * sizeof(char); - mod->init_layout.size =3D debug_align(mod->init_layout.size); -} - -/* - * We use the full symtab and strtab which layout_symtab arranged to - * be appended to the init section. Later we switch to the cut-down - * core-only ones. - */ -static void add_kallsyms(struct module *mod, const struct load_info *info) -{ - unsigned int i, ndst; - const Elf_Sym *src; - Elf_Sym *dst; - char *s; - Elf_Shdr *symsec =3D &info->sechdrs[info->index.sym]; - - /* Set up to point into init section. */ - mod->kallsyms =3D mod->init_layout.base + info->mod_kallsyms_init_off; - - mod->kallsyms->symtab =3D (void *)symsec->sh_addr; - mod->kallsyms->num_symtab =3D symsec->sh_size / sizeof(Elf_Sym); - /* Make sure we get permanent strtab: don't use info->strtab. */ - mod->kallsyms->strtab =3D (void *)info->sechdrs[info->index.str].sh_addr; - mod->kallsyms->typetab =3D mod->init_layout.base + info->init_typeoffs; - - /* - * Now populate the cut down core kallsyms for after init - * and set types up while we still have access to sections. - */ - mod->core_kallsyms.symtab =3D dst =3D mod->core_layout.base + info->symof= fs; - mod->core_kallsyms.strtab =3D s =3D mod->core_layout.base + info->stroffs; - mod->core_kallsyms.typetab =3D mod->core_layout.base + info->core_typeoff= s; - src =3D mod->kallsyms->symtab; - for (ndst =3D i =3D 0; i < mod->kallsyms->num_symtab; i++) { - mod->kallsyms->typetab[i] =3D elf_type(src + i, info); - if (i =3D=3D 0 || is_livepatch_module(mod) || - is_core_symbol(src+i, info->sechdrs, info->hdr->e_shnum, - info->index.pcpu)) { - mod->core_kallsyms.typetab[ndst] =3D - mod->kallsyms->typetab[i]; - dst[ndst] =3D src[i]; - dst[ndst++].st_name =3D s - mod->core_kallsyms.strtab; - s +=3D strlcpy(s, &mod->kallsyms->strtab[src[i].st_name], - KSYM_NAME_LEN) + 1; - } - } - mod->core_kallsyms.num_symtab =3D ndst; -} -#else -static inline void layout_symtab(struct module *mod, struct load_info *inf= o) -{ -} - -static void add_kallsyms(struct module *mod, const struct load_info *info) -{ -} -#endif /* CONFIG_KALLSYMS */ - -#if IS_ENABLED(CONFIG_KALLSYMS) && IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) -static void init_build_id(struct module *mod, const struct load_info *info) -{ - const Elf_Shdr *sechdr; - unsigned int i; - - for (i =3D 0; i < info->hdr->e_shnum; i++) { - sechdr =3D &info->sechdrs[i]; - if (!sect_empty(sechdr) && sechdr->sh_type =3D=3D SHT_NOTE && - !build_id_parse_buf((void *)sechdr->sh_addr, mod->build_id, - sechdr->sh_size)) - break; - } -} -#else -static void init_build_id(struct module *mod, const struct load_info *info) -{ -} -#endif - static void dynamic_debug_setup(struct module *mod, struct _ddebug *debug,= unsigned int num) { if (!debug) @@ -3770,285 +3541,6 @@ static inline int within(unsigned long addr, void *= start, unsigned long size) return ((void *)addr >=3D start && (void *)addr < start + size); } =20 -#ifdef CONFIG_KALLSYMS -/* - * This ignores the intensely annoying "mapping symbols" found - * in ARM ELF files: $a, $t and $d. - */ -static inline int is_arm_mapping_symbol(const char *str) -{ - if (str[0] =3D=3D '.' && str[1] =3D=3D 'L') - return true; - return str[0] =3D=3D '$' && strchr("axtd", str[1]) - && (str[2] =3D=3D '\0' || str[2] =3D=3D '.'); -} - -static const char *kallsyms_symbol_name(struct mod_kallsyms *kallsyms, uns= igned int symnum) -{ - return kallsyms->strtab + kallsyms->symtab[symnum].st_name; -} - -/* - * Given a module and address, find the corresponding symbol and return it= s name - * while providing its size and offset if needed. - */ -static const char *find_kallsyms_symbol(struct module *mod, - unsigned long addr, - unsigned long *size, - unsigned long *offset) -{ - unsigned int i, best =3D 0; - unsigned long nextval, bestval; - struct mod_kallsyms *kallsyms =3D rcu_dereference_sched(mod->kallsyms); - - /* At worse, next value is at end of module */ - if (within_module_init(addr, mod)) - nextval =3D (unsigned long)mod->init_layout.base+mod->init_layout.text_s= ize; - else - nextval =3D (unsigned long)mod->core_layout.base+mod->core_layout.text_s= ize; - - bestval =3D kallsyms_symbol_value(&kallsyms->symtab[best]); - - /* - * Scan for closest preceding symbol, and next symbol. (ELF - * starts real symbols at 1). - */ - for (i =3D 1; i < kallsyms->num_symtab; i++) { - const Elf_Sym *sym =3D &kallsyms->symtab[i]; - unsigned long thisval =3D kallsyms_symbol_value(sym); - - if (sym->st_shndx =3D=3D SHN_UNDEF) - continue; - - /* - * We ignore unnamed symbols: they're uninformative - * and inserted at a whim. - */ - if (*kallsyms_symbol_name(kallsyms, i) =3D=3D '\0' - || is_arm_mapping_symbol(kallsyms_symbol_name(kallsyms, i))) - continue; - - if (thisval <=3D addr && thisval > bestval) { - best =3D i; - bestval =3D thisval; - } - if (thisval > addr && thisval < nextval) - nextval =3D thisval; - } - - if (!best) - return NULL; - - if (size) - *size =3D nextval - bestval; - if (offset) - *offset =3D addr - bestval; - - return kallsyms_symbol_name(kallsyms, best); -} - -void * __weak dereference_module_function_descriptor(struct module *mod, - void *ptr) -{ - return ptr; -} - -/* - * For kallsyms to ask for address resolution. NULL means not found. Car= eful - * not to lock to avoid deadlock on oopses, simply disable preemption. - */ -const char *module_address_lookup(unsigned long addr, - unsigned long *size, - unsigned long *offset, - char **modname, - const unsigned char **modbuildid, - char *namebuf) -{ - const char *ret =3D NULL; - struct module *mod; - - preempt_disable(); - mod =3D __module_address(addr); - if (mod) { - if (modname) - *modname =3D mod->name; - if (modbuildid) { -#if IS_ENABLED(CONFIG_STACKTRACE_BUILD_ID) - *modbuildid =3D mod->build_id; -#else - *modbuildid =3D NULL; -#endif - } - - ret =3D find_kallsyms_symbol(mod, addr, size, offset); - } - /* Make a copy in here where it's safe */ - if (ret) { - strncpy(namebuf, ret, KSYM_NAME_LEN - 1); - ret =3D namebuf; - } - preempt_enable(); - - return ret; -} - -int lookup_module_symbol_name(unsigned long addr, char *symname) -{ - struct module *mod; - - preempt_disable(); - list_for_each_entry_rcu(mod, &modules, list) { - if (mod->state =3D=3D MODULE_STATE_UNFORMED) - continue; - if (within_module(addr, mod)) { - const char *sym; - - sym =3D find_kallsyms_symbol(mod, addr, NULL, NULL); - if (!sym) - goto out; - - strlcpy(symname, sym, KSYM_NAME_LEN); - preempt_enable(); - return 0; - } - } -out: - preempt_enable(); - return -ERANGE; -} - -int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, - unsigned long *offset, char *modname, char *name) -{ - struct module *mod; - - preempt_disable(); - list_for_each_entry_rcu(mod, &modules, list) { - if (mod->state =3D=3D MODULE_STATE_UNFORMED) - continue; - if (within_module(addr, mod)) { - const char *sym; - - sym =3D find_kallsyms_symbol(mod, addr, size, offset); - if (!sym) - goto out; - if (modname) - strlcpy(modname, mod->name, MODULE_NAME_LEN); - if (name) - strlcpy(name, sym, KSYM_NAME_LEN); - preempt_enable(); - return 0; - } - } -out: - preempt_enable(); - return -ERANGE; -} - -int module_get_kallsym(unsigned int symnum, unsigned long *value, char *ty= pe, - char *name, char *module_name, int *exported) -{ - struct module *mod; - - preempt_disable(); - list_for_each_entry_rcu(mod, &modules, list) { - struct mod_kallsyms *kallsyms; - - if (mod->state =3D=3D MODULE_STATE_UNFORMED) - continue; - kallsyms =3D rcu_dereference_sched(mod->kallsyms); - if (symnum < kallsyms->num_symtab) { - const Elf_Sym *sym =3D &kallsyms->symtab[symnum]; - - *value =3D kallsyms_symbol_value(sym); - *type =3D kallsyms->typetab[symnum]; - strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN); - strlcpy(module_name, mod->name, MODULE_NAME_LEN); - *exported =3D is_exported(name, *value, mod); - preempt_enable(); - return 0; - } - symnum -=3D kallsyms->num_symtab; - } - preempt_enable(); - return -ERANGE; -} - -/* Given a module and name of symbol, find and return the symbol's value */ -static unsigned long find_kallsyms_symbol_value(struct module *mod, const = char *name) -{ - unsigned int i; - struct mod_kallsyms *kallsyms =3D rcu_dereference_sched(mod->kallsyms); - - for (i =3D 0; i < kallsyms->num_symtab; i++) { - const Elf_Sym *sym =3D &kallsyms->symtab[i]; - - if (strcmp(name, kallsyms_symbol_name(kallsyms, i)) =3D=3D 0 && - sym->st_shndx !=3D SHN_UNDEF) - return kallsyms_symbol_value(sym); - } - return 0; -} - -/* Look for this name: can be of form module:name. */ -unsigned long module_kallsyms_lookup_name(const char *name) -{ - struct module *mod; - char *colon; - unsigned long ret =3D 0; - - /* Don't lock: we're in enough trouble already. */ - preempt_disable(); - if ((colon =3D strnchr(name, MODULE_NAME_LEN, ':')) !=3D NULL) { - if ((mod =3D find_module_all(name, colon - name, false)) !=3D NULL) - ret =3D find_kallsyms_symbol_value(mod, colon+1); - } else { - list_for_each_entry_rcu(mod, &modules, list) { - if (mod->state =3D=3D MODULE_STATE_UNFORMED) - continue; - if ((ret =3D find_kallsyms_symbol_value(mod, name)) !=3D 0) - break; - } - } - preempt_enable(); - return ret; -} - -#ifdef CONFIG_LIVEPATCH -int module_kallsyms_on_each_symbol(int (*fn)(void *, const char *, - struct module *, unsigned long), - void *data) -{ - struct module *mod; - unsigned int i; - int ret =3D 0; - - mutex_lock(&module_mutex); - list_for_each_entry(mod, &modules, list) { - /* We hold module_mutex: no need for rcu_dereference_sched */ - struct mod_kallsyms *kallsyms =3D mod->kallsyms; - - if (mod->state =3D=3D MODULE_STATE_UNFORMED) - continue; - for (i =3D 0; i < kallsyms->num_symtab; i++) { - const Elf_Sym *sym =3D &kallsyms->symtab[i]; - - if (sym->st_shndx =3D=3D SHN_UNDEF) - continue; - - ret =3D fn(data, kallsyms_symbol_name(kallsyms, i), - mod, kallsyms_symbol_value(sym)); - if (ret !=3D 0) - goto out; - } - } -out: - mutex_unlock(&module_mutex); - return ret; -} -#endif /* CONFIG_LIVEPATCH */ -#endif /* CONFIG_KALLSYMS */ - static void cfi_init(struct module *mod) { #ifdef CONFIG_CFI_CLANG --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 CFEAEC433F5 for ; Sun, 30 Jan 2022 21:33:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356942AbiA3Vdr (ORCPT ); Sun, 30 Jan 2022 16:33:47 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124]:26313 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356616AbiA3Vcd (ORCPT ); Sun, 30 Jan 2022 16:32:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578353; 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=al/jYJxi8UyrW4mEBmLd1lyNjLUXPonhqIBGpMy93Lw=; b=ZHgSOhdfic7wHsAQ2xR+1KSFavYbZV6igYvbZYdjn0p5mo7AVdQ+fC9N1Xl3BGhz3/CP9k pnssGgvZT1a1M8qYyDzzNfVA7tbK9XTe2pZhruZbY8YdjUmLDF0J1drsryhw1ftxSlAnc1 PsF7hD3xmuCjzwhJAy+fMx6C8qzuavk= 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-611-f8_ORzaZPB6dunyRSK2FFw-1; Sun, 30 Jan 2022 16:32:31 -0500 X-MC-Unique: f8_ORzaZPB6dunyRSK2FFw-1 Received: by mail-wm1-f71.google.com with SMTP id l16-20020a1c7910000000b0034e4206ecb7so5135391wme.7 for ; Sun, 30 Jan 2022 13:32:31 -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=al/jYJxi8UyrW4mEBmLd1lyNjLUXPonhqIBGpMy93Lw=; b=joP1IIV2q0LyfdgXWmSu1ZRhE48VnjSjN/vXHbOSaM7NYgfdBhr7/2oPy0tStW3PmQ Mz9rdNFJTvE3TggeJV7MqEyUezZ8R/RLWGbIyTWqQ3RiYZoNvgUIfsw84z/tENiUdPLC xEP+T7PAStIHylSRkoqI8kZ0PI/rqkQlleD2T8pD7RkQZXXMcnY6JpUgvlv67kAZ5+kU jt+F5sLMA8N/PorwzHCh4Tl7hOvsZS2726GMmpKstVxCCnKc1VmfVbYD0yKM53vArF15 /yXJXn2tO2RacusYTxr5p2lN1itPvNl7ua73KOZm3dxOt8+dPTkZ9fD0rflWfxfljs/i 7SLw== X-Gm-Message-State: AOAM531OM897SP7SjvW5dQn+7NxWBGg5TU7kswBOj3ew4Xa+Y+7zjdgk sZMIYvUzTzQi7v0qGjh/n+SnmFy3sAd0QyIb6GPkC8obLENY38n5238QCb0rK8rVIpzYQKz5QJD ekFwzfytUAVBUR/2y9y2DV14= X-Received: by 2002:a5d:4f08:: with SMTP id c8mr15236682wru.710.1643578350370; Sun, 30 Jan 2022 13:32:30 -0800 (PST) X-Google-Smtp-Source: ABdhPJwU0FmzRtmHVNoE9ddKC8OpTlksQWrPP2QV0qL/lMtoozpTHWYbo2UIdJxcVd9J5gyWDMSz8w== X-Received: by 2002:a5d:4f08:: with SMTP id c8mr15236666wru.710.1643578350053; Sun, 30 Jan 2022 13:32:30 -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 5sm11630584wrb.113.2022.01.30.13.32.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:29 -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 10/13] module: Move procfs support into a separate file Date: Sun, 30 Jan 2022 21:32:11 +0000 Message-Id: <20220130213214.1042497-11-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates code that allows one to generate a list of loaded/or linked modules via /proc when procfs support is enabled into kernel/module/procfs.c. Signed-off-by: Aaron Tomlin --- kernel/module/Makefile | 1 + kernel/module/internal.h | 1 + kernel/module/main.c | 131 +----------------------------------- kernel/module/procfs.c | 142 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+), 130 deletions(-) create mode 100644 kernel/module/procfs.c diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 906f83e1931b..812f11ad090e 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -14,4 +14,5 @@ obj-$(CONFIG_ARCH_HAS_STRICT_MODULE_RWX) +=3D arch_strict= _rwx.o obj-$(CONFIG_STRICT_MODULE_RWX) +=3D strict_rwx.o obj-$(CONFIG_DEBUG_KMEMLEAK) +=3D debug_kmemleak.o obj-$(CONFIG_KALLSYMS) +=3D kallsyms.o +obj-$(CONFIG_PROC_FS) +=3D procfs.o endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 866fce7c1a33..09484a9382bc 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -62,6 +62,7 @@ extern unsigned long kernel_symbol_value(const struct ker= nel_symbol *sym); extern int cmp_name(const void *name, const void *sym); extern long get_offset(struct module *mod, unsigned int *size, Elf_Shdr *s= echdr, unsigned int section); +extern char *module_flags(struct module *mod, char *buf); =20 #ifdef CONFIG_LIVEPATCH extern int copy_module_elf(struct module *mod, struct load_info *info); diff --git a/kernel/module/main.c b/kernel/module/main.c index 363a54298f7e..1c20393ab76d 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -21,7 +21,6 @@ #include #include #include -#include #include #include #include @@ -810,31 +809,6 @@ SYSCALL_DEFINE2(delete_module, const char __user *, na= me_user, return ret; } =20 -static inline void print_unload_info(struct seq_file *m, struct module *mo= d) -{ - struct module_use *use; - int printed_something =3D 0; - - seq_printf(m, " %i ", module_refcount(mod)); - - /* - * Always include a trailing , so userspace can differentiate - * between this and the old multi-field proc format. - */ - list_for_each_entry(use, &mod->source_list, source_list) { - printed_something =3D 1; - seq_printf(m, "%s,", use->source->name); - } - - if (mod->init !=3D NULL && mod->exit =3D=3D NULL) { - printed_something =3D 1; - seq_puts(m, "[permanent],"); - } - - if (!printed_something) - seq_puts(m, "-"); -} - void __symbol_put(const char *symbol) { struct find_symbol_arg fsa =3D { @@ -924,12 +898,6 @@ void module_put(struct module *module) EXPORT_SYMBOL(module_put); =20 #else /* !CONFIG_MODULE_UNLOAD */ -static inline void print_unload_info(struct seq_file *m, struct module *mo= d) -{ - /* We don't know the usage count, or what modules are using. */ - seq_puts(m, " - -"); -} - static inline void module_unload_free(struct module *mod) { } @@ -3576,7 +3544,7 @@ static void cfi_cleanup(struct module *mod) } =20 /* Keep in sync with MODULE_FLAGS_BUF_SIZE !!! */ -static char *module_flags(struct module *mod, char *buf) +char *module_flags(struct module *mod, char *buf) { int bx =3D 0; =20 @@ -3599,103 +3567,6 @@ static char *module_flags(struct module *mod, char = *buf) return buf; } =20 -#ifdef CONFIG_PROC_FS -/* Called by the /proc file system to return a list of modules. */ -static void *m_start(struct seq_file *m, loff_t *pos) -{ - mutex_lock(&module_mutex); - return seq_list_start(&modules, *pos); -} - -static void *m_next(struct seq_file *m, void *p, loff_t *pos) -{ - return seq_list_next(p, &modules, pos); -} - -static void m_stop(struct seq_file *m, void *p) -{ - mutex_unlock(&module_mutex); -} - -static int m_show(struct seq_file *m, void *p) -{ - struct module *mod =3D list_entry(p, struct module, list); - char buf[MODULE_FLAGS_BUF_SIZE]; - void *value; - - /* We always ignore unformed modules. */ - if (mod->state =3D=3D MODULE_STATE_UNFORMED) - return 0; - - seq_printf(m, "%s %u", - mod->name, mod->init_layout.size + mod->core_layout.size); - print_unload_info(m, mod); - - /* Informative for users. */ - seq_printf(m, " %s", - mod->state =3D=3D MODULE_STATE_GOING ? "Unloading" : - mod->state =3D=3D MODULE_STATE_COMING ? "Loading" : - "Live"); - /* Used by oprofile and other similar tools. */ - value =3D m->private ? NULL : mod->core_layout.base; - seq_printf(m, " 0x%px", value); - - /* Taints info */ - if (mod->taints) - seq_printf(m, " %s", module_flags(mod, buf)); - - seq_puts(m, "\n"); - return 0; -} - -/* - * Format: modulename size refcount deps address - * - * Where refcount is a number or -, and deps is a comma-separated list - * of depends or -. - */ -static const struct seq_operations modules_op =3D { - .start =3D m_start, - .next =3D m_next, - .stop =3D m_stop, - .show =3D m_show -}; - -/* - * This also sets the "private" pointer to non-NULL if the - * kernel pointers should be hidden (so you can just test - * "m->private" to see if you should keep the values private). - * - * We use the same logic as for /proc/kallsyms. - */ -static int modules_open(struct inode *inode, struct file *file) -{ - int err =3D seq_open(file, &modules_op); - - if (!err) { - struct seq_file *m =3D file->private_data; - m->private =3D kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul; - } - - return err; -} - -static const struct proc_ops modules_proc_ops =3D { - .proc_flags =3D PROC_ENTRY_PERMANENT, - .proc_open =3D modules_open, - .proc_read =3D seq_read, - .proc_lseek =3D seq_lseek, - .proc_release =3D seq_release, -}; - -static int __init proc_modules_init(void) -{ - proc_create("modules", 0, NULL, &modules_proc_ops); - return 0; -} -module_init(proc_modules_init); -#endif - /* Given an address, look for it in the module exception tables. */ const struct exception_table_entry *search_module_extables(unsigned long a= ddr) { diff --git a/kernel/module/procfs.c b/kernel/module/procfs.c new file mode 100644 index 000000000000..d706a798b52e --- /dev/null +++ b/kernel/module/procfs.c @@ -0,0 +1,142 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module proc support + * + * Copyright (C) 2008 Alexey Dobriyan + */ + +#include +#include +#include +#include +#include +#include "internal.h" + +#ifdef CONFIG_MODULE_UNLOAD +static inline void print_unload_info(struct seq_file *m, struct module *mo= d) +{ + struct module_use *use; + int printed_something =3D 0; + + seq_printf(m, " %i ", module_refcount(mod)); + + /* + * Always include a trailing , so userspace can differentiate + * between this and the old multi-field proc format. + */ + list_for_each_entry(use, &mod->source_list, source_list) { + printed_something =3D 1; + seq_printf(m, "%s,", use->source->name); + } + + if (mod->init !=3D NULL && mod->exit =3D=3D NULL) { + printed_something =3D 1; + seq_puts(m, "[permanent],"); + } + + if (!printed_something) + seq_puts(m, "-"); +} +#else /* !CONFIG_MODULE_UNLOAD */ +static inline void print_unload_info(struct seq_file *m, struct module *mo= d) +{ + /* We don't know the usage count, or what modules are using. */ + seq_puts(m, " - -"); +} +#endif /* CONFIG_MODULE_UNLOAD */ + +/* Called by the /proc file system to return a list of modules. */ +static void *m_start(struct seq_file *m, loff_t *pos) +{ + mutex_lock(&module_mutex); + return seq_list_start(&modules, *pos); +} + +static void *m_next(struct seq_file *m, void *p, loff_t *pos) +{ + return seq_list_next(p, &modules, pos); +} + +static void m_stop(struct seq_file *m, void *p) +{ + mutex_unlock(&module_mutex); +} + +static int m_show(struct seq_file *m, void *p) +{ + struct module *mod =3D list_entry(p, struct module, list); + char buf[MODULE_FLAGS_BUF_SIZE]; + void *value; + + /* We always ignore unformed modules. */ + if (mod->state =3D=3D MODULE_STATE_UNFORMED) + return 0; + + seq_printf(m, "%s %u", + mod->name, mod->init_layout.size + mod->core_layout.size); + print_unload_info(m, mod); + + /* Informative for users. */ + seq_printf(m, " %s", + mod->state =3D=3D MODULE_STATE_GOING ? "Unloading" : + mod->state =3D=3D MODULE_STATE_COMING ? "Loading" : + "Live"); + /* Used by oprofile and other similar tools. */ + value =3D m->private ? NULL : mod->core_layout.base; + seq_printf(m, " 0x%px", value); + + /* Taints info */ + if (mod->taints) + seq_printf(m, " %s", module_flags(mod, buf)); + + seq_puts(m, "\n"); + return 0; +} + +/* + * Format: modulename size refcount deps address + * + * Where refcount is a number or -, and deps is a comma-separated list + * of depends or -. + */ +static const struct seq_operations modules_op =3D { + .start =3D m_start, + .next =3D m_next, + .stop =3D m_stop, + .show =3D m_show +}; + +/* + * This also sets the "private" pointer to non-NULL if the + * kernel pointers should be hidden (so you can just test + * "m->private" to see if you should keep the values private). + * + * We use the same logic as for /proc/kallsyms. + */ +static int modules_open(struct inode *inode, struct file *file) +{ + int err =3D seq_open(file, &modules_op); + + if (!err) { + struct seq_file *m =3D file->private_data; + + m->private =3D kallsyms_show_value(file->f_cred) ? NULL : (void *)8ul; + } + + return err; +} + +static const struct proc_ops modules_proc_ops =3D { + .proc_flags =3D PROC_ENTRY_PERMANENT, + .proc_open =3D modules_open, + .proc_read =3D seq_read, + .proc_lseek =3D seq_lseek, + .proc_release =3D seq_release, +}; + +static int __init proc_modules_init(void) +{ + proc_create("modules", 0, NULL, &modules_proc_ops); + return 0; +} +module_init(proc_modules_init); --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 9E847C433F5 for ; Sun, 30 Jan 2022 21:33:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356787AbiA3Vdy (ORCPT ); Sun, 30 Jan 2022 16:33:54 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:54786 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356622AbiA3Vcf (ORCPT ); Sun, 30 Jan 2022 16:32:35 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578354; 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=WlileRWlr+EYMrkI/iFd5LtO7tED0dYLFSPCgY4cKLI=; b=YzhK54cpn08U+FE+b7P9M/1czz8ebDQDc3gqK6ddDl7ipXaN1X/kEiHkhzENxqEd5RnaiX 5ZInvJPQWtzWCoafZPzZNAPvneRLLExLV+xXHyNBKIB5krME1w50y9UNKyS9nA9jJjGHBF f0bOvkeR2+h9C/9KYW4phXtxggL7XtI= 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-606-r2vqSii0MOGtO-iSA7M1Ew-1; Sun, 30 Jan 2022 16:32:33 -0500 X-MC-Unique: r2vqSii0MOGtO-iSA7M1Ew-1 Received: by mail-wm1-f71.google.com with SMTP id c16-20020a1c9a10000000b0034dd409329eso5148477wme.3 for ; Sun, 30 Jan 2022 13:32:33 -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=WlileRWlr+EYMrkI/iFd5LtO7tED0dYLFSPCgY4cKLI=; b=baDvxhSE+BHMSuXnW/nHXcWpFvVoACeaopH6QWJHIiWAZEATO7VtdCw0NzwB2WoF1c t0yTLxiUOglHMKNvkcGyPglJ3NsdAP+4yH7rtwDi4TiDmqkJJDF+42K96LGcDwPEnxXK V5c1zm5iB/HzUdWWp9nys3iAe7+lHF0/xgJJgv2WKPr9qlDU39/Ar1qOCNvK0sr9uPEZ GGnnDCJVoLx/EYOtAf8MTgMNch3Qx4fy9cU+czpGEKChy8xgIqgplUvPKhAiMI3G1+6U eXshCPepYzzthv85IO3arsXfGoInfgtsM72A7EYU5LA3hneX7f+/ioH6PwYlcyg1aFKN 7ENA== X-Gm-Message-State: AOAM531sGVvZSHpdWi8NBGlkOA+lXt8W698t+IEC46waeLm9ANUAKLuX fNjAgUNlXanwu8y6S3iG3RfF21UCOtMavlyo4KHeGybn1eWd+SH8+sZm57Jc2OAIfBu8j9+LSca fvvrxto8qAOe980Z+89qGixE= X-Received: by 2002:a05:600c:1d8b:: with SMTP id p11mr24673916wms.115.1643578351765; Sun, 30 Jan 2022 13:32:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJxxtJcKZjdoAI2dovcmJoiQ0LfjYNq+4ldLU1Lj8Htg2ytCzYnKbQnFJm4bPgbRlHvKqR4cPA== X-Received: by 2002:a05:600c:1d8b:: with SMTP id p11mr24673901wms.115.1643578351467; Sun, 30 Jan 2022 13:32:31 -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 n10sm12385404wrf.96.2022.01.30.13.32.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:31 -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 11/13] module: Move sysfs support into a separate file Date: Sun, 30 Jan 2022 21:32:12 +0000 Message-Id: <20220130213214.1042497-12-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates module sysfs support out of core code into kernel/module/sysfs.c. In addition simple code refactoring to make this possible. Signed-off-by: Aaron Tomlin --- kernel/module/Makefile | 1 + kernel/module/internal.h | 24 ++ kernel/module/main.c | 458 +-------------------------------------- kernel/module/sysfs.c | 425 ++++++++++++++++++++++++++++++++++++ 4 files changed, 453 insertions(+), 455 deletions(-) create mode 100644 kernel/module/sysfs.c diff --git a/kernel/module/Makefile b/kernel/module/Makefile index 812f11ad090e..c8982b47b3a2 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -15,4 +15,5 @@ obj-$(CONFIG_STRICT_MODULE_RWX) +=3D strict_rwx.o obj-$(CONFIG_DEBUG_KMEMLEAK) +=3D debug_kmemleak.o obj-$(CONFIG_KALLSYMS) +=3D kallsyms.o obj-$(CONFIG_PROC_FS) +=3D procfs.o +obj-$(CONFIG_SYSFS) +=3D sysfs.o endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index 09484a9382bc..cf3baca4ebea 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -23,6 +23,9 @@ extern struct mutex module_mutex; extern struct list_head modules; =20 +extern struct module_attribute *modinfo_attrs[]; +extern size_t modinfo_attrs_count; + /* Provided by the linker */ extern const struct kernel_symbol __start___ksymtab[]; extern const struct kernel_symbol __stop___ksymtab[]; @@ -159,3 +162,24 @@ static inline char *find_kallsyms_symbol(struct module= *mod, unsigned long addr, return NULL; } #endif /* CONFIG_KALLSYMS */ + +#ifdef CONFIG_SYSFS +extern int mod_sysfs_setup(struct module *mod, const struct load_info *inf= o, + struct kernel_param *kparam, unsigned int num_params); +extern void mod_sysfs_fini(struct module *mod); +extern void module_remove_modinfo_attrs(struct module *mod, int end); +extern void del_usage_links(struct module *mod); +extern void init_param_lock(struct module *mod); +#else /* !CONFIG_SYSFS */ +static int mod_sysfs_setup(struct module *mod, + const struct load_info *info, + struct kernel_param *kparam, + unsigned int num_params) +{ + return 0; +} +static inline void mod_sysfs_fini(struct module *mod) { } +static inline void module_remove_modinfo_attrs(struct module *mod, int end= ) { } +static inline void del_usage_links(struct module *mod) { } +static inline void init_param_lock(struct module *mod) { } +#endif /* CONFIG_SYSFS */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 1c20393ab76d..1d16faea82b3 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -13,9 +13,7 @@ #include #include #include -#include #include -#include #include #include #include @@ -994,7 +992,7 @@ static ssize_t show_taint(struct module_attribute *matt= r, static struct module_attribute modinfo_taint =3D __ATTR(taint, 0444, show_taint, NULL); =20 -static struct module_attribute *modinfo_attrs[] =3D { +struct module_attribute *modinfo_attrs[] =3D { &module_uevent, &modinfo_version, &modinfo_srcversion, @@ -1008,6 +1006,8 @@ static struct module_attribute *modinfo_attrs[] =3D { NULL, }; =20 +size_t modinfo_attrs_count =3D ARRAY_SIZE(modinfo_attrs); + static const char vermagic[] =3D VERMAGIC_STRING; =20 static int try_to_force_load(struct module *mod, const char *reason) @@ -1258,458 +1258,6 @@ resolve_symbol_wait(struct module *mod, return ksym; } =20 -/* - * /sys/module/foo/sections stuff - * J. Corbet - */ -#ifdef CONFIG_SYSFS - -#ifdef CONFIG_KALLSYMS -struct module_sect_attr { - struct bin_attribute battr; - unsigned long address; -}; - -struct module_sect_attrs { - struct attribute_group grp; - unsigned int nsections; - struct module_sect_attr attrs[]; -}; - -static ssize_t module_sect_read(struct file *file, struct kobject *kobj, - struct bin_attribute *battr, - char *buf, loff_t pos, size_t count) -{ - struct module_sect_attr *sattr =3D - container_of(battr, struct module_sect_attr, battr); - char bounce[MODULE_SECT_READ_SIZE + 1]; - size_t wrote; - - if (pos !=3D 0) - return -EINVAL; - - /* - * Since we're a binary read handler, we must account for the - * trailing NUL byte that sprintf will write: if "buf" is - * too small to hold the NUL, or the NUL is exactly the last - * byte, the read will look like it got truncated by one byte. - * Since there is no way to ask sprintf nicely to not write - * the NUL, we have to use a bounce buffer. - */ - wrote =3D scnprintf(bounce, sizeof(bounce), "0x%px\n", - kallsyms_show_value(file->f_cred) - ? (void *)sattr->address : NULL); - count =3D min(count, wrote); - memcpy(buf, bounce, count); - - return count; -} - -static void free_sect_attrs(struct module_sect_attrs *sect_attrs) -{ - unsigned int section; - - for (section =3D 0; section < sect_attrs->nsections; section++) - kfree(sect_attrs->attrs[section].battr.attr.name); - kfree(sect_attrs); -} - -static void add_sect_attrs(struct module *mod, const struct load_info *inf= o) -{ - unsigned int nloaded =3D 0, i, size[2]; - struct module_sect_attrs *sect_attrs; - struct module_sect_attr *sattr; - struct bin_attribute **gattr; - - /* Count loaded sections and allocate structures */ - for (i =3D 0; i < info->hdr->e_shnum; i++) - if (!sect_empty(&info->sechdrs[i])) - nloaded++; - size[0] =3D ALIGN(struct_size(sect_attrs, attrs, nloaded), - sizeof(sect_attrs->grp.bin_attrs[0])); - size[1] =3D (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]); - sect_attrs =3D kzalloc(size[0] + size[1], GFP_KERNEL); - if (sect_attrs =3D=3D NULL) - return; - - /* Setup section attributes. */ - sect_attrs->grp.name =3D "sections"; - sect_attrs->grp.bin_attrs =3D (void *)sect_attrs + size[0]; - - sect_attrs->nsections =3D 0; - sattr =3D §_attrs->attrs[0]; - gattr =3D §_attrs->grp.bin_attrs[0]; - for (i =3D 0; i < info->hdr->e_shnum; i++) { - Elf_Shdr *sec =3D &info->sechdrs[i]; - if (sect_empty(sec)) - continue; - sysfs_bin_attr_init(&sattr->battr); - sattr->address =3D sec->sh_addr; - sattr->battr.attr.name =3D - kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL); - if (sattr->battr.attr.name =3D=3D NULL) - goto out; - sect_attrs->nsections++; - sattr->battr.read =3D module_sect_read; - sattr->battr.size =3D MODULE_SECT_READ_SIZE; - sattr->battr.attr.mode =3D 0400; - *(gattr++) =3D &(sattr++)->battr; - } - *gattr =3D NULL; - - if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) - goto out; - - mod->sect_attrs =3D sect_attrs; - return; - out: - free_sect_attrs(sect_attrs); -} - -static void remove_sect_attrs(struct module *mod) -{ - if (mod->sect_attrs) { - sysfs_remove_group(&mod->mkobj.kobj, - &mod->sect_attrs->grp); - /* - * We are positive that no one is using any sect attrs - * at this point. Deallocate immediately. - */ - free_sect_attrs(mod->sect_attrs); - mod->sect_attrs =3D NULL; - } -} - -/* - * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. - */ - -struct module_notes_attrs { - struct kobject *dir; - unsigned int notes; - struct bin_attribute attrs[]; -}; - -static ssize_t module_notes_read(struct file *filp, struct kobject *kobj, - struct bin_attribute *bin_attr, - char *buf, loff_t pos, size_t count) -{ - /* - * The caller checked the pos and count against our size. - */ - memcpy(buf, bin_attr->private + pos, count); - return count; -} - -static void free_notes_attrs(struct module_notes_attrs *notes_attrs, - unsigned int i) -{ - if (notes_attrs->dir) { - while (i-- > 0) - sysfs_remove_bin_file(notes_attrs->dir, - ¬es_attrs->attrs[i]); - kobject_put(notes_attrs->dir); - } - kfree(notes_attrs); -} - -static void add_notes_attrs(struct module *mod, const struct load_info *in= fo) -{ - unsigned int notes, loaded, i; - struct module_notes_attrs *notes_attrs; - struct bin_attribute *nattr; - - /* failed to create section attributes, so can't create notes */ - if (!mod->sect_attrs) - return; - - /* Count notes sections and allocate structures. */ - notes =3D 0; - for (i =3D 0; i < info->hdr->e_shnum; i++) - if (!sect_empty(&info->sechdrs[i]) && - (info->sechdrs[i].sh_type =3D=3D SHT_NOTE)) - ++notes; - - if (notes =3D=3D 0) - return; - - notes_attrs =3D kzalloc(struct_size(notes_attrs, attrs, notes), - GFP_KERNEL); - if (notes_attrs =3D=3D NULL) - return; - - notes_attrs->notes =3D notes; - nattr =3D ¬es_attrs->attrs[0]; - for (loaded =3D i =3D 0; i < info->hdr->e_shnum; ++i) { - if (sect_empty(&info->sechdrs[i])) - continue; - if (info->sechdrs[i].sh_type =3D=3D SHT_NOTE) { - sysfs_bin_attr_init(nattr); - nattr->attr.name =3D mod->sect_attrs->attrs[loaded].battr.attr.name; - nattr->attr.mode =3D S_IRUGO; - nattr->size =3D info->sechdrs[i].sh_size; - nattr->private =3D (void *) info->sechdrs[i].sh_addr; - nattr->read =3D module_notes_read; - ++nattr; - } - ++loaded; - } - - notes_attrs->dir =3D kobject_create_and_add("notes", &mod->mkobj.kobj); - if (!notes_attrs->dir) - goto out; - - for (i =3D 0; i < notes; ++i) - if (sysfs_create_bin_file(notes_attrs->dir, - ¬es_attrs->attrs[i])) - goto out; - - mod->notes_attrs =3D notes_attrs; - return; - - out: - free_notes_attrs(notes_attrs, i); -} - -static void remove_notes_attrs(struct module *mod) -{ - if (mod->notes_attrs) - free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); -} - -#else - -static inline void add_sect_attrs(struct module *mod, - const struct load_info *info) -{ -} - -static inline void remove_sect_attrs(struct module *mod) -{ -} - -static inline void add_notes_attrs(struct module *mod, - const struct load_info *info) -{ -} - -static inline void remove_notes_attrs(struct module *mod) -{ -} -#endif /* CONFIG_KALLSYMS */ - -static void del_usage_links(struct module *mod) -{ -#ifdef CONFIG_MODULE_UNLOAD - struct module_use *use; - - mutex_lock(&module_mutex); - list_for_each_entry(use, &mod->target_list, target_list) - sysfs_remove_link(use->target->holders_dir, mod->name); - mutex_unlock(&module_mutex); -#endif -} - -static int add_usage_links(struct module *mod) -{ - int ret =3D 0; -#ifdef CONFIG_MODULE_UNLOAD - struct module_use *use; - - mutex_lock(&module_mutex); - list_for_each_entry(use, &mod->target_list, target_list) { - ret =3D sysfs_create_link(use->target->holders_dir, - &mod->mkobj.kobj, mod->name); - if (ret) - break; - } - mutex_unlock(&module_mutex); - if (ret) - del_usage_links(mod); -#endif - return ret; -} - -static void module_remove_modinfo_attrs(struct module *mod, int end); - -static int module_add_modinfo_attrs(struct module *mod) -{ - struct module_attribute *attr; - struct module_attribute *temp_attr; - int error =3D 0; - int i; - - mod->modinfo_attrs =3D kzalloc((sizeof(struct module_attribute) * - (ARRAY_SIZE(modinfo_attrs) + 1)), - GFP_KERNEL); - if (!mod->modinfo_attrs) - return -ENOMEM; - - temp_attr =3D mod->modinfo_attrs; - for (i =3D 0; (attr =3D modinfo_attrs[i]); i++) { - if (!attr->test || attr->test(mod)) { - memcpy(temp_attr, attr, sizeof(*temp_attr)); - sysfs_attr_init(&temp_attr->attr); - error =3D sysfs_create_file(&mod->mkobj.kobj, - &temp_attr->attr); - if (error) - goto error_out; - ++temp_attr; - } - } - - return 0; - -error_out: - if (i > 0) - module_remove_modinfo_attrs(mod, --i); - else - kfree(mod->modinfo_attrs); - return error; -} - -static void module_remove_modinfo_attrs(struct module *mod, int end) -{ - struct module_attribute *attr; - int i; - - for (i =3D 0; (attr =3D &mod->modinfo_attrs[i]); i++) { - if (end >=3D 0 && i > end) - break; - /* pick a field to test for end of list */ - if (!attr->attr.name) - break; - sysfs_remove_file(&mod->mkobj.kobj, &attr->attr); - if (attr->free) - attr->free(mod); - } - kfree(mod->modinfo_attrs); -} - -static void mod_kobject_put(struct module *mod) -{ - DECLARE_COMPLETION_ONSTACK(c); - mod->mkobj.kobj_completion =3D &c; - kobject_put(&mod->mkobj.kobj); - wait_for_completion(&c); -} - -static int mod_sysfs_init(struct module *mod) -{ - int err; - struct kobject *kobj; - - if (!module_sysfs_initialized) { - pr_err("%s: module sysfs not initialized\n", mod->name); - err =3D -EINVAL; - goto out; - } - - kobj =3D kset_find_obj(module_kset, mod->name); - if (kobj) { - pr_err("%s: module is already loaded\n", mod->name); - kobject_put(kobj); - err =3D -EINVAL; - goto out; - } - - mod->mkobj.mod =3D mod; - - memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); - mod->mkobj.kobj.kset =3D module_kset; - err =3D kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, - "%s", mod->name); - if (err) - mod_kobject_put(mod); - -out: - return err; -} - -static int mod_sysfs_setup(struct module *mod, - const struct load_info *info, - struct kernel_param *kparam, - unsigned int num_params) -{ - int err; - - err =3D mod_sysfs_init(mod); - if (err) - goto out; - - mod->holders_dir =3D kobject_create_and_add("holders", &mod->mkobj.kobj); - if (!mod->holders_dir) { - err =3D -ENOMEM; - goto out_unreg; - } - - err =3D module_param_sysfs_setup(mod, kparam, num_params); - if (err) - goto out_unreg_holders; - - err =3D module_add_modinfo_attrs(mod); - if (err) - goto out_unreg_param; - - err =3D add_usage_links(mod); - if (err) - goto out_unreg_modinfo_attrs; - - add_sect_attrs(mod, info); - add_notes_attrs(mod, info); - - return 0; - -out_unreg_modinfo_attrs: - module_remove_modinfo_attrs(mod, -1); -out_unreg_param: - module_param_sysfs_remove(mod); -out_unreg_holders: - kobject_put(mod->holders_dir); -out_unreg: - mod_kobject_put(mod); -out: - return err; -} - -static void mod_sysfs_fini(struct module *mod) -{ - remove_notes_attrs(mod); - remove_sect_attrs(mod); - mod_kobject_put(mod); -} - -static void init_param_lock(struct module *mod) -{ - mutex_init(&mod->param_lock); -} -#else /* !CONFIG_SYSFS */ - -static int mod_sysfs_setup(struct module *mod, - const struct load_info *info, - struct kernel_param *kparam, - unsigned int num_params) -{ - return 0; -} - -static void mod_sysfs_fini(struct module *mod) -{ -} - -static void module_remove_modinfo_attrs(struct module *mod, int end) -{ -} - -static void del_usage_links(struct module *mod) -{ -} - -static void init_param_lock(struct module *mod) -{ -} -#endif /* CONFIG_SYSFS */ - static void mod_sysfs_teardown(struct module *mod) { del_usage_links(mod); diff --git a/kernel/module/sysfs.c b/kernel/module/sysfs.c new file mode 100644 index 000000000000..f5c72c567e71 --- /dev/null +++ b/kernel/module/sysfs.c @@ -0,0 +1,425 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module sysfs support + * + * Copyright (C) 2008 Rusty Russell + */ + +#include +#include +#include +#include +#include +#include +#include +#include "internal.h" + +/* + * /sys/module/foo/sections stuff + * J. Corbet + */ +#ifdef CONFIG_KALLSYMS +struct module_sect_attr { + struct bin_attribute battr; + unsigned long address; +}; + +struct module_sect_attrs { + struct attribute_group grp; + unsigned int nsections; + struct module_sect_attr attrs[]; +}; + +static ssize_t module_sect_read(struct file *file, struct kobject *kobj, + struct bin_attribute *battr, + char *buf, loff_t pos, size_t count) +{ + struct module_sect_attr *sattr =3D + container_of(battr, struct module_sect_attr, battr); + char bounce[MODULE_SECT_READ_SIZE + 1]; + size_t wrote; + + if (pos !=3D 0) + return -EINVAL; + + /* + * Since we're a binary read handler, we must account for the + * trailing NUL byte that sprintf will write: if "buf" is + * too small to hold the NUL, or the NUL is exactly the last + * byte, the read will look like it got truncated by one byte. + * Since there is no way to ask sprintf nicely to not write + * the NUL, we have to use a bounce buffer. + */ + wrote =3D scnprintf(bounce, sizeof(bounce), "0x%px\n", + kallsyms_show_value(file->f_cred) + ? (void *)sattr->address : NULL); + count =3D min(count, wrote); + memcpy(buf, bounce, count); + + return count; +} + +static void free_sect_attrs(struct module_sect_attrs *sect_attrs) +{ + unsigned int section; + + for (section =3D 0; section < sect_attrs->nsections; section++) + kfree(sect_attrs->attrs[section].battr.attr.name); + kfree(sect_attrs); +} + +static void add_sect_attrs(struct module *mod, const struct load_info *inf= o) +{ + unsigned int nloaded =3D 0, i, size[2]; + struct module_sect_attrs *sect_attrs; + struct module_sect_attr *sattr; + struct bin_attribute **gattr; + + /* Count loaded sections and allocate structures */ + for (i =3D 0; i < info->hdr->e_shnum; i++) + if (!sect_empty(&info->sechdrs[i])) + nloaded++; + size[0] =3D ALIGN(struct_size(sect_attrs, attrs, nloaded), + sizeof(sect_attrs->grp.bin_attrs[0])); + size[1] =3D (nloaded + 1) * sizeof(sect_attrs->grp.bin_attrs[0]); + sect_attrs =3D kzalloc(size[0] + size[1], GFP_KERNEL); + if (sect_attrs =3D=3D NULL) + return; + + /* Setup section attributes. */ + sect_attrs->grp.name =3D "sections"; + sect_attrs->grp.bin_attrs =3D (void *)sect_attrs + size[0]; + + sect_attrs->nsections =3D 0; + sattr =3D §_attrs->attrs[0]; + gattr =3D §_attrs->grp.bin_attrs[0]; + for (i =3D 0; i < info->hdr->e_shnum; i++) { + Elf_Shdr *sec =3D &info->sechdrs[i]; + + if (sect_empty(sec)) + continue; + sysfs_bin_attr_init(&sattr->battr); + sattr->address =3D sec->sh_addr; + sattr->battr.attr.name =3D + kstrdup(info->secstrings + sec->sh_name, GFP_KERNEL); + if (sattr->battr.attr.name =3D=3D NULL) + goto out; + sect_attrs->nsections++; + sattr->battr.read =3D module_sect_read; + sattr->battr.size =3D MODULE_SECT_READ_SIZE; + sattr->battr.attr.mode =3D 0400; + *(gattr++) =3D &(sattr++)->battr; + } + *gattr =3D NULL; + + if (sysfs_create_group(&mod->mkobj.kobj, §_attrs->grp)) + goto out; + + mod->sect_attrs =3D sect_attrs; + return; +out: + free_sect_attrs(sect_attrs); +} + +static void remove_sect_attrs(struct module *mod) +{ + if (mod->sect_attrs) { + sysfs_remove_group(&mod->mkobj.kobj, + &mod->sect_attrs->grp); + /* + * We are positive that no one is using any sect attrs + * at this point. Deallocate immediately. + */ + free_sect_attrs(mod->sect_attrs); + mod->sect_attrs =3D NULL; + } +} + +/* + * /sys/module/foo/notes/.section.name gives contents of SHT_NOTE sections. + */ + +struct module_notes_attrs { + struct kobject *dir; + unsigned int notes; + struct bin_attribute attrs[]; +}; + +static ssize_t module_notes_read(struct file *filp, struct kobject *kobj, + struct bin_attribute *bin_attr, + char *buf, loff_t pos, size_t count) +{ + /* + * The caller checked the pos and count against our size. + */ + memcpy(buf, bin_attr->private + pos, count); + return count; +} + +static void free_notes_attrs(struct module_notes_attrs *notes_attrs, + unsigned int i) +{ + if (notes_attrs->dir) { + while (i-- > 0) + sysfs_remove_bin_file(notes_attrs->dir, + ¬es_attrs->attrs[i]); + kobject_put(notes_attrs->dir); + } + kfree(notes_attrs); +} + +static void add_notes_attrs(struct module *mod, const struct load_info *in= fo) +{ + unsigned int notes, loaded, i; + struct module_notes_attrs *notes_attrs; + struct bin_attribute *nattr; + + /* failed to create section attributes, so can't create notes */ + if (!mod->sect_attrs) + return; + + /* Count notes sections and allocate structures. */ + notes =3D 0; + for (i =3D 0; i < info->hdr->e_shnum; i++) + if (!sect_empty(&info->sechdrs[i]) && + (info->sechdrs[i].sh_type =3D=3D SHT_NOTE)) + ++notes; + + if (notes =3D=3D 0) + return; + + notes_attrs =3D kzalloc(struct_size(notes_attrs, attrs, notes), + GFP_KERNEL); + if (notes_attrs =3D=3D NULL) + return; + + notes_attrs->notes =3D notes; + nattr =3D ¬es_attrs->attrs[0]; + for (loaded =3D i =3D 0; i < info->hdr->e_shnum; ++i) { + if (sect_empty(&info->sechdrs[i])) + continue; + if (info->sechdrs[i].sh_type =3D=3D SHT_NOTE) { + sysfs_bin_attr_init(nattr); + nattr->attr.name =3D mod->sect_attrs->attrs[loaded].battr.attr.name; + nattr->attr.mode =3D 0444; + nattr->size =3D info->sechdrs[i].sh_size; + nattr->private =3D (void *) info->sechdrs[i].sh_addr; + nattr->read =3D module_notes_read; + ++nattr; + } + ++loaded; + } + + notes_attrs->dir =3D kobject_create_and_add("notes", &mod->mkobj.kobj); + if (!notes_attrs->dir) + goto out; + + for (i =3D 0; i < notes; ++i) + if (sysfs_create_bin_file(notes_attrs->dir, + ¬es_attrs->attrs[i])) + goto out; + + mod->notes_attrs =3D notes_attrs; + return; + +out: + free_notes_attrs(notes_attrs, i); +} + +static void remove_notes_attrs(struct module *mod) +{ + if (mod->notes_attrs) + free_notes_attrs(mod->notes_attrs, mod->notes_attrs->notes); +} + +#else /* !CONFIG_KALLSYMS */ +static inline void add_sect_attrs(struct module *mod, const struct load_in= fo *info) { } +static inline void remove_sect_attrs(struct module *mod) { } +static inline void add_notes_attrs(struct module *mod, const struct load_i= nfo *info) { } +static inline void remove_notes_attrs(struct module *mod) { } +#endif /* CONFIG_KALLSYMS */ + +void del_usage_links(struct module *mod) +{ +#ifdef CONFIG_MODULE_UNLOAD + struct module_use *use; + + mutex_lock(&module_mutex); + list_for_each_entry(use, &mod->target_list, target_list) + sysfs_remove_link(use->target->holders_dir, mod->name); + mutex_unlock(&module_mutex); +#endif +} + +static int add_usage_links(struct module *mod) +{ + int ret =3D 0; +#ifdef CONFIG_MODULE_UNLOAD + struct module_use *use; + + mutex_lock(&module_mutex); + list_for_each_entry(use, &mod->target_list, target_list) { + ret =3D sysfs_create_link(use->target->holders_dir, + &mod->mkobj.kobj, mod->name); + if (ret) + break; + } + mutex_unlock(&module_mutex); + if (ret) + del_usage_links(mod); +#endif + return ret; +} + +static int module_add_modinfo_attrs(struct module *mod) +{ + struct module_attribute *attr; + struct module_attribute *temp_attr; + int error =3D 0; + int i; + + mod->modinfo_attrs =3D kzalloc((sizeof(struct module_attribute) * + (modinfo_attrs_count + 1)), + GFP_KERNEL); + if (!mod->modinfo_attrs) + return -ENOMEM; + + temp_attr =3D mod->modinfo_attrs; + for (i =3D 0; (attr =3D modinfo_attrs[i]); i++) { + if (!attr->test || attr->test(mod)) { + memcpy(temp_attr, attr, sizeof(*temp_attr)); + sysfs_attr_init(&temp_attr->attr); + error =3D sysfs_create_file(&mod->mkobj.kobj, + &temp_attr->attr); + if (error) + goto error_out; + ++temp_attr; + } + } + + return 0; + +error_out: + if (i > 0) + module_remove_modinfo_attrs(mod, --i); + else + kfree(mod->modinfo_attrs); + return error; +} + +void module_remove_modinfo_attrs(struct module *mod, int end) +{ + struct module_attribute *attr; + int i; + + for (i =3D 0; (attr =3D &mod->modinfo_attrs[i]); i++) { + if (end >=3D 0 && i > end) + break; + /* pick a field to test for end of list */ + if (!attr->attr.name) + break; + sysfs_remove_file(&mod->mkobj.kobj, &attr->attr); + if (attr->free) + attr->free(mod); + } + kfree(mod->modinfo_attrs); +} + +static void mod_kobject_put(struct module *mod) +{ + DECLARE_COMPLETION_ONSTACK(c); + + mod->mkobj.kobj_completion =3D &c; + kobject_put(&mod->mkobj.kobj); + wait_for_completion(&c); +} + +static int mod_sysfs_init(struct module *mod) +{ + int err; + struct kobject *kobj; + + if (!module_sysfs_initialized) { + pr_err("%s: module sysfs not initialized\n", mod->name); + err =3D -EINVAL; + goto out; + } + + kobj =3D kset_find_obj(module_kset, mod->name); + if (kobj) { + pr_err("%s: module is already loaded\n", mod->name); + kobject_put(kobj); + err =3D -EINVAL; + goto out; + } + + mod->mkobj.mod =3D mod; + + memset(&mod->mkobj.kobj, 0, sizeof(mod->mkobj.kobj)); + mod->mkobj.kobj.kset =3D module_kset; + err =3D kobject_init_and_add(&mod->mkobj.kobj, &module_ktype, NULL, + "%s", mod->name); + if (err) + mod_kobject_put(mod); + +out: + return err; +} + +int mod_sysfs_setup(struct module *mod, + const struct load_info *info, + struct kernel_param *kparam, + unsigned int num_params) +{ + int err; + + err =3D mod_sysfs_init(mod); + if (err) + goto out; + + mod->holders_dir =3D kobject_create_and_add("holders", &mod->mkobj.kobj); + if (!mod->holders_dir) { + err =3D -ENOMEM; + goto out_unreg; + } + + err =3D module_param_sysfs_setup(mod, kparam, num_params); + if (err) + goto out_unreg_holders; + + err =3D module_add_modinfo_attrs(mod); + if (err) + goto out_unreg_param; + + err =3D add_usage_links(mod); + if (err) + goto out_unreg_modinfo_attrs; + + add_sect_attrs(mod, info); + add_notes_attrs(mod, info); + + return 0; + +out_unreg_modinfo_attrs: + module_remove_modinfo_attrs(mod, -1); +out_unreg_param: + module_param_sysfs_remove(mod); +out_unreg_holders: + kobject_put(mod->holders_dir); +out_unreg: + mod_kobject_put(mod); +out: + return err; +} + +void mod_sysfs_fini(struct module *mod) +{ + remove_notes_attrs(mod); + remove_sect_attrs(mod); + mod_kobject_put(mod); +} + +void init_param_lock(struct module *mod) +{ + mutex_init(&mod->param_lock); +} --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 34DB2C433F5 for ; Sun, 30 Jan 2022 21:33:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356756AbiA3Vdm (ORCPT ); Sun, 30 Jan 2022 16:33:42 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:37615 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356636AbiA3Vcg (ORCPT ); Sun, 30 Jan 2022 16:32:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578355; 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=4WWouBNY6XHsJho+NkhowbAagBN3wD6Bcpj36xe5bwk=; b=iNbKc9aDbpu/cDmHdtvNCtV4oWZG/nt128Qw9G9qSjnDUQ7GuuB4/Abb84z2Z7UBmbPa9g nXdH5FOzcAnA5t3ZtQZ5m+2C2QAlMQVCIeAMD4i1MB25BaQ3airH99WpEdtVhNWGScdZ+Q O9g4Oqj2o2kEK5gWnzp0r517S0NVhQ0= 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-648-7IYZ8pORMzuiG9vTWAYc2g-1; Sun, 30 Jan 2022 16:32:34 -0500 X-MC-Unique: 7IYZ8pORMzuiG9vTWAYc2g-1 Received: by mail-wm1-f71.google.com with SMTP id n22-20020a05600c3b9600b00352d3f6a850so442138wms.3 for ; Sun, 30 Jan 2022 13:32:33 -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=4WWouBNY6XHsJho+NkhowbAagBN3wD6Bcpj36xe5bwk=; b=lhz49+F9yu88sXEs4DZPkODnMGM3uKm+UyCsDk5YcKUiLk/HurWgGHD+07NXyNQcBV kVdPnJQ53Ku2tPgqUzlL3M3gbdF8K5WwBtfk/L1fQHbBkSBS1rsSWpo29l+1HfAToetO a3zpKO2o6rDY7j3FLJbHRvrIx8P6IJvr6pgnEoDAbgg155A56Fm4UbYr/65yl2ja6BbZ MJ723U06P+RzitbexYcb+eoij9QiAL9vuHLwdIW27B8W23/insDHcePb5Q35z1lI8TyX T0/+SBQgiKiNTjANp0fCEp+TJCr1pOpdSkzjeO9orRyzVCATCqLEs1btGmADaw9+FB9u sUyw== X-Gm-Message-State: AOAM533HX+wXT9F38rsxgCfYkWVlpjloUfdF6tWNCWz/diT7hEZBUTOw KSE6++o/XbuD5z3NTJ2frN6vfJtJbJNgTfZr1rg6w0eSx/4q1j07bu1Pw/alM0LK20dE+0Sds/j 1+tF+wI6Q+28unwpRmD59zTc= X-Received: by 2002:a05:6000:1568:: with SMTP id 8mr15258951wrz.54.1643578353045; Sun, 30 Jan 2022 13:32:33 -0800 (PST) X-Google-Smtp-Source: ABdhPJzxnioIPHbSlQTlSmnwMfQCG2ZAC/+LiIaUTTnbob+kakVGHe+nULEei/uHEJHjOGr8GoH4Vw== X-Received: by 2002:a05:6000:1568:: with SMTP id 8mr15258935wrz.54.1643578352896; Sun, 30 Jan 2022 13:32:32 -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 o3sm10006613wrq.70.2022.01.30.13.32.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:32 -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 12/13] module: Move kdb_modules list out of core code Date: Sun, 30 Jan 2022 21:32:13 +0000 Message-Id: <20220130213214.1042497-13-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates kdb_modules list to core kdb code since the list of added/or loaded modules is no longer private. Signed-off-by: Aaron Tomlin --- kernel/debug/kdb/kdb_main.c | 5 +++++ kernel/module/main.c | 4 ---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/kernel/debug/kdb/kdb_main.c b/kernel/debug/kdb/kdb_main.c index 0852a537dad4..f101f5f078f4 100644 --- a/kernel/debug/kdb/kdb_main.c +++ b/kernel/debug/kdb/kdb_main.c @@ -59,6 +59,11 @@ EXPORT_SYMBOL(kdb_grepping_flag); int kdb_grep_leading; int kdb_grep_trailing; =20 +#ifdef CONFIG_MODULES +extern struct list_head modules; +struct list_head *kdb_modules =3D &modules; /* kdb needs the list of modul= es */ +#endif /* CONFIG_MODULES */ + /* * Kernel debugger state flags */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 1d16faea82b3..59e1c271812c 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -104,10 +104,6 @@ static void mod_update_bounds(struct module *mod) __mod_update_bounds(mod->init_layout.base, mod->init_layout.size); } =20 -#ifdef CONFIG_KGDB_KDB -struct list_head *kdb_modules =3D &modules; /* kdb needs the list of modul= es */ -#endif /* CONFIG_KGDB_KDB */ - static void module_assert_mutex_or_preempt(void) { #ifdef CONFIG_LOCKDEP --=20 2.34.1 From nobody Tue Jun 30 02:38:54 2026 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 CF4DAC433EF for ; Sun, 30 Jan 2022 21:33:46 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1356924AbiA3Vdq (ORCPT ); Sun, 30 Jan 2022 16:33:46 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:40266 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356699AbiA3Vcj (ORCPT ); Sun, 30 Jan 2022 16:32:39 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643578358; 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=kZQ9Hs4/wmCoj+16Mcb+2vAOK8w2tokt0G0/DM60MG4=; b=e0NQKTlZPF9TfrClQ0PDMWykcgE7gRtq5vsWL9tMuA229fLDiy5b6112rjGHwAhb71JPAq i3uHq2lfS3U4RzgwbLxHZ5LqFbGrlzMzmqQCLbud7HS0kxp/82pdGTyvs5ZIJLidITPXaC sSwy5hfKbfNJYf0s3fUyzByQFPqA86k= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-652-3HT5W4m7MUuOQFeGuGhfUA-1; Sun, 30 Jan 2022 16:32:35 -0500 X-MC-Unique: 3HT5W4m7MUuOQFeGuGhfUA-1 Received: by mail-wm1-f70.google.com with SMTP id s1-20020a1ca901000000b0034ece94dd8cso9356414wme.5 for ; Sun, 30 Jan 2022 13:32:35 -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=kZQ9Hs4/wmCoj+16Mcb+2vAOK8w2tokt0G0/DM60MG4=; b=FckjXLzX3LOOdE+KL+VpbhnwK3uRMc6u/ldhy34dvQoT2rzWfcNx8tzhjxQz7pmumT E0KjHFM4YbvPZKzd6ASMt8dvUNsbFtFhYsMHbgi+r0tzjLn22SAIUBFd+sY/qNGQmata 7brp7rII6g7xM3vAhcObroc7ZrCnDO1SBBziBbgNVKWiaJEzblCakmZ0Vd/O6BX8LjiK 2BN3IheFga5e2pFMq93dEou/V58z6pU8lMAec41a/dqn0NHbY3AxIHW8kuteDwNHC9bd e3DpjHfSiyU67fG1ctn3TsWwzyeNbgKLPsClxTlkZkOOfbQyv/LtL4k4ZIRdiztFEuhb gUsw== X-Gm-Message-State: AOAM533yVc2o3m7S6oxzizk4OA8M+U76WeR56F/OmzqJsnF7Ea29AFyM dK52nu1OfV4bdzdmjyiTT8dkvzztcTbSfPsSXiEQ6dvh8N0YD+rOOwTl6kABH+E6VYjLZqOuGAE on3PB4k9EvsXoohqR/+jKPkY= X-Received: by 2002:a05:6000:188f:: with SMTP id a15mr14647681wri.120.1643578354412; Sun, 30 Jan 2022 13:32:34 -0800 (PST) X-Google-Smtp-Source: ABdhPJww0FdtkutLjHGOt8QAZq6fsAghDXlKKHUJat0cbJMrLjCfhtBASZhdokVlaR6MRZUMwwlWpA== X-Received: by 2002:a05:6000:188f:: with SMTP id a15mr14647664wri.120.1643578354189; Sun, 30 Jan 2022 13:32:34 -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 k20sm3944585wmi.36.2022.01.30.13.32.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 30 Jan 2022 13:32:33 -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 13/13] module: Move version support into a separate file Date: Sun, 30 Jan 2022 21:32:14 +0000 Message-Id: <20220130213214.1042497-14-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: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" No functional change. This patch migrates module version support out of core code into kernel/module/version.c. In addition simple code refactoring to make this possible. Signed-off-by: Aaron Tomlin --- kernel/module/Makefile | 1 + kernel/module/internal.h | 51 +++++++++++++ kernel/module/main.c | 150 +-------------------------------------- kernel/module/version.c | 110 ++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 148 deletions(-) create mode 100644 kernel/module/version.c diff --git a/kernel/module/Makefile b/kernel/module/Makefile index c8982b47b3a2..6269f4669e47 100644 --- a/kernel/module/Makefile +++ b/kernel/module/Makefile @@ -16,4 +16,5 @@ obj-$(CONFIG_DEBUG_KMEMLEAK) +=3D debug_kmemleak.o obj-$(CONFIG_KALLSYMS) +=3D kallsyms.o obj-$(CONFIG_PROC_FS) +=3D procfs.o obj-$(CONFIG_SYSFS) +=3D sysfs.o +obj-$(CONFIG_MODVERSIONS) +=3D version.o endif diff --git a/kernel/module/internal.h b/kernel/module/internal.h index cf3baca4ebea..405e18847730 100644 --- a/kernel/module/internal.h +++ b/kernel/module/internal.h @@ -59,7 +59,31 @@ struct load_info { } index; }; =20 +struct symsearch { + const struct kernel_symbol *start, *stop; + const s32 *crcs; + enum mod_license { + NOT_GPL_ONLY, + GPL_ONLY, + } license; +}; + +struct find_symbol_arg { + /* Input */ + const char *name; + bool gplok; + bool warn; + + /* Output */ + struct module *owner; + const s32 *crc; + const struct kernel_symbol *sym; + enum mod_license license; +}; + extern int mod_verify_sig(const void *mod, struct load_info *info); +extern int try_to_force_load(struct module *mod, const char *reason); +extern bool find_symbol(struct find_symbol_arg *fsa); extern struct module *find_module_all(const char *name, size_t len, bool e= ven_unformed); extern unsigned long kernel_symbol_value(const struct kernel_symbol *sym); extern int cmp_name(const void *name, const void *sym); @@ -183,3 +207,30 @@ static inline void module_remove_modinfo_attrs(struct = module *mod, int end) { } static inline void del_usage_links(struct module *mod) { } static inline void init_param_lock(struct module *mod) { } #endif /* CONFIG_SYSFS */ + +#ifdef CONFIG_MODVERSIONS +extern int check_version(const struct load_info *info, + const char *symname, struct module *mod, const s32 *crc); +extern int check_modstruct_version(const struct load_info *info, struct mo= dule *mod); +extern int same_magic(const char *amagic, const char *bmagic, bool has_crc= s); +#else /* !CONFIG_MODVERSIONS */ +static inline int check_version(const struct load_info *info, + const char *symname, + struct module *mod, + const s32 *crc) +{ + return 1; +} + +static inline int check_modstruct_version(const struct load_info *info, + struct module *mod) +{ + return 1; +} + +static inline int same_magic(const char *amagic, const char *bmagic, + bool has_crcs) +{ + return strcmp(amagic, bmagic) =3D=3D 0; +} +#endif /* CONFIG_MODVERSIONS */ diff --git a/kernel/module/main.c b/kernel/module/main.c index 59e1c271812c..6b13c17427d8 100644 --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -240,28 +240,6 @@ static __maybe_unused void *any_section_objs(const str= uct load_info *info, #define symversion(base, idx) ((base !=3D NULL) ? ((base) + (idx)) : NULL) #endif =20 -struct symsearch { - const struct kernel_symbol *start, *stop; - const s32 *crcs; - enum mod_license { - NOT_GPL_ONLY, - GPL_ONLY, - } license; -}; - -struct find_symbol_arg { - /* Input */ - const char *name; - bool gplok; - bool warn; - - /* Output */ - struct module *owner; - const s32 *crc; - const struct kernel_symbol *sym; - enum mod_license license; -}; - static bool check_exported_symbol(const struct symsearch *syms, struct module *owner, unsigned int symnum, void *data) @@ -332,7 +310,7 @@ static bool find_exported_symbol_in_section(const struc= t symsearch *syms, * Find an exported symbol and return it, along with, (optional) crc and * (optional) module which owns it. Needs preempt disabled or module_mute= x. */ -static bool find_symbol(struct find_symbol_arg *fsa) +bool find_symbol(struct find_symbol_arg *fsa) { static const struct symsearch arr[] =3D { { __start___ksymtab, __stop___ksymtab, __start___kcrctab, @@ -1006,7 +984,7 @@ size_t modinfo_attrs_count =3D ARRAY_SIZE(modinfo_attr= s); =20 static const char vermagic[] =3D VERMAGIC_STRING; =20 -static int try_to_force_load(struct module *mod, const char *reason) +int try_to_force_load(struct module *mod, const char *reason) { #ifdef CONFIG_MODULE_FORCE_LOAD if (!test_taint(TAINT_FORCED_MODULE)) @@ -1018,115 +996,6 @@ static int try_to_force_load(struct module *mod, con= st char *reason) #endif } =20 -#ifdef CONFIG_MODVERSIONS - -static u32 resolve_rel_crc(const s32 *crc) -{ - return *(u32 *)((void *)crc + *crc); -} - -static int check_version(const struct load_info *info, - const char *symname, - struct module *mod, - const s32 *crc) -{ - Elf_Shdr *sechdrs =3D info->sechdrs; - unsigned int versindex =3D info->index.vers; - unsigned int i, num_versions; - struct modversion_info *versions; - - /* Exporting module didn't supply crcs? OK, we're already tainted. */ - if (!crc) - return 1; - - /* No versions at all? modprobe --force does this. */ - if (versindex =3D=3D 0) - return try_to_force_load(mod, symname) =3D=3D 0; - - versions =3D (void *) sechdrs[versindex].sh_addr; - num_versions =3D sechdrs[versindex].sh_size - / sizeof(struct modversion_info); - - for (i =3D 0; i < num_versions; i++) { - u32 crcval; - - if (strcmp(versions[i].name, symname) !=3D 0) - continue; - - if (IS_ENABLED(CONFIG_MODULE_REL_CRCS)) - crcval =3D resolve_rel_crc(crc); - else - crcval =3D *crc; - if (versions[i].crc =3D=3D crcval) - return 1; - pr_debug("Found checksum %X vs module %lX\n", - crcval, versions[i].crc); - goto bad_version; - } - - /* Broken toolchain. Warn once, then let it go.. */ - pr_warn_once("%s: no symbol version for %s\n", info->name, symname); - return 1; - -bad_version: - pr_warn("%s: disagrees about version of symbol %s\n", - info->name, symname); - return 0; -} - -static inline int check_modstruct_version(const struct load_info *info, - struct module *mod) -{ - struct find_symbol_arg fsa =3D { - .name =3D "module_layout", - .gplok =3D true, - }; - - /* - * Since this should be found in kernel (which can't be removed), no - * locking is necessary -- use preempt_disable() to placate lockdep. - */ - preempt_disable(); - if (!find_symbol(&fsa)) { - preempt_enable(); - BUG(); - } - preempt_enable(); - return check_version(info, "module_layout", mod, fsa.crc); -} - -/* First part is kernel version, which we ignore if module has crcs. */ -static inline int same_magic(const char *amagic, const char *bmagic, - bool has_crcs) -{ - if (has_crcs) { - amagic +=3D strcspn(amagic, " "); - bmagic +=3D strcspn(bmagic, " "); - } - return strcmp(amagic, bmagic) =3D=3D 0; -} -#else -static inline int check_version(const struct load_info *info, - const char *symname, - struct module *mod, - const s32 *crc) -{ - return 1; -} - -static inline int check_modstruct_version(const struct load_info *info, - struct module *mod) -{ - return 1; -} - -static inline int same_magic(const char *amagic, const char *bmagic, - bool has_crcs) -{ - return strcmp(amagic, bmagic) =3D=3D 0; -} -#endif /* CONFIG_MODVERSIONS */ - static char *get_modinfo(const struct load_info *info, const char *tag); static char *get_next_modinfo(const struct load_info *info, const char *ta= g, char *prev); @@ -3238,18 +3107,3 @@ void print_modules(void) pr_cont(" [last unloaded: %s]", last_unloaded_module); pr_cont("\n"); } - -#ifdef CONFIG_MODVERSIONS -/* - * Generate the signature for all relevant module structures here. - * If these change, we don't want to try to parse the module. - */ -void module_layout(struct module *mod, - struct modversion_info *ver, - struct kernel_param *kp, - struct kernel_symbol *ks, - struct tracepoint * const *tp) -{ -} -EXPORT_SYMBOL(module_layout); -#endif diff --git a/kernel/module/version.c b/kernel/module/version.c new file mode 100644 index 000000000000..10a1490d1b9e --- /dev/null +++ b/kernel/module/version.c @@ -0,0 +1,110 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Module version support + * + * Copyright (C) 2008 Rusty Russell + */ + +#include +#include +#include +#include "internal.h" + +static u32 resolve_rel_crc(const s32 *crc) +{ + return *(u32 *)((void *)crc + *crc); +} + +int check_version(const struct load_info *info, + const char *symname, + struct module *mod, + const s32 *crc) +{ + Elf_Shdr *sechdrs =3D info->sechdrs; + unsigned int versindex =3D info->index.vers; + unsigned int i, num_versions; + struct modversion_info *versions; + + /* Exporting module didn't supply crcs? OK, we're already tainted. */ + if (!crc) + return 1; + + /* No versions at all? modprobe --force does this. */ + if (versindex =3D=3D 0) + return try_to_force_load(mod, symname) =3D=3D 0; + + versions =3D (void *) sechdrs[versindex].sh_addr; + num_versions =3D sechdrs[versindex].sh_size + / sizeof(struct modversion_info); + + for (i =3D 0; i < num_versions; i++) { + u32 crcval; + + if (strcmp(versions[i].name, symname) !=3D 0) + continue; + + if (IS_ENABLED(CONFIG_MODULE_REL_CRCS)) + crcval =3D resolve_rel_crc(crc); + else + crcval =3D *crc; + if (versions[i].crc =3D=3D crcval) + return 1; + pr_debug("Found checksum %X vs module %lX\n", + crcval, versions[i].crc); + goto bad_version; + } + + /* Broken toolchain. Warn once, then let it go.. */ + pr_warn_once("%s: no symbol version for %s\n", info->name, symname); + return 1; + +bad_version: + pr_warn("%s: disagrees about version of symbol %s\n", + info->name, symname); + return 0; +} + +inline int check_modstruct_version(const struct load_info *info, + struct module *mod) +{ + struct find_symbol_arg fsa =3D { + .name =3D "module_layout", + .gplok =3D true, + }; + + /* + * Since this should be found in kernel (which can't be removed), no + * locking is necessary -- use preempt_disable() to placate lockdep. + */ + preempt_disable(); + if (!find_symbol(&fsa)) { + preempt_enable(); + BUG(); + } + preempt_enable(); + return check_version(info, "module_layout", mod, fsa.crc); +} + +/* First part is kernel version, which we ignore if module has crcs. */ +inline int same_magic(const char *amagic, const char *bmagic, + bool has_crcs) +{ + if (has_crcs) { + amagic +=3D strcspn(amagic, " "); + bmagic +=3D strcspn(bmagic, " "); + } + return strcmp(amagic, bmagic) =3D=3D 0; +} + +/* + * Generate the signature for all relevant module structures here. + * If these change, we don't want to try to parse the module. + */ +void module_layout(struct module *mod, + struct modversion_info *ver, + struct kernel_param *kp, + struct kernel_symbol *ks, + struct tracepoint * const *tp) +{ +} +EXPORT_SYMBOL(module_layout); --=20 2.34.1