Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
script3r
GitHub Repository: script3r/os161
Path: blob/master/user/testbin/badcall/bad_execv.c
737 views
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 <stdio.h>
38
#include <errno.h>
39
#include <err.h>
40
41
#include "config.h"
42
#include "test.h"
43
44
static
45
int
46
exec_common_fork(void)
47
{
48
int pid, rv, status;
49
50
pid = fork();
51
if (pid<0) {
52
warn("UH-OH: fork failed");
53
return -1;
54
}
55
56
if (pid==0) {
57
/* child */
58
return 0;
59
}
60
61
rv = waitpid(pid, &status, 0);
62
if (rv == -1) {
63
warn("UH-OH: waitpid failed");
64
return -1;
65
}
66
67
if (!WIFEXITED(status) || WEXITSTATUS(status) != MAGIC_STATUS) {
68
warnx("FAILURE: wrong exit code of subprocess");
69
}
70
return 1;
71
}
72
73
static
74
void
75
exec_badprog(const void *prog, const char *desc)
76
{
77
int rv;
78
char *args[2];
79
args[0] = (char *)"foo";
80
args[1] = NULL;
81
82
if (exec_common_fork() != 0) {
83
return;
84
}
85
86
rv = execv(prog, args);
87
report_test(rv, errno, EFAULT, desc);
88
exit(MAGIC_STATUS);
89
}
90
91
static
92
void
93
exec_emptyprog(void)
94
{
95
int rv;
96
char *args[2];
97
args[0] = (char *)"foo";
98
args[1] = NULL;
99
100
if (exec_common_fork() != 0) {
101
return;
102
}
103
104
rv = execv("", args);
105
report_test2(rv, errno, EINVAL, EISDIR, "exec the empty string");
106
exit(MAGIC_STATUS);
107
}
108
109
static
110
void
111
exec_badargs(void *args, const char *desc)
112
{
113
int rv;
114
115
if (exec_common_fork() != 0) {
116
return;
117
}
118
119
rv = execv("/bin/true", args);
120
report_test(rv, errno, EFAULT, desc);
121
exit(MAGIC_STATUS);
122
}
123
124
static
125
void
126
exec_onearg(void *ptr, const char *desc)
127
{
128
int rv;
129
130
char *args[3];
131
args[0] = (char *)"foo";
132
args[1] = (char *)ptr;
133
args[2] = NULL;
134
135
if (exec_common_fork() != 0) {
136
return;
137
}
138
139
rv = execv("/bin/true", args);
140
report_test(rv, errno, EFAULT, desc);
141
exit(MAGIC_STATUS);
142
}
143
144
void
145
test_execv(void)
146
{
147
exec_badprog(NULL, "exec NULL");
148
exec_badprog(INVAL_PTR, "exec invalid pointer");
149
exec_badprog(KERN_PTR, "exec kernel pointer");
150
151
exec_emptyprog();
152
153
exec_badargs(NULL, "exec /bin/true with NULL arglist");
154
exec_badargs(INVAL_PTR, "exec /bin/true with invalid pointer arglist");
155
exec_badargs(KERN_PTR, "exec /bin/true with kernel pointer arglist");
156
157
exec_onearg(INVAL_PTR, "exec /bin/true with invalid pointer arg");
158
exec_onearg(KERN_PTR, "exec /bin/true with kernel pointer arg");
159
}
160
161