[BUG] smb: client: a DFS target path longer than PATH_MAX is written past its UTF-16 buffer

From: Piyus Kedia

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


Hi,

cifs_strndup_to_utf16() sizes its buffer for at most maxlen bytes of
the string and then converts the whole string:

fs/smb/client/cifs_unicode.c:621 (cifs_strndup_to_utf16)
len = cifs_local_to_utf16_bytes(src, maxlen, cp);
len += 2; /* NULL */
dst = kmalloc(len, GFP_KERNEL);
...
cifsConvertToUTF16(dst, src, strlen(src), cp, remap);

cifs_local_to_utf16_bytes() stops after maxlen bytes.
cifsConvertToUTF16() with remapping - SFM, the default, or SFU with
mapchars - converts srclen bytes, here up to the end of the string;
without remapping it calls cifs_strtoUTF16(target, source, PATH_MAX,
cp) (cifs_unicode.c:452), which stops after PATH_MAX. Two callers
pass maxlen = PATH_MAX:

fs/smb/client/smb2misc.c:539 (cifs_convert_path_to_utf16)
return cifs_strndup_to_utf16(start_of_path, PATH_MAX, &len,
cifs_sb->local_nls, cifs_remap(cifs_sb));

fs/smb/client/smb2ops.c:3274 (smb2_get_dfs_refer)
utf16_path = cifs_strndup_to_utf16(search_name, PATH_MAX,
&utf16_path_len,
nls_codepage, remap);

so a remapped path longer than PATH_MAX bytes is written past the end
of the buffer: two bytes for every character after the ones it was
sized for, then the two-byte terminator. The other callers pass
strlen(src) as maxlen. We reached only the first. smb2_get_dfs_refer()
gets no remapping from the DFS cache (dfs_cache.c:692), so there the
conversion stops at PATH_MAX; it is called with cifs_remap() only from
cifs_inval_name_dfs_link_error() (misc.c:1069), which we did not try.

Such a path reaches cifs_convert_path_to_utf16() in a DFS mount. The
prefix path is taken from the target that the server's referral names
(dfs_parse_target_referral() -> smb3_parse_devname(), dfs.c:48-53,
fs_context.c:682, which do not bound it), cifs_build_path_to_root()
builds the path from it and cifs_is_path_remote() converts that path
(connect.c:3868-3875). With a referral to \127.0.0.1\s2\ followed by
5000 'A':

BUG: KASAN: slab-out-of-bounds in cifsConvertToUTF16.part.0 (fs/smb/client/cifs_unicode.c:562)
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)
cifs_smb3_do_mount (fs/smb/client/cifsfs.c:1132)
smb3_get_tree (fs/smb/client/fs_context.c:917)
vfs_get_tree (fs/super.c:1881)
...
__x64_sys_mount (fs/namespace.c:4540)

page last allocated via order 2, ... (poc)
___kmalloc_large_node (mm/slub.c:5470)
__kmalloc_noprof (mm/slub.c:5519 mm/slub.c:5556)
cifs_strndup_to_utf16 (include/linux/slab.h:995 fs/smb/client/cifs_unicode.c:623)
cifs_convert_path_to_utf16 (fs/smb/client/smb2misc.c:539)
...
Memory state around the buggy address:
>ffff88804d906000: 02 fe fe fe fe fe fe fe fe fe fe fe fe fe fe fe
^

The buffer is 2 * 4096 + 2 = 8194 bytes, a kmalloc_large (order 2)
allocation starting at ffff88804d904000, and the report is for its
first byte past the end. With multi-byte characters it is smaller. In
a mount whose local charset is utf8 a target of 1500 U+4E00 is 4500
bytes, the first 4096 of which hold 1366 characters:

BUG: KASAN: slab-out-of-bounds in cifsConvertToUTF16.part.0 (fs/smb/client/cifs_unicode.c:562)
Write of size 2 at addr ffff888046ed6aae by task poc/9452
...
The buggy address belongs to the object at ffff888046ed6000
which belongs to the cache kmalloc-4k of size 4096
The buggy address is located 0 bytes to the right of
allocated 2734-byte region [ffff888046ed6000, ffff888046ed6aae)

How it is reached. The reproducer is a minimal SMB 3.0 server on
loopback and a mount of //127.0.0.1/dfs in the same program, with
ordinary options (user=, pass=, vers=3.0; iocharset=utf8 for the
second case). The server accepts NTLMSSP as guest and answers
FSCTL_DFS_GET_REFERRALS with one V3 root referral whose target is the
long path. The client follows it, tree-connects to s2 and converts the
prefix path in cifs_is_path_remote(); the server answers the CREATE
with STATUS_OBJECT_NAME_NOT_FOUND, and the mount fails with ENOENT
after the report. The CREATE carried the name read back from the
overrun buffer: 8192 bytes of 'A' (SMB2_open() sends at most PATH_MAX
characters) and, in the second case, 3000 bytes of U+4E00, 266 of them
from past the 2734-byte object. A 100-character target takes the same
route with no report and a 200-byte name in the CREATE.

With "nomapposix" the 5000-character target gave no report:
cifs_strtoUTF16() stopped after PATH_MAX characters, and the CREATE
carried the first 4096 characters of the path, so the client looked up
a shorter path than the one the referral named.

Impact - who can trigger it:

