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
{
105 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
110 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
113 * Create recursively directory using the FULL path.
116 int _mkdir_recursive(void *_data
)
118 struct run_as_mkdir_data
*data
= _data
;
125 /* Safe to call as we have transitioned to the requested uid/gid. */
126 return _utils_mkdir_recursive_unsafe(path
, mode
);
130 int _mkdir(void *_data
)
132 struct run_as_mkdir_data
*data
= _data
;
134 return mkdir(data
->path
, data
->mode
);
138 int _open(void *_data
)
140 struct run_as_open_data
*data
= _data
;
142 return open(data
->path
, data
->flags
, data
->mode
);
146 int _unlink(void *_data
)
148 struct run_as_unlink_data
*data
= _data
;
150 return unlink(data
->path
);
154 int _recursive_rmdir(void *_data
)
156 struct run_as_recursive_rmdir_data
*data
= _data
;
158 return utils_recursive_rmdir(data
->path
);
162 int child_run_as(void *_data
)
165 struct run_as_data
*data
= _data
;
167 struct run_as_ret sendret
;
170 * Child: it is safe to drop egid and euid while sharing the
171 * file descriptors with the parent process, since we do not
172 * drop "uid": therefore, the user we are dropping egid/euid to
173 * cannot attach to this process with, e.g. ptrace, nor map this
176 if (data
->gid
!= getegid()) {
177 ret
= setegid(data
->gid
);
183 if (data
->uid
!= geteuid()) {
184 ret
= seteuid(data
->uid
);
191 * Also set umask to 0 for mkdir executable bit.
194 ret
= (*data
->cmd
)(data
->data
);
198 sendret
._errno
= errno
;
199 /* send back return value */
200 writelen
= lttng_write(data
->retval_pipe
, &sendret
, sizeof(sendret
));
201 if (writelen
< sizeof(sendret
)) {
202 PERROR("lttng_write error");
210 int run_as_clone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
212 struct run_as_data run_as_data
;
219 struct run_as_ret recvret
;
222 * If we are non-root, we can only deal with our own uid.
224 if (geteuid() != 0) {
225 if (uid
!= geteuid()) {
227 recvret
._errno
= EPERM
;
228 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
234 ret
= pipe(retval_pipe
);
237 recvret
._errno
= errno
;
241 run_as_data
.data
= data
;
242 run_as_data
.cmd
= cmd
;
243 run_as_data
.uid
= uid
;
244 run_as_data
.gid
= gid
;
245 run_as_data
.retval_pipe
= retval_pipe
[1]; /* write end */
246 child_stack
= mmap(NULL
, RUNAS_CHILD_STACK_SIZE
,
247 PROT_WRITE
| PROT_READ
,
248 MAP_PRIVATE
| MAP_GROWSDOWN
| MAP_ANONYMOUS
| LTTNG_MAP_STACK
,
250 if (child_stack
== MAP_FAILED
) {
252 recvret
._errno
= ENOMEM
;
257 * Pointing to the middle of the stack to support architectures
258 * where the stack grows up (HPPA).
260 pid
= lttng_clone_files(child_run_as
, child_stack
+ (RUNAS_CHILD_STACK_SIZE
/ 2),
264 recvret
._errno
= errno
;
268 /* receive return value */
269 readlen
= lttng_read(retval_pipe
[0], &recvret
, sizeof(recvret
));
270 if (readlen
< sizeof(recvret
)) {
272 recvret
._errno
= errno
;
276 * Parent: wait for child to return, in which case the
277 * shared memory map will have been created.
279 pid
= waitpid(pid
, &status
, 0);
280 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
282 recvret
._errno
= errno
;
286 ret
= munmap(child_stack
, RUNAS_CHILD_STACK_SIZE
);
289 recvret
._errno
= errno
;
293 ret
= close(retval_pipe
[0]);
296 recvret
._errno
= errno
;
299 ret
= close(retval_pipe
[1]);
302 recvret
._errno
= errno
;
306 errno
= recvret
._errno
;
311 * To be used on setups where gdb has issues debugging programs using
312 * clone/rfork. Note that this is for debuging ONLY, and should not be
316 int run_as_noclone(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
318 int ret
, saved_errno
;
331 int run_as(int (*cmd
)(void *data
), void *data
, uid_t uid
, gid_t gid
)
336 DBG("Using run_as_clone");
337 pthread_mutex_lock(<tng_libc_state_lock
);
338 ret
= run_as_clone(cmd
, data
, uid
, gid
);
339 pthread_mutex_unlock(<tng_libc_state_lock
);
342 DBG("Using run_as_noclone");
343 return run_as_noclone(cmd
, data
, uid
, gid
);
348 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
350 struct run_as_mkdir_data data
;
352 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
353 path
, mode
, uid
, gid
);
356 return run_as(_mkdir_recursive
, &data
, uid
, gid
);
360 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
362 struct run_as_mkdir_data data
;
364 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
365 path
, mode
, uid
, gid
);
368 return run_as(_mkdir
, &data
, uid
, gid
);
372 * Note: open_run_as is currently not working. We'd need to pass the fd
373 * opened in the child to the parent.
376 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
378 struct run_as_open_data data
;
380 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
381 path
, flags
, mode
, uid
, gid
);
385 return run_as(_open
, &data
, uid
, gid
);
389 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
391 struct run_as_unlink_data data
;
393 DBG3("unlink() %s with for uid %d and gid %d",
396 return run_as(_unlink
, &data
, uid
, gid
);
400 int run_as_recursive_rmdir(const char *path
, uid_t uid
, gid_t gid
)
402 struct run_as_recursive_rmdir_data data
;
404 DBG3("recursive_rmdir() %s with for uid %d and gid %d",
407 return run_as(_recursive_rmdir
, &data
, uid
, gid
);