[PATCH] ksmbd: do not read past a directory entry name in __query_dir()

From: Piyus Kedia

Date: Sat Dec 05 2026 - 05:17:50 EST


__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@xxxxxxxxxxx/
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Kapil Vaswani <kapil@xxxxxxxxxxxxxxx>
Signed-off-by: Piyus Kedia <piyus@xxxxxxxxxxx>
---
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