summaryrefslogtreecommitdiff
path: root/libpam/pam_modutil_priv.c
blob: 7df6e6b12f60f4a79d883f01935c269dc4ac6e49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
 * $Id$
 *
 * This file provides two functions:
 * pam_modutil_drop_priv:
 *   temporarily lower process fs privileges by switching to another uid/gid,
 * pam_modutil_regain_priv:
 *   regain process fs privileges lowered by pam_modutil_drop_priv().
 */

#include "pam_modutil_private.h"
#include <security/pam_ext.h>
#include <unistd.h>
#include <syslog.h>
#include <pwd.h>
#include <grp.h>
#ifdef HAVE_SYS_FSUID_H
#include <sys/fsuid.h>
#endif /* HAVE_SYS_FSUID_H */

/*
 * Two setfsuid() calls in a row are necessary to check
 * whether setfsuid() succeeded or not.
 */
static int change_uid(uid_t uid, uid_t *save)
{
#ifdef HAVE_SYS_FSUID_H
	uid_t tmp = setfsuid(uid);
	if (save)
		*save = tmp;
	return (uid_t) setfsuid(uid) == uid ? 0 : -1;
#else
	uid_t euid = geteuid();
	uid_t ruid = getuid();
	if (save)
		*save = ruid;
	if (ruid == uid && uid != 0)
		if (setreuid(euid, uid))
			return -1;
	else {
		setreuid(0, -1);
		if (setreuid(-1, uid)) {
			setreuid(-1, 0);
			setreuid(0, -1);
			if (setreuid(-1, uid))
				return -1;
		}
	}
#endif
}
static int change_gid(gid_t gid, gid_t *save)
{
#ifdef HAVE_SYS_FSUID_H
	gid_t tmp = setfsgid(gid);
	if (save)
		*save = tmp;
	return (gid_t) setfsgid(gid) == gid ? 0 : -1;
#else
	gid_t egid = getegid();
	gid_t rgid = getgid();
	if (save)
		*save = rgid;
	if (rgid == gid)
		if (setregid(egid, gid))
			return -1;
	else {
		setregid(0, -1);
		if (setregid(-1, gid)) {
			setregid(-1, 0);
			setregid(0, -1);
			if (setregid(-1, gid))
				return -1;
		}
	}
#endif
}

static int cleanup(struct pam_modutil_privs *p)
{
	if (p->allocated) {
		p->allocated = 0;
		free(p->grplist);
	}
	p->grplist = NULL;
	p->number_of_groups = 0;
	return -1;
}

#define PRIV_MAGIC			0x1004000a
#define PRIV_MAGIC_DONOTHING		0xdead000a

int pam_modutil_drop_priv(pam_handle_t *pamh,
			  struct pam_modutil_privs *p,
			  const struct passwd *pw)
{
	int res;

	if (p->is_dropped) {
		pam_syslog(pamh, LOG_CRIT,
			   "pam_modutil_drop_priv: called with dropped privileges");
		return -1;
	}

	/*
	 * If not root, we can do nothing.
	 * If switching to root, we have nothing to do.
	 * That is, in both cases, we do not care.
	 */
	if (geteuid() != 0 || pw->pw_uid == 0) {
		p->is_dropped = PRIV_MAGIC_DONOTHING;
		return 0;
	}

	if (!p->grplist || p->number_of_groups <= 0) {
		pam_syslog(pamh, LOG_CRIT,
			   "pam_modutil_drop_priv: called without room for supplementary groups");
		return -1;
	}
	res = getgroups(0, NULL);
	if (res < 0) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_drop_priv: getgroups failed: %m");
		return -1;
	}

	p->allocated = 0;
	if (res > p->number_of_groups) {
		p->grplist = calloc(res, sizeof(gid_t));
		if (!p->grplist) {
			pam_syslog(pamh, LOG_CRIT, "out of memory");
			return cleanup(p);
		}
		p->allocated = 1;
		p->number_of_groups = res;
	}

	res = getgroups(p->number_of_groups, p->grplist);
	if (res < 0) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_drop_priv: getgroups failed: %m");
		return cleanup(p);
	}

	p->number_of_groups = res;

	/*
	 * We should care to leave process credentials in consistent state.
	 * That is, e.g. if change_gid() succeeded but change_uid() failed,
	 * we should try to restore old gid.
	 *
	 * We try to add the supplementary groups on a best-effort
	 * basis.  If it fails, it's not fatal: we fall back to using an
	 * empty list.
	 */
	if (initgroups(pw->pw_name, pw->pw_gid)) {
		pam_syslog(pamh, LOG_WARNING,
			   "pam_modutil_drop_priv: initgroups failed: %m");

		if (setgroups(0, NULL)) {
			pam_syslog(pamh, LOG_ERR,
				   "pam_modutil_drop_priv: setgroups failed: %m");
			return cleanup(p);
		}
	}
	if (change_gid(pw->pw_gid, &p->old_gid)) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_drop_priv: change_gid failed: %m");
		(void) setgroups(p->number_of_groups, p->grplist);
		return cleanup(p);
	}
	if (change_uid(pw->pw_uid, &p->old_uid)) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_drop_priv: change_uid failed: %m");
		(void) change_gid(p->old_gid, NULL);
		(void) setgroups(p->number_of_groups, p->grplist);
		return cleanup(p);
	}

	p->is_dropped = PRIV_MAGIC;
	return 0;
}

int pam_modutil_regain_priv(pam_handle_t *pamh,
			  struct pam_modutil_privs *p)
{
	switch (p->is_dropped) {
		case PRIV_MAGIC_DONOTHING:
			p->is_dropped = 0;
			return 0;

		case PRIV_MAGIC:
			break;

		default:
			pam_syslog(pamh, LOG_CRIT,
				   "pam_modutil_regain_priv: called with invalid state");
			return -1;
		}

	if (change_uid(p->old_uid, NULL)) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_regain_priv: change_uid failed: %m");
		return cleanup(p);
	}
	if (change_gid(p->old_gid, NULL)) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_regain_priv: change_gid failed: %m");
		return cleanup(p);
	}
	if (setgroups(p->number_of_groups, p->grplist)) {
		pam_syslog(pamh, LOG_ERR,
			   "pam_modutil_regain_priv: setgroups failed: %m");
		return cleanup(p);
	}

	p->is_dropped = 0;
	cleanup(p);
	return 0;
}