Commit bc89217b authored by Alexandr Ilin's avatar Alexandr Ilin Committed by Commit Bot

PlatformSharedMemoryRegion permission checks on Mac

This CL implements the CheckPlatformHandlePermissionsCorrespondToMode()
function on Mac.

This also enables extra testing of this function since it's implemented
on all supported platforms.

Bug: 825177
Change-Id: I6982d62618a33b2c67091651c09c9b0d3a23326a
Reviewed-on: https://chromium-review.googlesource.com/1012075Reviewed-by: default avatarRobert Sesek <rsesek@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarErik Chen <erikchen@chromium.org>
Commit-Queue: Alexandr Ilin <alexilin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#552097}
parent 82cd0707
......@@ -183,7 +183,31 @@ bool PlatformSharedMemoryRegion::CheckPlatformHandlePermissionsCorrespondToMode(
PlatformHandle handle,
Mode mode,
size_t size) {
// TODO(https://crbug.com/825177): implement this.
mach_vm_address_t temp_addr = 0;
kern_return_t kr =
mach_vm_map(mach_task_self(), &temp_addr, size, 0, VM_FLAGS_ANYWHERE,
handle, 0, FALSE, VM_PROT_READ | VM_PROT_WRITE,
VM_PROT_READ | VM_PROT_WRITE, VM_INHERIT_NONE);
if (kr == KERN_SUCCESS) {
kern_return_t kr_deallocate =
mach_vm_deallocate(mach_task_self(), temp_addr, size);
MACH_DLOG_IF(ERROR, kr_deallocate != KERN_SUCCESS, kr_deallocate)
<< "mach_vm_deallocate";
} else if (kr != KERN_INVALID_RIGHT) {
MACH_DLOG(ERROR, kr) << "mach_vm_map";
return false;
}
bool is_read_only = kr == KERN_INVALID_RIGHT;
bool expected_read_only = mode == Mode::kReadOnly;
if (is_read_only != expected_read_only) {
DLOG(ERROR) << "VM region has a wrong protection mask: it is"
<< (is_read_only ? " " : " not ") << "read-only but it should"
<< (expected_read_only ? " " : " not ") << "be";
return false;
}
return true;
}
......
......@@ -223,19 +223,20 @@ TEST_F(PlatformSharedMemoryRegionTest,
PlatformSharedMemoryRegion::CreateWritable(kRegionSize);
ASSERT_TRUE(region.IsValid());
EXPECT_TRUE(check(region, Mode::kWritable));
EXPECT_FALSE(check(region, Mode::kReadOnly));
// Check kReadOnly region.
ASSERT_TRUE(region.ConvertToReadOnly());
EXPECT_TRUE(check(region, Mode::kReadOnly));
EXPECT_FALSE(check(region, Mode::kWritable));
EXPECT_FALSE(check(region, Mode::kUnsafe));
// Check kUnsafe region.
PlatformSharedMemoryRegion region2 =
PlatformSharedMemoryRegion::CreateUnsafe(kRegionSize);
ASSERT_TRUE(region2.IsValid());
EXPECT_TRUE(check(region2, Mode::kUnsafe));
// TODO(https://crbug.com/825177): add negative expectations once all
// platforms implement this check.
EXPECT_FALSE(check(region2, Mode::kReadOnly));
}
// Tests that it's impossible to create read-only platform shared memory region.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment