named_pipe_interception.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2006-2008 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. #include "sandbox/win/src/named_pipe_interception.h"
  5. #include "sandbox/win/src/crosscall_client.h"
  6. #include "sandbox/win/src/ipc_tags.h"
  7. #include "sandbox/win/src/policy_params.h"
  8. #include "sandbox/win/src/policy_target.h"
  9. #include "sandbox/win/src/sandbox_factory.h"
  10. #include "sandbox/win/src/sandbox_nt_util.h"
  11. #include "sandbox/win/src/sharedmem_ipc_client.h"
  12. #include "sandbox/win/src/target_services.h"
  13. namespace sandbox {
  14. HANDLE WINAPI
  15. TargetCreateNamedPipeW(CreateNamedPipeWFunction orig_CreateNamedPipeW,
  16. LPCWSTR pipe_name,
  17. DWORD open_mode,
  18. DWORD pipe_mode,
  19. DWORD max_instance,
  20. DWORD out_buffer_size,
  21. DWORD in_buffer_size,
  22. DWORD default_timeout,
  23. LPSECURITY_ATTRIBUTES security_attributes) {
  24. HANDLE pipe = orig_CreateNamedPipeW(
  25. pipe_name, open_mode, pipe_mode, max_instance, out_buffer_size,
  26. in_buffer_size, default_timeout, security_attributes);
  27. if (INVALID_HANDLE_VALUE != pipe)
  28. return pipe;
  29. // We don't trust that the IPC can work this early.
  30. if (!SandboxFactory::GetTargetServices()->GetState()->InitCalled())
  31. return INVALID_HANDLE_VALUE;
  32. DWORD original_error = ::GetLastError();
  33. // We don't support specific Security Attributes.
  34. if (security_attributes)
  35. return INVALID_HANDLE_VALUE;
  36. do {
  37. void* memory = GetGlobalIPCMemory();
  38. if (!memory)
  39. break;
  40. CountedParameterSet<NameBased> params;
  41. params[NameBased::NAME] = ParamPickerMake(pipe_name);
  42. if (!QueryBroker(IpcTag::CREATENAMEDPIPEW, params.GetBase()))
  43. break;
  44. SharedMemIPCClient ipc(memory);
  45. CrossCallReturn answer = {0};
  46. ResultCode code =
  47. CrossCall(ipc, IpcTag::CREATENAMEDPIPEW, pipe_name, open_mode,
  48. pipe_mode, max_instance, out_buffer_size, in_buffer_size,
  49. default_timeout, &answer);
  50. if (SBOX_ALL_OK != code)
  51. break;
  52. ::SetLastError(answer.win32_result);
  53. if (ERROR_SUCCESS != answer.win32_result)
  54. return INVALID_HANDLE_VALUE;
  55. return answer.handle;
  56. } while (false);
  57. ::SetLastError(original_error);
  58. return INVALID_HANDLE_VALUE;
  59. }
  60. } // namespace sandbox