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