From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795052019; b=uGzQDuTtxS5jw282E38jUuCC0/nC2uqr3gQdBUgN2I/NkfS//4YXX0Bg7bA3WFdEKReW a/5qVcvF+W1DCbJZ5TDKx7U+ZPFD+TT7st1WFWbad8jLz//OYL0NR+ERuE9rL/yyt+CGD Mw/iLaSuXwviWZQQxxYVCmcLYMzd3spZqAnxIIKO5buW+VD1ENQGHq4O2qr1IJ+rSkCYP egIDtiwFZ18sAQjOd6reNapCWhdpcEcMM/KFAf5dR4aQQPVcAPFKTDGj9FFu6WdS8gnQZ dOGX7brMhMdI/ZOUX3jlkOYyAQZV8MQr1DWJIAttiqWaCwf5IhJm5xw6mCnMXVM/Azw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795052019; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=ukKfi4XZSYF7z0EoTe0Qo5Xqg+TC2MyLOeBUp9UnjTw=; b=Kzl8/LNbBWyz9uG6mfE/KKaWXb8oshXsYrg+aSRBMCXG6RhZw3liPIFrSmKmfVKEtzKq 3v2Y+jRIRJyLBwbkw4qSFIx3Osjcooa4vFc5OXSfHbGvvAv1+SCyC6PL2ZBbS2cEciv4E cxl6Aiix2e1Hf/a1mrH91Zo0rEykD9GqrQjuMyf+j4x217w1/bWJ9QKU9ZrLzPME5AEfA nDKlFjZN7Ew++fMpjti+hIDCKJClf5ohZjzwZtoNEyou8sFJ2jz930o3ja3MOMBXpMllg nsavIpvHTUBCDBSx//j/IrLYF+cklmuXg0On3pcYp8V6lo+ffZB+9aPxwZERsIoGVWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=OjhdIXP5; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7DC66606A32A; Thu, 19 Nov 2026 01:33:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=OjhdIXP5 Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD6CF1F000C7; Thu, 19 Nov 2026 01:33:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795052019; bh=ukKfi4XZSYF7z0EoTe0Qo5Xqg+TC2MyLOeBUp9UnjTw=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=OjhdIXP5a0sIru0dWQlJsehEk06v3vQng49Fme5Cn8G7T19ahRsazcyUkq+ksfvrl ddfwLVltX2UH+67MBVG3t2LjYedjnc7xZKew66yl6XDiwOdH1Q/26Vm0cjD3etToLE LQUSX6qaKRQfWIDWNN0OfHQsUReT+2O0vfWZ1Gtg= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, DaeMyung Kang , Namjae Jeon , Sasha Levin Subject: [PATCH 5.15 0412/2214] ksmbd: fix named stream write and EOF handling Date: Wed, 18 Nov 2026 15:34:48 +0100 Message-ID: <20261118150027.239218546@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118150009.797899913@linuxfoundation.org> References: <20261118150009.797899913@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.15-stable review patch. If anyone has any objections, please let me know. ------------------ From: DaeMyung Kang [ Upstream commit 02165c8d402392278a5cd2cf97645ed73a7ad4a1 ] An SMB WRITE to an existing named stream can silently discard data after its written range. ksmbd stores streams as xattr values, but the write path passes offset + count as the length of the replacement xattr even when the old stream is longer. Writing 26 bytes at offset 0 to a 60-byte stream reports 26 bytes written while discarding the remaining 34 bytes. Keep the existing xattr length when an in-place write ends before EOF. Reject writes that cross XATTR_SIZE_MAX rather than storing only the portion that fits while reporting the full requested count. Commit 4ea0bb8aaedf ("ksmbd: handle set/get info file for streamed file") intentionally skipped ordinary inode truncation for stream handles: an EOF SetInfo on an ADS had truncated the base file. That fixed the base file corruption but left stream EOF changes reporting success without resizing the stream. Handle these requests by resizing only the stream xattr; the base file size remains untouched. Zero-fill stream extensions. This is needed alongside the write fix: a short write's accidental truncation previously masked an ignored explicit EOF change. The stream resize helper returns xattr errors directly rather than using the base-file truncate path's EBADF conversion. Fixes: f44158485826 ("cifsd: add file operations") Fixes: 4ea0bb8aaedf ("ksmbd: handle set/get info file for streamed file") Cc: stable@vger.kernel.org Signed-off-by: DaeMyung Kang Signed-off-by: Namjae Jeon [ sashal: adapted xattr calls to use user_namespace and file_mnt_user_ns() instead of the newer mount ID mapping API ] Signed-off-by: Sasha Levin --- fs/ksmbd/smb2pdu.c | 2 ++ fs/ksmbd/vfs.c | 71 ++++++++++++++++++++++++++++++++++++++++++---- fs/ksmbd/vfs.h | 1 + 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/fs/ksmbd/smb2pdu.c b/fs/ksmbd/smb2pdu.c index 7366c22776808..55117e96e9498 100644 --- a/fs/ksmbd/smb2pdu.c +++ b/fs/ksmbd/smb2pdu.c @@ -5963,6 +5963,8 @@ static int set_end_of_file_info(struct ksmbd_work *work, struct ksmbd_file *fp, newsize = le64_to_cpu(file_eof_info->EndOfFile); inode = file_inode(fp->filp); + if (ksmbd_stream_fd(fp)) + return ksmbd_vfs_stream_truncate(fp, newsize); /* * If FILE_END_OF_FILE_INFORMATION of set_info_file is called diff --git a/fs/ksmbd/vfs.c b/fs/ksmbd/vfs.c index 92159a82c232d..1e087ae68e239 100644 --- a/fs/ksmbd/vfs.c +++ b/fs/ksmbd/vfs.c @@ -415,19 +415,22 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos, { char *stream_buf = NULL, *wbuf; struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); - size_t size; + size_t size, write_end; ssize_t v_len; int err = 0; ksmbd_debug(VFS, "write stream data pos : %llu, count : %zd\n", *pos, count); - size = *pos + count; - if (size > XATTR_SIZE_MAX) { - size = XATTR_SIZE_MAX; - count = (*pos + count) - XATTR_SIZE_MAX; + if (*pos < 0 || *pos >= XATTR_SIZE_MAX) { + pr_err("stream write position %lld is out of bounds\n", *pos); + return -EINVAL; } + if (count > XATTR_SIZE_MAX - *pos) + return -EFBIG; + write_end = *pos + count; + v_len = ksmbd_vfs_getcasexattr(user_ns, fp->filp->f_path.dentry, fp->stream.name, @@ -438,6 +441,8 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos, err = v_len; goto out; } + /* Preserve the tail of an existing stream on an in-place write. */ + size = max_t(size_t, v_len, write_end); if (v_len < size) { wbuf = kvzalloc(size, GFP_KERNEL); @@ -471,6 +476,62 @@ static int ksmbd_vfs_stream_write(struct ksmbd_file *fp, char *buf, loff_t *pos, return err; } +/** + * ksmbd_vfs_stream_truncate() - change the length of a named stream + * @fp: stream file handle + * @newsize: new stream length + * + * Resize the stream xattr without changing the base inode size. + * + * Return: 0 on success, otherwise a negative error code + */ +int ksmbd_vfs_stream_truncate(struct ksmbd_file *fp, loff_t newsize) +{ + const struct cred *saved_cred; + struct user_namespace *user_ns = file_mnt_user_ns(fp->filp); + char *stream_buf = NULL, *new_buf = NULL; + ssize_t v_len; + int err; + + if (newsize < 0) + return -EINVAL; + if (newsize > XATTR_SIZE_MAX) + return -EFBIG; + + saved_cred = override_creds(fp->filp->f_cred); + v_len = ksmbd_vfs_getcasexattr(user_ns, fp->filp->f_path.dentry, + fp->stream.name, fp->stream.size, + &stream_buf); + if (v_len < 0) { + err = v_len; + goto out; + } + if (v_len == newsize) { + err = 0; + goto out; + } + + new_buf = stream_buf; + if (newsize > v_len) { + new_buf = kvzalloc(newsize, GFP_KERNEL); + if (!new_buf) { + err = -ENOMEM; + goto out; + } + if (v_len) + memcpy(new_buf, stream_buf, v_len); + } + + err = ksmbd_vfs_setxattr(user_ns, &fp->filp->f_path, fp->stream.name, + new_buf, newsize, 0, true); + if (new_buf != stream_buf) + kvfree(new_buf); +out: + kvfree(stream_buf); + revert_creds(saved_cred); + return err; +} + /** * ksmbd_vfs_write() - vfs helper for smb file write * @work: work diff --git a/fs/ksmbd/vfs.h b/fs/ksmbd/vfs.h index 6d108cba7e0cc..c9b1e4de44f02 100644 --- a/fs/ksmbd/vfs.h +++ b/fs/ksmbd/vfs.h @@ -120,6 +120,7 @@ int ksmbd_vfs_read(struct ksmbd_work *work, struct ksmbd_file *fp, size_t count, int ksmbd_vfs_write(struct ksmbd_work *work, struct ksmbd_file *fp, char *buf, size_t count, loff_t *pos, bool sync, ssize_t *written); +int ksmbd_vfs_stream_truncate(struct ksmbd_file *fp, loff_t newsize); int ksmbd_vfs_fsync(struct ksmbd_work *work, u64 fid, u64 p_id); int ksmbd_vfs_remove_file(struct ksmbd_work *work, const struct path *path); int ksmbd_vfs_link(struct ksmbd_work *work, -- 2.53.0