Skip to contentSkip to conversation

ksmbd: fix OOB read in ksmbd_validate_name_reconnect()

PATCH Superseded Dairui Zhang 2 participants 7 messages Sep 27 – Oct 4

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ksmbd: fix OOB read in ksmbd_validate_name_reconnect()

ksmbd_validate_name_reconnect() indexes ab_pathname[share->path_sz + 1] without checking that the path is longer than share->path_sz + 1 (d_path() fills the buffer from the end). A durable fp is not bound to its original share, so a client can preserve a file from a short-path share and reconnect it on a long-path share, making strcmp() read past the PATH_MAX buffer - with a long enough share path this oopses the kernel, and on a mapped page it also gives a byte-equality oracle on adjacent heap (the reconnect succeeds exactly when the out-of-bounds string equals the requested name). A same-share variant with the durable fp at the share root reads 1 byte past the buffer (strlen(ab_pathname) == share->path_sz).

Check the prefix before indexing: the path must be longer than share->path_sz and the next character must be '/'. The comparison for the valid case is unchanged.

Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")
Assisted-by LLM
---
 fs/smb/server/vfs_cache.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
fs/smb/server/vfs_cache.c +7 -3
index 293dab9..52e83cf 100644
@@ -1714,9 +1714,13 @@ int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
 		return -EACCES;
 	}
 
-	if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
-		ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
-		ret = -EINVAL;
+	if (name) {
+		if (strlen(ab_pathname) <= share->path_sz ||
+		    ab_pathname[share->path_sz] != '/' ||
+		    strcmp(&ab_pathname[share->path_sz + 1], name)) {
+			ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
+			ret = -EINVAL;
+		}
 	}
 
 	kfree(pathname);
Signature · 1 line
2.53.0
maintainer
#1 lore raw
Quoted 4 lines
fs/smb/server/vfs_cache.c +0 -0
@@ -1714,9 +1714,13 @@ int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
                return -EACCES;
        }
 

fs/smb/server/vfs_cache.c int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share, · line 1723

Quoted 10 lines
fs/smb/server/vfs_cache.c +7 -3
-       if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
-               ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
-               ret = -EINVAL;
+       if (name) {
+               if (strlen(ab_pathname) <= share->path_sz ||
+                   ab_pathname[share->path_sz] != '/' ||
+                   strcmp(&ab_pathname[share->path_sz + 1], name)) {
+                       ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
+                       ret = -EINVAL;
+               }

This change adds path length and separator checks, but still does not verify that ab_pathname starts with share->path. A reconnect through /srv/b can therefore still accept a durable handle for /srv/a/file when the requested name is file. Please check the share path prefix and component boundary before comparing the relative name.

And can you check Sashiko' review comments ?

https://sashiko.dev/#/patchset/20260927063339.1731061-1-zhangdairui%40gmail.com

Thanks.

#2 lore raw

PATCH v2 1/1 ksmbd: fix OOB read and cross-share confusion in ksmbd_validate_name_reconnect()

In reply to Dairui Zhang

ksmbd_validate_name_reconnect() indexes ab_pathname[share->path_sz + 1] without checking that the path is longer than share->path_sz + 1 (d_path() fills the buffer from the end). A durable fp is not bound to its original share, so a client can preserve a file from a short-path share and reconnect it on a long-path share, making strcmp() read past the PATH_MAX buffer - with a long enough share path this oopses the kernel, and on a mapped page it also gives a byte-equality oracle on adjacent heap (the reconnect succeeds exactly when the out-of-bounds string equals the requested name). A same-share variant with the durable fp at the share root reads 1 byte past the buffer (strlen(ab_pathname) == share->path_sz).

Check before comparing: the path must be longer than share->path_sz, it must start with share->path, and the next character must be '/'. The share root itself is accepted only for an empty name.

Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")
Assisted-by LLM

---
v1 -> v2:
- Also verify the share path prefix and component boundary before comparing the relative name, per Namjae's review (a same-length
share could otherwise pass the separator check for a file from a different share).
- Accept the share-root case explicitly (fp is the share root,
name is empty), per the Sashiko review note - v1 rejected it.
- v1: https://lore.kernel.org/linux-cifs/[email protected]/

---
 fs/smb/server/vfs_cache.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)
fs/smb/server/vfs_cache.c +15 -3
index 293dab9..1acab9a 100644
@@ -1714,9 +1714,21 @@ int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
 		return -EACCES;
 	}
 
