3ae2e4c38d35e5d943f8ca9f3e49213d14472ed4
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
{
81 * Create recursively directory using the FULL path.
84 int _mkdir_recursive(void *_data
)
86 struct run_as_mkdir_data
*data
= _data
;
93 return utils_mkdir_recursive(path
, mode
);
97 int _mkdir(void *_data
)
100 struct run_as_mkdir_data
*data
= _data
;
102 ret
= mkdir(data
->path
, data
->mode
);
111 int _open(void *_data
)
113 struct run_as_open_data
*data
= _data
;
114 return open(data
->path
, data
->flags
, data
->mode
);
118 int child_run_as(void *_data
)
121 struct run_as_data
*data
= _data
;
123 size_t writeleft
, index
;
130 * Child: it is safe to drop egid and euid while sharing the
131 * file descriptors with the parent process, since we do not
132 * drop "uid": therefore, the user we are dropping egid/euid to
133 * cannot attach to this process with, e.g. ptrace, nor map this
136 if (data
->gid
!= getegid()) {
137 ret
= setegid(data
->gid
);
144 if (data
->uid
!= geteuid()) {
145 ret
= seteuid(data
->uid
);
153 * Also set umask to 0 for mkdir executable bit.
156 sendret
.i
= (*data
->cmd
)(data
->data
);
159 /* send back return value */
160 writeleft
= sizeof(sendret
);
164 writelen
= write(data
->retval_pipe
, &sendret
.c
[index
],
166 } while (writelen
< 0 && errno
== EINTR
);
171 writeleft
-= writelen
;
173 } while (writeleft
> 0);
178 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
180 struct run_as_data run_as_data
;
185 ssize_t readlen
, readleft
, index
;
193 * If we are non-root, we can only deal with our own uid.
195 if (geteuid() != 0) {
196 if (uid
!= geteuid()) {
197 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
203 ret
= pipe(retval_pipe
);
209 run_as_data
.data
= data
;
210 run_as_data
.cmd
= cmd
;
211 run_as_data
.uid
= uid
;
212 run_as_data
.gid
= gid
;
213 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
214 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
215 PROT_WRITE
| PROT_READ
,
216 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
218 if (child_stack
== MAP_FAILED
) {
224 * Pointing to the middle of the stack to support architectures
225 * where the stack grows up (HPPA).
227 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
234 /* receive return value */
235 readleft
= sizeof(retval
);
238 readlen
= read(retval_pipe
[0], &retval
.c
[index
], readleft
);
246 } while (readleft
> 0);
249 * Parent: wait for child to return, in which case the
250 * shared memory map will have been created.
252 pid
= waitpid(pid
, &status
, 0);
253 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
258 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
264 ret
= close(retval_pipe
[0]);
268 ret
= close(retval_pipe
[1]);
277 * To be used on setups where gdb has issues debugging programs using
278 * clone/rfork. Note that this is for debuging ONLY, and should not be
282 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
295 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
297 if (!getenv("LTTNG_DEBUG_NOCLONE")) {
300 DBG("Using run_as_clone");
301 pthread_mutex_lock(<tng_libc_state_lock
);
302 ret
= run_as_clone(cmd
, data
, uid
, gid
);
303 pthread_mutex_unlock(<tng_libc_state_lock
);
306 DBG("Using run_as_noclone");
307 return run_as_noclone(cmd
, data
, uid
, gid
);
312 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
314 struct run_as_mkdir_data data
;
316 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
317 path
, mode
, uid
, gid
);
320 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
324 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
326 struct run_as_mkdir_data data
;
328 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
329 path
, mode
, uid
, gid
);
332 return run_as(_mkdir
, &data
, uid
, gid
);
336 * Note: open_run_as is currently not working. We'd need to pass the fd
337 * opened in the child to the parent.
340 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
342 struct run_as_open_data data
;
344 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
345 path
, flags
, mode
, uid
, gid
);
349 return run_as(_open
, &data
, uid
, gid
);
This page took 0.041069 seconds and 3 git commands to generate.