Skip to contentSkip to conversation

9p: handle long directory entry names in readdir

PATCH Superseded Haobin Wu 3 participants 5 messages Sep 16

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Haobin Wu

author
#0 lore raw

v2 cover 9p: handle long directory entry names in readdir

Hi Dominique,

Thanks for the review, and sorry for the delay. Here is v2 with the changes you and Jan asked for; details below.

A host directory entry whose name is longer than 255 bytes currently makes 9p's readdir fail with -EIO and hides every entry after it, which is how this was noticed on WSL (Plan 9 shares of Windows drives).

Patch 1 drops the copy into the fixed 256-byte p9_dirent::d_name and uses the string p9pdu_vreadf() already allocated, so the listing no longer aborts.

Patch 2 then skips entries whose name is longer than NAME_MAX instead of returning them to userspace, as discussed with Dominique and Jan on
v1: the VFS only enforces PATH_MAX in verify_dirent_name(), and nothing can operate on such a name afterwards anyway.

Changes since v1:
- Use my real name in From/Signed-off-by (Dominique).
- Split into two patches (Dominique).
- Reworded the subject and commit message of patch 1 to describe the existing readdir path and clarify that no allocation is added (Dominique).
- New patch 2 skipping entries longer than NAME_MAX (Dominique, Jan).
- Dropped the bouncing [email protected] address from Cc.

v1: https://lore.kernel.org/all/[email protected]/

Haobin Wu (2):
9p: skip intermediate directory entry name copy in p9dirent_read() 9p: skip directory entries with names longer than NAME_MAX

 fs/9p/vfs_dir.c         | 17 +++++++++++++----
 include/net/9p/client.h |  2 +-
 net/9p/protocol.c       | 12 ++----------
 3 files changed, 16 insertions(+), 15 deletions(-)

base-commit: 028ef9c96e96197026887c0f092424679298aae8

Signature · 1 line
2.54.0 (Apple Git-157)
author
#1 lore raw

PATCH v2 1/2 9p: skip intermediate directory entry name copy in p9dirent_read()

v9fs_dir_readdir_dotl() decodes each entry of a Rreaddir reply with p9dirent_read(), which parses it through p9pdu_readf("Qqbs"). The 's' conversion in p9pdu_vreadf() already allocates a NUL-terminated copy of the name from the wire buffer; p9dirent_read() then strscpy()s that copy into the fixed 256-byte p9_dirent::d_name and frees the original.

The wire format carries the name length in 16 bits, so a name longer than 255 bytes is valid on the wire and may well be valid on the server's filesystem, but it makes strscpy() return -E2BIG. v9fs_dir_readdir_dotl() turns that into -EIO and aborts getdents64(), so every entry after the long one disappears from the listing.

Drop the second copy: keep the string allocated by p9pdu_vreadf() in p9_dirent and let v9fs_dir_readdir_dotl(), its only user, free it once dir_emit() has consumed it. p9_dirent is a short-lived stack object, so the string's lifetime does not change.

Note that the VFS only rejects names of PATH_MAX bytes or more in verify_dirent_name(), so after this change a name between NAME_MAX and PATH_MAX is returned by getdents64() even though any later lookup on it fails with -ENAMETOOLONG. The next patch skips such entries.

Fixes 7751bdb3a095 ("9p: readdir implementation for 9p2000.L")
Assisted-by Codex:gpt-5
Assisted-by Claude:claude-fable-5-1
---
 fs/9p/vfs_dir.c         |  6 +++++-
 include/net/9p/client.h |  2 +-
 net/9p/protocol.c       | 12 ++----------
 3 files changed, 8 insertions(+), 12 deletions(-)
