Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / sandbox / win / src / named_pipe_dispatcher.cc
blob53bb7c4c658d0d81d3f9222271eac1b69e8061ae
1 // Copyright (c) 2013 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.
5 #include "sandbox/win/src/named_pipe_dispatcher.h"
7 #include "base/basictypes.h"
8 #include "base/strings/string_split.h"
10 #include "sandbox/win/src/crosscall_client.h"
11 #include "sandbox/win/src/interception.h"
12 #include "sandbox/win/src/interceptors.h"
13 #include "sandbox/win/src/ipc_tags.h"
14 #include "sandbox/win/src/named_pipe_interception.h"
15 #include "sandbox/win/src/named_pipe_policy.h"
16 #include "sandbox/win/src/policy_broker.h"
17 #include "sandbox/win/src/policy_params.h"
18 #include "sandbox/win/src/sandbox.h"
21 namespace sandbox {
23 NamedPipeDispatcher::NamedPipeDispatcher(PolicyBase* policy_base)
24 : policy_base_(policy_base) {
25 static const IPCCall create_params = {
26 {IPC_CREATENAMEDPIPEW_TAG,
27 {WCHAR_TYPE,
28 UINT32_TYPE,
29 UINT32_TYPE,
30 UINT32_TYPE,
31 UINT32_TYPE,
32 UINT32_TYPE,
33 UINT32_TYPE}},
34 reinterpret_cast<CallbackGeneric>(&NamedPipeDispatcher::CreateNamedPipe)};
36 ipc_calls_.push_back(create_params);
39 bool NamedPipeDispatcher::SetupService(InterceptionManager* manager,
40 int service) {
41 if (IPC_CREATENAMEDPIPEW_TAG == service)
42 return INTERCEPT_EAT(manager, kKerneldllName, CreateNamedPipeW,
43 CREATE_NAMED_PIPE_ID, 36);
45 return false;
48 bool NamedPipeDispatcher::CreateNamedPipe(IPCInfo* ipc,
49 base::string16* name,
50 uint32 open_mode,
51 uint32 pipe_mode,
52 uint32 max_instances,
53 uint32 out_buffer_size,
54 uint32 in_buffer_size,
55 uint32 default_timeout) {
56 ipc->return_info.win32_result = ERROR_ACCESS_DENIED;
57 ipc->return_info.handle = INVALID_HANDLE_VALUE;
59 base::StringPiece16 dotdot(L"..");
61 for (const base::StringPiece16& path : base::SplitStringPiece(
62 *name, base::string16(1, '/'),
63 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
64 for (const base::StringPiece16& inner : base::SplitStringPiece(
65 path, base::string16(1, '\\'),
66 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL)) {
67 if (inner == dotdot)
68 return true;
72 const wchar_t* pipe_name = name->c_str();
73 CountedParameterSet<NameBased> params;
74 params[NameBased::NAME] = ParamPickerMake(pipe_name);
76 EvalResult eval = policy_base_->EvalPolicy(IPC_CREATENAMEDPIPEW_TAG,
77 params.GetBase());
79 // "For file I/O, the "\\?\" prefix to a path string tells the Windows APIs to
80 // disable all string parsing and to send the string that follows it straight
81 // to the file system."
82 // http://msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
83 // This ensures even if there is a path traversal in the pipe name, and it is
84 // able to get past the checks above, it will still not be allowed to escape
85 // our whitelisted namespace.
86 if (name->compare(0, 4, L"\\\\.\\") == 0)
87 name->replace(0, 4, L"\\\\\?\\");
89 HANDLE pipe;
90 DWORD ret = NamedPipePolicy::CreateNamedPipeAction(eval, *ipc->client_info,
91 *name, open_mode,
92 pipe_mode, max_instances,
93 out_buffer_size,
94 in_buffer_size,
95 default_timeout, &pipe);
97 ipc->return_info.win32_result = ret;
98 ipc->return_info.handle = pipe;
99 return true;
102 } // namespace sandbox