Skip to contentSkip to conversation

ksmbd: smbd: fix missing client's memory region invalidation

PATCH Superseded Hyunchul Lee 2 participants 2 messages Jan 10, 2022 – Jan 11, 2022

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: smbd: fix missing client's memory region invalidation

if the Channel of a SMB2 WRITE request is SMB2_CHANNEL_RDMA_V1_INVALIDTE, a client does not invalidate its memory regions but ksmbd must do it by sending a SMB2 WRITE response with IB_WR_SEND_WITH_INV.

But if errors occur while processing a SMB2 READ/WRITE request, ksmbd sends a response with IB_WR_SEND. So a client could use memory regions already in use.

---
 fs/ksmbd/smb2pdu.c | 76 ++++++++++++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 27 deletions(-)
fs/ksmbd/smb2pdu.c +49 -27
index ced8f949a4d6..19355511b777 100644
@@ -6132,18 +6132,6 @@ static ssize_t smb2_read_rdma_channel(struct ksmbd_work *work,
 		(struct smb2_buffer_desc_v1 *)&req->Buffer[0];
 	int err;
 
-	if (work->conn->dialect == SMB30_PROT_ID &&
-	    req->Channel != SMB2_CHANNEL_RDMA_V1)
-		return -EINVAL;
-
-	if (req->ReadChannelInfoOffset == 0 ||
-	    le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
-		return -EINVAL;
-
-	work->need_invalidate_rkey =
-		(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
-	work->remote_key = le32_to_cpu(desc->token);
-
 	err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
 				    le32_to_cpu(desc->token),
 				    le64_to_cpu(desc->offset),
@@ -6179,6 +6167,28 @@ int smb2_read(struct ksmbd_work *work)
 		return smb2_read_pipe(work);
 	}
 
+	if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
+	    req->Channel == SMB2_CHANNEL_RDMA_V1) {
+		struct smb2_buffer_desc_v1 *desc =
+			(struct smb2_buffer_desc_v1 *)&req->Buffer[0];
+
+		if (work->conn->dialect == SMB30_PROT_ID &&
+		    req->Channel != SMB2_CHANNEL_RDMA_V1) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		if (req->ReadChannelInfoOffset == 0 ||
+		    le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		work->need_invalidate_rkey =
+			(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
+		work->remote_key = le32_to_cpu(desc->token);
+	}
+
 	fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
 				  le64_to_cpu(req->PersistentFileId));
 	if (!fp) {
@@ -6364,21 +6374,6 @@ static ssize_t smb2_write_rdma_channel(struct ksmbd_work *work,
 
 	desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
 
-	if (work->conn->dialect == SMB30_PROT_ID &&
-	    req->Channel != SMB2_CHANNEL_RDMA_V1)
-		return -EINVAL;
-
-	if (req->Length != 0 || req->DataOffset != 0)
-		return -EINVAL;
-
-	if (req->WriteChannelInfoOffset == 0 ||
-	    le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
-		return -EINVAL;
-
-	work->need_invalidate_rkey =
-		(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
-	work->remote_key = le32_to_cpu(desc->token);
-
 	data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
 	if (!data_buf)
 		return -ENOMEM;
@@ -6425,6 +6420,33 @@ int smb2_write(struct ksmbd_work *work)
 		return smb2_write_pipe(work);
 	}
 
+	if (req->Channel == SMB2_CHANNEL_RDMA_V1 ||
+	    req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
+		struct smb2_buffer_desc_v1 *desc =
+			(struct smb2_buffer_desc_v1 *)&req->Buffer[0];
+
+		if (work->conn->dialect == SMB30_PROT_ID &&
+		    req->Channel != SMB2_CHANNEL_RDMA_V1) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		if (req->Length != 0 || req->DataOffset != 0) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		if (req->WriteChannelInfoOffset == 0 ||
+		    le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		work->need_invalidate_rkey =
+			(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
+		work->remote_key = le32_to_cpu(desc->token);
+	}
+
 	if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
 		ksmbd_debug(SMB, "User does not have write permission\n");
 		err = -EACCES;
Signature · 1 line
2.25.1
maintainer
#1 lore raw

2022-01-10 10:36 GMT+09:00, Hyunchul Lee <[email protected]>:

Quoted 55 lines

if the Channel of a SMB2 WRITE request is SMB2_CHANNEL_RDMA_V1_INVALIDTE, a client does not invalidate its memory regions but ksmbd must do it by sending a SMB2 WRITE response with IB_WR_SEND_WITH_INV.

But if errors occur while processing a SMB2 READ/WRITE request, ksmbd sends a response with IB_WR_SEND. So a client could use memory regions already in use.

---
 fs/ksmbd/smb2pdu.c | 76 ++++++++++++++++++++++++++++++----------------
 1 file changed, 49 insertions(+), 27 deletions(-)
fs/ksmbd/smb2pdu.c +0 -12
index ced8f949a4d6..19355511b777 100644
@@ -6132,18 +6132,6 @@ static ssize_t smb2_read_rdma_channel(struct
 ksmbd_work *work,
 		(struct smb2_buffer_desc_v1 *)&req->Buffer[0];
 	int err;
 
-	if (work->conn->dialect == SMB30_PROT_ID &&
-	    req->Channel != SMB2_CHANNEL_RDMA_V1)
-		return -EINVAL;
-
-	if (req->ReadChannelInfoOffset == 0 ||
-	    le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc))
-		return -EINVAL;
-
-	work->need_invalidate_rkey =
-		(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
-	work->remote_key = le32_to_cpu(desc->token);
-
 	err = ksmbd_conn_rdma_write(work->conn, data_buf, length,
 				    le32_to_cpu(desc->token),
fs/ksmbd/smb2pdu.c +11 -0
 				    le64_to_cpu(desc->offset),
@@ -6179,6 +6167,28 @@ int smb2_read(struct ksmbd_work *work)
 		return smb2_read_pipe(work);
 	}
 
+	if (req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE ||
+	    req->Channel == SMB2_CHANNEL_RDMA_V1) {
+		struct smb2_buffer_desc_v1 *desc =
+			(struct smb2_buffer_desc_v1 *)&req->Buffer[0];
+
+		if (work->conn->dialect == SMB30_PROT_ID &&
+		    req->Channel != SMB2_CHANNEL_RDMA_V1) {
+			err = -EINVAL;
+			goto out;
+		}
+

fs/ksmbd/smb2pdu.c int smb2_read(struct ksmbd_work *work) · line 6190

Quoted 10 lines
fs/ksmbd/smb2pdu.c +10 -0
+		if (req->ReadChannelInfoOffset == 0 ||
+		    le16_to_cpu(req->ReadChannelInfoLength) < sizeof(*desc)) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		work->need_invalidate_rkey =
+			(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
+		work->remote_key = le32_to_cpu(desc->token);
+	}

Can we factor out a helper for this ?

Quoted 47 lines
fs/ksmbd/smb2pdu.c +1 -15
+
 	fp = ksmbd_lookup_fd_slow(work, le64_to_cpu(req->VolatileFileId),
 				  le64_to_cpu(req->PersistentFileId));
 	if (!fp) {
@@ -6364,21 +6374,6 @@ static ssize_t smb2_write_rdma_channel(struct
 ksmbd_work *work,
 
 	desc = (struct smb2_buffer_desc_v1 *)&req->Buffer[0];
 
-	if (work->conn->dialect == SMB30_PROT_ID &&
-	    req->Channel != SMB2_CHANNEL_RDMA_V1)
-		return -EINVAL;
-
-	if (req->Length != 0 || req->DataOffset != 0)
-		return -EINVAL;
-
-	if (req->WriteChannelInfoOffset == 0 ||
-	    le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc))
-		return -EINVAL;
-
-	work->need_invalidate_rkey =
-		(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
-	work->remote_key = le32_to_cpu(desc->token);
-
 	data_buf = kvmalloc(length, GFP_KERNEL | __GFP_ZERO);
 	if (!data_buf)
fs/ksmbd/smb2pdu.c +16 -0
 		return -ENOMEM;
@@ -6425,6 +6420,33 @@ int smb2_write(struct ksmbd_work *work)
 		return smb2_write_pipe(work);
 	}
 
+	if (req->Channel == SMB2_CHANNEL_RDMA_V1 ||
+	    req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
+		struct smb2_buffer_desc_v1 *desc =
+			(struct smb2_buffer_desc_v1 *)&req->Buffer[0];
+
+		if (work->conn->dialect == SMB30_PROT_ID &&
+		    req->Channel != SMB2_CHANNEL_RDMA_V1) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		if (req->Length != 0 || req->DataOffset != 0) {
+			err = -EINVAL;
+			goto out;
+		}
+

fs/ksmbd/smb2pdu.c int smb2_write(struct ksmbd_work *work) · line 6448

Quoted 10 lines
fs/ksmbd/smb2pdu.c +10 -0
+		if (req->WriteChannelInfoOffset == 0 ||
+		    le16_to_cpu(req->WriteChannelInfoLength) < sizeof(*desc)) {
+			err = -EINVAL;
+			goto out;
+		}
+
+		work->need_invalidate_rkey =
+			(req->Channel == SMB2_CHANNEL_RDMA_V1_INVALIDATE);
+		work->remote_key = le32_to_cpu(desc->token);
+	}

Ditto.

Thanks!

Quoted 8 lines
fs/ksmbd/smb2pdu.c +1 -1
+
 	if (!test_tree_conn_flag(work->tcon, KSMBD_TREE_CONN_FLAG_WRITABLE)) {
 		ksmbd_debug(SMB, "User does not have write permission\n");
 		err = -EACCES;
--

2.25.1