NetBSD Problem Report #792
From gnats Fri Feb 10 15:55:14 1995
Received: from pain.lcs.mit.edu (pain.lcs.mit.edu [128.52.46.239]) by sun-lamp.cs.berkeley.edu (8.6.9/8.6.9) with ESMTP id PAA00189 for <gnats-bugs@sun-lamp.cs.berkeley.edu>; Fri, 10 Feb 1995 15:55:11 -0800
Message-Id: <199502102339.KAA13806@anarres.mame.mu.oz.au>
Date: Sat, 11 Feb 1995 10:39:08 +1100
From: Daniel Carosone <dan@anarres.mame.mu.oz.au>
Reply-To: dan@anarres.mame.mu.oz.au
To: gnats-bugs@netbsd.org
Subject: su(1) ignores primary group, it and id(1) disagree with kernel on group membership
X-Send-Pr-Version: 3.2
>Number: 792
>Category: bin
>Synopsis: su(1) ignores primary group, it and id(1) disagree with kernel on group membership
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: bin-bug-people
>State: closed
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Fri Feb 10 16:05:05 +0000 1995
>Closed-Date: Fri Jun 27 17:02:20 +0000 1997
>Last-Modified: Fri Jun 27 17:02:42 +0000 1997
>Originator: Daniel Carosone
>Release: today
>Organization:
"bozo softwar confoundation"
>Environment:
System: NetBSD anarres 1.0A NetBSD 1.0A (_anarres_) #10: Thu Jan 26 09:07:33 EST 1995 dan@anarres:/amd/oi/fs/f/l/NetBSD/src/sys/arch/i386/compile/_anarres_ i386
>Description:
I created a temporary account on a new machine while I was setting it
up, with a primary gid of 0. The idea was to let me su and save an
edit of /etc/group.
I discovered that, while id(1) told me I was in group 0, su(1) told me
I was not in the correct group to su.
The su(1) program uses the getgrgid() function, which reads the
/etc/group file for secondary groups. It does not look at the primary
group. id and su should agree, this is clearly a bug, and a simple one
to fix.
I was also unsatified with the fact that it reads the group file to
find out secondary group membership, as this is clearly at odds with
the kernel's permissions checking on filesystem access, and with the
general way that I am accustomed to unix working. To me, the group
membership gets set on login, and is pretty much left alone from
there. Changes to passwd/group files don't become relevant until the
next login for permissions-checking at least.
I went to the source of id(1) to see how it extracted the secondary
group membership from the kernel, so as to use the same mechanism in
su(1). I discovered that id also uses a variant of the /etc/group
database file access routines, and so there is the possibility for
disagreement between id(1) and the kernel.
I find it very confusing when id tells me I am in the right group to
su, and I can't, or when it tells me I am in the right group to access
a file, and I can't.
>How-To-Repeat:
see above
>Fix:
I was going to send a simple fix for su to check the primary group
also, but I think the problem is larger. I don't know how to extract
the secondary group membership list from the kernel without frobbing
kmem or requiring /proc to be mounted -- and I suspect that it may be
`hard' and that's why these programs were (re-)written this way.
If anyone has suggestions to make as to how to accomplish this, let me
know and I'll make the changes. I am tempted to use /proc if it's
there, and if not fall back to the current mechanism while printing a
warning that the output may not reflect the kernel's idea of
reality. su is already an suid-0 program, it could frob kmem. I'd rather
not need id to be. I hope there's a better way.
--
Dan.
>Release-Note:
>Audit-Trail:
From: Daniel Carosone <danielce@ee.mu.oz.au>
To: gnats-bugs@NetBSD.ORG, gnats-admin@NetBSD.ORG
Subject: Re: bin/792: su(1) ignores primary group, it and id(1) disagree with kernel on group membership
Date: Sat, 11 Feb 1995 12:29:47 +1100
Ugh. Here's a fix for su(1) that uses getgroups(), thanks to cgd for
pointing out the existence of this rather important syscall.
Had I not been reading the id(1) sources too quickly, I would have
seen that it does in fact use this, unless you give it a flag, in
which case it uses the files, which was the code fragment I saw. As
for the rest of the ranting, well.. chalk it up to brain-frazzle from
getting up too early in the morning after a late night of merging two
large networks into one. Sorry about the noise.
--
Dan.
*** su.c.orig Wed May 25 22:46:15 1994
--- su.c Sat Feb 11 12:18:28 1995
***************
*** 80,86 ****
register char *p, **g;
struct group *gr;
uid_t ruid, getuid();
! int asme, ch, asthem, fastlogin, prio;
enum { UNSET, YES, NO } iscsh = UNSET;
char *user, *shell, *avshell, *username, *cleanenv[10], **np;
char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
--- 80,87 ----
register char *p, **g;
struct group *gr;
uid_t ruid, getuid();
! gid_t groups[NGROUPS+1];
! int asme, ch, asthem, fastlogin, prio, ngroups, gcnt;
enum { UNSET, YES, NO } iscsh = UNSET;
char *user, *shell, *avshell, *username, *cleanenv[10], **np;
char shellbuf[MAXPATHLEN], avshellbuf[MAXPATHLEN];
***************
*** 155,170 ****
#endif
{
/* only allow those in group zero to su to root. */
! if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0)))
! for (g = gr->gr_mem;; ++g) {
! if (!*g) {
! (void)fprintf(stderr,
! "su: you are not in the correct group to su %s.\n",
! user);
! exit(1);
! }
! if (!strcmp(username, *g))
! break;
}
/* if target requires a password, verify it */
if (*pwd->pw_passwd) {
--- 156,171 ----
#endif
{
/* only allow those in group zero to su to root. */
! if (pwd->pw_uid == 0 && (gr = getgrgid((gid_t)0))) {
! groups[0] = getgid();
! ngroups = getgroups(NGROUPS, groups+1) + 1;
! for (gcnt = 0; gcnt < ngroups+1 && groups[gcnt]; gcnt++); /* empty loop */
! if (ngroups+1 == gcnt) { /* we didn't find a 0 and fell off the end */
! (void)fprintf(stderr,
! "su: you are not in the correct group to su %s.\n",
! user);
! exit(1);
! }
}
/* if target requires a password, verify it */
if (*pwd->pw_passwd) {
State-Changed-From-To: open->closed
State-Changed-By: lukem
State-Changed-When: Fri Jun 27 10:02:20 PDT 1997
State-Changed-Why:
fixed. simpler form of your patch applied
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.