Linux CIFS filesystem development
 help / color / mirror / Atom feed
* [BUG] ksmbd: slab-out-of-bounds reads past a directory entry name in __query_dir()
@ 2026-12-05 10:17 Piyus Kedia
  2026-12-05 10:17 ` [PATCH] ksmbd: do not read " Piyus Kedia
  2026-12-05 19:57 ` [PATCH v2] " Piyus Kedia
  0 siblings, 2 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-12-05 10:17 UTC (permalink / raw)
  To: Namjae Jeon, linux-cifs
  Cc: Sergey Senozhatsky, Tom Talpey, ChenXiaoSong, linux-kernel,
	Kapil Vaswani

Hi,

__query_dir(), the iterate_dir() actor of SMB2 QUERY_DIRECTORY, gets
each entry as the name and length the filesystem emitted, and the name
need not be NUL-terminated.  Three of its checks read it as a C string
(fs/smb/server/smb2pdu.c:6836-6841 on ksmbd-for-next):

  if (!strcmp(".", name) || !strcmp("..", name))
  ...
  if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
  ...
  if (!match_pattern(name, namlen, priv->search_pattern))

Two of them can walk past the end of the name until they find a NUL
byte, and on ext4 without dir_index that reaches past a slab object.

1. The veto check.  ksmbd_share_veto_filename() matches the name
against each pattern of the share's "veto files" list with
match_wildcard() (lib/parser.c), which takes no length and walks while
(*s).  With a veto pattern that starts with '*', such as "*.tmp", it
walks every name that does not match to its end and on.

