smb: client: fix potential deadlock when releasing mids
authorSteve French <stfrench@microsoft.com>
Fri, 26 Jan 2024 00:59:59 +0000 (18:59 -0600)
committerSteve French <stfrench@microsoft.com>
Sat, 27 Jan 2024 06:14:27 +0000 (00:14 -0600)
All release_mid() callers seem to hold a reference of @mid so there is
no need to call kref_put(&mid->refcount, __release_mid) under
@server->mid_lock spinlock.  If they don't, then an use-after-free bug
would have occurred anyways.

By getting rid of such spinlock also fixes a potential deadlock as
shown below

CPU 0                                CPU 1
------------------------------------------------------------------
cifs_demultiplex_thread()            cifs_debug_data_proc_show()
 release_mid()
  spin_lock(&server->mid_lock);
                                     spin_lock(&cifs_tcp_ses_lock)
      spin_lock(&server->mid_lock)
  __release_mid()
   smb2_find_smb_tcon()
    spin_lock(&cifs_tcp_ses_lock) *deadlock*

Cc: stable@vger.kernel.org
Signed-off-by: Paulo Alcantara (SUSE) <pc@manguebit.com>
Signed-off-by: Steve French <stfrench@microsoft.com>
fs/cifs/cifsproto.h
fs/cifs/smb2misc.c
fs/cifs/transport.c

index 1b18519355be339016bba0ad4f23c26fdf3c0fc3..2a7321a991bfdccf0d6ac40c1cce028cfa537fe0 100644 (file)
@@ -81,7 +81,7 @@ extern char *cifs_build_path_to_root(struct smb3_fs_context *ctx,
 extern char *build_wildcard_path_from_dentry(struct dentry *direntry);
 char *cifs_build_devname(char *nodename, const char *prepath);
 extern void delete_mid(struct mid_q_entry *mid);
-extern void release_mid(struct mid_q_entry *mid);
+void __release_mid(struct kref *refcount);
 extern void cifs_wake_up_task(struct mid_q_entry *mid);
 extern int cifs_handle_standard(struct TCP_Server_Info *server,
                                struct mid_q_entry *mid);
@@ -742,4 +742,9 @@ static inline bool dfs_src_pathname_equal(const char *s1, const char *s2)
        return true;
 }
 
+static inline void release_mid(struct mid_q_entry *mid)
+{
+       kref_put(&mid->refcount, __release_mid);
+}
+
 #endif                 /* _CIFSPROTO_H */
index 25f7cd6f23d64cfa36e88982db4965a94eb816da..32dfa0f7a78c30013f9d5ead8c5f684b295183ec 100644 (file)
@@ -787,7 +787,7 @@ __smb2_handle_cancelled_cmd(struct cifs_tcon *tcon, __u16 cmd, __u64 mid,
 {
        struct close_cancelled_open *cancelled;
 
-       cancelled = kzalloc(sizeof(*cancelled), GFP_ATOMIC);
+       cancelled = kzalloc(sizeof(*cancelled), GFP_KERNEL);
        if (!cancelled)
                return -ENOMEM;
 
index 2841cb1c51b4b0caf8ae35e95c5edb985cd02445..e6de470c4536c10eb3c71a01e6bd7340e067ed56 100644 (file)
@@ -76,7 +76,7 @@ alloc_mid(const struct smb_hdr *smb_buffer, struct TCP_Server_Info *server)
        return temp;
 }
 
-static void __release_mid(struct kref *refcount)
+void __release_mid(struct kref *refcount)
 {
        struct mid_q_entry *midEntry =
                        container_of(refcount, struct mid_q_entry, refcount);
@@ -156,15 +156,6 @@ static void __release_mid(struct kref *refcount)
        mempool_free(midEntry, cifs_mid_poolp);
 }
 
-void release_mid(struct mid_q_entry *mid)
-{
-       struct TCP_Server_Info *server = mid->server;
-
-       spin_lock(&server->mid_lock);
-       kref_put(&mid->refcount, __release_mid);
-       spin_unlock(&server->mid_lock);
-}
-
 void
 delete_mid(struct mid_q_entry *mid)
 {