OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/memory/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
| 7 #include <aclapi.h> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_ptr.h" |
8 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
9 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
10 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 // Returns the length of the memory section starting at the supplied address. | 17 // Returns the length of the memory section starting at the supplied address. |
15 size_t GetMemorySectionSize(void* address) { | 18 size_t GetMemorySectionSize(void* address) { |
16 MEMORY_BASIC_INFORMATION memory_info; | 19 MEMORY_BASIC_INFORMATION memory_info; |
17 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) | 20 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info))) |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 return false; | 113 return false; |
111 | 114 |
112 // Check maximum accounting for overflow. | 115 // Check maximum accounting for overflow. |
113 if (options.size > | 116 if (options.size > |
114 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) | 117 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask) |
115 return false; | 118 return false; |
116 | 119 |
117 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; | 120 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask; |
118 name_ = ASCIIToWide(options.name_deprecated == NULL ? "" : | 121 name_ = ASCIIToWide(options.name_deprecated == NULL ? "" : |
119 *options.name_deprecated); | 122 *options.name_deprecated); |
| 123 SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, FALSE }; |
| 124 SECURITY_DESCRIPTOR sd; |
| 125 ACL dacl; |
| 126 |
120 if (options.share_read_only && name_.empty()) { | 127 if (options.share_read_only && name_.empty()) { |
| 128 // Add an empty DACL to enforce anonymous read-only sections. |
| 129 sa.lpSecurityDescriptor = &sd; |
| 130 if (!InitializeAcl(&dacl, sizeof(dacl), ACL_REVISION)) |
| 131 return false; |
| 132 if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) |
| 133 return false; |
| 134 if (!SetSecurityDescriptorDacl(&sd, TRUE, &dacl, FALSE)) |
| 135 return false; |
| 136 |
121 // Windows ignores DACLs on certain unnamed objects (like shared sections). | 137 // Windows ignores DACLs on certain unnamed objects (like shared sections). |
122 // So, we generate a random name when we need to enforce read-only. | 138 // So, we generate a random name when we need to enforce read-only. |
123 uint64_t rand_values[4]; | 139 uint64_t rand_values[4]; |
124 base::RandBytes(&rand_values, sizeof(rand_values)); | 140 base::RandBytes(&rand_values, sizeof(rand_values)); |
125 name_ = base::StringPrintf(L"CrSharedMem_%016x%016x%016x%016x", | 141 name_ = base::StringPrintf(L"CrSharedMem_%016x%016x%016x%016x", |
126 rand_values[0], rand_values[1], | 142 rand_values[0], rand_values[1], |
127 rand_values[2], rand_values[3]); | 143 rand_values[2], rand_values[3]); |
128 } | 144 } |
129 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, | 145 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, &sa, |
130 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), name_.c_str()); | 146 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), name_.c_str()); |
131 if (!mapped_file_) | 147 if (!mapped_file_) |
132 return false; | 148 return false; |
133 | 149 |
134 requested_size_ = options.size; | 150 requested_size_ = options.size; |
135 | 151 |
136 // Check if the shared memory pre-exists. | 152 // Check if the shared memory pre-exists. |
137 if (GetLastError() == ERROR_ALREADY_EXISTS) { | 153 if (GetLastError() == ERROR_ALREADY_EXISTS) { |
138 // If the file already existed, set requested_size_ to 0 to show that | 154 // If the file already existed, set requested_size_ to 0 to show that |
139 // we don't know the size. | 155 // we don't know the size. |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 void SharedMemory::UnlockDeprecated() { | 278 void SharedMemory::UnlockDeprecated() { |
263 DCHECK(lock_ != NULL); | 279 DCHECK(lock_ != NULL); |
264 ReleaseMutex(lock_); | 280 ReleaseMutex(lock_); |
265 } | 281 } |
266 | 282 |
267 SharedMemoryHandle SharedMemory::handle() const { | 283 SharedMemoryHandle SharedMemory::handle() const { |
268 return mapped_file_; | 284 return mapped_file_; |
269 } | 285 } |
270 | 286 |
271 } // namespace base | 287 } // namespace base |
OLD | NEW |