All of lore.kernel.org
 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

* [PATCH] ksmbd: do not read past a directory entry name in __query_dir()
  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 ` Piyus Kedia
  2026-12-05 12:41   ` Namjae Jeon
  2026-12-05 19:57 ` [PATCH v2] " Piyus Kedia
  1 sibling, 1 reply; 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

__query_dir(), the iterate_dir() actor of SMB2 QUERY_DIRECTORY, gets
each entry as a name and a length, and the name need not be
NUL-terminated: ext2, ext4 without dir_index, xfs, btrfs, f2fs and fuse
pass a pointer into their own directory buffers.  Three of its checks
read it as a C string:

 - strcmp() against "." and "..";

 - ksmbd_share_veto_filename(), whose match_wildcard() walks the name
   up to a NUL;

 - match_pattern(), which is given the length but walks
   while (*s && len), and decrements len only when a character is
   consumed by a match.  A mismatch after a '*' restarts the comparison
   one character further on (str++; s = str;) and leaves len alone, so
   after a mismatch at the first character of an attempt len
   overstates what is left, and the walk goes on to a NUL; after a
   partial match of two or more characters it understates it, and the
   walk stops early.

On ext4 without dir_index, an inline directory's names are emitted
from a kmalloc()ed copy of the inline data, and a 48-byte name ends at
the end of that 60-byte buffer.  A search pattern of "*Z" then reads
past it in match_pattern(), and on a share with a veto list such as
"*.tmp" any QUERY_DIRECTORY reads past it in match_wildcard():

  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)

  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)

  (both: 0 bytes to the right of a 60-byte region in kmalloc-64,
   allocated by ext4_read_inline_dir() at fs/ext4/inline.c:1439)

What lies past the name also decides the answer: in an ext4 directory
without dir_index, "file.txt" followed by another entry is not listed
for "*.txt", because the walk takes in the next entry's inode number.
And the miscount gets terminated names wrong as well: "aaab" does not
match "*aab", and "aaaaab" matches "aa*aa".

Give the 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 long).  match_wildcard() takes no length, so a copy covers it
without a bounded variant of it.  And bound match_pattern() by a
pointer to the end of the name instead of a count, so that restarting
the comparison needs no bookkeeping.

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

Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Closes: https://lore.kernel.org/linux-cifs/ksmbd-match-pattern-overread.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) with KASAN, the same config and
image before and after, only this change between the two builds.  The
reproducer formats an ext4 image with -O inline_data,^dir_index, plays
the ksmbd user-space daemon and, as an SMB 3.1.1 guest over loopback,
lists a directory whose one entry has a 48-byte name, which ends at the
end of the 60-byte inline buffer:

  before   "*Z", no veto list: KASAN in match_pattern, 4 of 4 runs
           "*", veto list "*.tmp": KASAN in match_wildcard, 3 of 3 runs
           both with a 44-byte name instead: no report
  after    "*Z": no report, 4 of 4 runs, STATUS_NO_MORE_FILES as before
           "*", veto list "*.tmp": no report, 3 of 3 runs, the file
           listed as before, and x.tmp in another directory still
           vetoed (checked in 2 of the runs)

The 44-byte controls were not run on the patched kernel.  Of 13
listing queries on the same filesystem, compared with a plain '*'/'?'
matcher, 5 were answered wrong before ("file.txt" missing from "*.txt"
and listed for "*.txt?" and "*.txt\x0f", "aaab" missing from "*aab",
"aaaaab" listed for "aa*aa") and none after.  With only the
match_pattern() half, the match_wildcard() read remained (1 run).

In userspace, the old and new match_pattern() on 24.3 million
NUL-terminated (name, pattern) pairs answer differently only where the
old one disagrees with a plain recursive matcher.

 fs/smb/server/misc.c    |  7 +++----
 fs/smb/server/smb2pdu.c | 19 +++++++++++++++++++
 2 files changed, 22 insertions(+), 4 deletions(-)

diff --git a/fs/smb/server/misc.c b/fs/smb/server/misc.c
index 94781246b4d6..9cc73735759c 100644
--- a/fs/smb/server/misc.c
+++ b/fs/smb/server/misc.c
@@ -22,22 +22,22 @@
  * TODO : implement consideration about DOS_DOT, DOS_QM and DOS_STAR
  *
  * @str:	string to compare with a pattern
- * @len:	string length
+ * @len:	string length; @str need not be NUL-terminated
  * @pattern:	pattern string which might include wildcard '*' and '?'
  *
  * Return:	0 if pattern matched with the string, otherwise non zero value
  */
 int match_pattern(const char *str, size_t len, const char *pattern)
 {
+	const char *end = str + len;
 	const char *s = str;
 	const char *p = pattern;
 	bool star = false;
 
-	while (*s && len) {
+	while (s < end && *s) {
 		switch (*p) {
 		case '?':
 			s++;
-			len--;
 			p++;
 			break;
 		case '*':
@@ -50,7 +50,6 @@ int match_pattern(const char *str, size_t len, const char *pattern)
 		default:
 			if (tolower(*s) == tolower(*p)) {
 				s++;
-				len--;
 				p++;
 			} else {
 				if (!star)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index a7bf61a6fd6a..ea534c4919a8 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -6582,6 +6582,7 @@ struct smb2_query_dir_private {
 
 	struct ksmbd_dir_info	*d_info;
 	int			info_level;
+	char			*name_buf;	/* PATH_MAX, for __query_dir() */
 };
 
 static int process_query_dir_entries(struct smb2_query_dir_private *priv)
@@ -6832,6 +6833,16 @@ static bool __query_dir(struct dir_context *ctx, const char *name, int namlen,
 	priv	= buf->private;
 	d_info	= priv->d_info;
 
+	/*
+	 * The filesystem need not NUL-terminate name, and the checks below
+	 * read it as a string: give them a terminated copy.
+	 */
+	if (namlen >= PATH_MAX)
+		return true;
+	memcpy(priv->name_buf, name, namlen);
+	priv->name_buf[namlen] = '\0';
+	name = priv->name_buf;
+
 	/* dot and dotdot entries are already reserved */
 	if (!strcmp(".", name) || !strcmp("..", name))
 		return true;
@@ -6987,6 +6998,12 @@ int smb2_query_dir(struct ksmbd_work *work)
 
 	mutex_lock(&dir_fp->readdir_lock);
 
+	query_dir_private.name_buf = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
+	if (!query_dir_private.name_buf) {
+		rc = -ENOMEM;
+		goto err_out;
+	}
+
 	if (srch_flag & SMB2_REOPEN || srch_flag & SMB2_RESTART_SCANS) {
 		ksmbd_debug(SMB, "Restart directory scan\n");
 		generic_file_llseek(dir_fp->filp, 0, SEEK_SET);
@@ -7093,6 +7110,7 @@ int smb2_query_dir(struct ksmbd_work *work)
 
 	mutex_unlock(&dir_fp->readdir_lock);
 	kfree(srch_ptr);
+	kfree(query_dir_private.name_buf);
 	ksmbd_fd_put(work, dir_fp);
 	ksmbd_revert_fsids(work);
 	return 0;
@@ -7101,6 +7119,7 @@ int smb2_query_dir(struct ksmbd_work *work)
 	pr_err("error while processing smb2 query dir rc = %d\n", rc);
 	mutex_unlock(&dir_fp->readdir_lock);
 	kfree(srch_ptr);
+	kfree(query_dir_private.name_buf);
 
 err_out2:
 	if (rc == -EINVAL)
-- 
2.34.1


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

* Re: [PATCH] ksmbd: do not read past a directory entry name in __query_dir()
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Namjae Jeon @ 2026-12-05 12:41 UTC (permalink / raw)
  To: Piyus Kedia
  Cc: linux-cifs, Sergey Senozhatsky, Tom Talpey, ChenXiaoSong,
	linux-kernel, Kapil Vaswani

> @@ -6832,6 +6833,16 @@ static bool __query_dir(struct dir_context *ctx, const char *name, int namlen,
>         priv    = buf->private;
>         d_info  = priv->d_info;
>
> +       /*
> +        * The filesystem need not NUL-terminate name, and the checks below
> +        * read it as a string: give them a terminated copy.
> +        */
> +       if (namlen >= PATH_MAX)
> +               return true;
> +       memcpy(priv->name_buf, name, namlen);
 Could we make the veto check use namlen and check the name directly?
That would avoid copying overhead.

> +       priv->name_buf[namlen] = '\0';
> +       name = priv->name_buf;

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

* [PATCH v2] ksmbd: do not read past a directory entry name in __query_dir()
  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 19:57 ` Piyus Kedia
  1 sibling, 0 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-12-05 19:57 UTC (permalink / raw)
  To: Namjae Jeon, linux-cifs
  Cc: Sergey Senozhatsky, Tom Talpey, ChenXiaoSong, linux-kernel,
	Kapil Vaswani

