Update version to v2.6.3
[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/common.h>
34 #include <common/utils.h>
35 #include <common/compat/mman.h>
36 #include <common/compat/clone.h>
37
38 #include "runas.h"
39
40 #define RUNAS_CHILD_STACK_SIZE 10485760
41
42 #ifndef MAP_STACK
43 #define MAP_STACK 0
44 #endif
45
46 #ifdef __FreeBSD__
47 /* FreeBSD MAP_STACK always return -ENOMEM */
48 #define LTTNG_MAP_STACK 0
49 #else
50 #define LTTNG_MAP_STACK MAP_STACK
51 #endif
52
53 #ifndef MAP_GROWSDOWN
54 #define MAP_GROWSDOWN 0
55 #endif
56
57 #ifndef MAP_ANONYMOUS
58 #define MAP_ANONYMOUS MAP_ANON
59 #endif
60
61 struct run_as_data {
62 int (*cmd)(void *data);
63 void *data;
64 uid_t uid;
65 gid_t gid;
66 int retval_pipe;
67 };
68
69 struct run_as_mkdir_data {
70 const char *path;
71 mode_t mode;
72 };
73
74 struct run_as_open_data {
75 const char *path;
76 int flags;
77 mode_t mode;
78 };
79
80 struct run_as_unlink_data {
81 const char *path;
82 };
83
84 struct run_as_ret {
85 int ret;
86 int _errno;
87 };
88
89 #ifdef VALGRIND
90 static
91 int use_clone(void)
92 {
93 return 0;
94 }
95 #else
96 static
97 int use_clone(void)
98 {
99 return !getenv("LTTNG_DEBUG_NOCLONE");
100 }
101 #endif
102
103 LTTNG_HIDDEN
104 int _utils_mkdir_recursive_unsafe(const char *path, mode_t mode);
105
106 /*
107 * Create recursively directory using the FULL path.
108 */
109 static
110 int _mkdir_recursive(void *_data)
111 {
112 struct run_as_mkdir_data *data = _data;
113 const char *path;
114 mode_t mode;
115
116 path = data->path;
117 mode = data->mode;
118
119 /* Safe to call as we have transitioned to the requested uid/gid. */
120 return _utils_mkdir_recursive_unsafe(path, mode);
121 }
122
123 static
124 int _mkdir(void *_data)
125 {
126 struct run_as_mkdir_data *data = _data;
127
128 return mkdir(data->path, data->mode);
129 }
130
131 static
132 int _open(void *_data)
133 {
134 struct run_as_open_data *data = _data;
135
136 return open(data->path, data->flags, data->mode);
137 }
138
139 static
140 int _unlink(void *_data)
141 {
142 struct run_as_unlink_data *data = _data;
143
144 return unlink(data->path);
145 }
146
147 static
148 int child_run_as(void *_data)
149 {
150 int ret;
151 struct run_as_data *data = _data;
152 ssize_t writelen;
153 struct run_as_ret sendret;
154
155 /*
156 * Child: it is safe to drop egid and euid while sharing the
157 * file descriptors with the parent process, since we do not
158 * drop "uid": therefore, the user we are dropping egid/euid to
159 * cannot attach to this process with, e.g. ptrace, nor map this
160 * process memory.
161 */
162 if (data->gid != getegid()) {
163 ret = setegid(data->gid);
164 if (ret < 0) {
165 PERROR("setegid");
166 goto write_return;
167 }
168 }
169 if (data->uid != geteuid()) {
170 ret = seteuid(data->uid);
171 if (ret < 0) {
172 PERROR("seteuid");
173 goto write_return;
174 }
175 }
176 /*
177 * Also set umask to 0 for mkdir executable bit.
178 */
179 umask(0);
180 ret = (*data->cmd)(data->data);
181
182 write_return:
183 sendret.ret = ret;
184 sendret._errno = errno;
185 /* send back return value */
186 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
187 if (writelen < sizeof(sendret)) {
188 PERROR("lttng_write error");
189 return EXIT_FAILURE;
190 } else {
191 return EXIT_SUCCESS;
192 }
193 }
194
195 static
196 int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
197 {
198 struct run_as_data run_as_data;
199 int ret = 0;
200 ssize_t readlen;
201 int status;
202 pid_t pid;
203 int retval_pipe[2];
204 void *child_stack;
205 struct run_as_ret recvret;
206
207 /*
208 * If we are non-root, we can only deal with our own uid.
209 */
210 if (geteuid() != 0) {
211 if (uid != geteuid()) {
212 recvret.ret = -1;
213 recvret._errno = EPERM;
214 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
215 uid, geteuid());
216 goto end;
217 }
218 }
219
220 ret = pipe(retval_pipe);
221 if (ret < 0) {
222 recvret.ret = -1;
223 recvret._errno = errno;
224 PERROR("pipe");
225 goto end;
226 }
227 run_as_data.data = data;
228 run_as_data.cmd = cmd;
229 run_as_data.uid = uid;
230 run_as_data.gid = gid;
231 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
232 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
233 PROT_WRITE | PROT_READ,
234 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
235 -1, 0);
236 if (child_stack == MAP_FAILED) {
237 recvret.ret = -1;
238 recvret._errno = ENOMEM;
239 PERROR("mmap");
240 goto close_pipe;
241 }
242 /*
243 * Pointing to the middle of the stack to support architectures
244 * where the stack grows up (HPPA).
245 */
246 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
247 &run_as_data);
248 if (pid < 0) {
249 recvret.ret = -1;
250 recvret._errno = errno;
251 PERROR("clone");
252 goto unmap_stack;
253 }
254 /* receive return value */
255 readlen = lttng_read(retval_pipe[0], &recvret, sizeof(recvret));
256 if (readlen < sizeof(recvret)) {
257 recvret.ret = -1;
258 recvret._errno = errno;
259 }
260
261 /*
262 * Parent: wait for child to return, in which case the
263 * shared memory map will have been created.
264 */
265 pid = waitpid(pid, &status, 0);
266 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
267 recvret.ret = -1;
268 recvret._errno = errno;
269 PERROR("wait");
270 }
271 unmap_stack:
272 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
273 if (ret < 0) {
274 recvret.ret = -1;
275 recvret._errno = errno;
276 PERROR("munmap");
277 }
278 close_pipe:
279 ret = close(retval_pipe[0]);
280 if (ret) {
281 recvret.ret = -1;
282 recvret._errno = errno;
283 PERROR("close");
284 }
285 ret = close(retval_pipe[1]);
286 if (ret) {
287 recvret.ret = -1;
288 recvret._errno = errno;
289 PERROR("close");
290 }
291 end:
292 errno = recvret._errno;
293 return recvret.ret;
294 }
295
296 /*
297 * To be used on setups where gdb has issues debugging programs using
298 * clone/rfork. Note that this is for debuging ONLY, and should not be
299 * considered secure.
300 */
301 static
302 int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
303 {
304 int ret, saved_errno;
305 mode_t old_mask;
306
307 old_mask = umask(0);
308 ret = cmd(data);
309 saved_errno = errno;
310 umask(old_mask);
311 errno = saved_errno;
312
313 return ret;
314 }
315
316 static
317 int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
318 {
319 if (use_clone()) {
320 int ret;
321
322 DBG("Using run_as_clone");
323 pthread_mutex_lock(&lttng_libc_state_lock);
324 ret = run_as_clone(cmd, data, uid, gid);
325 pthread_mutex_unlock(&lttng_libc_state_lock);
326 return ret;
327 } else {
328 DBG("Using run_as_noclone");
329 return run_as_noclone(cmd, data, uid, gid);
330 }
331 }
332
333 LTTNG_HIDDEN
334 int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
335 {
336 struct run_as_mkdir_data data;
337
338 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
339 path, mode, uid, gid);
340 data.path = path;
341 data.mode = mode;
342 return run_as(_mkdir_recursive, &data, uid, gid);
343 }
344
345 LTTNG_HIDDEN
346 int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
347 {
348 struct run_as_mkdir_data data;
349
350 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
351 path, mode, uid, gid);
352 data.path = path;
353 data.mode = mode;
354 return run_as(_mkdir, &data, uid, gid);
355 }
356
357 /*
358 * Note: open_run_as is currently not working. We'd need to pass the fd
359 * opened in the child to the parent.
360 */
361 LTTNG_HIDDEN
362 int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
363 {
364 struct run_as_open_data data;
365
366 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
367 path, flags, mode, uid, gid);
368 data.path = path;
369 data.flags = flags;
370 data.mode = mode;
371 return run_as(_open, &data, uid, gid);
372 }
373
374 LTTNG_HIDDEN
375 int run_as_unlink(const char *path, uid_t uid, gid_t gid)
376 {
377 struct run_as_unlink_data data;
378
379 DBG3("unlink() %s with for uid %d and gid %d",
380 path, uid, gid);
381 data.path = path;
382 return run_as(_unlink, &data, uid, gid);
383 }
This page took 0.038588 seconds and 4 git commands to generate.