Fix: race with index file creation
[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{
7ce36756 99 int ret;
e11d277b 100 struct run_as_mkdir_data *data = _data;
7ce36756
DG
101
102 ret = mkdir(data->path, data->mode);
103 if (ret < 0) {
104 ret = -errno;
105 }
106
107 return ret;
60b6c79c
MD
108}
109
110static
111int _open(void *_data)
112{
e11d277b 113 struct run_as_open_data *data = _data;
60b6c79c
MD
114 return open(data->path, data->flags, data->mode);
115}
116
c2b75c49
MD
117static
118int child_run_as(void *_data)
119{
6775595e 120 int ret;
c2b75c49 121 struct run_as_data *data = _data;
6775595e 122 ssize_t writelen;
6cd525e8 123 int sendret;
c2b75c49
MD
124
125 /*
126 * Child: it is safe to drop egid and euid while sharing the
127 * file descriptors with the parent process, since we do not
128 * drop "uid": therefore, the user we are dropping egid/euid to
129 * cannot attach to this process with, e.g. ptrace, nor map this
130 * process memory.
131 */
1576d582
MD
132 if (data->gid != getegid()) {
133 ret = setegid(data->gid);
134 if (ret < 0) {
4c462e79 135 PERROR("setegid");
6cd525e8 136 sendret = -1;
6d73c4ef 137 goto write_return;
1576d582 138 }
c2b75c49 139 }
1576d582
MD
140 if (data->uid != geteuid()) {
141 ret = seteuid(data->uid);
142 if (ret < 0) {
4c462e79 143 PERROR("seteuid");
6cd525e8 144 sendret = -1;
6d73c4ef 145 goto write_return;
1576d582 146 }
c2b75c49
MD
147 }
148 /*
149 * Also set umask to 0 for mkdir executable bit.
150 */
151 umask(0);
6cd525e8 152 sendret = (*data->cmd)(data->data);
6d73c4ef
MD
153
154write_return:
c2b75c49 155 /* send back return value */
6cd525e8
MD
156 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
157 if (writelen < sizeof(sendret)) {
158 PERROR("lttng_write error");
159 return EXIT_FAILURE;
160 } else {
161 return EXIT_SUCCESS;
162 }
c2b75c49
MD
163}
164
60b6c79c 165static
2d85a600 166int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
60b6c79c 167{
c2b75c49 168 struct run_as_data run_as_data;
60b6c79c 169 int ret = 0;
6cd525e8 170 ssize_t readlen;
c2b75c49 171 int status;
60b6c79c 172 pid_t pid;
c2b75c49 173 int retval_pipe[2];
d5bd7573 174 void *child_stack;
6cd525e8 175 int retval;
60b6c79c
MD
176
177 /*
178 * If we are non-root, we can only deal with our own uid.
179 */
180 if (geteuid() != 0) {
181 if (uid != geteuid()) {
182 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
183 uid, geteuid());
184 return -EPERM;
185 }
60b6c79c
MD
186 }
187
c2b75c49
MD
188 ret = pipe(retval_pipe);
189 if (ret < 0) {
4c462e79 190 PERROR("pipe");
6cd525e8 191 retval = ret;
60b6c79c 192 goto end;
c2b75c49
MD
193 }
194 run_as_data.data = data;
195 run_as_data.cmd = cmd;
196 run_as_data.uid = uid;
197 run_as_data.gid = gid;
198 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
e11d277b 199 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
d5bd7573 200 PROT_WRITE | PROT_READ,
0756d557 201 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
d5bd7573
MD
202 -1, 0);
203 if (child_stack == MAP_FAILED) {
4c462e79 204 PERROR("mmap");
6cd525e8 205 retval = -ENOMEM;
d5bd7573
MD
206 goto close_pipe;
207 }
208 /*
209 * Pointing to the middle of the stack to support architectures
210 * where the stack grows up (HPPA).
211 */
89831a74
MD
212 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
213 &run_as_data);
c2b75c49 214 if (pid < 0) {
4c462e79 215 PERROR("clone");
6cd525e8 216 retval = pid;
d5bd7573 217 goto unmap_stack;
c2b75c49
MD
218 }
219 /* receive return value */
6cd525e8
MD
220 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
221 if (readlen < sizeof(retval)) {
222 ret = -1;
223 }
c2b75c49
MD
224
225 /*
226 * Parent: wait for child to return, in which case the
227 * shared memory map will have been created.
228 */
47fb7563 229 pid = waitpid(pid, &status, 0);
c2b75c49 230 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
4c462e79 231 PERROR("wait");
6cd525e8 232 retval = -1;
60b6c79c 233 }
d5bd7573 234unmap_stack:
e11d277b 235 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
d5bd7573 236 if (ret < 0) {
4c462e79 237 PERROR("munmap");
6cd525e8 238 retval = ret;
d5bd7573 239 }
c2b75c49 240close_pipe:
4c462e79
MD
241 ret = close(retval_pipe[0]);
242 if (ret) {
243 PERROR("close");
244 }
245 ret = close(retval_pipe[1]);
246 if (ret) {
247 PERROR("close");
248 }
60b6c79c 249end:
6cd525e8 250 return retval;
60b6c79c
MD
251}
252
2d85a600
MD
253/*
254 * To be used on setups where gdb has issues debugging programs using
255 * clone/rfork. Note that this is for debuging ONLY, and should not be
256 * considered secure.
257 */
258static
259int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
260{
5b73926f
DG
261 int ret;
262 mode_t old_mask;
263
264 old_mask = umask(0);
265 ret = cmd(data);
266 umask(old_mask);
267
268 return ret;
2d85a600
MD
269}
270
271static
272int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
273{
274 if (!getenv("LTTNG_DEBUG_NOCLONE")) {
9ef70f87
MD
275 int ret;
276
2d85a600 277 DBG("Using run_as_clone");
9ef70f87
MD
278 pthread_mutex_lock(&lttng_libc_state_lock);
279 ret = run_as_clone(cmd, data, uid, gid);
280 pthread_mutex_unlock(&lttng_libc_state_lock);
281 return ret;
2d85a600
MD
282 } else {
283 DBG("Using run_as_noclone");
284 return run_as_noclone(cmd, data, uid, gid);
285 }
286}
287
90e535ef 288LTTNG_HIDDEN
e11d277b 289int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 290{
e11d277b 291 struct run_as_mkdir_data data;
60b6c79c
MD
292
293 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
294 path, mode, uid, gid);
295 data.path = path;
296 data.mode = mode;
297 return run_as(_mkdir_recursive, &data, uid, gid);
298}
299
90e535ef 300LTTNG_HIDDEN
e11d277b 301int run_as_mkdir(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() %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, &data, uid, gid);
310}
311
312/*
313 * Note: open_run_as is currently not working. We'd need to pass the fd
314 * opened in the child to the parent.
315 */
90e535ef 316LTTNG_HIDDEN
e11d277b 317int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
60b6c79c 318{
e11d277b 319 struct run_as_open_data data;
c2b75c49 320
47fb7563
MD
321 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
322 path, flags, mode, uid, gid);
60b6c79c
MD
323 data.path = path;
324 data.flags = flags;
325 data.mode = mode;
326 return run_as(_open, &data, uid, gid);
60b6c79c 327}
This page took 0.074568 seconds and 4 git commands to generate.