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