block uid/gid changes during posix_spawn
[musl] / src / process / posix_spawn.c
1 #include <spawn.h>
2 #include <unistd.h>
3 #include <signal.h>
4 #include <stdint.h>
5 #include <fcntl.h>
6 #include "syscall.h"
7 #include "fdop.h"
8 #include "libc.h"
9
10 extern char **environ;
11
12 static void dummy_0()
13 {
14 }
15 weak_alias(dummy_0, __acquire_ptc);
16 weak_alias(dummy_0, __release_ptc);
17
18 pid_t __vfork(void);
19
20 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
21         int (*exec)(const char *, char *const *),
22         const posix_spawn_file_actions_t *fa,
23         const posix_spawnattr_t *restrict attr,
24         char *const argv[restrict], char *const envp[restrict])
25 {
26         pid_t pid;
27         sigset_t oldmask;
28         int i;
29         posix_spawnattr_t dummy_attr = { 0 };
30
31         if (!attr) attr = &dummy_attr;
32
33         sigprocmask(SIG_BLOCK, (void *)(uint64_t []){-1}, &oldmask);
34
35         __acquire_ptc();
36         pid = __vfork();
37         __release_ptc();
38
39         if (pid) {
40                 sigprocmask(SIG_SETMASK, &oldmask, 0);
41                 if (pid < 0) return -pid;
42                 *res = pid;
43                 return 0;
44         }
45
46         for (i=1; i<=64; i++) {
47                 struct sigaction sa;
48                 sigaction(i, 0, &sa);
49                 if (sa.sa_handler!=SIG_IGN ||
50                     ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
51                      && sigismember(&attr->__def, i) )) {
52                         sa.sa_handler = SIG_DFL;
53                         sigaction(i, &sa, 0);
54                 }
55         }
56
57         if ((attr->__flags&POSIX_SPAWN_SETPGROUP) && setpgid(0, attr->__pgrp))
58                 _exit(127);
59
60         /* Use syscalls directly because pthread state is not consistent
61          * for making calls to the library wrappers... */
62         if ((attr->__flags&POSIX_SPAWN_RESETIDS) && (
63                 __syscall(SYS_setgid, __syscall(SYS_getgid)) ||
64                 __syscall(SYS_setuid, __syscall(SYS_getuid)) ))
65                 _exit(127);
66
67         if (fa && fa->__actions) {
68                 struct fdop *op;
69                 int ret, fd;
70                 for (op = fa->__actions; op->next; op = op->next);
71                 for (; op; op = op->prev) {
72                         switch(op->cmd) {
73                         case FDOP_CLOSE:
74                                 ret = __syscall(SYS_close, op->fd);
75                                 break;
76                         case FDOP_DUP2:
77                                 ret = __syscall(SYS_dup2, op->fd, op->newfd)<0;
78                                 break;
79                         case FDOP_OPEN:
80                                 fd = __syscall(SYS_open, op->path,
81                                         op->oflag | O_LARGEFILE, op->mode);
82                                 if (fd == op->fd) {
83                                         ret = 0;
84                                 } else {
85                                         ret = __syscall(SYS_dup2, fd, op->fd)<0;
86                                         __syscall(SYS_close, fd);
87                                 }
88                                 break;
89                         }
90                         if (ret) _exit(127);
91                 }
92         }
93
94         sigprocmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
95                 ? &attr->__mask : &oldmask, 0);
96
97         if (envp) environ = (char **)envp;
98         exec(path, argv);
99         _exit(127);
100
101         return 0;
102 }
103
104 int posix_spawn(pid_t *restrict res, const char *restrict path,
105         const posix_spawn_file_actions_t *fa,
106         const posix_spawnattr_t *restrict attr,
107         char *const argv[restrict], char *const envp[restrict])
108 {
109         return __posix_spawnx(res, path, execv, fa, attr, argv, envp);
110 }