michael@0: // Copyright (c) 2012 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/callback.h" michael@0: #include "base/logging.h" michael@0: #include "base/memory/scoped_ptr.h" michael@0: #include "sandbox/win/src/sharedmem_ipc_server.h" michael@0: #include "sandbox/win/src/sharedmem_ipc_client.h" michael@0: #include "sandbox/win/src/sandbox.h" michael@0: #include "sandbox/win/src/sandbox_types.h" michael@0: #include "sandbox/win/src/crosscall_params.h" michael@0: #include "sandbox/win/src/crosscall_server.h" michael@0: michael@0: namespace sandbox { michael@0: michael@0: SharedMemIPCServer::SharedMemIPCServer(HANDLE target_process, michael@0: DWORD target_process_id, michael@0: HANDLE target_job, michael@0: ThreadProvider* thread_provider, michael@0: Dispatcher* dispatcher) michael@0: : client_control_(NULL), michael@0: thread_provider_(thread_provider), michael@0: target_process_(target_process), michael@0: target_process_id_(target_process_id), michael@0: target_job_object_(target_job), michael@0: call_dispatcher_(dispatcher) { michael@0: } michael@0: michael@0: SharedMemIPCServer::~SharedMemIPCServer() { michael@0: // Free the wait handles associated with the thread pool. michael@0: if (!thread_provider_->UnRegisterWaits(this)) { michael@0: // Better to leak than to crash. michael@0: return; michael@0: } michael@0: // Free the IPC signal events. michael@0: ServerContexts::iterator it; michael@0: for (it = server_contexts_.begin(); it != server_contexts_.end(); ++it) { michael@0: ServerControl* context = (*it); michael@0: ::CloseHandle(context->ping_event); michael@0: ::CloseHandle(context->pong_event); michael@0: delete context; michael@0: } michael@0: } michael@0: michael@0: bool SharedMemIPCServer::Init(void* shared_mem, uint32 shared_size, michael@0: uint32 channel_size) { michael@0: // The shared memory needs to be at least as big as a channel. michael@0: if (shared_size < channel_size) { michael@0: return false; michael@0: } michael@0: // The channel size should be aligned. michael@0: if (0 != (channel_size % 32)) { michael@0: return false; michael@0: } michael@0: michael@0: // Calculate how many channels we can fit in the shared memory. michael@0: shared_size -= offsetof(IPCControl, channels); michael@0: size_t channel_count = shared_size / (sizeof(ChannelControl) + channel_size); michael@0: michael@0: // If we cannot fit even one channel we bail out. michael@0: if (0 == channel_count) { michael@0: return false; michael@0: } michael@0: // Calculate the start of the first channel. michael@0: size_t base_start = (sizeof(ChannelControl)* channel_count) + michael@0: offsetof(IPCControl, channels); michael@0: michael@0: client_control_ = reinterpret_cast(shared_mem); michael@0: client_control_->channels_count = 0; michael@0: michael@0: // This is the initialization that we do per-channel. Basically: michael@0: // 1) make two events (ping & pong) michael@0: // 2) create handles to the events for the client and the server. michael@0: // 3) initialize the channel (client_context) with the state. michael@0: // 4) initialize the server side of the channel (service_context). michael@0: // 5) call the thread provider RegisterWait to register the ping events. michael@0: for (size_t ix = 0; ix != channel_count; ++ix) { michael@0: ChannelControl* client_context = &client_control_->channels[ix]; michael@0: ServerControl* service_context = new ServerControl; michael@0: server_contexts_.push_back(service_context); michael@0: michael@0: if (!MakeEvents(&service_context->ping_event, michael@0: &service_context->pong_event, michael@0: &client_context->ping_event, michael@0: &client_context->pong_event)) { michael@0: return false; michael@0: } michael@0: michael@0: client_context->channel_base = base_start; michael@0: client_context->state = kFreeChannel; michael@0: michael@0: // Note that some of these values are available as members of this michael@0: // object but we put them again into the service_context because we michael@0: // will be called on a static method (ThreadPingEventReady) michael@0: service_context->shared_base = reinterpret_cast(shared_mem); michael@0: service_context->channel_size = channel_size; michael@0: service_context->channel = client_context; michael@0: service_context->channel_buffer = service_context->shared_base + michael@0: client_context->channel_base; michael@0: service_context->dispatcher = call_dispatcher_; michael@0: service_context->target_info.process = target_process_; michael@0: service_context->target_info.process_id = target_process_id_; michael@0: service_context->target_info.job_object = target_job_object_; michael@0: // Advance to the next channel. michael@0: base_start += channel_size; michael@0: // Register the ping event with the threadpool. michael@0: thread_provider_->RegisterWait(this, service_context->ping_event, michael@0: ThreadPingEventReady, service_context); michael@0: } michael@0: michael@0: // We create a mutex that the server locks. If the server dies unexpectedly, michael@0: // the thread that owns it will fail to release the lock and windows will michael@0: // report to the target (when it tries to acquire it) that the wait was michael@0: // abandoned. Note: We purposely leak the local handle because we want it to michael@0: // be closed by Windows itself so it is properly marked as abandoned if the michael@0: // server dies. michael@0: if (!::DuplicateHandle(::GetCurrentProcess(), michael@0: ::CreateMutexW(NULL, TRUE, NULL), michael@0: target_process_, &client_control_->server_alive, michael@0: SYNCHRONIZE | EVENT_MODIFY_STATE, FALSE, 0)) { michael@0: return false; michael@0: } michael@0: // This last setting indicates to the client all is setup. michael@0: client_control_->channels_count = channel_count; michael@0: return true; michael@0: } michael@0: michael@0: // Releases memory allocated for IPC arguments, if needed. michael@0: void ReleaseArgs(const IPCParams* ipc_params, void* args[kMaxIpcParams]) { michael@0: for (size_t i = 0; i < kMaxIpcParams; i++) { michael@0: switch (ipc_params->args[i]) { michael@0: case WCHAR_TYPE: { michael@0: delete reinterpret_cast(args[i]); michael@0: args[i] = NULL; michael@0: break; michael@0: } michael@0: case INOUTPTR_TYPE: { michael@0: delete reinterpret_cast(args[i]); michael@0: args[i] = NULL; michael@0: break; michael@0: } michael@0: default: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Fills up the list of arguments (args and ipc_params) for an IPC call. michael@0: bool GetArgs(CrossCallParamsEx* params, IPCParams* ipc_params, michael@0: void* args[kMaxIpcParams]) { michael@0: if (kMaxIpcParams < params->GetParamsCount()) michael@0: return false; michael@0: michael@0: for (uint32 i = 0; i < params->GetParamsCount(); i++) { michael@0: uint32 size; michael@0: ArgType type; michael@0: args[i] = params->GetRawParameter(i, &size, &type); michael@0: if (args[i]) { michael@0: ipc_params->args[i] = type; michael@0: switch (type) { michael@0: case WCHAR_TYPE: { michael@0: scoped_ptr data(new std::wstring); michael@0: if (!params->GetParameterStr(i, data.get())) { michael@0: args[i] = 0; michael@0: ReleaseArgs(ipc_params, args); michael@0: return false; michael@0: } michael@0: args[i] = data.release(); michael@0: break; michael@0: } michael@0: case ULONG_TYPE: { michael@0: uint32 data; michael@0: if (!params->GetParameter32(i, &data)) { michael@0: ReleaseArgs(ipc_params, args); michael@0: return false; michael@0: } michael@0: IPCInt ipc_int(data); michael@0: args[i] = ipc_int.AsVoidPtr(); michael@0: break; michael@0: } michael@0: case VOIDPTR_TYPE : { michael@0: void* data; michael@0: if (!params->GetParameterVoidPtr(i, &data)) { michael@0: ReleaseArgs(ipc_params, args); michael@0: return false; michael@0: } michael@0: args[i] = data; michael@0: break; michael@0: } michael@0: case INOUTPTR_TYPE: { michael@0: if (!args[i]) { michael@0: ReleaseArgs(ipc_params, args); michael@0: return false; michael@0: } michael@0: CountedBuffer* buffer = new CountedBuffer(args[i] , size); michael@0: args[i] = buffer; michael@0: break; michael@0: } michael@0: default: break; michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: bool SharedMemIPCServer::InvokeCallback(const ServerControl* service_context, michael@0: void* ipc_buffer, michael@0: CrossCallReturn* call_result) { michael@0: // Set the default error code; michael@0: SetCallError(SBOX_ERROR_INVALID_IPC, call_result); michael@0: uint32 output_size = 0; michael@0: // Parse, verify and copy the message. The handler operates on a copy michael@0: // of the message so the client cannot play dirty tricks by changing the michael@0: // data in the channel while the IPC is being processed. michael@0: scoped_ptr params( michael@0: CrossCallParamsEx::CreateFromBuffer(ipc_buffer, michael@0: service_context->channel_size, michael@0: &output_size)); michael@0: if (!params.get()) michael@0: return false; michael@0: michael@0: uint32 tag = params->GetTag(); michael@0: COMPILE_ASSERT(0 == INVALID_TYPE, Incorrect_type_enum); michael@0: IPCParams ipc_params = {0}; michael@0: ipc_params.ipc_tag = tag; michael@0: michael@0: void* args[kMaxIpcParams]; michael@0: if (!GetArgs(params.get(), &ipc_params, args)) michael@0: return false; michael@0: michael@0: IPCInfo ipc_info = {0}; michael@0: ipc_info.ipc_tag = tag; michael@0: ipc_info.client_info = &service_context->target_info; michael@0: Dispatcher* dispatcher = service_context->dispatcher; michael@0: DCHECK(dispatcher); michael@0: bool error = true; michael@0: Dispatcher* handler = NULL; michael@0: michael@0: Dispatcher::CallbackGeneric callback_generic; michael@0: handler = dispatcher->OnMessageReady(&ipc_params, &callback_generic); michael@0: if (handler) { michael@0: switch (params->GetParamsCount()) { michael@0: case 0: { michael@0: // Ask the IPC dispatcher if she can service this IPC. michael@0: Dispatcher::Callback0 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info)) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 1: { michael@0: Dispatcher::Callback1 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 2: { michael@0: Dispatcher::Callback2 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 3: { michael@0: Dispatcher::Callback3 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 4: { michael@0: Dispatcher::Callback4 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2], michael@0: args[3])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 5: { michael@0: Dispatcher::Callback5 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2], args[3], michael@0: args[4])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 6: { michael@0: Dispatcher::Callback6 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2], args[3], michael@0: args[4], args[5])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 7: { michael@0: Dispatcher::Callback7 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2], args[3], michael@0: args[4], args[5], args[6])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 8: { michael@0: Dispatcher::Callback8 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2], args[3], michael@0: args[4], args[5], args[6], args[7])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: case 9: { michael@0: Dispatcher::Callback9 callback = michael@0: reinterpret_cast(callback_generic); michael@0: if (!(handler->*callback)(&ipc_info, args[0], args[1], args[2], args[3], michael@0: args[4], args[5], args[6], args[7], args[8])) michael@0: break; michael@0: error = false; michael@0: break; michael@0: } michael@0: default: { michael@0: NOTREACHED(); michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (error) { michael@0: if (handler) michael@0: SetCallError(SBOX_ERROR_FAILED_IPC, call_result); michael@0: } else { michael@0: memcpy(call_result, &ipc_info.return_info, sizeof(*call_result)); michael@0: SetCallSuccess(call_result); michael@0: if (params->IsInOut()) { michael@0: // Maybe the params got changed by the broker. We need to upadte the michael@0: // memory section. michael@0: memcpy(ipc_buffer, params.get(), output_size); michael@0: } michael@0: } michael@0: michael@0: ReleaseArgs(&ipc_params, args); michael@0: michael@0: return !error; michael@0: } michael@0: michael@0: // This function gets called by a thread from the thread pool when a michael@0: // ping event fires. The context is the same as passed in the RegisterWait() michael@0: // call above. michael@0: void __stdcall SharedMemIPCServer::ThreadPingEventReady(void* context, michael@0: unsigned char) { michael@0: if (NULL == context) { michael@0: DCHECK(false); michael@0: return; michael@0: } michael@0: ServerControl* service_context = reinterpret_cast(context); michael@0: // Since the event fired, the channel *must* be busy. Change to kAckChannel michael@0: // while we service it. michael@0: LONG last_state = michael@0: ::InterlockedCompareExchange(&service_context->channel->state, michael@0: kAckChannel, kBusyChannel); michael@0: if (kBusyChannel != last_state) { michael@0: DCHECK(false); michael@0: return; michael@0: } michael@0: michael@0: // Prepare the result structure. At this point we will return some result michael@0: // even if the IPC is invalid, malformed or has no handler. michael@0: CrossCallReturn call_result = {0}; michael@0: void* buffer = service_context->channel_buffer; michael@0: michael@0: InvokeCallback(service_context, buffer, &call_result); michael@0: michael@0: // Copy the answer back into the channel and signal the pong event. This michael@0: // should wake up the client so he can finish the the ipc cycle. michael@0: CrossCallParams* call_params = reinterpret_cast(buffer); michael@0: memcpy(call_params->GetCallReturn(), &call_result, sizeof(call_result)); michael@0: ::InterlockedExchange(&service_context->channel->state, kAckChannel); michael@0: ::SetEvent(service_context->pong_event); michael@0: } michael@0: michael@0: bool SharedMemIPCServer::MakeEvents(HANDLE* server_ping, HANDLE* server_pong, michael@0: HANDLE* client_ping, HANDLE* client_pong) { michael@0: // Note that the IPC client has no right to delete the events. That would michael@0: // cause problems. The server *owns* the events. michael@0: const DWORD kDesiredAccess = SYNCHRONIZE | EVENT_MODIFY_STATE; michael@0: michael@0: // The events are auto reset, and start not signaled. michael@0: *server_ping = ::CreateEventW(NULL, FALSE, FALSE, NULL); michael@0: if (!::DuplicateHandle(::GetCurrentProcess(), *server_ping, target_process_, michael@0: client_ping, kDesiredAccess, FALSE, 0)) { michael@0: return false; michael@0: } michael@0: *server_pong = ::CreateEventW(NULL, FALSE, FALSE, NULL); michael@0: if (!::DuplicateHandle(::GetCurrentProcess(), *server_pong, target_process_, michael@0: client_pong, kDesiredAccess, FALSE, 0)) { michael@0: return false; michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: } // namespace sandbox