1 // Copyright 2014 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/linux/syscall_broker/broker_host.h"
8 #include <sys/socket.h>
10 #include <sys/syscall.h>
11 #include <sys/types.h>
17 #include "base/files/scoped_file.h"
18 #include "base/logging.h"
19 #include "base/pickle.h"
20 #include "base/posix/eintr_wrapper.h"
21 #include "base/posix/unix_domain_socket_linux.h"
22 #include "base/third_party/valgrind/valgrind.h"
23 #include "sandbox/linux/syscall_broker/broker_common.h"
24 #include "sandbox/linux/syscall_broker/broker_policy.h"
25 #include "sandbox/linux/system_headers/linux_syscalls.h"
29 namespace syscall_broker
{
33 bool IsRunningOnValgrind() {
34 return RUNNING_ON_VALGRIND
;
37 // A little open(2) wrapper to handle some oddities for us. In the general case
38 // make a direct system call since we want to keep in control of the broker
39 // process' system calls profile to be able to loosely sandbox it.
40 int sys_open(const char* pathname
, int flags
) {
41 // Hardcode mode to rw------- when creating files.
43 if (flags
& O_CREAT
) {
48 if (IsRunningOnValgrind()) {
49 // Valgrind does not support AT_FDCWD, just use libc's open() in this case.
50 return open(pathname
, flags
, mode
);
52 return syscall(__NR_openat
, AT_FDCWD
, pathname
, flags
, mode
);
56 // Open |requested_filename| with |flags| if allowed by our policy.
57 // Write the syscall return value (-errno) to |write_pickle| and append
58 // a file descriptor to |opened_files| if relevant.
59 void OpenFileForIPC(const BrokerPolicy
& policy
,
60 const std::string
& requested_filename
,
62 base::Pickle
* write_pickle
,
63 std::vector
<int>* opened_files
) {
66 const char* file_to_open
= NULL
;
67 bool unlink_after_open
= false;
68 const bool safe_to_open_file
= policy
.GetFileNameIfAllowedToOpen(
69 requested_filename
.c_str(), flags
, &file_to_open
, &unlink_after_open
);
71 if (safe_to_open_file
) {
73 int opened_fd
= sys_open(file_to_open
, flags
);
75 write_pickle
->WriteInt(-errno
);
78 if (unlink_after_open
) {
81 opened_files
->push_back(opened_fd
);
82 write_pickle
->WriteInt(0);
85 write_pickle
->WriteInt(-policy
.denied_errno());
89 // Perform access(2) on |requested_filename| with mode |mode| if allowed by our
90 // policy. Write the syscall return value (-errno) to |write_pickle|.
91 void AccessFileForIPC(const BrokerPolicy
& policy
,
92 const std::string
& requested_filename
,
94 base::Pickle
* write_pickle
) {
96 const char* file_to_access
= NULL
;
97 const bool safe_to_access_file
= policy
.GetFileNameIfAllowedToAccess(
98 requested_filename
.c_str(), mode
, &file_to_access
);
100 if (safe_to_access_file
) {
101 CHECK(file_to_access
);
102 int access_ret
= access(file_to_access
, mode
);
103 int access_errno
= errno
;
105 write_pickle
->WriteInt(0);
107 write_pickle
->WriteInt(-access_errno
);
109 write_pickle
->WriteInt(-policy
.denied_errno());
113 // Handle a |command_type| request contained in |iter| and send the reply
115 // Currently COMMAND_OPEN and COMMAND_ACCESS are supported.
116 bool HandleRemoteCommand(const BrokerPolicy
& policy
,
117 IPCCommand command_type
,
119 base::PickleIterator iter
) {
120 // Currently all commands have two arguments: filename and flags.
121 std::string requested_filename
;
123 if (!iter
.ReadString(&requested_filename
) || !iter
.ReadInt(&flags
))
126 base::Pickle write_pickle
;
127 std::vector
<int> opened_files
;
129 switch (command_type
) {
131 AccessFileForIPC(policy
, requested_filename
, flags
, &write_pickle
);
135 policy
, requested_filename
, flags
, &write_pickle
, &opened_files
);
138 LOG(ERROR
) << "Invalid IPC command";
142 CHECK_LE(write_pickle
.size(), kMaxMessageLength
);
143 ssize_t sent
= base::UnixDomainSocket::SendMsg(
144 reply_ipc
, write_pickle
.data(), write_pickle
.size(), opened_files
);
146 // Close anything we have opened in this process.
147 for (std::vector
<int>::iterator it
= opened_files
.begin();
148 it
!= opened_files
.end();
150 int ret
= IGNORE_EINTR(close(*it
));
151 DCHECK(!ret
) << "Could not close file descriptor";
155 LOG(ERROR
) << "Could not send IPC reply";
163 BrokerHost::BrokerHost(const BrokerPolicy
& broker_policy
,
164 BrokerChannel::EndPoint ipc_channel
)
165 : broker_policy_(broker_policy
), ipc_channel_(ipc_channel
.Pass()) {
168 BrokerHost::~BrokerHost() {
171 // Handle a request on the IPC channel ipc_channel_.
172 // A request should have a file descriptor attached on which we will reply and
173 // that we will then close.
174 // A request should start with an int that will be used as the command type.
175 BrokerHost::RequestStatus
BrokerHost::HandleRequest() const {
176 ScopedVector
<base::ScopedFD
> fds
;
177 char buf
[kMaxMessageLength
];
179 const ssize_t msg_len
= base::UnixDomainSocket::RecvMsg(
180 ipc_channel_
.get(), buf
, sizeof(buf
), &fds
);
182 if (msg_len
== 0 || (msg_len
== -1 && errno
== ECONNRESET
)) {
183 // EOF from the client, or the client died, we should die.
184 return RequestStatus::LOST_CLIENT
;
187 // The client should send exactly one file descriptor, on which we
188 // will write the reply.
189 // TODO(mdempsky): ScopedVector doesn't have 'at()', only 'operator[]'.
190 if (msg_len
< 0 || fds
.size() != 1 || fds
[0]->get() < 0) {
191 PLOG(ERROR
) << "Error reading message from the client";
192 return RequestStatus::FAILURE
;
195 base::ScopedFD
temporary_ipc(fds
[0]->Pass());
197 base::Pickle
pickle(buf
, msg_len
);
198 base::PickleIterator
iter(pickle
);
200 if (iter
.ReadInt(&command_type
)) {
201 bool command_handled
= false;
202 // Go through all the possible IPC messages.
203 switch (command_type
) {
206 // We reply on the file descriptor sent to us via the IPC channel.
207 command_handled
= HandleRemoteCommand(
208 broker_policy_
, static_cast<IPCCommand
>(command_type
),
209 temporary_ipc
.get(), iter
);
216 if (command_handled
) {
217 return RequestStatus::SUCCESS
;
219 return RequestStatus::FAILURE
;
225 LOG(ERROR
) << "Error parsing IPC request";
226 return RequestStatus::FAILURE
;
229 } // namespace syscall_broker
231 } // namespace sandbox