OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/memory/shared_memory.h" | 7 #include "base/memory/shared_memory.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/stringprintf.h" | |
11 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
12 #include "base/test/multiprocess_test.h" | 13 #include "base/test/multiprocess_test.h" |
13 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
14 #include "base/time/time.h" | 15 #include "base/time/time.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 #include "testing/multiprocess_func_list.h" | 17 #include "testing/multiprocess_func_list.h" |
17 | 18 |
18 #if defined(OS_MACOSX) | 19 #if defined(OS_MACOSX) |
19 #include "base/mac/scoped_nsautorelease_pool.h" | 20 #include "base/mac/scoped_nsautorelease_pool.h" |
20 #endif | 21 #endif |
21 | 22 |
22 #if defined(OS_POSIX) | 23 #if defined(OS_POSIX) |
24 #include <errno.h> | |
25 #include <fcntl.h> | |
23 #include <sys/mman.h> | 26 #include <sys/mman.h> |
24 #include <sys/stat.h> | 27 #include <sys/stat.h> |
25 #include <sys/types.h> | 28 #include <sys/types.h> |
26 #include <unistd.h> | 29 #include <unistd.h> |
27 #endif | 30 #endif |
28 | 31 |
29 static const int kNumThreads = 5; | 32 static const int kNumThreads = 5; |
30 static const int kNumTasks = 5; | 33 static const int kNumTasks = 5; |
31 | 34 |
32 namespace base { | 35 namespace base { |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 else | 357 else |
355 EXPECT_EQ(0, pointers[j][0]); | 358 EXPECT_EQ(0, pointers[j][0]); |
356 } | 359 } |
357 } | 360 } |
358 | 361 |
359 for (int i = 0; i < count; i++) { | 362 for (int i = 0; i < count; i++) { |
360 memories[i].Close(); | 363 memories[i].Close(); |
361 } | 364 } |
362 } | 365 } |
363 | 366 |
367 TEST(SharedMemoryTest, AnonymousReadOnly) { | |
368 StringPiece contents = "Hello World"; | |
369 scoped_ptr<SharedMemory> shmem( | |
370 SharedMemory::NewAnonymousReadOnly("Hello World")); | |
371 | |
372 ASSERT_TRUE(shmem->Map(contents.size())); | |
373 EXPECT_EQ( | |
374 contents, | |
375 StringPiece(static_cast<const char*>(shmem->memory()), contents.size())); | |
376 | |
377 // We'd like to check that if we send the read-only segment to another | |
378 // process, then that other process can't reopen it read/write. (Since that | |
379 // would be a security hole.) Setting up multiple processes is hard in a | |
380 // unittest, so this test checks that the *current* process can't reopen the | |
381 // segment read/write. I think the test here is stronger than we actually | |
382 // care about, but there's a remote possibility that sending a file over a | |
383 // pipe would transform it into read/write. | |
384 SharedMemoryHandle handle = shmem->handle(); | |
385 #if OS_POSIX | |
jln (very slow on Chromium)
2013/10/16 00:16:39
Style:
#if defined()
Jeffrey Yasskin
2013/10/16 01:16:39
Done.
| |
386 | |
387 EXPECT_EQ(O_RDONLY, fcntl(handle.fd, F_GETFL) & O_ACCMODE) | |
388 << "The descriptor itself should be read-only."; | |
389 | |
390 errno = 0; | |
391 void* writable = mmap(NULL, | |
392 shmem->mapped_size(), | |
393 PROT_READ | PROT_WRITE, | |
394 MAP_SHARED, | |
395 handle.fd, | |
396 0); | |
397 int mmap_errno = errno; | |
398 EXPECT_EQ(MAP_FAILED, writable) | |
399 << "It shouldn't be possible to re-mmap the descriptor writable."; | |
400 EXPECT_EQ(EACCES, mmap_errno); | |
401 | |
402 // Try to re-open through /dev/fd. This is an end-run around the notion of an | |
403 // FD as a capability. | |
404 const std::string shmem_path = StringPrintf("/dev/fd/%d", handle.fd); | |
405 errno = 0; | |
406 int readable_fd = open(shmem_path.c_str(), O_RDONLY); | |
407 EXPECT_NE(-1, readable_fd) << strerror(errno); | |
408 close(readable_fd); | |
409 | |
410 errno = 0; | |
411 int writable_fd = open(shmem_path.c_str(), O_WRONLY); | |
412 int open_writable_errno = errno; | |
413 EXPECT_EQ(-1, writable_fd); | |
414 EXPECT_EQ(EACCES, open_writable_errno) << strerror(open_writable_errno); | |
415 close(writable_fd); | |
416 | |
417 // However, if we explicitly make the entry in /dev/fd writable first, the | |
418 // open() call successfully creates a writable file on Linux. The sandbox has | |
419 // to prevent opening this path. TODO(jln): Write a test that attacks this | |
420 // from inside the sandbox. | |
421 fchmod(handle.fd, S_IRUSR | S_IWUSR); | |
jln (very slow on Chromium)
2013/10/16 00:16:39
Please, check the return value, it's better to kee
Jeffrey Yasskin
2013/10/16 01:16:39
Done.
| |
422 | |
423 errno = 0; | |
424 writable_fd = open(shmem_path.c_str(), O_WRONLY); | |
425 open_writable_errno = errno; | |
426 // Mac appears to get this right by treating open(/dev/fd/N) as dup(N). | |
jln (very slow on Chromium)
2013/10/16 00:16:39
This is not correct, open(/dev/fd/N) is very diffe
Jeffrey Yasskin
2013/10/16 01:16:39
https://developer.apple.com/library/mac/documentat
jln (very slow on Chromium)
2013/10/16 17:51:15
Ohh yeah, F_DUPFD is confusing. It really has noth
| |
427 #if !OS_LINUX | |
jln (very slow on Chromium)
2013/10/16 00:16:39
Style: #if !defined()
Jeffrey Yasskin
2013/10/16 01:16:39
Done.
| |
428 EXPECT_EQ(-1, writable_fd); | |
429 EXPECT_EQ(EACCES, open_writable_errno) << strerror(open_writable_errno); | |
430 #endif | |
431 close(writable_fd); | |
432 | |
433 #elif OS_WIN | |
Jeffrey Yasskin
2013/10/15 23:03:52
Please try to think of other attacks I should be t
| |
434 EXPECT_EQ(NULL, MapViewOfFile(handle, FILE_MAP_WRITE, 0, 0, 0)) | |
435 << "Shouldn't be able to map memory writable."; | |
436 | |
437 SharedMemoryHandle writable_handle = INVALID_HANDLE_VALUE; | |
438 EXPECT_EQ(0, | |
439 ::DuplicateHandle(GetCurrentProcess(), | |
440 handle, | |
441 GetCurrentProcess, | |
442 &writable_handle, | |
443 FILE_MAP_ALL_ACCESS, | |
444 false, | |
445 0)) | |
446 << "Shouldn't be able to duplicate the handle into a writable one."; | |
447 #else | |
448 #error Unexpected platform; write a test that tries to make 'handle' writable. | |
449 #endif | |
450 } | |
451 | |
364 TEST(SharedMemoryTest, MapAt) { | 452 TEST(SharedMemoryTest, MapAt) { |
365 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); | 453 ASSERT_TRUE(SysInfo::VMAllocationGranularity() >= sizeof(uint32)); |
366 const size_t kCount = SysInfo::VMAllocationGranularity(); | 454 const size_t kCount = SysInfo::VMAllocationGranularity(); |
367 const size_t kDataSize = kCount * sizeof(uint32); | 455 const size_t kDataSize = kCount * sizeof(uint32); |
368 | 456 |
369 SharedMemory memory; | 457 SharedMemory memory; |
370 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); | 458 ASSERT_TRUE(memory.CreateAndMapAnonymous(kDataSize)); |
371 ASSERT_TRUE(memory.Map(kDataSize)); | 459 ASSERT_TRUE(memory.Map(kDataSize)); |
372 uint32* ptr = static_cast<uint32*>(memory.memory()); | 460 uint32* ptr = static_cast<uint32*>(memory.memory()); |
373 ASSERT_NE(ptr, static_cast<void*>(NULL)); | 461 ASSERT_NE(ptr, static_cast<void*>(NULL)); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
552 SharedMemoryProcessTest::CleanUp(); | 640 SharedMemoryProcessTest::CleanUp(); |
553 } | 641 } |
554 | 642 |
555 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { | 643 MULTIPROCESS_TEST_MAIN(SharedMemoryTestMain) { |
556 return SharedMemoryProcessTest::TaskTestMain(); | 644 return SharedMemoryProcessTest::TaskTestMain(); |
557 } | 645 } |
558 | 646 |
559 #endif // !OS_IOS | 647 #endif // !OS_IOS |
560 | 648 |
561 } // namespace base | 649 } // namespace base |
OLD | NEW |