root/user/testbin/badcall/bad_execv.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. exec_common_fork
  2. exec_badprog
  3. exec_emptyprog
  4. exec_badargs
  5. exec_onearg
  6. test_execv

   1 /*
   2  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
   3  *      The President and Fellows of Harvard College.
   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  * 1. Redistributions of source code must retain the above copyright
   9  *    notice, this list of conditions and the following disclaimer.
  10  * 2. Redistributions in binary form must reproduce the above copyright
  11  *    notice, this list of conditions and the following disclaimer in the
  12  *    documentation and/or other materials provided with the distribution.
  13  * 3. Neither the name of the University nor the names of its contributors
  14  *    may be used to endorse or promote products derived from this software
  15  *    without specific prior written permission.
  16  *
  17  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27  * SUCH DAMAGE.
  28  */
  29 
  30 /*
  31  * bad calls to execv()
  32  */
  33 
  34 #include <sys/types.h>
  35 #include <stdlib.h>
  36 #include <unistd.h>
  37 #include <errno.h>
  38 #include <err.h>
  39 
  40 #include "config.h"
  41 #include "test.h"
  42 
  43 static
  44 int
  45 exec_common_fork(void)
  46 {
  47         int pid, rv, status;
  48 
  49         pid = fork();
  50         if (pid<0) {
  51                 warn("UH-OH: fork failed");
  52                 return -1;
  53         }
  54         
  55         if (pid==0) {
  56                 /* child */
  57                 return 0;
  58         }
  59 
  60         rv = waitpid(pid, &status, 0);
  61         if (rv == -1) {
  62                 warn("UH-OH: waitpid failed");
  63                 return -1;
  64         }
  65         if (!WIFEXITED(status) || WEXITSTATUS(status) != MAGIC_STATUS) {
  66                 warnx("FAILURE: wrong exit code of subprocess");
  67         }
  68         return 1;
  69 }
  70 
  71 static
  72 void
  73 exec_badprog(const void *prog, const char *desc)
  74 {
  75         int rv;
  76         char *args[2];
  77         args[0] = (char *)"foo";
  78         args[1] = NULL;
  79 
  80         if (exec_common_fork() != 0) {
  81                 return;
  82         }
  83 
  84         rv = execv(prog, args);
  85         report_test(rv, errno, EFAULT, desc);
  86         exit(MAGIC_STATUS);
  87 }
  88 
  89 static
  90 void
  91 exec_emptyprog(void)
  92 {
  93         int rv;
  94         char *args[2];
  95         args[0] = (char *)"foo";
  96         args[1] = NULL;
  97 
  98         if (exec_common_fork() != 0) {
  99                 return;
 100         }
 101 
 102         rv = execv("", args);
 103         report_test2(rv, errno, EINVAL, EISDIR, "exec the empty string");
 104         exit(MAGIC_STATUS);
 105 }
 106 
 107 static
 108 void
 109 exec_badargs(void *args, const char *desc)
 110 {
 111         int rv;
 112 
 113         if (exec_common_fork() != 0) {
 114                 return;
 115         }
 116 
 117         rv = execv("/bin/true", args);
 118         report_test(rv, errno, EFAULT, desc);
 119         exit(MAGIC_STATUS);
 120 }
 121 
 122 static
 123 void
 124 exec_onearg(void *ptr, const char *desc)
 125 {
 126         int rv;
 127 
 128         char *args[3];
 129         args[0] = (char *)"foo";
 130         args[1] = (char *)ptr;
 131         args[2] = NULL;
 132 
 133         if (exec_common_fork() != 0) {
 134                 return;
 135         }
 136 
 137         rv = execv("/bin/true", args);
 138         report_test(rv, errno, EFAULT, desc);
 139         exit(MAGIC_STATUS);
 140 }
 141 
 142 void
 143 test_execv(void)
 144 {
 145         exec_badprog(NULL, "exec NULL");
 146         exec_badprog(INVAL_PTR, "exec invalid pointer");
 147         exec_badprog(KERN_PTR, "exec kernel pointer");
 148 
 149         exec_emptyprog();
 150 
 151         exec_badargs(NULL, "exec /bin/true with NULL arglist");
 152         exec_badargs(INVAL_PTR, "exec /bin/true with invalid pointer arglist");
 153         exec_badargs(KERN_PTR, "exec /bin/true with kernel pointer arglist");
 154 
 155         exec_onearg(INVAL_PTR, "exec /bin/true with invalid pointer arg");
 156         exec_onearg(KERN_PTR, "exec /bin/true with kernel pointer arg");
 157 }

/* [<][>][^][v][top][bottom][index][help] */