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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
/*
* This file has been modified for the cdrkit suite.
*
* The behaviour and appearence of the program code below can differ to a major
* extent from the version distributed by the original author(s).
*
* For details, see Changelog file distributed with the cdrkit package. If you
* received this file from another source then ask the distributing person for
* a log of modifications.
*
*/
/* @(#)setuid.c 1.11 06/02/19 Copyright 1998,1999,2004 Heiko Eissfeldt, Copyright 2004-2006 J. Schilling */
/* Security functions by zblaxell
If these functions fail, it is because there was an installation error
or a programming error, and we can't be sure about what privileges
we do or do not have. This means we might not be able to recover
the privileges we need to fix anything that may be broken (e.g. the
CDDA state of some interface types), and we may in fact do something
quite dangerous (like write to the WAV file as root).
In any case, it is unsafe to do anything but exit *now*. Ideally we'd
kill -9 our process group too, just to be sure. Root privileges are not
something you want floating around at random in user-level applications.
If any signal handlers or child processes are introduced into this
program, it will be necessary to call dontneedroot() or neverneedroot()
on entry, respectively; otherwise, it will be possible to trick
the program into executing the signal handler or child process with
root privileges by sending signals at the right time.
*/
#include "config.h"
#include <unixstd.h>
#include <stdio.h>
#include <stdxlib.h>
#include "exitcodes.h"
#include "setuid.h"
/*#undef DEBUG*/
/*#define DEBUG*/
/* True at return from initsecurity */
static uid_t real_uid = (uid_t) (-1);
static uid_t effective_uid = (uid_t) (-1);
static gid_t real_gid = (gid_t) (-1);
static gid_t effective_gid = (gid_t) (-1);
/* Run this at the beginning of the program to initialize this code and
to drop privileges before someone uses them to shoot us in the foot.
Do not pass(go), do not dollars += 200. */
void initsecurity()
{
int leffective_uid;
alarm(0); /* can be inherited from parent process */
real_uid = getuid();
leffective_uid = geteuid();
if ((int) real_uid != leffective_uid && leffective_uid != 0) { /* sanity check */
fprintf(stderr, "Warning: setuid but not to root (uid=%ld, euid=%d)\n", (long) real_uid, leffective_uid);
fprintf(stderr, "Dropping setuid privileges now.\n");
neverneedroot();
} else {
effective_uid = leffective_uid;
}
real_gid = getgid();
effective_gid = getegid();
dontneedroot();
dontneedgroup();
}
/* Temporarily gain root privileges. */
#if defined _POSIX_SAVED_IDS && defined (HAVE_SETEUID) && defined SCO
/* SCO seems to lack the prototypes... */
int seteuid(uid_t euid);
int setegid(gid_t guid);
#endif
void needroot(int necessary)
{
#ifdef DEBUG
fprintf(stderr, "call to needroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
if (effective_uid) {
if (necessary) {
fprintf(stderr, "Fatal error: require root privilege but not setuid root.\n");
exit(PERM_ERROR);
} else
return;
}
if (real_uid == (uid_t) (-1)) {
fprintf(stderr, "Fatal error: initsecurity() not called.\n");
exit(INTERNAL_ERROR);
}
if (geteuid() == 0) return; /* nothing to do */
#if defined _POSIX_SAVED_IDS && defined (HAVE_SETEUID)
if (seteuid(effective_uid)) {
perror("seteuid in needroot()");
exit(PERM_ERROR);
}
#else
#if defined (HAVE_SETREUID)
if (setreuid(real_uid, effective_uid)) {
perror("setreuid in needroot()");
exit(PERM_ERROR);
}
#endif
#endif
if (geteuid() != 0 && necessary) {
fprintf(stderr, "Fatal error: did not get root privilege.\n");
exit(PERM_ERROR);
}
#ifdef DEBUG
fprintf(stderr, "exit of needroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
}
/* Temporarily drop root privileges. */
void dontneedroot()
{
#ifdef DEBUG
fprintf(stderr, "call to dontneedroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
if (real_uid == (uid_t) (-1)) {
fprintf(stderr, "Fatal error: initsecurity() not called.\n");
exit(INTERNAL_ERROR);
}
if (effective_uid)
return;
if (geteuid() != 0) return; /* nothing to do */
#if defined _POSIX_SAVED_IDS && defined (HAVE_SETEUID)
if (seteuid(real_uid)) {
perror("seteuid in dontneedroot()");
exit(PERM_ERROR);
}
#else
#if defined (HAVE_SETREUID)
if (setreuid(effective_uid, real_uid)) {
perror("setreuid in dontneedroot()");
exit(PERM_ERROR);
}
#endif
#endif
if (geteuid() != real_uid) {
fprintf(stderr, "Fatal error: did not drop root privilege.\n");
#ifdef DEBUG
fprintf(stderr, "in to dontneedroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
exit(PERM_ERROR);
}
}
/* Permanently drop root privileges. */
void neverneedroot()
{
#ifdef DEBUG
fprintf(stderr, "call to neverneedroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
if (real_uid == (uid_t) (-1)) {
fprintf(stderr, "Fatal error: initsecurity() not called.\n");
exit(INTERNAL_ERROR);
}
if (geteuid() == effective_uid) {
#if defined (HAVE_SETUID)
if (setuid(real_uid)) {
perror("setuid in neverneedroot()");
exit(PERM_ERROR);
}
#endif
}
#if defined(__FreeBSD__) || defined(__DragonFly__) /* XXX this is a big hack and and not a permanent solution */
else {
#if defined (HAVE_SETUID)
if (setuid(real_uid)) {
perror("setuid in neverneedroot()");
exit(PERM_ERROR);
}
#endif
}
#endif
if (geteuid() != real_uid || getuid() != real_uid) {
fprintf(stderr, "Fatal error: did not drop root privilege.\n");
#ifdef DEBUG
fprintf(stderr, "in to neverneedroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
exit(PERM_ERROR);
}
effective_uid = real_uid;
#ifdef DEBUG
fprintf(stderr, "exit of neverneedroot (_euid_=%d, uid=%d), current=%d/%d, pid=%d\n",
effective_uid, real_uid, geteuid(), getuid(), getpid());
#endif
}
/* Temporarily gain group privileges. */
void needgroup(int necessary)
{
#ifdef DEBUG
fprintf(stderr, "call to needgroup (egid=%d, gid=%d), current=%d/%d, pid=%d\n",
effective_gid, real_gid, getegid(), getgid(), getpid());
#endif
if (real_gid == (gid_t) (-1)) {
fprintf(stderr, "Fatal error: initsecurity() not called.\n");
exit(INTERNAL_ERROR);
}
if (getegid() == effective_gid) return; /* nothing to do */
#if defined _POSIX_SAVED_IDS && defined (HAVE_SETEGID)
if (setegid(effective_gid)) {
perror("setegid in needgroup()");
exit(PERM_ERROR);
}
#else
#if defined (HAVE_SETREGID)
if (setregid(real_gid, effective_gid)) {
perror("setregid in needgroup()");
exit(PERM_ERROR);
}
#endif
#endif
if (necessary && getegid() != effective_gid) {
fprintf(stderr, "Fatal error: did not get group privilege.\n");
exit(PERM_ERROR);
}
}
/* Temporarily drop group privileges. */
void dontneedgroup()
{
#ifdef DEBUG
fprintf(stderr, "call to dontneedgroup (egid=%d, gid=%d), current=%d/%d, pid=%d\n",
effective_gid, real_gid, getegid(), getgid(), getpid());
#endif
if (real_gid == (gid_t) (-1)) {
fprintf(stderr, "Fatal error: initsecurity() not called.\n");
exit(INTERNAL_ERROR);
}
if (getegid() != effective_gid) return; /* nothing to do */
#if defined _POSIX_SAVED_IDS && defined (HAVE_SETEGID)
if (setegid(real_gid)) {
perror("setegid in dontneedgroup()");
exit(PERM_ERROR);
}
#else
#if defined (HAVE_SETREGID)
if (setregid(effective_gid, real_gid)) {
perror("setregid in dontneedgroup()");
exit(PERM_ERROR);
}
#endif
#endif
if (getegid() != real_gid) {
fprintf(stderr, "Fatal error: did not drop group privilege.\n");
exit(PERM_ERROR);
}
#ifdef DEBUG
fprintf(stderr, "exit if dontneedgroup (egid=%d, gid=%d), current=%d/%d, pid=%d\n",
effective_gid, real_gid, getegid(), getgid(), getpid());
#endif
}
/* Permanently drop group privileges. */
void neverneedgroup()
{
#ifdef DEBUG
fprintf(stderr, "call to neverneedgroup (egid=%d, gid=%d), current=%d/%d, pid=%d\n",
effective_gid, real_gid, getegid(), getgid(), getpid());
#endif
if (real_gid == (gid_t) (-1)) {
fprintf(stderr, "Fatal error: initsecurity() not called.\n");
exit(INTERNAL_ERROR);
}
if (getegid() == effective_gid) {
#if defined (HAVE_SETGID)
if (setgid(real_gid)) {
perror("setgid in neverneedgroup()");
exit(PERM_ERROR);
}
#endif
}
#if defined(__FreeBSD__) || defined(__DragonFly__) /* XXX this is a big hack and and not a permanent solution */
else {
#if defined (HAVE_SETGID)
if (setgid(real_gid)) {
perror("setgid in neverneedgroup()");
exit(PERM_ERROR);
}
#endif
}
#endif
if (getegid() != real_gid || getgid() != real_gid) {
fprintf(stderr, "Fatal error: did not drop group privilege.\n");
exit(PERM_ERROR);
}
effective_gid = real_gid;
}
#if defined (HPUX)
int seteuid(uid_t uid)
{
return setresuid(-1, uid, -1);
}
int setreuid(uid_t uid1, uid_t uid2)
{
return setresuid(uid2, uid2, uid1 == uid2 ? uid2 : 0);
}
int setregid(gid_t gid1, gid_t gid2)
{
return setresgid(gid2, gid2, gid1 == gid2 ? gid2 : 0);
}
#endif
|