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 <errno.h>
8 #include <sys/wait.h>
9 #include "syscall.h"
10 #include "lock.h"
11 #include "pthread_impl.h"
12 #include "fdop.h"
13 #include <unsupported_api.h>
14
15 struct args {
16 int p[2];
17 sigset_t oldmask;
18 const char *path;
19 const posix_spawn_file_actions_t *fa;
20 const posix_spawnattr_t *restrict attr;
21 char *const *argv, *const *envp;
22 };
23
__sys_dup2(int old,int new)24 static int __sys_dup2(int old, int new)
25 {
26 #ifdef SYS_dup2
27 return __syscall(SYS_dup2, old, new);
28 #else
29 return __syscall(SYS_dup3, old, new, 0);
30 #endif
31 }
32
33 #ifdef ENABLE_HWASAN
child(void * args_vp)34 __attribute__((no_sanitize("hwaddress"))) int child(void *args_vp)
35 #else
36 static int child(void *args_vp)
37 #endif
38 {
39 int i, ret;
40 struct sigaction sa = {0};
41 struct args *args = args_vp;
42 int p = args->p[1];
43 const posix_spawn_file_actions_t *fa = args->fa;
44 const posix_spawnattr_t *restrict attr = args->attr;
45 sigset_t hset;
46
47 close(args->p[0]);
48
49 /* All signal dispositions must be either SIG_DFL or SIG_IGN
50 * before signals are unblocked. Otherwise a signal handler
51 * from the parent might get run in the child while sharing
52 * memory, with unpredictable and dangerous results. To
53 * reduce overhead, sigaction has tracked for us which signals
54 * potentially have a signal handler. */
55 __get_handler_set(&hset);
56 for (i=1; i<_NSIG; i++) {
57 if ((attr->__flags & POSIX_SPAWN_SETSIGDEF)
58 && sigismember(&attr->__def, i)) {
59 sa.sa_handler = SIG_DFL;
60 } else if (sigismember(&hset, i)) {
61 if (i-32<3U) {
62 sa.sa_handler = SIG_IGN;
63 } else {
64 __libc_sigaction(i, 0, &sa);
65 if (sa.sa_handler==SIG_IGN) continue;
66 sa.sa_handler = SIG_DFL;
67 }
68 } else {
69 continue;
70 }
71 __libc_sigaction(i, &sa, 0);
72 }
73
74 if (attr->__flags & POSIX_SPAWN_SETSID)
75 if ((ret=__syscall(SYS_setsid)) < 0)
76 goto fail;
77
78 if (attr->__flags & POSIX_SPAWN_SETPGROUP)
79 if ((ret=__syscall(SYS_setpgid, 0, attr->__pgrp)))
80 goto fail;
81
82 /* Use syscalls directly because the library functions attempt
83 * to do a multi-threaded synchronized id-change, which would
84 * trash the parent's state. */
85 if (attr->__flags & POSIX_SPAWN_RESETIDS)
86 if ((ret=__syscall(SYS_setgid, __syscall(SYS_getgid))) ||
87 (ret=__syscall(SYS_setuid, __syscall(SYS_getuid))) )
88 goto fail;
89
90 if (fa && fa->__actions) {
91 struct fdop *op;
92 int fd;
93 for (op = fa->__actions; op->next; op = op->next);
94 for (; op; op = op->prev) {
95 /* It's possible that a file operation would clobber
96 * the pipe fd used for synchronizing with the
97 * parent. To avoid that, we dup the pipe onto
98 * an unoccupied fd. */
99 if (op->fd == p) {
100 ret = __syscall(SYS_dup, p);
101 if (ret < 0) goto fail;
102 __syscall(SYS_close, p);
103 p = ret;
104 }
105 switch(op->cmd) {
106 case FDOP_CLOSE:
107 __syscall(SYS_close, op->fd);
108 break;
109 case FDOP_DUP2:
110 fd = op->srcfd;
111 if (fd == p) {
112 ret = -EBADF;
113 goto fail;
114 }
115 if (fd != op->fd) {
116 if ((ret=__sys_dup2(fd, op->fd))<0)
117 goto fail;
118 } else {
119 ret = __syscall(SYS_fcntl, fd, F_GETFD);
120 ret = __syscall(SYS_fcntl, fd, F_SETFD,
121 ret & ~FD_CLOEXEC);
122 if (ret<0)
123 goto fail;
124 }
125 break;
126 case FDOP_OPEN:
127 fd = __sys_open(op->path, op->oflag, op->mode);
128 if ((ret=fd) < 0) goto fail;
129 if (fd != op->fd) {
130 if ((ret=__sys_dup2(fd, op->fd))<0)
131 goto fail;
132 __syscall(SYS_close, fd);
133 }
134 break;
135 case FDOP_CHDIR:
136 ret = __syscall(SYS_chdir, op->path);
137 if (ret<0) goto fail;
138 break;
139 case FDOP_FCHDIR:
140 ret = __syscall(SYS_fchdir, op->fd);
141 if (ret<0) goto fail;
142 break;
143 }
144 }
145 }
146
147 /* Close-on-exec flag may have been lost if we moved the pipe
148 * to a different fd. We don't use F_DUPFD_CLOEXEC above because
149 * it would fail on older kernels and atomicity is not needed --
150 * in this process there are no threads or signal handlers. */
151 __syscall(SYS_fcntl, p, F_SETFD, FD_CLOEXEC);
152
153 pthread_sigmask(SIG_SETMASK, (attr->__flags & POSIX_SPAWN_SETSIGMASK)
154 ? &attr->__mask : &args->oldmask, 0);
155
156 int (*exec)(const char *, char *const *, char *const *) =
157 attr->__fn ? (int (*)())attr->__fn : execve;
158
159 exec(args->path, args->argv, args->envp);
160 ret = -errno;
161
162 fail:
163 /* Since sizeof errno < PIPE_BUF, the write is atomic. */
164 ret = -ret;
165 if (ret) {
166 int r;
167 do r = __syscall(SYS_write, p, &ret, sizeof ret);
168 while (r<0 && r!=-EPIPE);
169 }
170 _exit(127);
171 }
172
173 #ifdef ENABLE_HWASAN
174 __attribute__((no_sanitize("hwaddress")))
175 #endif
posix_spawn(pid_t * restrict res,const char * restrict path,const posix_spawn_file_actions_t * fa,const posix_spawnattr_t * restrict attr,char * const argv[restrict],char * const envp[restrict])176 int posix_spawn(pid_t *restrict res, const char *restrict path,
177 const posix_spawn_file_actions_t *fa,
178 const posix_spawnattr_t *restrict attr,
179 char *const argv[restrict], char *const envp[restrict])
180 {
181 pid_t pid;
182 char stack[1024+PATH_MAX];
183 int ec=0, cs;
184 struct args args;
185
186 UNSUPPORTED_API_VOID(LITEOS_A);
187 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
188
189 args.path = path;
190 args.fa = fa;
191 args.attr = attr ? attr : &(const posix_spawnattr_t){0};
192 args.argv = argv;
193 args.envp = envp;
194 pthread_sigmask(SIG_BLOCK, SIGALL_SET, &args.oldmask);
195
196 /* The lock guards both against seeing a SIGABRT disposition change
197 * by abort and against leaking the pipe fd to fork-without-exec. */
198 LOCK(__abort_lock);
199
200 if (pipe2(args.p, O_CLOEXEC)) {
201 UNLOCK(__abort_lock);
202 ec = errno;
203 goto fail;
204 }
205
206 pid = __clone(child, stack+sizeof stack,
207 CLONE_VM|CLONE_VFORK|SIGCHLD, &args);
208 close(args.p[1]);
209 UNLOCK(__abort_lock);
210
211 if (pid > 0) {
212 if (read(args.p[0], &ec, sizeof ec) != sizeof ec) ec = 0;
213 else waitpid(pid, &(int){0}, 0);
214 } else {
215 ec = -pid;
216 }
217
218 close(args.p[0]);
219
220 if (!ec && res) *res = pid;
221
222 fail:
223 pthread_sigmask(SIG_SETMASK, &args.oldmask, 0);
224 pthread_setcancelstate(cs, 0);
225
226 return ec;
227 }
228