git: ec6629628cbe - main - linux: Bound native getdents and getdents64 batches

From: Kyle Evans <kevans_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 03:08:35 UTC
The branch main has been updated by kevans:

URL: https://cgit.FreeBSD.org/src/commit/?id=ec6629628cbed2816f27aed8a6d5f9eb3521c1c1

commit ec6629628cbed2816f27aed8a6d5f9eb3521c1c1
Author:     Hayzam Sherif <hayzam@alchemilla.io>
AuthorDate: 2026-11-09 03:06:11 +0000
Commit:     Kyle Evans <kevans@FreeBSD.org>
CommitDate: 2026-11-09 03:06:11 +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.
    
    Fixes:  110f2567cb51 ("linux_file.c: Fix handling of NFS [...]")
    MFC after:      3 days
    Sponsored by:   Alchemilla Ventures Private Limited
    Reviewed by:    kevans, markj
    Differential Revision:  https://reviews.freebsd.org/D60760
---
 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 023da9a24772..851064537b39 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,