View | Details | Raw Unified | Return to bug 787 | Differences between
and this patch

Collapse All | Expand All

(-)groupaccess.c (-3 / +17 lines)
Lines 31-37 Link Here
31
#include "log.h"
31
#include "log.h"
32
32
33
static int ngroups;
33
static int ngroups;
34
static char *groups_byname[NGROUPS_MAX + 1];	/* +1 for base/primary group */
34
static char **groups_byname;
35
35
36
/*
36
/*
37
 * Initialize group access list for user with primary (base) and
37
 * Initialize group access list for user with primary (base) and
Lines 40-58 Link Here
40
int
40
int
41
ga_init(const char *user, gid_t base)
41
ga_init(const char *user, gid_t base)
42
{
42
{
43
	gid_t groups_bygid[NGROUPS_MAX + 1];
43
	gid_t *groups_bygid;
44
	int i, j;
44
	int i, j;
45
	struct group *gr;
45
	struct group *gr;
46
	gid_t fake;
46
47
47
	if (ngroups > 0)
48
	if (ngroups > 0)
48
		ga_free();
49
		ga_free();
49
50
50
	ngroups = sizeof(groups_bygid) / sizeof(gid_t);
51
	/*
52
	 * Some implementations of getgrouplist() expect to write at least
53
	 * the base group before checking the ngroups parameter.  We expect
54
	 * the first call to getgrouplist() to fail, and populate ngroups
55
	 * for us.
56
	 */
57
	ngroups = 1;
58
	getgrouplist(user, base, &fake, &ngroups);
59
60
	groups_bygid = xmalloc(ngroups * sizeof(*groups_bygid));
61
	groups_byname = xmalloc(ngroups * sizeof(*groups_byname));
62
51
	if (getgrouplist(user, base, groups_bygid, &ngroups) == -1)
63
	if (getgrouplist(user, base, groups_bygid, &ngroups) == -1)
52
		logit("getgrouplist: groups list too small");
64
		logit("getgrouplist: groups list too small");
53
	for (i = 0, j = 0; i < ngroups; i++)
65
	for (i = 0, j = 0; i < ngroups; i++)
54
		if ((gr = getgrgid(groups_bygid[i])) != NULL)
66
		if ((gr = getgrgid(groups_bygid[i])) != NULL)
55
			groups_byname[j++] = xstrdup(gr->gr_name);
67
			groups_byname[j++] = xstrdup(gr->gr_name);
68
	xfree(groups_bygid);
56
	return (ngroups = j);
69
	return (ngroups = j);
57
}
70
}
58
71
Lines 84-88 Link Here
84
		for (i = 0; i < ngroups; i++)
97
		for (i = 0; i < ngroups; i++)
85
			xfree(groups_byname[i]);
98
			xfree(groups_byname[i]);
86
		ngroups = 0;
99
		ngroups = 0;
100
		xfree(groups_byname);
87
	}
101
	}
88
}
102
}
(-)uidswap.c (-3 / +24 lines)
Lines 16-21 Link Here
16
16
17
#include "log.h"
17
#include "log.h"
18
#include "uidswap.h"
18
#include "uidswap.h"
19
#include "xmalloc.h"
19
20
20
/*
21
/*
21
 * Note: all these functions must work in all of the following cases:
22
 * Note: all these functions must work in all of the following cases:
Lines 38-44 Link Here
38
/* Saved effective uid. */
39
/* Saved effective uid. */
39
static int	privileged = 0;
40
static int	privileged = 0;
40
static int	temporarily_use_uid_effective = 0;
41
static int	temporarily_use_uid_effective = 0;
41
static gid_t	saved_egroups[NGROUPS_MAX], user_groups[NGROUPS_MAX];
42
static gid_t	*saved_egroups, *user_groups;
42
static int	saved_egroupslen = -1, user_groupslen = -1;
43
static int	saved_egroupslen = -1, user_groupslen = -1;
43
44
44
/*
45
/*
Lines 68-85 Link Here
68
69
69
	privileged = 1;
70
	privileged = 1;
70
	temporarily_use_uid_effective = 1;
71
	temporarily_use_uid_effective = 1;
71
	saved_egroupslen = getgroups(NGROUPS_MAX, saved_egroups);
72
73
	saved_egroupslen = getgroups(0, NULL);
72
	if (saved_egroupslen < 0)
74
	if (saved_egroupslen < 0)
73
		fatal("getgroups: %.100s", strerror(errno));
75
		fatal("getgroups: %.100s", strerror(errno));
76
	if (saved_egroupslen > 0) {
77
		saved_egroups = xrealloc(saved_egroups,
78
		    saved_egroupslen * sizeof(gid_t));
79
		if (getgroups(saved_egroupslen, saved_egroups) < 0)
80
			fatal("getgroups: %.100s", strerror(errno));
81
	} else { /* saved_egroupslen == 0 */
82
		if (saved_egroups)
83
			xfree(saved_egroups);
84
	}
74
85
75
	/* set and save the user's groups */
86
	/* set and save the user's groups */
76
	if (user_groupslen == -1) {
87
	if (user_groupslen == -1) {
77
		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
88
		if (initgroups(pw->pw_name, pw->pw_gid) < 0)
78
			fatal("initgroups: %s: %.100s", pw->pw_name,
89
			fatal("initgroups: %s: %.100s", pw->pw_name,
79
			    strerror(errno));
90
			    strerror(errno));
80
		user_groupslen = getgroups(NGROUPS_MAX, user_groups);
91
92
		user_groupslen = getgroups(0, NULL);
81
		if (user_groupslen < 0)
93
		if (user_groupslen < 0)
82
			fatal("getgroups: %.100s", strerror(errno));
94
			fatal("getgroups: %.100s", strerror(errno));
95
		if (user_groupslen > 0) {
96
			user_groups = xrealloc(user_groups,
97
			    user_groupslen * sizeof(gid_t));
98
			if (getgroups(user_groupslen, user_groups) < 0)
99
				fatal("getgroups: %.100s", strerror(errno));
100
		} else { /* user_groupslen == 0 */
101
			if (user_groups)
102
				xfree(user_groups);
103
		}
83
	}
104
	}
84
	/* Set the effective uid to the given (unprivileged) uid. */
105
	/* Set the effective uid to the given (unprivileged) uid. */
85
	if (setgroups(user_groupslen, user_groups) < 0)
106
	if (setgroups(user_groupslen, user_groups) < 0)

Return to bug 787