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>,
	keyrings@vger.kernel.org, "Paulo Alcantara" <pc@manguebit.org>,
	"Namjae Jeon" <linkinjeon@kernel.org>,
	"Ronnie Sahlberg" <ronniesahlberg@gmail.com>,
	"Shyam Prasad N" <sprasad@microsoft.com>,
	"Tom Talpey" <tom@talpey.com>,
	"Bharath SM" <bharathsm@microsoft.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>,
	"moderated list:COMMON INTERNET FILE SYSTEM CLIENT (CIFS and
	SMB3)" <samba-technical@lists.samba.org>,
	"open list:SECURITY SUBSYSTEM"
	<linux-security-module@vger.kernel.org>
Subject: Re: [RFC PATCH v2 05/11] smb: client: Clear idmap/SPNEGO caches when the last session is unloaded
Date: Wed, 04 Nov 2026 03:03:04 +0000	[thread overview]
Message-ID: <sashiko-outbox-196569@kernel.org> (raw)
In-Reply-To: <20261103030226.2685251-6-jarkko@kernel.org>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] smb: client: key_gc_flush() in sesInfoFree() on WQ_MEM_RECLAIM workqueues
- [Medium] smb: client: WARN_ON() on keyring_clear() failure in cache clear helpers
- [Medium] smb: client: premature SPNEGO cache clear in dfs_mount_share()
--

commit 1af6c60c9338c6ee58b722558872d2fff3e07106
Author: Jarkko Sakkinen <jarkko@kernel.org>

smb: client: Clear idmap/SPNEGO caches when the last session is unloaded

This commit serializes CIFS session count transitions with a mutex and clears
the idmap and SPNEGO cache keyrings, flushing key garbage collection when the
last SMB session is freed.

> diff --git a/fs/smb/client/cifs_spnego.c b/fs/smb/client/cifs_spnego.c
> index 44c4072756804..3bd272c7d9fe6 100644
> --- a/fs/smb/client/cifs_spnego.c
> +++ b/fs/smb/client/cifs_spnego.c
> @@ -242,6 +242,14 @@ init_cifs_spnego(void)
>  	return ret;
>  }
>  
> +void cifs_spnego_clear_cache(void)
> +{
> +	int ret;
> +
> +	ret = keyring_clear(spnego_cred->thread_keyring);
> +	WARN_ON(ret);
> +}
> +
>  void
>  exit_cifs_spnego(void)
>  {
> diff --git a/fs/smb/client/cifsacl.c b/fs/smb/client/cifsacl.c
> index 346899da91eb2..7504cedf64177 100644
> --- a/fs/smb/client/cifsacl.c
> +++ b/fs/smb/client/cifsacl.c
> @@ -590,6 +590,14 @@ init_cifs_idmap(void)
>  	return ret;
>  }
>  
> +void cifs_idmap_clear_cache(void)
> +{
> +	int ret;
> +
> +	ret = keyring_clear(root_cred->thread_keyring);
> +	WARN_ON(ret);
> +}

[Severity: Medium]
Can keyring_clear() in cifs_spnego_clear_cache() and cifs_idmap_clear_cache()
trigger a WARN_ON() on a normal memory allocation failure?

When sesInfoFree() frees the last SMB session while keys are cached in
spnego_cred->thread_keyring or root_cred->thread_keyring, keyring_clear()
calls assoc_array_clear():

security/keys/keyring.c:keyring_clear()
  assoc_array_clear(&keyring->keys, &keyring_assoc_array_ops)
    kzalloc_obj(struct assoc_array_edit)

If that GFP_KERNEL allocation fails under memory pressure, memcg limits, or
fault injection, assoc_array_clear() returns ERR_PTR(-ENOMEM) and
keyring_clear() returns -ENOMEM, causing WARN_ON(ret) to fire (and panicking
systems with panic_on_warn enabled).

Other commits in this series (such as nfs_idmap_delete() in
fs/nfs/nfs4idmap.c and dns_resolver_cache_put() in net/dns_resolver/dns_key.c)
log a ratelimited warning with pr_warn_ratelimited() and skip key_gc_flush()
when keyring_clear() fails. Should cifs_spnego_clear_cache() and
cifs_idmap_clear_cache() handle errors the same way instead of using
WARN_ON()?

> diff --git a/fs/smb/client/misc.c b/fs/smb/client/misc.c
> index ec28f102ea08a..4503434942f97 100644
> --- a/fs/smb/client/misc.c
> +++ b/fs/smb/client/misc.c

[ ... ]

