a8fe47e8eafe7b2f91fb7ea0a1bb205c02c50117
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
{
90 return !getenv("LTTNG_DEBUG_NOCLONE");
95 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
98 * Create recursively directory using the FULL path.
101 int _mkdir_recursive(void *_data
)
103 struct run_as_mkdir_data
*data
= _data
;
110 /* Safe to call as we have transitioned to the requested uid/gid. */
111 return _utils_mkdir_recursive_unsafe(path
, mode
);
115 int _mkdir(void *_data
)
118 struct run_as_mkdir_data
*data
= _data
;
120 ret
= mkdir(data
->path
, data
->mode
);
129 int _open(void *_data
)
131 struct run_as_open_data
*data
= _data
;
132 return open(data
->path
, data
->flags
, data
->mode
);
136 int child_run_as(void *_data
)
139 struct run_as_data
*data
= _data
;
144 * Child: it is safe to drop egid and euid while sharing the
145 * file descriptors with the parent process, since we do not
146 * drop "uid": therefore, the user we are dropping egid/euid to
147 * cannot attach to this process with, e.g. ptrace, nor map this
150 if (data
->gid
!= getegid()) {
151 ret
= setegid(data
->gid
);
158 if (data
->uid
!= geteuid()) {
159 ret
= seteuid(data
->uid
);
167 * Also set umask to 0 for mkdir executable bit.
170 sendret
= (*data
->cmd
)(data
->data
);
173 /* send back return value */
174 writelen
= lttng_write(data
->retval_pipe
, &sendret
, sizeof(sendret
));
175 if (writelen
< sizeof(sendret
)) {
176 PERROR("lttng_write error");
184 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
186 struct run_as_data run_as_data
;
196 * If we are non-root, we can only deal with our own uid.
198 if (geteuid() != 0) {
199 if (uid
!= geteuid()) {
200 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
206 ret
= pipe(retval_pipe
);
212 run_as_data
.data
= data
;
213 run_as_data
.cmd
= cmd
;
214 run_as_data
.uid
= uid
;
215 run_as_data
.gid
= gid
;
216 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
217 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
218 PROT_WRITE
| PROT_READ
,
219 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
221 if (child_stack
== MAP_FAILED
) {
227 * Pointing to the middle of the stack to support architectures
228 * where the stack grows up (HPPA).
230 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
237 /* receive return value */
238 readlen
= lttng_read(retval_pipe
[0], &retval
, sizeof(retval
));
239 if (readlen
< sizeof(retval
)) {
244 * Parent: wait for child to return, in which case the
245 * shared memory map will have been created.
247 pid
= waitpid(pid
, &status
, 0);
248 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
253 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
259 ret
= close(retval_pipe
[0]);
263 ret
= close(retval_pipe
[1]);
272 * To be used on setups where gdb has issues debugging programs using
273 * clone/rfork. Note that this is for debuging ONLY, and should not be
277 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
290 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
295 DBG("Using run_as_clone");
296 pthread_mutex_lock(<tng_libc_state_lock
);
297 ret
= run_as_clone(cmd
, data
, uid
, gid
);
298 pthread_mutex_unlock(<tng_libc_state_lock
);
301 DBG("Using run_as_noclone");
302 return run_as_noclone(cmd
, data
, uid
, gid
);
307 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
309 struct run_as_mkdir_data data
;
311 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
312 path
, mode
, uid
, gid
);
315 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
319 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
321 struct run_as_mkdir_data data
;
323 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
324 path
, mode
, uid
, gid
);
327 return run_as(_mkdir
, &data
, uid
, gid
);
331 * Note: open_run_as is currently not working. We'd need to pass the fd
332 * opened in the child to the parent.
335 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
337 struct run_as_open_data data
;
339 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
340 path
, flags
, mode
, uid
, gid
);
344 return run_as(_open
, &data
, uid
, gid
);
This page took 0.037517 seconds and 4 git commands to generate.