From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=74.125.82.171 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796500672; b=oEdiHIYWFYs/xwEZ67HarUzv6L0UTh9NWZi09WhmrGyyhR37r9JzEFA9qyV0wk3VF93Z QGaHKIBHgZ3G2/ytwzDCTze4ELCQiv0C81wteUcyViwzzwcYmAXOgHRi0olfz2n4N/HRU YFCH6jiB07M6p4bj6E7RSzLTOmig9e6tCkdP98b12y4vRU633FkY5qU1Sze+4RgH7JEfk m7kSEgNL+Plf1fvhDrxB1P1GCDEON/QC/T8bFTZCrAuoxFbhAdr0h52w5zmrZClOC+RsU L8PepLyMyEMCtmfQ/QcLMv+hV2ZK6W736y/cUQ7cllz5y+Wuz58k5hZHa5lQaA2HV1A== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796500672; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=Ni3aXQHy5eZrPbaXyEgsKTHGRcgUNhE7Nds5fbN/weI=; b=UoaRj9hQLwECeHVWMVxjGsMxnxWPLZwQi+P2UyuhftKKZOMjcUWk/oPZcNsK6YVwkuz7 tbEROK4p+nexzU0pb6wiXJEtTRa9a/k/Nn3BUSGPSJlZReZ9FJmOEQ3lY3XLFu8D6d3PB UBvkiY+tl9xRR6vaspn0hOUgZYLMebOaFQ4G7JmH+RSY3M+Fnml6CAmTUN5BD0yfbpSno LaYAYV7m8jTje981kL8ZT/8eYL469v+B18J+AicP0WdvOPfbs+TWIKZRBgnOIVp9X2JuM VInEc9ZtAybMAQRYdjPxcOHwCl4DT3vurruj4m1CM6xZiR2PMmYceJvCkyQvx+V8CNw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=iiitd.ac.in; spf=pass smtp.mailfrom=iiitd.ac.in; dkim=pass header.d=iiitd.ac.in header.i=@iiitd.ac.in header.a=rsa-sha256 header.s=google header.b=C6Nzc4Hq; arc=none smtp.remote-ip=74.125.82.171 Received: from mail-dy1-f171.google.com (mail-dy1-f171.google.com [74.125.82.171]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2644F606A31F for ; Sat, 5 Dec 2026 19:57:52 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 2644F606A31F Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=iiitd.ac.in Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=iiitd.ac.in Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=iiitd.ac.in header.i=@iiitd.ac.in header.a=rsa-sha256 header.s=google header.b=C6Nzc4Hq Received: by mail-dy1-f171.google.com with SMTP id 5a478bee46e88-385c497bf94so5020284eec.1 for ; Sat, 05 Dec 2026 11:57:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iiitd.ac.in; s=google; t=1796500671; x=1797105471; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to:content-type; bh=Ni3aXQHy5eZrPbaXyEgsKTHGRcgUNhE7Nds5fbN/weI=; b=C6Nzc4Hq+IDai03E9cj7sWc3coEdeEcjEG75y8xixegMfPGyRXJ0qh2S+SYF0nc0zH P4O+RrEYTGlHfJ+ooDEBZxJDke3/+wUq0sP3biYF16b5+bI8Nv3TNsG3NP6GmimqNb/s 9DVIIl5j5ZMsuWGf7un8H4uYpEJjxgOV7KneM= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796500671; x=1797105471; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to:content-type; bh=Ni3aXQHy5eZrPbaXyEgsKTHGRcgUNhE7Nds5fbN/weI=; b=yMThNF3vnju4SniwatylYb9Spc3gp6wS4aZ6b3SAoVQRc8gR6NwMQt6X6NsPIwJHiq 65i9/Epb4ONwZ+pNxCFukVNTlt2dcI9XfXhgvc8fDF3qGuEaUhsEs9cwFPZalDPCQSFa NQyrGAR0aPkepS2jmV+LG4a2WjLQEn6ha8cVEzkWa5ocT6dndf/ImZBPHkNRuKq1v1aT 7wq2PiV+mEtI5yzHVPKE6zLhlG2GGEu5/KMrI2ZHuV0zQqit8z0yeR874ygBp+BceGOl EGA/VpX+uInv/QSMlEALNKRBfJ9zAAyw2zIs/Yh4O3NA/x2hdyGfIy32C6WV4pq+xrYA BhJg== X-Forwarded-Encrypted: i=1; AL+hErFKhRIgEvE4fLJPzMwUz6iG+0/2wnz6Itl8AyYNrDTvH5Vd+x/oab/itAv8LzsRzc2UhaehUouGeGO9SWo=@vger.kernel.org X-Gm-Message-State: AB4q+uEKf4dDjrDJ1pcX65+B3scTHSnfPEWiJaoSJQK6oz/DvkYBCg3M l8anciG7UAk20iyo8e2t2E3YJfiAZKUQpwknGcHtVpROw45Ajv2OsyERnXMvO9cdSCQ= X-Gm-Gg: AZFdTZctTVZcTXicvSqhn5e9x7CDjeLQmgjGrcoMMYX7MQ1t5yj92Vyr7iz6akrVU9j 7ntg/4TWhncEU1IsYnayHMUygUkvFwbNoFtAJBbaVgAGucNsWozyLAsPsGUR0xrjCBZypBkCJqj BaCyuxclUFaasOWtxDwshjTaRNnq1sBSa3NaZ8KwIyNSBhaOQQ9PZ03oYKu8BRwij8jSbY7kEbY xfltoQFwaK8CWeUGEFR4OeovsCXB7T7mMhEtOtUcjn9YAzyWrRcB1OZ8bUTYgQT2+JRGMFCuzzD BlpD5ssmTAu+herQl18aWynC2K34KNw1qAj4+l2zo5lyXn5a8KLYlEvDCsmSfJEJ7OlD8f93NqI iFjLGM0gJA5KntSM7ku/d0vTVo/eo9RjExUswHPsxBZKkXJrjrB/kE2krVq5rvAcDcSjy+YOMBR pwHm+11G7oC8jBAU3ZM6sZ+6zOtO7U4lg/xY6v2c9zJ/0WypafP8snd475H/X9L2lsY57O5LGJr Nu1vn+6y1fQ5RtrtkIGYZG/r3H1bk5h7amBSKFlTTBg4qDWDhaqiKnjHZvmOvZZMW0KQJSmTYt6 7aqVT6qBXP72PrEVSOyaLN4MQZkq3/Oy8RzkQPandBBUjyfPGb0x3GuRUqnyQmeg7yVg+bQsJ2C i0t9T3D/YqCXPt08aL24iz3fBt+YZsSq25b4a7RhNTZaOVRcMVTfRay72gginkc6g X-Received: by 2002:a05:693c:2e85:b0:381:a037:b060 with SMTP id 5a478bee46e88-38752169154mr5815848eec.9.1796500671147; Sat, 05 Dec 2026 11:57:51 -0800 (PST) Received: from iiitd-S2600STB.iiitd.edu.in ([103.25.231.160]) by smtp.gmail.com with ESMTPSA id 5a478bee46e88-387521e5c89sm35807249eec.18.2026.12.05.11.57.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 05 Dec 2026 11:57:50 -0800 (PST) From: Piyus Kedia To: Namjae Jeon , linux-cifs@vger.kernel.org Cc: Sergey Senozhatsky , Tom Talpey , ChenXiaoSong , linux-kernel@vger.kernel.org, Kapil Vaswani Subject: [PATCH v2] ksmbd: do not read past a directory entry name in __query_dir() Date: Sun, 6 Dec 2026 01:27:45 +0530 Message-Id: <20261205195745.508424-1-piyus@iiitd.ac.in> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit __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 Signed-off-by: Piyus Kedia --- 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