Skip to contentSkip to conversation

SUNRPC: reserve space before encode gid

RFC PATCH In review Kinglong Mee 1 participant 1 message Oct 16, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/5 SUNRPC: reserve space before encode gid

Reserve space must be done before gid encoding.

---
 net/sunrpc/auth_unix.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
net/sunrpc/auth_unix.c +9 -6
index 1e091d3fa607..9ef3025682bf 100644
@@ -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 */
 
Signature · 1 line
2.47.0