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