Date: Wed, 17 Jun 2009 19:01:47 +0000 (UTC) From: Brooks Davis <brooks@FreeBSD.org> To: src-committers@freebsd.org, svn-src-projects@freebsd.org Subject: svn commit: r194388 - in projects/ngroups: lib/libc/gen lib/libc/rpc usr.bin/id usr.bin/newgrp usr.bin/quota usr.sbin/chown usr.sbin/chroot usr.sbin/jail usr.sbin/jexec usr.sbin/lpr/lpc Message-ID: <200906171901.n5HJ1lCo085225@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: brooks Date: Wed Jun 17 19:01:47 2009 New Revision: 194388 URL: http://svn.freebsd.org/changeset/base/194388 Log: Consistently use arrays of {NGROUPS_MAX}+1 with getgroups() and getgrouplist(). In doing so, use the pattern from the example from the posix manpages where ngroups_max is a long and set to sysconf(_SC_NGROUPS_MAX)+1. In id(1), don't use a combination of getgid() and getgroups() to retrieve the list of groups. There's no need to account for the possibility that getgroups does not return the primary gid, espeically when we only do it in of of the two cases. Modified: projects/ngroups/lib/libc/gen/initgroups.c projects/ngroups/lib/libc/rpc/auth_unix.c projects/ngroups/usr.bin/id/id.c projects/ngroups/usr.bin/newgrp/newgrp.c projects/ngroups/usr.bin/quota/quota.c projects/ngroups/usr.sbin/chown/chown.c projects/ngroups/usr.sbin/chroot/chroot.c projects/ngroups/usr.sbin/jail/jail.c projects/ngroups/usr.sbin/jexec/jexec.c projects/ngroups/usr.sbin/lpr/lpc/lpc.c Modified: projects/ngroups/lib/libc/gen/initgroups.c ============================================================================== --- projects/ngroups/lib/libc/gen/initgroups.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/lib/libc/gen/initgroups.c Wed Jun 17 19:01:47 2009 (r194388) @@ -49,16 +49,18 @@ initgroups(uname, agroup) gid_t agroup; { int ngroups, ret; + long ngroups_max; gid_t *groups; /* * Provide space for one group more than possible to allow * setgroups to fail and set errno. */ - ngroups = sysconf(_SC_NGROUPS_MAX) + 1; - if ((groups = malloc(sizeof(*groups) * ngroups)) == NULL) + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 2; + if ((groups = malloc(sizeof(*groups) * ngroups_max)) == NULL) return (ENOMEM); + ngroups = (int)ngroups_max; getgrouplist(uname, agroup, groups, &ngroups); ret = setgroups(ngroups, groups); free(groups); Modified: projects/ngroups/lib/libc/rpc/auth_unix.c ============================================================================== --- projects/ngroups/lib/libc/rpc/auth_unix.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/lib/libc/rpc/auth_unix.c Wed Jun 17 19:01:47 2009 (r194388) @@ -186,13 +186,14 @@ AUTH * authunix_create_default() { int ngids; + long ngids_max; char machname[MAXHOSTNAMELEN + 1]; uid_t uid; gid_t gid; gid_t *gids; - ngids = sysconf(_SC_NGROUPS_MAX); - gids = malloc(sizeof(gid_t) * ngids); + ngids_max = sysconf(_SC_NGROUPS_MAX) + 1; + gids = malloc(sizeof(gid_t) * ngids_max); if (gids == NULL) return (NULL); @@ -201,7 +202,7 @@ authunix_create_default() machname[sizeof(machname) - 1] = 0; uid = geteuid(); gid = getegid(); - if ((ngids = getgroups(NGROUPS_MAX, gids)) < 0) + if ((ngids = getgroups(ngids_max, gids)) < 0) abort(); if (ngids > NGRPS) ngids = NGRPS; Modified: projects/ngroups/usr.bin/id/id.c ============================================================================== --- projects/ngroups/usr.bin/id/id.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.bin/id/id.c Wed Jun 17 19:01:47 2009 (r194388) @@ -257,7 +257,8 @@ id_print(struct passwd *pw, int use_ggl, struct group *gr; gid_t gid, egid, lastgid; uid_t uid, euid; - int cnt, ngroups, ngroups_max; + int cnt, ngroups; + long ngroups_max; gid_t *groups; const char *fmt; @@ -270,16 +271,16 @@ id_print(struct passwd *pw, int use_ggl, gid = getgid(); } - ngroups_max = sysconf(_SC_NGROUPS_MAX); - if ((groups = malloc(sizeof(gid_t) * (ngroups_max + 1))) == NULL) + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; + if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) err(1, "malloc"); if (use_ggl && pw != NULL) { - ngroups = ngroups_max + 1; + ngroups = ngroups_max; getgrouplist(pw->pw_name, gid, groups, &ngroups); } else { - ngroups = getgroups(ngroups_max + 1, groups); + ngroups = getgroups(ngroups_max, groups); } if (pw != NULL) @@ -365,20 +366,20 @@ void group(struct passwd *pw, int nflag) { struct group *gr; - int cnt, id, lastid, ngroups, ngroups_max; + int cnt, id, lastid, ngroups; + long ngroups_max; gid_t *groups; const char *fmt; - ngroups_max = sysconf(_SC_NGROUPS_MAX); - if ((groups = malloc(sizeof(gid_t) * (ngroups_max + 1))) == NULL) + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; + if ((groups = malloc(sizeof(gid_t) * (ngroups_max))) == NULL) err(1, "malloc"); if (pw) { - ngroups = ngroups_max + 1; + ngroups = ngroups_max; (void) getgrouplist(pw->pw_name, pw->pw_gid, groups, &ngroups); } else { - groups[0] = getgid(); - ngroups = getgroups(ngroups_max, groups + 1) + 1; + ngroups = getgroups(ngroups_max, groups); } fmt = nflag ? "%s" : "%u"; for (lastid = -1, cnt = 0; cnt < ngroups; ++cnt) { Modified: projects/ngroups/usr.bin/newgrp/newgrp.c ============================================================================== --- projects/ngroups/usr.bin/newgrp/newgrp.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.bin/newgrp/newgrp.c Wed Jun 17 19:01:47 2009 (r194388) @@ -147,8 +147,8 @@ static void addgroup(const char *grpname) { gid_t *grps; - long lgid; - int dbmember, i, ngrps, ngrps_max; + long lgid, ngrps_max; + int dbmember, i, ngrps; gid_t egid; struct group *grp; char *ep, *pass; @@ -185,7 +185,7 @@ addgroup(const char *grpname) } } - ngrps_max = sysconf(_SC_NGROUPS_MAX); + ngrps_max = sysconf(_SC_NGROUPS_MAX) + 1; if ((grps = malloc(sizeof(gid_t) * ngrps_max)) == NULL) err(1, "malloc"); if ((ngrps = getgroups(ngrps_max, (gid_t *)grps)) < 0) { Modified: projects/ngroups/usr.bin/quota/quota.c ============================================================================== --- projects/ngroups/usr.bin/quota/quota.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.bin/quota/quota.c Wed Jun 17 19:01:47 2009 (r194388) @@ -117,6 +117,7 @@ int main(int argc, char *argv[]) { int ngroups; + long ngroups_max; gid_t mygid, *gidset; int i, ch, gflag = 0, uflag = 0, errflag = 0; @@ -159,10 +160,11 @@ main(int argc, char *argv[]) errflag += showuid(getuid()); if (gflag) { mygid = getgid(); - ngroups = sysconf(_SC_NGROUPS_MAX); - if ((gidset = malloc(sizeof(gid_t) * ngroups)) == NULL) + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; + if ((gidset = malloc(sizeof(gid_t) * ngroups_max)) + == NULL) err(1, "malloc"); - ngroups = getgroups(ngroups, gidset); + ngroups = getgroups(ngroups_max, gidset); if (ngroups < 0) err(1, "getgroups"); errflag += showgid(mygid); Modified: projects/ngroups/usr.sbin/chown/chown.c ============================================================================== --- projects/ngroups/usr.sbin/chown/chown.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.sbin/chown/chown.c Wed Jun 17 19:01:47 2009 (r194388) @@ -268,7 +268,8 @@ void chownerr(const char *file) { static uid_t euid = -1; - static int ngroups = -1, ngroups_max; + static int ngroups = -1; + static long ngroups_max; gid_t *groups; /* Check for chown without being root. */ @@ -281,7 +282,7 @@ chownerr(const char *file) /* Check group membership; kernel just returns EPERM. */ if (gid != (gid_t)-1 && ngroups == -1 && euid == (uid_t)-1 && (euid = geteuid()) != 0) { - ngroups_max = sysconf(_SC_NGROUPS_MAX); + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) err(1, "malloc"); ngroups = getgroups(ngroups_max, groups); Modified: projects/ngroups/usr.sbin/chroot/chroot.c ============================================================================== --- projects/ngroups/usr.sbin/chroot/chroot.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.sbin/chroot/chroot.c Wed Jun 17 19:01:47 2009 (r194388) @@ -71,7 +71,8 @@ main(argc, argv) const char *shell; gid_t gid, *gidlist; uid_t uid; - int ch, gids, ngroups_max; + int ch, gids; + long ngroups_max; gid = 0; uid = 0; @@ -117,7 +118,7 @@ main(argc, argv) } } - ngroups_max = sysconf(_SC_NGROUPS_MAX); + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; if ((gidlist = malloc(sizeof(gid_t) * ngroups_max)) == NULL) err(1, "malloc"); for (gids = 0; Modified: projects/ngroups/usr.sbin/jail/jail.c ============================================================================== --- projects/ngroups/usr.sbin/jail/jail.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.sbin/jail/jail.c Wed Jun 17 19:01:47 2009 (r194388) @@ -119,6 +119,7 @@ main(int argc, char **argv) size_t sysvallen; int ch, cmdarg, i, jail_set_flags, jid, ngroups, sysval; int hflag, iflag, Jflag, lflag, rflag, uflag, Uflag; + long ngroups_max; unsigned pi; char *ep, *jailname, *securelevel, *username, *JidFile; char errmsg[ERRMSG_SIZE], enforce_statfs[4]; @@ -132,7 +133,7 @@ main(int argc, char **argv) jailname = securelevel = username = JidFile = cleanenv = NULL; fp = NULL; - ngroups_max = sysconf(_SC_NGROUPS_MAX); + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) err(1, "malloc"); Modified: projects/ngroups/usr.sbin/jexec/jexec.c ============================================================================== --- projects/ngroups/usr.sbin/jexec/jexec.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.sbin/jexec/jexec.c Wed Jun 17 19:01:47 2009 (r194388) @@ -72,12 +72,13 @@ main(int argc, char *argv[]) login_cap_t *lcap = NULL; struct passwd *pwd = NULL; gid_t *groups = NULL; - int ch, ngroups, ngroups_max, uflag, Uflag; + int ch, ngroups, uflag, Uflag; + long ngroups_max; char *ep, *username; ch = uflag = Uflag = 0; username = NULL; - ngroups_max = sysconf(_SC_NGROUPS_MAX); + ngroups_max = sysconf(_SC_NGROUPS_MAX) + 1; if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) err(1, "malloc"); Modified: projects/ngroups/usr.sbin/lpr/lpc/lpc.c ============================================================================== --- projects/ngroups/usr.sbin/lpr/lpc/lpc.c Wed Jun 17 18:55:29 2009 (r194387) +++ projects/ngroups/usr.sbin/lpr/lpc/lpc.c Wed Jun 17 19:01:47 2009 (r194388) @@ -356,6 +356,7 @@ ingroup(const char *grname) { static struct group *gptr=NULL; static int ngroups = 0; + static long ngroups_max; static gid_t *groups; register gid_t gid; register int i; @@ -365,10 +366,10 @@ ingroup(const char *grname) warnx("warning: unknown group '%s'", grname); return(0); } - ngroups = sysconf(_SC_NGROUPS_MAX); - if ((groups = malloc(sizeof(gid_t) * ngroups)) == NULL) + ngroups_max = sysconf(_SC_NGROUPS_MAX); + if ((groups = malloc(sizeof(gid_t) * ngroups_max)) == NULL) err(1, "malloc"); - ngroups = getgroups(ngroups, groups); + ngroups = getgroups(ngroups_max, groups); if (ngroups < 0) err(1, "getgroups"); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200906171901.n5HJ1lCo085225>