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