]> git.hungrycats.org Git - linux/commitdiff
ksmbd: prevent stale data from being written by SMB2 WRITE requests
authorNamjae Jeon <linkinjeon@kernel.org>
Tue, 3 Nov 2026 15:47:24 +0000 (10:47 -0500)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 6 Nov 2026 17:06:13 +0000 (12:06 -0500)
[ Upstream commit bc0e0f5ac1d7b044d403f859406b4b66d4b1ce9b ]

Initialize the compatibility byte in request buffers without zeroing
the entire allocation. Keep the one-byte compatibility exception for
SMB2 messages so valid zero-length WRITE requests remain compatible,
and validate the data range in the WRITE handlers against the current
compound request before accessing it.

This prevents stale request-buffer data from being written to files or
sent to IPC pipes.

Fixes: 443d61d1fa9f ("ksmbd: allocate one more byte for implied bcc[0]")
Cc: stable@vger.kernel.org
Reported-by: zdi-disclosures@trendmicro.com # ZDI-CAN-32436
Signed-off-by: Namjae Jeon <linkinjeon@kernel.org>
[ sashal: Inlined the unavailable smb2_current_req_len() helper and omitted the decompression change because compression support is absent. ]
Signed-off-by: Sasha Levin <sashal@kernel.org>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
fs/smb/server/connection.c
fs/smb/server/smb2pdu.c

index 8f1b70e39362e7d846448ba55c9d73cf2ce0b445..e8739b482b2fe524339de3876162872202cf4d9f 100644 (file)
@@ -520,6 +520,7 @@ recheck:
                if (!conn->request_buf)
                        break;
 
+               conn->request_buf[pdu_size + 4] = 0;
                memcpy(conn->request_buf, hdr_buf, sizeof(hdr_buf));
 
                /*
index 39c98fc304f3f2f9dc8607364d535667ee9b4f49..9688c70385a23fb472deeaa16c16671b94152fed 100644 (file)
@@ -7218,23 +7218,28 @@ static noinline int smb2_write_pipe(struct ksmbd_work *work)
        int err = 0, ret = 0;
        char *data_buf;
        size_t length;
+       unsigned int data_offset, req_len;
 
        WORK_BUFFERS(work, req, rsp);
 
        length = le32_to_cpu(req->Length);
        id = req->VolatileFileId;
+       data_offset = le16_to_cpu(req->DataOffset);
+       req_len = le32_to_cpu(req->hdr.NextCommand);
+       if (!req_len)
+               req_len = get_rfc1002_len(work->request_buf) -
+                       work->next_smb2_rcv_hdr_off;
 
-       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));
+       if (data_offset < offsetof(struct smb2_write_req, Buffer) ||
+           data_offset > req_len || length > req_len - data_offset) {
+               pr_err("invalid write data offset %u, length %zu, req_len %u\n",
+                      data_offset, length, req_len);
                err = -EINVAL;
                goto out;
        }
 
        data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
-                          le16_to_cpu(req->DataOffset));
+                          data_offset);
 
        rpc_resp = ksmbd_rpc_write(work->sess, id, data_buf, length);
        if (rpc_resp) {
@@ -7391,14 +7396,25 @@ int smb2_write(struct ksmbd_work *work)
                writethrough = true;
 
        if (is_rdma_channel == false) {
-               if (le16_to_cpu(req->DataOffset) <
-                   offsetof(struct smb2_write_req, Buffer)) {
+               unsigned int data_offset = le16_to_cpu(req->DataOffset);
+               unsigned int req_len = le32_to_cpu(req->hdr.NextCommand);
+
+               if (!req_len)
+                       req_len = get_rfc1002_len(work->request_buf) -
+                               work->next_smb2_rcv_hdr_off;
+
+               if (data_offset < offsetof(struct smb2_write_req, Buffer) ||
+                   data_offset > req_len ||
+                   length > req_len - data_offset) {
+                       ksmbd_debug(SMB,
+                                   "invalid write data offset %u, length %zu, req_len %u\n",
+                                   data_offset, length, req_len);
                        err = -EINVAL;
                        goto out;
                }
 
                data_buf = (char *)(((char *)&req->hdr.ProtocolId) +
-                                   le16_to_cpu(req->DataOffset));
+                                   data_offset);
 
                ksmbd_debug(SMB, "filename %pD, offset %lld, len %zu\n",
                            fp->filp, offset, length);