fs/9p/vfs_dir.c +5 -1
index e0d34e4e9076..af00b79d801e 100644
@@ -185,8 +185,12 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
 			if (!dir_emit(ctx, curdirent.d_name,
 				      strlen(curdirent.d_name),
 				      QID2INO(&curdirent.qid),
-				      curdirent.d_type))
+				      curdirent.d_type)) {
+				kfree(curdirent.d_name);
 				return 0;
+			}
+
+			kfree(curdirent.d_name);
 
 			ctx->pos = curdirent.d_off;
 			rdir->head += err;
include/net/9p/client.h +1 -1
index 838a94218b59..9f3079c6f386 100644
@@ -268,7 +268,7 @@ struct p9_dirent {
 	struct p9_qid qid;
 	u64 d_off;
 	unsigned char d_type;
-	char d_name[256];
+	char *d_name;
 };
 
 struct iov_iter;
net/9p/protocol.c +2 -10
index 67b0586d807f..d4335884e0c0 100644
@@ -770,7 +770,7 @@ int p9dirent_read(struct p9_client *clnt, char *buf, int len,
 {
 	struct p9_fcall fake_pdu;
 	int ret;
-	char *nameptr;
+	char *nameptr = NULL;
 
 	fake_pdu.size = len;
 	fake_pdu.capacity = len;
@@ -785,15 +785,7 @@ int p9dirent_read(struct p9_client *clnt, char *buf, int len,
 		return ret;
 	}
 
-	ret = strscpy(dirent->d_name, nameptr, sizeof(dirent->d_name));
-	if (ret < 0) {
-		p9_debug(P9_DEBUG_ERROR,
-			 "On the wire dirent name too long: %s\n",
-			 nameptr);
-		kfree(nameptr);
-		return ret;
-	}
-	kfree(nameptr);
+	dirent->d_name = nameptr;
 
 	return fake_pdu.offset;
 }
Signature · 1 line
2.54.0 (Apple Git-157)
author
#2 lore raw

PATCH v2 2/2 9p: skip directory entries with names longer than NAME_MAX

In reply to Haobin Wu

The 9p wire format carries directory entry names of up to 65535 bytes and nothing on the client checks them against NAME_MAX. Since the previous patch, v9fs_dir_readdir_dotl() passes such names straight to dir_emit(), and the VFS only rejects names of PATH_MAX bytes or more in verify_dirent_name(), as -EIO, which again fails the whole getdents64() call.

A name between NAME_MAX and PATH_MAX is therefore returned to userspace even though every later operation on it fails with -ENAMETOOLONG, and POSIX requires readdir() to only return components of at most NAME_MAX bytes. Nothing can use such an entry, so skip it instead of returning it or failing the listing, reusing the strlen() result that was already computed for dir_emit().

---
 fs/9p/vfs_dir.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/9p/vfs_dir.c +9 -4
index af00b79d801e..ad6fdc99883d 100644
@@ -173,6 +173,7 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
 		}
 
 		while (rdir->head < rdir->tail) {
+			size_t namelen;
 
 			err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
 					    rdir->tail - rdir->head,
@@ -182,10 +183,14 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
 				return -EIO;
 			}
 
-			if (!dir_emit(ctx, curdirent.d_name,
-				      strlen(curdirent.d_name),
-				      QID2INO(&curdirent.qid),
-				      curdirent.d_type)) {
+			namelen = strlen(curdirent.d_name);
+			if (namelen > NAME_MAX) {
+				p9_debug(P9_DEBUG_VFS,
+					 "skipping entry with %zu byte name\n",
+					 namelen);
+			} else if (!dir_emit(ctx, curdirent.d_name, namelen,
+					     QID2INO(&curdirent.qid),
+					     curdirent.d_type)) {
 				kfree(curdirent.d_name);
 				return 0;
 			}
Signature · 1 line
2.54.0 (Apple Git-157)
#3 lore raw

Re: [PATCH v2 2/2] 9p: skip directory entries with names longer than NAME_MAX

Quoted 38 lines · On Wednesday, 16 September 2026 15:54:03 CEST Haobin Wu wrote:

The 9p wire format carries directory entry names of up to 65535 bytes and nothing on the client checks them against NAME_MAX. Since the previous patch, v9fs_dir_readdir_dotl() passes such names straight to dir_emit(), and the VFS only rejects names of PATH_MAX bytes or more in verify_dirent_name(), as -EIO, which again fails the whole getdents64() call.

A name between NAME_MAX and PATH_MAX is therefore returned to userspace even though every later operation on it fails with -ENAMETOOLONG, and POSIX requires readdir() to only return components of at most NAME_MAX bytes. Nothing can use such an entry, so skip it instead of returning it or failing the listing, reusing the strlen() result that was already computed for dir_emit().

Link:
https://lore.kernel.org/all/vz5bum547fqyxf5z4m3x7tuqkuq52jlopm65t7hvynqeulh
7i3@2t4wnhfxs7qv/ Assisted-by: Claude:claude-fable-5-1

---
 fs/9p/vfs_dir.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/9p/vfs_dir.c +1 -0
index af00b79d801e..ad6fdc99883d 100644
@@ -173,6 +173,7 @@ static int v9fs_dir_readdir_dotl(struct file *file,
 struct dir_context *ctx) }
 
 		while (rdir->head < rdir->tail) {
+			size_t namelen;
 
 			err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
 					    rdir->tail - rdir->head,
@@ -182,10 +183,14 @@ static int v9fs_dir_readdir_dotl(struct file *file,
 struct dir_context *ctx) return -EIO;
 			}

fs/9p/vfs_dir.c static int v9fs_dir_readdir_dotl(struct file *file, · line 190

Quoted 10 lines
fs/9p/vfs_dir.c +5 -4
 
-			if (!dir_emit(ctx, curdirent.d_name,
-				      strlen(curdirent.d_name),
-				      QID2INO(&curdirent.qid),
-				      curdirent.d_type)) {
+			namelen = strlen(curdirent.d_name);
+			if (namelen > NAME_MAX) {
+				p9_debug(P9_DEBUG_VFS,
+					 "skipping entry with %zu byte name\n",
+					 namelen);

Fair to say why:

"skip dentry: name length %zu > NAME_MAX"

This is only handled for 9p2000.L so far. For legacy 9p2000(.u) this should then also be limited in v9fs_dir_readdir() IMO.

/Christian

Quoted 6 lines
fs/9p/vfs_dir.c +3 -0
+			} else if (!dir_emit(ctx, curdirent.d_name, namelen,
+					     QID2INO(&curdirent.qid),
+					     curdirent.d_type)) {
 				kfree(curdirent.d_name);
 				return 0;
 			}
#4 lore raw

Re: [PATCH v2 2/2] 9p: skip directory entries with names longer than NAME_MAX

Christian Schoenebeck wrote on Wed, Sep 16, 2026 at 10:02:08PM +0200:

Quoted 8 lines

fs/9p/vfs_dir.c static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx) · line 190

Quoted 4 lines
fs/9p/vfs_dir.c +4 -0
+			if (namelen > NAME_MAX) {
+				p9_debug(P9_DEBUG_VFS,
+					 "skipping entry with %zu byte name\n",
+					 namelen);

Fair to say why:

"skip dentry: name length %zu > NAME_MAX"

Agreed.

I'd also say to keep this as P9_DEBUG_ERROR rather than VFS: it should be rare enough and nobody will enable DEBUG_VFS immediately, this keeps the message at the same log level as it was previously

(P9_DEBUG_ERROR is not printed by default, but I've been meaning to try to change that eventually... Someday™)

Quoted 2 lines

This is only handled for 9p2000.L so far. For legacy 9p2000(.u) this should then also be limited in v9fs_dir_readdir() IMO.

In practice this was already the case because of the processing in p9dirent_read(), so I have no strong opinion here, but I do agree it makes more sense to be symetrical yes.
If done it should be noted as a behavior change in the commit message (or split in yet another commit) though

Signature · 1 line
Dominique Martinet | Asmadeus