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

From: Piyus Kedia

Date: Sat Dec 05 2026 - 14:57:55 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".

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

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