git: bde575b273ee - main - kern___realpathat(): honor uio_seg argument

From: Konstantin Belousov <kib_at_FreeBSD.org>
Date: Mon, 25 Nov 2024 19:36:49 UTC
The branch main has been updated by kib:

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

commit bde575b273eeb17abe878e43dd018e8373741298
Author:     Konstantin Belousov <kib@FreeBSD.org>
AuthorDate: 2024-11-25 14:07:15 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2024-11-25 19:36:41 +0000

    kern___realpathat(): honor uio_seg argument
    
    Reviewed by:    markj
    Sponsored by:   The FreeBSD Foundation
    MFC after:      1 week
    Differential revision:  https://reviews.freebsd.org/D47739
---
 sys/kern/vfs_cache.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/sys/kern/vfs_cache.c b/sys/kern/vfs_cache.c
index f7178ee006ea..c55e103a1a37 100644
--- a/sys/kern/vfs_cache.c
+++ b/sys/kern/vfs_cache.c
@@ -3290,7 +3290,15 @@ kern___realpathat(struct thread *td, int fd, const char *path, char *buf,
 		    &freebuf, &size);
 	}
 	if (error == 0) {
-		error = copyout(retbuf, buf, min(strlen(retbuf) + 1, size));
+		size_t len;
+
+		len = strlen(retbuf) + 1;
+		if (size < len)
+			error = ENAMETOOLONG;
+		else if (pathseg == UIO_USERSPACE)
+			error = copyout(retbuf, buf, len);
+		else
+			memcpy(buf, retbuf, len);
 		free(freebuf, M_TEMP);
 	}
 out: