git: 1086299d7abf - stable/14 - linux: getsockopt(): Simplify exporting groups a bit

From: Olivier Certner <olce_at_FreeBSD.org>
Date: Fri, 10 Oct 2025 17:16:21 UTC
The branch stable/14 has been updated by olce:

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

commit 1086299d7abf67440668513bf387f03a8c667c21
Author:     Olivier Certner <olce@FreeBSD.org>
AuthorDate: 2025-08-29 08:26:59 +0000
Commit:     Olivier Certner <olce@FreeBSD.org>
CommitDate: 2025-10-10 17:15:57 +0000

    linux: getsockopt(): Simplify exporting groups a bit
    
    No functional change (intended).
    
    Go through conversion to a 'l_gid_t' before copying out in order to cope
    with differing group types (except for not representable values, of
    course).  This is what is done, e.g., for getgroups() in 'linux_misc.c'.
    As Linux's group type is the same as ours on all architectures, we could
    as well just stop bothering and copy out our memory representation,
    eliminating the loop here.  Whatever the choice, though, it has to be
    consistent here and there.
    
    Introduce 'out' of type 'l_gid_t' to avoid performing "by hand" array
    arithmetics when copying out.
    
    MFC after:      5 days
    Sponsored by:   The FreeBSD Foundation
    Differential Revision:  https://reviews.freebsd.org/D52280
    
    (cherry picked from commit 10b789d7e14862c77f77f07e8af84d73d73012c6)
---
 sys/compat/linux/linux_socket.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/sys/compat/linux/linux_socket.c b/sys/compat/linux/linux_socket.c
index ee93ba9a6034..50e960528bf5 100644
--- a/sys/compat/linux/linux_socket.c
+++ b/sys/compat/linux/linux_socket.c
@@ -2178,6 +2178,7 @@ static int
 linux_getsockopt_so_peergroups(struct thread *td,
     struct linux_getsockopt_args *args)
 {
+	l_gid_t *out = PTRIN(args->optval);
 	struct xucred xu;
 	socklen_t xulen, len;
 	int error, i;
@@ -2196,13 +2197,12 @@ linux_getsockopt_so_peergroups(struct thread *td,
 		return (error);
 	}
 
-	/*
-	 * "- 1" to skip the primary group.
-	 */
+	/* "- 1" to skip the primary group. */
 	for (i = 0; i < xu.cr_ngroups - 1; i++) {
-		error = copyout(xu.cr_groups + i + 1,
-		    (void *)(args->optval + i * sizeof(l_gid_t)),
-		    sizeof(l_gid_t));
+		/* Copy to cope with a possible type discrepancy. */
+		const l_gid_t g = xu.cr_groups[i + 1];
+
+		error = copyout(&g, out + i, sizeof(l_gid_t));
 		if (error != 0)
 			return (error);
 	}