1 |
kumaneko |
1066 |
/* |
2 |
|
|
* tomoyo_file_test.c |
3 |
|
|
* |
4 |
|
|
* Testing program for fs/tomoyo_file.c |
5 |
|
|
* |
6 |
kumaneko |
2030 |
* Copyright (C) 2005-2009 NTT DATA CORPORATION |
7 |
kumaneko |
1066 |
* |
8 |
kumaneko |
2943 |
* Version: 1.7.0-pre 2009/08/24 |
9 |
kumaneko |
1066 |
* |
10 |
|
|
*/ |
11 |
|
|
#include "include.h" |
12 |
|
|
|
13 |
|
|
static const char *policy = ""; |
14 |
|
|
|
15 |
kumaneko |
2943 |
#if 0 |
16 |
kumaneko |
1726 |
static int write_policy(void) |
17 |
|
|
{ |
18 |
kumaneko |
1066 |
FILE *fp; |
19 |
|
|
char buffer[8192]; |
20 |
|
|
int domain_found = 0; |
21 |
|
|
int policy_found = 0; |
22 |
|
|
memset(buffer, 0, sizeof(buffer)); |
23 |
kumaneko |
2943 |
set_profile(0, "file::open"); |
24 |
kumaneko |
1066 |
fp = fopen(proc_policy_domain_policy, "r"); |
25 |
kumaneko |
2943 |
set_profile(3, "file::open"); |
26 |
kumaneko |
2908 |
fprintf(domain_fp, "%s\n", policy); |
27 |
kumaneko |
1066 |
if (!fp) { |
28 |
|
|
printf("%s : BUG: policy read failed\n", policy); |
29 |
|
|
return 0; |
30 |
|
|
} |
31 |
|
|
while (fgets(buffer, sizeof(buffer) - 1, fp)) { |
32 |
kumaneko |
2908 |
char *cp = strchr(buffer, '\n'); |
33 |
kumaneko |
1726 |
if (cp) |
34 |
|
|
*cp = '\0'; |
35 |
|
|
if (!strncmp(buffer, "<kernel>", 8)) |
36 |
|
|
domain_found = !strcmp(self_domain, buffer); |
37 |
kumaneko |
2908 |
if (!domain_found) |
38 |
|
|
continue; |
39 |
|
|
/* printf("<%s>\n", buffer); */ |
40 |
|
|
if (!strcmp(buffer, policy)) { |
41 |
|
|
policy_found = 1; |
42 |
|
|
break; |
43 |
kumaneko |
1066 |
} |
44 |
|
|
} |
45 |
|
|
fclose(fp); |
46 |
|
|
if (!policy_found) { |
47 |
|
|
printf("%s : BUG: policy write failed\n", policy); |
48 |
|
|
return 0; |
49 |
|
|
} |
50 |
|
|
errno = 0; |
51 |
|
|
return 1; |
52 |
|
|
} |
53 |
kumaneko |
2943 |
#endif |
54 |
kumaneko |
1066 |
|
55 |
kumaneko |
1726 |
static void show_result(int result, char should_success) |
56 |
|
|
{ |
57 |
kumaneko |
1066 |
int err = errno; |
58 |
|
|
printf("%s : ", policy); |
59 |
|
|
if (should_success) { |
60 |
kumaneko |
1726 |
if (result != EOF) |
61 |
|
|
printf("OK\n"); |
62 |
|
|
else |
63 |
|
|
printf("FAILED: %s\n", strerror(err)); |
64 |
kumaneko |
1066 |
} else { |
65 |
|
|
if (result == EOF) { |
66 |
kumaneko |
1726 |
if (err == EPERM) |
67 |
|
|
printf("OK: Permission denied.\n"); |
68 |
|
|
else |
69 |
|
|
printf("FAILED: %s\n", strerror(err)); |
70 |
kumaneko |
1066 |
} else { |
71 |
|
|
printf("BUG: didn't fail.\n"); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
kumaneko |
1726 |
static void create2(const char *pathname) |
77 |
|
|
{ |
78 |
kumaneko |
2943 |
set_profile(0, "file::create"); |
79 |
|
|
set_profile(0, "file::open"); |
80 |
kumaneko |
1066 |
close(creat(pathname, 0600)); |
81 |
kumaneko |
2943 |
set_profile(3, "file::create"); |
82 |
|
|
set_profile(3, "file::open"); |
83 |
kumaneko |
1148 |
errno = 0; |
84 |
kumaneko |
1066 |
} |
85 |
|
|
|
86 |
kumaneko |
1726 |
static void mkdir2(const char *pathname) |
87 |
|
|
{ |
88 |
kumaneko |
2943 |
set_profile(0, "file::mkdir"); |
89 |
kumaneko |
1066 |
mkdir(pathname, 0600); |
90 |
kumaneko |
2943 |
set_profile(3, "file::mkdir"); |
91 |
kumaneko |
1148 |
errno = 0; |
92 |
kumaneko |
1066 |
} |
93 |
|
|
|
94 |
kumaneko |
1726 |
static void unlink2(const char *pathname) |
95 |
|
|
{ |
96 |
kumaneko |
2943 |
set_profile(0, "file::unlink"); |
97 |
kumaneko |
1066 |
unlink(pathname); |
98 |
kumaneko |
2943 |
set_profile(3, "file::unlink"); |
99 |
kumaneko |
1148 |
errno = 0; |
100 |
kumaneko |
1726 |
} |
101 |
kumaneko |
1066 |
|
102 |
kumaneko |
1726 |
static void rmdir2(const char *pathname) |
103 |
|
|
{ |
104 |
kumaneko |
2943 |
set_profile(0, "file::rmdir"); |
105 |
kumaneko |
1066 |
rmdir(pathname); |
106 |
kumaneko |
2943 |
set_profile(3, "file::rmdir"); |
107 |
kumaneko |
1148 |
errno = 0; |
108 |
kumaneko |
1066 |
} |
109 |
|
|
|
110 |
kumaneko |
2563 |
static void mkfifo2(const char *pathname) |
111 |
|
|
{ |
112 |
kumaneko |
2943 |
set_profile(0, "file::mkfifo"); |
113 |
kumaneko |
2563 |
mkfifo(pathname, 0600); |
114 |
kumaneko |
2943 |
set_profile(3, "file::mkfifo"); |
115 |
kumaneko |
2563 |
errno = 0; |
116 |
|
|
} |
117 |
|
|
|
118 |
kumaneko |
1744 |
static void stage_file_test(void) |
119 |
kumaneko |
1726 |
{ |
120 |
kumaneko |
2943 |
static int name[] = { CTL_NET, NET_IPV4, NET_IPV4_LOCAL_PORT_RANGE }; |
121 |
|
|
int buffer[2] = { 32768, 61000 }; |
122 |
|
|
size_t size = sizeof(buffer); |
123 |
|
|
int pipe_fd[2] = { EOF, EOF }; |
124 |
|
|
int err = 0; |
125 |
|
|
int fd; |
126 |
|
|
char pbuffer[1024]; |
127 |
|
|
struct stat sbuf; |
128 |
|
|
struct sockaddr_un addr; |
129 |
|
|
struct ifreq ifreq; |
130 |
kumaneko |
1066 |
char *filename = ""; |
131 |
kumaneko |
2943 |
set_profile(3, "file::execute"); |
132 |
|
|
set_profile(3, "file::open"); |
133 |
|
|
set_profile(3, "file::create"); |
134 |
|
|
set_profile(3, "file::unlink"); |
135 |
|
|
set_profile(3, "file::mkdir"); |
136 |
|
|
set_profile(3, "file::rmdir"); |
137 |
|
|
set_profile(3, "file::mkfifo"); |
138 |
|
|
set_profile(3, "file::mksock"); |
139 |
|
|
set_profile(3, "file::truncate"); |
140 |
|
|
set_profile(3, "file::symlink"); |
141 |
|
|
set_profile(3, "file::rewrite"); |
142 |
|
|
set_profile(3, "file::mkblock"); |
143 |
|
|
set_profile(3, "file::mkchar"); |
144 |
|
|
set_profile(3, "file::link"); |
145 |
|
|
set_profile(3, "file::rename"); |
146 |
|
|
set_profile(3, "file::chmod"); |
147 |
|
|
set_profile(3, "file::chown"); |
148 |
|
|
set_profile(3, "file::chgrp"); |
149 |
|
|
set_profile(3, "file::ioctl"); |
150 |
|
|
set_profile(3, "file::chroot"); |
151 |
|
|
set_profile(3, "file::mount"); |
152 |
|
|
set_profile(3, "file::umount"); |
153 |
|
|
set_profile(3, "file::pivot_root"); |
154 |
|
|
|
155 |
kumaneko |
1726 |
policy = "allow_read /proc/sys/net/ipv4/ip_local_port_range " |
156 |
|
|
"if task.uid=0 task.gid=0"; |
157 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
158 |
|
|
show_result(sysctl(name, 3, buffer, &size, 0, 0), 1); |
159 |
|
|
write_domain_policy(policy, 1); |
160 |
|
|
show_result(sysctl(name, 3, buffer, &size, 0, 0), 0); |
161 |
|
|
|
162 |
kumaneko |
1726 |
policy = "allow_write /proc/sys/net/ipv4/ip_local_port_range " |
163 |
|
|
"if task.euid=0 0=0 1-100=10-1000"; |
164 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
165 |
|
|
show_result(sysctl(name, 3, 0, 0, buffer, size), 1); |
166 |
|
|
write_domain_policy(policy, 1); |
167 |
|
|
show_result(sysctl(name, 3, 0, 0, buffer, size), 0); |
168 |
|
|
|
169 |
kumaneko |
1726 |
policy = "allow_read/write /proc/sys/net/ipv4/ip_local_port_range " |
170 |
|
|
"if 1!=10-100"; |
171 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
172 |
|
|
show_result(sysctl(name, 3, buffer, &size, buffer, size), 1); |
173 |
|
|
write_domain_policy(policy, 1); |
174 |
|
|
show_result(sysctl(name, 3, buffer, &size, buffer, size), 0); |
175 |
|
|
|
176 |
kumaneko |
1726 |
policy = "allow_read /bin/true " |
177 |
|
|
"if path1.uid=0 path1.parent.uid=0 10=10-100"; |
178 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
179 |
|
|
show_result(uselib("/bin/true"), 1); |
180 |
|
|
write_domain_policy(policy, 1); |
181 |
|
|
show_result(uselib("/bin/true"), 0); |
182 |
|
|
|
183 |
kumaneko |
1664 |
policy = "allow_execute /bin/true if task.uid!=10 path1.parent.uid=0"; |
184 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
185 |
|
|
fflush(stdout); |
186 |
|
|
fflush(stderr); |
187 |
|
|
pipe(pipe_fd); |
188 |
|
|
if (fork() == 0) { |
189 |
|
|
execl("/bin/true", "/bin/true", NULL); |
190 |
|
|
err = errno; |
191 |
|
|
write(pipe_fd[1], &err, sizeof(err)); |
192 |
|
|
_exit(0); |
193 |
kumaneko |
1066 |
} |
194 |
kumaneko |
2943 |
close(pipe_fd[1]); |
195 |
|
|
read(pipe_fd[0], &err, sizeof(err)); |
196 |
|
|
close(pipe_fd[0]); |
197 |
|
|
wait(NULL); |
198 |
|
|
errno = err; |
199 |
|
|
show_result(err ? EOF : 0, 1); |
200 |
|
|
write_domain_policy(policy, 1); |
201 |
|
|
fflush(stdout); |
202 |
|
|
fflush(stderr); |
203 |
|
|
pipe(pipe_fd); |
204 |
|
|
if (fork() == 0) { |
205 |
|
|
execl("/bin/true", "/bin/true", NULL); |
206 |
|
|
err = errno; |
207 |
|
|
write(pipe_fd[1], &err, sizeof(err)); |
208 |
|
|
_exit(0); |
209 |
|
|
} |
210 |
|
|
close(pipe_fd[1]); |
211 |
|
|
read(pipe_fd[0], &err, sizeof(err)); |
212 |
|
|
close(pipe_fd[0]); |
213 |
|
|
wait(NULL); |
214 |
|
|
errno = err; |
215 |
|
|
show_result(err ? EOF : 0, 0); |
216 |
kumaneko |
1066 |
|
217 |
kumaneko |
1996 |
policy = "allow_read /dev/null if path1.type=char path1.dev_major=1 " |
218 |
|
|
"path1.dev_minor=3"; |
219 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
220 |
|
|
fd = open("/dev/null", O_RDONLY); |
221 |
|
|
show_result(fd, 1); |
222 |
|
|
if (fd != EOF) |
223 |
|
|
close(fd); |
224 |
|
|
write_domain_policy(policy, 1); |
225 |
|
|
fd = open("/dev/null", O_RDONLY); |
226 |
|
|
show_result(fd, 0); |
227 |
|
|
if (fd != EOF) |
228 |
|
|
close(fd); |
229 |
|
|
|
230 |
kumaneko |
1995 |
policy = "allow_read /dev/null if path1.perm=0666"; |
231 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
232 |
|
|
fd = open("/dev/null", O_RDONLY); |
233 |
|
|
show_result(fd, 1); |
234 |
|
|
if (fd != EOF) |
235 |
|
|
close(fd); |
236 |
|
|
write_domain_policy(policy, 1); |
237 |
|
|
fd = open("/dev/null", O_RDONLY); |
238 |
|
|
show_result(fd, 0); |
239 |
|
|
if (fd != EOF) |
240 |
|
|
close(fd); |
241 |
kumaneko |
1995 |
|
242 |
kumaneko |
1996 |
policy = "allow_read /dev/null if path1.perm!=0777"; |
243 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
244 |
|
|
fd = open("/dev/null", O_RDONLY); |
245 |
|
|
show_result(fd, 1); |
246 |
|
|
if (fd != EOF) |
247 |
|
|
close(fd); |
248 |
|
|
write_domain_policy(policy, 1); |
249 |
|
|
fd = open("/dev/null", O_RDONLY); |
250 |
|
|
show_result(fd, 0); |
251 |
|
|
if (fd != EOF) |
252 |
|
|
close(fd); |
253 |
kumaneko |
1995 |
|
254 |
kumaneko |
1996 |
policy = "allow_read /dev/null if path1.perm=owner_read " |
255 |
|
|
"path1.perm=owner_write path1.perm!=owner_execute " |
256 |
|
|
"path1.perm=group_read path1.perm=group_write " |
257 |
|
|
"path1.perm!=group_execute path1.perm=others_read " |
258 |
|
|
"path1.perm=others_write path1.perm!=others_execute " |
259 |
|
|
"path1.perm!=setuid path1.perm!=setgid path1.perm!=sticky"; |
260 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
261 |
|
|
fd = open("/dev/null", O_RDONLY); |
262 |
|
|
show_result(fd, 1); |
263 |
|
|
if (fd != EOF) |
264 |
|
|
close(fd); |
265 |
|
|
write_domain_policy(policy, 1); |
266 |
|
|
fd = open("/dev/null", O_RDONLY); |
267 |
|
|
show_result(fd, 0); |
268 |
|
|
if (fd != EOF) |
269 |
|
|
close(fd); |
270 |
kumaneko |
1996 |
|
271 |
kumaneko |
2943 |
set_profile(3, "file::mkfifo"); |
272 |
|
|
policy = "allow_mkfifo /tmp/mknod_fifo_test 0644 " |
273 |
kumaneko |
1996 |
"if path1.parent.perm=01777 path1.parent.perm=sticky " |
274 |
|
|
"path1.parent.uid=0 path1.parent.gid=0"; |
275 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
276 |
|
|
filename = "/tmp/mknod_fifo_test"; |
277 |
|
|
show_result(mknod(filename, S_IFIFO | 0644, 0), 1); |
278 |
|
|
write_domain_policy(policy, 1); |
279 |
|
|
unlink2(filename); |
280 |
|
|
show_result(mknod(filename, S_IFIFO | 0644, 0), 0); |
281 |
kumaneko |
1996 |
|
282 |
kumaneko |
2943 |
memset(pbuffer, 0, sizeof(pbuffer)); |
283 |
|
|
memset(&sbuf, 0, sizeof(sbuf)); |
284 |
|
|
filename = "/dev/null"; |
285 |
|
|
stat(filename, &sbuf); |
286 |
|
|
snprintf(pbuffer, sizeof(pbuffer) - 1, |
287 |
|
|
"allow_write %s if path1.major=%u path1.minor=%u", |
288 |
|
|
filename, (unsigned int) MAJOR(sbuf.st_dev), |
289 |
|
|
(unsigned int) MINOR(sbuf.st_dev)); |
290 |
|
|
policy = pbuffer; |
291 |
|
|
write_domain_policy(policy, 0); |
292 |
|
|
fd = open(filename, O_WRONLY); |
293 |
|
|
show_result(fd, 1); |
294 |
|
|
if (fd != EOF) |
295 |
|
|
close(fd); |
296 |
|
|
write_domain_policy(policy, 1); |
297 |
|
|
fd = open(filename, O_WRONLY); |
298 |
|
|
show_result(fd, 0); |
299 |
|
|
if (fd != EOF) |
300 |
|
|
close(fd); |
301 |
kumaneko |
1996 |
|
302 |
kumaneko |
2563 |
policy = "allow_read/write /tmp/fifo if path1.type=fifo"; |
303 |
|
|
mkfifo2("/tmp/fifo"); |
304 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
305 |
|
|
fd = open("/tmp/fifo", O_RDWR); |
306 |
|
|
show_result(fd, 1); |
307 |
|
|
if (fd != EOF) |
308 |
|
|
close(fd); |
309 |
|
|
write_domain_policy(policy, 1); |
310 |
|
|
fd = open("/tmp/fifo", O_RDWR); |
311 |
|
|
show_result(fd, 0); |
312 |
|
|
if (fd != EOF) |
313 |
|
|
close(fd); |
314 |
kumaneko |
1996 |
|
315 |
kumaneko |
1664 |
policy = "allow_read /dev/null if path1.parent.ino=path1.parent.ino"; |
316 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
317 |
|
|
fd = open("/dev/null", O_RDONLY); |
318 |
|
|
show_result(fd, 1); |
319 |
|
|
if (fd != EOF) |
320 |
|
|
close(fd); |
321 |
|
|
write_domain_policy(policy, 1); |
322 |
|
|
fd = open("/dev/null", O_RDONLY); |
323 |
|
|
show_result(fd, 0); |
324 |
|
|
if (fd != EOF) |
325 |
|
|
close(fd); |
326 |
kumaneko |
1066 |
|
327 |
kumaneko |
1664 |
policy = "allow_write /dev/null if path1.uid=path1.gid"; |
328 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
329 |
|
|
fd = open("/dev/null", O_WRONLY); |
330 |
|
|
show_result(fd, 1); |
331 |
|
|
if (fd != EOF) |
332 |
|
|
close(fd); |
333 |
|
|
write_domain_policy(policy, 1); |
334 |
|
|
fd = open("/dev/null", O_WRONLY); |
335 |
|
|
show_result(fd, 0); |
336 |
|
|
if (fd != EOF) |
337 |
|
|
close(fd); |
338 |
kumaneko |
1066 |
|
339 |
kumaneko |
1664 |
policy = "allow_read/write /dev/null if task.uid=path1.parent.uid"; |
340 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
341 |
|
|
fd = open("/dev/null", O_RDWR); |
342 |
|
|
show_result(fd, 1); |
343 |
|
|
if (fd != EOF) |
344 |
|
|
close(fd); |
345 |
|
|
write_domain_policy(policy, 1); |
346 |
|
|
fd = open("/dev/null", O_RDWR); |
347 |
|
|
show_result(fd, 0); |
348 |
|
|
if (fd != EOF) |
349 |
|
|
close(fd); |
350 |
kumaneko |
1066 |
|
351 |
kumaneko |
2943 |
policy = "allow_create /tmp/open_test 0644 if path1.parent.uid=task.uid"; |
352 |
|
|
write_domain_policy(policy, 0); |
353 |
|
|
policy = "allow_write /tmp/open_test if path1.parent.uid=0"; |
354 |
|
|
write_domain_policy(policy, 0); |
355 |
|
|
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644); |
356 |
|
|
show_result(fd, 1); |
357 |
|
|
if (fd != EOF) |
358 |
|
|
close(fd); |
359 |
|
|
unlink2("/tmp/open_test"); |
360 |
|
|
write_domain_policy(policy, 1); |
361 |
|
|
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644); |
362 |
|
|
show_result(fd, 0); |
363 |
|
|
if (fd != EOF) |
364 |
|
|
close(fd); |
365 |
|
|
unlink2("/tmp/open_test"); |
366 |
kumaneko |
1066 |
|
367 |
kumaneko |
2943 |
policy = "allow_create /tmp/open_test 0644 " |
368 |
|
|
"if path1.parent.uid=task.uid"; |
369 |
|
|
write_domain_policy(policy, 1); |
370 |
|
|
|
371 |
kumaneko |
1664 |
policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0"; |
372 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
373 |
|
|
policy = "allow_create /tmp/open_test 0644 if 0=0"; |
374 |
|
|
write_domain_policy(policy, 0); |
375 |
|
|
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644); |
376 |
|
|
show_result(fd, 1); |
377 |
|
|
if (fd != EOF) |
378 |
|
|
close(fd); |
379 |
|
|
unlink2("/tmp/open_test"); |
380 |
|
|
write_domain_policy(policy, 1); |
381 |
|
|
fd = open("/tmp/open_test", O_WRONLY | O_CREAT | O_EXCL, 0644); |
382 |
|
|
show_result(fd, 0); |
383 |
|
|
if (fd != EOF) |
384 |
|
|
close(fd); |
385 |
|
|
unlink2("/tmp/open_test"); |
386 |
|
|
policy = "allow_write /tmp/open_test if task.uid=0 path1.ino!=0"; |
387 |
|
|
write_domain_policy(policy, 1); |
388 |
kumaneko |
1066 |
|
389 |
|
|
filename = "/tmp/truncate_test"; |
390 |
|
|
create2(filename); |
391 |
|
|
|
392 |
kumaneko |
1664 |
policy = "allow_truncate /tmp/truncate_test if task.uid=path1.uid"; |
393 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
394 |
|
|
policy = "allow_write /tmp/truncate_test if 1!=100-1000000"; |
395 |
|
|
write_domain_policy(policy, 0); |
396 |
|
|
fd = open(filename, O_WRONLY | O_TRUNC); |
397 |
|
|
show_result(fd, 1); |
398 |
|
|
if (fd != EOF) |
399 |
|
|
close(fd); |
400 |
|
|
write_domain_policy(policy, 1); |
401 |
|
|
fd = open(filename, O_WRONLY | O_TRUNC); |
402 |
|
|
show_result(fd, 0); |
403 |
|
|
if (fd != EOF) |
404 |
|
|
close(fd); |
405 |
|
|
policy = "allow_truncate /tmp/truncate_test " |
406 |
|
|
"if task.uid=path1.uid"; |
407 |
|
|
write_domain_policy(policy, 1); |
408 |
kumaneko |
1066 |
|
409 |
|
|
policy = "allow_write /tmp/truncate_test"; |
410 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
411 |
|
|
policy = "allow_truncate /tmp/truncate_test"; |
412 |
|
|
write_domain_policy(policy, 0); |
413 |
|
|
fd = open(filename, O_WRONLY | O_TRUNC); |
414 |
|
|
show_result(fd, 1); |
415 |
|
|
if (fd != EOF) |
416 |
|
|
close(fd); |
417 |
|
|
write_domain_policy(policy, 1); |
418 |
|
|
fd = open(filename, O_WRONLY | O_TRUNC); |
419 |
|
|
show_result(fd, 0); |
420 |
|
|
if (fd != EOF) |
421 |
|
|
close(fd); |
422 |
|
|
policy = "allow_write /tmp/truncate_test"; |
423 |
|
|
write_domain_policy(policy, 1); |
424 |
kumaneko |
1726 |
|
425 |
kumaneko |
1066 |
policy = "allow_truncate /tmp/truncate_test"; |
426 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
427 |
|
|
show_result(truncate(filename, 0), 1); |
428 |
|
|
write_domain_policy(policy, 1); |
429 |
|
|
show_result(truncate(filename, 0), 0); |
430 |
kumaneko |
1066 |
|
431 |
|
|
policy = "allow_truncate /tmp/truncate_test"; |
432 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
433 |
|
|
set_profile(0, "file::open"); |
434 |
|
|
fd = open(filename, O_WRONLY); |
435 |
|
|
set_profile(3, "file::open"); |
436 |
|
|
show_result(ftruncate(fd, 0), 1); |
437 |
|
|
write_domain_policy(policy, 1); |
438 |
|
|
show_result(ftruncate(fd, 0), 0); |
439 |
|
|
if (fd != EOF) |
440 |
|
|
close(fd); |
441 |
kumaneko |
1726 |
|
442 |
kumaneko |
1066 |
unlink2(filename); |
443 |
kumaneko |
1726 |
|
444 |
kumaneko |
2943 |
policy = "allow_create /tmp/mknod_reg_test 0644"; |
445 |
|
|
write_domain_policy(policy, 0); |
446 |
|
|
filename = "/tmp/mknod_reg_test"; |
447 |
|
|
show_result(mknod(filename, S_IFREG | 0644, 0), 1); |
448 |
|
|
write_domain_policy(policy, 1); |
449 |
|
|
unlink2(filename); |
450 |
|
|
show_result(mknod(filename, S_IFREG | 0644, 0), 0); |
451 |
kumaneko |
1066 |
|
452 |
kumaneko |
2943 |
policy = "allow_mkchar /tmp/mknod_chr_test 0644 1 3"; |
453 |
|
|
write_domain_policy(policy, 0); |
454 |
|
|
filename = "/tmp/mknod_chr_test"; |
455 |
|
|
show_result(mknod(filename, S_IFCHR | 0644, MKDEV(1, 3)), 1); |
456 |
|
|
write_domain_policy(policy, 1); |
457 |
|
|
unlink2(filename); |
458 |
|
|
show_result(mknod(filename, S_IFCHR | 0644, MKDEV(1, 3)), 0); |
459 |
kumaneko |
1066 |
|
460 |
kumaneko |
2943 |
policy = "allow_mkblock /tmp/mknod_blk_test 0644 1 0"; |
461 |
|
|
write_domain_policy(policy, 0); |
462 |
|
|
filename = "/tmp/mknod_blk_test"; |
463 |
|
|
show_result(mknod(filename, S_IFBLK | 0644, MKDEV(1, 0)), 1); |
464 |
|
|
write_domain_policy(policy, 1); |
465 |
|
|
unlink2(filename); |
466 |
|
|
show_result(mknod(filename, S_IFBLK | 0644, MKDEV(1, 0)), 0); |
467 |
kumaneko |
1066 |
|
468 |
kumaneko |
2943 |
policy = "allow_mkfifo /tmp/mknod_fifo_test 0644"; |
469 |
|
|
write_domain_policy(policy, 0); |
470 |
|
|
filename = "/tmp/mknod_fifo_test"; |
471 |
|
|
show_result(mknod(filename, S_IFIFO | 0644, 0), 1); |
472 |
|
|
write_domain_policy(policy, 1); |
473 |
|
|
unlink2(filename); |
474 |
|
|
show_result(mknod(filename, S_IFIFO | 0644, 0), 0); |
475 |
kumaneko |
1066 |
|
476 |
kumaneko |
2943 |
policy = "allow_mksock /tmp/mknod_sock_test 0644"; |
477 |
|
|
write_domain_policy(policy, 0); |
478 |
|
|
filename = "/tmp/mknod_sock_test"; |
479 |
|
|
show_result(mknod(filename, S_IFSOCK | 0644, 0), 1); |
480 |
|
|
write_domain_policy(policy, 1); |
481 |
|
|
unlink2(filename); |
482 |
|
|
show_result(mknod(filename, S_IFSOCK | 0644, 0), 0); |
483 |
kumaneko |
1726 |
|
484 |
kumaneko |
2943 |
policy = "allow_mkdir /tmp/mkdir_test/ 0600"; |
485 |
|
|
write_domain_policy(policy, 0); |
486 |
|
|
filename = "/tmp/mkdir_test"; |
487 |
|
|
show_result(mkdir(filename, 0600), 1); |
488 |
|
|
write_domain_policy(policy, 1); |
489 |
|
|
rmdir2(filename); |
490 |
|
|
show_result(mkdir(filename, 0600), 0); |
491 |
kumaneko |
1726 |
|
492 |
kumaneko |
1066 |
policy = "allow_rmdir /tmp/rmdir_test/"; |
493 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
494 |
|
|
filename = "/tmp/rmdir_test"; |
495 |
|
|
mkdir2(filename); |
496 |
|
|
show_result(rmdir(filename), 1); |
497 |
|
|
write_domain_policy(policy, 1); |
498 |
|
|
mkdir2(filename); |
499 |
|
|
show_result(rmdir(filename), 0); |
500 |
|
|
rmdir2(filename); |
501 |
kumaneko |
1726 |
|
502 |
kumaneko |
1066 |
policy = "allow_unlink /tmp/unlink_test"; |
503 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
504 |
|
|
filename = "/tmp/unlink_test"; |
505 |
|
|
create2(filename); |
506 |
|
|
show_result(unlink(filename), 1); |
507 |
|
|
write_domain_policy(policy, 1); |
508 |
|
|
create2(filename); |
509 |
|
|
show_result(unlink(filename), 0); |
510 |
|
|
unlink2(filename); |
511 |
kumaneko |
1726 |
|
512 |
kumaneko |
1066 |
policy = "allow_symlink /tmp/symlink_source_test"; |
513 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
514 |
|
|
filename = "/tmp/symlink_source_test"; |
515 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
516 |
|
|
write_domain_policy(policy, 1); |
517 |
|
|
unlink2(filename); |
518 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
519 |
kumaneko |
1726 |
|
520 |
kumaneko |
2574 |
policy = "allow_symlink /tmp/symlink_source_test " |
521 |
|
|
"if symlink.target=\"/tmp/symlink_\\*_test\""; |
522 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
523 |
|
|
filename = "/tmp/symlink_source_test"; |
524 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
525 |
|
|
write_domain_policy(policy, 1); |
526 |
|
|
unlink2(filename); |
527 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
528 |
kumaneko |
2563 |
|
529 |
kumaneko |
2574 |
policy = "allow_symlink /tmp/symlink_source_test " |
530 |
|
|
"if task.uid=0 symlink.target=\"/tmp/symlink_\\*_test\""; |
531 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
532 |
|
|
filename = "/tmp/symlink_source_test"; |
533 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
534 |
|
|
write_domain_policy(policy, 1); |
535 |
|
|
unlink2(filename); |
536 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
537 |
kumaneko |
2563 |
|
538 |
kumaneko |
2574 |
policy = "allow_symlink /tmp/symlink_source_test " |
539 |
|
|
"if symlink.target!=\"\\*\""; |
540 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
541 |
|
|
filename = "/tmp/symlink_source_test"; |
542 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 1); |
543 |
|
|
write_domain_policy(policy, 1); |
544 |
|
|
unlink2(filename); |
545 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
546 |
kumaneko |
2563 |
|
547 |
kumaneko |
2574 |
policy = "allow_symlink /tmp/symlink_source_test " |
548 |
|
|
"if symlink.target!=\"/tmp/symlink_\\*_test\""; |
549 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
550 |
|
|
filename = "/tmp/symlink_source_test"; |
551 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
552 |
|
|
write_domain_policy(policy, 1); |
553 |
|
|
unlink2(filename); |
554 |
|
|
show_result(symlink("/tmp/symlink_dest_test", filename), 0); |
555 |
kumaneko |
2563 |
|
556 |
kumaneko |
1066 |
policy = "allow_link /tmp/link_source_test /tmp/link_dest_test"; |
557 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
558 |
|
|
filename = "/tmp/link_source_test"; |
559 |
|
|
create2(filename); |
560 |
|
|
show_result(link(filename, "/tmp/link_dest_test"), 1); |
561 |
|
|
write_domain_policy(policy, 1); |
562 |
|
|
unlink2("/tmp/link_dest_test"); |
563 |
|
|
show_result(link(filename, "/tmp/link_dest_test"), 0); |
564 |
|
|
unlink2(filename); |
565 |
kumaneko |
1066 |
|
566 |
|
|
policy = "allow_rename /tmp/rename_source_test /tmp/rename_dest_test"; |
567 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
568 |
|
|
filename = "/tmp/rename_source_test"; |
569 |
|
|
create2(filename); |
570 |
|
|
show_result(rename(filename, "/tmp/rename_dest_test"), 1); |
571 |
|
|
write_domain_policy(policy, 1); |
572 |
|
|
unlink2("/tmp/rename_dest_test"); |
573 |
|
|
create2(filename); |
574 |
|
|
show_result(rename(filename, "/tmp/rename_dest_test"), 0); |
575 |
|
|
unlink2(filename); |
576 |
kumaneko |
1066 |
|
577 |
kumaneko |
2943 |
policy = "allow_mksock /tmp/socket_test 0755"; |
578 |
|
|
write_domain_policy(policy, 0); |
579 |
|
|
filename = "/tmp/socket_test"; |
580 |
|
|
memset(&addr, 0, sizeof(addr)); |
581 |
|
|
addr.sun_family = AF_UNIX; |
582 |
|
|
strncpy(addr.sun_path, filename, sizeof(addr.sun_path) - 1); |
583 |
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
584 |
|
|
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), |
585 |
|
|
1); |
586 |
|
|
if (fd != EOF) |
587 |
|
|
close(fd); |
588 |
|
|
write_domain_policy(policy, 1); |
589 |
|
|
unlink2(filename); |
590 |
|
|
fd = socket(AF_UNIX, SOCK_STREAM, 0); |
591 |
|
|
show_result(bind(fd, (struct sockaddr *) &addr, sizeof(addr)), |
592 |
|
|
0); |
593 |
|
|
if (fd != EOF) |
594 |
|
|
close(fd); |
595 |
kumaneko |
1067 |
|
596 |
|
|
filename = "/tmp/rewrite_test"; |
597 |
|
|
create2(filename); |
598 |
|
|
policy = "allow_read/write /tmp/rewrite_test"; |
599 |
kumaneko |
2943 |
write_domain_policy(policy, 0); |
600 |
|
|
write_exception_policy("deny_rewrite /tmp/rewrite_test", 0); |
601 |
|
|
policy = "allow_truncate /tmp/rewrite_test"; |
602 |
|
|
write_domain_policy(policy, 0); |
603 |
kumaneko |
1067 |
|
604 |
kumaneko |
2943 |
fd = open(filename, O_RDONLY); |
605 |
|
|
show_result(fd, 1); |
606 |
|
|
if (fd != EOF) |
607 |
|
|
close(fd); |
608 |
|
|
|
609 |
|
|
fd = open(filename, O_WRONLY | O_APPEND); |
610 |
|
|
show_result(fd, 1); |
611 |
|
|
if (fd != EOF) |
612 |
|
|
close(fd); |
613 |
|
|
|
614 |
|
|
fd = open(filename, O_WRONLY); |
615 |
|
|
show_result(fd, 0); |
616 |
|
|
if (fd != EOF) |
617 |
|
|
close(fd); |
618 |
|
|
|
619 |
|
|
fd = open(filename, O_WRONLY | O_TRUNC); |
620 |
|
|
show_result(fd, 0); |
621 |
|
|
if (fd != EOF) |
622 |
|
|
close(fd); |
623 |
|
|
|
624 |
|
|
fd = open(filename, O_WRONLY | O_TRUNC | O_APPEND); |
625 |
|
|
show_result(fd, 0); |
626 |
|
|
if (fd != EOF) |
627 |
|
|
close(fd); |
628 |
|
|
|
629 |
|
|
show_result(truncate(filename, 0), 0); |
630 |
|
|
|
631 |
|
|
set_profile(0, "file::open"); |
632 |
|
|
fd = open(filename, O_WRONLY | O_APPEND); |
633 |
|
|
set_profile(3, "file::open"); |
634 |
|
|
show_result(ftruncate(fd, 0), 0); |
635 |
|
|
|
636 |
|
|
show_result(fcntl(fd, F_SETFL, |
637 |
|
|
fcntl(fd, F_GETFL) & ~O_APPEND), 0); |
638 |
|
|
if (fd != EOF) |
639 |
|
|
close(fd); |
640 |
|
|
|
641 |
|
|
write_domain_policy(policy, 1); |
642 |
kumaneko |
1726 |
|
643 |
kumaneko |
2943 |
policy = "allow_read/write /tmp/rewrite_test"; |
644 |
|
|
write_domain_policy(policy, 1); |
645 |
|
|
write_exception_policy("deny_rewrite /tmp/rewrite_test", 1); |
646 |
kumaneko |
1726 |
|
647 |
kumaneko |
1067 |
unlink2(filename); |
648 |
kumaneko |
2301 |
|
649 |
kumaneko |
2943 |
policy = "allow_ioctl socket:[family=2:type=2:protocol=17] " |
650 |
|
|
"35122-35124 if task.uid=0"; |
651 |
|
|
write_domain_policy(policy, 0); |
652 |
|
|
fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); |
653 |
|
|
memset(&ifreq, 0, sizeof(ifreq)); |
654 |
|
|
snprintf(ifreq.ifr_name, sizeof(ifreq.ifr_name) - 1, |
655 |
|
|
"lo"); |
656 |
|
|
show_result(ioctl(fd, 35123, &ifreq), 1); |
657 |
|
|
write_domain_policy(policy, 1); |
658 |
|
|
policy = "allow_ioctl " |
659 |
|
|
"socket:[family=2:type=2:protocol=17] 0-35122"; |
660 |
|
|
write_domain_policy(policy, 0); |
661 |
|
|
show_result(ioctl(fd, 35123, &ifreq), 0); |
662 |
|
|
write_domain_policy(policy, 1); |
663 |
|
|
if (fd != EOF) |
664 |
|
|
close(fd); |
665 |
kumaneko |
1066 |
} |
666 |
|
|
|
667 |
kumaneko |
1726 |
int main(int argc, char *argv[]) |
668 |
|
|
{ |
669 |
kumaneko |
1744 |
ccs_test_init(); |
670 |
kumaneko |
2908 |
fprintf(domain_fp, "%s /bin/true\n", self_domain); |
671 |
|
|
fprintf(domain_fp, "use_profile 255\n"); |
672 |
|
|
fprintf(domain_fp, "select pid=%u\n", pid); |
673 |
|
|
fprintf(profile_fp, "255-MAX_REJECT_LOG=1024\n"); |
674 |
kumaneko |
1744 |
stage_file_test(); |
675 |
kumaneko |
2908 |
fprintf(domain_fp, "use_profile 0\n"); |
676 |
kumaneko |
1744 |
clear_status(); |
677 |
kumaneko |
1066 |
return 0; |
678 |
|
|
} |