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