- The SMB server that answers the client's referral request. Unless
the mount has "nodfs", dfs_mount_share() asks the server it mounts
for a referral (dfs.c:363-364) and follows the target it gets, so
any server a client mounts can trigger it; from the code, so can a
server that a referral sends the client to, which the walk asks for
the next referral when it answers STATUS_PATH_NOT_COVERED. Our
server accepted the client's NTLMSSP authentication without
checking it. The prefix path is bounded only by the size of the
referral response, which the client takes if it fits in
CIFSMaxBufSize, 16384 bytes by default, plus the header
(connect.c:1200), so the server can name some 8000 characters.

- It needs the default SFM mapping or mapchars; with "nomapposix"
there is no overflow (above). It also needs a utf8 local charset,
from iocharset=utf8 or CONFIG_NLS_DEFAULT="utf8" (the setting in
the kernel we tested, and in the Ubuntu 6.8 kernel of our test
host). Since fa72921a0c9b ("smb: client: convert DFS target node
name from cache charset on mount"), in 7.4-rc5, the target is
converted from the DFS cache's utf8 to the local charset when the
two differ, without remapping, and that conversion stops after
PATH_MAX characters (dfs_cache.c:129-134). With
iocharset=iso8859-1 the 5000-character target gave no report, and
the CREATE carried 4082 characters: the 4096 that conversion kept,
less the \127.0.0.1\s2\ in front. Before that commit the target
went into the prefix path as the cache holds it, so on earlier
kernels the local charset should not matter; we did not test an
earlier kernel.

- A local unprivileged user cannot trigger it directly: mount(2) of
cifs needs CAP_SYS_ADMIN in the initial user namespace (the
filesystem is not FS_USERNS_MOUNT). Where an administrator allows
user mounts through mount.cifs, the server is the one the
administrator configured; we have not tested that.

- What the write does. The conversion writes the UTF-16 form of the
path, after the client's remapping of the reserved characters, two
bytes per character, so the server chooses how far past the buffer
it goes and, within what a path can hold, what is written there.
Booted with kasan_multi_shot=1, a 4100-character target gave four
reports, for the two-byte writes at offsets 8194, 8196, 8198 and
8200 of the 8194-byte buffer: three characters and the terminator.
With ASCII the buffer is at the start of a 16 KiB order-2
allocation, and with the default CIFSMaxBufSize a referral alone
cannot make the path long enough to write past those 16 KiB: KASAN
reports the write, but nothing else is in those pages. With
three-byte characters the buffer is a kmalloc-4k object, and from
the sizes a target of more than 2047 such characters would write
past the object's 4096 bytes into the next one on a kernel without
KASAN (KASAN lays the cache out with more room per object); we have
not run that, nor tried to make anything of it.

Seen on 7.4.0-rc5, where the fuzzer's referral (the same 5000 'A') gave
the same report at the same lines (cifs_unicode.c:628, smb2misc.c:539,
smb2ops.c:984, connect.c:3875, dfs.c:236, an 8194-byte buffer), and
REPRODUCED ON ksmbd-for-next (d70570cf8003, 2026-12-04), whose
fs/smb/client differs from mainline (c3500c5ef648, 2026-12-04) only in
smbdirect.c, with KASAN: the 5000-'A' referral gave this report in 4
of 4 boots, and the 1500-U+4E00 one the kmalloc-4k report in 3 of 3.
The 100-'A' control gave none in 2 of 2, and neither did the 5000 'A'
with nomapposix or with iocharset=iso8859-1 (1 boot each). The code
is the same in cifs-next (9b62aa64ed00, 2026-12-03).

The mismatch dates from 2503a0dba989 ("CIFS: Add SMB2 support for
is_path_accessible"), v3.6, which added cifs_strndup_to_utf16() and
cifs_convert_path_to_utf16(); remapping then needed mapchars. Since
2baa2682531f ("Remap reserved posix characters by default (part
3/3)"), v3.18, SFM remapping is the default.

A patch follows in reply to this mail. It makes
cifs_strndup_to_utf16() return NULL for a string longer than maxlen,
before it allocates, so the conversion never writes more than was
allocated. The callers report NULL as -ENOMEM. On ksmbd-for-next,
same config and image, the patch the only change between the two
builds:

without the patch the reports above; with nomapposix no report,
but the CREATE carries the first 4096 of the
5000 characters
with the patch 5000 'A', 1500 U+4E00 and 5000 'A' with
nomapposix: the client sends no CREATE for the
long path and the mount fails with ENOMEM; no
report in any of 9 boots, which ran the first 4
times, the second 3 times, the third once, and
4100 'A' with kasan_multi_shot=1 once
both the 100-'A' control: no report, a CREATE with the
200-byte name, and ENOENT

A clearer errno than ENOMEM would need the callers changed; we kept the
patch to the helper. Whether the client should also refuse a referral
target or prefix path longer than PATH_MAX where it parses one, in
dfs_parse_target_referral() or smb3_parse_devname(), we leave to you.

This was found by AI-driven fuzzing built on top of syzkaller; its
analysis was then checked by hand against the code and every claim
above was reproduced. The reproducer is fuzzer-generated, runs a
minimal SMB server and the mount in one program over loopback, and we
are happy to send it on request.

Kapil Vaswani
Piyus Kedia