linux: Simplify further getgroups() after 'cr_gid' not in cr_groups[]

No functional change (intended).

While here, fix/improve style a bit and in setgroups().

MFC after:      5 days
MFC to:         stable/15
Sponsored by:   The FreeBSD Foundation
Differential Revision:  https://reviews.freebsd.org/D52276
This commit is contained in:
Olivier Certner
2025-08-28 17:29:33 +02:00
parent f75d0dc533
commit a207833f4f
2 changed files with 32 additions and 40 deletions
+16 -20
View File
@@ -1028,18 +1028,19 @@ linux_nice(struct thread *td, struct linux_nice_args *args)
int
linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
{
const int ngrp = args->gidsetsize;
struct ucred *newcred, *oldcred;
l_gid_t *linux_gidset;
int ngrp, error;
int error;
struct proc *p;
ngrp = args->gidsetsize;
if (ngrp < 0 || ngrp >= ngroups_max)
return (EINVAL);
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));
if (error)
goto out;
newcred = crget();
crextend(newcred, ngrp);
p = td->td_proc;
@@ -1071,34 +1072,29 @@ linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
int
linux_getgroups(struct thread *td, struct linux_getgroups_args *args)
{
struct ucred *cred;
const struct ucred *const cred = td->td_ucred;
l_gid_t *linux_gidset;
gid_t *bsd_gidset;
int bsd_gidsetsz, ngrp, error;
int ngrp, error;
cred = td->td_ucred;
bsd_gidset = cred->cr_groups;
bsd_gidsetsz = cred->cr_ngroups;
ngrp = args->gidsetsize;
if ((ngrp = args->gidsetsize) == 0) {
td->td_retval[0] = bsd_gidsetsz;
if (ngrp == 0) {
td->td_retval[0] = cred->cr_ngroups;
return (0);
}
if (ngrp < bsd_gidsetsz)
if (ngrp < cred->cr_ngroups)
return (EINVAL);
ngrp = 0;
linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
M_LINUX, M_WAITOK);
while (ngrp < bsd_gidsetsz) {
linux_gidset[ngrp] = bsd_gidset[ngrp];
ngrp++;
}
ngrp = cred->cr_ngroups;
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
for (int i = 0; i < ngrp; ++i)
linux_gidset[i] = cred->cr_groups[i];
error = copyout(linux_gidset, args->grouplist, ngrp * sizeof(l_gid_t));
free(linux_gidset, M_LINUX);
if (error)
if (error != 0)
return (error);
td->td_retval[0] = ngrp;
+16 -20
View File
@@ -85,12 +85,12 @@ linux_lchown16(struct thread *td, struct linux_lchown16_args *args)
int
linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
{
const int ngrp = args->gidsetsize;
struct ucred *newcred, *oldcred;
l_gid16_t *linux_gidset;
int ngrp, error;
int error;
struct proc *p;
ngrp = args->gidsetsize;
if (ngrp < 0 || ngrp >= ngroups_max)
return (EINVAL);
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
@@ -100,6 +100,7 @@ linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
free(linux_gidset, M_LINUX);
return (error);
}
newcred = crget();
p = td->td_proc;
PROC_LOCK(p);
@@ -133,34 +134,29 @@ linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
int
linux_getgroups16(struct thread *td, struct linux_getgroups16_args *args)
{
struct ucred *cred;
const struct ucred *const cred = td->td_ucred;
l_gid16_t *linux_gidset;
gid_t *bsd_gidset;
int bsd_gidsetsz, ngrp, error;
int ngrp, error;
cred = td->td_ucred;
bsd_gidset = cred->cr_groups;
bsd_gidsetsz = cred->cr_ngroups;
ngrp = args->gidsetsize;
if ((ngrp = args->gidsetsize) == 0) {
td->td_retval[0] = bsd_gidsetsz;
if (ngrp == 0) {
td->td_retval[0] = cred->cr_ngroups;
return (0);
}
if (ngrp < bsd_gidsetsz)
if (ngrp < cred->cr_ngroups)
return (EINVAL);
ngrp = 0;
linux_gidset = malloc(bsd_gidsetsz * sizeof(*linux_gidset),
M_LINUX, M_WAITOK);
while (ngrp < bsd_gidsetsz) {
linux_gidset[ngrp] = bsd_gidset[ngrp];
ngrp++;
}
ngrp = cred->cr_ngroups;
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_LINUX, M_WAITOK);
for (int i = 0; i < ngrp; ++i)
linux_gidset[i] = cred->cr_groups[i];
error = copyout(linux_gidset, args->gidset, ngrp * sizeof(l_gid16_t));
free(linux_gidset, M_LINUX);
if (error) {
if (error != 0) {
LIN_SDT_PROBE1(uid16, linux_getgroups16, copyout_error, error);
return (error);
}