2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 * Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
27 #include <sys/types.h>
32 #include <sys/signal.h>
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>
42 #define RUNAS_CHILD_STACK_SIZE 10485760
49 /* FreeBSD MAP_STACK always return -ENOMEM */
50 #define LTTNG_MAP_STACK 0
52 #define LTTNG_MAP_STACK MAP_STACK
56 #define MAP_GROWSDOWN 0
60 #define MAP_ANONYMOUS MAP_ANON
64 int (*cmd
)(void *data
);
71 struct run_as_mkdir_data
{
76 struct run_as_open_data
{
82 struct run_as_unlink_data
{
86 struct run_as_recursive_rmdir_data
{
100 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
105 * Create recursively directory using the FULL path.
108 int _mkdir_recursive(void *_data
)
110 struct run_as_mkdir_data
*data
= _data
;
117 return utils_mkdir_recursive(path
, mode
);
121 int _mkdir(void *_data
)
124 struct run_as_mkdir_data
*data
= _data
;
126 ret
= mkdir(data
->path
, data
->mode
);
135 int _open(void *_data
)
137 struct run_as_open_data
*data
= _data
;
138 return open(data
->path
, data
->flags
, data
->mode
);
142 int _unlink(void *_data
)
145 struct run_as_unlink_data
*data
= _data
;
147 ret
= unlink(data
->path
);
156 int _recursive_rmdir(void *_data
)
159 struct run_as_recursive_rmdir_data
*data
= _data
;
161 ret
= utils_recursive_rmdir(data
->path
);
170 int child_run_as(void *_data
)
173 struct run_as_data
*data
= _data
;
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
184 if (data
->gid
!= getegid()) {
185 ret
= setegid(data
->gid
);
192 if (data
->uid
!= geteuid()) {
193 ret
= seteuid(data
->uid
);
201 * Also set umask to 0 for mkdir executable bit.
204 sendret
= (*data
->cmd
)(data
->data
);
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");
218 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
220 struct run_as_data run_as_data
;
230 * If we are non-root, we can only deal with our own uid.
232 if (geteuid() != 0) {
233 if (uid
!= geteuid()) {
234 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
240 ret
= pipe(retval_pipe
);
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
,
255 if (child_stack
== MAP_FAILED
) {
261 * Pointing to the middle of the stack to support architectures
262 * where the stack grows up (HPPA).
264 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
271 /* receive return value */
272 readlen
= lttng_read(retval_pipe
[0], &retval
, sizeof(retval
));
273 if (readlen
< sizeof(retval
)) {
278 * Parent: wait for child to return, in which case the
279 * shared memory map will have been created.
281 pid
= waitpid(pid
, &status
, 0);
282 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
287 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
293 ret
= close(retval_pipe
[0]);
297 ret
= close(retval_pipe
[1]);
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
311 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
324 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
329 DBG("Using run_as_clone");
330 pthread_mutex_lock(<tng_libc_state_lock
);
331 ret
= run_as_clone(cmd
, data
, uid
, gid
);
332 pthread_mutex_unlock(<tng_libc_state_lock
);
335 DBG("Using run_as_noclone");
336 return run_as_noclone(cmd
, data
, uid
, gid
);
341 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
343 struct run_as_mkdir_data data
;
345 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
346 path
, mode
, uid
, gid
);
349 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
353 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
355 struct run_as_mkdir_data data
;
357 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
358 path
, mode
, uid
, gid
);
361 return run_as(_mkdir
, &data
, uid
, gid
);
365 * Note: open_run_as is currently not working. We'd need to pass the fd
366 * opened in the child to the parent.
369 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
371 struct run_as_open_data data
;
373 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
374 path
, flags
, mode
, uid
, gid
);
378 return run_as(_open
, &data
, uid
, gid
);
382 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
384 struct run_as_unlink_data data
;
386 DBG3("unlink() %s with for uid %d and gid %d",
389 return run_as(_unlink
, &data
, uid
, gid
);
393 int run_as_recursive_rmdir(const char *path
, uid_t uid
, gid_t gid
)
395 struct run_as_recursive_rmdir_data data
;
397 DBG3("recursive_rmdir() %s with for uid %d and gid %d",
400 return run_as(_recursive_rmdir
, &data
, uid
, gid
);