]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
ksmbd: validate length in smb2_write()
authorNamjae Jeon <linkinjeon@kernel.org>
Mon, 18 Dec 2023 15:32:52 +0000 (00:32 +0900)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 23 Dec 2023 09:41:51 +0000 (10:41 +0100)
[ Upstream commit 158a66b245739e15858de42c0ba60fcf3de9b8e6 ]

The SMB2 Write packet contains data that is to be written
to a file or to a pipe. Depending on the client, there may
be padding between the header and the data field.
Currently, the length is validated only in the case padding
is present.

Since the DataOffset field always points to the beginning
of the data, there is no need to have a special case for
padding. By removing this, the length is validated in both
cases.

Signed-off-by: Marios Makassikis <mmakassikis@freebox.fr>
Acked-by: Namjae Jeon <linkinjeon@kernel.org>
Signed-off-by: Steve French <stfrench@microsoft.com>
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/ksmbd/smb2pdu.c

index ac8b7dbe33d4b3fc85e15c26e98201d8195d5c75..85f96e8adfaabf8b15e0bc345015694cb0e80311 100644 (file)
@@ -6410,23 +6410,18 @@ static noinline int smb2_write_pipe(struct ksmbd_work *work)
        length = le32_to_cpu(req->Length);
        id = req->VolatileFileId;
 
-       if (le16_to_cpu(req->DataOffset) ==
-           offsetof(struct smb2_write_req, Buffer)) {
-               data_buf = (char *)&req->Buffer[0];
-       } else {
-               if ((u64)le16_to_cpu(req->DataOffset) + length >
-                   get_rfc1002_len(work->request_buf)) {
-                       pr_err("invalid write data offset %u, smb_len %u\n",
-                              le16_to_cpu(req->DataOffset),
-                              get_rfc1002_len(work->request_buf));
-                       err = -EINVAL;
-                       goto out;
-               }
-
-               data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
-                               le16_to_cpu(req->DataOffset));
+       if ((u64)le16_to_cpu(req->DataOffset) + length >
+           get_rfc1002_len(work->request_buf)) {
+               pr_err("invalid write data offset %u, smb_len %u\n",
+                      le16_to_cpu(req->DataOffset),
+                      get_rfc1002_len(work->request_buf));
+               err = -EINVAL;
+               goto out;
        }
 
+       data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
+                          le16_to_cpu(req->DataOffset));
+
        rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
        if (rpc_resp) {
                if (rpc_resp->flags == KSMBD_RPC_ENOTIMPLEMENTED) {
@@ -6571,20 +6566,15 @@ int smb2_write(struct ksmbd_work *work)
 
        if (req->Channel != SMB2_CHANNEL_RDMA_V1 &&
            req->Channel != SMB2_CHANNEL_RDMA_V1_INVALIDATE) {
-               if (le16_to_cpu(req->DataOffset) ==
+               if (le16_to_cpu(req->DataOffset) <
                    offsetof(struct smb2_write_req, Buffer)) {
-                       data_buf = (char *)&req->Buffer[0];
-               } else {
-                       if (le16_to_cpu(req->DataOffset) <
-                           offsetof(struct smb2_write_req, Buffer)) {
-                               err = -EINVAL;
-                               goto out;
-                       }
-
-                       data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
-                                       le16_to_cpu(req->DataOffset));
+                       err = -EINVAL;
+                       goto out;
                }
 
+               data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
+                                   le16_to_cpu(req->DataOffset));
+
                ksmbd_debug(SMB, "flags %u\n", le32_to_cpu(req->Flags));
                if (le32_to_cpu(req->Flags) & SMB2_WRITEFLAG_WRITE_THROUGH)
                        writethrough = true;