__query_dir(), the iterate_dir() actor of SMB2 QUERY_DIRECTORY, gets
each entry as a name and a length, and the name need not be
NUL-terminated: ext2, ext4 without dir_index, xfs, btrfs, f2fs and fuse
pass a pointer into their own directory buffers.  Three of its checks
read it as a C string:

 - strcmp() against "." and "..";

 - ksmbd_share_veto_filename(), whose match_wildcard() walks the name
   up to a NUL;

 - match_pattern(), which is given the length but walks
   while (*s && len), and decrements len only when a character is
   consumed by a match.  A mismatch after a '*' restarts the comparison
   one character further on (str++; s = str;) and leaves len alone, so
   after a mismatch at the first character of an attempt len
   overstates what is left, and the walk goes on to a NUL; after a
   partial match of two or more characters it understates it, and the
   walk stops early.

On ext4 without dir_index, an inline directory's names are emitted
from a kmalloc()ed copy of the inline data, and a 48-byte name ends at
the end of that 60-byte buffer.  A search pattern of "*Z" then reads
past it in match_pattern(), and on a share with a veto list such as
"*.tmp" any QUERY_DIRECTORY reads past it in match_wildcard():

  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)

  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)

  (both: 0 bytes to the right of a 60-byte region in kmalloc-64,
   allocated by ext4_read_inline_dir() at fs/ext4/inline.c:1439)

