lttng-crash: support recursive traces
[lttng-tools.git] / src / common / runas.c
... / ...
CommitLineData
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#define _LGPL_SOURCE
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>
31#include <sched.h>
32#include <sys/signal.h>
33
34#include <common/common.h>
35#include <common/utils.h>
36#include <common/compat/mman.h>
37#include <common/compat/clone.h>
38#include <common/compat/getenv.h>
39
40#include "runas.h"
41
42#define RUNAS_CHILD_STACK_SIZE 10485760
43
44#ifndef MAP_STACK
45#define MAP_STACK 0
46#endif
47
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
53#endif
54
55#ifndef MAP_GROWSDOWN
56#define MAP_GROWSDOWN 0
57#endif
58
59#ifndef MAP_ANONYMOUS
60#define MAP_ANONYMOUS MAP_ANON
61#endif
62
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
71struct run_as_mkdir_data {
72 const char *path;
73 mode_t mode;
74};
75
76struct run_as_open_data {
77 const char *path;
78 int flags;
79 mode_t mode;
80};
81
82#ifdef VALGRIND
83static
84int use_clone(void)
85{
86 return 0;
87}
88#else
89static
90int use_clone(void)
91{
92 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
93}
94#endif
95
96/*
97 * Create recursively directory using the FULL path.
98 */
99static
100int _mkdir_recursive(void *_data)
101{
102 struct run_as_mkdir_data *data = _data;
103 const char *path;
104 mode_t mode;
105
106 path = data->path;
107 mode = data->mode;
108
109 return utils_mkdir_recursive(path, mode);
110}
111
112static
113int _mkdir(void *_data)
114{
115 int ret;
116 struct run_as_mkdir_data *data = _data;
117
118 ret = mkdir(data->path, data->mode);
119 if (ret < 0) {
120 ret = -errno;
121 }
122
123 return ret;
124}
125
126static
127int _open(void *_data)
128{
129 struct run_as_open_data *data = _data;
130 return open(data->path, data->flags, data->mode);
131}
132
133static
134int child_run_as(void *_data)
135{
136 int ret;
137 struct run_as_data *data = _data;
138 ssize_t writelen;
139 int sendret;
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 */
148 if (data->gid != getegid()) {
149 ret = setegid(data->gid);
150 if (ret < 0) {
151 PERROR("setegid");
152 sendret = -1;
153 goto write_return;
154 }
155 }
156 if (data->uid != geteuid()) {
157 ret = seteuid(data->uid);
158 if (ret < 0) {
159 PERROR("seteuid");
160 sendret = -1;
161 goto write_return;
162 }
163 }
164 /*
165 * Also set umask to 0 for mkdir executable bit.
166 */
167 umask(0);
168 sendret = (*data->cmd)(data->data);
169
170write_return:
171 /* send back return value */
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 }
179}
180
181static
182int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
183{
184 struct run_as_data run_as_data;
185 int ret = 0;
186 ssize_t readlen;
187 int status;
188 pid_t pid;
189 int retval_pipe[2];
190 void *child_stack;
191 int retval;
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 }
202 }
203
204 ret = pipe(retval_pipe);
205 if (ret < 0) {
206 PERROR("pipe");
207 retval = ret;
208 goto end;
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 */
215 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
216 PROT_WRITE | PROT_READ,
217 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
218 -1, 0);
219 if (child_stack == MAP_FAILED) {
220 PERROR("mmap");
221 retval = -ENOMEM;
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 */
228 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
229 &run_as_data);
230 if (pid < 0) {
231 PERROR("clone");
232 retval = pid;
233 goto unmap_stack;
234 }
235 /* receive return value */
236 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
237 if (readlen < sizeof(retval)) {
238 ret = -1;
239 }
240
241 /*
242 * Parent: wait for child to return, in which case the
243 * shared memory map will have been created.
244 */
245 pid = waitpid(pid, &status, 0);
246 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
247 PERROR("wait");
248 retval = -1;
249 }
250unmap_stack:
251 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
252 if (ret < 0) {
253 PERROR("munmap");
254 retval = ret;
255 }
256close_pipe:
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 }
265end:
266 return retval;
267}
268
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{
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;
285}
286
287static
288int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
289{
290 if (use_clone()) {
291 int ret;
292
293 DBG("Using run_as_clone");
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;
298 } else {
299 DBG("Using run_as_noclone");
300 return run_as_noclone(cmd, data, uid, gid);
301 }
302}
303
304LTTNG_HIDDEN
305int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
306{
307 struct run_as_mkdir_data data;
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
316LTTNG_HIDDEN
317int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
318{
319 struct run_as_mkdir_data data;
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 */
332LTTNG_HIDDEN
333int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
334{
335 struct run_as_open_data data;
336
337 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
338 path, flags, mode, uid, gid);
339 data.path = path;
340 data.flags = flags;
341 data.mode = mode;
342 return run_as(_open, &data, uid, gid);
343}
This page took 0.023404 seconds and 4 git commands to generate.