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