Re: [PATCH v2] ksmbd: do not read past a directory entry name in __query_dir()
From: Namjae Jeon
Date: Sun Dec 06 2026 - 02:04:54 EST
On Sun, Dec 6, 2026 at 4:57 AM Piyus Kedia <piyus@xxxxxxxxxxx> wrote:
>
> __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>
Applied it to #ksmbd-for-next.
Thanks.