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"
23 NamedPipeDispatcher::NamedPipeDispatcher(PolicyBase
* policy_base
)
24 : policy_base_(policy_base
) {
25 static const IPCCall create_params
= {
26 {IPC_CREATENAMEDPIPEW_TAG
,
34 reinterpret_cast<CallbackGeneric
>(&NamedPipeDispatcher::CreateNamedPipe
)};
36 ipc_calls_
.push_back(create_params
);
39 bool NamedPipeDispatcher::SetupService(InterceptionManager
* manager
,
41 if (IPC_CREATENAMEDPIPEW_TAG
== service
)
42 return INTERCEPT_EAT(manager
, kKerneldllName
, CreateNamedPipeW
,
43 CREATE_NAMED_PIPE_ID
, 36);
48 bool NamedPipeDispatcher::CreateNamedPipe(IPCInfo
* ipc
,
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
)) {
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
,
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
"\\\\\?\\");
90 DWORD ret
= NamedPipePolicy::CreateNamedPipeAction(eval
, *ipc
->client_info
,
92 pipe_mode
, max_instances
,
95 default_timeout
, &pipe
);
97 ipc
->return_info
.win32_result
= ret
;
98 ipc
->return_info
.handle
= pipe
;
102 } // namespace sandbox