-	if (name && strcmp(&ab_pathname[share->path_sz + 1], name)) {
-		ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
-		ret = -EINVAL;
+	if (name) {
+		size_t len = strlen(ab_pathname);
+
+		if (len == share->path_sz && !strncmp(ab_pathname, share->path, len)) {
+			/* the durable fp is the share root itself */
+			if (name[0])
+				ret = -EINVAL;
+		} else if (len <= share->path_sz ||
+			   strncmp(ab_pathname, share->path, share->path_sz) ||
+			   ab_pathname[share->path_sz] != '/' ||
+			   strcmp(&ab_pathname[share->path_sz + 1], name)) {
+			ret = -EINVAL;
+		}
+		if (ret)
+			ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
 	}
 
 	kfree(pathname);
Signature · 1 line
2.53.0
maintainer Applied
#3 lore raw

Re: [PATCH v2] ksmbd: fix OOB read and cross-share confusion in ksmbd_validate_name_reconnect()

Quoted 18 lines · Dairui Zhang

ksmbd_validate_name_reconnect() indexes ab_pathname[share->path_sz + 1] without checking that the path is longer than share->path_sz + 1 (d_path() fills the buffer from the end). A durable fp is not bound to its original share, so a client can preserve a file from a short-path share and reconnect it on a long-path share, making strcmp() read past the PATH_MAX buffer - with a long enough share path this oopses the kernel, and on a mapped page it also gives a byte-equality oracle on adjacent heap (the reconnect succeeds exactly when the out-of-bounds string equals the requested name). A same-share variant with the durable fp at the share root reads 1 byte past the buffer (strlen(ab_pathname) == share->path_sz).

Check before comparing: the path must be longer than share->path_sz, it must start with share->path, and the next character must be '/'. The share root itself is accepted only for an empty name.

Quoted 5 lines
Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")
Assisted-by LLM

Applied it to #ksmbd-for-next.
Thanks!

#4 lore raw

PATCH 1/1 ksmbd: rework ksmbd_validate_name_reconnect() to use a dentry walk

In reply to Dairui Zhang

Namjae,

I went over my v2 patch again and found some problems with it. Sorry for the issues in my earlier patch. I wrote a more rigorous version; could you please take a look and let me know if you see any problems with it?

The string prefix check in v2 rejects legitimate durable reconnects when the share path contains a symlink, because d_path() of the fp is resolved while the configured string is not. The new version replaces the string check with a dentry walk towards share->vfs_path, and also fixes the older case of a share exported at "/".

From c8efcc786146a951091588e5fa7e3c754850cb3c Mon Sep 17 00:00:00 2001
From: Dairui Zhang <[email protected]>
Date: Wed, 30 Sep 2026 03:10:00 +0800
Subject: [PATCH] ksmbd: rework ksmbd_validate_name_reconnect() to use a
dentry walk

The name check in ksmbd_validate_name_reconnect() compares d_path() of the durable fp against share->path as strings. The fp path is resolved, the configured string is not, so when an intermediate component of the share path is a symlink, a legitimate reconnect to the same file on the same share is rejected. Example: /a/link -> /real with the share configured as /a/link/b: a durable handle for /real/b/foo is rejected.

Replace the string checks with an object-based walk from the fp's dentry towards share->vfs_path, the same root the open paths use. Each component is compared against the requested name from its end, under rename_lock so a concurrent rename cannot mix snapshots. Mount roots are detected by mnt_root and crossed with follow_up(), so files in nested mounts (crossmnt) keep working. Reaching the filesystem root without meeting the share root means the fp does not belong to the share. The share root itself only matches an empty name, and an unlinked fp is rejected explicitly. Each walk is limited to 8192 hops and 16 walk attempts, returning -EAGAIN when a limit is hit.

This also fixes reconnects through a share exported at "/", where the original code compared "oo" for /foo. With no PATH_MAX buffer or d_path() left in this function, the out-of-bounds read fixed by v1/v2 cannot reoccur.

Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")

---
Follow-up to the v2 patch in ksmbd-for-next: the string prefix
check still rejects legitimate reconnects on symlinked share
paths, this rework fixes that and the older root-"/" case.
- v2: https://lore.kernel.org/linux-cifs/[email protected]/

---
 fs/smb/server/vfs_cache.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+), 27 deletions(-)
fs/smb/server/vfs_cache.c +88 -27
@@ -11,6 +11,7 @@
 #include <linux/kthread.h>
 #include <linux/freezer.h>
 #include <linux/dcache.h>
+#include <linux/namei.h>
 
 #include "glob.h"
 #include "vfs_cache.h"
