1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <err.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/errno.h>
#include <sys/wait.h>
#include <sys/stat.h>
pid_t child;
char pp_dir1[] = "/tmp/ppdir1.XXXXXX"; /* pledgepathed */
char pp_dir2[] = "/tmp/ppdir2.XXXXXX"; /* not pledgepathed */
char pp_file1[] = "/tmp/ppfile1.XXXXXX"; /* pledgepathed */
char pp_file2[] = "/tmp/ppfile2.XXXXXX"; /* not pledgepathed */
#define PP_SHOULD_SUCCEED(A, B) do { \
if (A) { \
err(1, "%s:%d - %s", __FILE__, __LINE__, B); \
} \
} while (0)
#define PP_SHOULD_FAIL(A, B) do { \
if (A) { \
if (do_pp && errno != ENOENT) \
err(1, "%s:%d - %s", __FILE__, __LINE__, B); \
} else { \
if (do_pp) \
errx(1, "%s:%d - %s worked when it should not " \
"have", __FILE__, __LINE__, B); \
} \
} while(0)
static void
do_pledgepath(void)
{
if (pledgepath(pp_dir1, 0) == -1)
err(1, "%s:%d - pledgepath", __FILE__, __LINE__);
if (pledgepath(pp_file1, 0) == -1)
err(1, "%s:%d - pledgepath", __FILE__, __LINE__);
}
static int
runcompare(int (*func)(int))
{
int ppath = 0, nonppath = 0, status;
pid_t pid = fork();
if (pid == 0) {
exit(func(0));
}
status = 0;
waitpid(pid, &status, 0);
if (WIFEXITED(status))
nonppath = WEXITSTATUS(status);
pid = fork();
if (pid == 0) {
exit(func(1));
}
status = 0;
waitpid(pid, &status, 0);
if (WIFEXITED(status))
ppath = WEXITSTATUS(status);
if (ppath == nonppath) {
printf("[SUCCESS] ppath = %d, nonppath = %d\n", ppath, nonppath);
return 0;
}
printf("[FAIL] ppath = %d, nonppath = %d\n", ppath, nonppath);
return 1;
}
static int
test_open(int do_pp)
{
char filename[256];
int dirfd;
PP_SHOULD_SUCCEED(((dirfd = open("/", O_RDONLY | O_DIRECTORY)) == -1), "open");
if (do_pp) {
printf("testing open\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath cpath wpath", NULL) == -1), "pledge");
PP_SHOULD_SUCCEED((open(pp_file1, O_RDWR) == -1), "open");
PP_SHOULD_SUCCEED((open(pp_file1, O_RDWR) == -1), "open");
PP_SHOULD_SUCCEED((openat(dirfd, "etc/hosts", O_RDONLY) == -1), "openat");
PP_SHOULD_FAIL((openat(dirfd, pp_file2, O_RDWR) == -1), "openat");
PP_SHOULD_FAIL((open(pp_file2, O_RDWR) == -1), "open");
(void) snprintf(filename, sizeof(filename), "%s/%s", pp_dir1, "newfile");
PP_SHOULD_SUCCEED((open(filename, O_RDWR|O_CREAT) == -1), "open");
(void) snprintf(filename, sizeof(filename), "%s/%s", pp_dir2, "newfile");
PP_SHOULD_FAIL((open(filename, O_RDWR|O_CREAT) == -1), "open");
return 0;
}
static int
test_unlink(int do_pp)
{
char filename1[256];
char filename2[256];
char filename3[] = "/tmp/nukeme.XXXXXX";
int fd;
(void) snprintf(filename1, sizeof(filename1), "%s/%s", pp_dir1,
"nukeme");
(void) snprintf(filename2, sizeof(filename2), "%s/%s", pp_dir2,
"nukeme");
PP_SHOULD_SUCCEED((open(filename1, O_RDWR|O_CREAT) == -1), "open");
PP_SHOULD_SUCCEED((open(filename2, O_RDWR|O_CREAT) == -1), "open");
if ((fd = mkstemp(filename3)) == -1)
err(1, "%s:%d - mkstemp", __FILE__, __LINE__);
if (do_pp) {
printf("testing unlink\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath cpath wpath", NULL) == -1),
"pledge");
PP_SHOULD_SUCCEED((unlink(filename1) == -1), "unlink");
PP_SHOULD_FAIL((unlink(filename2) == -1), "unlink");
PP_SHOULD_FAIL((unlink(filename3) == -1), "unlink");
return 0;
}
static int
test_link(int do_pp)
{
char filename[256];
char filename2[256];
if (do_pp) {
printf("testing link\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath cpath wpath", NULL) == -1),
"pledge");
(void) snprintf(filename, sizeof(filename), "%s/%s", pp_dir1,
"linkpp1");
(void) snprintf(filename2, sizeof(filename2), "%s/%s", pp_dir2,
"linkpp2");
unlink(filename);
unlink(filename2);
PP_SHOULD_SUCCEED((link(pp_file1, filename) == -1), "link");
unlink(filename);
PP_SHOULD_FAIL((link(pp_file2, filename) == -1), "link");
PP_SHOULD_FAIL((link(pp_file1, filename2) == -1), "link");
return 0;
}
static int
test_chdir(int do_pp)
{
if (do_pp) {
printf("testing chdir\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath", NULL) == -1), "pledge");
PP_SHOULD_SUCCEED((chdir(pp_dir1) == -1), "chdir");
PP_SHOULD_FAIL((chdir(pp_dir2) == -1), "chdir");
return 0;
}
static int
test_rename(int do_pp)
{
char filename1[256];
char filename2[256];
char rfilename1[256];
char rfilename2[256];
int dirfd1, dirfd2;
if ((dirfd1 = open(pp_dir1, O_RDONLY | O_DIRECTORY)) == -1)
err(1, "%s:%d - open of dir1", __FILE__, __LINE__);
if ((dirfd2 = open(pp_dir2, O_RDONLY | O_DIRECTORY)) == -1)
err(1, "%s:%d - open of dir2", __FILE__, __LINE__);
(void) snprintf(filename1, sizeof(filename1), "%s/%s", pp_dir1,
"file1");
PP_SHOULD_SUCCEED((open(filename1, O_RDWR|O_CREAT) == -1), "open");
(void) snprintf(filename2, sizeof(filename2), "%s/%s", pp_dir2,
"file2");
PP_SHOULD_SUCCEED((open(filename2, O_RDWR|O_CREAT) == -1), "open");
(void) snprintf(rfilename1, sizeof(rfilename1), "%s/%s", pp_dir1,
"rfile1");
(void) snprintf(rfilename2, sizeof(rfilename2), "%s/%s", pp_dir2,
"rfile2");
if (do_pp) {
printf("testing rename\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath wpath cpath", NULL) == -1),
"pledge");
PP_SHOULD_SUCCEED((rename(filename1, rfilename1) == -1), "rename");
PP_SHOULD_FAIL((rename(filename2, rfilename2) == -1), "rename");
PP_SHOULD_SUCCEED((open(filename1, O_RDWR|O_CREAT) == -1), "open");
PP_SHOULD_FAIL((rename(filename1, rfilename2) == -1), "rename");
PP_SHOULD_SUCCEED((open(filename1, O_RDWR|O_CREAT) == -1), "open");
PP_SHOULD_FAIL((rename(filename1, pp_file2) == -1), "rename");
PP_SHOULD_SUCCEED((open(filename1, O_RDWR|O_CREAT) == -1), "open");
PP_SHOULD_SUCCEED((renameat(dirfd1, "file1", dirfd2, "rfile2") == -1),
"renameat");
PP_SHOULD_SUCCEED((open(filename1, O_RDWR|O_CREAT) == -1), "open");
PP_SHOULD_FAIL((renameat(dirfd1, "file1", dirfd2, rfilename2) == -1),
"renameat");
return (0);
}
static int
test_access(int do_pp)
{
if (do_pp) {
printf("testing access\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath", NULL) == -1), "pledge");
PP_SHOULD_SUCCEED((access(pp_file1, R_OK) == -1), "access");
PP_SHOULD_FAIL((access(pp_file2, R_OK) == -1), "access");
PP_SHOULD_SUCCEED((access(pp_dir1, R_OK) == -1), "access");
PP_SHOULD_FAIL((access(pp_dir2, R_OK) == -1), "access");
return 0;
}
static int
test_chflags(int do_pp)
{
if (do_pp) {
printf("testing chflags\n");
do_pledgepath();
}
PP_SHOULD_SUCCEED((pledge("stdio rpath", NULL) == -1), "pledge");
PP_SHOULD_SUCCEED((chflags(pp_file1, UF_NODUMP) == -1), "chflags");
PP_SHOULD_FAIL((chflags(pp_file2, UF_NODUMP) == -1), "chflags");
return 0;
}
int
main (int argc, char *argv[])
{
int fd1, fd2, failures = 0;
PP_SHOULD_SUCCEED((mkdtemp(pp_dir1) == NULL), "mkdtmp");
PP_SHOULD_SUCCEED((mkdtemp(pp_dir2) == NULL), "mkdtmp");
PP_SHOULD_SUCCEED(((fd1 = mkstemp(pp_file1)) == -1), "mkstemp");
close(fd1);
PP_SHOULD_SUCCEED(((fd2 = mkstemp(pp_file2)) == -1), "mkstemp");
close(fd2);
failures += runcompare(test_open);
failures += runcompare(test_unlink);
failures += runcompare(test_link);
failures += runcompare(test_chdir);
failures += runcompare(test_rename);
failures += runcompare(test_access);
failures += runcompare(test_chflags);
exit(failures);
}
|