mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [BUG] smb: client: a DFS target path longer than PATH_MAX is written past its UTF-16 buffer
@ 2026-12-05 19:43 Piyus Kedia
  2026-12-05 19:43 ` [PATCH] smb: client: refuse strings longer than maxlen in cifs_strndup_to_utf16() Piyus Kedia
  0 siblings, 1 reply; 2+ messages in thread
From: Piyus Kedia @ 2026-12-05 19:43 UTC (permalink / raw)
  To: Paulo Alcantara, Namjae Jeon, linux-cifs
  Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	samba-technical, linux-kernel, Kapil Vaswani

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

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] smb: client: refuse strings longer than maxlen in cifs_strndup_to_utf16()
  2026-12-05 19:43 [BUG] smb: client: a DFS target path longer than PATH_MAX is written past its UTF-16 buffer Piyus Kedia
@ 2026-12-05 19:43 ` Piyus Kedia
  0 siblings, 0 replies; 2+ messages in thread
From: Piyus Kedia @ 2026-12-05 19:43 UTC (permalink / raw)
  To: Paulo Alcantara, Namjae Jeon, linux-cifs
  Cc: Ronnie Sahlberg, Shyam Prasad N, Tom Talpey, Bharath SM,
	samba-technical, linux-kernel, Kapil Vaswani

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@iiitd.ac.in/
Cc: stable@vger.kernel.org
Signed-off-by: Kapil Vaswani <kapil@presceralabs.ai>
Signed-off-by: Piyus Kedia <piyus@iiitd.ac.in>
---
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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-12-05 19:43 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-05 19:43 [BUG] smb: client: a DFS target path longer than PATH_MAX is written past its UTF-16 buffer Piyus Kedia
2026-12-05 19:43 ` [PATCH] smb: client: refuse strings longer than maxlen in cifs_strndup_to_utf16() Piyus Kedia

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®