overhaul posix_spawn to use CLONE_VM instead of vfork
[musl] / src / process / posix_spawn.c
1 #define _GNU_SOURCE
2 #include <spawn.h>
3 #include <sched.h>
4 #include <unistd.h>
5 #include <signal.h>
6 #include <fcntl.h>
7 #include <sys/wait.h>
8 #include "syscall.h"
9 #include "pthread_impl.h"
10 #include "fdop.h"
11 #include "libc.h"
12
13 static void dummy_0()
14 {
15 }
16 weak_alias(dummy_0, __acquire_ptc);
17 weak_alias(dummy_0, __release_ptc);
18
19 struct args {
20         int p[2];
21         sigset_t oldmask;
22         const char *path;
23         int (*exec)(const char *, char *const *, char *const *);
24         const posix_spawn_file_actions_t *fa;
25         const posix_spawnattr_t *restrict attr;
26         char *const *argv, *const *envp;
27 };
28
29 static int child(void *args_vp)
30 {
31         int i, ret;
32         struct sigaction sa;
33         struct args *args = args_vp;
34         int p = args->p[1];
35         const posix_spawn_file_actions_t *fa = args->fa;
36         const posix_spawnattr_t *restrict attr = args->attr;
37
38         close(args->p[0]);
39
40         /* All signal dispositions must be either SIG_DFL or SIG_IGN
41          * before signals are unblocked. Otherwise a signal handler
42          * from the parent might get run in the child while sharing
43          * memory, with unpredictable and dangerous results. */
44         for (i=1; i<_NSIG; i++) {
45                 __libc_sigaction(i, 0, &sa);
46                 if (sa.sa_handler!=SIG_DFL && (sa.sa_handler!=SIG_IGN ||
47                     ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
48                      && sigismember(&attr->__def, i) ))) {
49                         sa.sa_handler = SIG_DFL;
50                         __libc_sigaction(i, &sa, 0);
51                 }
52         }
53
54         if (attr->__flags & POSIX_SPAWN_SETPGROUP)
55                 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
56                         goto fail;
57
58         /* Use syscalls directly because pthread state because the
59          * library functions attempt to do a multi-threaded synchronized
60          * id-change, which would trash the parent's state. */
61         if (attr->__flags & POSIX_SPAWN_RESETIDS)
62                 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
63                     (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
64                         goto fail;
65
66         if (fa && fa->__actions) {
67                 struct fdop *op;
68                 int fd;
69                 for (op = fa->__actions; op->next; op = op->next);
70                 for (; op; op = op->prev) {
71                         /* It's possible that a file operation would clobber
72                          * the pipe fd used for synchronizing with the
73                          * parent. To avoid that, we dup the pipe onto
74                          * an unoccupied fd. */
75                         if (op->fd == p) {
76                                 ret = __syscall(SYS_dup, p);
77                                 if (ret < 0) goto fail;
78                                 __syscall(SYS_close, p);
79                                 p = ret;
80                         }
81                         switch(op->cmd) {
82                         case FDOP_CLOSE:
83                                 if ((ret=__syscall(SYS_close, op->fd)))
84                                         goto fail;
85                                 break;
86                         case FDOP_DUP2:
87                                 if ((ret=__syscall(SYS_dup2, op->srcfd, op->fd))<0)
88                                         goto fail;
89                                 break;
90                         case FDOP_OPEN:
91                                 fd = __syscall(SYS_open, op->path,
92                                         op->oflag | O_LARGEFILE, op->mode);
93                                 if ((ret=fd) < 0) goto fail;
94                                 if (fd != op->fd) {
95                                         if ((ret=__syscall(SYS_dup2, fd, op->fd))<0)
96                                                 goto fail;
97                                         __syscall(SYS_close, fd);
98                                 }
99                                 break;
100                         }
101                 }
102         }
103
104         /* Close-on-exec flag may have been lost if we moved the pipe
105          * to a different fd. We don't use F_DUPFD_CLOEXEC above because
106          * it would fail on older kernels and atomicity is not needed --
107          * in this process there are no threads or signal handlers. */
108         __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
109
110         pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
111                 ? &attr->__mask : &args->oldmask, 0);
112
113         args->exec(args->path, args->argv, args->envp);
114
115 fail:
116         /* Since sizeof errno < PIPE_BUF, the write is atomic. */
117         ret = -ret;
118         if (ret) while (write(p, &ret, sizeof ret) < 0);
119         _exit(127);
120 }
121
122
123 int __posix_spawnx(pid_t *restrict res, const char *restrict path,
124         int (*exec)(const char *, char *const *, char *const *),
125         const posix_spawn_file_actions_t *fa,
126         const posix_spawnattr_t *restrict attr,
127         char *const argv[restrict], char *const envp[restrict])
128 {
129         pid_t pid;
130         char stack[1024];
131         int ec=0, cs;
132         struct args args;
133
134         if (pipe2(args.p, O_CLOEXEC))
135                 return errno;
136
137         pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
138
139         args.path = path;
140         args.exec = exec;
141         args.fa = fa;
142         args.attr = attr ? attr : &(const posix_spawnattr_t){0};
143         args.argv = argv;
144         args.envp = envp;
145         pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
146
147         /* This lock prevents setuid/setgid operations while the parent
148          * is sharing memory with the child. Situations where processes
149          * with different permissions share VM are fundamentally unsafe. */
150         __acquire_ptc();
151         pid = __clone(child, stack+sizeof stack, CLONE_VM|SIGCHLD, &args);
152         close(args.p[1]);
153
154         if (pid > 0) {
155                 if (read(args.p[0], &ec, sizeof ec) < sizeof ec) ec = 0;
156                 else waitpid(pid, &(int){0}, 0);
157         } else {
158                 ec = -pid;
159         }
160
161         /* At this point, the child has either exited or successfully
162          * performed exec, so the lock may be released. */
163         __release_ptc();
164         close(args.p[0]);
165
166         if (!ec) *res = pid;
167
168         pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
169         pthread_setcancelstate(cs, 0);
170
171         return ec;
172 }
173
174 int posix_spawn(pid_t *restrict res, const char *restrict path,
175         const posix_spawn_file_actions_t *fa,
176         const posix_spawnattr_t *restrict attr,
177         char *const argv[restrict], char *const envp[restrict])
178 {
179         return __posix_spawnx(res, path, execve, fa, attr, argv, envp);
180 }