Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(767)

Side by Side Diff: tests/limited_file_access/limited_file_access.cc

Issue 1690983004: Extended restricted filesystem to support relative paths. (Closed) Base URL: https://chromium.googlesource.com/native_client/src/native_client.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/trusted/service_runtime/sel_main.c ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The Native Client Authors. All rights reserved. 2 * Copyright 2016 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be 3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file. 4 * found in the LICENSE file.
5 */ 5 */
6 6
7 /* 7 /*
8 * NaCl tests for limited file access 8 * NaCl tests for limited file access
9 */ 9 */
10 10
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 67
68 ASSERT_EQ(test_string_len, write(fd, test_string, test_string_len)); 68 ASSERT_EQ(test_string_len, write(fd, test_string, test_string_len));
69 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET)); 69 ASSERT_EQ(0, lseek(fd, 0, SEEK_SET));
70 ASSERT_EQ(test_string_len, read(fd, buf, test_string_len)); 70 ASSERT_EQ(test_string_len, read(fd, buf, test_string_len));
71 ASSERT_EQ(0, memcmp(buf, test_string, test_string_len)); 71 ASSERT_EQ(0, memcmp(buf, test_string, test_string_len));
72 ASSERT_EQ(0, close(fd)); 72 ASSERT_EQ(0, close(fd));
73 } 73 }
74 74
75 void test_directory_walk() { 75 void test_directory_walk() {
76 // Attempt to walk down valid directory structure (and back again). 76 // Attempt to walk down valid directory structure (and back again).
77 ASSERT_EQ_MSG(chdir("/"), 0, "chdir() failed");
78
79 char dirname[PATH_MAX]; 77 char dirname[PATH_MAX];
80 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed"); 78 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed");
81 ASSERT_EQ(strcmp(dirname, "/"), 0); 79 ASSERT_EQ(strcmp(dirname, "/"), 0);
82 80
81 ASSERT_EQ_MSG(chdir("."), 0, "chdir() failed");
82 ASSERT_EQ_MSG(chdir("/"), 0, "chdir() failed");
83
83 DIR *d = opendir(dirname); 84 DIR *d = opendir(dirname);
84 ASSERT_NE_MSG(d, NULL, "opendir() failed"); 85 ASSERT_NE_MSG(d, NULL, "opendir() failed");
85 int count = 0; 86 int count = 0;
86 struct dirent *ent; 87 struct dirent *ent;
87 88
88 /* 89 /*
89 * We expect to see: 90 * We expect to see:
90 * temp_file 91 * temp_file
91 * temp_symlink 92 * temp_symlink
92 * sub_temp_dir 93 * sub_temp_dir
(...skipping 27 matching lines...) Expand all
120 } 121 }
121 ASSERT_EQ_MSG(closedir(d), 0, "closedir() failed"); 122 ASSERT_EQ_MSG(closedir(d), 0, "closedir() failed");
122 123
123 ASSERT(temp_file_seen); 124 ASSERT(temp_file_seen);
124 ASSERT(temp_symlink_seen); 125 ASSERT(temp_symlink_seen);
125 ASSERT(sub_temp_dir_seen); 126 ASSERT(sub_temp_dir_seen);
126 ASSERT(parent_directory_seen); 127 ASSERT(parent_directory_seen);
127 ASSERT(current_directory_seen); 128 ASSERT(current_directory_seen);
128 ASSERT_EQ(count, 5); 129 ASSERT_EQ(count, 5);
129 130
131 // Chdir with relative path name
132 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_name), 0, "chdir() failed");
133 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed");
134 ASSERT_EQ(strcmp(dirname, g_temp_sub_dir_path), 0);
135
136 // Chdir with absolute path name
130 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_path), 0, "chdir() failed"); 137 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_path), 0, "chdir() failed");
131 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed"); 138 ASSERT_NE_MSG(getcwd(dirname, PATH_MAX), NULL, "getcwd() failed");
132 ASSERT_EQ(strcmp(dirname, g_temp_sub_dir_path), 0); 139 ASSERT_EQ(strcmp(dirname, g_temp_sub_dir_path), 0);
140
133 d = opendir(dirname); 141 d = opendir(dirname);
134 count = 0; 142 count = 0;
135 143
136 /* 144 /*
137 * We expect to see: 145 * We expect to see:
138 * temp_sub_file 146 * temp_sub_file
139 * .. 147 * ..
140 * . 148 * .
141 */ 149 */
142 150
(...skipping 30 matching lines...) Expand all
173 181
174 void test_new_directory_access() { 182 void test_new_directory_access() {
175 // Create a new directory, removes that directory. 183 // Create a new directory, removes that directory.
176 mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR; 184 mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR;
177 ASSERT_EQ(mkdir("/test_dir", mode), 0); 185 ASSERT_EQ(mkdir("/test_dir", mode), 0);
178 ASSERT_EQ(rmdir("/test_dir"), 0); 186 ASSERT_EQ(rmdir("/test_dir"), 0);
179 187
180 ASSERT_EQ(mkdir("/test_dir/", mode), 0); 188 ASSERT_EQ(mkdir("/test_dir/", mode), 0);
181 ASSERT_EQ(rmdir("/test_dir/"), 0); 189 ASSERT_EQ(rmdir("/test_dir/"), 0);
182 190
183 // Cannot make directory using relative path. 191 // Test that relative paths can also be used.
184 ASSERT_EQ(mkdir("test_dir/", mode), -1); 192 ASSERT_EQ(mkdir("test_dir", mode), 0);
185 ASSERT_EQ(errno, EACCES); 193 ASSERT_EQ(rmdir("test_dir"), 0);
186 194
187 char file_name[PATH_MAX]; 195 char file_name[PATH_MAX];
188 snprintf(file_name, PATH_MAX, "%s/test_dir", g_temp_sub_dir_path); 196 snprintf(file_name, PATH_MAX, "%s/test_dir", g_temp_sub_dir_path);
189 ASSERT_EQ(mkdir(file_name, mode), 0); 197 ASSERT_EQ(mkdir(file_name, mode), 0);
190 ASSERT_EQ(rmdir(file_name), 0); 198 ASSERT_EQ(rmdir(file_name), 0);
191 199
192 ASSERT_NE(mkdir("/this_dir_does_not_exist/sub_dir", mode), 0); 200 ASSERT_NE(mkdir("/this_dir_does_not_exist/sub_dir", mode), 0);
193 passed("test_new_directory_access", "all"); 201 passed("test_new_directory_access", "all");
194 } 202 }
195 203
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 // information leak could also lead to discovering directories and files 299 // information leak could also lead to discovering directories and files
292 // outside the mount point. 300 // outside the mount point.
293 char path[PATH_MAX]; 301 char path[PATH_MAX];
294 struct stat buf; 302 struct stat buf;
295 303
296 // We should be able to access the root directory. 304 // We should be able to access the root directory.
297 ASSERT_EQ(stat("/", &buf), 0); 305 ASSERT_EQ(stat("/", &buf), 0);
298 ASSERT_EQ(stat("//", &buf), 0); 306 ASSERT_EQ(stat("//", &buf), 0);
299 ASSERT_EQ(stat("/./.", &buf), 0); 307 ASSERT_EQ(stat("/./.", &buf), 0);
300 ASSERT_EQ(stat("/./////.", &buf), 0); 308 ASSERT_EQ(stat("/./////.", &buf), 0);
301 309 ASSERT_EQ(stat(".", &buf), 0);
302 // We should not be able to access relative paths.
303 ASSERT_EQ(stat(".", &buf), -1);
304 ASSERT_EQ(errno, EACCES);
305 310
306 // We should not be able to access paths containing "..". 311 // We should not be able to access paths containing "..".
307 snprintf(path, PATH_MAX, "%s/..", g_temp_sub_dir_path); 312 snprintf(path, PATH_MAX, "%s/..", g_temp_sub_dir_path);
308 ASSERT_EQ(stat(path, &buf), -1); 313 ASSERT_EQ(stat(path, &buf), -1);
309 ASSERT_EQ(errno, EACCES); 314 ASSERT_EQ(errno, EACCES);
310 315
311 // We should not be able to access the parent of the root directory. 316 // We should not be able to access the parent of the root directory.
312 ASSERT_EQ(stat("/..", &buf), -1); 317 ASSERT_EQ(stat("/..", &buf), -1);
313 ASSERT_EQ(errno, EACCES); 318 ASSERT_EQ(errno, EACCES);
314 319
315 // We should not be able to identify our mount point this way. 320 // We should not be able to identify our mount point this way.
316 snprintf(path, PATH_MAX, "/../%s", g_temp_dir_name); 321 snprintf(path, PATH_MAX, "/../%s", g_temp_dir_name);
317 ASSERT_EQ(stat(path, &buf), -1); 322 ASSERT_EQ(stat(path, &buf), -1);
318 ASSERT_EQ(errno, EACCES); 323 ASSERT_EQ(errno, EACCES);
319 snprintf(path, PATH_MAX, "//../%s", g_temp_dir_name); 324 snprintf(path, PATH_MAX, "//../%s", g_temp_dir_name);
320 ASSERT_EQ(stat(path, &buf), -1); 325 ASSERT_EQ(stat(path, &buf), -1);
321 ASSERT_EQ(errno, EACCES); 326 ASSERT_EQ(errno, EACCES);
322 snprintf(path, PATH_MAX, "/.//..//%s", g_temp_dir_name); 327 snprintf(path, PATH_MAX, "/.//..//%s", g_temp_dir_name);
323 ASSERT_EQ(stat(path, &buf), -1); 328 ASSERT_EQ(stat(path, &buf), -1);
324 ASSERT_EQ(errno, EACCES); 329 ASSERT_EQ(errno, EACCES);
325 330
326 passed("test_information_leak", "all"); 331 passed("test_information_leak", "all");
327 } 332 }
328 333
329 void test_valid_file_access() { 334 void test_valid_file_access() {
330 // Show that reads and writes to valid files work. 335 // Show that reads and writes to valid files work.
331 char file_name[PATH_MAX]; 336 char file_name[PATH_MAX];
332 337
338 // Absolute path
333 snprintf(file_name, PATH_MAX, "%s", g_temp_file_path); 339 snprintf(file_name, PATH_MAX, "%s", g_temp_file_path);
334 do_test_write_read_file(file_name, false); 340 do_test_write_read_file(file_name, /* new_file= */ false);
335 341
342 // Relative path
343 snprintf(file_name, PATH_MAX, "%s", g_temp_file_name);
344 do_test_write_read_file(file_name, /* new_file= */ false);
345
346 // Absolute path
336 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_path, 347 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_path,
337 g_temp_sub_file_name); 348 g_temp_sub_file_name);
338 do_test_write_read_file(file_name, false); 349 do_test_write_read_file(file_name, /* new_file= */ false);
339 350
340 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_path, 351 // Relative path
352 snprintf(file_name, PATH_MAX, "%s/%s", g_temp_sub_dir_name,
341 g_temp_sub_file_name); 353 g_temp_sub_file_name);
342 do_test_write_read_file(file_name, false); 354 do_test_write_read_file(file_name, /* new_file= */ false);
355
356 ASSERT_EQ_MSG(chdir(g_temp_sub_dir_name), 0, "chdir() failed");
357
358 // Relative path
359 snprintf(file_name, PATH_MAX, "%s", g_temp_sub_file_name);
360 do_test_write_read_file(file_name, /* new_file= */ false);
343 361
344 passed("test_valid_file_access", "all"); 362 passed("test_valid_file_access", "all");
345 } 363 }
346 364
347 void test_new_file_access() { 365 void test_new_file_access() {
348 // Create a new file, show that it is readable / writable. 366 // Create a new file, show that it is readable / writable.
349 char file_name[PATH_MAX]; 367 char file_name[PATH_MAX];
350 do_test_write_read_file("/new_temp_file", true); 368 do_test_write_read_file("/new_temp_file", true);
351 369
352 snprintf(file_name, PATH_MAX, "%s/newer_temp_file", g_temp_sub_dir_path); 370 snprintf(file_name, PATH_MAX, "%s/newer_temp_file", g_temp_sub_dir_path);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
403 g_temp_sub_dir_name, argv[5]); 421 g_temp_sub_dir_name, argv[5]);
404 422
405 snprintf(g_temp_inaccessible_dir_name, PATH_MAX, "%s", argv[6]); 423 snprintf(g_temp_inaccessible_dir_name, PATH_MAX, "%s", argv[6]);
406 snprintf(g_temp_inaccessible_file_name, PATH_MAX, "%s", argv[7]); 424 snprintf(g_temp_inaccessible_file_name, PATH_MAX, "%s", argv[7]);
407 425
408 // Run the full test suite. 426 // Run the full test suite.
409 testSuite(); 427 testSuite();
410 printf("All tests PASSED\n"); 428 printf("All tests PASSED\n");
411 exit(0); 429 exit(0);
412 } 430 }
OLDNEW
« no previous file with comments | « src/trusted/service_runtime/sel_main.c ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698