Fix: reference counting of consumer output
[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
4628484a
MD
82struct run_as_unlink_data {
83 const char *path;
84};
85
86struct run_as_recursive_rmdir_data {
87 const char *path;
88};
89
8f0044bf
MD
90#ifdef VALGRIND
91static
92int use_clone(void)
93{
94 return 0;
95}
96#else
97static
98int use_clone(void)
99{
e8fa9fb0 100 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
8f0044bf
MD
101}
102#endif
103
60b6c79c
MD
104/*
105 * Create recursively directory using the FULL path.
106 */
107static
108int _mkdir_recursive(void *_data)
109{
e11d277b 110 struct run_as_mkdir_data *data = _data;
60b6c79c 111 const char *path;
60b6c79c 112 mode_t mode;
60b6c79c
MD
113
114 path = data->path;
115 mode = data->mode;
116
3fd15a74 117 return utils_mkdir_recursive(path, mode);
60b6c79c
MD
118}
119
120static
121int _mkdir(void *_data)
122{
7ce36756 123 int ret;
e11d277b 124 struct run_as_mkdir_data *data = _data;
7ce36756
DG
125
126 ret = mkdir(data->path, data->mode);
127 if (ret < 0) {
128 ret = -errno;
129 }
130
131 return ret;
60b6c79c
MD
132}
133
134static
135int _open(void *_data)
136{
e11d277b 137 struct run_as_open_data *data = _data;
60b6c79c
MD
138 return open(data->path, data->flags, data->mode);
139}
140
4628484a
MD
141static
142int _unlink(void *_data)
143{
144 int ret;
145 struct run_as_unlink_data *data = _data;
146
147 ret = unlink(data->path);
148 if (ret < 0) {
149 ret = -errno;
150 }
151
152 return ret;
153}
154
155static
156int _recursive_rmdir(void *_data)
157{
158 int ret;
159 struct run_as_recursive_rmdir_data *data = _data;
160
161 ret = utils_recursive_rmdir(data->path);
162 if (ret < 0) {
163 ret = -errno;
164 }
165
166 return ret;
167}
168
c2b75c49
MD
169static
170int child_run_as(void *_data)
171{
6775595e 172 int ret;
c2b75c49 173 struct run_as_data *data = _data;
6775595e 174 ssize_t writelen;
6cd525e8 175 int sendret;
c2b75c49
MD
176
177 /*
178 * Child: it is safe to drop egid and euid while sharing the
179 * file descriptors with the parent process, since we do not
180 * drop "uid": therefore, the user we are dropping egid/euid to
181 * cannot attach to this process with, e.g. ptrace, nor map this
182 * process memory.
183 */
1576d582
MD
184 if (data->gid != getegid()) {
185 ret = setegid(data->gid);
186 if (ret < 0) {
4c462e79 187 PERROR("setegid");
6cd525e8 188 sendret = -1;
6d73c4ef 189 goto write_return;
1576d582 190 }
c2b75c49 191 }
1576d582
MD
192 if (data->uid != geteuid()) {
193 ret = seteuid(data->uid);
194 if (ret < 0) {
4c462e79 195 PERROR("seteuid");
6cd525e8 196 sendret = -1;
6d73c4ef 197 goto write_return;
1576d582 198 }
c2b75c49
MD
199 }
200 /*
201 * Also set umask to 0 for mkdir executable bit.
202 */
203 umask(0);
6cd525e8 204 sendret = (*data->cmd)(data->data);
6d73c4ef
MD
205
206write_return:
c2b75c49 207 /* send back return value */
6cd525e8
MD
208 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
209 if (writelen < sizeof(sendret)) {
210 PERROR("lttng_write error");
211 return EXIT_FAILURE;
212 } else {
213 return EXIT_SUCCESS;
214 }
c2b75c49
MD
215}
216
60b6c79c 217static
2d85a600 218int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
60b6c79c 219{
c2b75c49 220 struct run_as_data run_as_data;
60b6c79c 221 int ret = 0;
6cd525e8 222 ssize_t readlen;
c2b75c49 223 int status;
60b6c79c 224 pid_t pid;
c2b75c49 225 int retval_pipe[2];
d5bd7573 226 void *child_stack;
6cd525e8 227 int retval;
60b6c79c
MD
228
229 /*
230 * If we are non-root, we can only deal with our own uid.
231 */
232 if (geteuid() != 0) {
233 if (uid != geteuid()) {
234 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
235 uid, geteuid());
236 return -EPERM;
237 }
60b6c79c
MD
238 }
239
c2b75c49
MD
240 ret = pipe(retval_pipe);
241 if (ret < 0) {
4c462e79 242 PERROR("pipe");
6cd525e8 243 retval = ret;
60b6c79c 244 goto end;
c2b75c49
MD
245 }
246 run_as_data.data = data;
247 run_as_data.cmd = cmd;
248 run_as_data.uid = uid;
249 run_as_data.gid = gid;
250 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
e11d277b 251 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
d5bd7573 252 PROT_WRITE | PROT_READ,
0756d557 253 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
d5bd7573
MD
254 -1, 0);
255 if (child_stack == MAP_FAILED) {
4c462e79 256 PERROR("mmap");
6cd525e8 257 retval = -ENOMEM;
d5bd7573
MD
258 goto close_pipe;
259 }
260 /*
261 * Pointing to the middle of the stack to support architectures
262 * where the stack grows up (HPPA).
263 */
89831a74
MD
264 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
265 &run_as_data);
c2b75c49 266 if (pid < 0) {
4c462e79 267 PERROR("clone");
6cd525e8 268 retval = pid;
d5bd7573 269 goto unmap_stack;
c2b75c49
MD
270 }
271 /* receive return value */
6cd525e8
MD
272 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
273 if (readlen < sizeof(retval)) {
274 ret = -1;
275 }
c2b75c49
MD
276
277 /*
278 * Parent: wait for child to return, in which case the
279 * shared memory map will have been created.
280 */
47fb7563 281 pid = waitpid(pid, &status, 0);
c2b75c49 282 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
4c462e79 283 PERROR("wait");
6cd525e8 284 retval = -1;
60b6c79c 285 }
d5bd7573 286unmap_stack:
e11d277b 287 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
d5bd7573 288 if (ret < 0) {
4c462e79 289 PERROR("munmap");
6cd525e8 290 retval = ret;
d5bd7573 291 }
c2b75c49 292close_pipe:
4c462e79
MD
293 ret = close(retval_pipe[0]);
294 if (ret) {
295 PERROR("close");
296 }
297 ret = close(retval_pipe[1]);
298 if (ret) {
299 PERROR("close");
300 }
60b6c79c 301end:
6cd525e8 302 return retval;
60b6c79c
MD
303}
304
2d85a600
MD
305/*
306 * To be used on setups where gdb has issues debugging programs using
307 * clone/rfork. Note that this is for debuging ONLY, and should not be
308 * considered secure.
309 */
310static
311int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
312{
5b73926f
DG
313 int ret;
314 mode_t old_mask;
315
316 old_mask = umask(0);
317 ret = cmd(data);
318 umask(old_mask);
319
320 return ret;
2d85a600
MD
321}
322
323static
324int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
325{
8f0044bf 326 if (use_clone()) {
9ef70f87
MD
327 int ret;
328
2d85a600 329 DBG("Using run_as_clone");
9ef70f87
MD
330 pthread_mutex_lock(&lttng_libc_state_lock);
331 ret = run_as_clone(cmd, data, uid, gid);
332 pthread_mutex_unlock(&lttng_libc_state_lock);
333 return ret;
2d85a600
MD
334 } else {
335 DBG("Using run_as_noclone");
336 return run_as_noclone(cmd, data, uid, gid);
337 }
338}
339
90e535ef 340LTTNG_HIDDEN
e11d277b 341int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 342{
e11d277b 343 struct run_as_mkdir_data data;
60b6c79c
MD
344
345 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
346 path, mode, uid, gid);
347 data.path = path;
348 data.mode = mode;
349 return run_as(_mkdir_recursive, &data, uid, gid);
350}
351
90e535ef 352LTTNG_HIDDEN
e11d277b 353int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 354{
e11d277b 355 struct run_as_mkdir_data data;
60b6c79c
MD
356
357 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
358 path, mode, uid, gid);
359 data.path = path;
360 data.mode = mode;
361 return run_as(_mkdir, &data, uid, gid);
362}
363
364/*
365 * Note: open_run_as is currently not working. We'd need to pass the fd
366 * opened in the child to the parent.
367 */
90e535ef 368LTTNG_HIDDEN
e11d277b 369int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 370{
e11d277b 371 struct run_as_open_data data;
c2b75c49 372
47fb7563
MD
373 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
374 path, flags, mode, uid, gid);
60b6c79c
MD
375 data.path = path;
376 data.flags = flags;
377 data.mode = mode;
378 return run_as(_open, &data, uid, gid);
60b6c79c 379}
4628484a
MD
380
381LTTNG_HIDDEN
382int run_as_unlink(const char *path, uid_t uid, gid_t gid)
383{
384 struct run_as_unlink_data data;
385
386 DBG3("unlink() %s with for uid %d and gid %d",
387 path, uid, gid);
388 data.path = path;
389 return run_as(_unlink, &data, uid, gid);
390}
391
392LTTNG_HIDDEN
393int run_as_recursive_rmdir(const char *path, uid_t uid, gid_t gid)
394{
395 struct run_as_recursive_rmdir_data data;
396
397 DBG3("recursive_rmdir() %s with for uid %d and gid %d",
398 path, uid, gid);
399 data.path = path;
400 return run_as(_recursive_rmdir, &data, uid, gid);
401}
This page took 0.090818 seconds and 4 git commands to generate.