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
{
99 return !getenv("LTTNG_DEBUG_NOCLONE");
104 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
107 * Create recursively directory using the FULL path.
110 int _mkdir_recursive(void *_data
)
112 struct run_as_mkdir_data
*data
= _data
;
119 /* Safe to call as we have transitioned to the requested uid/gid. */
120 return _utils_mkdir_recursive_unsafe(path
, mode
);
124 int _mkdir(void *_data
)
126 struct run_as_mkdir_data
*data
= _data
;
128 return mkdir(data
->path
, data
->mode
);
132 int _open(void *_data
)
134 struct run_as_open_data
*data
= _data
;
136 return open(data
->path
, data
->flags
, data
->mode
);
140 int _unlink(void *_data
)
142 struct run_as_unlink_data
*data
= _data
;
144 return unlink(data
->path
);
148 int child_run_as(void *_data
)
151 struct run_as_data
*data
= _data
;
153 struct run_as_ret sendret
;
156 * Child: it is safe to drop egid and euid while sharing the
157 * file descriptors with the parent process, since we do not
158 * drop "uid": therefore, the user we are dropping egid/euid to
159 * cannot attach to this process with, e.g. ptrace, nor map this
162 if (data
->gid
!= getegid()) {
163 ret
= setegid(data
->gid
);
169 if (data
->uid
!= geteuid()) {
170 ret
= seteuid(data
->uid
);
177 * Also set umask to 0 for mkdir executable bit.
180 ret
= (*data
->cmd
)(data
->data
);
184 sendret
._errno
= errno
;
185 /* send back return value */
186 writelen
= lttng_write(data
->retval_pipe
, &sendret
, sizeof(sendret
));
187 if (writelen
< sizeof(sendret
)) {
188 PERROR("lttng_write error");
196 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
198 struct run_as_data run_as_data
;
205 struct run_as_ret recvret
;
208 * If we are non-root, we can only deal with our own uid.
210 if (geteuid() != 0) {
211 if (uid
!= geteuid()) {
213 recvret
._errno
= EPERM
;
214 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
220 ret
= pipe(retval_pipe
);
223 recvret
._errno
= errno
;
227 run_as_data
.data
= data
;
228 run_as_data
.cmd
= cmd
;
229 run_as_data
.uid
= uid
;
230 run_as_data
.gid
= gid
;
231 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
232 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
233 PROT_WRITE
| PROT_READ
,
234 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
236 if (child_stack
== MAP_FAILED
) {
238 recvret
._errno
= ENOMEM
;
243 * Pointing to the middle of the stack to support architectures
244 * where the stack grows up (HPPA).
246 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
250 recvret
._errno
= errno
;
254 /* receive return value */
255 readlen
= lttng_read(retval_pipe
[0], &recvret
, sizeof(recvret
));
256 if (readlen
< sizeof(recvret
)) {
258 recvret
._errno
= errno
;
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) {
268 recvret
._errno
= errno
;
272 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
275 recvret
._errno
= errno
;
279 ret
= close(retval_pipe
[0]);
282 recvret
._errno
= errno
;
285 ret
= close(retval_pipe
[1]);
288 recvret
._errno
= errno
;
292 errno
= recvret
._errno
;
297 * To be used on setups where gdb has issues debugging programs using
298 * clone/rfork. Note that this is for debuging ONLY, and should not be
302 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
304 int ret
, saved_errno
;
317 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
322 DBG("Using run_as_clone");
323 pthread_mutex_lock(<tng_libc_state_lock
);
324 ret
= run_as_clone(cmd
, data
, uid
, gid
);
325 pthread_mutex_unlock(<tng_libc_state_lock
);
328 DBG("Using run_as_noclone");
329 return run_as_noclone(cmd
, data
, uid
, gid
);
334 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
336 struct run_as_mkdir_data data
;
338 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
339 path
, mode
, uid
, gid
);
342 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
346 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
348 struct run_as_mkdir_data data
;
350 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
351 path
, mode
, uid
, gid
);
354 return run_as(_mkdir
, &data
, uid
, gid
);
358 * Note: open_run_as is currently not working. We'd need to pass the fd
359 * opened in the child to the parent.
362 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
364 struct run_as_open_data data
;
366 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
367 path
, flags
, mode
, uid
, gid
);
371 return run_as(_open
, &data
, uid
, gid
);
375 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
377 struct run_as_unlink_data data
;
379 DBG3("unlink() %s with for uid %d and gid %d",
382 return run_as(_unlink
, &data
, uid
, gid
);
This page took 0.046812 seconds and 4 git commands to generate.