Skip to contentSkip to conversation

fix bidirectional fsctl passthrough

PATCH In review Ronnie Sahlberg 4 participants 5 messages Apr 15, 2019 – Apr 17, 2019

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

Steve, Tom

Please find an updated version that adds bounds checking as pointer out by Tom.

#1 lore raw

PATCH 1/1 cifs: fix bi-directional fsctl passthrough calls

SMB2 Ioctl responses from servers may respond with both the request blob from the client followed by the actual reply blob for ioctls that are bi-directional.

In that case we can not assume that the reply blob comes immediately after the ioctl response structure.

This fixes FSCTLs such as SMB2:FSCTL_QUERY_ALLOCATED_RANGES

---
 fs/cifs/smb2ops.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +8 -1
index 0b7e2be2a781..2824b97c5869 100644
@@ -1462,12 +1462,19 @@ smb2_ioctl_query_info(const unsigned int xid,
 		io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
 		if (le32_to_cpu(io_rsp->OutputCount) < qi.input_buffer_length)
 			qi.input_buffer_length = le32_to_cpu(io_rsp->OutputCount);
+		if (qi.input_buffer_length > 0 &&
+		    le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length > rsp_iov[1].iov_len) {
+			rc = -EFAULT;
+			goto iqinf_exit;
+		}
 		if (copy_to_user(&pqi->input_buffer_length, &qi.input_buffer_length,
 				 sizeof(qi.input_buffer_length))) {
 			rc = -EFAULT;
 			goto iqinf_exit;
 		}
-		if (copy_to_user(pqi + 1, &io_rsp[1], qi.input_buffer_length)) {
+		if (copy_to_user((char *)pqi + sizeof(struct smb_query_info),
+				 (char *)io_rsp + le32_to_cpu(io_rsp->OutputOffset),
+				 qi.input_buffer_length)) {
 			rc = -EFAULT;
 			goto iqinf_exit;
 		}
Signature · 1 line
2.13.6
#2 lore raw

RE: [PATCH] cifs: fix bi-directional fsctl passthrough calls

Quoted 29 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Ronnie Sahlberg
Sent: Sunday, April 14, 2019 10:14 PM
To: linux-cifs <[email protected]>
Cc: Steve French <[email protected]>; Ronnie Sahlberg <[email protected]>
Subject: [PATCH] cifs: fix bi-directional fsctl passthrough calls

SMB2 Ioctl responses from servers may respond with both the request blob from
the client followed by the actual reply blob for ioctls that are bi-directional.

In that case we can not assume that the reply blob comes immediately after the
ioctl response structure.

This fixes FSCTLs such as SMB2:FSCTL_QUERY_ALLOCATED_RANGES

---
 fs/cifs/smb2ops.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +0 -0
index 0b7e2be2a781..2824b97c5869 100644

fs/cifs/smb2ops.c smb2_ioctl_query_info(const unsigned int xid, · line 1469

Quoted 8 lines
fs/cifs/smb2ops.c +2 -0
@@ -1462,12 +1462,19 @@ smb2_ioctl_query_info(const unsigned int xid,
 		io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
 		if (le32_to_cpu(io_rsp->OutputCount) <
 qi.input_buffer_length)
 			qi.input_buffer_length = le32_to_cpu(io_rsp-
 >OutputCount);
+		if (qi.input_buffer_length > 0 &&
+		    le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
 > rsp_iov[1].iov_len) {

I hope this isn't a dumb question, but I can't readily figure it out due to multiple layers of #include nesting. Is le32_to_cpu() signed or unsigned? If it's signed, this validation could still be attacked.

And second question, is the fsctl output payload always guaranteed to land in rsp_iov[1]?

Tom.

Quoted 19 lines
fs/cifs/smb2ops.c +5 -1
+			rc = -EFAULT;
+			goto iqinf_exit;
+		}
 		if (copy_to_user(&pqi->input_buffer_length,
 &qi.input_buffer_length,
 				 sizeof(qi.input_buffer_length))) {
 			rc = -EFAULT;
 			goto iqinf_exit;
 		}
-		if (copy_to_user(pqi + 1, &io_rsp[1], qi.input_buffer_length)) {
+		if (copy_to_user((char *)pqi + sizeof(struct smb_query_info),
+				 (char *)io_rsp + le32_to_cpu(io_rsp-
Quoted 1 line

OutputOffset),

fs/cifs/smb2ops.c +1 -1
+				 qi.input_buffer_length)) {
 			rc = -EFAULT;
 			goto iqinf_exit;
 		}
--

2.13.6

#3 lore raw

Re: [PATCH] cifs: fix bi-directional fsctl passthrough calls

le32_to_cpu() returns unsigned - see below from
uapi/linux/byteorder/little_endian.h:

     #define __le32_to_cpu(x) ((__force __u32)(__le32)(x))
Quoted 68 lines · Tom Talpey
Quoted 30 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Ronnie Sahlberg
Sent: Sunday, April 14, 2019 10:14 PM
To: linux-cifs <[email protected]>
Cc: Steve French <[email protected]>; Ronnie Sahlberg <[email protected]>
Subject: [PATCH] cifs: fix bi-directional fsctl passthrough calls

SMB2 Ioctl responses from servers may respond with both the request blob from
the client followed by the actual reply blob for ioctls that are bi-directional.

In that case we can not assume that the reply blob comes immediately after the
ioctl response structure.

This fixes FSCTLs such as SMB2:FSCTL_QUERY_ALLOCATED_RANGES

---
 fs/cifs/smb2ops.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +0 -0
index 0b7e2be2a781..2824b97c5869 100644

fs/cifs/smb2ops.c smb2_ioctl_query_info(const unsigned int xid, · line 1469

Quoted 8 lines
fs/cifs/smb2ops.c +2 -0
@@ -1462,12 +1462,19 @@ smb2_ioctl_query_info(const unsigned int xid,
              io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
              if (le32_to_cpu(io_rsp->OutputCount) <
 qi.input_buffer_length)
                      qi.input_buffer_length = le32_to_cpu(io_rsp-
 >OutputCount);
+             if (qi.input_buffer_length > 0 &&
+                 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
 > rsp_iov[1].iov_len) {

I hope this isn't a dumb question, but I can't readily figure it out due to multiple layers of #include nesting. Is le32_to_cpu() signed or unsigned? If it's signed, this validation could still be attacked.

And second question, is the fsctl output payload always guaranteed to land in rsp_iov[1]?

Tom.

Quoted 19 lines
fs/cifs/smb2ops.c +5 -1
+                     rc = -EFAULT;
+                     goto iqinf_exit;
+             }
              if (copy_to_user(&pqi->input_buffer_length,
 &qi.input_buffer_length,
                               sizeof(qi.input_buffer_length))) {
                      rc = -EFAULT;
                      goto iqinf_exit;
              }
-             if (copy_to_user(pqi + 1, &io_rsp[1], qi.input_buffer_length)) {
+             if (copy_to_user((char *)pqi + sizeof(struct smb_query_info),
+                              (char *)io_rsp + le32_to_cpu(io_rsp-
Quoted 1 line

OutputOffset),

+                              qi.input_buffer_length)) {
                      rc = -EFAULT;
                      goto iqinf_exit;
              }

--
2.13.6

Signature · 3 lines
Thanks,

Steve
#4 lore raw

Re: [PATCH] cifs: fix bi-directional fsctl passthrough calls

In reply to Tom Talpey
Quoted 37 lines · Tom Talpey
Quoted 37 lines

-----Original Message-----
From: [email protected] <[email protected]> On Behalf Of Ronnie Sahlberg
Sent: Sunday, April 14, 2019 10:14 PM
To: linux-cifs <[email protected]>
Cc: Steve French <[email protected]>; Ronnie Sahlberg <[email protected]>
Subject: [PATCH] cifs: fix bi-directional fsctl passthrough calls

SMB2 Ioctl responses from servers may respond with both the request blob from
the client followed by the actual reply blob for ioctls that are bi-directional.

In that case we can not assume that the reply blob comes immediately after the
ioctl response structure.

This fixes FSCTLs such as SMB2:FSCTL_QUERY_ALLOCATED_RANGES

---
 fs/cifs/smb2ops.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/cifs/smb2ops.c +2 -0
index 0b7e2be2a781..2824b97c5869 100644
@@ -1462,12 +1462,19 @@ smb2_ioctl_query_info(const unsigned int xid,
              io_rsp = (struct smb2_ioctl_rsp *)rsp_iov[1].iov_base;
              if (le32_to_cpu(io_rsp->OutputCount) <
 qi.input_buffer_length)
                      qi.input_buffer_length = le32_to_cpu(io_rsp-
 >OutputCount);
+             if (qi.input_buffer_length > 0 &&
+                 le32_to_cpu(io_rsp->OutputOffset) + qi.input_buffer_length
Quoted 8 lines

fs/cifs/smb2ops.c smb2_ioctl_query_info(const unsigned int xid, · line 1469

Quoted 1 line
fs/cifs/smb2ops.c +0 -0
 > rsp_iov[1].iov_len) {

I hope this isn't a dumb question, but I can't readily figure it out due to multiple layers of #include nesting. Is le32_to_cpu() signed or unsigned? If it's signed, this validation could still be attacked.

And second question, is the fsctl output payload always guaranteed to land in rsp_iov[1]?

Yes. It will always land in rsp_iov[1].

Quoted 23 lines

Tom.

Quoted 19 lines
fs/cifs/smb2ops.c +5 -1
+                     rc = -EFAULT;
+                     goto iqinf_exit;
+             }
              if (copy_to_user(&pqi->input_buffer_length,
 &qi.input_buffer_length,
                               sizeof(qi.input_buffer_length))) {
                      rc = -EFAULT;
                      goto iqinf_exit;
              }
-             if (copy_to_user(pqi + 1, &io_rsp[1], qi.input_buffer_length)) {
+             if (copy_to_user((char *)pqi + sizeof(struct smb_query_info),
+                              (char *)io_rsp + le32_to_cpu(io_rsp-
Quoted 1 line

OutputOffset),

+                              qi.input_buffer_length)) {
                      rc = -EFAULT;
                      goto iqinf_exit;
              }

--
2.13.6