What lies past the name also decides the answer: in an ext4 directory
without dir_index, "file.txt" followed by another entry is not listed
for "*.txt", because the walk takes in the next entry's inode number.
And the miscount gets terminated names wrong as well: "aaab" does not
match "*aab", and "aaaaab" matches "aa*aa".

Make all three checks go by the length.  Use name_is_dot_dotdot() for
"." and "..".  Add ksmbd_share_veto_filename_len(), which matches the
veto patterns with a copy of match_wildcard() bounded by the length, so
the veto rules themselves do not change; the open and rename paths,
whose names are NUL-terminated, keep ksmbd_share_veto_filename().  And
bound match_pattern() by a pointer to the end of the name instead of a
count, so that restarting the comparison needs no bookkeeping.

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

Fixes: e2f34481b24d ("cifsd: add server-side procedures for SMB3")
Closes: https://lore.kernel.org/linux-cifs/ksmbd-match-pattern-overread.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>
---
v2:
 - Check the name in place by its length instead of in a NUL-terminated
   copy (Namjae): name_is_dot_dotdot() for "." and "..", and a new
   ksmbd_share_veto_filename_len() for the veto list.  The PATH_MAX
   buffer is gone, and the open and rename paths are unchanged.
 - The bounded match_wildcard() is a static helper in share_config.c
   rather than a length variant in lib/parser.c, so that the fix stays
   within ksmbd.
 - match_pattern() is as in v1.
