MFC r202143,202163,202341,202342,204278

Replace the static NGROUPS=NGROUPS_MAX+1=1024 with a dynamic
  kern.ngroups+1.  kern.ngroups can range from NGROUPS_MAX=1023 to
  somewhere in the neighborhood of INT_MAX/4 one a system with sufficent
  RAM and memory bandwidth.  Given that the Windows group limit is
  1024, this range should be sufficient for most applications

r202342:
  Only allocate the space we need before calling kern_getgroups instead
  of allocating what ever the user asks for up to "ngroups_max + 1".  On
  systems with large values of kern.ngroups this will be more efficient.

  The now redundant check that the array is large enough in
  kern_getgroups() is deliberate to allow this change to be merged to
  stable/8 without breaking potential third party consumers of the API.
This commit is contained in:
brooks 2010-02-24 22:16:16 +00:00
parent c2f20743f2
commit 2c6e79090a
11 changed files with 44 additions and 20 deletions

View File

@ -101,6 +101,7 @@ module_path="/boot/modules" # Set the module search path
#kern.maxusers="32" # Set size of various static tables
#kern.nbuf="" # Set the number of buffer headers
#kern.ncallout="" # Set the maximum # of timer events
#kern.ngroups="1023" # Set the maximum # of supplemental groups
#kern.sgrowsiz="" # Set the amount to grow stack
#kern.cam.scsi_delay="2000" # Delay (in ms) before probing SCSI
#kern.ipc.maxsockets="" # Set the maximum number of sockets avaliable

View File

@ -1138,7 +1138,7 @@ linux_setgroups(struct thread *td, struct linux_setgroups_args *args)
struct proc *p;
ngrp = args->gidsetsize;
if (ngrp < 0 || ngrp >= NGROUPS)
if (ngrp < 0 || ngrp >= ngroups_max + 1)
return (EINVAL);
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
error = copyin(args->grouplist, linux_gidset, ngrp * sizeof(l_gid_t));

View File

@ -109,7 +109,7 @@ linux_setgroups16(struct thread *td, struct linux_setgroups16_args *args)
#endif
ngrp = args->gidsetsize;
if (ngrp < 0 || ngrp >= NGROUPS)
if (ngrp < 0 || ngrp >= ngroups_max + 1)
return (EINVAL);
linux_gidset = malloc(ngrp * sizeof(*linux_gidset), M_TEMP, M_WAITOK);
error = copyin(args->gidset, linux_gidset, ngrp * sizeof(l_gid16_t));

View File

@ -708,7 +708,7 @@ svr4_sys_sysconfig(td, uap)
switch (uap->name) {
case SVR4_CONFIG_NGROUPS:
*retval = NGROUPS_MAX;
*retval = ngroups_max;
break;
case SVR4_CONFIG_CHILD_MAX:
*retval = maxproc;

View File

@ -663,9 +663,13 @@ ibcs2_getgroups(td, uap)
u_int i, ngrp;
int error;
if (uap->gidsetsize < 0)
return (EINVAL);
ngrp = MIN(uap->gidsetsize, NGROUPS);
if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
if (uap->gidsetsize == 0)
ngrp = 0;
else
return (EINVAL);
} else
ngrp = td->td_ucred->cr_ngroups;
gp = malloc(ngrp * sizeof(*gp), M_TEMP, M_WAITOK);
error = kern_getgroups(td, &ngrp, gp);
if (error)
@ -693,7 +697,7 @@ ibcs2_setgroups(td, uap)
gid_t *gp;
int error, i;
if (uap->gidsetsize < 0 || uap->gidsetsize > NGROUPS)
if (uap->gidsetsize < 0 || uap->gidsetsize > ngroups_max + 1)
return (EINVAL);
if (uap->gidsetsize && uap->gidset == NULL)
return (EINVAL);

View File

