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