[PATCH] Finish the task 'Embedd group table into struct ucred'

Tiwei Bie btw at mail.ustc.edu.cn
Tue Nov 4 09:52:05 UTC 2014


Hi, Mateusz!

I have finished the task: Embedd group table into struct ucred [1].

This is an explanation of my patch:

In crextend(), when n <= XU_NGROUPS, I did such updates directly:

	cr->cr_groups = cr->cr_smallgroups;
	cr->cr_agroups = XU_NGROUPS;

Because there is a check in the front of this function:

	if (n <= cr->cr_agroups)
		return;

So when n <= XU_NGROUPS, cr->cr_groups would never point to the memory
allocated by malloc(), and it won't need to be freed.

Following is my patch:

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;
+	}
+
 	/*
 	 * We extend by 2 each time since we're using a power of two
 	 * allocator until we need enough groups to fill a page.
@@ -1997,7 +2004,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_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

[1] https://wiki.freebsd.org/JuniorJobs#Embedd_group_table_into_struct_ucred

Tiwei Bie



More information about the freebsd-hackers mailing list