Skip to contentSkip to conversation

ksmbd: ZDI Vulnerability patches for 5.15.y

PATCH In review Namjae Jeon 2 participants 11 messages Jul 20, 2023 Stable branch

lore
Share & tools
lore
#0 lore raw

cover ksmbd: ZDI Vulnerability patches for 5.15.y

These are ZDI Vulnerability patches that was not applied in linux 5.15 stable kernel.

Namjae Jeon (4):
ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
ksmbd: validate command payload size
ksmbd: fix out-of-bound read in smb2_write
ksmbd: validate session id and tree id in the compound request

 fs/ksmbd/server.c   | 33 ++++++++++++++++++++-------------
 fs/ksmbd/smb2misc.c | 38 ++++++++++++++++++++------------------
 fs/ksmbd/smb2pdu.c  | 44 +++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 79 insertions(+), 36 deletions(-)
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/4 ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()

From: Ralph Boehme <[email protected]>

commit b83b27909e74d27796de19c802fbc3b65ab4ba9a upstream.

Use ksmbd_req_buf_next() in ksmbd_smb2_check_message().

---
 fs/ksmbd/smb2misc.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
fs/ksmbd/smb2misc.c +2 -7
index 33a927df64f1..abc18af14f04 100644
@@ -347,16 +347,11 @@ static int smb2_validate_credit_charge(struct ksmbd_conn *conn,
 
 int ksmbd_smb2_check_message(struct ksmbd_work *work)
 {
-	struct smb2_pdu *pdu = work->request_buf;
+	struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
 	struct smb2_hdr *hdr = &pdu->hdr;
 	int command;
 	__u32 clc_len;  /* calculated length */
-	__u32 len = get_rfc1002_len(pdu);
-
-	if (work->next_smb2_rcv_hdr_off) {
-		pdu = ksmbd_req_buf_next(work);
-		hdr = &pdu->hdr;
-	}
+	__u32 len = get_rfc1002_len(work->request_buf);
 
 	if (le32_to_cpu(hdr->NextCommand) > 0)
 		len = le32_to_cpu(hdr->NextCommand);
Signature · 1 line
2.25.1
#4 lore raw

PATCH 2/4 ksmbd: validate command payload size

In reply to Namjae Jeon

commit 2b9b8f3b68edb3d67d79962f02e26dbb5ae3808d upstream.

->StructureSize2 indicates command payload size. ksmbd should validate this size with rfc1002 length before accessing it.
This patch remove unneeded check and add the validation for this.

[    8.912583] BUG: KASAN: slab-out-of-bounds in ksmbd_smb2_check_message+0x12a/0xc50
[    8.913051] Read of size 2 at addr ffff88800ac7d92c by task kworker/0:0/7
...
[    8.914967] Call Trace:
[    8.915126]  <TASK>
[    8.915267]  dump_stack_lvl+0x33/0x50
[    8.915506]  print_report+0xcc/0x620
[    8.916558]  kasan_report+0xae/0xe0
[    8.917080]  kasan_check_range+0x35/0x1b0
[    8.917334]  ksmbd_smb2_check_message+0x12a/0xc50
[    8.917935]  ksmbd_verify_smb_message+0xae/0xd0
[    8.918223]  handle_ksmbd_work+0x192/0x820
[    8.918478]  process_one_work+0x419/0x760
[    8.918727]  worker_thread+0x2a2/0x6f0
[    8.919222]  kthread+0x187/0x1d0
[    8.919723]  ret_from_fork+0x1f/0x30
[    8.919954]  </TASK>
---
 fs/ksmbd/smb2misc.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
fs/ksmbd/smb2misc.c +12 -11
index abc18af14f04..ad805b37b81d 100644
@@ -352,6 +352,7 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)
 	int command;
 	__u32 clc_len;  /* calculated length */
 	__u32 len = get_rfc1002_len(work->request_buf);
+	__u32 req_struct_size;
 
 	if (le32_to_cpu(hdr->NextCommand) > 0)
 		len = le32_to_cpu(hdr->NextCommand);
@@ -374,17 +375,9 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)
 	}
 
 	if (smb2_req_struct_sizes[command] != pdu->StructureSize2) {
-		if (command != SMB2_OPLOCK_BREAK_HE &&
-		    (hdr->Status == 0 || pdu->StructureSize2 != SMB2_ERROR_STRUCTURE_SIZE2_LE)) {
-			/* error packets have 9 byte structure size */
-			ksmbd_debug(SMB,
-				    "Illegal request size %u for command %d\n",
-				    le16_to_cpu(pdu->StructureSize2), command);
-			return 1;
-		} else if (command == SMB2_OPLOCK_BREAK_HE &&
-			   hdr->Status == 0 &&
-			   le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_20 &&
-			   le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_21) {
+		if (command == SMB2_OPLOCK_BREAK_HE &&
+		    le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_20 &&
+		    le16_to_cpu(pdu->StructureSize2) != OP_BREAK_STRUCT_SIZE_21) {
 			/* special case for SMB2.1 lease break message */
 			ksmbd_debug(SMB,
 				    "Illegal request size %d for oplock break\n",
@@ -393,6 +386,14 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)
 		}
 	}
 
+	req_struct_size = le16_to_cpu(pdu->StructureSize2) +
+		__SMB2_HEADER_STRUCTURE_SIZE;
+	if (command == SMB2_LOCK_HE)
+		req_struct_size -= sizeof(struct smb2_lock_element);
+
+	if (req_struct_size > len + 1)
+		return 1;
+
 	if (smb2_calc_size(hdr, &clc_len))
 		return 1;
 
Signature · 1 line
2.25.1
#5 lore raw

PATCH 3/4 ksmbd: fix out-of-bound read in smb2_write

In reply to Namjae Jeon

commit 5fe7f7b78290638806211046a99f031ff26164e1 upstream.

ksmbd_smb2_check_message doesn't validate hdr->NextCommand. If ->NextCommand is bigger than Offset + Length of smb2 write, It will allow oversized smb2 write length. It will cause OOB read in smb2_write.

---
 fs/ksmbd/smb2misc.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
fs/ksmbd/smb2misc.c +9 -3
index ad805b37b81d..c24674fc1904 100644
@@ -352,10 +352,16 @@ int ksmbd_smb2_check_message(struct ksmbd_work *work)
 	int command;
 	__u32 clc_len;  /* calculated length */
 	__u32 len = get_rfc1002_len(work->request_buf);
-	__u32 req_struct_size;
+	__u32 req_struct_size, next_cmd = le32_to_cpu(hdr->NextCommand);
 
-	if (le32_to_cpu(hdr->NextCommand) > 0)
-		len = le32_to_cpu(hdr->NextCommand);
+	if ((u64)work->next_smb2_rcv_hdr_off + next_cmd > len) {
+		pr_err("next command(%u) offset exceeds smb msg size\n",
+				next_cmd);
+		return 1;
+	}
+
+	if (next_cmd > 0)
+		len = next_cmd;
 	else if (work->next_smb2_rcv_hdr_off)
 		len -= work->next_smb2_rcv_hdr_off;
 
Signature · 1 line
2.25.1
#8 lore raw

PATCH 4/4 ksmbd: validate session id and tree id in the compound request

In reply to Namjae Jeon

commit 5005bcb4219156f1bf7587b185080ec1da08518e upstream.

This patch validate session id and tree id in compound request. If first operation in the compound is SMB2 ECHO request, ksmbd bypass session and tree validation. So work->sess and work->tcon could be NULL. If secound request in the compound access work->sess or tcon, It cause NULL pointer dereferecing error.

---
 fs/ksmbd/server.c  | 33 ++++++++++++++++++++-------------
 fs/ksmbd/smb2pdu.c | 44 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 59 insertions(+), 18 deletions(-)
fs/ksmbd/server.c +20 -13
index 1c5e7e023058..eb45d56b3577 100644
@@ -184,24 +184,31 @@ static void __handle_ksmbd_work(struct ksmbd_work *work,
 		goto send;
 	}
 
-	if (conn->ops->check_user_session) {
-		rc = conn->ops->check_user_session(work);
-		if (rc < 0) {
-			command = conn->ops->get_cmd_val(work);
-			conn->ops->set_rsp_status(work,
-					STATUS_USER_SESSION_DELETED);
-			goto send;
-		} else if (rc > 0) {
-			rc = conn->ops->get_ksmbd_tcon(work);
+	do {
+		if (conn->ops->check_user_session) {
+			rc = conn->ops->check_user_session(work);
 			if (rc < 0) {
-				conn->ops->set_rsp_status(work,
-					STATUS_NETWORK_NAME_DELETED);
+				if (rc == -EINVAL)
+					conn->ops->set_rsp_status(work,
+						STATUS_INVALID_PARAMETER);
+				else
+					conn->ops->set_rsp_status(work,
+						STATUS_USER_SESSION_DELETED);
 				goto send;
+			} else if (rc > 0) {
+				rc = conn->ops->get_ksmbd_tcon(work);
+				if (rc < 0) {
+					if (rc == -EINVAL)
+						conn->ops->set_rsp_status(work,
+							STATUS_INVALID_PARAMETER);
+					else
+						conn->ops->set_rsp_status(work,
+							STATUS_NETWORK_NAME_DELETED);
+					goto send;
+				}
 			}
 		}
-	}
 
-	do {
 		rc = __process_request(work, conn, &command);
 		if (rc == SERVER_HANDLER_ABORT)
 			break;
fs/ksmbd/smb2pdu.c +39 -5

78 lines not on this page · raw message

Signature · 1 line
2.25.1
#6 lore raw

cover ksmbd: ZDI Vulnerability patches for 5.15.y

In reply to Namjae Jeon

These are ZDI Vulnerability patches that was not applied in linux 5.15 stable kernel.

Namjae Jeon (4):
ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
ksmbd: validate command payload size
ksmbd: fix out-of-bound read in smb2_write
ksmbd: validate session id and tree id in the compound request

 fs/ksmbd/server.c   | 33 ++++++++++++++++++++-------------
 fs/ksmbd/smb2misc.c | 38 ++++++++++++++++++++------------------
 fs/ksmbd/smb2pdu.c  | 44 +++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 79 insertions(+), 36 deletions(-)
Signature · 1 line
2.25.1
#2 lore raw

PATCH 1/4 ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()

In reply to Namjae Jeon

From: Ralph Boehme <[email protected]>

commit b83b27909e74d27796de19c802fbc3b65ab4ba9a upstream.

Use ksmbd_req_buf_next() in ksmbd_smb2_check_message().

---
 fs/ksmbd/smb2misc.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
fs/ksmbd/smb2misc.c +2 -7
index 33a927df64f1..abc18af14f04 100644
@@ -347,16 +347,11 @@ static int smb2_validate_credit_charge(struct ksmbd_conn *conn,
 
 int ksmbd_smb2_check_message(struct ksmbd_work *work)
 {
-	struct smb2_pdu *pdu = work->request_buf;
+	struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
 	struct smb2_hdr *hdr = &pdu->hdr;
 	int command;
 	__u32 clc_len;  /* calculated length */
-	__u32 len = get_rfc1002_len(pdu);
-
-	if (work->next_smb2_rcv_hdr_off) {
-		pdu = ksmbd_req_buf_next(work);
-		hdr = &pdu->hdr;
-	}
+	__u32 len = get_rfc1002_len(work->request_buf);
 
 	if (le32_to_cpu(hdr->NextCommand) > 0)
 		len = le32_to_cpu(hdr->NextCommand);
Signature · 1 line
2.25.1
#7 lore raw
In reply to Namjae Jeon

2023-07-20 22:23 GMT+09:00, Namjae Jeon <[email protected]>:

Quoted 2 lines

These are ZDI Vulnerability patches that was not applied in linux 5.15 stable kernel.

Note that same patch sent twice...

Quoted 16 lines

Namjae Jeon (4):
ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
ksmbd: validate command payload size
ksmbd: fix out-of-bound read in smb2_write
ksmbd: validate session id and tree id in the compound request

 fs/ksmbd/server.c   | 33 ++++++++++++++++++++-------------
 fs/ksmbd/smb2misc.c | 38 ++++++++++++++++++++------------------
 fs/ksmbd/smb2pdu.c  | 44 +++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 79 insertions(+), 36 deletions(-)

--
2.25.1

#3 lore raw

Re: [5.15.y PATCH 1/4] ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()

In reply to Namjae Jeon

2023-07-20 22:23 GMT+09:00, Namjae Jeon <[email protected]>:

Quoted 5 lines

From: Ralph Boehme <[email protected]>

commit b83b27909e74d27796de19c802fbc3b65ab4ba9a upstream.

Use ksmbd_req_buf_next() in ksmbd_smb2_check_message().

Note that same patch sent twice...

Quoted 40 lines
---
 fs/ksmbd/smb2misc.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
fs/ksmbd/smb2misc.c +2 -7
index 33a927df64f1..abc18af14f04 100644
@@ -347,16 +347,11 @@ static int smb2_validate_credit_charge(struct
 ksmbd_conn *conn,
 
 int ksmbd_smb2_check_message(struct ksmbd_work *work)
 {
-	struct smb2_pdu *pdu = work->request_buf;
+	struct smb2_pdu *pdu = ksmbd_req_buf_next(work);
 	struct smb2_hdr *hdr = &pdu->hdr;
 	int command;
 	__u32 clc_len;  /* calculated length */
-	__u32 len = get_rfc1002_len(pdu);
-
-	if (work->next_smb2_rcv_hdr_off) {
-		pdu = ksmbd_req_buf_next(work);
-		hdr = &pdu->hdr;
-	}
+	__u32 len = get_rfc1002_len(work->request_buf);
 
 	if (le32_to_cpu(hdr->NextCommand) > 0)

len = le32_to_cpu(hdr->NextCommand);
--
2.25.1

maintainer
#9 lore raw
In reply to Namjae Jeon
Quoted 9 lines · Namjae Jeon

These are ZDI Vulnerability patches that was not applied in linux 5.15 stable kernel.

Namjae Jeon (4):
ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
ksmbd: validate command payload size
ksmbd: fix out-of-bound read in smb2_write
ksmbd: validate session id and tree id in the compound request

Quoted 8 lines
 fs/ksmbd/server.c   | 33 ++++++++++++++++++++-------------
 fs/ksmbd/smb2misc.c | 38 ++++++++++++++++++++------------------
 fs/ksmbd/smb2pdu.c  | 44 +++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 79 insertions(+), 36 deletions(-)
Signature · 1 line
2.25.1

All now queued up now, thanks!

greg k-h

#10 lore raw

2023-07-21 2:54 GMT+09:00, Greg KH <[email protected]>:

Quoted 15 lines · Namjae Jeon
Quoted 14 lines

These are ZDI Vulnerability patches that was not applied in linux 5.15 stable kernel.

Namjae Jeon (4):
ksmbd: use ksmbd_req_buf_next() in ksmbd_smb2_check_message()
ksmbd: validate command payload size
ksmbd: fix out-of-bound read in smb2_write
ksmbd: validate session id and tree id in the compound request

 fs/ksmbd/server.c   | 33 ++++++++++++++++++++-------------
 fs/ksmbd/smb2misc.c | 38 ++++++++++++++++++++------------------
 fs/ksmbd/smb2pdu.c  | 44 +++++++++++++++++++++++++++++++++++++++-----
 3 files changed, 79 insertions(+), 36 deletions(-)
Quoted 5 lines
Quoted 3 lines

--
2.25.1

All now queued up now, thanks!

Thank you!

Quoted 3 lines

greg k-h