OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/edk/embedder/named_platform_handle_utils.h" |
| 6 |
| 7 #include <sddl.h> |
| 8 #include <windows.h> |
| 9 |
| 10 #include <memory> |
| 11 |
| 12 #include "base/logging.h" |
| 13 #include "base/win/windows_version.h" |
| 14 #include "mojo/edk/embedder/named_platform_handle.h" |
| 15 |
| 16 namespace mojo { |
| 17 namespace edk { |
| 18 |
| 19 ScopedPlatformHandle CreateClientHandle( |
| 20 const NamedPlatformHandle& named_handle) { |
| 21 if (!named_handle.is_valid()) |
| 22 return ScopedPlatformHandle(); |
| 23 |
| 24 base::string16 pipe_name = named_handle.pipe_name(); |
| 25 |
| 26 // Note: This may block. |
| 27 if (!WaitNamedPipeW(pipe_name.c_str(), NMPWAIT_USE_DEFAULT_WAIT)) |
| 28 return ScopedPlatformHandle(); |
| 29 |
| 30 const DWORD kDesiredAccess = GENERIC_READ | GENERIC_WRITE; |
| 31 // The SECURITY_ANONYMOUS flag means that the server side cannot impersonate |
| 32 // the client. |
| 33 const DWORD kFlags = |
| 34 SECURITY_SQOS_PRESENT | SECURITY_ANONYMOUS | FILE_FLAG_OVERLAPPED; |
| 35 ScopedPlatformHandle handle( |
| 36 PlatformHandle(CreateFileW(pipe_name.c_str(), kDesiredAccess, |
| 37 0, // No sharing. |
| 38 nullptr, OPEN_EXISTING, kFlags, |
| 39 nullptr))); // No template file. |
| 40 PCHECK(handle.is_valid()); |
| 41 return handle; |
| 42 } |
| 43 |
| 44 ScopedPlatformHandle CreateServerHandle(const NamedPlatformHandle& named_handle, |
| 45 bool enforce_uniqueness) { |
| 46 if (!named_handle.is_valid()) |
| 47 return ScopedPlatformHandle(); |
| 48 |
| 49 PSECURITY_DESCRIPTOR security_desc = nullptr; |
| 50 ULONG security_desc_len = 0; |
| 51 // Create a DACL to grant: |
| 52 // GA = Generic All |
| 53 // access to: |
| 54 // SY = LOCAL_SYSTEM |
| 55 // BA = BUILTIN_ADMINISTRATORS |
| 56 // OW = OWNER_RIGHTS |
| 57 PCHECK(ConvertStringSecurityDescriptorToSecurityDescriptor( |
| 58 L"D:(A;;GA;;;SY)(A;;GA;;;BA)(A;;GA;;;OW)", SDDL_REVISION_1, |
| 59 &security_desc, &security_desc_len)); |
| 60 std::unique_ptr<void, decltype(::LocalFree)*> p(security_desc, ::LocalFree); |
| 61 SECURITY_ATTRIBUTES security_attributes = {sizeof(SECURITY_ATTRIBUTES), |
| 62 security_desc, FALSE}; |
| 63 |
| 64 const DWORD kOpenMode = enforce_uniqueness |
| 65 ? PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 66 FILE_FLAG_FIRST_PIPE_INSTANCE |
| 67 : PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED; |
| 68 const DWORD kPipeMode = |
| 69 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_REJECT_REMOTE_CLIENTS; |
| 70 PlatformHandle handle( |
| 71 CreateNamedPipeW(named_handle.pipe_name().c_str(), kOpenMode, kPipeMode, |
| 72 enforce_uniqueness ? 1 : 255, // Max instances. |
| 73 4096, // Out buffer size. |
| 74 4096, // In buffer size. |
| 75 5000, // Timeout in milliseconds. |
| 76 &security_attributes)); |
| 77 handle.needs_connection = true; |
| 78 return ScopedPlatformHandle(handle); |
| 79 } |
| 80 |
| 81 } // namespace edk |
| 82 } // namespace mojo |
OLD | NEW |