v1: https://lore.kernel.org/linux-cifs/20261205101738.229603-1-piyus@iiitd.ac.in/

Tested on ksmbd-for-next (d70570cf8003) with KASAN and v1's reproducer
(an ext4 -O inline_data,^dir_index directory whose one entry, a 48-byte
name, ends at the end of the 60-byte inline buffer), now also opening
and renaming under the veto list:

  before   "*Z": KASAN in match_pattern
           "*", veto list "*.tmp": KASAN in match_wildcard
  after    "*Z": no report, STATUS_NO_MORE_FILES as before
           "*", veto list "*.tmp": no report, the file listed as before

Before and after alike, the veto list hides x.tmp from a listing,
refuses opening x.tmp and renaming a file to z.tmp, and allows
renaming it to w.txt.

In userspace, veto_match() on an unterminated buffer of exactly the
name's length agrees with match_wildcard() on the terminated name for
all 21.3 million (name, pattern) pairs up to 6 and 5 characters, under
AddressSanitizer.

 fs/smb/server/mgmt/share_config.c | 60 +++++++++++++++++++++++++++++++
 fs/smb/server/mgmt/share_config.h |  2 ++
 fs/smb/server/misc.c              |  7 ++--
 fs/smb/server/smb2pdu.c           |  5 +--
 4 files changed, 68 insertions(+), 6 deletions(-)

diff --git a/fs/smb/server/mgmt/share_config.c b/fs/smb/server/mgmt/share_config.c
index f85134c38cdd..33714cbdc228 100644
--- a/fs/smb/server/mgmt/share_config.c
+++ b/fs/smb/server/mgmt/share_config.c
@@ -372,3 +372,63 @@ bool ksmbd_share_veto_filename(struct ksmbd_share_config *share,
 	}
 	return false;
 }
+
+/*
+ * match_wildcard() bounded by @len: @name need not be NUL-terminated, as the
+ * names iterate_dir() passes to __query_dir() are not.
+ */
+static bool veto_match(const char *pattern, const char *name, size_t len)
+{
+	const char *end = name + len;
+	const char *s = name;
+	const char *p = pattern;
+	bool star = false;
+
+	while (s < end && *s) {
+		switch (*p) {
+		case '?':
+			s++;
+			p++;
+			break;
+		case '*':
+			star = true;
+			name = s;
+			if (!*++p)
+				return true;
+			pattern = p;
+			break;
+		default:
+			if (*s == *p) {
+				s++;
+				p++;
+			} else {
+				if (!star)
+					return false;
+				name++;
+				s = name;
+				p = pattern;
+			}
+			break;
+		}
+	}
+
+	while (*p == '*')
+		++p;
+	return !*p;
+}
+
+/*
+ * ksmbd_share_veto_filename() for a name of @len bytes that need not be
+ * NUL-terminated.
+ */
+bool ksmbd_share_veto_filename_len(struct ksmbd_share_config *share,
+				   const char *name, size_t len)
+{
+	struct ksmbd_veto_pattern *p;
+
+	list_for_each_entry(p, &share->veto_list, list) {
+		if (veto_match(p->pattern, name, len))
+			return true;
+	}
+	return false;
+}
diff --git a/fs/smb/server/mgmt/share_config.h b/fs/smb/server/mgmt/share_config.h
index 390c966726f4..3ed034a6725b 100644
--- a/fs/smb/server/mgmt/share_config.h
+++ b/fs/smb/server/mgmt/share_config.h
@@ -100,5 +100,7 @@ struct ksmbd_share_config *ksmbd_share_config_get(struct ksmbd_work *work,
 						  const char *name);
 bool ksmbd_share_veto_filename(struct ksmbd_share_config *share,
 			       const char *filename);
+bool ksmbd_share_veto_filename_len(struct ksmbd_share_config *share,
+				   const char *name, size_t len);
 int create_proc_shares(void);
 #endif /* __SHARE_CONFIG_MANAGEMENT_H__ */