@@ -1701,38 +1702,98 @@
 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
 				  struct ksmbd_file *fp, char *name)
 {
-	char *pathname, *ab_pathname;
+	const struct path *root = &share->vfs_path;
+	struct path cur;
+	const char *p, *end;
+	unsigned int seq, attempts = 0, hops;
 	int ret = 0;
 
-	pathname = kmalloc(PATH_MAX, KSMBD_DEFAULT_GFP);
-	if (!pathname)
-		return -EACCES;
-
-	ab_pathname = d_path(&fp->filp->f_path, pathname, PATH_MAX);
-	if (IS_ERR(ab_pathname)) {
-		kfree(pathname);
-		return -EACCES;
-	}
-
-	if (name) {
-		size_t len = strlen(ab_pathname);
-
-		if (len == share->path_sz && !strncmp(ab_pathname, share->path, len)) {
-			/* the durable fp is the share root itself */
-			if (name[0])
+	if (!name)
+		return 0;
+	if (name[0] == '/' || d_unlinked(fp->filp->f_path.dentry))
+		return -EINVAL;
+
+	for (;;) {
+		if (++attempts > 16)
+			return -EAGAIN;
+		ret = 0;
+		p = name;
+		end = name + strlen(name);
+		cur = fp->filp->f_path;
+		path_get(&cur);
+		seq = read_seqbegin(&rename_lock);
+		hops = 0;
+		for (;;) {
+			const char *comp;
+			size_t clen;
+			struct dentry *parent;
+
+			/* Bound the walk so mount/rename storms cannot spin forever. */
+			if (++hops > 8192) {
+				ret = -EAGAIN;
+				break;
+			}
+
+			if (cur.dentry == root->dentry &&
+			    cur.mnt == root->mnt) {
+				/* the share root itself only matches an empty name */
+				if (end != p)
+					ret = -EINVAL;
+				break;
+			}
+
+			if (cur.dentry == cur.mnt->mnt_root) {
+				/*
+				 * Mount root: hop into the parent mount and
+				 * let its mountpoint supply the component.
+				 * Mount topology can change mid-walk
+				 * (mount_lock is not available here), but
+				 * racing that needs local mount privileges.
+				 */
+				if (!follow_up(&cur)) {
+					ret = -EINVAL;
+					break;
+				}
+				continue;
+			}
+
+			for (comp = end; comp > p && comp[-1] != '/'; comp--)
+				;
+			clen = end - comp;
+			if (!clen) {
+				/* name exhausted while the fp is deeper, or empty component */
 				ret = -EINVAL;
-		} else if (len <= share->path_sz ||
-			   strncmp(ab_pathname, share->path, share->path_sz) ||
-			   ab_pathname[share->path_sz] != '/' ||
-			   strcmp(&ab_pathname[share->path_sz + 1], name)) {
-			ret = -EINVAL;
-		}
-		if (ret)
-			ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
-	}
+				break;
+			}
 
-	kfree(pathname);
+			spin_lock(&cur.dentry->d_lock);
+			if (cur.dentry->d_name.len != clen ||
+			    memcmp(cur.dentry->d_name.name, comp, clen))
+				ret = -EINVAL;
+			spin_unlock(&cur.dentry->d_lock);
+			if (ret)
+				break;
+
+			parent = dget_parent(cur.dentry);
+			if (parent == cur.dentry) {
+				/* escaped the mount without reaching the share root */
+				dput(parent);
+				ret = -EINVAL;
+				break;
+			}
+			dput(cur.dentry);
+			cur.dentry = parent;
 
+			end = comp;
+			if (end > p && end[-1] == '/')
+				end--;
+		}
+		path_put(&cur);
+		if (!read_seqretry(&rename_lock, seq))
+			break;
+	}
+	if (ret)
+		ksmbd_debug(SMB, "invalid name reconnect %s\n", name);
 	return ret;
 }
 
Signature · 1 line
2.53.0
maintainer
#5 lore raw

Re: [PATCH] ksmbd: rework ksmbd_validate_name_reconnect() to use a dentry walk

Quoted 2 lines · Dairui Zhang

Namjae,

Hi Dairui,

Quoted 6 lines

I went over my v2 patch again and found some problems with it. Sorry for the issues in my earlier patch. I wrote a more rigorous version; could you please take a look and let me know if you see any problems with it?

Quoted 6 lines

The string prefix check in v2 rejects legitimate durable reconnects when the share path contains a symlink, because d_path() of the fp is resolved while the configured string is not. The new version replaces the string check with a dentry walk towards share->vfs_path, and also fixes the older case of a share exported at "/".

Can you check if the attached patches fix this issue?

Thanks!

From 0274201c77cacfb7f7914a604b2a28c468c5333d Mon Sep 17 00:00:00 2001
From: Namjae Jeon <[email protected]>
Date: Sun, 4 Oct 2026 09:58:56 +0900
Subject: [PATCH 2/2] ksmbd: fix durable reconnects for root shares

When a share exports "/", ksmbd_validate_name_reconnect() expects another slash after the share path. For a file at /foo, this rejects the valid reconnect name "foo" even though the file is in the share.

