diff mbox series

[RFC,1/5] SUNRPC: reserve space before encode gid

Message ID f48015df-9b1b-43f4-82a1-a47664a7c994@gmail.com (mailing list archive)
State New
Headers show
Series [RFC,1/5] SUNRPC: reserve space before encode gid | expand

Commit Message

Kinglong Mee Oct. 16, 2024, 11:48 a.m. UTC
Reserve space must be done before gid encoding.

Signed-off-by: Kinglong Mee <kinglongmee@gmail.com>
---
 net/sunrpc/auth_unix.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
diff mbox series

Patch

diff --git a/net/sunrpc/auth_unix.c b/net/sunrpc/auth_unix.c
index 1e091d3fa607..9ef3025682bf 100644
--- a/net/sunrpc/auth_unix.c
+++ b/net/sunrpc/auth_unix.c
@@ -113,7 +113,7 @@  unx_marshal(struct rpc_task *task, struct xdr_stream *xdr)
 	struct rpc_clnt	*clnt = task->tk_client;
 	struct rpc_cred	*cred = task->tk_rqstp->rq_cred;
 	__be32		*p, *cred_len, *gidarr_len;
-	int		i;
+	int		i, ngroups;
 	struct group_info *gi = cred->cr_cred->group_info;
 	struct user_namespace *userns = clnt->cl_cred ?
 		clnt->cl_cred->user_ns : &init_user_ns;
@@ -136,14 +136,17 @@  unx_marshal(struct rpc_task *task, struct xdr_stream *xdr)
 	*p++ = cpu_to_be32(from_kgid_munged(userns, cred->cr_cred->fsgid));
 
 	gidarr_len = p++;
-	if (gi)
-		for (i = 0; i < UNX_NGROUPS && i < gi->ngroups; i++)
+	if (gi && gi->ngroups) {
+		ngroups = min(UNX_NGROUPS, gi->ngroups);
+		p = xdr_reserve_space(xdr, ngroups * sizeof(*p));
+		if (!p)
+			goto marshal_failed;
+		for (i = 0; i < ngroups; i++)
 			*p++ = cpu_to_be32(from_kgid_munged(userns, gi->gid[i]));
+	}
+
 	*gidarr_len = cpu_to_be32(p - gidarr_len - 1);
 	*cred_len = cpu_to_be32((p - cred_len - 1) << 2);
-	p = xdr_reserve_space(xdr, (p - gidarr_len - 1) << 2);
-	if (!p)
-		goto marshal_failed;
 
 	/* Verifier */