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/getenv.h>
37 #include <common/sessiond-comm/unix.h>
42 typedef int (*run_as_fct
)(struct run_as_data
*data
);
44 struct run_as_mkdir_data
{
49 struct run_as_open_data
{
55 struct run_as_unlink_data
{
59 struct run_as_rmdir_recursive_data
{
67 RUN_AS_RMDIR_RECURSIVE
,
68 RUN_AS_MKDIR_RECURSIVE
,
74 struct run_as_mkdir_data mkdir
;
75 struct run_as_open_data open
;
76 struct run_as_unlink_data unlink
;
77 struct run_as_rmdir_recursive_data rmdir_recursive
;
88 struct run_as_worker
{
89 pid_t pid
; /* Worker PID. */
94 /* Single global worker per process (for now). */
95 static struct run_as_worker
*global_worker
;
96 /* Lock protecting the worker. */
97 static pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
109 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
114 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
117 * Create recursively directory using the FULL path.
120 int _mkdir_recursive(struct run_as_data
*data
)
125 path
= data
->u
.mkdir
.path
;
126 mode
= data
->u
.mkdir
.mode
;
128 /* Safe to call as we have transitioned to the requested uid/gid. */
129 return _utils_mkdir_recursive_unsafe(path
, mode
);
133 int _mkdir(struct run_as_data
*data
)
135 return mkdir(data
->u
.mkdir
.path
, data
->u
.mkdir
.mode
);
139 int _open(struct run_as_data
*data
)
141 return open(data
->u
.open
.path
, data
->u
.open
.flags
, data
->u
.open
.mode
);
145 int _unlink(struct run_as_data
*data
)
147 return unlink(data
->u
.unlink
.path
);
151 int _rmdir_recursive(struct run_as_data
*data
)
153 return utils_recursive_rmdir(data
->u
.rmdir_recursive
.path
);
157 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
166 case RUN_AS_RMDIR_RECURSIVE
:
167 return _rmdir_recursive
;
168 case RUN_AS_MKDIR_RECURSIVE
:
169 return _mkdir_recursive
;
171 ERR("Unknown command %d", (int) cmd
)
177 int do_send_fd(struct run_as_worker
*worker
,
178 enum run_as_cmd cmd
, int fd
)
191 len
= lttcomm_send_fds_unix_sock(worker
->sockpair
[1], &fd
, 1);
193 PERROR("lttcomm_send_fds_unix_sock");
204 int do_recv_fd(struct run_as_worker
*worker
,
205 enum run_as_cmd cmd
, int *fd
)
218 len
= lttcomm_recv_fds_unix_sock(worker
->sockpair
[0], fd
, 1);
221 } else if (len
< 0) {
222 PERROR("lttcomm_recv_fds_unix_sock");
229 * Return < 0 on error, 0 if OK, 1 on hangup.
232 int handle_one_cmd(struct run_as_worker
*worker
)
235 struct run_as_data data
;
236 ssize_t readlen
, writelen
;
237 struct run_as_ret sendret
;
242 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
249 if (readlen
< sizeof(data
)) {
250 PERROR("lttcomm_recv_unix_sock error");
255 cmd
= run_as_enum_to_fct(data
.cmd
);
261 prev_euid
= getuid();
262 if (data
.gid
!= getegid()) {
263 ret
= setegid(data
.gid
);
269 if (data
.uid
!= prev_euid
) {
270 ret
= seteuid(data
.uid
);
277 * Also set umask to 0 for mkdir executable bit.
284 sendret
._errno
= errno
;
285 /* send back return value */
286 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
288 if (writelen
< sizeof(sendret
)) {
289 PERROR("lttcomm_send_unix_sock error");
293 ret
= do_send_fd(worker
, data
.cmd
, ret
);
295 PERROR("do_send_fd error");
299 if (seteuid(prev_euid
) < 0) {
310 int run_as_worker(struct run_as_worker
*worker
)
314 struct run_as_ret sendret
;
315 size_t proc_orig_len
;
318 * Initialize worker. Set a different process cmdline.
320 proc_orig_len
= strlen(worker
->procname
);
321 memset(worker
->procname
, 0, proc_orig_len
);
322 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
324 ret
= pthread_setname_np(pthread_self(), DEFAULT_RUN_AS_WORKER_NAME
);
328 PERROR("pthread_setname_np");
334 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
336 if (writelen
< sizeof(sendret
)) {
337 PERROR("lttcomm_send_unix_sock error");
343 ret
= handle_one_cmd(worker
);
347 } else if (ret
> 0) {
350 continue; /* Next command. */
359 int run_as_cmd(struct run_as_worker
*worker
,
361 struct run_as_data
*data
,
362 uid_t uid
, gid_t gid
)
364 ssize_t readlen
, writelen
;
365 struct run_as_ret recvret
;
367 pthread_mutex_lock(&worker_lock
);
369 * If we are non-root, we can only deal with our own uid.
371 if (geteuid() != 0) {
372 if (uid
!= geteuid()) {
374 recvret
._errno
= EPERM
;
375 ERR("Client (%d)/Server (%d) UID mismatch (and sessiond is not root)",
385 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[0], data
,
387 if (writelen
< sizeof(*data
)) {
388 PERROR("Error writing message to run_as");
390 recvret
._errno
= errno
;
394 /* receive return value */
395 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0], &recvret
,
398 ERR("Run-as worker has hung-up during run_as_cmd");
400 recvret
._errno
= EIO
;
402 } else if (readlen
< sizeof(recvret
)) {
403 PERROR("Error reading response from run_as");
405 recvret
._errno
= errno
;
407 if (do_recv_fd(worker
, cmd
, &recvret
.ret
)) {
409 recvret
._errno
= EIO
;
413 pthread_mutex_unlock(&worker_lock
);
414 errno
= recvret
._errno
;
419 * This is for debugging ONLY and should not be considered secure.
422 int run_as_noworker(enum run_as_cmd cmd
,
423 struct run_as_data
*data
, uid_t uid
, gid_t gid
)
425 int ret
, saved_errno
;
429 fct
= run_as_enum_to_fct(cmd
);
445 int run_as(struct run_as_worker
*worker
,
447 struct run_as_data
*data
, uid_t uid
, gid_t gid
)
452 DBG("Using run_as worker");
453 ret
= run_as_cmd(worker
, cmd
, data
, uid
, gid
);
455 DBG("Using run_as without worker");
456 ret
= run_as_noworker(cmd
, data
, uid
, gid
);
462 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
464 struct run_as_worker
*worker
= global_worker
;
465 struct run_as_data data
;
467 DBG3("mkdir() recursive %s with mode %d for uid %d and gid %d",
468 path
, mode
, uid
, gid
);
469 strncpy(data
.u
.mkdir
.path
, path
, PATH_MAX
- 1);
470 data
.u
.mkdir
.path
[PATH_MAX
- 1] = '\0';
471 data
.u
.mkdir
.mode
= mode
;
472 return run_as(worker
, RUN_AS_MKDIR_RECURSIVE
, &data
, uid
, gid
);
476 int run_as_mkdir(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
478 struct run_as_worker
*worker
= global_worker
;
479 struct run_as_data data
;
481 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
482 path
, mode
, uid
, gid
);
483 strncpy(data
.u
.mkdir
.path
, path
, PATH_MAX
- 1);
484 data
.u
.mkdir
.path
[PATH_MAX
- 1] = '\0';
485 data
.u
.mkdir
.mode
= mode
;
486 return run_as(worker
, RUN_AS_MKDIR
, &data
, uid
, gid
);
490 * Note: open_run_as is currently not working. We'd need to pass the fd
491 * opened in the child to the parent.
494 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
496 struct run_as_worker
*worker
= global_worker
;
497 struct run_as_data data
;
499 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
500 path
, flags
, mode
, uid
, gid
);
501 strncpy(data
.u
.open
.path
, path
, PATH_MAX
- 1);
502 data
.u
.open
.path
[PATH_MAX
- 1] = '\0';
503 data
.u
.open
.flags
= flags
;
504 data
.u
.open
.mode
= mode
;
505 return run_as(worker
, RUN_AS_OPEN
, &data
, uid
, gid
);
509 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
511 struct run_as_worker
*worker
= global_worker
;
512 struct run_as_data data
;
514 DBG3("unlink() %s with for uid %d and gid %d",
516 strncpy(data
.u
.unlink
.path
, path
, PATH_MAX
- 1);
517 data
.u
.unlink
.path
[PATH_MAX
- 1] = '\0';
518 return run_as(worker
, RUN_AS_UNLINK
, &data
, uid
, gid
);
522 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
)
524 struct run_as_worker
*worker
= global_worker
;
525 struct run_as_data data
;
527 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
529 strncpy(data
.u
.rmdir_recursive
.path
, path
, PATH_MAX
- 1);
530 data
.u
.rmdir_recursive
.path
[PATH_MAX
- 1] = '\0';
531 return run_as(worker
, RUN_AS_RMDIR_RECURSIVE
, &data
, uid
, gid
);
535 int run_as_create_worker(char *procname
)
540 struct run_as_ret recvret
;
541 struct run_as_worker
*worker
;
547 worker
= zmalloc(sizeof(*worker
));
552 worker
->procname
= procname
;
553 /* Create unix socket. */
554 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
564 } else if (pid
== 0) {
567 /* Just close, no shutdown. */
568 if (close(worker
->sockpair
[0])) {
572 worker
->sockpair
[0] = -1;
573 ret
= run_as_worker(worker
);
574 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
578 worker
->sockpair
[1] = -1;
579 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
583 /* Just close, no shutdown. */
584 if (close(worker
->sockpair
[1])) {
589 worker
->sockpair
[1] = -1;
591 /* Wait for worker to become ready. */
592 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
593 &recvret
, sizeof(recvret
));
594 if (readlen
< sizeof(recvret
)) {
595 ERR("readlen: %zd", readlen
);
596 PERROR("Error reading response from run_as at creation");
600 global_worker
= worker
;
605 /* Error handling. */
607 for (i
= 0; i
< 2; i
++) {
608 if (worker
->sockpair
[i
] < 0) {
611 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
614 worker
->sockpair
[i
] = -1;
622 void run_as_destroy_worker(void)
624 struct run_as_worker
*worker
= global_worker
;
631 /* Close unix socket */
632 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
635 worker
->sockpair
[0] = -1;
636 /* Wait for worker. */
637 pid
= waitpid(worker
->pid
, &status
, 0);
638 if (pid
< 0 || !WIFEXITED(status
) || WEXITSTATUS(status
) != 0) {
642 global_worker
= NULL
;