2. match_pattern().  It is given the length, and walks

  fs/smb/server/misc.c:36
      while (*s && len) {

but len is decremented only when a character is consumed by a match
(misc.c:40 and :53).  When a mismatch after a '*' restarts the
comparison one character further on, len is left alone:

  fs/smb/server/misc.c:56
      if (!star)
          return false;
      str++;
      s = str;
      p = pattern;

so it no longer says how much of the name is left.  After a mismatch
at the first character of an attempt it overstates it, and the walk
goes on past the end of the name.  For a name of 48 'a's and the
search pattern "*Z", every attempt fails at its first character, len
stays 48 throughout, and the 49th attempt starts at name[48]:

  attempt   s at       *s     *p    len
  1         name[0]    'a'    'Z'   48    mismatch: str++, s = str
  2         name[1]    'a'    'Z'   48    mismatch: str++, s = str
  ...
  48        name[47]   'a'    'Z'   48    mismatch: str++, s = str
  49        name[48]   the loop test reads the byte after the name

(The strcmp() against "." and ".." reads at most two bytes past a name
that is "." or "..".)

On ext4 without dir_index, ext4_read_inline_dir() emits an inline
directory's names from a kmalloc(inline_size) copy of the inline data
(fs/ext4/inline.c:1439), 60 bytes when it all fits in i_block.  After
4 bytes of parent inode number and an 8-byte entry header, a 48-byte
name ends at the last byte of that buffer.  On ksmbd-for-next, with
KASAN, a QUERY_DIRECTORY for "*Z" on a share without a veto list:

  BUG: KASAN: slab-out-of-bounds in match_pattern (fs/smb/server/misc.c:36)
  Read of size 1 at addr ffff88801bbff8bc by task kworker/0:2/55
  Workqueue: ksmbd-io handle_ksmbd_work
   match_pattern (fs/smb/server/misc.c:36)
   __query_dir (fs/smb/server/smb2pdu.c:6841)
   ext4_read_inline_dir (fs/ext4/inline.c:1539)
   ext4_readdir (fs/ext4/dir.c:165)
   iterate_dir (fs/readdir.c:114)
   smb2_query_dir (fs/smb/server/smb2pdu.c:7034)
   handle_ksmbd_work (fs/smb/server/server.c:223)

  Allocated by task 55:
   __kmalloc_noprof (mm/slub.c:5556)
   ext4_read_inline_dir (fs/ext4/inline.c:1439)
   ext4_readdir (fs/ext4/dir.c:165)
   iterate_dir (fs/readdir.c:114)
   smb2_query_dir (fs/smb/server/smb2pdu.c:7034)

  The buggy address belongs to the object at ffff88801bbff880
   which belongs to the cache kmalloc-64 of size 64
  The buggy address is located 0 bytes to the right of
   allocated 60-byte region [ffff88801bbff880, ffff88801bbff8bc)

and a QUERY_DIRECTORY for "*", which match_pattern() accepts at once,
on a share whose veto list is "*.tmp":

  BUG: KASAN: slab-out-of-bounds in match_wildcard (lib/parser.c:290)
  Read of size 1 at addr ffff888048c3823c by task kworker/1:1/53
  Workqueue: ksmbd-io handle_ksmbd_work
   match_wildcard (lib/parser.c:290)
   ksmbd_share_veto_filename (fs/smb/server/mgmt/share_config.c:370)
   __query_dir (fs/smb/server/smb2pdu.c:6839)
   ext4_read_inline_dir (fs/ext4/inline.c:1539)
   ext4_readdir (fs/ext4/dir.c:165)
   iterate_dir (fs/readdir.c:114)
   smb2_query_dir (fs/smb/server/smb2pdu.c:7034)

  (the same kind of object: 0 bytes to the right of a 60-byte region
   in kmalloc-64 allocated by ext4_read_inline_dir)

The bytes after the name also decide the answer, and match_pattern()'s
miscount goes wrong the other way too: an attempt that matched k
characters before failing moves s back by k - 1 without giving len
back, so len understates what is left and the walk can stop before
the end of the name.  Measured through QUERY_DIRECTORY on the same
ext4 filesystem, against what a plain '*'/'?' matcher lists:

  - in a directory holding "file.txt" and then "b", "file.txt" is not
    listed for "*.txt", but is listed for "*.txt?" and for "*.txt\x0f",
    and not for "*.txt\x10": the byte after the name is the low byte
    of b's inode number, 15, which is the next entry's first field;
  - "aaab" is not listed for "*aab", and "aaaaab" is listed for
    "aa*aa", though both names are followed by zero padding;
  - "*", "?", "*.txt", "*.TXT", "a*b*c", "README" and "*E*" on names
    followed by zero padding are listed correctly.

How it is reached.  The reproducer makes an ext4 image with
-O inline_data,^dir_index on a loop device, creates a directory
holding one file whose name is 48 'a's, and shares the filesystem.  It
then registers itself as the ksmbd daemon over the ksmbd generic
netlink family, answering the login as a guest and the share config
with or without the veto list "*.tmp", and as an SMB 3.1.1 client over
loopback opens the directory and sends QUERY_DIRECTORY
(FileDirectoryInformation).  The queries answer as they should
(STATUS_NO_MORE_FILES for "*Z", the file for "*"), and KASAN reports
the reads above: the match_pattern() one in 4 of 4 runs, the
match_wildcard() one in 3 of 3.  With a 44-byte name, which leaves
zero padding after it in the buffer, neither reports anything.

Impact - who can trigger it:

  - Any SMB client that can list a directory on a share: an
    authenticated user, or anyone where guest access is enabled (our
    runs used a guest session).  It needs no other request and can be
    done remotely.  On any share, the client chooses the search
    pattern, and a '*' followed by a character that a name does not
    contain, as in "*Z", takes match_pattern() to the byte after that
    name, and on past it unless that byte is a NUL.  On a share with a
    veto pattern that starts with '*', every listing does the same in
    match_wildcard(), whatever the search pattern.

  - What follows a name is up to the filesystem.  From the source,
    ext2, ext4 directories without dir_index (block and inline), xfs,
    btrfs, f2fs and fuse emit pointers into their own directory
    buffers; ext4 with dir_index (the mkfs default) normally emits a
    NUL-terminated copy, as do tmpfs, overlayfs and nfs.  We ran only
    ext4; with -O inline_data alone, that is with dir_index, the
    fuzzer's 48-byte run on 7.4.0-rc5 reported nothing.  Elsewhere the
    walk goes into whatever the filesystem keeps after the name: in
    xfs with the ftype feature (the default), the entry's file type
    byte, which is not zero for a file or a directory; for fuse, bytes
    the fuse daemon wrote.  For the read to leave a slab object, as
    above, a name has to end exactly at the end of an inline
    directory's buffer, which a user who can create files in the
    shared directory can arrange.  KASAN sees nothing when the read
    stays inside the buffer.

  - What the reads do: they read up to the next NUL byte, and let those
    bytes decide whether the entry is listed (or vetoed).  Nothing read
    is copied to the client, but the listing is an oracle on them, as
    the "*.txt\x0f" query above shows for the next entry's inode
    number.  In the inline case on a kernel without KASAN, the bytes
    read are the start of the next slab object, up to its first zero
    byte; we have not measured what a client can learn about them that
    way.  We have not seen a walk fault.

Seen on 7.4.0-rc5 and REPRODUCED ON ksmbd-for-next (d70570cf8003,
2026-12-04) with KASAN; the excerpts above are from ksmbd-for-next.
On 7.4.0-rc5 the same reports came through __query_dir at
smb2pdu.c:6581 (match_pattern) and :6579 (the veto check, with a veto
list of "*z").  The code is the same in mainline (c3500c5ef648,
2026-12-04).  All three checks have read the name this way since
e2f34481b24d ("cifsd: add server-side procedures for SMB3"), v5.15;
b24c93358035 ("cifsd: Pass string length parameter to
match_pattern()"), in the same release, gave match_pattern() its len
bound for names that are not NUL-terminated, but not on the
backtracking path.

A patch follows in reply to this mail.  It does two things:

  - __query_dir() gives its checks a NUL-terminated copy of the name,
    in a PATH_MAX buffer allocated once per request (a fuse name can
    be up to PATH_MAX - 1 bytes).  One copy covers all three checks,
    and any added later; giving each its own bound would need a
    length-taking variant of match_wildcard() in lib/parser.c, and the
    copy costs a memcpy() of each name.

  - match_pattern() is bounded by a pointer to the end of the name
    instead of a count, so that restarting the comparison needs no
    bookkeeping; it still stops at a NUL as before.  With the copy it
    no longer reads past the name either way, but this is what makes
    "*aab" and "aa*aa" answer correctly.

We compared the old and new match_pattern() side by side in userspace,
against a plain recursive matcher, on all 21,330,666 pairs of names
over {a,b,A,B} up to 6 characters and patterns over {a,b,A,*,?} up to
5, and on 3,000,000 random pairs over a wider alphabet, every name
NUL-terminated: they answer differently for 621,384 and 3,919 pairs,
and in each of those the new one agrees with the plain matcher and the
old one does not.  Where both disagree with it, the pattern has two
'*' in a row (the trailing-'*' handling, which the patch leaves
alone).  With each name placed against a PROT_NONE page, the new one
never read past it.

On ksmbd-for-next with KASAN, same config and image, only the patch
between the two builds:

  before  "*Z", 48-byte name       KASAN in match_pattern, 4 of 4
          "*", veto "*.tmp", 48    KASAN in match_wildcard, 3 of 3
          both with 44-byte names  no report (2 runs, 1 run)
          13 listing queries       5 answered wrong (those above)
  after   "*Z", 48-byte name       no report, 4 of 4 runs
          "*", veto "*.tmp", 48    no report, 3 of 3 runs; x.tmp in
                                   another directory still vetoed
                                   (checked in 2 of them)
          13 listing queries       all answered right

(The 44-byte controls were not run on the patched kernel.)

With only the match_pattern() half of the patch, the match_pattern()
read was gone (3 runs) and the match_wildcard() one was not (1 run).

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 derived from the fuzzer's
program, runs the daemon and the client in one program over loopback,
and we are happy to send it on request.

Kapil Vaswani
Piyus Kedia

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

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

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 10:17 [BUG] ksmbd: slab-out-of-bounds reads past a directory entry name in __query_dir() Piyus Kedia
2026-12-05 10:17 ` [PATCH] ksmbd: do not read " Piyus Kedia
2026-12-05 12:41   ` Namjae Jeon
2026-12-05 20:23     ` Piyus Kedia
2026-12-05 19:57 ` [PATCH v2] " Piyus Kedia

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