[PATCH] smb: client: refuse strings longer than maxlen in cifs_strndup_to_utf16()

From: Piyus Kedia

Date: Sat Dec 05 2026 - 14:43:23 EST


cifs_strndup_to_utf16() sizes its buffer for at most maxlen bytes of
src, with cifs_local_to_utf16_bytes(src, maxlen, cp), and then converts
src with cifsConvertToUTF16(dst, src, strlen(src), cp, remap). When
the path is remapped, as it is by default (SFM, or SFU with mapchars),
cifsConvertToUTF16() converts up to the end of src, so a src longer
than maxlen is written past the end of dst: two bytes for every
character after the ones dst was sized for, then the terminator.

cifs_convert_path_to_utf16() and smb2_get_dfs_refer() pass PATH_MAX,
and the path is not bounded by it. The prefix path of a DFS mount
comes from the target that the server's referral names, and
cifs_is_path_remote() converts it. A referral to \127.0.0.1\s2\
followed by 5000 'A' makes the client write 10002 bytes into an
8194-byte buffer:

BUG: KASAN: slab-out-of-bounds in cifsConvertToUTF16.part.0+0x1cb/0x8f0
Write of size 2 at addr ffff88804d906002 by task poc/9451
cifsConvertToUTF16.part.0 (fs/smb/client/cifs_unicode.c:562)
cifs_strndup_to_utf16 (fs/smb/client/cifs_unicode.c:628)
cifs_convert_path_to_utf16 (fs/smb/client/smb2misc.c:539)
smb2_is_path_accessible (fs/smb/client/smb2ops.c:984)
cifs_is_path_remote (fs/smb/client/connect.c:3875)
dfs_mount_share (fs/smb/client/dfs.c:236 fs/smb/client/dfs.c:274
fs/smb/client/dfs.c:291 fs/smb/client/dfs.c:385)
cifs_mount (fs/smb/client/connect.c:3942)

With three-byte UTF-8 characters the first PATH_MAX bytes hold only
1366 of them, and a target of 1500 U+4E00 is written over 3002 bytes of
a 2734-byte kmalloc-4k object.

Refuse a src longer than maxlen. The callers treat a NULL return as an
allocation failure, so such a mount now fails with -ENOMEM. Without
remapping, cifsConvertToUTF16() stops after PATH_MAX characters by
itself, so there was no overflow, but the path was cut to its first
PATH_MAX characters and sent to the server like that; it is refused now
as well. The other callers pass strlen(src) as maxlen and are not
affected.

We found this with a syzkaller-based fuzzing harness driven by an AI tool.

Fixes: 2503a0dba989 ("CIFS: Add SMB2 support for is_path_accessible")
Closes: https://lore.kernel.org/r/cifs-utf16-long-path.20261205@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
Tested on ksmbd-for-next (d70570cf8003), whose fs/smb/client differs
from mainline's only in smbdirect.c, with KASAN; this patch was the
only change between the two builds. The reproducer is a minimal SMB
3.0 server on loopback whose DFS referral names \127.0.0.1\s2\
followed by N characters, and a mount of it:

before 5000 'A': the report above, 4 of 4 boots; 1500 U+4E00 with
iocharset=utf8: the write past the 2734-byte kmalloc-4k
object, 3 of 3; 5000 'A' with nomapposix: no report, but
the CREATE carries the first 4096 characters
after the same three: no CREATE for the long path, and the mount
fails with ENOMEM; no report in 9 boots (the first 4
times, the second 3 times, the third once, and 4100 'A'
with kasan_multi_shot=1 once)
both 100 'A': no report, the CREATE carries the 200-byte name and
the mount fails with ENOENT

The patch applies to cifs-next (9b62aa64ed00) and mainline
(c3500c5ef648).

fs/smb/client/cifs_unicode.c | 10 ++++++++++
1 file changed, 10 insertions(+)

diff --git a/fs/smb/client/cifs_unicode.c b/fs/smb/client/cifs_unicode.c
index 264595633b1e..fe458c1f1652 100644
--- a/fs/smb/client/cifs_unicode.c
+++ b/fs/smb/client/cifs_unicode.c
@@ -618,6 +618,16 @@ cifs_strndup_to_utf16(const char *src, const int maxlen, int *utf16_len,
if (!src)
return NULL;

+ /*
+ * dst is sized for at most maxlen bytes of src, and
+ * cifsConvertToUTF16() converts up to the end of src, so refuse a
+ * longer src rather than write past the end of dst.
+ */
+ if (strlen(src) > maxlen) {
+ *utf16_len = 0;
+ return NULL;
+ }
+
len = cifs_local_to_utf16_bytes(src, maxlen, cp);
len += 2; /* NULL */
dst = kmalloc(len, GFP_KERNEL);
--
2.34.1