mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
To: Paulo Alcantara <pc@manguebit.org>
Cc: Ronnie Sahlberg <ronniesahlberg@gmail.com>,
	Shyam Prasad N <sprasad@microsoft.com>,
	Tom Talpey <tom@talpey.com>, Bharath SM <bharathsm@microsoft.com>,
	Allen Pais <apais@microsoft.com>,
	Rachel Menge <rachelmenge@linux.microsoft.com>,
	Hardik Garg <hargar@linux.microsoft.com>,
	Siddharth Chintamaneni <sidchintamaneni@gmail.com>,
	Gillian Laming <glaming@microsoft.com>,
	linux-cifs@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] smb: client: fix dentry leak and data race in open_cached_dir()
Date: Mon, 30 Nov 2026 18:35:50 +0000	[thread overview]
Message-ID: <20261130183550.150287-1-sidchintamaneni@gmail.com> (raw)

find_or_create_cached_dir() adds a newly allocated cached_fid entry onto
cfids->entries under cfids->cfid_list_lock with cfid->dentry == NULL and
cfid->on_list == true, after which open_cached_dir() drops
cfid_list_lock to resolve the dentry via path_to_dentry() and assigns
cfid->dentry = dentry without re-acquiring cfid_list_lock.

This unlocked assignment creates two concurrency issues:

1. If invalidate_all_cached_dirs() runs while open_cached_dir() is in
   path_to_dentry(), it moves cfid from cfids->entries to cfids->dying,
   clears cfid->on_list, and schedules cfids_laundromat_worker(). When
   cfids_laundromat_worker() pops cfid from cfids->dying and executes
   dput(cfid->dentry) followed by cfid->dentry = NULL concurrently with
   open_cached_dir() assigning cfid->dentry = dentry, the worker can
   call dput(NULL) and then overwrite the newly assigned cfid->dentry
   with NULL:

  Thread 1 (open_cached_dir)          Thread 2 (invalidate / laundromat)
  --------------------------          ----------------------------------
  find_or_create_cached_dir()
    list_add(&cfid->entry, &entries)
    cfid->on_list = true
  spin_unlock(&cfid_list_lock)
  dentry = path_to_dentry(...)        invalidate_all_cached_dirs()
                                        list_move(&cfid->entry, &dying)
                                        cfid->on_list = false
                                      cfids_laundromat_worker()
                                        dput(cfid->dentry) [sees NULL]
  cfid->dentry = dentry
                                        cfid->dentry = NULL [overwrites]
  close_cached_dir(cfid)
    smb2_close_cached_fid()
      [sees cfid->dentry == NULL]
      -> dentry leaked, triggering BUG: Dentry still in use on unmount!

2. Reader paths walking cfids->entries or cfids->dying under
   cfids->cfid_list_lock (such as close_all_cached_dirs() and
   open_cached_dir_by_dentry()) read and dereference cfid->dentry
   concurrently with the unlocked write in open_cached_dir().

Fix this by holding cfids->cfid_list_lock when checking parent_cfid and
initializing cfid->dentry and cfid->tcon in open_cached_dir(), and
aborting with dput(dentry) if !cfid->on_list indicates the entry was
already invalidated while cfid_list_lock was dropped.

Fixes: 5c86919455c1 ("smb: client: fix use-after-free in smb2_query_info_compound()")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
---
 fs/smb/client/cached_dir.c | 39 ++++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 16 deletions(-)

diff --git a/fs/smb/client/cached_dir.c b/fs/smb/client/cached_dir.c
index 2c6243cbe10e4..b9b6e2183d17d 100644
--- a/fs/smb/client/cached_dir.c
+++ b/fs/smb/client/cached_dir.c
@@ -227,28 +227,35 @@ int open_cached_dir(unsigned int xid, struct cifs_tcon *tcon,
 			rc = -ENOENT;
 			goto out;
 		}
-		if (dentry->d_parent && server->dialect >= SMB30_PROT_ID) {
-			struct cached_fid *parent_cfid;
-
-			spin_lock(&cfids->cfid_list_lock);
-			list_for_each_entry(parent_cfid, &cfids->entries, entry) {
-				if (parent_cfid->dentry == dentry->d_parent) {
-					cifs_dbg(FYI, "found a parent cached file handle\n");
-					if (is_valid_cached_dir(parent_cfid)) {
-						lease_flags
-							|= SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
-						memcpy(pfid->parent_lease_key,
-						       parent_cfid->fid.lease_key,
-						       SMB2_LEASE_KEY_SIZE);
-					}
-					break;
+	}
+
+	spin_lock(&cfids->cfid_list_lock);
+	if (!cfid->on_list) {
+		spin_unlock(&cfids->cfid_list_lock);
+		dput(dentry);
+		rc = -ENOENT;
+		goto out;
+	}
+	if (npath[0] && dentry->d_parent && server->dialect >= SMB30_PROT_ID) {
+		struct cached_fid *parent_cfid;
+
+		list_for_each_entry(parent_cfid, &cfids->entries, entry) {
+			if (parent_cfid->dentry == dentry->d_parent) {
+				cifs_dbg(FYI, "found a parent cached file handle\n");
+				if (is_valid_cached_dir(parent_cfid)) {
+					lease_flags |=
+						SMB2_LEASE_FLAG_PARENT_LEASE_KEY_SET_LE;
+					memcpy(pfid->parent_lease_key,
+					       parent_cfid->fid.lease_key,
+					       SMB2_LEASE_KEY_SIZE);
 				}
+				break;
 			}
-			spin_unlock(&cfids->cfid_list_lock);
 		}
 	}
 	cfid->dentry = dentry;
 	cfid->tcon = tcon;
+	spin_unlock(&cfids->cfid_list_lock);
 
 	/*
 	 * We do not hold the lock for the open because in case
-- 
2.43.0


                 reply	other threads:[~2026-11-30 18:35 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261130183550.150287-1-sidchintamaneni@gmail.com \
    --to=sidchintamaneni@gmail.com \
    --cc=apais@microsoft.com \
    --cc=bharathsm@microsoft.com \
    --cc=glaming@microsoft.com \
    --cc=hargar@linux.microsoft.com \
    --cc=linux-cifs@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=pc@manguebit.org \
    --cc=rachelmenge@linux.microsoft.com \
    --cc=ronniesahlberg@gmail.com \
    --cc=sprasad@microsoft.com \
    --cc=tom@talpey.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

all inboxes | Powered by JetHome®