1 |
/* |
2 |
* tomoyo_file_test.c |
3 |
* |
4 |
* Testing program for fs/tomoyo_file.c |
5 |
* |
6 |
* Copyright (C) 2005-2008 NTT DATA CORPORATION |
7 |
* |
8 |
* Version: 1.6.0-rc 2008/03/28 |
9 |
* |
10 |
*/ |
11 |
#include "include.h" |
12 |
|
13 |
static int profile_fd = EOF; |
14 |
static int domain_fd = EOF; |
15 |
static int exception_fd = EOF; |
16 |
static const char *policy = ""; |
17 |
static char self_domain[4096] = ""; |
18 |
|
19 |
static int write_policy(void) { |
20 |
FILE *fp; |
21 |
char buffer[8192]; |
22 |
char *cp; |
23 |
int domain_found = 0; |
24 |
int policy_found = 0; |
25 |
memset(buffer, 0, sizeof(buffer)); |
26 |
cp = "255-MAC_FOR_FILE=disabled\n"; |
27 |
write(profile_fd, cp, strlen(cp)); |
28 |
fp = fopen(proc_policy_domain_policy, "r"); |
29 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
30 |
write(profile_fd, cp, strlen(cp)); |
31 |
write(domain_fd, policy, strlen(policy)); |
32 |
write(domain_fd, "\n", 1); |
33 |
if (!fp) { |
34 |
printf("%s : BUG: policy read failed\n", policy); |
35 |
return 0; |
36 |
} |
37 |
while (fgets(buffer, sizeof(buffer) - 1, fp)) { |
38 |
cp = strchr(buffer, '\n'); |
39 |
if (cp) *cp = '\0'; |
40 |
if (!strncmp(buffer, "<kernel>", 8)) domain_found = !strcmp(self_domain, buffer); |
41 |
if (domain_found) { |
42 |
//printf("<%s>\n", buffer); |
43 |
if (!strcmp(buffer, policy)) { |
44 |
policy_found = 1; |
45 |
break; |
46 |
} |
47 |
} |
48 |
} |
49 |
fclose(fp); |
50 |
if (!policy_found) { |
51 |
printf("%s : BUG: policy write failed\n", policy); |
52 |
return 0; |
53 |
} |
54 |
errno = 0; |
55 |
return 1; |
56 |
} |
57 |
|
58 |
static void delete_policy(void) { |
59 |
write(domain_fd, "delete ", 7); |
60 |
write(domain_fd, policy, strlen(policy)); |
61 |
write(domain_fd, "\n", 1); |
62 |
} |
63 |
|
64 |
static void show_result(int result, char should_success) { |
65 |
int err = errno; |
66 |
printf("%s : ", policy); |
67 |
if (should_success) { |
68 |
if (result != EOF) printf("OK\n"); |
69 |
else printf("FAILED: %s\n", strerror(err)); |
70 |
} else { |
71 |
if (result == EOF) { |
72 |
if (err == EPERM) printf("OK: Permission denied.\n"); |
73 |
else printf("FAILED: %s\n", strerror(err)); |
74 |
} else { |
75 |
printf("BUG: didn't fail.\n"); |
76 |
} |
77 |
} |
78 |
} |
79 |
|
80 |
static void create2(const char *pathname) { |
81 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
82 |
write(profile_fd, cp, strlen(cp)); |
83 |
close(creat(pathname, 0600)); |
84 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
85 |
write(profile_fd, cp, strlen(cp)); |
86 |
} |
87 |
|
88 |
static void mkdir2(const char *pathname) { |
89 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
90 |
write(profile_fd, cp, strlen(cp)); |
91 |
mkdir(pathname, 0600); |
92 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
93 |
write(profile_fd, cp, strlen(cp)); |
94 |
} |
95 |
|
96 |
static void unlink2(const char *pathname) { |
97 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
98 |
write(profile_fd, cp, strlen(cp)); |
99 |
unlink(pathname); |
100 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
101 |
write(profile_fd, cp, strlen(cp)); |
102 |
} |
103 |
|
104 |
static void rmdir2(const char *pathname) { |
105 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
106 |
write(profile_fd, cp, strlen(cp)); |
107 |
rmdir(pathname); |
108 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
109 |
write(profile_fd, cp, strlen(cp)); |
110 |
} |
111 |
|
112 |
static void StageFileTest(void) { |
113 |
char *filename = ""; |
114 |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range"; |
115 |
if (write_policy()) { |
116 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
117 |
int buffer[2] = { 32768, 61000 }; |
118 |
size_t size = sizeof(buffer); |
119 |
show_result(sysctl(name, 3, buffer, &size, 0, 0), 1); |
120 |
delete_policy(); |
121 |
show_result(sysctl(name, 3, buffer, &size, 0, 0), 0); |
122 |
} |
123 |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range"; |
124 |
if (write_policy()) { |
125 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
126 |
int buffer[2] = { 32768, 61000 }; |
127 |
size_t size = sizeof(buffer); |
128 |
show_result(sysctl(name, 3, 0, 0, buffer, size), 1); |
129 |
delete_policy(); |
130 |
show_result(sysctl(name, 3, 0, 0, buffer, size), 0); |
131 |
} |
132 |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range"; |
133 |
if (write_policy()) { |
134 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
135 |
int buffer[2] = { 32768, 61000 }; |
136 |
size_t size = sizeof(buffer); |
137 |
show_result(sysctl(name, 3, buffer, &size, buffer, size), 1); |
138 |
delete_policy(); |
139 |
show_result(sysctl(name, 3, buffer, &size, buffer, size), 0); |
140 |
} |
141 |
|
142 |
policy = "allow_read /bin/true"; |
143 |
if (write_policy()) { |
144 |
show_result(uselib("/bin/true"), 1); |
145 |
delete_policy(); |
146 |
show_result(uselib("/bin/true"), 0); |
147 |
} |
148 |
|
149 |
policy = "allow_execute /bin/true"; |
150 |
if (write_policy()) { |
151 |
int pipe_fd[2] = { EOF, EOF }; |
152 |
int err = 0; |
153 |
fflush(stdout); fflush(stderr); |
154 |
pipe(pipe_fd); |
155 |
if (fork() == 0) { |
156 |
execl("/bin/true", "/bin/true", NULL); |
157 |
err = errno; |
158 |
write(pipe_fd[1], &err, sizeof(err)); |
159 |
_exit(0); |
160 |
} |
161 |
close(pipe_fd[1]); |
162 |
read(pipe_fd[0], &err, sizeof(err)); |
163 |
close(pipe_fd[0]); |
164 |
wait(NULL); |
165 |
errno = err; |
166 |
show_result(err ? EOF : 0, 1); |
167 |
delete_policy(); |
168 |
fflush(stdout); fflush(stderr); |
169 |
pipe(pipe_fd); |
170 |
if (fork() == 0) { |
171 |
execl("/bin/true", "/bin/true", NULL); |
172 |
err = errno; |
173 |
write(pipe_fd[1], &err, sizeof(err)); |
174 |
_exit(0); |
175 |
} |
176 |
close(pipe_fd[1]); |
177 |
read(pipe_fd[0], &err, sizeof(err)); |
178 |
close(pipe_fd[0]); |
179 |
wait(NULL); |
180 |
errno = err; |
181 |
show_result(err ? EOF : 0, 0); |
182 |
} |
183 |
|
184 |
policy = "allow_read /dev/null"; |
185 |
if (write_policy()) { |
186 |
int fd = open("/dev/null", O_RDONLY); |
187 |
show_result(fd, 1); |
188 |
if (fd != EOF) close(fd); |
189 |
delete_policy(); |
190 |
fd = open("/dev/null", O_RDONLY); |
191 |
show_result(fd, 0); |
192 |
if (fd != EOF) close(fd); |
193 |
} |
194 |
|
195 |
policy = "allow_write /dev/null"; |
196 |
if (write_policy()) { |
197 |
int fd = open("/dev/null", O_WRONLY); |
198 |
show_result(fd, 1); |
199 |
if (fd != EOF) close(fd); |
200 |
delete_policy(); |
201 |
fd = open("/dev/null", O_WRONLY); |
202 |
show_result(fd, 0); |
203 |
if (fd != EOF) close(fd); |
204 |
} |
205 |
|
206 |
policy = "allow_read/write /dev/null"; |
207 |
if (write_policy()) { |
208 |
int fd = open("/dev/null", O_RDWR); |
209 |
show_result(fd, 1); |
210 |
if (fd != EOF) close(fd); |
211 |
delete_policy(); |
212 |
fd = open("/dev/null", O_RDWR); |
213 |
show_result(fd, 0); |
214 |
if (fd != EOF) close(fd); |
215 |
} |
216 |
|
217 |
policy = "allow_create /tmp/open_test"; |
218 |
if (write_policy()) { |
219 |
policy = "allow_write /tmp/open_test"; |
220 |
if (write_policy()) { |
221 |
int fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
222 |
show_result(fd, 1); |
223 |
if (fd != EOF) close(fd); |
224 |
unlink2("/tmp/open_test"); |
225 |
delete_policy(); |
226 |
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
227 |
show_result(fd, 0); |
228 |
if (fd != EOF) close(fd); |
229 |
unlink2("/tmp/open_test"); |
230 |
} |
231 |
policy = "allow_create /tmp/open_test\n"; |
232 |
delete_policy(); |
233 |
} |
234 |
|
235 |
policy = "allow_write /tmp/open_test"; |
236 |
if (write_policy()) { |
237 |
policy = "allow_create /tmp/open_test"; |
238 |
if (write_policy()) { |
239 |
int fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
240 |
show_result(fd, 1); |
241 |
if (fd != EOF) close(fd); |
242 |
unlink2("/tmp/open_test"); |
243 |
delete_policy(); |
244 |
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0666); |
245 |
show_result(fd, 0); |
246 |
if (fd != EOF) close(fd); |
247 |
unlink2("/tmp/open_test"); |
248 |
} |
249 |
policy = "allow_write /tmp/open_test\n"; |
250 |
delete_policy(); |
251 |
} |
252 |
|
253 |
filename = "/tmp/truncate_test"; |
254 |
create2(filename); |
255 |
|
256 |
policy = "allow_truncate /tmp/truncate_test"; |
257 |
if (write_policy()) { |
258 |
policy = "allow_write /tmp/truncate_test"; |
259 |
if (write_policy()) { |
260 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
261 |
show_result(fd, 1); |
262 |
if (fd != EOF) close(fd); |
263 |
delete_policy(); |
264 |
fd = open(filename, O_WRONLY | O_TRUNC); |
265 |
show_result(fd, 0); |
266 |
if (fd != EOF) close(fd); |
267 |
} |
268 |
policy = "allow_truncate /tmp/truncate_test"; |
269 |
delete_policy(); |
270 |
} |
271 |
|
272 |
policy = "allow_write /tmp/truncate_test"; |
273 |
if (write_policy()) { |
274 |
policy = "allow_truncate /tmp/truncate_test"; |
275 |
if (write_policy()) { |
276 |
int fd = open(filename, O_WRONLY | O_TRUNC); |
277 |
show_result(fd, 1); |
278 |
if (fd != EOF) close(fd); |
279 |
delete_policy(); |
280 |
fd = open(filename, O_WRONLY | O_TRUNC); |
281 |
show_result(fd, 0); |
282 |
if (fd != EOF) close(fd); |
283 |
} |
284 |
policy = "allow_write /tmp/truncate_test\n"; |
285 |
delete_policy(); |
286 |
} |
287 |
|
288 |
policy = "allow_truncate /tmp/truncate_test"; |
289 |
if (write_policy()) { |
290 |
show_result(truncate(filename, 0), 1); |
291 |
delete_policy(); |
292 |
show_result(truncate(filename, 0), 0); |
293 |
} |
294 |
|
295 |
policy = "allow_truncate /tmp/truncate_test"; |
296 |
if (write_policy()) { |
297 |
int fd; |
298 |
const char *cp = "255-MAC_FOR_FILE=disabled\n"; |
299 |
write(profile_fd, cp, strlen(cp)); |
300 |
fd = open(filename, O_WRONLY); |
301 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
302 |
write(profile_fd, cp, strlen(cp)); |
303 |
show_result(ftruncate(fd, 0), 1); |
304 |
delete_policy(); |
305 |
show_result(ftruncate(fd, 0), 0); |
306 |
if (fd != EOF) close(fd); |
307 |
} |
308 |
|
309 |
unlink2(filename); |
310 |
|
311 |
policy = "allow_create /tmp/mknod_reg_test"; |
312 |
if (write_policy()) { |
313 |
filename = "/tmp/mknod_reg_test"; |
314 |
show_result(mknod(filename, S_IFREG, 0), 1); |
315 |
delete_policy(); |
316 |
unlink2(filename); |
317 |
show_result(mknod(filename, S_IFREG, 0), 0); |
318 |
} |
319 |
|
320 |
policy = "allow_mkchar /tmp/mknod_chr_test"; |
321 |
if (write_policy()) { |
322 |
filename = "/tmp/mknod_chr_test"; |
323 |
show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 1); |
324 |
delete_policy(); |
325 |
unlink2(filename); |
326 |
show_result(mknod(filename, S_IFCHR, MKDEV(1, 3)), 0); |
327 |
} |
328 |
|
329 |
policy = "allow_mkblock /tmp/mknod_blk_test"; |
330 |
if (write_policy()) { |
331 |
filename = "/tmp/mknod_blk_test"; |
332 |
show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 1); |
333 |
delete_policy(); |
334 |
unlink2(filename); |
335 |
show_result(mknod(filename, S_IFBLK, MKDEV(1, 0)), 0); |
336 |
} |
337 |
|
338 |
policy = "allow_mkfifo /tmp/mknod_fifo_test"; |
339 |
if (write_policy()) { |
340 |
filename = "/tmp/mknod_fifo_test"; |
341 |
show_result(mknod(filename, S_IFIFO, 0), 1); |
342 |
delete_policy(); |
343 |
unlink2(filename); |
344 |
show_result(mknod(filename, S_IFIFO, 0), 0); |
345 |
} |
346 |
|
347 |
policy = "allow_mksock /tmp/mknod_sock_test"; |
348 |
if (write_policy()) { |
349 |
filename = "/tmp/mknod_sock_test"; |
350 |
show_result(mknod(filename, S_IFSOCK, 0), 1); |
351 |
delete_policy(); |
352 |
unlink2(filename); |
353 |
show_result(mknod(filename, S_IFSOCK, 0), 0); |
354 |
} |
355 |
|
356 |
policy = "allow_mkdir /tmp/mkdir_test/"; |
357 |
if (write_policy()) { |
358 |
filename = "/tmp/mkdir_test"; |
359 |
show_result(mkdir(filename, 0600), 1); |
360 |
delete_policy(); |
361 |
rmdir2(filename); |
362 |
show_result(mkdir(filename, 0600), 0); |
363 |
} |
364 |
|
365 |
policy = "allow_rmdir /tmp/rmdir_test/"; |
366 |
if (write_policy()) { |
367 |
filename = "/tmp/rmdir_test"; |
368 |
mkdir2(filename); |
369 |
show_result(rmdir(filename), 1); |
370 |
delete_policy(); |
371 |
mkdir2(filename); |
372 |
show_result(rmdir(filename), 0); |
373 |
rmdir2(filename); |
374 |
} |
375 |
|
376 |
policy = "allow_unlink /tmp/unlink_test"; |
377 |
if (write_policy()) { |
378 |
filename = "/tmp/unlink_test"; |
379 |
create2(filename); |
380 |
show_result(unlink(filename), 1); |
381 |
delete_policy(); |
382 |
create2(filename); |
383 |
show_result(unlink(filename), 0); |
384 |
unlink2(filename); |
385 |
} |
386 |
|
387 |
policy = "allow_symlink /tmp/symlink_source_test"; |
388 |
if (write_policy()) { |
389 |
filename = "/tmp/symlink_source_test"; |
390 |
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
391 |
delete_policy(); |
392 |
unlink2(filename); |
393 |
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
394 |
} |
395 |
|
396 |
policy = "allow_link /tmp/link_source_test /tmp/link_dest_test"; |
397 |
if (write_policy()) { |
398 |
filename = "/tmp/link_source_test"; |
399 |
create2(filename); |
400 |
show_result(link(filename, "/tmp/link_dest_test"), 1); |
401 |
delete_policy(); |
402 |
unlink2("/tmp/link_dest_test"); |
403 |
show_result(link(filename, "/tmp/link_dest_test"), 0); |
404 |
unlink2(filename); |
405 |
} |
406 |
|
407 |
policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test"; |
408 |
if (write_policy()) { |
409 |
filename = "/tmp/rename_source_test"; |
410 |
create2(filename); |
411 |
show_result(rename(filename, "/tmp/rename_dest_test"), 1); |
412 |
delete_policy(); |
413 |
unlink2("/tmp/rename_dest_test"); |
414 |
create2(filename); |
415 |
show_result(rename(filename, "/tmp/rename_dest_test"), 0); |
416 |
unlink2(filename); |
417 |
} |
418 |
|
419 |
policy = "allow_mksock /tmp/socket_test"; |
420 |
if (write_policy()) { |
421 |
struct sockaddr_un addr; |
422 |
int fd; |
423 |
filename = "/tmp/socket_test"; |
424 |
memset(&addr, 0, sizeof(addr)); |
425 |
addr.sun_family = AF_UNIX; |
426 |
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1); |
427 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
428 |
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 1); |
429 |
if (fd != EOF) close(fd); |
430 |
delete_policy(); |
431 |
unlink2(filename); |
432 |
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
433 |
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), 0); |
434 |
if (fd != EOF) close(fd); |
435 |
} |
436 |
|
437 |
filename = "/tmp/rewrite_test"; |
438 |
create2(filename); |
439 |
policy = "allow_read/write /tmp/rewrite_test"; |
440 |
if (write_policy()) { |
441 |
char *cp = "deny_rewrite /tmp/rewrite_test\n"; |
442 |
write(exception_fd, cp, strlen(cp)); |
443 |
policy = "allow_truncate /tmp/rewrite_test"; |
444 |
if (write_policy()) { |
445 |
int fd; |
446 |
|
447 |
fd = open(filename, O_RDONLY); |
448 |
show_result(fd, 1); |
449 |
if (fd != EOF) close(fd); |
450 |
|
451 |
fd = open(filename, O_WRONLY | O_APPEND); |
452 |
show_result(fd, 1); |
453 |
if (fd != EOF) close(fd); |
454 |
|
455 |
fd = open(filename, O_WRONLY); |
456 |
show_result(fd, 0); |
457 |
if (fd != EOF) close(fd); |
458 |
|
459 |
fd = open(filename, O_WRONLY | O_TRUNC); |
460 |
show_result(fd, 0); |
461 |
if (fd != EOF) close(fd); |
462 |
|
463 |
fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND); |
464 |
show_result(fd, 0); |
465 |
if (fd != EOF) close(fd); |
466 |
|
467 |
show_result(truncate(filename, 0), 0); |
468 |
|
469 |
cp = "255-MAC_FOR_FILE=disabled\n"; |
470 |
write(profile_fd, cp, strlen(cp)); |
471 |
fd = open(filename, O_WRONLY | O_APPEND); |
472 |
cp = "255-MAC_FOR_FILE=enforcing\n"; |
473 |
write(profile_fd, cp, strlen(cp)); |
474 |
show_result(ftruncate(fd, 0), 0); |
475 |
|
476 |
show_result(fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_APPEND), 0); |
477 |
if (fd != EOF) close(fd); |
478 |
|
479 |
delete_policy(); |
480 |
} |
481 |
policy = "allow_read/write /tmp/rewrite_test"; |
482 |
delete_policy(); |
483 |
cp = "delete deny_rewrite /tmp/rewrite_test\n"; |
484 |
write(exception_fd, cp, strlen(cp)); |
485 |
} |
486 |
unlink2(filename); |
487 |
} |
488 |
|
489 |
int main(int argc, char *argv[]) { |
490 |
char *cp; |
491 |
Init(); |
492 |
profile_fd = open(proc_policy_profile, O_WRONLY); |
493 |
domain_fd = open(proc_policy_domain_policy, O_WRONLY); |
494 |
exception_fd = open(proc_policy_exception_policy, O_WRONLY); |
495 |
{ |
496 |
int self_fd = open(proc_policy_self_domain, O_RDONLY); |
497 |
memset(self_domain, 0, sizeof(self_domain)); |
498 |
read(self_fd, self_domain, sizeof(self_domain) - 1); |
499 |
close(self_fd); |
500 |
write(domain_fd, self_domain, strlen(self_domain)); |
501 |
cp = " /bin/true\n"; |
502 |
write(domain_fd, cp, strlen(cp)); |
503 |
write(domain_fd, self_domain, strlen(self_domain)); |
504 |
write(domain_fd, "\n", 1); |
505 |
cp = "use_profile 255\n"; |
506 |
write(domain_fd, cp, strlen(cp)); |
507 |
} |
508 |
cp = "255-MAX_REJECT_LOG=1024\n"; |
509 |
write(profile_fd, cp, strlen(cp)); |
510 |
StageFileTest(); |
511 |
cp = "use_profile 0\n"; |
512 |
write(domain_fd, cp, strlen(cp)); |
513 |
ClearStatus(); |
514 |
return 0; |
515 |
} |