public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Sami Tolvanen <samitolvanen@google.com>
To: Nicholas Sielicki <linux@opensource.nslick.com>
Cc: Luis Chamberlain <mcgrof@kernel.org>,
	Petr Pavlu <petr.pavlu@suse.com>,
	Daniel Gomez <da.gomez@kernel.org>,
	Aaron Tomlin <atomlin@atomlin.com>,
	Matthias Maennich <maennich@google.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Jonathan Corbet <corbet@lwn.net>,
	Randy Dunlap <rdunlap@infradead.org>,
	linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] module: expose imported namespaces via sysfs
Date: Thu, 5 Mar 2026 22:32:45 +0000	[thread overview]
Message-ID: <20260305223245.GA732271@google.com> (raw)
In-Reply-To: <20260108044710.33036-1-linux@opensource.nslick.com>

On Wed, Jan 07, 2026 at 10:47:09PM -0600, Nicholas Sielicki wrote:
>  
> +static ssize_t show_modinfo_import_ns(const struct module_attribute *mattr,
> +				      struct module_kobject *mk, char *buffer)
> +{
> +	return sysfs_emit(buffer, "%s\n", mk->mod->imported_namespaces);
> +}
> +
> +static int modinfo_import_ns_exists(struct module *mod)
> +{
> +	return mod->imported_namespaces != NULL;
> +}
> +
> +static const struct module_attribute modinfo_import_ns = {
> +	.attr = { .name = "import_ns", .mode = 0444 },
> +	.show = show_modinfo_import_ns,
> +	.test = modinfo_import_ns_exists,
> +};
> +

Don't we need a .setup function that initializes mod->imported_namespaces
to NULL? Currently, if setup_modinfo returns an error, the pointer remains
initialized to whatever value we read from .gnu.linkonce.this_module, and
we'll pass that arbitrary pointer to kfree.

This isn't normally a problem since modpost zero-initializes the field, but
we don't want to rely on userspace to initialize our pointers.

Also, define .free to release the buffer instead of adding a direct call
to free_modinfo.

>  static struct {
>  	char name[MODULE_NAME_LEN];
>  	char taints[MODULE_FLAGS_BUF_SIZE];
> @@ -1058,6 +1075,7 @@ const struct module_attribute *const modinfo_attrs[] = {
>  	&module_uevent,
>  	&modinfo_version,
>  	&modinfo_srcversion,
> +	&modinfo_import_ns,
>  	&modinfo_initstate,
>  	&modinfo_coresize,
>  #ifdef CONFIG_ARCH_WANTS_MODULES_DATA_IN_VMALLOC
> @@ -1753,11 +1771,48 @@ static void module_license_taint_check(struct module *mod, const char *license)
>  	}
>  }
>  
> +static int copy_modinfo_import_ns(struct module *mod, struct load_info *info)
> +{
> +	char *ns;
> +	size_t len, total_len = 0;
> +	char *buf, *p;
> +
> +	for_each_modinfo_entry(ns, info, "import_ns")
> +		total_len += strlen(ns) + 1;
> +
> +	if (!total_len) {
> +		mod->imported_namespaces = NULL;
> +		return 0;
> +	}
> +
> +	buf = kmalloc(total_len, GFP_KERNEL);
> +	if (!buf)
> +		return -ENOMEM;

For example, if kmalloc fails, mod->imported_namespaces isn't initialized.

> +
> +	p = buf;
> +	for_each_modinfo_entry(ns, info, "import_ns") {
> +		len = strlen(ns);
> +		memcpy(p, ns, len);
> +		p += len;
> +		*p++ = '\n';
> +	}
> +	/* Replace trailing newline with null terminator. */
> +	*(p - 1) = '\0';
> +
> +	mod->imported_namespaces = buf;
> +	return 0;
> +}
> +
> +static void free_modinfo_import_ns(struct module *mod)
> +{
> +	kfree(mod->imported_namespaces);

mod->imported_namespaces = NULL;

> +}
> +
>  static int setup_modinfo(struct module *mod, struct load_info *info)
>  {
>  	const struct module_attribute *attr;
>  	char *imported_namespace;
> -	int i;
> +	int i, err;
>  
>  	for (i = 0; (attr = modinfo_attrs[i]); i++) {
>  		if (attr->setup)
> @@ -1776,6 +1831,10 @@ static int setup_modinfo(struct module *mod, struct load_info *info)
>  		}
>  	}

Also setup_modinfo can fail before copy_modinfo_import_ns is even
called.

> +	err = copy_modinfo_import_ns(mod, info);
> +	if (err)
> +		return err;
> +

Sami

      parent reply	other threads:[~2026-03-05 22:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-08  4:47 [PATCH 1/2] module: expose imported namespaces via sysfs Nicholas Sielicki
2026-01-08  4:47 ` [PATCH 2/2] docs: symbol-namespaces: mention sysfs attribute Nicholas Sielicki
2026-02-13 17:44 ` [PATCH 1/2] module: expose imported namespaces via sysfs Sami Tolvanen
2026-02-13 20:25   ` Nicholas Sielicki
2026-03-05 22:32 ` Sami Tolvanen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260305223245.GA732271@google.com \
    --to=samitolvanen@google.com \
    --cc=atomlin@atomlin.com \
    --cc=corbet@lwn.net \
    --cc=da.gomez@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=linux@opensource.nslick.com \
    --cc=maennich@google.com \
    --cc=mcgrof@kernel.org \
    --cc=peterz@infradead.org \
    --cc=petr.pavlu@suse.com \
    --cc=rdunlap@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox