9p: handle long directory entry names in readdir
Subsystems (4)
v3 cover 9p: handle long directory entry names in readdir
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 in the 9p2000.L readdir 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.
Patch 3 applies the same limit to the legacy 9p2000/9p2000.u readdir. It is a separate commit because it changes behaviour there: such names used to be listed.
On patch 3, to be explicit about why this is a behaviour change: the 256-byte limit lives in p9dirent_read(), whose only caller is the 9p2000.L path. v9fs_dir_readdir() parses with p9stat_read(), where p9_wstat::name comes from the 's' conversion (kmalloc(len + 1), no NAME_MAX check), so legacy really did emit over-long names.
Changes since v2:
- Patch 2: say why the entry is skipped in the debug message and log it at P9_DEBUG_ERROR, the level of the strscpy() message it replaces (Christian, Dominique).
- New patch 3: same NAME_MAX limit in v9fs_dir_readdir() for legacy 9p2000(.u), as its own commit (Christian, Dominique).
- Dropped the Assisted-by: trailers.
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.
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Haobin Wu (3):
9p: skip intermediate directory entry name copy in p9dirent_read() 9p: skip directory entries with names longer than NAME_MAX 9p: skip over-long directory entry names for legacy 9p2000 too
fs/9p/vfs_dir.c | 31 +++++++++++++++++++++++++------ include/net/9p/client.h | 2 +- net/9p/protocol.c | 12 ++---------- 3 files changed, 28 insertions(+), 17 deletions(-)
base-commit: 028ef9c96e96197026887c0f092424679298aae8
Signature · 1 line
2.54.0 (Apple Git-157)
PATCH v3 1/3 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.
--- 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
@@ -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
@@ -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
@@ -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)
PATCH v3 2/3 9p: skip directory entries with names longer than NAME_MAX
In reply to Haobin WuThe 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(). Log the skipped entry at P9_DEBUG_ERROR, the same level as the strscpy() failure message this replaces.
--- fs/9p/vfs_dir.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
fs/9p/vfs_dir.c +9 -4
@@ -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_ERROR , +"skip dentry: name length %zu > NAME_MAX\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)
PATCH v3 3/3 9p: skip over-long directory entry names for legacy 9p2000 too
In reply to Haobin WuThe legacy 9p2000 and 9p2000.u readdir path, v9fs_dir_readdir(), parses each entry with p9stat_read() and, unlike the 9p2000.L path, never had a 256-byte name limit: a name of any length up to the wire maximum was handed to dir_emit(). getdents64() therefore returned names longer than NAME_MAX that no later syscall can use, and failed with -EIO for names of PATH_MAX bytes or more.
Apply the same NAME_MAX check as the previous patch does for v9fs_dir_readdir_dotl(), so both protocol variants behave the same.
Note that this is a behaviour change for 9p2000 and 9p2000.u: entries with names longer than NAME_MAX used to be listed and are now skipped.
--- fs/9p/vfs_dir.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)
fs/9p/vfs_dir.c +12 -2
@@ -119,6 +119,8 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) rdir->tail = n; }while (rdir->head < rdir->tail) { +size_t namelen; + err =p9stat_read (fid->clnt, rdir->buf + rdir->head, rdir->tail - rdir->head, &st);if (err <=0 ) {
@@ -126,8 +128,16 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)return -EIO ; } - over = !dir_emit (ctx, st.name,strlen (st.name), -QID2INO (&st.qid),dt_type (&st)); + namelen =strlen (st.name); +if (namelen >NAME_MAX ) { +p9_debug (P9_DEBUG_ERROR , +"skip dentry: name length %zu > NAME_MAX\n" , + namelen); + over = false; + }else { + over = !dir_emit (ctx, st.name, namelen, +QID2INO (&st.qid),dt_type (&st)); + }p9stat_free (&st);if (over)return 0 ;
Signature · 1 line
2.54.0 (Apple Git-157)
Re: [PATCH v3 1/3] 9p: skip intermediate directory entry name copy in p9dirent_read()
In reply to Haobin WuQuoted 17 lines · On Friday, 18 September 2026 04:48:49 CEST Haobin Wu wrote:
v9fs_dir_readdir_dotl()decodes each entry of a Rreaddir reply withp9dirent_read(), which parses it throughp9pdu_readf("Qqbs"). The 's' conversion inp9pdu_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-bytep9_dirent::d_nameand 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-EIOand abortsgetdents64(), so every entry after the long one disappears from the listing.Drop the second copy: keep the string allocated by
p9pdu_vreadf()inp9_direntand letv9fs_dir_readdir_dotl(), its only user, free it oncedir_emit()has consumed it.p9_direntis a short-lived stack object, so the string's lifetime does not change.
Quoted 8 lines
Note that the VFS only rejects names of
PATH_MAXbytes or more inverify_dirent_name(), so after this change a name betweenNAME_MAXandPATH_MAXis returned bygetdents64()even though any later lookup on it fails with-ENAMETOOLONG. The next patch skips such entries.Fixes 7751bdb3a095 ("9p: readdir implementation for 9p2000.L")Signed-off-by Haobin Wu <[email protected]>
Quoted 5 lines
--- fs/9p/vfs_dir.c | 6 +++++- include/net/9p/client.h | 2 +- net/9p/protocol.c | 12 ++---------- 3 files changed, 8 insertions(+), 12 deletions(-)
Re: [PATCH v3 2/3] 9p: skip directory entries with names longer than NAME_MAX
In reply to Haobin WuQuoted 15 lines · On Friday, 18 September 2026 04:48:50 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 todir_emit(), and the VFS only rejects names ofPATH_MAXbytes or more inverify_dirent_name(), as-EIO, which again fails the wholegetdents64()call.A name between
NAME_MAXandPATH_MAXis therefore returned to userspace even though every later operation on it fails with-ENAMETOOLONG, and POSIX requiresreaddir()to only return components of at mostNAME_MAXbytes. Nothing can use such an entry, so skip it instead of returning it or failing the listing, reusing thestrlen()result that was already computed fordir_emit(). Log the skipped entry atP9_DEBUG_ERROR, the same level as thestrscpy()failure message this replaces.
Quoted 3 lines
Quoted 3 lines
--- fs/9p/vfs_dir.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
Christian Schoenebeck
reviewerRe: [PATCH v3 3/3] 9p: skip over-long directory entry names for legacy 9p2000 too
In reply to Haobin WuQuoted 7 lines · On Friday, 18 September 2026 04:48:51 CEST Haobin Wu wrote:
The legacy 9p2000 and 9p2000.u readdir path,
v9fs_dir_readdir(), parses each entry withp9stat_read()and, unlike the 9p2000.L path, never had a 256-byte name limit: a name of any length up to the wire maximum was handed todir_emit().getdents64()therefore returned names longer thanNAME_MAXthat no later syscall can use, and failed with-EIOfor names ofPATH_MAXbytes or more.
Quoted 5 lines
Apply the same
NAME_MAXcheck as the previous patch does forv9fs_dir_readdir_dotl(), so both protocol variants behave the same.Note that this is a behaviour change for 9p2000 and 9p2000.u: entries with names longer than
NAME_MAXused to be listed and are now skipped.
That sentence is a bit misleading, isn't it?
Name length 256..4095:
- before: listed, but unusable (ENAMETOOLONG)
- now: entry skipped, fetch continues
Name length >=4096:
- before: aborts the entire fetch (similar to 9p2000.L side)
- now: entry skipped, fetch continues
But yes, it would be a user-space change nevertheless, so ...
Quoted 21 lines
Suggested-by Christian Schoenebeck <[email protected]>Signed-off-by Haobin Wu <[email protected]>--- fs/9p/vfs_dir.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-)fs/9p/vfs_dir.c +2
-0@@ -119,6 +119,8 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) rdir->tail = n; }while (rdir->head < rdir->tail) { +size_t namelen; + err =p9stat_read (fid->clnt, rdir->buf + rdir->head, rdir->tail - rdir->head, &st);if (err <=0 ) {
fs/9p/vfs_dir.c static int v9fs_dir_readdir(struct file *file, struct · line 132
Quoted 7 lines
fs/9p/vfs_dir.c +2
-2@@ -126,8 +128,16 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) return -EIO; } - over = !dir_emit (ctx, st.name,strlen (st.name), -QID2INO (&st.qid),dt_type (&st)); + namelen =strlen (st.name); +if (namelen >NAME_MAX ) {
... maybe better if (namelen >= PATH_MAX) here for legacy 9p2000(.u)?
fs/9p/vfs_dir.c static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) · line 136
Quoted 4 lines
fs/9p/vfs_dir.c +4
-0+p9_debug (P9_DEBUG_ERROR , +"skip dentry: name length %zu > NAME_MAX\n" , + namelen); + over = false;
Nit: alternative would be init over = false and dropping explicit assignment inside loop branch here. But that's already personal taste level.
/Christian
Quoted 7 lines
fs/9p/vfs_dir.c +4
-0+ }else { + over = !dir_emit (ctx, st.name, namelen, +QID2INO (&st.qid),dt_type (&st)); + }p9stat_free (&st);if (over)return 0 ;
Dominique Martinet
maintainerRe: [PATCH v3 3/3] 9p: skip over-long directory entry names for legacy 9p2000 too
Haobin Wu,
Thanks, I picked up all three patches
Christian Schoenebeck wrote on Fri, Sep 18, 2026 at 02:08:00PM +0200:
Quoted 16 lines · On Friday, 18 September 2026 04:48:51 CEST Haobin Wu wrote:
Quoted 7 lines
The legacy 9p2000 and 9p2000.u readdir path,
v9fs_dir_readdir(), parses each entry withp9stat_read()and, unlike the 9p2000.L path, never had a 256-byte name limit: a name of any length up to the wire maximum was handed todir_emit().getdents64()therefore returned names longer thanNAME_MAXthat no later syscall can use, and failed with-EIOfor names ofPATH_MAXbytes or more.Quoted 5 lines
Apply the same
NAME_MAXcheck as the previous patch does forv9fs_dir_readdir_dotl(), so both protocol variants behave the same.Note that this is a behaviour change for 9p2000 and 9p2000.u: entries with names longer than
NAME_MAXused to be listed and are now skipped.That sentence is a bit misleading, isn't it?
Quoted 7 lines
Name length 256..4095:
- before: listed, but unusable (ENAMETOOLONG)
- now: entry skipped, fetch continuesName length >=4096:
- before: aborts the entire fetch (similar to 9p2000.L side)
- now: entry skipped, fetch continues
Right, I've shamelessly copied that in when applying
https://github.com/martinetd/linux/commit/55d6732b8015705a657d96fc5bd37fbed349624a
Quoted 10 lines
fs/9p/vfs_dir.c static int v9fs_dir_readdir(struct file *file, struct · line 132
Quoted 8 lines
fs/9p/vfs_dir.c +2
-2@@ -126,8 +128,16 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) return -EIO; } - over = !dir_emit (ctx, st.name,strlen (st.name), -QID2INO (&st.qid),dt_type (&st)); + namelen =strlen (st.name); +if (namelen >NAME_MAX ) {... maybe better if (namelen >=
PATH_MAX) here for legacy 9p2000(.u)?
Given things won't work after the name is picked up anyway (from what I tried at least stat didn't work), I don't think it matters much, but I'm fine breaking compatibility here.
We can bring the limit back up to PATH_MAX if someone claims it's useful somewhere.
Quoted 7 lines
fs/9p/vfs_dir.c static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) · line 136
Quoted 4 lines
fs/9p/vfs_dir.c +4
-0+p9_debug (P9_DEBUG_ERROR , +"skip dentry: name length %zu > NAME_MAX\n" , + namelen); + over = false;Nit: alternative would be init over = false and dropping explicit assignment inside loop branch here. But that's already personal taste level.
(I actually had the same comment, so made the swap locally)
Signature · 1 line
Dominique Martinet | Asmadeus