1 /*
   2  * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #include <dirent.h>
  27 #include <errno.h>
  28 #include <fcntl.h>
  29 #include <stdlib.h>
  30 #include <string.h>
  31 #include <unistd.h>
  32 #include <limits.h>
  33 
  34 #include "childproc.h"
  35 
  36 
  37 ssize_t
  38 restartableWrite(int fd, const void *buf, size_t count)
  39 {
  40     ssize_t result;
  41     RESTARTABLE(write(fd, buf, count), result);
  42     return result;
  43 }
  44 
  45 int
  46 restartableDup2(int fd_from, int fd_to)
  47 {
  48     int err;
  49     RESTARTABLE(dup2(fd_from, fd_to), err);
  50     return err;
  51 }
  52 
  53 int
  54 closeSafely(int fd)
  55 {
  56     return (fd == -1) ? 0 : close(fd);
  57 }
  58 
  59 int
  60 isAsciiDigit(char c)
  61 {
  62   return c >= '0' && c <= '9';
  63 }
  64 
  65 #if defined(_AIX)
  66   /* AIX does not understand '/proc/self' - it requires the real process ID */
  67   #define FD_DIR aix_fd_dir
  68   #define DIR DIR64
  69   #define dirent dirent64
  70   #define opendir opendir64
  71   #define readdir readdir64
  72   #define closedir closedir64
  73 #elif defined(_ALLBSD_SOURCE)
  74   #define FD_DIR "/dev/fd"
  75 #else
  76   #define FD_DIR "/proc/self/fd"
  77 #endif
  78 
  79 int
  80 closeDescriptors(void)
  81 {
  82     DIR *dp;
  83     struct dirent *dirp;
  84     int from_fd = FAIL_FILENO + 1;
  85 
  86     /* We're trying to close all file descriptors, but opendir() might
  87      * itself be implemented using a file descriptor, and we certainly
  88      * don't want to close that while it's in use.  We assume that if
  89      * opendir() is implemented using a file descriptor, then it uses
  90      * the lowest numbered file descriptor, just like open().  So we
  91      * close a couple explicitly.  */
  92 
  93     close(from_fd);          /* for possible use by opendir() */
  94     close(from_fd + 1);      /* another one for good luck */
  95 
  96 #if defined(_AIX)
  97     /* AIX does not understand '/proc/self' - it requires the real process ID */
  98     char aix_fd_dir[32];     /* the pid has at most 19 digits */
  99     snprintf(aix_fd_dir, 32, "/proc/%d/fd", getpid());
 100 #endif
 101 
 102     if ((dp = opendir(FD_DIR)) == NULL)
 103         return 0;
 104 
 105     while ((dirp = readdir(dp)) != NULL) {
 106         int fd;
 107         if (isAsciiDigit(dirp->d_name[0]) &&
 108             (fd = strtol(dirp->d_name, NULL, 10)) >= from_fd + 2)
 109             close(fd);
 110     }
 111 
 112     closedir(dp);
 113 
 114     return 1;
 115 }
 116 
 117 int
 118 moveDescriptor(int fd_from, int fd_to)
 119 {
 120     if (fd_from != fd_to) {
 121         if ((restartableDup2(fd_from, fd_to) == -1) ||
 122             (close(fd_from) == -1))
 123             return -1;
 124     }
 125     return 0;
 126 }
 127 
 128 int
 129 magicNumber() {
 130     return 43110;
 131 }
 132 
 133 /*
 134  * Reads nbyte bytes from file descriptor fd into buf,
 135  * The read operation is retried in case of EINTR or partial reads.
 136  *
 137  * Returns number of bytes read (normally nbyte, but may be less in
 138  * case of EOF).  In case of read errors, returns -1 and sets errno.
 139  */
 140 ssize_t
 141 readFully(int fd, void *buf, size_t nbyte)
 142 {
 143     ssize_t remaining = nbyte;
 144     for (;;) {
 145         ssize_t n = read(fd, buf, remaining);
 146         if (n == 0) {
 147             return nbyte - remaining;
 148         } else if (n > 0) {
 149             remaining -= n;
 150             if (remaining <= 0)
 151                 return nbyte;
 152             /* We were interrupted in the middle of reading the bytes.
 153              * Unlikely, but possible. */
 154             buf = (void *) (((char *)buf) + n);
 155         } else if (errno == EINTR) {
 156             /* Strange signals like SIGJVM1 are possible at any time.
 157              * See http://www.dreamsongs.com/WorseIsBetter.html */
 158         } else {
 159             return -1;
 160         }
 161     }
 162 }
 163 
 164 void
 165 initVectorFromBlock(const char**vector, const char* block, int count)
 166 {
 167     int i;
 168     const char *p;
 169     for (i = 0, p = block; i < count; i++) {
 170         /* Invariant: p always points to the start of a C string. */
 171         vector[i] = p;
 172         while (*(p++));
 173     }
 174     vector[count] = NULL;
 175 }
 176 
 177 /**
 178  * Exec FILE as a traditional Bourne shell script (i.e. one without #!).
 179  * If we could do it over again, we would probably not support such an ancient
 180  * misfeature, but compatibility wins over sanity.  The original support for
 181  * this was imported accidentally from execvp().
 182  */
 183 void
 184 execve_as_traditional_shell_script(const char *file,
 185                                    const char *argv[],
 186                                    const char *const envp[])
 187 {
 188     /* Use the extra word of space provided for us in argv by caller. */
 189     const char *argv0 = argv[0];
 190     const char *const *end = argv;
 191     while (*end != NULL)
 192         ++end;
 193     memmove(argv+2, argv+1, (end-argv) * sizeof(*end));
 194     argv[0] = "/bin/sh";
 195     argv[1] = file;
 196     execve(argv[0], (char **) argv, (char **) envp);
 197     /* Can't even exec /bin/sh?  Big trouble, but let's soldier on... */
 198     memmove(argv+1, argv+2, (end-argv) * sizeof(*end));
 199     argv[0] = argv0;
 200 }
 201 
 202 /**
 203  * Like execve(2), except that in case of ENOEXEC, FILE is assumed to
 204  * be a shell script and the system default shell is invoked to run it.
 205  */
 206 void
 207 execve_with_shell_fallback(int mode, const char *file,
 208                            const char *argv[],
 209                            const char *const envp[])
 210 {
 211     if (mode == MODE_CLONE || mode == MODE_VFORK) {
 212         /* shared address space; be very careful. */
 213         execve(file, (char **) argv, (char **) envp);
 214         if (errno == ENOEXEC)
 215             execve_as_traditional_shell_script(file, argv, envp);
 216     } else {
 217         /* unshared address space; we can mutate environ. */
 218         environ = (char **) envp;
 219         execvp(file, (char **) argv);
 220     }
 221 }
 222 
 223 /**
 224  * 'execvpe' should have been included in the Unix standards,
 225  * and is a GNU extension in glibc 2.10.
 226  *
 227  * JDK_execvpe is identical to execvp, except that the child environment is
 228  * specified via the 3rd argument instead of being inherited from environ.
 229  */
 230 void
 231 JDK_execvpe(int mode, const char *file,
 232             const char *argv[],
 233             const char *const envp[])
 234 {
 235     if (envp == NULL || (char **) envp == environ) {
 236         execvp(file, (char **) argv);
 237         // ENOEXEC indicates that the file header was not recognized. The musl C
 238         // library does not implement the fallback to /bin/sh for that case, so fall
 239         // through to the code below which implements that fallback using
 240         // execve_with_shell_fallback.
 241         if (errno != ENOEXEC) {
 242             return;
 243         }
 244     }
 245 
 246     if (*file == '\0') {
 247         errno = ENOENT;
 248         return;
 249     }
 250 
 251     if (strchr(file, '/') != NULL) {
 252         execve_with_shell_fallback(mode, file, argv, envp);
 253     } else {
 254         /* We must search PATH (parent's, not child's) */
 255         char expanded_file[PATH_MAX];
 256         int filelen = strlen(file);
 257         int sticky_errno = 0;
 258         const char * const * dirs;
 259         for (dirs = parentPathv; *dirs; dirs++) {
 260             const char * dir = *dirs;
 261             int dirlen = strlen(dir);
 262             if (filelen + dirlen + 2 >= PATH_MAX) {
 263                 errno = ENAMETOOLONG;
 264                 continue;
 265             }
 266             memcpy(expanded_file, dir, dirlen);
 267             if (expanded_file[dirlen - 1] != '/')
 268                 expanded_file[dirlen++] = '/';
 269             memcpy(expanded_file + dirlen, file, filelen);
 270             expanded_file[dirlen + filelen] = '\0';
 271             execve_with_shell_fallback(mode, expanded_file, argv, envp);
 272             /* There are 3 responses to various classes of errno:
 273              * return immediately, continue (especially for ENOENT),
 274              * or continue with "sticky" errno.
 275              *
 276              * From exec(3):
 277              *
 278              * If permission is denied for a file (the attempted
 279              * execve returned EACCES), these functions will continue
 280              * searching the rest of the search path.  If no other
 281              * file is found, however, they will return with the
 282              * global variable errno set to EACCES.
 283              */
 284             switch (errno) {
 285             case EACCES:
 286                 sticky_errno = errno;
 287                 /* FALLTHRU */
 288             case ENOENT:
 289             case ENOTDIR:
 290 #ifdef ELOOP
 291             case ELOOP:
 292 #endif
 293 #ifdef ESTALE
 294             case ESTALE:
 295 #endif
 296 #ifdef ENODEV
 297             case ENODEV:
 298 #endif
 299 #ifdef ETIMEDOUT
 300             case ETIMEDOUT:
 301 #endif
 302                 break; /* Try other directories in PATH */
 303             default:
 304                 return;
 305             }
 306         }
 307         if (sticky_errno != 0)
 308             errno = sticky_errno;
 309     }
 310 }
 311 
 312 /**
 313  * Child process after a successful fork().
 314  * This function must not return, and must be prepared for either all
 315  * of its address space to be shared with its parent, or to be a copy.
 316  * It must not modify global variables such as "environ".
 317  */
 318 int
 319 childProcess(void *arg)
 320 {
 321     const ChildStuff* p = (const ChildStuff*) arg;
 322 
 323     /* Close the parent sides of the pipes.
 324        Closing pipe fds here is redundant, since closeDescriptors()
 325        would do it anyways, but a little paranoia is a good thing. */
 326     if ((closeSafely(p->in[1])   == -1) ||
 327         (closeSafely(p->out[0])  == -1) ||
 328         (closeSafely(p->err[0])  == -1) ||
 329         (closeSafely(p->childenv[0])  == -1) ||
 330         (closeSafely(p->childenv[1])  == -1) ||
 331         (closeSafely(p->fail[0]) == -1))
 332         goto WhyCantJohnnyExec;
 333 
 334     /* Give the child sides of the pipes the right fileno's. */
 335     /* Note: it is possible for in[0] == 0 */
 336     if ((moveDescriptor(p->in[0] != -1 ?  p->in[0] : p->fds[0],
 337                         STDIN_FILENO) == -1) ||
 338         (moveDescriptor(p->out[1]!= -1 ? p->out[1] : p->fds[1],
 339                         STDOUT_FILENO) == -1))
 340         goto WhyCantJohnnyExec;
 341 
 342     if (p->redirectErrorStream) {
 343         if ((closeSafely(p->err[1]) == -1) ||
 344             (restartableDup2(STDOUT_FILENO, STDERR_FILENO) == -1))
 345             goto WhyCantJohnnyExec;
 346     } else {
 347         if (moveDescriptor(p->err[1] != -1 ? p->err[1] : p->fds[2],
 348                            STDERR_FILENO) == -1)
 349             goto WhyCantJohnnyExec;
 350     }
 351 
 352     if (moveDescriptor(p->fail[1], FAIL_FILENO) == -1)
 353         goto WhyCantJohnnyExec;
 354 
 355     /* close everything */
 356     if (closeDescriptors() == 0) { /* failed,  close the old way */
 357         int max_fd = (int)sysconf(_SC_OPEN_MAX);
 358         int fd;
 359         for (fd = FAIL_FILENO + 1; fd < max_fd; fd++)
 360             if (close(fd) == -1 && errno != EBADF)
 361                 goto WhyCantJohnnyExec;
 362     }
 363 
 364     /* change to the new working directory */
 365     if (p->pdir != NULL && chdir(p->pdir) < 0)
 366         goto WhyCantJohnnyExec;
 367 
 368     if (fcntl(FAIL_FILENO, F_SETFD, FD_CLOEXEC) == -1)
 369         goto WhyCantJohnnyExec;
 370 
 371     JDK_execvpe(p->mode, p->argv[0], p->argv, p->envv);
 372 
 373  WhyCantJohnnyExec:
 374     /* We used to go to an awful lot of trouble to predict whether the
 375      * child would fail, but there is no reliable way to predict the
 376      * success of an operation without *trying* it, and there's no way
 377      * to try a chdir or exec in the parent.  Instead, all we need is a
 378      * way to communicate any failure back to the parent.  Easy; we just
 379      * send the errno back to the parent over a pipe in case of failure.
 380      * The tricky thing is, how do we communicate the *success* of exec?
 381      * We use FD_CLOEXEC together with the fact that a read() on a pipe
 382      * yields EOF when the write ends (we have two of them!) are closed.
 383      */
 384     {
 385         int errnum = errno;
 386         restartableWrite(FAIL_FILENO, &errnum, sizeof(errnum));
 387     }
 388     close(FAIL_FILENO);
 389     _exit(-1);
 390     return 0;  /* Suppress warning "no return value from function" */
 391 }