> @@ -115,6 +122,14 @@ sesInfoFree(struct cifs_ses *buf_to_free)
>  		kref_put(&iface->refcount, release_iface);
>  	spin_unlock(&buf_to_free->iface_lock);
>  	kfree_sensitive(buf_to_free);
> +
> +	mutex_lock(&cifs_session_mutex);
> +	if (atomic_dec_and_test(&cifs_ses_alloc_count)) {
> +		cifs_idmap_clear_cache();
> +		cifs_spnego_clear_cache();

[Severity: Medium]
When mounting a DFS share with no existing SMB sessions active, does clearing
the caches whenever cifs_ses_alloc_count drops to zero prematurely evict the
SPNEGO key just obtained during the mount?

In fs/smb/client/dfs.c:dfs_mount_share(), when !ctx->dfs_conn initially, the
mount path sets up a session (populating spnego_cred->thread_keyring via an
upcall), queries the DFS referral, and then tears down that initial connection
before allocating the new DFS session:

dfs_mount_share()
  get_session(mnt_ctx, NULL)        /* cifs_ses_alloc_count: 0 -> 1 */
  dfs_get_referral(...)
  if (!ctx->dfs_conn) {
      ctx->dfs_conn = true;
      cifs_mount_put_conns(mnt_ctx) /* cifs_ses_alloc_count: 1 -> 0 */
      rc = get_session(mnt_ctx, NULL);
  }

Because cifs_mount_put_conns() calls cifs_put_smb_ses() ->
__cifs_put_smb_ses() -> sesInfoFree() before the replacement session is
allocated, cifs_ses_alloc_count transiently reaches 0. This wipes the SPNEGO
keyring and stalls in key_gc_flush(), forcing the immediately following
get_session() call to perform a redundant userspace SPNEGO upcall.

> +		key_gc_flush();
> +	}
> +	mutex_unlock(&cifs_session_mutex);
>  }

[Severity: High]
What happens if the last reference to struct cifs_ses is dropped from a
workqueue created with WQ_MEM_RECLAIM, such as cifsiod_wq?

In init_cifs(), cifsiod_wq is allocated with WQ_MEM_RECLAIM, and
cifs_kill_sb() does not flush cifsiod_wq before calling cifs_umount().
Background work items running on cifsiod_wq can hold session or tcon
references and drop the final session reference if unmount completes
concurrently:

fs/smb/client/connect.c:mchan_mount_work_fn() [on cifsiod_wq]
  mchan_mount_free()
    cifs_put_smb_ses()
      __cifs_put_smb_ses()
        sesInfoFree()

or:

fs/smb/client/smb2pdu.c:smb2_reconnect_server() [on cifsiod_wq]
  cifs_put_smb_ses() / cifs_put_tcon()
    __cifs_put_smb_ses()
      sesInfoFree()

When sesInfoFree() clears non-empty keyrings and calls key_gc_flush():

security/keys/gc.c:key_gc_flush() {
	do {
		rcu_barrier();
	} while (flush_work(&key_gc_work));
}

rcu_barrier() runs assoc_array_rcu_cleanup() -> keyring_free_object() ->
key_put(), which schedules key_gc_work on system_dfl_wq (a workqueue without
WQ_MEM_RECLAIM). Calling flush_work(&key_gc_work) from a WQ_MEM_RECLAIM worker
on cifsiod_wq will then trigger the WARN_ONCE() in check_flush_dependency()
for flushing a !WQ_MEM_RECLAIM workqueue from a memory-reclaim worker.

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

  reply	other threads:[~2026-11-04  3:03 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-03  3:02 [RFC PATCH v2 00/11] KEYS: Sanitize the garbage collector Jarkko Sakkinen
2026-11-03  3:02 ` [RFC PATCH v2 01/11] KEYS: Add key_gc_flush() for draining RCU callbacks Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 02/11] nvme-keyring: Add a missing key_put() call to nvme_tls_key_lookup() Jarkko Sakkinen
2026-11-03  5:33   ` Christoph Hellwig
2026-11-03 12:29     ` Jarkko Sakkinen
2026-11-04  9:04       ` Christoph Hellwig
2026-11-04 14:43         ` Jarkko Sakkinen
2026-11-04 15:53           ` Keith Busch
2026-11-04  3:03   ` sashiko-bot
2026-11-04  7:45   ` Hannes Reinecke
2026-11-04 14:43     ` Jarkko Sakkinen
2026-11-03  3:02 ` [RFC PATCH v2 03/11] dns_resolver: Clear .dns_resolver cache when the last client unloads Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 04/11] NFSv4: Clear idmap keyring " Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 05/11] smb: client: Clear idmap/SPNEGO caches when the last session is unloaded Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot [this message]
2026-11-03  3:02 ` [RFC PATCH v2 06/11] s390/cert_store: Track subsystem keys instead of re-registering key_type Jarkko Sakkinen
2026-11-03 12:06   ` Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 07/11] KEYS: trusted: TEE: Disable sysfs bind attributes Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 08/11] KEYS: trusted: TEE: Synchronize session lifetime Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 09/11] KEYS: Pin the backing kernel module for a key type Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 10/11] KEYS: Remove key_type_dead Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:02 ` [RFC PATCH v2 11/11] KEYS: key_garbage_collector(): process in chunks Jarkko Sakkinen
2026-11-04  3:03   ` sashiko-bot
2026-11-03  3:10 ` [RFC PATCH v2 00/11] KEYS: Sanitize the garbage collector Jarkko Sakkinen
2026-11-03 12:32   ` Jarkko Sakkinen
2026-11-03 10:03 ` David Howells
2026-11-03 12:03   ` Jarkko Sakkinen
2026-11-03 14:00     ` David Howells
2026-11-03 15:06       ` Jarkko Sakkinen
2026-11-03 15:32         ` David Howells
2026-11-03 16:58           ` Jarkko Sakkinen
2026-11-04  0:30             ` 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-196569@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bharathsm@microsoft.com \
    --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=linkinjeon@kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=paul@paul-moore.com \
    --cc=pc@manguebit.org \
    --cc=roberto.sassu@huawei.com \
    --cc=ronniesahlberg@gmail.com \
    --cc=samba-technical@lists.samba.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=serge@hallyn.com \
    --cc=sprasad@microsoft.com \
    --cc=tom@talpey.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