From nobody Wed Dec 17 08:51:14 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 84338255F4E; Fri, 2 May 2025 14:25:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195909; cv=none; b=gzARrMOccH/gxpyT7m+IulQ1oNuC7CvhWTc1Lz0F9wgRRWbhdrZpncidNckCkQ+LgLsCowXDTJVJHI8o58Wawoo07CglX9PrmZLuANGQzEsO7OOqCmsytBuGInRgX7DmGHMBp8cS5nxIJHlIyL8u2WUK5Urlp/DBKQXeXHyQJsY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195909; c=relaxed/simple; bh=jRjBKw4YOqZTbdVmrR6qZ/i4ZyoyCPf7+SVKGt83R/k=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=pE5jEPYxuuzgtzMy+vcRhuwUKh7KYbuDUOpi4NB0FtdrvasayWnjLmbQsZR5twQaFdleafCWXYaa0TeI3+ElsUMDdSART2VAXUSwDaUAZRg1RvJFkPIwUJpea+L83CoeWR2iwgZNdymMLPR9egraOvQm2cG5G4BMPa16qwrPVJk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=jOCdhI43; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="jOCdhI43" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=wSEofVt37+OE35DFLYfF1av+a4A68i6wp+MZRME4os4=; b=jOCdhI43EtUJ6ce96sPwbhJOu6 3WfqKCd/O0nHmlLZEG3mHGuAhVQoXqeh5jdnt1DGphdBQVlOrX8sWuh5NyvtXrbiV3jQYxhgIY3We t9lT2q2t7ei474Cad0di45lO/K14mcywFxLJVxBsLxZUhRO+vrymre+fPOs/EDnI6tSzoTph7Eist Nf4fSC6IXZOnpiw19das0+Axy83BPznXCZ228I+gfhNbsPDpbKzJVBuR5sMhvc+kSnDrMR6C5TT7r QmYcKVr2QVT7doSgaRSmv/tblCk9szfvPLFID7zM+Hajw+XcNzpDHp2as/0Hg1ZWsCzJ+G/56hzMt tJI3MUiw==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1uArK1-0000000EzDd-16cX; Fri, 02 May 2025 14:25:01 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id E4ADA300777; Fri, 2 May 2025 16:24:59 +0200 (CEST) Message-ID: <20250502141843.937580735@infradead.org> User-Agent: quilt/0.66 Date: Fri, 02 May 2025 16:12:05 +0200 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, roypat@amazon.co.uk, "Peter Zijlstra (Intel)" Subject: [PATCH v3 1/5] modpost: Use for() loop References: <20250502141204.500293812@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Slight cleanup by using a for() loop instead of while(). This makes it clearer what is the iteration and what is the actual work done. Signed-off-by: Peter Zijlstra (Intel) Acked-by: Greg Kroah-Hartman --- scripts/mod/modpost.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1595,12 +1595,10 @@ static void read_symbols(const char *mod license =3D get_next_modinfo(&info, "license", license); } =20 - namespace =3D get_modinfo(&info, "import_ns"); - while (namespace) { + for (namespace =3D get_modinfo(&info, "import_ns"); + namespace; + namespace =3D get_next_modinfo(&info, "import_ns", namespace)) add_namespace(&mod->imported_namespaces, namespace); - namespace =3D get_next_modinfo(&info, "import_ns", - namespace); - } =20 if (!get_modinfo(&info, "description")) warn("missing MODULE_DESCRIPTION() in %s\n", modname); From nobody Wed Dec 17 08:51:14 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 698182561D4; Fri, 2 May 2025 14:25:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195965; cv=none; b=IyNc3k15o3ALpPj6RSpdx+jNqIDT7bsp/r88GGKnmPfxV9nSRrKqMIznPSSt4AcEIoaTFNcANHvop0EcE7EYcaUr0WuCsHl+YC7qM1wim7WZB3csc9LGlydOOqpb1m54GwukryC0id6g6ZVMg4UYrIxypRsBb/m4UBwQFtGR41Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195965; c=relaxed/simple; bh=Ev5in1o5uPO+eUSus0eAQkz9t8/6/6cCR+cECSU4aPc=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=MBFe6Cp1rVgS0yeHGh1Vbr06DXSuqtDMsUHMYaAzws4ejXOfHptQZM+jBurX0g5c5PBS0GjFj5v6SeExjB/CGMxhwjftAhojXrijtzAWBQf1L93B91FkhJ5GJFMcJ9iQIFv9plU97DGHUIP5LApGQQSTWlSZm4jnvRbMm+6rLr0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=nbODw3Ow; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="nbODw3Ow" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=orH9pxNknjY+XgTLI4Ne4ZRWvYOjc6KHHMNe5II3pf4=; b=nbODw3Owl/GGA/xJnl/eCynY6n 5tIcrA2tYzQtT68Cq/PYDddPQvx+nVKFpmerX7zfXvDq6lOZDOqfWM33HF+kjC1Bhn9XYsZjOFNkq iFpmiFPlWmn0J0ro1tTPEAyCBmoZmpLZ+uavk0vUofn46snq55aalIK9JM4wH8vuPN15/kbAhBjtt YTLCy2M08Q7g+Wkp3AzhhLfokUTsz9DSyK4OYJju5Uy9W/9tA+eBSssmyAaBonOmPqDNHZH4er85z Xl/hv+0NGpnrqdd3XGGujE9KO4ucBWxGAJK/Axq3au7aSf/RIsbxzuz6xAV4A5S6C6Du3Vkg/Q3O4 oM7Eaeyg==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1uArKC-00000002iJi-3DtM; Fri, 02 May 2025 14:25:13 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id E81DD300780; Fri, 2 May 2025 16:24:59 +0200 (CEST) Message-ID: <20250502141844.046738270@infradead.org> User-Agent: quilt/0.66 Date: Fri, 02 May 2025 16:12:06 +0200 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, roypat@amazon.co.uk, "Peter Zijlstra (Intel)" Subject: [PATCH v3 2/5] module: Add module specific symbol namespace support References: <20250502141204.500293812@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Designate the "module:${modname}" symbol namespace to mean: 'only export to the named module'. Notably, explicit imports of anything in the "module:" space is forbidden. Signed-off-by: Peter Zijlstra (Intel) Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Pavlu --- kernel/module/main.c | 33 +++++++++++++++++++++++++++++++-- scripts/mod/modpost.c | 11 ++++++++++- 2 files changed, 41 insertions(+), 3 deletions(-) --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1083,6 +1083,14 @@ static char *get_modinfo(const struct lo return get_next_modinfo(info, tag, NULL); } =20 +static bool verify_module_namespace(const char *namespace, const char *mod= name) +{ + const char *prefix =3D "module:"; + + return strstarts(namespace, prefix) && + !strsmp(namespace + strlen(prefix), modname); +} + static int verify_namespace_is_imported(const struct load_info *info, const struct kernel_symbol *sym, struct module *mod) @@ -1092,6 +1100,10 @@ static int verify_namespace_is_imported( =20 namespace =3D kernel_symbol_namespace(sym); if (namespace && namespace[0]) { + + if (verify_module_namespace(namespace, mod->name)) + return 0; + for_each_modinfo_entry(imported_namespace, info, "import_ns") { if (strcmp(namespace, imported_namespace) =3D=3D 0) return 0; @@ -1659,15 +1671,30 @@ static void module_license_taint_check(s } } =20 -static void setup_modinfo(struct module *mod, struct load_info *info) +static int setup_modinfo(struct module *mod, struct load_info *info) { const struct module_attribute *attr; + char *imported_namespace; int i; =20 for (i =3D 0; (attr =3D modinfo_attrs[i]); i++) { if (attr->setup) attr->setup(mod, get_modinfo(info, attr->attr.name)); } + + for_each_modinfo_entry(imported_namespace, info, "import_ns") { + /* + * 'module:' prefixed namespaces are implicit, disallow + * explicit imports. + */ + if (strstarts(imported_namespace, "module:")) { + pr_err("%s: module tries to import module namespace: %s\n", + mod->name, imported_namespace); + return -EPERM; + } + } + + return 0; } =20 static void free_modinfo(struct module *mod) @@ -3335,7 +3362,9 @@ static int load_module(struct load_info goto free_unload; =20 /* Set up MODINFO_ATTR fields */ - setup_modinfo(mod, info); + err =3D setup_modinfo(mod, info); + if (err) + goto free_modinfo; =20 /* Fix up syms, so that st_value is a pointer to location. */ err =3D simplify_symbols(mod, info); --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1682,6 +1682,14 @@ void buf_write(struct buffer *buf, const buf->pos +=3D len; } =20 +static bool verify_module_namespace(const char *namespace, const char *mod= name) +{ + const char *prefix =3D "module:"; + + return strstarts(namespace, prefix) && + !strcmp(namespace + strlen(prefix), modname); +} + static void check_exports(struct module *mod) { struct symbol *s, *exp; @@ -1709,7 +1717,8 @@ static void check_exports(struct module =20 basename =3D get_basename(mod->name); =20 - if (!contains_namespace(&mod->imported_namespaces, exp->namespace)) { + if (!verify_module_namespace(exp->namespace, basename) && + !contains_namespace(&mod->imported_namespaces, exp->namespace)) { modpost_log(!allow_missing_ns_imports, "module %s uses symbol %s from namespace %s, but does not import i= t.\n", basename, exp->name, exp->namespace); From nobody Wed Dec 17 08:51:14 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3C409256C9B; Fri, 2 May 2025 14:26:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195971; cv=none; b=Ot/aTKbMzPwsXBoUuHbq+WkjQvvh0gM1adWOyJrqxmaDLVrbM0Ri1q+dQ202DiTxRbu2umT8eHNjrSL+7UEd/RC4eS3izjn36L6eXtNXln4it33C2rq6k547vVVQrllCm0nVnCXv0YmDYSLmwvniBUuswDdFjDDuPaCg9+5WLbQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195971; c=relaxed/simple; bh=r7bJ04rxm1egxZUxKBy0DaRcOzZy9pv4ih3VucjjqiQ=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=sjFh3T5p5BIRX2tHFqAu+MKJfAmzD22MmTsyETTng4nQqqI7eYyDV97JanRnLdb+R5MAAgkVhzeJPcAXUHQdEICXqOcxJMw7G+hfShefUezU+HWqCmcIoJ+xDryKubbj1F8PGKprWOHnC4mk4UL697Yta0qFh/j7BGs185OI8Kc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=GgT39S32; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="GgT39S32" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=9/d9HGSmnu1K8plMQ/z7T8XUHN+iiRR+u0jykNWAszU=; b=GgT39S32uyjySX0HPX7e53L9DP AvRt81KovfDOhWpovtyAUNYEVYV/JjQQzre1FzF9JgXabkYzt1IQZtRyZaQ/6maBbwZ4Pl97yboaV odume75X6YelhfWEXof2oNUHJzTAahD6ebnv0NggIE7LlHyO1424l+2KtiD4pyI735wWQQFSPruKC D8ZTo3W9ILct4rrJnPd72KWodSRBe2yQo/o8WBdVLGSINUYasjWF6tuZarfGVEUnvCoM/CyJHBWKW JsNeMjyjUROqI9APYnebo7AYIqV7CWFqh8wBmzpAg8zxBd+lxCz0bu3P8euWp+5PzzwzUUGlEb8y0 xqw0TNLA==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1uArKE-00000002iJh-3iXp; Fri, 02 May 2025 14:25:16 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id EBEDD30078C; Fri, 2 May 2025 16:24:59 +0200 (CEST) Message-ID: <20250502141844.154517322@infradead.org> User-Agent: quilt/0.66 Date: Fri, 02 May 2025 16:12:07 +0200 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, roypat@amazon.co.uk, "Peter Zijlstra (Intel)" Subject: [PATCH v3 3/5] module: Extend the MODULE_ namespace parsing References: <20250502141204.500293812@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Instead of only accepting "module:${name}", extend it with a comma separated list of module names and add tail glob support. That is, something like: "module:foo-*,bar" is now possible. Signed-off-by: Peter Zijlstra (Intel) Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Pavlu --- kernel/module/main.c | 36 ++++++++++++++++++++++++++++++++++-- scripts/mod/modpost.c | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 4 deletions(-) --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -1083,12 +1083,44 @@ static char *get_modinfo(const struct lo return get_next_modinfo(info, tag, NULL); } =20 +/** + * verify_module_namespace() - does @modname have access to this symbol's = @namespace + * @namespace: export symbol namespace + * @modname: module name + * + * If @namespace is prefixed with "module:" to indicate it is a module nam= espace + * then test if @modname matches any of the comma separated patterns. + * + * The patterns only support tail-glob. + */ static bool verify_module_namespace(const char *namespace, const char *mod= name) { + size_t len, modlen =3D strlen(modname); const char *prefix =3D "module:"; + const char *sep; + bool glob; =20 - return strstarts(namespace, prefix) && - !strsmp(namespace + strlen(prefix), modname); + if (!strstarts(namespace, prefix)) + return false; + + for (namespace +=3D strlen(prefix); *namespace; namespace =3D sep) { + sep =3D strchrnul(namespace, ','); + len =3D sep - namespace; + + glob =3D false; + if (sep[-1] =3D=3D '*') { + len--; + glob =3D true; + } + + if (*sep) + sep++; + + if (strncmp(namespace, modname, len) =3D=3D 0 && (glob || len =3D=3D mod= len)) + return true; + } + + return false; } =20 static int verify_namespace_is_imported(const struct load_info *info, --- a/scripts/mod/modpost.c +++ b/scripts/mod/modpost.c @@ -1682,12 +1682,44 @@ void buf_write(struct buffer *buf, const buf->pos +=3D len; } =20 +/** + * verify_module_namespace() - does @modname have access to this symbol's = @namespace + * @namespace: export symbol namespace + * @modname: module name + * + * If @namespace is prefixed with "module:" to indicate it is a module nam= espace + * then test if @modname matches any of the comma separated patterns. + * + * The patterns only support tail-glob. + */ static bool verify_module_namespace(const char *namespace, const char *mod= name) { + size_t len, modlen =3D strlen(modname); const char *prefix =3D "module:"; + const char *sep; + bool glob; =20 - return strstarts(namespace, prefix) && - !strcmp(namespace + strlen(prefix), modname); + if (!strstarts(namespace, prefix)) + return false; + + for (namespace +=3D strlen(prefix); *namespace; namespace =3D sep) { + sep =3D strchrnul(namespace, ','); + len =3D sep - namespace; + + glob =3D false; + if (sep[-1] =3D=3D '*') { + len--; + glob =3D true; + } + + if (*sep) + sep++; + + if (strncmp(namespace, modname, len) =3D=3D 0 && (glob || len =3D=3D mod= len)) + return true; + } + + return false; } =20 static void check_exports(struct module *mod) From nobody Wed Dec 17 08:51:14 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6976E2561C7; Fri, 2 May 2025 14:25:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195961; cv=none; b=LofyoGBIBCp53XjHPnqWkbfhBBFiN2spLvTlC4+zsENgK4FYmRPP2mJV7LLENXjCJ/ugE9V/VPEg6k/aeiZ81/NvKWFrTlCJQ50n+IfVOmIoTFS9c2gL0/fWAfyzsMnclY1usf9Zq1m0iWun0n4B2U2xZOXbj1mDhyP6+X3lWAg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195961; c=relaxed/simple; bh=REB2eBCK+R9hPD7UeAqPnHnHhVOyXX6Q/mLp4WgtHlA=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=LZUbpChxspsH733MbNuvVjV8BJF1CiCjH5LVeB0OyEITOTs9G6AWpDzgzWwUb+RkmJIfvo7xFWXKfdad9NapkKurG7NUtTx2cMNl4Cil6zmAGA6wtTmyJqh76ktTQ4bQJ8VlT7U8Ouqm2JxUNwV0OQK7PpLbM/6p1JqdWh8nsjM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=QpXStV5Y; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="QpXStV5Y" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=/UvIJyPu4PFnUbnZPb6KkO/nGn6jmjyInuZMnh8rDj0=; b=QpXStV5YToMC6plCNaJSXQHB4K YeJQE2XoMrdquYIoXPUBk+07E3YFIRiR03BC6Sl3g/96xyck/qYGSG7l8DfopcijtmaXCUQtphmQu EqLGsV38TUMPW3xfkKjbPFtRmJ9yBxHSVlg0csvlcx0rEPzv4mPiVHaq4pQL0qxWD86DNaIRXYX8v 5Ei5pBUEkorSP81YRVxhNB81C4BbTs3K1+pcqn4d6CPuNuC9a/Gt6hWH6uikwTzoKaY/U/T3OFXqu Oheu+VX0aL4eHbqTcbRle78lAfD5kPneD/1doeEmlUDhYLXjtIKXLGcjv6JO1HMRm8HRlbj04WsUS ON6yzVxg==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1uArKD-00000002iJj-3aSK; Fri, 02 May 2025 14:25:14 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id F16A830081F; Fri, 2 May 2025 16:24:59 +0200 (CEST) Message-ID: <20250502141844.263611823@infradead.org> User-Agent: quilt/0.66 Date: Fri, 02 May 2025 16:12:08 +0200 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, roypat@amazon.co.uk, Sean Christopherson , "Peter Zijlstra (Intel)" Subject: [PATCH v3 4/5] module: Account for the build time module name mangling References: <20250502141204.500293812@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Sean noted that scripts/Makefile.lib:name-fix-token rule will mangle the module name with s/-/_/g. Since this happens late in the build, only the kernel needs to bother with this, the modpost tool still sees the original name. Reported-by: Sean Christopherson Signed-off-by: Peter Zijlstra (Intel) Tested-by: Sean Christopherson Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Pavlu --- kernel/module/main.c | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) --- a/kernel/module/main.c +++ b/kernel/module/main.c @@ -170,6 +170,30 @@ static inline void add_taint_module(stru } =20 /* + * Like strncmp(), except s/-/_/g as per scripts/Makefile.lib:name-fix-tok= en rule. + */ +static int mod_strncmp(const char *str_a, const char *str_b, size_t n) +{ + for (int i =3D 0; i < n; i++) { + char a =3D str_a[i]; + char b =3D str_b[i]; + int d; + + if (a =3D=3D '-') a =3D '_'; + if (b =3D=3D '-') b =3D '_'; + + d =3D a - b; + if (d) + return d; + + if (!a) + break; + } + + return 0; +} + +/* * A thread that wants to hold a reference to a module only while it * is running can call this to safely exit. */ @@ -1116,7 +1140,7 @@ static bool verify_module_namespace(cons if (*sep) sep++; =20 - if (strncmp(namespace, modname, len) =3D=3D 0 && (glob || len =3D=3D mod= len)) + if (mod_strncmp(namespace, modname, len) =3D=3D 0 && (glob || len =3D=3D= modlen)) return true; } From nobody Wed Dec 17 08:51:14 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F32623A562; Fri, 2 May 2025 14:25:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195909; cv=none; b=hRCzvs7pka8R1NZ8XxPsQc1i6JdIUKEz2hHSsKjcyEzRhPnLH8MnAkooRGL7499y+I6tR+mza7h/y16/xt7UbVd9EZ9Lf3dywGint7rL9vZkAA1z/ZzJKd+o82+qBcamZHROUfsSmGf34k/OsxRErL/6NfkLsEh2PqNYvXEaLlo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746195909; c=relaxed/simple; bh=32/4+qmrGM/xnIFFxa4eHYZW/9RkizeOtr7qMjIWSHk=; h=Message-ID:Date:From:To:Cc:Subject:References:MIME-Version: Content-Type; b=rUGmziZ9Lclw9XQJMqaHkCNdCBG9KPQuL/F/04kI+JYfwqGuEChlru+xPeqRd8nY+rfHStg4CiNFrSncWepqJ+FaZ7sI4l1WPcakWHos11jm8KGNgfavdQ2GlYA8/8RXRWU28Mv6Pbgx8kR4cZn7TcNHPXOTpiM8NQoh3HvjTWo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=CTTHQzKJ; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="CTTHQzKJ" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Content-Type:MIME-Version:References: Subject:Cc:To:From:Date:Message-ID:Sender:Reply-To:Content-Transfer-Encoding: Content-ID:Content-Description:In-Reply-To; bh=WUExRaEQQnsPbw/eE+wP4SXVeUzVT4DYtJ6VYKm6RJk=; b=CTTHQzKJDVw6WuXZTThKx+OeIO eYjbrEQz2JDcu8DFhxMoFGger3VPyg7yimLDQFdspR1RcJD3T0/0NVHn+Jyh5+wMzW0ZqzyLXE4/3 yr2/y1fQ5ilUCDjD8cCZu9V1J293H7ZXpAWOscv3Mn9i7nQvM8SDwknMlaf86Dg7xU1H7nabjBR24 uy/A7J1wpxMD1bNkTIW9WLGkvPEIAGY0RBC46h9PQZP863sduzYxqydV2O4qCR7F55LSK3KHFrnWy Pi3pCJBrHUmFsEpAkihVG27mAOYsgzPpW2e+lCkPBvGymms6D36yWoD+fEuyt8VWSKc+GTs+s6OUp S/OR9ewg==; Received: from 77-249-17-252.cable.dynamic.v4.ziggo.nl ([77.249.17.252] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1uArK2-0000000EzDq-0NeD; Fri, 02 May 2025 14:25:02 +0000 Received: by noisy.programming.kicks-ass.net (Postfix, from userid 0) id 037DD3001AE; Fri, 2 May 2025 16:24:59 +0200 (CEST) Message-ID: <20250502141844.369838967@infradead.org> User-Agent: quilt/0.66 Date: Fri, 02 May 2025 16:12:09 +0200 From: Peter Zijlstra To: mcgrof@kernel.org Cc: x86@kernel.org, hpa@zytor.com, petr.pavlu@suse.com, samitolvanen@google.com, da.gomez@samsung.com, masahiroy@kernel.org, nathan@kernel.org, nicolas@fjasle.eu, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, linux-kbuild@vger.kernel.org, hch@infradead.org, gregkh@linuxfoundation.org, roypat@amazon.co.uk, "Peter Zijlstra (Intel)" Subject: [PATCH v3 5/5] module: Provide EXPORT_SYMBOL_GPL_FOR_MODULES() helper References: <20250502141204.500293812@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Helper macro to more easily limit the export of a symbol to a given list of modules. Eg: EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm"); will limit the use of said function to kvm.ko, any other module trying to use this symbol will refure to load (and get modpost build failures). Requested-by: Masahiro Yamada Requested-by: Christoph Hellwig Signed-off-by: Peter Zijlstra (Intel) Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Pavlu --- Documentation/core-api/symbol-namespaces.rst | 22 ++++++++++++++++++++++ include/linux/export.h | 12 ++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) --- a/Documentation/core-api/symbol-namespaces.rst +++ b/Documentation/core-api/symbol-namespaces.rst @@ -28,6 +28,9 @@ kernel. As of today, modules that make u are required to import the namespace. Otherwise the kernel will, depending= on its configuration, reject loading the module or warn about a missing impor= t. =20 +Additionally, it is possible to put symbols into a module namespace, stric= tly +limiting which modules are allowed to use these symbols. + 2. How to define Symbol Namespaces =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -83,6 +86,22 @@ A second option to define the default na within the corresponding compilation unit before the #include for . Typically it's placed before the first #include statemen= t. =20 +2.3 Using the EXPORT_SYMBOL_GPL_FOR_MODULES() macro +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D + +Symbols exported using this macro are put into a module namespace. This +namespace cannot be imported. + +The macro takes a comma separated list of module names, allowing only those +modules to access this symbol. Simple tail-globs are supported. + +For example: + + EXPORT_SYMBOL_GPL_FOR_MODULES(preempt_notifier_inc, "kvm,kvm-*") + +will limit usage of this symbol to modules whoes name matches the given +patterns. + 3. How to use Symbols exported in Namespaces =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D =20 @@ -154,3 +173,6 @@ Again, ``make nsdeps`` will eventually a You can also run nsdeps for external module builds. A typical usage is:: =20 $ make -C M=3D$PWD nsdeps + +Note: it will happily generate an import statement for the module namespac= e; +which will not work and generates build and runtime failures. --- a/include/linux/export.h +++ b/include/linux/export.h @@ -24,11 +24,17 @@ .long sym #endif =20 -#define ___EXPORT_SYMBOL(sym, license, ns) \ +/* + * LLVM integrated assembler cam merge adjacent string literals (like + * C and GNU-as) passed to '.ascii', but not to '.asciz' and chokes on: + * + * .asciz "MODULE_" "kvm" ; + */ +#define ___EXPORT_SYMBOL(sym, license, ns...) \ .section ".export_symbol","a" ASM_NL \ __export_symbol_##sym: ASM_NL \ .asciz license ASM_NL \ - .asciz ns ASM_NL \ + .ascii ns "\0" ASM_NL \ __EXPORT_SYMBOL_REF(sym) ASM_NL \ .previous =20 @@ -85,4 +91,6 @@ #define EXPORT_SYMBOL_NS(sym, ns) __EXPORT_SYMBOL(sym, "", ns) #define EXPORT_SYMBOL_NS_GPL(sym, ns) __EXPORT_SYMBOL(sym, "GPL", ns) =20 +#define EXPORT_SYMBOL_GPL_FOR_MODULES(sym, mods) __EXPORT_SYMBOL(sym, "GPL= ", "module:" mods) + #endif /* _LINUX_EXPORT_H */