@ -124,8 +124,8 @@ SYSCTL_INT(_kern, KERN_ARGMAX, argmax, CTLFLAG_RD,
SYSCTL_INT(_kern, KERN_POSIX1, posix1version, CTLFLAG_RD,
0, _POSIX_VERSION, "Version of POSIX attempting to comply to");
SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RD,
0, NGROUPS_MAX,
SYSCTL_INT(_kern, KERN_NGROUPS, ngroups, CTLFLAG_RDTUN,
&ngroups_max, 0,
"Maximum number of supplemental groups a user can belong to");
SYSCTL_INT(_kern, KERN_JOB_CONTROL, job_control, CTLFLAG_RD,

View File

@ -283,7 +283,13 @@ getgroups(struct thread *td, register struct getgroups_args *uap)
u_int ngrp;
int error;
ngrp = MIN(uap->gidsetsize, NGROUPS);
if (uap->gidsetsize < td->td_ucred->cr_ngroups) {
if (uap->gidsetsize == 0)
ngrp = 0;
else
return (EINVAL);
} else
ngrp = td->td_ucred->cr_ngroups;
groups = malloc(ngrp * sizeof(*groups), M_TEMP, M_WAITOK);
error = kern_getgroups(td, &ngrp, groups);
if (error)
@ -796,7 +802,7 @@ setgroups(struct thread *td, struct setgroups_args *uap)
gid_t *groups = NULL;
int error;
if (uap->gidsetsize > NGROUPS)
if (uap->gidsetsize > ngroups_max + 1)
return (EINVAL);
groups = malloc(uap->gidsetsize * sizeof(gid_t), M_TEMP, M_WAITOK);
error = copyin(uap->gidset, groups, uap->gidsetsize * sizeof(gid_t));
@ -815,7 +821,7 @@ kern_setgroups(struct thread *td, u_int ngrp, gid_t *groups)
struct ucred *newcred, *oldcred;
int error;
if (ngrp > NGROUPS)
if (ngrp > ngroups_max + 1)
return (EINVAL);
AUDIT_ARG_GROUPSET(groups, ngrp);
newcred = crget();
@ -2022,14 +2028,14 @@ crsetgroups_locked(struct ucred *cr, int ngrp, gid_t *groups)
/*
* Copy groups in to a credential after expanding it if required.
* Truncate the list to NGROUPS if it is too large.
* Truncate the list to (ngroups_max + 1) if it is too large.
*/
void
crsetgroups(struct ucred *cr, int ngrp, gid_t *groups)
{
if (ngrp > NGROUPS)
ngrp = NGROUPS;
if (ngrp > ngroups_max + 1)
ngrp = ngroups_max + 1;
crextend(cr, ngrp);
crsetgroups_locked(cr, ngrp, groups);

View File

@ -40,6 +40,7 @@ __FBSDID("$FreeBSD$");
#include "opt_param.h"
#include "opt_maxusers.h"
#include <sys/limits.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
@ -88,6 +89,7 @@ int maxfiles; /* sys. wide open files limit */
int maxfilesperproc; /* per-proc open files limit */
int ncallout; /* maximum # of timer events */
int nbuf;
int ngroups_max; /* max # groups per process */
int nswbuf;
long maxswzone; /* max swmeta KVA storage */
long maxbcache; /* max buffer cache KVA storage */
@ -228,6 +230,16 @@ init_param1(void)
TUNABLE_ULONG_FETCH("kern.maxssiz", &maxssiz);
sgrowsiz = SGROWSIZ;
TUNABLE_ULONG_FETCH("kern.sgrowsiz", &sgrowsiz);
/*
* Let the administrator set {NGROUPS_MAX}, but disallow values
* less than NGROUPS_MAX which would violate POSIX.1-2008 or
* greater than INT_MAX-1 which would result in overflow.
*/
ngroups_max = NGROUPS_MAX;
TUNABLE_INT_FETCH("kern.ngroups", &ngroups_max);
if (ngroups_max < NGROUPS_MAX)
ngroups_max = NGROUPS_MAX;
}
/*

View File

@ -110,7 +110,7 @@ xdr_authunix_parms(XDR *xdrs, uint32_t *time, struct xucred *cred)
if (!xdr_uint32_t(xdrs, &ngroups))
return (FALSE);
for (i = 0; i < ngroups; i++) {
if (i + 1 < NGROUPS) {
if (i + 1 < ngroups_max + 1) {
if (!xdr_uint32_t(xdrs, &cred->cr_groups[i + 1]))
return (FALSE);
} else {
@ -120,8 +120,8 @@ xdr_authunix_parms(XDR *xdrs, uint32_t *time, struct xucred *cred)
}
if (xdrs->x_op == XDR_DECODE) {
if (ngroups + 1 > NGROUPS)
cred->cr_ngroups = NGROUPS;
if (ngroups + 1 > ngroups_max + 1)
cred->cr_ngroups = ngroups_max + 1;
else
cred->cr_ngroups = ngroups + 1;
}

View File

@ -262,8 +262,8 @@ audit_arg_groupset(gid_t *gidset, u_int gidset_size)
u_int i;
struct kaudit_record *ar;
KASSERT(gidset_size <= NGROUPS,
("audit_arg_groupset: gidset_size > NGROUPS"));
KASSERT(gidset_size <= ngroups_max + 1,
("audit_arg_groupset: gidset_size > (kern.ngroups + 1)"));
ar = currecord();
if (ar == NULL)

View File

@ -64,6 +64,7 @@ extern int boothowto; /* reboot flags, from console subsystem */
extern int bootverbose; /* nonzero to print verbose messages */
extern int maxusers; /* system tune hint */
extern int ngroups_max; /* max # of supplemental groups */
#ifdef INVARIANTS /* The option is always available */
#define KASSERT(exp,msg) do { \