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 <sys/prctl.h>
36 #include <common/common.h>
37 #include <common/utils.h>
38 #include <common/compat/getenv.h>
39 #include <common/sessiond-comm/unix.h>
44 typedef int (*run_as_fct
)(struct run_as_data
*data
);
46 struct run_as_mkdir_data
{
51 struct run_as_open_data
{
57 struct run_as_unlink_data
{
61 struct run_as_rmdir_recursive_data
{
69 RUN_AS_RMDIR_RECURSIVE
,
70 RUN_AS_MKDIR_RECURSIVE
,
76 struct run_as_mkdir_data mkdir
;
77 struct run_as_open_data open
;
78 struct run_as_unlink_data unlink
;
79 struct run_as_rmdir_recursive_data rmdir_recursive
;
90 struct run_as_worker
{
91 pid_t pid
; /* Worker PID. */
96 /* Single global worker per process (for now). */
97 static struct run_as_worker
*global_worker
;
98 /* Lock protecting the worker. */
99 static pthread_mutex_t worker_lock
= PTHREAD_MUTEX_INITIALIZER
;
111 return !lttng_secure_getenv("LTTNG_DEBUG_NOCLONE");
116 int _utils_mkdir_recursive_unsafe(const char *path
, mode_t mode
);
119 * Create recursively directory using the FULL path.
122 int _mkdir_recursive(struct run_as_data
*data
)
127 path
= data
->u
.mkdir
.path
;
128 mode
= data
->u
.mkdir
.mode
;
130 /* Safe to call as we have transitioned to the requested uid/gid. */
131 return _utils_mkdir_recursive_unsafe(path
, mode
);
135 int _mkdir(struct run_as_data
*data
)
137 return mkdir(data
->u
.mkdir
.path
, data
->u
.mkdir
.mode
);
141 int _open(struct run_as_data
*data
)
143 return open(data
->u
.open
.path
, data
->u
.open
.flags
, data
->u
.open
.mode
);
147 int _unlink(struct run_as_data
*data
)
149 return unlink(data
->u
.unlink
.path
);
153 int _rmdir_recursive(struct run_as_data
*data
)
155 return utils_recursive_rmdir(data
->u
.rmdir_recursive
.path
);
159 run_as_fct
run_as_enum_to_fct(enum run_as_cmd cmd
)
168 case RUN_AS_RMDIR_RECURSIVE
:
169 return _rmdir_recursive
;
170 case RUN_AS_MKDIR_RECURSIVE
:
171 return _mkdir_recursive
;
173 ERR("Unknown command %d", (int) cmd
);
179 int do_send_fd(struct run_as_worker
*worker
,
180 enum run_as_cmd cmd
, int fd
)
193 len
= lttcomm_send_fds_unix_sock(worker
->sockpair
[1], &fd
, 1);
195 PERROR("lttcomm_send_fds_unix_sock");
206 int do_recv_fd(struct run_as_worker
*worker
,
207 enum run_as_cmd cmd
, int *fd
)
220 len
= lttcomm_recv_fds_unix_sock(worker
->sockpair
[0], fd
, 1);
223 } else if (len
< 0) {
224 PERROR("lttcomm_recv_fds_unix_sock");
231 * Return < 0 on error, 0 if OK, 1 on hangup.
234 int handle_one_cmd(struct run_as_worker
*worker
)
237 struct run_as_data data
;
238 ssize_t readlen
, writelen
;
239 struct run_as_ret sendret
;
244 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[1], &data
,
251 if (readlen
< sizeof(data
)) {
252 PERROR("lttcomm_recv_unix_sock error");
257 cmd
= run_as_enum_to_fct(data
.cmd
);
263 prev_euid
= getuid();
264 if (data
.gid
!= getegid()) {
265 ret
= setegid(data
.gid
);
271 if (data
.uid
!= prev_euid
) {
272 ret
= seteuid(data
.uid
);
279 * Also set umask to 0 for mkdir executable bit.
286 sendret
._errno
= errno
;
287 /* send back return value */
288 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
290 if (writelen
< sizeof(sendret
)) {
291 PERROR("lttcomm_send_unix_sock error");
295 ret
= do_send_fd(worker
, data
.cmd
, ret
);
297 PERROR("do_send_fd error");
301 if (seteuid(prev_euid
) < 0) {
312 int run_as_worker(struct run_as_worker
*worker
)
316 struct run_as_ret sendret
;
317 size_t proc_orig_len
;
320 * Initialize worker. Set a different process cmdline.
322 proc_orig_len
= strlen(worker
->procname
);
323 memset(worker
->procname
, 0, proc_orig_len
);
324 strncpy(worker
->procname
, DEFAULT_RUN_AS_WORKER_NAME
, proc_orig_len
);
326 ret
= prctl(PR_SET_NAME
, DEFAULT_RUN_AS_WORKER_NAME
, 0, 0, 0);
328 /* Don't fail as this is not essential. */
329 PERROR("prctl PR_SET_NAME");
335 writelen
= lttcomm_send_unix_sock(worker
->sockpair
[1], &sendret
,
337 if (writelen
< sizeof(sendret
)) {
338 PERROR("lttcomm_send_unix_sock error");
344 ret
= handle_one_cmd(worker
);
348 } else if (ret
> 0) {
351 continue; /* Next command. */
360 int run_as_cmd(struct run_as_worker
*worker
,
362 struct run_as_data
*data
,
363 uid_t uid
, gid_t gid
)
365 ssize_t readlen
, writelen
;
366 struct run_as_ret recvret
;
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 errno
= recvret
._errno
;
418 * This is for debugging ONLY and should not be considered secure.
421 int run_as_noworker(enum run_as_cmd cmd
,
422 struct run_as_data
*data
, uid_t uid
, gid_t gid
)
424 int ret
, saved_errno
;
428 fct
= run_as_enum_to_fct(cmd
);
444 int run_as(enum run_as_cmd cmd
, struct run_as_data
*data
, uid_t uid
, gid_t gid
)
449 DBG("Using run_as worker");
450 pthread_mutex_lock(&worker_lock
);
451 assert(global_worker
);
452 ret
= run_as_cmd(global_worker
, cmd
, data
, uid
, gid
);
453 pthread_mutex_unlock(&worker_lock
);
456 DBG("Using run_as without worker");
457 ret
= run_as_noworker(cmd
, data
, uid
, gid
);
463 int run_as_mkdir_recursive(const char *path
, mode_t mode
, uid_t uid
, gid_t gid
)
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(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_data data
;
480 DBG3("mkdir() %s with mode %d for uid %d and gid %d",
481 path
, mode
, uid
, gid
);
482 strncpy(data
.u
.mkdir
.path
, path
, PATH_MAX
- 1);
483 data
.u
.mkdir
.path
[PATH_MAX
- 1] = '\0';
484 data
.u
.mkdir
.mode
= mode
;
485 return run_as(RUN_AS_MKDIR
, &data
, uid
, gid
);
489 * Note: open_run_as is currently not working. We'd need to pass the fd
490 * opened in the child to the parent.
493 int run_as_open(const char *path
, int flags
, mode_t mode
, uid_t uid
, gid_t gid
)
495 struct run_as_data data
;
497 DBG3("open() %s with flags %X mode %d for uid %d and gid %d",
498 path
, flags
, mode
, uid
, gid
);
499 strncpy(data
.u
.open
.path
, path
, PATH_MAX
- 1);
500 data
.u
.open
.path
[PATH_MAX
- 1] = '\0';
501 data
.u
.open
.flags
= flags
;
502 data
.u
.open
.mode
= mode
;
503 return run_as(RUN_AS_OPEN
, &data
, uid
, gid
);
507 int run_as_unlink(const char *path
, uid_t uid
, gid_t gid
)
509 struct run_as_data data
;
511 DBG3("unlink() %s with for uid %d and gid %d",
513 strncpy(data
.u
.unlink
.path
, path
, PATH_MAX
- 1);
514 data
.u
.unlink
.path
[PATH_MAX
- 1] = '\0';
515 return run_as(RUN_AS_UNLINK
, &data
, uid
, gid
);
519 int run_as_rmdir_recursive(const char *path
, uid_t uid
, gid_t gid
)
521 struct run_as_data data
;
523 DBG3("rmdir_recursive() %s with for uid %d and gid %d",
525 strncpy(data
.u
.rmdir_recursive
.path
, path
, PATH_MAX
- 1);
526 data
.u
.rmdir_recursive
.path
[PATH_MAX
- 1] = '\0';
527 return run_as(RUN_AS_RMDIR_RECURSIVE
, &data
, uid
, gid
);
531 void reset_sighandler(void)
535 DBG("Resetting run_as worker signal handlers to default");
536 for (sig
= 1; sig
<= 31; sig
++) {
537 (void) signal(sig
, SIG_DFL
);
542 void worker_sighandler(int sig
)
547 * The worker will its parent's signals since they are part of the same
548 * process group. However, in the case of SIGINT and SIGTERM, we want
549 * to give the worker a chance to teardown gracefully when its parent
550 * closes the command socket.
563 DBG("run_as worker received signal %s", signame
);
567 int set_worker_sighandlers(void)
573 if ((ret
= sigemptyset(&sigset
)) < 0) {
574 PERROR("sigemptyset");
578 sa
.sa_handler
= worker_sighandler
;
581 if ((ret
= sigaction(SIGINT
, &sa
, NULL
)) < 0) {
582 PERROR("sigaction SIGINT");
586 if ((ret
= sigaction(SIGTERM
, &sa
, NULL
)) < 0) {
587 PERROR("sigaction SIGTERM");
591 DBG("run_as signal handler set for SIGTERM and SIGINT");
597 int run_as_create_worker(char *procname
)
602 struct run_as_ret recvret
;
603 struct run_as_worker
*worker
;
605 pthread_mutex_lock(&worker_lock
);
606 assert(!global_worker
);
609 * Don't initialize a worker, all run_as tasks will be performed
610 * in the current process.
615 worker
= zmalloc(sizeof(*worker
));
620 worker
->procname
= procname
;
621 /* Create unix socket. */
622 if (lttcomm_create_anon_unix_socketpair(worker
->sockpair
) < 0) {
632 } else if (pid
== 0) {
637 set_worker_sighandlers();
639 /* The child has no use for this lock. */
640 pthread_mutex_unlock(&worker_lock
);
641 /* Just close, no shutdown. */
642 if (close(worker
->sockpair
[0])) {
646 worker
->sockpair
[0] = -1;
647 ret
= run_as_worker(worker
);
648 if (lttcomm_close_unix_sock(worker
->sockpair
[1])) {
652 worker
->sockpair
[1] = -1;
653 exit(ret
? EXIT_FAILURE
: EXIT_SUCCESS
);
657 /* Just close, no shutdown. */
658 if (close(worker
->sockpair
[1])) {
663 worker
->sockpair
[1] = -1;
665 /* Wait for worker to become ready. */
666 readlen
= lttcomm_recv_unix_sock(worker
->sockpair
[0],
667 &recvret
, sizeof(recvret
));
668 if (readlen
< sizeof(recvret
)) {
669 ERR("readlen: %zd", readlen
);
670 PERROR("Error reading response from run_as at creation");
674 global_worker
= worker
;
677 pthread_mutex_unlock(&worker_lock
);
680 /* Error handling. */
682 for (i
= 0; i
< 2; i
++) {
683 if (worker
->sockpair
[i
] < 0) {
686 if (lttcomm_close_unix_sock(worker
->sockpair
[i
])) {
689 worker
->sockpair
[i
] = -1;
693 pthread_mutex_unlock(&worker_lock
);
698 void run_as_destroy_worker(void)
700 struct run_as_worker
*worker
= global_worker
;
702 pthread_mutex_lock(&worker_lock
);
706 /* Close unix socket */
707 if (lttcomm_close_unix_sock(worker
->sockpair
[0])) {
710 worker
->sockpair
[0] = -1;
711 /* Wait for worker. */
716 wait_ret
= waitpid(worker
->pid
, &status
, 0);
718 if (errno
== EINTR
) {
725 if (WIFEXITED(status
)) {
726 LOG(WEXITSTATUS(status
) == 0 ? PRINT_DBG
: PRINT_ERR
,
727 DEFAULT_RUN_AS_WORKER_NAME
" terminated with status code %d",
728 WEXITSTATUS(status
));
730 } else if (WIFSIGNALED(status
)) {
731 ERR(DEFAULT_RUN_AS_WORKER_NAME
" was killed by signal %d",
737 global_worker
= NULL
;
739 pthread_mutex_unlock(&worker_lock
);