Move file creation/unlink from liblttng-ust-ctl to consumerd
[lttng-tools.git] / src / common / runas.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
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.
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
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
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.
17 */
18
19#define _GNU_SOURCE
20#define _LGPL_SOURCE
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>
31#include <sched.h>
32#include <sys/signal.h>
33
34#include <common/common.h>
35#include <common/utils.h>
36#include <common/compat/mman.h>
37#include <common/compat/clone.h>
38#include <common/compat/getenv.h>
39
40#include "runas.h"
41
42#define RUNAS_CHILD_STACK_SIZE 10485760
43
44#ifndef MAP_STACK
45#define MAP_STACK 0
46#endif
47
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
53#endif
54
55#ifndef MAP_GROWSDOWN
56#define MAP_GROWSDOWN 0
57#endif
58
59#ifndef MAP_ANONYMOUS
60#define MAP_ANONYMOUS MAP_ANON
61#endif
62
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
71struct run_as_mkdir_data {
72 const char *path;
73 mode_t mode;
74};
75
76struct run_as_open_data {
77 const char *path;
78 int flags;
79 mode_t mode;
80};
81
82struct run_as_unlink_data {
83 const char *path;
84};
85
86struct run_as_recursive_rmdir_data {
87 const char *path;
88};
89
90#ifdef VALGRIND
91static
92int use_clone(void)
93{
94 return 0;
95}
96#else
97static
98int use_clone(void)
99{
100 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
101}
102#endif
103
104/*
105 * Create recursively directory using the FULL path.
106 */
107static
108int _mkdir_recursive(void *_data)
109{
110 struct run_as_mkdir_data *data = _data;
111 const char *path;
112 mode_t mode;
113
114 path = data->path;
115 mode = data->mode;
116
117 return utils_mkdir_recursive(path, mode);
118}
119
120static
121int _mkdir(void *_data)
122{
123 int ret;
124 struct run_as_mkdir_data *data = _data;
125
126 ret = mkdir(data->path, data->mode);
127 if (ret < 0) {
128 ret = -errno;
129 }
130
131 return ret;
132}
133
134static
135int _open(void *_data)
136{
137 struct run_as_open_data *data = _data;
138 return open(data->path, data->flags, data->mode);
139}
140
141static
142int _unlink(void *_data)
143{
144 int ret;
145 struct run_as_unlink_data *data = _data;
146
147 ret = unlink(data->path);
148 if (ret < 0) {
149 ret = -errno;
150 }
151
152 return ret;
153}
154
155static
156int _recursive_rmdir(void *_data)
157{
158 int ret;
159 struct run_as_recursive_rmdir_data *data = _data;
160
161 ret = utils_recursive_rmdir(data->path);
162 if (ret < 0) {
163 ret = -errno;
164 }
165
166 return ret;
167}
168
169static
170int child_run_as(void *_data)
171{
172 int ret;
173 struct run_as_data *data = _data;
174 ssize_t writelen;
175 int sendret;
176
177 /*
178 * Child: it is safe to drop egid and euid while sharing the
179 * file descriptors with the parent process, since we do not
180 * drop "uid": therefore, the user we are dropping egid/euid to
181 * cannot attach to this process with, e.g. ptrace, nor map this
182 * process memory.
183 */
184 if (data->gid != getegid()) {
185 ret = setegid(data->gid);
186 if (ret < 0) {
187 PERROR("setegid");
188 sendret = -1;
189 goto write_return;
190 }
191 }
192 if (data->uid != geteuid()) {
193 ret = seteuid(data->uid);
194 if (ret < 0) {
195 PERROR("seteuid");
196 sendret = -1;
197 goto write_return;
198 }
199 }
200 /*
201 * Also set umask to 0 for mkdir executable bit.
202 */
203 umask(0);
204 sendret = (*data->cmd)(data->data);
205
206write_return:
207 /* send back return value */
208 writelen = lttng_write(data->retval_pipe, &sendret, sizeof(sendret));
209 if (writelen < sizeof(sendret)) {
210 PERROR("lttng_write error");
211 return EXIT_FAILURE;
212 } else {
213 return EXIT_SUCCESS;
214 }
215}
216
217static
218int run_as_clone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
219{
220 struct run_as_data run_as_data;
221 int ret = 0;
222 ssize_t readlen;
223 int status;
224 pid_t pid;
225 int retval_pipe[2];
226 void *child_stack;
227 int retval;
228
229 /*
230 * If we are non-root, we can only deal with our own uid.
231 */
232 if (geteuid() != 0) {
233 if (uid != geteuid()) {
234 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
235 uid, geteuid());
236 return -EPERM;
237 }
238 }
239
240 ret = pipe(retval_pipe);
241 if (ret < 0) {
242 PERROR("pipe");
243 retval = ret;
244 goto end;
245 }
246 run_as_data.data = data;
247 run_as_data.cmd = cmd;
248 run_as_data.uid = uid;
249 run_as_data.gid = gid;
250 run_as_data.retval_pipe = retval_pipe[1]; /* write end */
251 child_stack = mmap(NULL, RUNAS_CHILD_STACK_SIZE,
252 PROT_WRITE | PROT_READ,
253 MAP_PRIVATE | MAP_GROWSDOWN | MAP_ANONYMOUS | LTTNG_MAP_STACK,
254 -1, 0);
255 if (child_stack == MAP_FAILED) {
256 PERROR("mmap");
257 retval = -ENOMEM;
258 goto close_pipe;
259 }
260 /*
261 * Pointing to the middle of the stack to support architectures
262 * where the stack grows up (HPPA).
263 */
264 pid = lttng_clone_files(child_run_as, child_stack + (RUNAS_CHILD_STACK_SIZE / 2),
265 &run_as_data);
266 if (pid < 0) {
267 PERROR("clone");
268 retval = pid;
269 goto unmap_stack;
270 }
271 /* receive return value */
272 readlen = lttng_read(retval_pipe[0], &retval, sizeof(retval));
273 if (readlen < sizeof(retval)) {
274 ret = -1;
275 }
276
277 /*
278 * Parent: wait for child to return, in which case the
279 * shared memory map will have been created.
280 */
281 pid = waitpid(pid, &status, 0);
282 if (pid < 0 || !WIFEXITED(status) || WEXITSTATUS(status) != 0) {
283 PERROR("wait");
284 retval = -1;
285 }
286unmap_stack:
287 ret = munmap(child_stack, RUNAS_CHILD_STACK_SIZE);
288 if (ret < 0) {
289 PERROR("munmap");
290 retval = ret;
291 }
292close_pipe:
293 ret = close(retval_pipe[0]);
294 if (ret) {
295 PERROR("close");
296 }
297 ret = close(retval_pipe[1]);
298 if (ret) {
299 PERROR("close");
300 }
301end:
302 return retval;
303}
304
305/*
306 * To be used on setups where gdb has issues debugging programs using
307 * clone/rfork. Note that this is for debuging ONLY, and should not be
308 * considered secure.
309 */
310static
311int run_as_noclone(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
312{
313 int ret;
314 mode_t old_mask;
315
316 old_mask = umask(0);
317 ret = cmd(data);
318 umask(old_mask);
319
320 return ret;
321}
322
323static
324int run_as(int (*cmd)(void *data), void *data, uid_t uid, gid_t gid)
325{
326 if (use_clone()) {
327 int ret;
328
329 DBG("Using run_as_clone");
330 pthread_mutex_lock(&lttng_libc_state_lock);
331 ret = run_as_clone(cmd, data, uid, gid);
332 pthread_mutex_unlock(&lttng_libc_state_lock);
333 return ret;
334 } else {
335 DBG("Using run_as_noclone");
336 return run_as_noclone(cmd, data, uid, gid);
337 }
338}
339
340LTTNG_HIDDEN
341int run_as_mkdir_recursive(const char *path, mode_t mode, uid_t uid, gid_t gid)
342{
343 struct run_as_mkdir_data data;
344
345 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
346 path, mode, uid, gid);
347 data.path = path;
348 data.mode = mode;
349 return run_as(_mkdir_recursive, &data, uid, gid);
350}
351
352LTTNG_HIDDEN
353int run_as_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid)
354{
355 struct run_as_mkdir_data data;
356
357 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
358 path, mode, uid, gid);
359 data.path = path;
360 data.mode = mode;
361 return run_as(_mkdir, &data, uid, gid);
362}
363
364/*
365 * Note: open_run_as is currently not working. We'd need to pass the fd
366 * opened in the child to the parent.
367 */
368LTTNG_HIDDEN
369int run_as_open(const char *path, int flags, mode_t mode, uid_t uid, gid_t gid)
370{
371 struct run_as_open_data data;
372
373 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
374 path, flags, mode, uid, gid);
375 data.path = path;
376 data.flags = flags;
377 data.mode = mode;
378 return run_as(_open, &data, uid, gid);
379}
380
381LTTNG_HIDDEN
382int run_as_unlink(const char *path, uid_t uid, gid_t gid)
383{
384 struct run_as_unlink_data data;
385
386 DBG3("unlink() %s with for uid %d and gid %d",
387 path, uid, gid);
388 data.path = path;
389 return run_as(_unlink, &data, uid, gid);
390}
391
392LTTNG_HIDDEN
393int run_as_recursive_rmdir(const char *path, uid_t uid, gid_t gid)
394{
395 struct run_as_recursive_rmdir_data data;
396
397 DBG3("recursive_rmdir() %s with for uid %d and gid %d",
398 path, uid, gid);
399 data.path = path;
400 return run_as(_recursive_rmdir, &data, uid, gid);
401}
This page took 0.024631 seconds and 4 git commands to generate.