git: 4cbde86a6de2 - releng/15.2 - linux: Bound native getdents and getdents64 batches
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Thu, 12 Nov 2026 23:41:52 UTC
The branch releng/15.2 has been updated by cperciva:
URL: https://cgit.FreeBSD.org/src/commit/?id=4cbde86a6de284899118b9c4453ddaced9a8b321
commit 4cbde86a6de284899118b9c4453ddaced9a8b321
Author: Hayzam Sherif <hayzam@alchemilla.io>
AuthorDate: 2026-11-09 03:06:11 +0000
Commit: Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2026-11-12 23:41:23 +0000
linux: Bound native getdents and getdents64 batches
linux_getdents() and linux_getdents64() round their native read size up
to DEV_BSIZE before converting entries. For an unaligned caller buffer,
the native batch can contain more entries than fit in the Linux buffer.
Conversion then returns EINVAL even after copying complete entries,
and the native read has already advanced the directory position.
For counts of at least DEV_BSIZE, use min(args->count, MAXBSIZE) instead.
A 768-byte request, such as rustix's initial directory read, now uses a
768-byte native buffer rather than 1024. The converted Linux dirent and
dirent64 records are no larger than their native counterparts, so the
whole translated batch fits. Returning a short batch is allowed, and
consuming the whole native batch avoids needing to rewind the directory
position.
Keep the existing sizing for counts below DEV_BSIZE. This fixes the
oversized-batch case in both getdents interfaces, including legacy
getdents for 32-bit and 64-bit callers. The remaining small-buffer,
copyout-failure and old single-entry readdir problems are outside the
scope of this change.
Approved by: re (cperciva)
Fixes: 110f2567cb51 ("linux_file.c: Fix handling of NFS [...]")
Sponsored by: Alchemilla Ventures Private Limited
Reviewed by: kevans, markj
(cherry picked from commit ec6629628cbed2816f27aed8a6d5f9eb3521c1c1)
(cherry picked from commit 4d419a4118eecbe178de6b22759dae54895befea)
---
sys/compat/linux/linux_file.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/sys/compat/linux/linux_file.c b/sys/compat/linux/linux_file.c
index 9a1f21d5feb7..86c96a000366 100644
--- a/sys/compat/linux/linux_file.c
+++ b/sys/compat/linux/linux_file.c
@@ -451,7 +451,15 @@ linux_getdents(struct thread *td, struct linux_getdents_args *args)
int buflen, error;
size_t retval;
- buflen = min(roundup2(args->count, DEV_BSIZE), MAXBSIZE);
+ /*
+ * Linux dirent records are no larger than native dirents.
+ * For counts of at least DEV_BSIZE, keep the native batch
+ * within the caller's buffer size.
+ */
+ if (args->count >= DEV_BSIZE)
+ buflen = min(args->count, MAXBSIZE);
+ else
+ buflen = roundup2(args->count, DEV_BSIZE);
bufsav = buf = malloc(buflen, M_LINUX, M_WAITOK);
error = linux_getdirentries(td, args->fd, &buf, buflen,
@@ -529,7 +537,15 @@ linux_getdents64(struct thread *td, struct linux_getdents64_args *args)
int buflen, error;
size_t retval;
- buflen = min(roundup2(args->count, DEV_BSIZE), MAXBSIZE);
+ /*
+ * Linux dirent64 records are no larger than native dirents.
+ * For counts of at least DEV_BSIZE, keep the native batch
+ * within the caller's buffer size.
+ */
+ if (args->count >= DEV_BSIZE)
+ buflen = min(args->count, MAXBSIZE);
+ else
+ buflen = roundup2(args->count, DEV_BSIZE);
bufsav = buf = malloc(buflen, M_LINUX, M_WAITOK);
error = linux_getdirentries(td, args->fd, &buf, buflen,