Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 4 Nov 2014 20:41:20 +0800
From:      Tiwei Bie <btw@mail.ustc.edu.cn>
To:        Mateusz Guzik <mjguzik@gmail.com>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: [PATCH] Finish the task 'Embedd group table into struct ucred'
Message-ID:  <20141104124120.GA11252@freebsd>
In-Reply-To: <20141104100130.GC4032@dft-labs.eu>
References:  <1415094686-39302-1-git-send-email-btw@mail.ustc.edu.cn> <20141104100130.GC4032@dft-labs.eu>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Nov 04, 2014 at 11:01:30AM +0100, Mateusz Guzik wrote:
> On Tue, Nov 04, 2014 at 05:51:26PM +0800, Tiwei Bie wrote:
> > diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c
> > index 9b2bcd8..76d2cfc 100644
> > --- a/sys/kern/kern_prot.c
> > +++ b/sys/kern/kern_prot.c
> > @@ -1864,7 +1864,8 @@ crfree(struct ucred *cr)
> >  #ifdef MAC
> >  		mac_cred_destroy(cr);
> >  #endif
> > -		free(cr->cr_groups, M_CRED);
> > +		if (cr->cr_groups != cr->cr_smallgroups)
> > +			free(cr->cr_groups, M_CRED);
> >  		free(cr, M_CRED);
> >  	}
> >  }
> > @@ -1976,6 +1977,12 @@ crextend(struct ucred *cr, int n)
> >  	if (n <= cr->cr_agroups)
> >  		return;
> >  
> > +	if (n <= XU_NGROUPS) {
> > +		cr->cr_groups = cr->cr_smallgroups;
> > +		cr->cr_agroups = XU_NGROUPS;
> > +		return;
> > +	}
> > +
> 
> This should be initialized in crget, which should no longer call this
> function.

Sorry for my delay...

Yeah, you are right. I didn't notice it... :-(  My new patch:

diff --git a/sys/kern/kern_prot.c b/sys/kern/kern_prot.c
index 9b2bcd8..c83af66 100644
--- a/sys/kern/kern_prot.c
+++ b/sys/kern/kern_prot.c
@@ -1817,7 +1817,9 @@ crget(void)
 #ifdef MAC
 	mac_cred_init(cr);
 #endif
-	crextend(cr, XU_NGROUPS);
+	cr->cr_groups = cr->cr_smallgroups;
+	cr->cr_agroups = XU_NGROUPS;
+
 	return (cr);
 }
 
@@ -1864,7 +1866,8 @@ crfree(struct ucred *cr)
 #ifdef MAC
 		mac_cred_destroy(cr);
 #endif
-		free(cr->cr_groups, M_CRED);
+		if (cr->cr_groups != cr->cr_smallgroups)
+			free(cr->cr_groups, M_CRED);
 		free(cr, M_CRED);
 	}
 }
@@ -1997,7 +2000,7 @@ crextend(struct ucred *cr, int n)
 		cnt = roundup2(n, PAGE_SIZE / sizeof(gid_t));
 
 	/* Free the old array. */
-	if (cr->cr_groups)
+	if (cr->cr_groups != cr->cr_smallgroups)
 		free(cr->cr_groups, M_CRED);
 
 	cr->cr_groups = malloc(cnt * sizeof(gid_t), M_CRED, M_WAITOK | M_ZERO);
diff --git a/sys/sys/ucred.h b/sys/sys/ucred.h
index 81e4520..a6531c4 100644
--- a/sys/sys/ucred.h
+++ b/sys/sys/ucred.h
@@ -37,6 +37,8 @@
 
 struct loginclass;
 
+#define	XU_NGROUPS	16
+
 /*
  * Credentials.
  *
@@ -64,13 +66,12 @@ struct ucred {
 	struct auditinfo_addr	cr_audit;	/* Audit properties. */
 	gid_t	*cr_groups;		/* groups */
 	int	cr_agroups;		/* Available groups */
+	gid_t   cr_smallgroups[XU_NGROUPS];	/* storage for small groups */
 };
 #define	NOCRED	((struct ucred *)0)	/* no credential available */
 #define	FSCRED	((struct ucred *)-1)	/* filesystem credential */
 #endif /* _KERNEL || _WANT_UCRED */
 
-#define	XU_NGROUPS	16
-
 /*
  * Flags for cr_flags.
  */
-- 
2.1.0

Tiwei Bie





Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20141104124120.GA11252>