From: Muhammad Abdullah lz77_match_len() XORs two native-endian 64-bit reads and uses count_trailing_zeros() on the result to find how many leading bytes of the two windows are equal. This is only correct on little-endian. On big-endian the trailing zero bits belong to the last bytes in memory, so the function returns the number of equal bytes at the end of the 8-byte window instead. That number can be smaller than the 4 bytes the caller has already verified to match. A length below 3 underflows in lz77_encode_match() ("len -= 3") and is encoded as a maximum-size match token of 9 or 10 bytes, while srcp only advances by 0 to 2 bytes. smb_lz77_compress() does not bounds check @dst because the output is assumed to never exceed smb_lz77_compressed_alloc_size(). That assumption no longer holds, so on big-endian the compressor writes past the end of the destination buffer, and the stream it produces is corrupt. ksmbd calls smb_lz77_compress() to compress SMB2 READ responses for any SMB 3.1.1 client that negotiated LZ77 and sets SMB2_READFLAG_REQUEST_COMPRESSED, on every architecture. A client that can read a file whose contents it controls can therefore trigger the out-of-bounds write on a big-endian server. cifs.ko reaches the same code for SMB2 WRITE requests when CONFIG_CIFS_COMPRESSION is enabled and compression was negotiated for the mount. Read the windows as little-endian, so that the least significant byte is always the first byte in memory. Generated code is unchanged on little-endian. The problem was found by building lz77.c unmodified for s390x and running it in user space under qemu-s390x, with the destination buffer sized by smb_lz77_compressed_alloc_size(). A structured input of about 50 KB caused more than 200 KB to be written past the end of the destination, and most pseudo-random inputs failed to round-trip through smb_lz77_decompress(). With this patch the same tests show no out-of-bounds writes and every compressed buffer round-trips, on s390x and on arm64. The fix has not been tested in a running big-endian kernel. A reproducer is available to maintainers on request. The LZ77 flag accumulator is also shifted by its full width on 32-bit builds. That is a separate problem, reported in the Link below, and is not addressed here. The analysis, the test harness and this patch were produced with the help of an AI coding assistant (Claude Code). Fixes: 94ae8c3fee94 ("smb: client: compress: LZ77 code improvements cleanup") Link: https://lore.kernel.org/linux-cifs/20260709173019.36808-1-acharyalaxman8848@gmail.com/ Cc: stable@vger.kernel.org Signed-off-by: Muhammad Abdullah --- fs/smb/common/compress/lz77.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/fs/smb/common/compress/lz77.c b/fs/smb/common/compress/lz77.c index c915b114a..03836a140 100644 --- a/fs/smb/common/compress/lz77.c +++ b/fs/smb/common/compress/lz77.c @@ -56,7 +56,12 @@ static __always_inline u32 lz77_read32(const u32 *ptr) static __always_inline u64 lz77_read64(const u64 *ptr) { - return get_unaligned(ptr); + /* + * lz77_match_len() counts trailing zero bits of the XOR of two reads to + * find the number of equal leading bytes, which requires the first byte + * in memory to be the least significant one. + */ + return get_unaligned_le64(ptr); } static __always_inline void lz77_write8(u8 *ptr, u8 v) --- base-commit: 28a23309c8e40d651faf4cc3cc9b71458a11b987 change-id: 20261205-smb-lz77-be-match-len-2ac2afac217d Best regards, -- Muhammad Abdullah