Linux Security Modules development
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Jarkko Sakkinen" <jarkko@kernel.org>,
	"David Howells" <dhowells@redhat.com>
Cc: "Eric Biggers" <ebiggers@kernel.org>,
	"Mimi Zohar" <zohar@linux.ibm.com>,
	"Roberto Sassu" <roberto.sassu@huawei.com>,
	"Dmitry Kasatkin" <dmitry.kasatkin@gmail.com>,
	"Eric Snowberg" <eric.snowberg@oracle.com>,
	"Paul Moore" <paul@paul-moore.com>,
	"James Morris" <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	"open list:KEYS/KEYRINGS" <keyrings@vger.kernel.org>,
	"open list:SECURITY SUBSYSTEM"
	<linux-security-module@vger.kernel.org>
Subject: Re: [RFC PATCH 2/4] KEYS: Pin the backing module for struct key_type
Date: Mon, 02 Nov 2026 12:32:55 +0000	[thread overview]
Message-ID: <sashiko-outbox-193418@kernel.org> (raw)
In-Reply-To: <20261102121518.1197453-3-jarkko@kernel.org>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] KEYS: self-pinning module reference cycle in key_alloc()
--

commit 8793dbc22dd9f6067e0bfcfa9c4b1b437edcf052
Author: Jarkko Sakkinen <jarkko@kernel.org>

KEYS: Pin the backing module for struct key_type

This commit adds an owner field populated with THIS_MODULE to struct key_type
when registering a key type. It pins the module in key_alloc() and
keyring_restrict(), releasing the reference in key_gc_unused_keys() and
keyring_destroy().

> diff --git a/security/keys/key.c b/security/keys/key.c
> index e88548f1a7040..9f1f08fcbe863 100644
> --- a/security/keys/key.c
> +++ b/security/keys/key.c
> @@ -7,6 +7,7 @@
>  
>  #include <linux/export.h>
>  #include <linux/init.h>
> +#include <linux/module.h>
>  #include <linux/poison.h>
>  #include <linux/sched.h>
>  #include <linux/slab.h>
> @@ -234,19 +235,24 @@ struct key *key_alloc(struct key_type *type, const char *desc,
>  
>  	key = ERR_PTR(-EINVAL);
>  	if (!desc || !*desc)
> -		goto error;
> +		goto out;
> +
> +	if (!try_module_get(type->owner)) {
> +		key = ERR_PTR(-ENOENT);
> +		goto out;
> +	}

[Severity: High]
Does pinning type->owner in key_alloc() until key_gc_unused_keys() create a
self-pinning reference cycle for modular key types that cache keys in their
own internal keyrings?

With register_key_type() setting ktype->owner to THIS_MODULE:

include/linux/key-type.h:
#define register_key_type(ktype) __register_key_type(ktype, THIS_MODULE)

every allocated key of that type holds a module reference on its owning
module until the key's reference count drops to zero and it is garbage
collected in key_gc_unused_keys().

However, several loadable modules allocate an internal keyring during module
initialization, link keys of their own key_type into that keyring during
normal operation, and only revoke and release that keyring in their module
exit path:

drivers/nvme/common/keyring.c:nvme_keyring_exit() {
	unregister_key_type(&nvme_tls_psk_key_type);
	key_revoke(nvme_keyring);
	key_put(nvme_keyring);
}

fs/nfs/nfs4idmap.c:nfs_idmap_quit() {
	key_revoke(id_resolver_cache->thread_keyring);
	unregister_key_type(&key_type_id_resolver);
	unregister_key_type(&key_type_id_resolver_legacy);
	put_cred(id_resolver_cache);
}

fs/smb/client/cifsacl.c:exit_cifs_idmap() {
	key_revoke(root_cred->thread_keyring);
	unregister_key_type(&cifs_idmap_key_type);
	put_cred(root_cred);
	cifs_dbg(FYI, "Unregistered %s key type\n", cifs_idmap_key_type.name);
}

net/dns_resolver/dns_key.c:exit_dns_resolver() {
	key_revoke(dns_resolver_cache->thread_keyring);
	unregister_key_type(&key_type_dns_resolver);
	put_cred(dns_resolver_cache);
}

Once any key is linked into one of these internal module keyrings (or added
by userspace via add_key() to a session or user keyring), the key holds a
reference on the module while the module's keyring holds a reference on the
key. Won't this prevent the module reference count from ever reaching zero,
making it impossible to unload these modules unless all cached keys expire or
are manually evicted first?

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261102121518.1197453-1-jarkko@kernel.org?part=2

  parent reply	other threads:[~2026-11-02 12:32 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-02 12:15 [RFC PATCH 0/4] KEYS: sanitize the garbage collector behavior Jarkko Sakkinen
2026-11-02 12:15 ` [RFC PATCH 1/4] KEYS: trusted: Disable sysfs bind attributes for TEE driver Jarkko Sakkinen
2026-11-02 12:27   ` sashiko-bot
2026-11-02 12:15 ` [RFC PATCH 2/4] KEYS: Pin the backing module for struct key_type Jarkko Sakkinen
2026-11-02 12:22   ` Jarkko Sakkinen
2026-11-02 12:32   ` sashiko-bot [this message]
2026-11-02 12:41     ` Jarkko Sakkinen
2026-11-02 12:15 ` [RFC PATCH 3/4] KEYS: Remove key_type_dead Jarkko Sakkinen
2026-11-02 12:21   ` Jarkko Sakkinen
2026-11-02 12:35   ` sashiko-bot
2026-11-02 12:15 ` [RFC PATCH 4/4] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
2026-11-02 12:31   ` Jarkko Sakkinen
2026-11-02 12:32   ` sashiko-bot
2026-11-02 13:34     ` Jarkko Sakkinen
2026-11-02 12:19 ` [RFC PATCH 0/4] KEYS: sanitize the garbage collector behavior Jarkko Sakkinen
2026-11-03  0:03 ` Jarkko Sakkinen
2026-11-03  0:42   ` Jarkko Sakkinen

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=sashiko-outbox-193418@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=dhowells@redhat.com \
    --cc=dmitry.kasatkin@gmail.com \
    --cc=ebiggers@kernel.org \
    --cc=eric.snowberg@oracle.com \
    --cc=jarkko@kernel.org \
    --cc=jmorris@namei.org \
    --cc=keyrings@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=roberto.sassu@huawei.com \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.com \
    --cc=zohar@linux.ibm.com \
    /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