/*1* Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 20092* The President and Fellows of Harvard College.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. Neither the name of the University nor the names of its contributors13* may be used to endorse or promote products derived from this software14* without specific prior written permission.15*16* THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829/*30* bad calls to execv()31*/3233#include <sys/types.h>34#include <stdlib.h>35#include <unistd.h>36#include <stdio.h>37#include <errno.h>38#include <err.h>3940#include "config.h"41#include "test.h"4243static44int45exec_common_fork(void)46{47int pid, rv, status;4849pid = fork();50if (pid<0) {51warn("UH-OH: fork failed");52return -1;53}5455if (pid==0) {56/* child */57return 0;58}5960rv = waitpid(pid, &status, 0);61if (rv == -1) {62warn("UH-OH: waitpid failed");63return -1;64}6566if (!WIFEXITED(status) || WEXITSTATUS(status) != MAGIC_STATUS) {67warnx("FAILURE: wrong exit code of subprocess");68}69return 1;70}7172static73void74exec_badprog(const void *prog, const char *desc)75{76int rv;77char *args[2];78args[0] = (char *)"foo";79args[1] = NULL;8081if (exec_common_fork() != 0) {82return;83}8485rv = execv(prog, args);86report_test(rv, errno, EFAULT, desc);87exit(MAGIC_STATUS);88}8990static91void92exec_emptyprog(void)93{94int rv;95char *args[2];96args[0] = (char *)"foo";97args[1] = NULL;9899if (exec_common_fork() != 0) {100return;101}102103rv = execv("", args);104report_test2(rv, errno, EINVAL, EISDIR, "exec the empty string");105exit(MAGIC_STATUS);106}107108static109void110exec_badargs(void *args, const char *desc)111{112int rv;113114if (exec_common_fork() != 0) {115return;116}117118rv = execv("/bin/true", args);119report_test(rv, errno, EFAULT, desc);120exit(MAGIC_STATUS);121}122123static124void125exec_onearg(void *ptr, const char *desc)126{127int rv;128129char *args[3];130args[0] = (char *)"foo";131args[1] = (char *)ptr;132args[2] = NULL;133134if (exec_common_fork() != 0) {135return;136}137138rv = execv("/bin/true", args);139report_test(rv, errno, EFAULT, desc);140exit(MAGIC_STATUS);141}142143void144test_execv(void)145{146exec_badprog(NULL, "exec NULL");147exec_badprog(INVAL_PTR, "exec invalid pointer");148exec_badprog(KERN_PTR, "exec kernel pointer");149150exec_emptyprog();151152exec_badargs(NULL, "exec /bin/true with NULL arglist");153exec_badargs(INVAL_PTR, "exec /bin/true with invalid pointer arglist");154exec_badargs(KERN_PTR, "exec /bin/true with kernel pointer arglist");155156exec_onearg(INVAL_PTR, "exec /bin/true with invalid pointer arg");157exec_onearg(KERN_PTR, "exec /bin/true with kernel pointer arg");158}159160161