1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <spawn.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <signal.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <unistd.h>
37 
38 #include <android/fdsan.h>
39 
40 #include "private/ScopedSignalBlocker.h"
41 #include "private/SigSetConverter.h"
42 
43 enum Action {
44   kOpen,
45   kClose,
46   kDup2
47 };
48 
49 struct __posix_spawn_file_action {
50   __posix_spawn_file_action* next;
51 
52   Action what;
53   int fd;
54   int new_fd;
55   char* path;
56   int flags;
57   mode_t mode;
58 
Do__posix_spawn_file_action59   void Do() {
60     if (what == kOpen) {
61       fd = open(path, flags, mode);
62       if (fd == -1) _exit(127);
63       // If it didn't land where we wanted it, move it.
64       if (fd != new_fd) {
65         if (dup2(fd, new_fd) == -1) _exit(127);
66         close(fd);
67       }
68     } else if (what == kClose) {
69       // Failure to close is ignored.
70       close(fd);
71     } else {
72       if (dup2(fd, new_fd) == -1) _exit(127);
73     }
74   }
75 };
76 
77 struct __posix_spawn_file_actions {
78   __posix_spawn_file_action* head;
79   __posix_spawn_file_action* last;
80 
Do__posix_spawn_file_actions81   void Do() {
82     for (__posix_spawn_file_action* action = head; action != nullptr; action = action->next) {
83       action->Do();
84     }
85   }
86 };
87 
88 struct __posix_spawnattr {
89   short flags;
90   pid_t pgroup;
91   sched_param schedparam;
92   int schedpolicy;
93   SigSetConverter sigmask;
94   SigSetConverter sigdefault;
95 };
96 
ApplyAttrs(short flags,const posix_spawnattr_t * attr)97 static void ApplyAttrs(short flags, const posix_spawnattr_t* attr) {
98   // POSIX: "If POSIX_SPAWN_SETSIGDEF is set ... signals in sigdefault ...
99   // shall be set to their default actions in the child process."
100   // POSIX: "Signals set to be caught by the calling process shall be
101   // set to the default action in the child process."
102   bool use_sigdefault = ((flags & POSIX_SPAWN_SETSIGDEF) != 0);
103   const struct sigaction64 default_sa = { .sa_handler = SIG_DFL };
104   for (int s = 1; s < _NSIG; ++s) {
105     bool reset = false;
106     if (use_sigdefault && sigismember64(&(*attr)->sigdefault.sigset64, s)) {
107       reset = true;
108     } else {
109       struct sigaction64 current;
110       if (sigaction64(s, nullptr, &current) == -1) _exit(127);
111       reset = (current.sa_handler != SIG_IGN && current.sa_handler != SIG_DFL);
112     }
113     if (reset && sigaction64(s, &default_sa, nullptr) == -1) _exit(127);
114   }
115 
116   if ((flags & POSIX_SPAWN_SETPGROUP) != 0 && setpgid(0, (*attr)->pgroup) == -1) _exit(127);
117   if ((flags & POSIX_SPAWN_SETSID) != 0 && setsid() == -1) _exit(127);
118 
119   // POSIX_SPAWN_SETSCHEDULER overrides POSIX_SPAWN_SETSCHEDPARAM, but it is not an error
120   // to set both.
121   if ((flags & POSIX_SPAWN_SETSCHEDULER) != 0) {
122     if (sched_setscheduler(0, (*attr)->schedpolicy, &(*attr)->schedparam) == -1) _exit(127);
123   } else if ((flags & POSIX_SPAWN_SETSCHEDPARAM) != 0) {
124     if (sched_setparam(0, &(*attr)->schedparam) == -1) _exit(127);
125   }
126 
127   if ((flags & POSIX_SPAWN_RESETIDS) != 0) {
128     if (seteuid(getuid()) == -1 || setegid(getgid()) == -1) _exit(127);
129   }
130 
131   if ((flags & POSIX_SPAWN_SETSIGMASK) != 0) {
132     if (sigprocmask64(SIG_SETMASK, &(*attr)->sigmask.sigset64, nullptr)) _exit(127);
133   }
134 }
135 
posix_spawn(pid_t * pid_ptr,const char * path,const posix_spawn_file_actions_t * actions,const posix_spawnattr_t * attr,char * const argv[],char * const env[],int exec_fn (const char * path,char * const argv[],char * const env[]))136 static int posix_spawn(pid_t* pid_ptr,
137                        const char* path,
138                        const posix_spawn_file_actions_t* actions,
139                        const posix_spawnattr_t* attr,
140                        char* const argv[],
141                        char* const env[],
142                        int exec_fn(const char* path, char* const argv[], char* const env[])) {
143   // See http://man7.org/linux/man-pages/man3/posix_spawn.3.html
144   // and http://pubs.opengroup.org/onlinepubs/9699919799/functions/posix_spawn.html
145 
146   ScopedSignalBlocker ssb;
147 
148   short flags = attr ? (*attr)->flags : 0;
149   bool use_vfork = ((flags & POSIX_SPAWN_USEVFORK) != 0) || (actions == nullptr && flags == 0);
150 
151   pid_t pid = use_vfork ? vfork() : fork();
152   if (pid == -1) return errno;
153 
154   if (pid == 0) {
155     // Child.
156     ApplyAttrs(flags, attr);
157     if (actions) (*actions)->Do();
158     if ((flags & POSIX_SPAWN_SETSIGMASK) == 0) ssb.reset();
159     exec_fn(path, argv, env ? env : environ);
160     _exit(127);
161   }
162 
163   // Parent.
164   if (pid_ptr) *pid_ptr = pid;
165   return 0;
166 }
167 
posix_spawn(pid_t * pid,const char * path,const posix_spawn_file_actions_t * actions,const posix_spawnattr_t * attr,char * const argv[],char * const env[])168 int posix_spawn(pid_t* pid, const char* path, const posix_spawn_file_actions_t* actions,
169                 const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
170   return posix_spawn(pid, path, actions, attr, argv, env, execve);
171 }
172 
posix_spawnp(pid_t * pid,const char * file,const posix_spawn_file_actions_t * actions,const posix_spawnattr_t * attr,char * const argv[],char * const env[])173 int posix_spawnp(pid_t* pid, const char* file, const posix_spawn_file_actions_t* actions,
174                  const posix_spawnattr_t* attr, char* const argv[], char* const env[]) {
175   return posix_spawn(pid, file, actions, attr, argv, env, execvpe);
176 }
177 
posix_spawnattr_init(posix_spawnattr_t * attr)178 int posix_spawnattr_init(posix_spawnattr_t* attr) {
179   *attr = reinterpret_cast<__posix_spawnattr*>(calloc(1, sizeof(__posix_spawnattr)));
180   return (*attr == nullptr) ? errno : 0;
181 }
182 
posix_spawnattr_destroy(posix_spawnattr_t * attr)183 int posix_spawnattr_destroy(posix_spawnattr_t* attr) {
184   free(*attr);
185   *attr = nullptr;
186   return 0;
187 }
188 
posix_spawnattr_setflags(posix_spawnattr_t * attr,short flags)189 int posix_spawnattr_setflags(posix_spawnattr_t* attr, short flags) {
190   if ((flags & ~(POSIX_SPAWN_RESETIDS | POSIX_SPAWN_SETPGROUP | POSIX_SPAWN_SETSIGDEF |
191                  POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSCHEDPARAM | POSIX_SPAWN_SETSCHEDULER |
192                  POSIX_SPAWN_USEVFORK | POSIX_SPAWN_SETSID)) != 0) {
193     return EINVAL;
194   }
195   (*attr)->flags = flags;
196   return 0;
197 }
198 
posix_spawnattr_getflags(const posix_spawnattr_t * attr,short * flags)199 int posix_spawnattr_getflags(const posix_spawnattr_t* attr, short* flags) {
200   *flags = (*attr)->flags;
201   return 0;
202 }
203 
posix_spawnattr_setpgroup(posix_spawnattr_t * attr,pid_t pgroup)204 int posix_spawnattr_setpgroup(posix_spawnattr_t* attr, pid_t pgroup) {
205   (*attr)->pgroup = pgroup;
206   return 0;
207 }
208 
posix_spawnattr_getpgroup(const posix_spawnattr_t * attr,pid_t * pgroup)209 int posix_spawnattr_getpgroup(const posix_spawnattr_t* attr, pid_t* pgroup) {
210   *pgroup = (*attr)->pgroup;
211   return 0;
212 }
213 
posix_spawnattr_setsigmask(posix_spawnattr_t * attr,const sigset_t * mask)214 int posix_spawnattr_setsigmask(posix_spawnattr_t* attr, const sigset_t* mask) {
215   (*attr)->sigmask.sigset = *mask;
216   return 0;
217 }
218 
posix_spawnattr_setsigmask64(posix_spawnattr_t * attr,const sigset64_t * mask)219 int posix_spawnattr_setsigmask64(posix_spawnattr_t* attr, const sigset64_t* mask) {
220   (*attr)->sigmask.sigset64 = *mask;
221   return 0;
222 }
223 
posix_spawnattr_getsigmask(const posix_spawnattr_t * attr,sigset_t * mask)224 int posix_spawnattr_getsigmask(const posix_spawnattr_t* attr, sigset_t* mask) {
225   *mask = (*attr)->sigmask.sigset;
226   return 0;
227 }
228 
posix_spawnattr_getsigmask64(const posix_spawnattr_t * attr,sigset64_t * mask)229 int posix_spawnattr_getsigmask64(const posix_spawnattr_t* attr, sigset64_t* mask) {
230   *mask = (*attr)->sigmask.sigset64;
231   return 0;
232 }
233 
posix_spawnattr_setsigdefault(posix_spawnattr_t * attr,const sigset_t * mask)234 int posix_spawnattr_setsigdefault(posix_spawnattr_t* attr, const sigset_t* mask) {
235   (*attr)->sigdefault.sigset = *mask;
236   return 0;
237 }
238 
posix_spawnattr_setsigdefault64(posix_spawnattr_t * attr,const sigset64_t * mask)239 int posix_spawnattr_setsigdefault64(posix_spawnattr_t* attr, const sigset64_t* mask) {
240   (*attr)->sigdefault.sigset64 = *mask;
241   return 0;
242 }
243 
posix_spawnattr_getsigdefault(const posix_spawnattr_t * attr,sigset_t * mask)244 int posix_spawnattr_getsigdefault(const posix_spawnattr_t* attr, sigset_t* mask) {
245   *mask = (*attr)->sigdefault.sigset;
246   return 0;
247 }
248 
posix_spawnattr_getsigdefault64(const posix_spawnattr_t * attr,sigset64_t * mask)249 int posix_spawnattr_getsigdefault64(const posix_spawnattr_t* attr, sigset64_t* mask) {
250   *mask = (*attr)->sigdefault.sigset64;
251   return 0;
252 }
253 
posix_spawnattr_setschedparam(posix_spawnattr_t * attr,const struct sched_param * param)254 int posix_spawnattr_setschedparam(posix_spawnattr_t* attr, const struct sched_param* param) {
255   (*attr)->schedparam = *param;
256   return 0;
257 }
258 
posix_spawnattr_getschedparam(const posix_spawnattr_t * attr,struct sched_param * param)259 int posix_spawnattr_getschedparam(const posix_spawnattr_t* attr, struct sched_param* param) {
260   *param = (*attr)->schedparam;
261   return 0;
262 }
263 
posix_spawnattr_setschedpolicy(posix_spawnattr_t * attr,int policy)264 int posix_spawnattr_setschedpolicy(posix_spawnattr_t* attr, int policy) {
265   (*attr)->schedpolicy = policy;
266   return 0;
267 }
268 
posix_spawnattr_getschedpolicy(const posix_spawnattr_t * attr,int * policy)269 int posix_spawnattr_getschedpolicy(const posix_spawnattr_t* attr, int* policy) {
270   *policy = (*attr)->schedpolicy;
271   return 0;
272 }
273 
posix_spawn_file_actions_init(posix_spawn_file_actions_t * actions)274 int posix_spawn_file_actions_init(posix_spawn_file_actions_t* actions) {
275   *actions = reinterpret_cast<__posix_spawn_file_actions*>(calloc(1, sizeof(**actions)));
276   return (*actions == nullptr) ? errno : 0;
277 }
278 
posix_spawn_file_actions_destroy(posix_spawn_file_actions_t * actions)279 int posix_spawn_file_actions_destroy(posix_spawn_file_actions_t* actions) {
280   __posix_spawn_file_action* a = (*actions)->head;
281   while (a) {
282     __posix_spawn_file_action* last = a;
283     a = a->next;
284     free(last->path);
285     free(last);
286   }
287   free(*actions);
288   *actions = nullptr;
289   return 0;
290 }
291 
posix_spawn_add_file_action(posix_spawn_file_actions_t * actions,Action what,int fd,int new_fd,const char * path,int flags,mode_t mode)292 static int posix_spawn_add_file_action(posix_spawn_file_actions_t* actions,
293                                        Action what,
294                                        int fd,
295                                        int new_fd,
296                                        const char* path,
297                                        int flags,
298                                        mode_t mode) {
299   __posix_spawn_file_action* action =
300       reinterpret_cast<__posix_spawn_file_action*>(malloc(sizeof(*action)));
301   if (action == nullptr) return errno;
302 
303   action->next = nullptr;
304   if (path != nullptr) {
305     action->path = strdup(path);
306     if (action->path == nullptr) {
307       free(action);
308       return errno;
309     }
310   } else {
311     action->path = nullptr;
312   }
313   action->what = what;
314   action->fd = fd;
315   action->new_fd = new_fd;
316   action->flags = flags;
317   action->mode = mode;
318 
319   if ((*actions)->head == nullptr) {
320     (*actions)->head = (*actions)->last = action;
321   } else {
322     (*actions)->last->next = action;
323     (*actions)->last = action;
324   }
325 
326   return 0;
327 }
328 
posix_spawn_file_actions_addopen(posix_spawn_file_actions_t * actions,int fd,const char * path,int flags,mode_t mode)329 int posix_spawn_file_actions_addopen(posix_spawn_file_actions_t* actions,
330                                      int fd, const char* path, int flags, mode_t mode) {
331   if (fd < 0) return EBADF;
332   return posix_spawn_add_file_action(actions, kOpen, -1, fd, path, flags, mode);
333 }
334 
posix_spawn_file_actions_addclose(posix_spawn_file_actions_t * actions,int fd)335 int posix_spawn_file_actions_addclose(posix_spawn_file_actions_t* actions, int fd) {
336   if (fd < 0) return EBADF;
337   return posix_spawn_add_file_action(actions, kClose, fd, -1, nullptr, 0, 0);
338 }
339 
posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t * actions,int fd,int new_fd)340 int posix_spawn_file_actions_adddup2(posix_spawn_file_actions_t* actions, int fd, int new_fd) {
341   if (fd < 0 || new_fd < 0) return EBADF;
342   return posix_spawn_add_file_action(actions, kDup2, fd, new_fd, nullptr, 0, 0);
343 }
344