erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 1 | // Copyright 2015 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 "build/build_config.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | #include <memory> |
tzik | 55e3e4d | 2016-03-08 05:47:44 | [diff] [blame] | 10 | #include <tuple> |
| 11 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
| 13 | #include "base/files/file_util.h" |
| 14 | #include "base/files/scoped_temp_dir.h" |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 15 | #include "base/memory/shared_memory.h" |
| 16 | #include "base/memory/shared_memory_handle.h" |
| 17 | #include "base/win/scoped_handle.h" |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 18 | #include "ipc/attachment_broker_privileged_win.h" |
erikchen | 484c0084 | 2015-07-28 23:25:44 | [diff] [blame] | 19 | #include "ipc/attachment_broker_unprivileged_win.h" |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 20 | #include "ipc/handle_attachment_win.h" |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 21 | #include "ipc/handle_win.h" |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 22 | #include "ipc/ipc_listener.h" |
| 23 | #include "ipc/ipc_message.h" |
| 24 | #include "ipc/ipc_test_base.h" |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 25 | #include "ipc/ipc_test_messages.h" |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 26 | |
| 27 | namespace { |
| 28 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 29 | using base::win::ScopedHandle; |
| 30 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 31 | const char kDataBuffer[] = "This is some test data to write to the file."; |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 32 | const size_t kSharedMemorySize = 20000; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 33 | |
| 34 | // Returns the contents of the file represented by |h| as a std::string. |
| 35 | std::string ReadFromFile(HANDLE h) { |
| 36 | SetFilePointer(h, 0, nullptr, FILE_BEGIN); |
| 37 | char buffer[100]; |
| 38 | DWORD bytes_read; |
| 39 | BOOL success = ::ReadFile(h, buffer, static_cast<DWORD>(strlen(kDataBuffer)), |
| 40 | &bytes_read, nullptr); |
| 41 | return success ? std::string(buffer, bytes_read) : std::string(); |
| 42 | } |
| 43 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 44 | ScopedHandle GetHandleFromBrokeredAttachment( |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 45 | const scoped_refptr<IPC::BrokerableAttachment>& attachment) { |
| 46 | if (attachment->GetType() != |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 47 | IPC::BrokerableAttachment::TYPE_BROKERABLE_ATTACHMENT) { |
| 48 | LOG(INFO) << "Attachment type not TYPE_BROKERABLE_ATTACHMENT."; |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 49 | return ScopedHandle(nullptr); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | if (attachment->GetBrokerableType() != |
| 53 | IPC::BrokerableAttachment::WIN_HANDLE) { |
| 54 | LOG(INFO) << "Brokerable type not WIN_HANDLE."; |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 55 | return ScopedHandle(nullptr); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 56 | } |
| 57 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 58 | IPC::internal::HandleAttachmentWin* received_handle_attachment = |
| 59 | static_cast<IPC::internal::HandleAttachmentWin*>(attachment.get()); |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 60 | ScopedHandle h(received_handle_attachment->get_handle()); |
| 61 | received_handle_attachment->reset_handle_ownership(); |
| 62 | return h; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 63 | } |
| 64 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 65 | // |message| must be deserializable as a TestHandleWinMsg. Returns the HANDLE, |
| 66 | // or nullptr if deserialization failed. |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 67 | ScopedHandle GetHandleFromTestHandleWinMsg(const IPC::Message& message) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 68 | // Expect a message with a brokered attachment. |
| 69 | if (!message.HasBrokerableAttachments()) { |
| 70 | LOG(INFO) << "Message missing brokerable attachment."; |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 71 | return ScopedHandle(nullptr); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | TestHandleWinMsg::Schema::Param p; |
| 75 | bool success = TestHandleWinMsg::Read(&message, &p); |
| 76 | if (!success) { |
| 77 | LOG(INFO) << "Failed to deserialize message."; |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 78 | return ScopedHandle(nullptr); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 79 | } |
| 80 | |
tzik | 55e3e4d | 2016-03-08 05:47:44 | [diff] [blame] | 81 | IPC::HandleWin handle_win = std::get<1>(p); |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 82 | return ScopedHandle(handle_win.get_handle()); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 83 | } |
| 84 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 85 | // Returns a mapped, shared memory region based on the handle in |message|. |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 86 | std::unique_ptr<base::SharedMemory> GetSharedMemoryFromSharedMemoryHandleMsg1( |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 87 | const IPC::Message& message, |
| 88 | size_t size) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 89 | // Expect a message with a brokered attachment. |
| 90 | if (!message.HasBrokerableAttachments()) { |
| 91 | LOG(INFO) << "Message missing brokerable attachment."; |
| 92 | return nullptr; |
| 93 | } |
| 94 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 95 | TestSharedMemoryHandleMsg1::Schema::Param p; |
| 96 | bool success = TestSharedMemoryHandleMsg1::Read(&message, &p); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 97 | if (!success) { |
| 98 | LOG(INFO) << "Failed to deserialize message."; |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
tzik | 55e3e4d | 2016-03-08 05:47:44 | [diff] [blame] | 102 | base::SharedMemoryHandle handle = std::get<0>(p); |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 103 | std::unique_ptr<base::SharedMemory> shared_memory( |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 104 | new base::SharedMemory(handle, false)); |
| 105 | |
| 106 | shared_memory->Map(size); |
Hans Wennborg | b7da247 | 2016-01-08 14:33:52 | [diff] [blame] | 107 | return shared_memory; |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // |message| must be deserializable as a TestTwoHandleWinMsg. Returns the |
| 111 | // HANDLE, or nullptr if deserialization failed. |
| 112 | bool GetHandleFromTestTwoHandleWinMsg(const IPC::Message& message, |
| 113 | HANDLE* h1, |
| 114 | HANDLE* h2) { |
| 115 | // Expect a message with a brokered attachment. |
| 116 | if (!message.HasBrokerableAttachments()) { |
| 117 | LOG(INFO) << "Message missing brokerable attachment."; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | TestTwoHandleWinMsg::Schema::Param p; |
| 122 | bool success = TestTwoHandleWinMsg::Read(&message, &p); |
| 123 | if (!success) { |
| 124 | LOG(INFO) << "Failed to deserialize message."; |
| 125 | return false; |
| 126 | } |
| 127 | |
tzik | 55e3e4d | 2016-03-08 05:47:44 | [diff] [blame] | 128 | IPC::HandleWin handle_win = std::get<0>(p); |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 129 | *h1 = handle_win.get_handle(); |
tzik | 55e3e4d | 2016-03-08 05:47:44 | [diff] [blame] | 130 | handle_win = std::get<1>(p); |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 131 | *h2 = handle_win.get_handle(); |
| 132 | return true; |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // |message| must be deserializable as a TestHandleWinMsg. Returns true if the |
| 136 | // attached file HANDLE has contents |kDataBuffer|. |
| 137 | bool CheckContentsOfTestMessage(const IPC::Message& message) { |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 138 | ScopedHandle h(GetHandleFromTestHandleWinMsg(message)); |
| 139 | if (h.Get() == nullptr) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 140 | LOG(INFO) << "Failed to get handle from TestHandleWinMsg."; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 141 | return false; |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 142 | } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 143 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 144 | std::string contents = ReadFromFile(h.Get()); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 145 | bool success = (contents == std::string(kDataBuffer)); |
| 146 | if (!success) { |
| 147 | LOG(INFO) << "Expected contents: " << std::string(kDataBuffer); |
| 148 | LOG(INFO) << "Read contents: " << contents; |
| 149 | } |
| 150 | return success; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 151 | } |
| 152 | |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 153 | // Returns 0 on error. |
| 154 | DWORD GetCurrentProcessHandleCount() { |
| 155 | DWORD handle_count = 0; |
| 156 | BOOL success = ::GetProcessHandleCount(::GetCurrentProcess(), &handle_count); |
| 157 | return success ? handle_count : 0; |
| 158 | } |
| 159 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 160 | enum TestResult { |
| 161 | RESULT_UNKNOWN, |
| 162 | RESULT_SUCCESS, |
| 163 | RESULT_FAILURE, |
| 164 | }; |
| 165 | |
| 166 | // Once the test is finished, send a control message to the parent process with |
| 167 | // the result. The message may require the runloop to be run before its |
| 168 | // dispatched. |
| 169 | void SendControlMessage(IPC::Sender* sender, bool success) { |
| 170 | IPC::Message* message = new IPC::Message(0, 2, IPC::Message::PRIORITY_NORMAL); |
| 171 | TestResult result = success ? RESULT_SUCCESS : RESULT_FAILURE; |
| 172 | message->WriteInt(result); |
| 173 | sender->Send(message); |
| 174 | } |
| 175 | |
| 176 | class MockObserver : public IPC::AttachmentBroker::Observer { |
| 177 | public: |
| 178 | void ReceivedBrokerableAttachmentWithId( |
| 179 | const IPC::BrokerableAttachment::AttachmentId& id) override { |
| 180 | id_ = id; |
| 181 | } |
| 182 | IPC::BrokerableAttachment::AttachmentId* get_id() { return &id_; } |
| 183 | |
| 184 | private: |
| 185 | IPC::BrokerableAttachment::AttachmentId id_; |
| 186 | }; |
| 187 | |
| 188 | // Forwards all messages to |listener_|. Quits the message loop after a |
| 189 | // message is received, or the channel has an error. |
| 190 | class ProxyListener : public IPC::Listener { |
| 191 | public: |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 192 | ProxyListener() : listener_(nullptr), reason_(MESSAGE_RECEIVED) {} |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 193 | ~ProxyListener() override {} |
| 194 | |
| 195 | // The reason for exiting the message loop. |
| 196 | enum Reason { MESSAGE_RECEIVED, CHANNEL_ERROR }; |
| 197 | |
| 198 | bool OnMessageReceived(const IPC::Message& message) override { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 199 | bool result = false; |
| 200 | if (listener_) |
| 201 | result = listener_->OnMessageReceived(message); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 202 | reason_ = MESSAGE_RECEIVED; |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 203 | messages_.push_back(message); |
| 204 | base::MessageLoop::current()->QuitNow(); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 205 | return result; |
| 206 | } |
| 207 | |
| 208 | void OnChannelError() override { |
| 209 | reason_ = CHANNEL_ERROR; |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 210 | base::MessageLoop::current()->QuitNow(); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | void set_listener(IPC::Listener* listener) { listener_ = listener; } |
| 214 | Reason get_reason() { return reason_; } |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 215 | IPC::Message get_first_message() { return messages_[0]; } |
| 216 | void pop_first_message() { messages_.erase(messages_.begin()); } |
| 217 | bool has_message() { return !messages_.empty(); } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 218 | |
| 219 | private: |
| 220 | IPC::Listener* listener_; |
| 221 | Reason reason_; |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 222 | std::vector<IPC::Message> messages_; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | // Waits for a result to be sent over the channel. Quits the message loop |
| 226 | // after a message is received, or the channel has an error. |
| 227 | class ResultListener : public IPC::Listener { |
| 228 | public: |
| 229 | ResultListener() : result_(RESULT_UNKNOWN) {} |
| 230 | ~ResultListener() override {} |
| 231 | |
| 232 | bool OnMessageReceived(const IPC::Message& message) override { |
| 233 | base::PickleIterator iter(message); |
| 234 | |
| 235 | int result; |
| 236 | EXPECT_TRUE(iter.ReadInt(&result)); |
| 237 | result_ = static_cast<TestResult>(result); |
| 238 | return true; |
| 239 | } |
| 240 | |
| 241 | TestResult get_result() { return result_; } |
| 242 | |
| 243 | private: |
| 244 | TestResult result_; |
| 245 | }; |
| 246 | |
| 247 | // The parent process acts as an unprivileged process. The forked process acts |
| 248 | // as the privileged process. |
| 249 | class IPCAttachmentBrokerPrivilegedWinTest : public IPCTestBase { |
| 250 | public: |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 251 | IPCAttachmentBrokerPrivilegedWinTest() {} |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 252 | ~IPCAttachmentBrokerPrivilegedWinTest() override {} |
| 253 | |
| 254 | void SetUp() override { |
| 255 | IPCTestBase::SetUp(); |
| 256 | ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
| 257 | ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_.path(), &temp_path_)); |
| 258 | } |
| 259 | |
| 260 | void TearDown() override { IPCTestBase::TearDown(); } |
| 261 | |
| 262 | // Takes ownership of |broker|. Has no effect if called after CommonSetUp(). |
erikchen | 484c0084 | 2015-07-28 23:25:44 | [diff] [blame] | 263 | void set_broker(IPC::AttachmentBrokerUnprivilegedWin* broker) { |
| 264 | broker_.reset(broker); |
| 265 | } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 266 | |
| 267 | void CommonSetUp() { |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 268 | PreConnectSetUp(); |
| 269 | PostConnectSetUp(); |
| 270 | } |
| 271 | |
| 272 | // All of setup before the channel is connected. |
| 273 | void PreConnectSetUp() { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 274 | if (!broker_.get()) |
erikchen | 484c0084 | 2015-07-28 23:25:44 | [diff] [blame] | 275 | set_broker(new IPC::AttachmentBrokerUnprivilegedWin); |
erikchen | 94c9b70 | 2015-11-06 21:12:36 | [diff] [blame] | 276 | broker_->AddObserver(&observer_, task_runner()); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 277 | CreateChannel(&proxy_listener_); |
erikchen | 8666287 | 2016-02-17 04:09:14 | [diff] [blame] | 278 | broker_->RegisterBrokerCommunicationChannel(channel()); |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // All of setup including the connection and everything after. |
| 282 | void PostConnectSetUp() { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 283 | ASSERT_TRUE(ConnectChannel()); |
| 284 | ASSERT_TRUE(StartClient()); |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 285 | |
| 286 | handle_count_ = GetCurrentProcessHandleCount(); |
| 287 | EXPECT_NE(handle_count_, 0u); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | void CommonTearDown() { |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 291 | EXPECT_EQ(handle_count_, handle_count_); |
| 292 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 293 | // Close the channel so the client's OnChannelError() gets fired. |
| 294 | channel()->Close(); |
| 295 | |
| 296 | EXPECT_TRUE(WaitForClientShutdown()); |
| 297 | DestroyChannel(); |
| 298 | broker_.reset(); |
| 299 | } |
| 300 | |
| 301 | HANDLE CreateTempFile() { |
| 302 | EXPECT_NE(-1, WriteFile(temp_path_, kDataBuffer, |
| 303 | static_cast<int>(strlen(kDataBuffer)))); |
| 304 | |
| 305 | HANDLE h = |
| 306 | CreateFile(temp_path_.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, |
| 307 | nullptr, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr); |
| 308 | EXPECT_NE(h, INVALID_HANDLE_VALUE); |
| 309 | return h; |
| 310 | } |
| 311 | |
| 312 | void SendMessageWithAttachment(HANDLE h) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 313 | IPC::HandleWin handle_win(h, IPC::HandleWin::FILE_READ_WRITE); |
| 314 | IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 315 | sender()->Send(message); |
| 316 | } |
| 317 | |
| 318 | ProxyListener* get_proxy_listener() { return &proxy_listener_; } |
erikchen | 484c0084 | 2015-07-28 23:25:44 | [diff] [blame] | 319 | IPC::AttachmentBrokerUnprivilegedWin* get_broker() { return broker_.get(); } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 320 | MockObserver* get_observer() { return &observer_; } |
| 321 | |
| 322 | private: |
| 323 | base::ScopedTempDir temp_dir_; |
| 324 | base::FilePath temp_path_; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 325 | ProxyListener proxy_listener_; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 326 | std::unique_ptr<IPC::AttachmentBrokerUnprivilegedWin> broker_; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 327 | MockObserver observer_; |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 328 | DWORD handle_count_; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 329 | }; |
| 330 | |
| 331 | // A broker which always sets the current process as the destination process |
| 332 | // for attachments. |
erikchen | 484c0084 | 2015-07-28 23:25:44 | [diff] [blame] | 333 | class MockBroker : public IPC::AttachmentBrokerUnprivilegedWin { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 334 | public: |
| 335 | MockBroker() {} |
| 336 | ~MockBroker() override {} |
erikchen | a03dde6f | 2015-10-29 22:37:04 | [diff] [blame] | 337 | bool SendAttachmentToProcess( |
| 338 | const scoped_refptr<IPC::BrokerableAttachment>& attachment, |
| 339 | base::ProcessId destination_process) override { |
erikchen | 484c0084 | 2015-07-28 23:25:44 | [diff] [blame] | 340 | return IPC::AttachmentBrokerUnprivilegedWin::SendAttachmentToProcess( |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 341 | attachment, base::Process::Current().Pid()); |
| 342 | } |
| 343 | }; |
| 344 | |
| 345 | // An unprivileged process makes a file HANDLE, and writes a string to it. The |
| 346 | // file HANDLE is sent to the privileged process using the attachment broker. |
| 347 | // The privileged process dups the HANDLE into its own HANDLE table. This test |
| 348 | // checks that the file has the same contents in the privileged process. |
erikchen | ee44450 | 2015-12-04 17:20:37 | [diff] [blame] | 349 | TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandle) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 350 | Init("SendHandle"); |
| 351 | |
| 352 | CommonSetUp(); |
| 353 | ResultListener result_listener; |
| 354 | get_proxy_listener()->set_listener(&result_listener); |
| 355 | |
| 356 | HANDLE h = CreateTempFile(); |
| 357 | SendMessageWithAttachment(h); |
| 358 | base::MessageLoop::current()->Run(); |
| 359 | |
| 360 | // Check the result. |
| 361 | ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 362 | get_proxy_listener()->get_reason()); |
| 363 | ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 364 | |
| 365 | CommonTearDown(); |
| 366 | } |
| 367 | |
| 368 | // Similar to SendHandle, except the file HANDLE attached to the message has |
| 369 | // neither read nor write permissions. |
erikchen | 44067ac | 2015-09-04 02:35:49 | [diff] [blame] | 370 | TEST_F(IPCAttachmentBrokerPrivilegedWinTest, |
erikchen | ee44450 | 2015-12-04 17:20:37 | [diff] [blame] | 371 | SendHandleWithoutPermissions) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 372 | Init("SendHandleWithoutPermissions"); |
| 373 | |
| 374 | CommonSetUp(); |
| 375 | ResultListener result_listener; |
| 376 | get_proxy_listener()->set_listener(&result_listener); |
| 377 | |
| 378 | HANDLE h = CreateTempFile(); |
| 379 | HANDLE h2; |
| 380 | BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 381 | &h2, 0, FALSE, DUPLICATE_CLOSE_SOURCE); |
| 382 | ASSERT_TRUE(result); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 383 | IPC::HandleWin handle_win(h2, IPC::HandleWin::DUPLICATE); |
| 384 | IPC::Message* message = new TestHandleWinMsg(100, handle_win, 200); |
| 385 | sender()->Send(message); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 386 | base::MessageLoop::current()->Run(); |
| 387 | |
| 388 | // Check the result. |
| 389 | ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 390 | get_proxy_listener()->get_reason()); |
| 391 | ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 392 | |
| 393 | CommonTearDown(); |
| 394 | } |
| 395 | |
| 396 | // Similar to SendHandle, except the attachment's destination process is this |
| 397 | // process. This is an unrealistic scenario, but simulates an unprivileged |
| 398 | // process sending an attachment to another unprivileged process. |
erikchen | ee44450 | 2015-12-04 17:20:37 | [diff] [blame] | 399 | TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleToSelf) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 400 | Init("SendHandleToSelf"); |
| 401 | |
| 402 | set_broker(new MockBroker); |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 403 | |
| 404 | PreConnectSetUp(); |
erikchen | 8c73f83 | 2015-07-30 22:26:08 | [diff] [blame] | 405 | // Technically, the channel is an endpoint, but we need the proxy listener to |
| 406 | // receive the messages so that it can quit the message loop. |
erikchen | a09b9be7 | 2015-08-10 19:22:33 | [diff] [blame] | 407 | channel()->SetAttachmentBrokerEndpoint(false); |
erikchen | 9097190 | 2016-04-25 23:45:31 | [diff] [blame] | 408 | PostConnectSetUp(); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 409 | get_proxy_listener()->set_listener(get_broker()); |
| 410 | |
| 411 | HANDLE h = CreateTempFile(); |
| 412 | SendMessageWithAttachment(h); |
| 413 | base::MessageLoop::current()->Run(); |
| 414 | |
| 415 | // Get the received attachment. |
| 416 | IPC::BrokerableAttachment::AttachmentId* id = get_observer()->get_id(); |
| 417 | scoped_refptr<IPC::BrokerableAttachment> received_attachment; |
| 418 | get_broker()->GetAttachmentWithId(*id, &received_attachment); |
| 419 | ASSERT_NE(received_attachment.get(), nullptr); |
| 420 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 421 | // Check that it's a different entry in the HANDLE table. |
| 422 | ScopedHandle h2(GetHandleFromBrokeredAttachment(received_attachment)); |
| 423 | EXPECT_NE(h2.Get(), h); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 424 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 425 | // But still points to the same file. |
| 426 | std::string contents = ReadFromFile(h2.Get()); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 427 | EXPECT_EQ(contents, std::string(kDataBuffer)); |
| 428 | |
| 429 | CommonTearDown(); |
| 430 | } |
| 431 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 432 | // Similar to SendHandle, but sends a message with two instances of the same |
| 433 | // handle. |
erikchen | ee44450 | 2015-12-04 17:20:37 | [diff] [blame] | 434 | TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendTwoHandles) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 435 | Init("SendTwoHandles"); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 436 | |
| 437 | CommonSetUp(); |
| 438 | ResultListener result_listener; |
| 439 | get_proxy_listener()->set_listener(&result_listener); |
| 440 | |
| 441 | HANDLE h = CreateTempFile(); |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 442 | HANDLE h2; |
| 443 | BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 444 | &h2, 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 445 | ASSERT_TRUE(result); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 446 | IPC::HandleWin handle_win1(h, IPC::HandleWin::FILE_READ_WRITE); |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 447 | IPC::HandleWin handle_win2(h2, IPC::HandleWin::FILE_READ_WRITE); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 448 | IPC::Message* message = new TestTwoHandleWinMsg(handle_win1, handle_win2); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 449 | sender()->Send(message); |
| 450 | base::MessageLoop::current()->Run(); |
| 451 | |
| 452 | // Check the result. |
| 453 | ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 454 | get_proxy_listener()->get_reason()); |
| 455 | ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 456 | |
| 457 | CommonTearDown(); |
| 458 | } |
| 459 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 460 | // Similar to SendHandle, but sends the same message twice. |
erikchen | ee44450 | 2015-12-04 17:20:37 | [diff] [blame] | 461 | TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendHandleTwice) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 462 | Init("SendHandleTwice"); |
| 463 | |
| 464 | CommonSetUp(); |
| 465 | ResultListener result_listener; |
| 466 | get_proxy_listener()->set_listener(&result_listener); |
| 467 | |
| 468 | HANDLE h = CreateTempFile(); |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 469 | HANDLE h2; |
| 470 | BOOL result = ::DuplicateHandle(GetCurrentProcess(), h, GetCurrentProcess(), |
| 471 | &h2, 0, FALSE, DUPLICATE_SAME_ACCESS); |
| 472 | ASSERT_TRUE(result); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 473 | SendMessageWithAttachment(h); |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 474 | SendMessageWithAttachment(h2); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 475 | base::MessageLoop::current()->Run(); |
| 476 | |
| 477 | // Check the result. |
| 478 | ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 479 | get_proxy_listener()->get_reason()); |
| 480 | ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 481 | |
| 482 | CommonTearDown(); |
| 483 | } |
| 484 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 485 | // An unprivileged process makes a shared memory region and sends it to the |
| 486 | // privileged process. |
erikchen | 0da9425 | 2016-02-03 04:44:39 | [diff] [blame] | 487 | TEST_F(IPCAttachmentBrokerPrivilegedWinTest, SendSharedMemoryHandle) { |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 488 | Init("SendSharedMemoryHandle"); |
| 489 | |
| 490 | CommonSetUp(); |
| 491 | ResultListener result_listener; |
| 492 | get_proxy_listener()->set_listener(&result_listener); |
| 493 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 494 | std::unique_ptr<base::SharedMemory> shared_memory(new base::SharedMemory); |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 495 | shared_memory->CreateAndMapAnonymous(kSharedMemorySize); |
| 496 | memcpy(shared_memory->memory(), kDataBuffer, strlen(kDataBuffer)); |
| 497 | sender()->Send(new TestSharedMemoryHandleMsg1(shared_memory->handle())); |
| 498 | base::MessageLoop::current()->Run(); |
| 499 | |
| 500 | // Check the result. |
| 501 | ASSERT_EQ(ProxyListener::MESSAGE_RECEIVED, |
| 502 | get_proxy_listener()->get_reason()); |
| 503 | ASSERT_EQ(result_listener.get_result(), RESULT_SUCCESS); |
| 504 | |
| 505 | CommonTearDown(); |
| 506 | } |
| 507 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 508 | using OnMessageReceivedCallback = void (*)(IPC::Sender* sender, |
| 509 | const IPC::Message& message); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 510 | |
| 511 | int CommonPrivilegedProcessMain(OnMessageReceivedCallback callback, |
| 512 | const char* channel_name) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 513 | LOG(INFO) << "Privileged process start."; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 514 | base::MessageLoopForIO main_message_loop; |
| 515 | ProxyListener listener; |
| 516 | |
| 517 | // Set up IPC channel. |
| 518 | IPC::AttachmentBrokerPrivilegedWin broker; |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 519 | std::unique_ptr<IPC::Channel> channel(IPC::Channel::CreateClient( |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 520 | IPCTestBase::GetChannelName(channel_name), &listener)); |
erikchen | a91d0513 | 2016-03-21 23:19:40 | [diff] [blame] | 521 | broker.RegisterCommunicationChannel(channel.get(), nullptr); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 522 | CHECK(channel->Connect()); |
| 523 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 524 | while (true) { |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 525 | LOG(INFO) << "Privileged process spinning run loop."; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 526 | base::MessageLoop::current()->Run(); |
| 527 | ProxyListener::Reason reason = listener.get_reason(); |
| 528 | if (reason == ProxyListener::CHANNEL_ERROR) |
| 529 | break; |
| 530 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 531 | while (listener.has_message()) { |
| 532 | LOG(INFO) << "Privileged process running callback."; |
| 533 | callback(channel.get(), listener.get_first_message()); |
| 534 | LOG(INFO) << "Privileged process finishing callback."; |
| 535 | listener.pop_first_message(); |
| 536 | } |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 537 | } |
| 538 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 539 | LOG(INFO) << "Privileged process end."; |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 540 | return 0; |
| 541 | } |
| 542 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 543 | void SendHandleCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 544 | bool success = CheckContentsOfTestMessage(message); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 545 | SendControlMessage(sender, success); |
| 546 | } |
| 547 | |
| 548 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandle) { |
| 549 | return CommonPrivilegedProcessMain(&SendHandleCallback, "SendHandle"); |
| 550 | } |
| 551 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 552 | void SendHandleWithoutPermissionsCallback(IPC::Sender* sender, |
| 553 | const IPC::Message& message) { |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 554 | ScopedHandle h(GetHandleFromTestHandleWinMsg(message)); |
| 555 | if (h.Get() != nullptr) { |
| 556 | SetFilePointer(h.Get(), 0, nullptr, FILE_BEGIN); |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 557 | |
| 558 | char buffer[100]; |
| 559 | DWORD bytes_read; |
| 560 | BOOL success = |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 561 | ::ReadFile(h.Get(), buffer, static_cast<DWORD>(strlen(kDataBuffer)), |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 562 | &bytes_read, nullptr); |
| 563 | if (!success && GetLastError() == ERROR_ACCESS_DENIED) { |
| 564 | SendControlMessage(sender, true); |
| 565 | return; |
| 566 | } |
| 567 | } |
| 568 | |
| 569 | SendControlMessage(sender, false); |
| 570 | } |
| 571 | |
| 572 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleWithoutPermissions) { |
| 573 | return CommonPrivilegedProcessMain(&SendHandleWithoutPermissionsCallback, |
| 574 | "SendHandleWithoutPermissions"); |
| 575 | } |
| 576 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 577 | void SendHandleToSelfCallback(IPC::Sender* sender, const IPC::Message&) { |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 578 | // Do nothing special. The default behavior already runs the |
| 579 | // AttachmentBrokerPrivilegedWin. |
| 580 | } |
| 581 | |
| 582 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleToSelf) { |
| 583 | return CommonPrivilegedProcessMain(&SendHandleToSelfCallback, |
| 584 | "SendHandleToSelf"); |
| 585 | } |
| 586 | |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 587 | void SendTwoHandlesCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 588 | // Check for two handles. |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 589 | HANDLE h1, h2; |
| 590 | EXPECT_TRUE(GetHandleFromTestTwoHandleWinMsg(message, &h1, &h2)); |
erikchen | 98daa73 | 2015-09-25 18:30:03 | [diff] [blame] | 591 | if (h1 == nullptr || h2 == nullptr) { |
| 592 | SendControlMessage(sender, false); |
| 593 | return; |
| 594 | } |
| 595 | |
| 596 | // Check that their contents are correct. |
| 597 | std::string contents1 = ReadFromFile(h1); |
| 598 | std::string contents2 = ReadFromFile(h2); |
| 599 | if (contents1 != std::string(kDataBuffer) || |
| 600 | contents2 != std::string(kDataBuffer)) { |
| 601 | SendControlMessage(sender, false); |
| 602 | return; |
| 603 | } |
| 604 | |
| 605 | // Check that the handles point to the same file. |
| 606 | const char text[] = "katy perry"; |
| 607 | DWORD bytes_written = 0; |
| 608 | SetFilePointer(h1, 0, nullptr, FILE_BEGIN); |
| 609 | BOOL success = ::WriteFile(h1, text, static_cast<DWORD>(strlen(text)), |
| 610 | &bytes_written, nullptr); |
| 611 | if (!success) { |
| 612 | SendControlMessage(sender, false); |
| 613 | return; |
| 614 | } |
| 615 | |
| 616 | SetFilePointer(h2, 0, nullptr, FILE_BEGIN); |
| 617 | char buffer[100]; |
| 618 | DWORD bytes_read; |
| 619 | success = ::ReadFile(h2, buffer, static_cast<DWORD>(strlen(text)), |
| 620 | &bytes_read, nullptr); |
| 621 | if (!success) { |
| 622 | SendControlMessage(sender, false); |
| 623 | return; |
| 624 | } |
| 625 | success = std::string(buffer, bytes_read) == std::string(text); |
| 626 | SendControlMessage(sender, success); |
| 627 | } |
| 628 | |
| 629 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendTwoHandles) { |
| 630 | return CommonPrivilegedProcessMain(&SendTwoHandlesCallback, "SendTwoHandles"); |
| 631 | } |
| 632 | |
| 633 | void SendHandleTwiceCallback(IPC::Sender* sender, const IPC::Message& message) { |
| 634 | // We expect the same message twice. |
| 635 | static int i = 0; |
| 636 | static bool success = true; |
| 637 | success &= CheckContentsOfTestMessage(message); |
| 638 | if (i == 0) { |
| 639 | LOG(INFO) << "Received first message."; |
| 640 | ++i; |
| 641 | } else { |
| 642 | LOG(INFO) << "Received second message."; |
| 643 | SendControlMessage(sender, success); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendHandleTwice) { |
| 648 | return CommonPrivilegedProcessMain(&SendHandleTwiceCallback, |
| 649 | "SendHandleTwice"); |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 650 | } |
| 651 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 652 | void SendSharedMemoryHandleCallback(IPC::Sender* sender, |
| 653 | const IPC::Message& message) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 654 | std::unique_ptr<base::SharedMemory> shared_memory = |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 655 | GetSharedMemoryFromSharedMemoryHandleMsg1(message, kSharedMemorySize); |
| 656 | bool success = |
| 657 | memcmp(shared_memory->memory(), kDataBuffer, strlen(kDataBuffer)) == 0; |
| 658 | SendControlMessage(sender, success); |
| 659 | } |
| 660 | |
| 661 | MULTIPROCESS_IPC_TEST_CLIENT_MAIN(SendSharedMemoryHandle) { |
| 662 | return CommonPrivilegedProcessMain(&SendSharedMemoryHandleCallback, |
| 663 | "SendSharedMemoryHandle"); |
| 664 | } |
| 665 | |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 666 | } // namespace |