-
Notifications
You must be signed in to change notification settings - Fork 1
/
setuidHelper.cc
211 lines (164 loc) · 4.87 KB
/
setuidHelper.cc
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
/*
* Copyright (c) 2016 Mark Heily <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef __linux__
// Required to get setgroups(2)
#define _BSD_SOURCE
#endif
#include <err.h>
#include <sys/stat.h>
#include <grp.h>
#include "namespaceImport.h"
#include "logger.h"
#include "setuidHelper.h"
static bool isInitialized = false;
static bool isLoweredPrivs = false;
static bool isDroppedPrivs = false;
static uid_t euid = 999999999; // KLUDGE: hopefully an unused UID
static gid_t egid = 999999999; // KLUDGE: hopefully an unused GID
static mode_t saved_umask = S_IWGRP|S_IWOTH;
static bool debugModule = false;
#define debug_printf(fmt,...) do { \
if (debugModule) { printf(fmt"\n", ## __VA_ARGS__); } \
} while (0)
static void debugPrintUid() {
if (debugModule) {
uid_t real, effective, saved;
if (getresuid(&real, &effective, &saved) < 0) {
throw std::system_error(errno, std::system_category());
}
printf("getresuid(2): real=%d effective=%d saved=%d\n", real, effective, saved);
printf("priv state: lowered=%d dropped=%d\n", (int)isLoweredPrivs, (int)isDroppedPrivs);
}
}
void SetuidHelper::raisePrivileges() {
debugPrintUid();
if (isDroppedPrivs) {
errx(1, "privileges are dropped");
}
if (!isInitialized) {
errx(1, "must call checkPrivileges() first");
}
if (!isLoweredPrivs) {
errx(1, "privileges are not currently lowered");
}
saved_umask = umask(S_IWGRP|S_IWOTH);
if (debugModule) {
log_debug("raising privileges");
}
if (setresuid(0, 0, 0) < 0) {
err(1, "setresuid(2)");
}
if (setresgid(0, 0, 0) < 0) {
err(1, "setresgid(2)");
}
if (geteuid() != 0) {
errx(1, "unable to regain priviliges");
}
debugPrintUid();
isLoweredPrivs = false;
}
void SetuidHelper::lowerPrivileges() {
debugPrintUid();
if (!isInitialized) {
throw std::logic_error("must call checkPrivileges() first");
}
if (isDroppedPrivs) {
throw std::logic_error("privileges are dropped");
}
if (isLoweredPrivs) {
errx(1, "privileges already lowered");
}
if (debugModule) {
log_debug("lowering privileges (current: uid=%d, euid=%d)", getuid(), geteuid());
}
// TODO: should call getgroups(3) to save the current grouplist,
// and restore the privileges later
if (setgroups(0, NULL) < 0) {
throw std::system_error(errno, std::system_category());
}
if (setegid(egid) < 0) {
throw std::system_error(errno, std::system_category());
}
if (seteuid(euid) < 0) {
throw std::system_error(errno, std::system_category());
}
debugPrintUid();
(void) umask(saved_umask);
isLoweredPrivs = true;
}
void SetuidHelper::dropPrivileges() {
if (!isInitialized) {
throw std::logic_error("must call checkPrivileges() first");
}
if (isDroppedPrivs) {
throw std::logic_error("privileges are already dropped");
}
if (isLoweredPrivs) {
raisePrivileges();
}
log_debug("dropping privileges (current: uid=%d, euid=%d)", getuid(), geteuid());
if (setgroups(0, NULL) < 0) {
log_errno("setgroups(2)");
throw std::system_error(errno, std::system_category());
}
if (setresgid(egid, egid, egid) < 0) {
log_errno("setresgid(2)");
throw std::system_error(errno, std::system_category());
}
if (setresuid(euid, euid, euid) < 0) {
log_errno("setresuid(2)");
throw std::system_error(errno, std::system_category());
}
debugPrintUid();
isLoweredPrivs = false;
isDroppedPrivs = true;
}
void SetuidHelper::checkPrivileges() {
// FIXME: what about the case where euid != 0 && uid != 0 ?
// the entire raise/lower will not work, and should throw
// exceptions.
debugPrintUid();
if (geteuid() == 0 && getuid() == 0 && getenv("SUDO_UID")) {
const char* buf = getenv("SUDO_UID");
if (buf) {
// FIXME: assumes gid == uid
egid = euid = std::stoul(buf);
//printf("2 got %d %d %d\n", euid, egid, getegid());
} else {
throw std::runtime_error("Unable to parse SUDO_UID");
}
} else {
euid = getuid();
egid = getgid();
//printf("got %d %d\n", euid, egid);
}
if (setresuid(-1, -1, 0) < 0) {
throw std::system_error(errno, std::system_category());
}
isInitialized = true;
isLoweredPrivs = false;
}
uid_t SetuidHelper::getActualUid()
{
return euid;
}
void SetuidHelper::logPrivileges()
{
bool saved = debugModule;
debugModule = true;
debugPrintUid();
debugModule = saved;
}