Skip to contentSkip to conversation

CIFS: fix deadlock in cached root handling

PATCH In review Aurelien Aptel 3 participants 3 messages Jul 17, 2019 – Jul 18, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 CIFS: fix deadlock in cached root handling

Prevent deadlock between open_shroot() and cifs_mark_open_files_invalid() by releasing the lock before entering SMB2_open, taking it again after and checking if we still need to use the result.

Fixes 3d4ef9a15343 ("smb3: fix redundant opens on root")

---

this is the for-next version of the patch I sent in the thread
https://lore.kernel.org/linux-cifs/[email protected]/T/#u

 fs/cifs/smb2ops.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +45 -1
index 6cb4def11ebe..8202c996b55e 100644
@@ -694,8 +694,51 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
 
 	smb2_set_related(&rqst[1]);
 
+	/*
+	 * We do not hold the lock for the open because in case
+	 * SMB2_open needs to reconnect, it will end up calling
+	 * cifs_mark_open_files_invalid() which takes the lock again
+	 * thus causing a deadlock
+	 */
+
+	mutex_unlock(&tcon->crfid.fid_mutex);
 	rc = compound_send_recv(xid, ses, flags, 2, rqst,
 				resp_buftype, rsp_iov);
+	mutex_lock(&tcon->crfid.fid_mutex);
+
+	/*
+	 * Now we need to check again as the cached root might have
+	 * been successfully re-opened from a concurrent process
+	 */
+
+	if (tcon->crfid.is_valid) {
+		/* work was already done */
+
+		/* stash fids for close() later */
+		struct cifs_fid fid = {
+			.persistent_fid = pfid->persistent_fid,
+			.volatile_fid = pfid->volatile_fid,
+		};
+
+		/*
+		 * caller expects this func to set pfid to a valid
+		 * cached root, so we copy the existing one and get a
+		 * reference.
+		 */
+		memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
+		kref_get(&tcon->crfid.refcount);
+
+		mutex_unlock(&tcon->crfid.fid_mutex);
+
+		if (rc == 0) {
+			/* close extra handle outside of crit sec */
+			SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
+		}
+	       goto oshr_free;
+	}
+
+	/* Cached root is still invalid, continue normaly */
+
 	if (rc)
 		goto oshr_exit;
 
@@ -729,8 +772,9 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
 				(char *)&tcon->crfid.file_all_info))
 		tcon->crfid.file_all_info_is_valid = 1;
 
- oshr_exit:
+oshr_exit:
 	mutex_unlock(&tcon->crfid.fid_mutex);
+oshr_free:
 	SMB2_open_free(&rqst[0]);
 	SMB2_query_info_free(&rqst[1]);
 	free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
Signature · 1 line
2.16.4
Reviewed-by on v1 1/1
#1 lore raw

Looks good.

--
Best regards,
Pavel Shilovsky

ср, 17 июл. 2019 г. в 03:46, Aurelien Aptel <[email protected]>:

Quoted 88 lines

Prevent deadlock between open_shroot() and cifs_mark_open_files_invalid() by releasing the lock before entering SMB2_open, taking it again after and checking if we still need to use the result.

Fixes 3d4ef9a15343 ("smb3: fix redundant opens on root")

---

this is the for-next version of the patch I sent in the thread
https://lore.kernel.org/linux-cifs/[email protected]/T/#u

 fs/cifs/smb2ops.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +45 -2
index 6cb4def11ebe..8202c996b55e 100644
@@ -694,8 +694,51 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
 
        smb2_set_related(&rqst[1]);
 
+       /*
+        * We do not hold the lock for the open because in case
+        * SMB2_open needs to reconnect, it will end up calling
+        * cifs_mark_open_files_invalid() which takes the lock again
+        * thus causing a deadlock
+        */
+
+       mutex_unlock(&tcon->crfid.fid_mutex);
        rc = compound_send_recv(xid, ses, flags, 2, rqst,
                                resp_buftype, rsp_iov);
