License header fixes
[lttng-tools.git] / src / common / runas.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #define _GNU_SOURCE
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sched.h>
31 #include <sys/signal.h>
32
33 #include <common/error.h>
34 #include <common/compat/mman.h>
35 #include <common/compat/clone.h>
36
37 #include "runas.h"
38
39 #define RUNAS_CHILD_STACK_SIZE 10485760
40
41 #ifdef __FreeBSD__
42 /* FreeBSD MAP_STACK always return -ENOMEM */
43 #define LTTNG_MAP_STACK 0
44 #else
45 #define LTTNG_MAP_STACK MAP_STACK
46 #endif
47
48 #ifndef MAP_GROWSDOWN
49 #define MAP_GROWSDOWN 0
50 #endif
51
52 #ifndef MAP_ANONYMOUS
53 #define MAP_ANONYMOUS MAP_ANON
54 #endif
55
56 struct run_as_data {
57 int (*cmd)(void *data);
58 void *data;
59 uid_t uid;
60 gid_t gid;
61 int retval_pipe;
62 };
63
64 struct run_as_mkdir_data {
65 const char *path;
66 mode_t mode;
67 };
68
69 struct run_as_open_data {
70 const char *path;
71 int flags;
72 mode_t mode;
73 };
74
75 /*
76 * Create recursively directory using the FULL path.
77 */
78 static
79 int _mkdir_recursive(void *_data)
80 {
81 struct run_as_mkdir_data *data = _data;
82 const char *path;
83 char *p, tmp[PATH_MAX];
84 struct stat statbuf;
85 mode_t mode;
86 size_t len;
87 int ret;
88
89 path = data->path;
90 mode = data->mode;
91
92 ret = snprintf(tmp, sizeof(tmp), "%s", path);
93 if (ret < 0) {
94 PERROR("snprintf mkdir");
95 goto error;
96 }
97
98 len = ret;
99 if (tmp[len - 1] == '/') {
100 tmp[len - 1] = 0;
101 }
102
103 for (p = tmp + 1; *p; p++) {
104 if (*p == '/') {
105 *p = 0;
106 ret = stat(tmp, &statbuf);
107 if (ret < 0) {
108 ret = mkdir(tmp, mode);
109 if (ret < 0) {
110 if (!(errno == EEXIST)) {
111 PERROR("mkdir recursive");
112 ret = -errno;
113 goto error;
114 }
115 }
116 }
117 *p = '/';
118 }
119 }
120
121 ret = mkdir(tmp, mode);
122 if (ret < 0) {
123 if (!(errno == EEXIST)) {
124 PERROR("mkdir recursive last piece");
125 ret = -errno;
126 } else {
127 ret = 0;
128 }
129 }
130
131 error:
132 return ret;
133 }
134
135 static
136 int _mkdir(void *_data)
137 {
138 struct run_as_mkdir_data *data = _data;
139 return mkdir(data->path, data->mode);
140 }
141
142 static
143 int _open(void *_data)
144 {
145 struct run_as_open_data *data = _data;
146 return open(data->path, data->flags, data->mode);
147 }
148
149 static
150 int child_run_as(void *_data)
151 {
152 struct run_as_data *data = _data;
153 size_t writelen, writeleft, index;
154 union {
155 int i;
156 char c[sizeof(int)];
157 } sendret;
158 int ret;
159
160 /*
161 * Child: it is safe to drop egid and euid while sharing the
162 * file descriptors with the parent process, since we do not
163 * drop "uid": therefore, the user we are dropping egid/euid to
164 * cannot attach to this process with, e.g. ptrace, nor map this
165 * process memory.
166 */
167 if (data->gid != getegid()) {
168 ret = setegid(data->gid);
169 if (ret < 0) {
170 PERROR("setegid");
171 return EXIT_FAILURE;
172 }
173 }
174 if (data->uid != geteuid()) {
175 ret = seteuid(data->uid);
176 if (ret < 0) {
177 PERROR("seteuid");
178 return EXIT_FAILURE;
179 }
180 }
181 /*
182 * Also set umask to 0 for mkdir executable bit.
183 */
184 umask(0);
185 sendret.i = (*data->cmd)(data->data);
186 /* send back return value */
187 writeleft = sizeof(sendret);
188 index = 0;
189 do {
190 writelen = write(data->retval_pipe, &sendret.c[index],
191 writeleft);
192 if (writelen < 0) {
193 PERROR("write");
194 return EXIT_FAILURE;
195 }
196 writeleft -= writelen;
197 index += writelen;
198 } while (writeleft > 0);
199 return EXIT_SUCCESS;
200 }
201
202 static
203 int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
204 {
205 struct run_as_data run_as_data;
206 int ret = 0;
207 int status;
208 pid_t pid;
209 int retval_pipe[2];
210 ssize_t readlen, readleft, index;
211 void *child_stack;
212 union {
213 int i;
214 char c[sizeof(int)];
215 } retval;
216
217 /*
218 * If we are non-root, we can only deal with our own uid.
219 */
220 if (geteuid() != 0) {
221 if (uid != geteuid()) {
222 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
223 uid, geteuid());
224 return -EPERM;
225 }
226 }
227
228 ret = pipe(retval_pipe);
229 if (ret < 0) {
230 PERROR("pipe");
231 retval.i = ret;
232 goto end;
233 }
234 run_as_data.data = data;
235 run_as_data.cmd = cmd;
236 run_as_data.uid = uid;
237 run_as_data.gid = gid;
238 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
239 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
240 PROT_WRITE | PROT_READ,
241 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
242 -1, 0);
243 if (child_stack == MAP_FAILED) {
244 PERROR("mmap");
245 retval.i = -ENOMEM;
246 goto close_pipe;
247 }
248 /*
249 * Pointing to the middle of the stack to support architectures
250 * where the stack grows up (HPPA).
251 */
252 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
253 &run_as_data);
254 if (pid < 0) {
255 PERROR("clone");
256 retval.i = pid;
257 goto unmap_stack;
258 }
259 /* receive return value */
260 readleft = sizeof(retval);
261 index = 0;
262 do {
263 readlen = read(retval_pipe[0], &retval.c[index], readleft);
264 if (readlen < 0) {
265 PERROR("read");
266 ret = -1;
267 break;
268 }
269 readleft -= readlen;
270 index += readlen;
271 } while (readleft > 0);
272
273 /*
274 * Parent: wait for child to return, in which case the
275 * shared memory map will have been created.
276 */
277 pid = waitpid(pid, &status, 0);
278 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
279 PERROR("wait");
280 retval.i = -1;
281 }
282 unmap_stack:
283 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
284 if (ret < 0) {
285 PERROR("munmap");
286 retval.i = ret;
287 }
288 close_pipe:
289 ret = close(retval_pipe[0]);
290 if (ret) {
291 PERROR("close");
292 }
293 ret = close(retval_pipe[1]);
294 if (ret) {
295 PERROR("close");
296 }
297 end:
298 return retval.i;
299 }
300
301 /*
302 * To be used on setups where gdb has issues debugging programs using
303 * clone/rfork. Note that this is for debuging ONLY, and should not be
304 * considered secure.
305 */
306 static
307 int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
308 {
309 return cmd(data);
310 }
311
312 static
313 int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
314 {
315 if (!getenv("LTTNG_DEBUG_NOCLONE")) {
316 DBG("Using run_as_clone");
317 return run_as_clone(cmd, data, uid, gid);
318 } else {
319 DBG("Using run_as_noclone");
320 return run_as_noclone(cmd, data, uid, gid);
321 }
322 }
323
324 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
325 {
326 struct run_as_mkdir_data data;
327
328 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
329 path, mode, uid, gid);
330 data.path = path;
331 data.mode = mode;
332 return run_as(_mkdir_recursive, &data, uid, gid);
333 }
334
335 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
336 {
337 struct run_as_mkdir_data data;
338
339 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
340 path, mode, uid, gid);
341 data.path = path;
342 data.mode = mode;
343 return run_as(_mkdir, &data, uid, gid);
344 }
345
346 /*
347 * Note: open_run_as is currently not working. We'd need to pass the fd
348 * opened in the child to the parent.
349 */
350 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
351 {
352 struct run_as_open_data data;
353
354 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
355 path, flags, mode, uid, gid);
356 data.path = path;
357 data.flags = flags;
358 data.mode = mode;
359 return run_as(_open, &data, uid, gid);
360 }
This page took 0.036737 seconds and 5 git commands to generate.