| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 base::// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/platform_file.h" | 10 #include "base/platform_file.h" |
| 11 #include "content/browser/child_process_security_policy_impl.h" | 11 #include "content/browser/child_process_security_policy_impl.h" |
| (...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 | 293 |
| 294 p->Remove(kRendererID); | 294 p->Remove(kRendererID); |
| 295 } | 295 } |
| 296 | 296 |
| 297 TEST_F(ChildProcessSecurityPolicyTest, CanReadFiles) { | 297 TEST_F(ChildProcessSecurityPolicyTest, CanReadFiles) { |
| 298 ChildProcessSecurityPolicyImpl* p = | 298 ChildProcessSecurityPolicyImpl* p = |
| 299 ChildProcessSecurityPolicyImpl::GetInstance(); | 299 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 300 | 300 |
| 301 p->Add(kRendererID); | 301 p->Add(kRendererID); |
| 302 | 302 |
| 303 EXPECT_FALSE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd")))); | 303 EXPECT_FALSE(p->CanReadFile(kRendererID, |
| 304 p->GrantReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd"))); | 304 base::FilePath(TEST_PATH("/etc/passwd")))); |
| 305 EXPECT_TRUE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd")))); | 305 p->GrantReadFile(kRendererID, base::FilePath(TEST_PATH("/etc/passwd"))); |
| 306 EXPECT_FALSE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/shadow")))); | 306 EXPECT_TRUE(p->CanReadFile(kRendererID, |
| 307 base::FilePath(TEST_PATH("/etc/passwd")))); |
| 308 EXPECT_FALSE(p->CanReadFile(kRendererID, |
| 309 base::FilePath(TEST_PATH("/etc/shadow")))); |
| 307 | 310 |
| 308 p->Remove(kRendererID); | 311 p->Remove(kRendererID); |
| 309 p->Add(kRendererID); | 312 p->Add(kRendererID); |
| 310 | 313 |
| 311 EXPECT_FALSE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd")))); | 314 EXPECT_FALSE(p->CanReadFile(kRendererID, |
| 312 EXPECT_FALSE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/shadow")))); | 315 base::FilePath(TEST_PATH("/etc/passwd")))); |
| 316 EXPECT_FALSE(p->CanReadFile(kRendererID, |
| 317 base::FilePath(TEST_PATH("/etc/shadow")))); |
| 313 | 318 |
| 314 p->Remove(kRendererID); | 319 p->Remove(kRendererID); |
| 315 } | 320 } |
| 316 | 321 |
| 317 TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) { | 322 TEST_F(ChildProcessSecurityPolicyTest, CanReadDirectories) { |
| 318 ChildProcessSecurityPolicyImpl* p = | 323 ChildProcessSecurityPolicyImpl* p = |
| 319 ChildProcessSecurityPolicyImpl::GetInstance(); | 324 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 320 | 325 |
| 321 p->Add(kRendererID); | 326 p->Add(kRendererID); |
| 322 | 327 |
| 323 EXPECT_FALSE(p->CanReadDirectory(kRendererID, FilePath(TEST_PATH("/etc/")))); | 328 EXPECT_FALSE(p->CanReadDirectory(kRendererID, |
| 324 p->GrantReadDirectory(kRendererID, FilePath(TEST_PATH("/etc/"))); | 329 base::FilePath(TEST_PATH("/etc/")))); |
| 325 EXPECT_TRUE(p->CanReadDirectory(kRendererID, FilePath(TEST_PATH("/etc/")))); | 330 p->GrantReadDirectory(kRendererID, |
| 326 EXPECT_TRUE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd")))); | 331 base::FilePath(TEST_PATH("/etc/"))); |
| 332 EXPECT_TRUE(p->CanReadDirectory(kRendererID, |
| 333 base::FilePath(TEST_PATH("/etc/")))); |
| 334 EXPECT_TRUE(p->CanReadFile(kRendererID, |
| 335 base::FilePath(TEST_PATH("/etc/passwd")))); |
| 327 | 336 |
| 328 p->Remove(kRendererID); | 337 p->Remove(kRendererID); |
| 329 p->Add(kRendererID); | 338 p->Add(kRendererID); |
| 330 | 339 |
| 331 EXPECT_FALSE(p->CanReadDirectory(kRendererID, FilePath(TEST_PATH("/etc/")))); | 340 EXPECT_FALSE(p->CanReadDirectory(kRendererID, |
| 332 EXPECT_FALSE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd")))); | 341 base::FilePath(TEST_PATH("/etc/")))); |
| 342 EXPECT_FALSE(p->CanReadFile(kRendererID, |
| 343 base::FilePath(TEST_PATH("/etc/passwd")))); |
| 333 | 344 |
| 334 // Just granting read permission as a file doesn't imply reading as a | 345 // Just granting read permission as a file doesn't imply reading as a |
| 335 // directory. | 346 // directory. |
| 336 p->GrantReadFile(kRendererID, FilePath(TEST_PATH("/etc/"))); | 347 p->GrantReadFile(kRendererID, base::FilePath(TEST_PATH("/etc/"))); |
| 337 EXPECT_TRUE(p->CanReadFile(kRendererID, FilePath(TEST_PATH("/etc/passwd")))); | 348 EXPECT_TRUE(p->CanReadFile(kRendererID, |
| 338 EXPECT_FALSE(p->CanReadDirectory(kRendererID, FilePath(TEST_PATH("/etc/")))); | 349 base::FilePath(TEST_PATH("/etc/passwd")))); |
| 350 EXPECT_FALSE(p->CanReadDirectory(kRendererID, |
| 351 base::FilePath(TEST_PATH("/etc/")))); |
| 339 | 352 |
| 340 p->Remove(kRendererID); | 353 p->Remove(kRendererID); |
| 341 } | 354 } |
| 342 | 355 |
| 343 TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) { | 356 TEST_F(ChildProcessSecurityPolicyTest, FilePermissions) { |
| 344 FilePath granted_file = FilePath(TEST_PATH("/home/joe")); | 357 base::FilePath granted_file = FilePath(TEST_PATH("/home/joe")); |
| 345 FilePath sibling_file = FilePath(TEST_PATH("/home/bob")); | 358 base::FilePath sibling_file = FilePath(TEST_PATH("/home/bob")); |
| 346 FilePath child_file = FilePath(TEST_PATH("/home/joe/file")); | 359 base::FilePath child_file = FilePath(TEST_PATH("/home/joe/file")); |
| 347 FilePath parent_file = FilePath(TEST_PATH("/home")); | 360 base::FilePath parent_file = FilePath(TEST_PATH("/home")); |
| 348 FilePath parent_slash_file = FilePath(TEST_PATH("/home/")); | 361 base::FilePath parent_slash_file = FilePath(TEST_PATH("/home/")); |
| 349 FilePath child_traversal1 = FilePath(TEST_PATH("/home/joe/././file")); | 362 base::FilePath child_traversal1 = FilePath(TEST_PATH("/home/joe/././file")); |
| 350 FilePath child_traversal2 = FilePath( | 363 base::FilePath child_traversal2 = FilePath( |
| 351 TEST_PATH("/home/joe/file/../otherfile")); | 364 TEST_PATH("/home/joe/file/../otherfile")); |
| 352 FilePath evil_traversal1 = FilePath(TEST_PATH("/home/joe/../../etc/passwd")); | 365 base::FilePath evil_traversal1 = |
| 353 FilePath evil_traversal2 = FilePath( | 366 FilePath(TEST_PATH("/home/joe/../../etc/passwd")); |
| 367 base::FilePath evil_traversal2 = FilePath( |
| 354 TEST_PATH("/home/joe/./.././../etc/passwd")); | 368 TEST_PATH("/home/joe/./.././../etc/passwd")); |
| 355 FilePath self_traversal = FilePath(TEST_PATH("/home/joe/../joe/file")); | 369 base::FilePath self_traversal = FilePath(TEST_PATH("/home/joe/../joe/file")); |
| 356 FilePath relative_file = FilePath(FILE_PATH_LITERAL("home/joe")); | 370 base::FilePath relative_file = FilePath(FILE_PATH_LITERAL("home/joe")); |
| 357 | 371 |
| 358 ChildProcessSecurityPolicyImpl* p = | 372 ChildProcessSecurityPolicyImpl* p = |
| 359 ChildProcessSecurityPolicyImpl::GetInstance(); | 373 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 360 | 374 |
| 361 // Grant permissions for a file. | 375 // Grant permissions for a file. |
| 362 p->Add(kRendererID); | 376 p->Add(kRendererID); |
| 363 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, | 377 EXPECT_FALSE(p->HasPermissionsForFile(kRendererID, granted_file, |
| 364 base::PLATFORM_FILE_OPEN)); | 378 base::PLATFORM_FILE_OPEN)); |
| 365 | 379 |
| 366 p->GrantPermissionsForFile(kRendererID, granted_file, | 380 p->GrantPermissionsForFile(kRendererID, granted_file, |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 503 EXPECT_TRUE(p->CanRequestURL(kRendererID, url)); | 517 EXPECT_TRUE(p->CanRequestURL(kRendererID, url)); |
| 504 | 518 |
| 505 p->Remove(kRendererID); | 519 p->Remove(kRendererID); |
| 506 } | 520 } |
| 507 | 521 |
| 508 TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) { | 522 TEST_F(ChildProcessSecurityPolicyTest, RemoveRace) { |
| 509 ChildProcessSecurityPolicyImpl* p = | 523 ChildProcessSecurityPolicyImpl* p = |
| 510 ChildProcessSecurityPolicyImpl::GetInstance(); | 524 ChildProcessSecurityPolicyImpl::GetInstance(); |
| 511 | 525 |
| 512 GURL url("file:///etc/passwd"); | 526 GURL url("file:///etc/passwd"); |
| 513 FilePath file(TEST_PATH("/etc/passwd")); | 527 base::FilePath file(TEST_PATH("/etc/passwd")); |
| 514 | 528 |
| 515 p->Add(kRendererID); | 529 p->Add(kRendererID); |
| 516 | 530 |
| 517 p->GrantRequestURL(kRendererID, url); | 531 p->GrantRequestURL(kRendererID, url); |
| 518 p->GrantReadFile(kRendererID, file); | 532 p->GrantReadFile(kRendererID, file); |
| 519 p->GrantWebUIBindings(kRendererID); | 533 p->GrantWebUIBindings(kRendererID); |
| 520 | 534 |
| 521 EXPECT_TRUE(p->CanRequestURL(kRendererID, url)); | 535 EXPECT_TRUE(p->CanRequestURL(kRendererID, url)); |
| 522 EXPECT_TRUE(p->CanReadFile(kRendererID, file)); | 536 EXPECT_TRUE(p->CanReadFile(kRendererID, file)); |
| 523 EXPECT_TRUE(p->HasWebUIBindings(kRendererID)); | 537 EXPECT_TRUE(p->HasWebUIBindings(kRendererID)); |
| 524 | 538 |
| 525 p->Remove(kRendererID); | 539 p->Remove(kRendererID); |
| 526 | 540 |
| 527 // Renderers are added and removed on the UI thread, but the policy can be | 541 // Renderers are added and removed on the UI thread, but the policy can be |
| 528 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be | 542 // queried on the IO thread. The ChildProcessSecurityPolicy needs to be |
| 529 // prepared to answer policy questions about renderers who no longer exist. | 543 // prepared to answer policy questions about renderers who no longer exist. |
| 530 | 544 |
| 531 // In this case, we default to secure behavior. | 545 // In this case, we default to secure behavior. |
| 532 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); | 546 EXPECT_FALSE(p->CanRequestURL(kRendererID, url)); |
| 533 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); | 547 EXPECT_FALSE(p->CanReadFile(kRendererID, file)); |
| 534 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); | 548 EXPECT_FALSE(p->HasWebUIBindings(kRendererID)); |
| 535 } | 549 } |
| 536 | 550 |
| 537 } // namespace content | 551 } // namespace content |
| OLD | NEW |