Compare the requested name with the rendered file path after its leading slash when the resolved share root is "/". Keep the exact-root/empty-name check and reject rendered paths without an initial slash.

Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")
---
 fs/smb/server/vfs_cache.c | 4 ++++
 1 file changed, 4 insertions(+)
fs/smb/server/vfs_cache.c +4 -0
index 38afc5a3799d..ae30ee08e5e8 100644
@@ -1838,6 +1838,10 @@ int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
 			/* the durable fp is the share root itself */
 			if (name[0])
 				ret = -EINVAL;
+		} else if (share_path_sz == 1 && share_path[0] == '/') {
+			if (ab_pathname[0] != '/' ||
+			    strcmp(ab_pathname + 1, name))
+				ret = -EINVAL;
 		} else if (len <= share_path_sz ||
 			   strncmp(ab_pathname, share_path, share_path_sz) ||
 			   ab_pathname[share_path_sz] != '/' ||
Signature · 1 line
2.25.1

From 4505f6085832c2ee6abf723291c3e4db38d5cfc6 Mon Sep 17 00:00:00 2001
From: Namjae Jeon <[email protected]>
Date: Sun, 4 Oct 2026 09:58:34 +0900
Subject: [PATCH 1/2] ksmbd: cache the resolved share path for durable
reconnects

A share configured as /a/link/b, with /a/link pointing to /real, is opened relative to share->vfs_path at /real/b. However, ksmbd_validate_name_reconnect() compares the file's d_path() result against the original /a/link/b string and rejects a valid reconnect.

Render share->vfs_path with d_path() after the existing kern_path() lookup and cache a copy in real_path along with its length. Use the resolved string for durable reconnect name validation. Keep path and path_sz as the configured spelling for existing consumers, including the volume serial calculation and path lookups.

Fixes c8efcc786146 ("ksmbd: add support for durable handles v1/v2")
---
 fs/smb/server/mgmt/share_config.c | 43 +++++++++++++++++++++++++++++--
 fs/smb/server/mgmt/share_config.h |  2 ++
 fs/smb/server/vfs_cache.c         | 12 +++++----
 3 files changed, 50 insertions(+), 7 deletions(-)
fs/smb/server/mgmt/share_config.c +41 -2

71 lines not on this page · raw message

fs/smb/server/mgmt/share_config.h +2 -0
index d157545fe7d1..91691b525907 100644
@@ -16,8 +16,10 @@ struct ksmbd_work;
 struct ksmbd_share_config {
 	char			*name;
 	char			*path;
+	char			*real_path;
 
 	unsigned int		path_sz;
+	unsigned int		real_path_sz;
 	unsigned int		flags;
 	struct list_head	veto_list;
 
fs/smb/server/vfs_cache.c +7 -5
index 7bc95fae4ac0..38afc5a3799d 100644
@@ -1816,6 +1816,8 @@ void ksmbd_free_global_file_table(void)
 int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
 				  struct ksmbd_file *fp, char *name)
 {
+	const char *share_path = share->real_path;
+	size_t share_path_sz = share->real_path_sz;
 	char *pathname, *ab_pathname;
 	int ret = 0;
 
@@ -1832,14 +1834,14 @@ int ksmbd_validate_name_reconnect(struct ksmbd_share_config *share,
 	if (name) {
 		size_t len = strlen(ab_pathname);
 
-		if (len == share->path_sz && !strncmp(ab_pathname, share->path, len)) {
+		if (len == share_path_sz && !strncmp(ab_pathname, share_path, len)) {
 			/* the durable fp is the share root itself */
 			if (name[0])
 				ret = -EINVAL;
-		} else if (len <= share->path_sz ||
-			   strncmp(ab_pathname, share->path, share->path_sz) ||
-			   ab_pathname[share->path_sz] != '/' ||
-			   strcmp(&ab_pathname[share->path_sz + 1], name)) {
+		} else if (len <= share_path_sz ||
+			   strncmp(ab_pathname, share_path, share_path_sz) ||
+			   ab_pathname[share_path_sz] != '/' ||
+			   strcmp(&ab_pathname[share_path_sz + 1], name)) {
 			ret = -EINVAL;
 		}
 		if (ret)
Signature · 1 line
2.25.1
#6 lore raw

Re: [PATCH] ksmbd: rework ksmbd_validate_name_reconnect() to use a dentry walk

In reply to Dairui Zhang

Namjae,

Thanks for the patches. I went through both and they look correct, including the failure-path refcounting. My patch was not good - it was too complicated. Yours is simpler and just as effective.

One corner case: if the share root is unlinked between kern_path() and d_path() at init, real_path can end up with a " (deleted)" suffix, and a later cross-share fp at that literal path would pass the prefix check.