diff --git a/fs/smb/server/misc.c b/fs/smb/server/misc.c
index 94781246b4d6..9cc73735759c 100644
--- a/fs/smb/server/misc.c
+++ b/fs/smb/server/misc.c
@@ -22,22 +22,22 @@
  * TODO : implement consideration about DOS_DOT, DOS_QM and DOS_STAR
  *
  * @str:	string to compare with a pattern
- * @len:	string length
+ * @len:	string length; @str need not be NUL-terminated
  * @pattern:	pattern string which might include wildcard '*' and '?'
  *
  * Return:	0 if pattern matched with the string, otherwise non zero value
  */
 int match_pattern(const char *str, size_t len, const char *pattern)
 {
+	const char *end = str + len;
 	const char *s = str;
 	const char *p = pattern;
 	bool star = false;
 
-	while (*s && len) {
+	while (s < end && *s) {
 		switch (*p) {
 		case '?':
 			s++;
-			len--;
 			p++;
 			break;
 		case '*':
@@ -50,7 +50,6 @@ int match_pattern(const char *str, size_t len, const char *pattern)
 		default:
 			if (tolower(*s) == tolower(*p)) {
 				s++;
-				len--;
 				p++;
 			} else {
 				if (!star)
diff --git a/fs/smb/server/smb2pdu.c b/fs/smb/server/smb2pdu.c
index a7bf61a6fd6a..e57f9b699286 100644
--- a/fs/smb/server/smb2pdu.c
+++ b/fs/smb/server/smb2pdu.c
@@ -6833,10 +6833,11 @@ static bool __query_dir(struct dir_context *ctx, const char *name, int namlen,
 	d_info	= priv->d_info;
 
 	/* dot and dotdot entries are already reserved */
-	if (!strcmp(".", name) || !strcmp("..", name))
+	if (name_is_dot_dotdot(name, namlen))
 		return true;
 	d_info->num_scan++;
-	if (ksmbd_share_veto_filename(priv->work->tcon->share_conf, name))
+	if (ksmbd_share_veto_filename_len(priv->work->tcon->share_conf, name,
+					  namlen))
 		return true;
 	if (!match_pattern(name, namlen, priv->search_pattern))
 		return true;
-- 
2.34.1


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

* Re: [PATCH] ksmbd: do not read past a directory entry name in __query_dir()
  2026-12-05 12:41   ` Namjae Jeon
@ 2026-12-05 20:23     ` Piyus Kedia
  0 siblings, 0 replies; 5+ messages in thread
From: Piyus Kedia @ 2026-12-05 20:23 UTC (permalink / raw)
  To: Namjae Jeon
  Cc: linux-cifs, Sergey Senozhatsky, Tom Talpey, ChenXiaoSong,
	linux-kernel, Kapil Vaswani

Hi Namjae,

Sorry, somehow the patch also went out as a reply to this thread. We
have submitted another patch with the fix you suggested and have also
tested it. In particular, we removed additional allocation and copy.
We reused the bound information already available in the code. Please
take a look.

https://lore.kernel.org/all/20261205195745.508424-1-piyus@iiitd.ac.in/

Thanks,
Piyus

On Sat, Dec 5, 2026 at 6:11 PM Namjae Jeon <linkinjeon@kernel.org> wrote:
>
> > @@ -6832,6 +6833,16 @@ static bool __query_dir(struct dir_context *ctx, const char *name, int namlen,
> >         priv    = buf->private;
> >         d_info  = priv->d_info;
> >
> > +       /*
> > +        * The filesystem need not NUL-terminate name, and the checks below
> > +        * read it as a string: give them a terminated copy.
> > +        */
> > +       if (namlen >= PATH_MAX)
> > +               return true;
> > +       memcpy(priv->name_buf, name, namlen);
>  Could we make the veto check use namlen and check the name directly?
> That would avoid copying overhead.
>
> > +       priv->name_buf[namlen] = '\0';
> > +       name = priv->name_buf;

^ 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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.