os161-1.99
 All Data Structures
bad_execv.c
00001 /*
00002  * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
00003  *      The President and Fellows of Harvard College.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 /*
00031  * bad calls to execv()
00032  */
00033 
00034 #include <sys/types.h>
00035 #include <stdlib.h>
00036 #include <unistd.h>
00037 #include <errno.h>
00038 #include <err.h>
00039 
00040 #include "config.h"
00041 #include "test.h"
00042 
00043 static
00044 int
00045 exec_common_fork(void)
00046 {
00047         int pid, rv, status;
00048 
00049         pid = fork();
00050         if (pid<0) {
00051                 warn("UH-OH: fork failed");
00052                 return -1;
00053         }
00054         
00055         if (pid==0) {
00056                 /* child */
00057                 return 0;
00058         }
00059 
00060         rv = waitpid(pid, &status, 0);
00061         if (rv == -1) {
00062                 warn("UH-OH: waitpid failed");
00063                 return -1;
00064         }
00065         if (!WIFEXITED(status) || WEXITSTATUS(status) != MAGIC_STATUS) {
00066                 warnx("FAILURE: wrong exit code of subprocess");
00067         }
00068         return 1;
00069 }
00070 
00071 static
00072 void
00073 exec_badprog(const void *prog, const char *desc)
00074 {
00075         int rv;
00076         char *args[2];
00077         args[0] = (char *)"foo";
00078         args[1] = NULL;
00079 
00080         if (exec_common_fork() != 0) {
00081                 return;
00082         }
00083 
00084         rv = execv(prog, args);
00085         report_test(rv, errno, EFAULT, desc);
00086         exit(MAGIC_STATUS);
00087 }
00088 
00089 static
00090 void
00091 exec_emptyprog(void)
00092 {
00093         int rv;
00094         char *args[2];
00095         args[0] = (char *)"foo";
00096         args[1] = NULL;
00097 
00098         if (exec_common_fork() != 0) {
00099                 return;
00100         }
00101 
00102         rv = execv("", args);
00103         report_test2(rv, errno, EINVAL, EISDIR, "exec the empty string");
00104         exit(MAGIC_STATUS);
00105 }
00106 
00107 static
00108 void
00109 exec_badargs(void *args, const char *desc)
00110 {
00111         int rv;
00112 
00113         if (exec_common_fork() != 0) {
00114                 return;
00115         }
00116 
00117         rv = execv("/bin/true", args);
00118         report_test(rv, errno, EFAULT, desc);
00119         exit(MAGIC_STATUS);
00120 }
00121 
00122 static
00123 void
00124 exec_onearg(void *ptr, const char *desc)
00125 {
00126         int rv;
00127 
00128         char *args[3];
00129         args[0] = (char *)"foo";
00130         args[1] = (char *)ptr;
00131         args[2] = NULL;
00132 
00133         if (exec_common_fork() != 0) {
00134                 return;
00135         }
00136 
00137         rv = execv("/bin/true", args);
00138         report_test(rv, errno, EFAULT, desc);
00139         exit(MAGIC_STATUS);
00140 }
00141 
00142 void
00143 test_execv(void)
00144 {
00145         exec_badprog(NULL, "exec NULL");
00146         exec_badprog(INVAL_PTR, "exec invalid pointer");
00147         exec_badprog(KERN_PTR, "exec kernel pointer");
00148 
00149         exec_emptyprog();
00150 
00151         exec_badargs(NULL, "exec /bin/true with NULL arglist");
00152         exec_badargs(INVAL_PTR, "exec /bin/true with invalid pointer arglist");
00153         exec_badargs(KERN_PTR, "exec /bin/true with kernel pointer arglist");
00154 
00155         exec_onearg(INVAL_PTR, "exec /bin/true with invalid pointer arg");
00156         exec_onearg(KERN_PTR, "exec /bin/true with kernel pointer arg");
00157 }
 All Data Structures