Skip to contentSkip to conversation

cifs: fix kref underflow in close_shroot()

PATCH In review Ronnie Sahlberg 3 participants 3 messages Mar 27, 2019 – Mar 28, 2019

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

PATCH 1/1 cifs: fix kref underflow in close_shroot()

Fix a bug where we used to not initialize the cached fid structure at all in open_shroot() if the open was successful but we did not get a lease. This would leave the structure uninitialized and later when we close the handle we would in close_shroot() try to kref_put() an uninitialized refcount.

Fix this by always initializing this structure when we are about to return 0/success but make the extra kref_get() on the refcount conditional to whether we got a lease or not.
This extra get() is only used to hold the structure until we get a lease break from the server at which point we will kref_put() it during lease processing.

---
 fs/cifs/smb2ops.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fs/cifs/smb2ops.c +14 -8
index 1022a3771e14..4f0fb53cd0de 100644
@@ -724,14 +724,6 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
 	else
 		goto oshr_exit;
 
-
-	memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
-	tcon->crfid.tcon = tcon;
-	tcon->crfid.is_valid = true;
-	kref_init(&tcon->crfid.refcount);
-	kref_get(&tcon->crfid.refcount);
-
-
 	qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
 	if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
 		goto oshr_exit;
@@ -745,6 +737,20 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
 	tcon->crfid.file_all_info_is_valid = 1;
 
  oshr_exit:
+	/*
+	 * If we return 0/success then we must also initialize this struct.
+	 * If we got a lease we need to take out an extra get() to keep
+	 * the structure around until we receive the actual lease break.
+	 */
+	if (rc == 0) {
+		memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
+		tcon->crfid.tcon = tcon;
+		tcon->crfid.is_valid = true;
+		kref_init(&tcon->crfid.refcount);
+		if (o_rsp && o_rsp->OplockLevel == SMB2_OPLOCK_LEVEL_LEASE)
+			kref_get(&tcon->crfid.refcount);
+	}
+
 	mutex_unlock(&tcon->crfid.fid_mutex);
 	SMB2_open_free(&rqst[0]);
 	SMB2_query_info_free(&rqst[1]);
Signature · 1 line
2.13.6
#1 lore raw

ср, 27 мар. 2019 г. в 16:58, Ronnie Sahlberg <[email protected]>:

Quoted 38 lines

Fix a bug where we used to not initialize the cached fid structure at all in open_shroot() if the open was successful but we did not get a lease. This would leave the structure uninitialized and later when we close the handle we would in close_shroot() try to kref_put() an uninitialized refcount.

Fix this by always initializing this structure when we are about to return 0/success but make the extra kref_get() on the refcount conditional to whether we got a lease or not.
This extra get() is only used to hold the structure until we get a lease break from the server at which point we will kref_put() it during lease processing.

---
 fs/cifs/smb2ops.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fs/cifs/smb2ops.c +0 -8
index 1022a3771e14..4f0fb53cd0de 100644
@@ -724,14 +724,6 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
        else
                goto oshr_exit;
 
-
-       memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
-       tcon->crfid.tcon = tcon;
-       tcon->crfid.is_valid = true;
-       kref_init(&tcon->crfid.refcount);
-       kref_get(&tcon->crfid.refcount);
-
-
        qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
        if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
                goto oshr_exit;

fs/cifs/smb2ops.c int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid) · line 745

Quoted 9 lines
fs/cifs/smb2ops.c +6 -0
@@ -745,6 +737,20 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
        tcon->crfid.file_all_info_is_valid = 1;
 
  oshr_exit:
+       /*
+        * If we return 0/success then we must also initialize this struct.
+        * If we got a lease we need to take out an extra get() to keep
+        * the structure around until we receive the actual lease break.
+        */
+       if (rc == 0) {
            ^^^

This will still leak a handle if smb2_validate_and_copy_iov failed above.

--
Best regards,
Pavel Shilovsky

#2 lore raw
Quoted 50 lines · Pavel Shilovsky

ср, 27 мар. 2019 г. в 16:58, Ronnie Sahlberg <[email protected]>:

Quoted 38 lines

Fix a bug where we used to not initialize the cached fid structure at all in open_shroot() if the open was successful but we did not get a lease. This would leave the structure uninitialized and later when we close the handle we would in close_shroot() try to kref_put() an uninitialized refcount.

Fix this by always initializing this structure when we are about to return 0/success but make the extra kref_get() on the refcount conditional to whether we got a lease or not.
This extra get() is only used to hold the structure until we get a lease break from the server at which point we will kref_put() it during lease processing.

---
 fs/cifs/smb2ops.c | 22 ++++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)
fs/cifs/smb2ops.c +0 -8
index 1022a3771e14..4f0fb53cd0de 100644
@@ -724,14 +724,6 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
        else
                goto oshr_exit;
 
-
-       memcpy(tcon->crfid.fid, pfid, sizeof(struct cifs_fid));
-       tcon->crfid.tcon = tcon;
-       tcon->crfid.is_valid = true;
-       kref_init(&tcon->crfid.refcount);
-       kref_get(&tcon->crfid.refcount);
-
-
        qi_rsp = (struct smb2_query_info_rsp *)rsp_iov[1].iov_base;
        if (le32_to_cpu(qi_rsp->OutputBufferLength) < sizeof(struct smb2_file_all_info))
                goto oshr_exit;

fs/cifs/smb2ops.c int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid) · line 745

Quoted 9 lines
fs/cifs/smb2ops.c +6 -0
@@ -745,6 +737,20 @@ int open_shroot(unsigned int xid, struct cifs_tcon *tcon, struct cifs_fid *pfid)
        tcon->crfid.file_all_info_is_valid = 1;
 
  oshr_exit:
+       /*
+        * If we return 0/success then we must also initialize this struct.
+        * If we got a lease we need to take out an extra get() to keep
+        * the structure around until we receive the actual lease break.
+        */
+       if (rc == 0) {
            ^^^
Quoted 1 line

This will still leak a handle if smb2_validate_and_copy_iov failed above.

Right. I will resend.

Quoted 4 lines

--
Best regards,
Pavel Shilovsky