+       mutex_lock(&tcon->crfid.fid_mutex);
+
+       /*
+        * Now we need to check again as the cached root might have
+        * been successfully re-opened from a concurrent process
+        */
+
+       if (tcon->crfid.is_valid) {
+               /* work was already done */
+
+               /* stash fids for close() later */
+               struct cifs_fid fid = {
+                       .persistent_fid = pfid->persistent_fid,
+                       .volatile_fid = pfid->volatile_fid,
+               };
+
+               /*
+                * caller expects this func to set pfid to a valid
+                * cached root, so we copy the existing one and get a
+                * reference.
+                */
+               memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
+               kref_get(&tcon->crfid.refcount);
+
+               mutex_unlock(&tcon->crfid.fid_mutex);
+
+               if (rc == 0) {
+                       /* close extra handle outside of crit sec */
+                       SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
+               }
+              goto oshr_free;
+       }
+
+       /* Cached root is still invalid, continue normaly */
+
        if (rc)
                goto oshr_exit;
 
@@ -729,8 +772,9 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
                                (char *)&tcon->crfid.file_all_info))
                tcon->crfid.file_all_info_is_valid = 1;
 
- oshr_exit:
+oshr_exit:
        mutex_unlock(&tcon->crfid.fid_mutex);
+oshr_free:
        SMB2_open_free(&rqst[0]);
        SMB2_query_info_free(&rqst[1]);
        free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
--

2.16.4

#2 lore raw

added cc: stable and made minor cleanup of missing tab

Quoted 98 lines · Pavel Shilovsky

Looks good.

--
Best regards,
Pavel Shilovsky

ср, 17 июл. 2019 г. в 03:46, Aurelien Aptel <[email protected]>:

Quoted 88 lines

Prevent deadlock between open_shroot() and cifs_mark_open_files_invalid() by releasing the lock before entering SMB2_open, taking it again after and checking if we still need to use the result.

Fixes 3d4ef9a15343 ("smb3: fix redundant opens on root")

---

this is the for-next version of the patch I sent in the thread
https://lore.kernel.org/linux-cifs/[email protected]/T/#u

 fs/cifs/smb2ops.c | 46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +45 -2
index 6cb4def11ebe..8202c996b55e 100644
@@ -694,8 +694,51 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
 
        smb2_set_related(&rqst[1]);
 
+       /*
+        * We do not hold the lock for the open because in case
+        * SMB2_open needs to reconnect, it will end up calling
+        * cifs_mark_open_files_invalid() which takes the lock again
+        * thus causing a deadlock
+        */
+
+       mutex_unlock(&tcon->crfid.fid_mutex);
        rc = compound_send_recv(xid, ses, flags, 2, rqst,
                                resp_buftype, rsp_iov);
+       mutex_lock(&tcon->crfid.fid_mutex);
+
+       /*
+        * Now we need to check again as the cached root might have
+        * been successfully re-opened from a concurrent process
+        */
+
+       if (tcon->crfid.is_valid) {
+               /* work was already done */
+
+               /* stash fids for close() later */
+               struct cifs_fid fid = {
+                       .persistent_fid = pfid->persistent_fid,
+                       .volatile_fid = pfid->volatile_fid,
+               };
+
+               /*
+                * caller expects this func to set pfid to a valid
+                * cached root, so we copy the existing one and get a
+                * reference.
+                */
+               memcpy(pfid, tcon->crfid.fid, sizeof(*pfid));
+               kref_get(&tcon->crfid.refcount);
+
+               mutex_unlock(&tcon->crfid.fid_mutex);
+
+               if (rc == 0) {
+                       /* close extra handle outside of crit sec */
+                       SMB2_close(xid, tcon, fid.persistent_fid, fid.volatile_fid);
+               }
+              goto oshr_free;
+       }
+
+       /* Cached root is still invalid, continue normaly */
+
        if (rc)
                goto oshr_exit;
 
@@ -729,8 +772,9 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
                                (char *)&tcon->crfid.file_all_info))
                tcon->crfid.file_all_info_is_valid = 1;
 
- oshr_exit:
+oshr_exit:
        mutex_unlock(&tcon->crfid.fid_mutex);
+oshr_free:
        SMB2_open_free(&rqst[0]);
        SMB2_query_info_free(&rqst[1]);
        free_rsp_buf(resp_buftype[0], rsp_iov[0].iov_base);
--

2.16.4

Signature · 3 lines
Thanks,

Steve