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.
26 #include <sys/types.h>
31 #include <sys/signal.h>
33 #include <common/common.h>
34 #include <common/utils.h>
35 #include <common/compat/mman.h>
36 #include <common/compat/clone.h>
40 #define RUNAS_CHILD_STACK_SIZE 10485760
47 /* FreeBSD MAP_STACK always return -ENOMEM */
48 #define LTTNG_MAP_STACK 0
50 #define LTTNG_MAP_STACK MAP_STACK
54 #define MAP_GROWSDOWN 0
58 #define MAP_ANONYMOUS MAP_ANON
62 int (*cmd
)(void *data
);
69 struct run_as_mkdir_data
{
74 struct run_as_open_data
{
80 struct run_as_unlink_data
{
94 return !getenv("LTTNG_DEBUG_NOCLONE");
99 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
102 * Create recursively directory using the FULL path.
105 int _mkdir_recursive(void *_data
)
107 struct run_as_mkdir_data
*data
= _data
;
114 /* Safe to call as we have transitioned to the requested uid/gid. */
115 return _utils_mkdir_recursive_unsafe(path
, mode
);
119 int _mkdir(void *_data
)
122 struct run_as_mkdir_data
*data
= _data
;
124 ret
= mkdir(data
->path
, data
->mode
);
133 int _open(void *_data
)
135 struct run_as_open_data
*data
= _data
;
136 return open(data
->path
, data
->flags
, data
->mode
);
140 int _unlink(void *_data
)
143 struct run_as_unlink_data
*data
= _data
;
145 ret
= unlink(data
->path
);
154 int child_run_as(void *_data
)
157 struct run_as_data
*data
= _data
;
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
168 if (data
->gid
!= getegid()) {
169 ret
= setegid(data
->gid
);
176 if (data
->uid
!= geteuid()) {
177 ret
= seteuid(data
->uid
);
185 * Also set umask to 0 for mkdir executable bit.
188 sendret
= (*data
->cmd
)(data
->data
);
191 /* send back return value */
192 writelen
= lttng_write(data
->retval_pipe
, &sendret
, sizeof(sendret
));
193 if (writelen
< sizeof(sendret
)) {
194 PERROR("lttng_write error");
202 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
204 struct run_as_data run_as_data
;
214 * If we are non-root, we can only deal with our own uid.
216 if (geteuid() != 0) {
217 if (uid
!= geteuid()) {
218 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
224 ret
= pipe(retval_pipe
);
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 */
235 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
236 PROT_WRITE
| PROT_READ
,
237 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
239 if (child_stack
== MAP_FAILED
) {
245 * Pointing to the middle of the stack to support architectures
246 * where the stack grows up (HPPA).
248 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
255 /* receive return value */
256 readlen
= lttng_read(retval_pipe
[0], &retval
, sizeof(retval
));
257 if (readlen
< sizeof(retval
)) {
262 * Parent: wait for child to return, in which case the
263 * shared memory map will have been created.
265 pid
= waitpid(pid
, &status
, 0);
266 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
271 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
277 ret
= close(retval_pipe
[0]);
281 ret
= close(retval_pipe
[1]);
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
295 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
308 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
313 DBG("Using run_as_clone");
314 pthread_mutex_lock(<tng_libc_state_lock
);
315 ret
= run_as_clone(cmd
, data
, uid
, gid
);
316 pthread_mutex_unlock(<tng_libc_state_lock
);
319 DBG("Using run_as_noclone");
320 return run_as_noclone(cmd
, data
, uid
, gid
);
325 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
327 struct run_as_mkdir_data data
;
329 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
330 path
, mode
, uid
, gid
);
333 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
337 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
339 struct run_as_mkdir_data data
;
341 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
342 path
, mode
, uid
, gid
);
345 return run_as(_mkdir
, &data
, uid
, gid
);
349 * Note: open_run_as is currently not working. We'd need to pass the fd
350 * opened in the child to the parent.
353 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
355 struct run_as_open_data data
;
357 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
358 path
, flags
, mode
, uid
, gid
);
362 return run_as(_open
, &data
, uid
, gid
);
366 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
368 struct run_as_unlink_data data
;
370 DBG3("unlink() %s with for uid %d and gid %d",
373 return run_as(_unlink
, &data
, uid
, gid
);
This page took 0.041458 seconds and 5 git commands to generate.