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/services/linux_syscalls.h"
24 #include "sandbox/linux/syscall_broker/broker_common.h"
25 #include "sandbox/linux/syscall_broker/broker_policy.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 // Always pass a defined |mode| in case flags mistakenly contains O_CREAT.
43 if (IsRunningOnValgrind()) {
44 // Valgrind does not support AT_FDCWD, just use libc's open() in this case.
45 return open(pathname, flags, mode);
47 return syscall(__NR_openat, AT_FDCWD, pathname, flags, mode);
51 // Open |requested_filename| with |flags| if allowed by our policy.
52 // Write the syscall return value (-errno) to |write_pickle| and append
53 // a file descriptor to |opened_files| if relevant.
54 void OpenFileForIPC(const BrokerPolicy& policy,
55 const std::string& requested_filename,
58 std::vector<int>* opened_files) {
61 const char* file_to_open = NULL;
62 const bool safe_to_open_file = policy.GetFileNameIfAllowedToOpen(
63 requested_filename.c_str(), flags, &file_to_open);
65 if (safe_to_open_file) {
67 int opened_fd = sys_open(file_to_open, flags);
69 write_pickle->WriteInt(-errno);
72 opened_files->push_back(opened_fd);
73 write_pickle->WriteInt(0);
76 write_pickle->WriteInt(-policy.denied_errno());
80 // Perform access(2) on |requested_filename| with mode |mode| if allowed by our
81 // policy. Write the syscall return value (-errno) to |write_pickle|.
82 void AccessFileForIPC(const BrokerPolicy& policy,
83 const std::string& requested_filename,
85 Pickle* write_pickle) {
87 const char* file_to_access = NULL;
88 const bool safe_to_access_file = policy.GetFileNameIfAllowedToAccess(
89 requested_filename.c_str(), mode, &file_to_access);
91 if (safe_to_access_file) {
92 CHECK(file_to_access);
93 int access_ret = access(file_to_access, mode);
94 int access_errno = errno;
96 write_pickle->WriteInt(0);
98 write_pickle->WriteInt(-access_errno);
100 write_pickle->WriteInt(-policy.denied_errno());
104 // Handle a |command_type| request contained in |read_pickle| and send the reply
106 // Currently COMMAND_OPEN and COMMAND_ACCESS are supported.
107 bool HandleRemoteCommand(const BrokerPolicy& policy,
108 IPCCommand command_type,
110 const Pickle& read_pickle,
111 PickleIterator iter) {
112 // Currently all commands have two arguments: filename and flags.
113 std::string requested_filename;
115 if (!read_pickle.ReadString(&iter, &requested_filename) ||
116 !read_pickle.ReadInt(&iter, &flags)) {
121 std::vector<int> opened_files;
123 switch (command_type) {
125 AccessFileForIPC(policy, requested_filename, flags, &write_pickle);
129 policy, requested_filename, flags, &write_pickle, &opened_files);
132 LOG(ERROR) << "Invalid IPC command";
136 CHECK_LE(write_pickle.size(), kMaxMessageLength);
137 ssize_t sent = UnixDomainSocket::SendMsg(
138 reply_ipc, write_pickle.data(), write_pickle.size(), opened_files);
140 // Close anything we have opened in this process.
141 for (std::vector<int>::iterator it = opened_files.begin();
142 it != opened_files.end();
144 int ret = IGNORE_EINTR(close(*it));
145 DCHECK(!ret) << "Could not close file descriptor";
149 LOG(ERROR) << "Could not send IPC reply";
157 BrokerHost::BrokerHost(const BrokerPolicy& broker_policy, int ipc_channel)
158 : broker_policy_(broker_policy), ipc_channel_(ipc_channel) {
161 BrokerHost::~BrokerHost() {
164 // Handle a request on the IPC channel ipc_channel_.
165 // A request should have a file descriptor attached on which we will reply and
166 // that we will then close.
167 // A request should start with an int that will be used as the command type.
168 bool BrokerHost::HandleRequest() const {
169 ScopedVector<base::ScopedFD> fds;
170 char buf[kMaxMessageLength];
172 const ssize_t msg_len =
173 UnixDomainSocket::RecvMsg(ipc_channel_, buf, sizeof(buf), &fds);
175 if (msg_len == 0 || (msg_len == -1 && errno == ECONNRESET)) {
176 // EOF from the client, or the client died, we should die.
177 // TODO(jln): change this.
181 // The client should send exactly one file descriptor, on which we
182 // will write the reply.
183 // TODO(mdempsky): ScopedVector doesn't have 'at()', only 'operator[]'.
184 if (msg_len < 0 || fds.size() != 1 || fds[0]->get() < 0) {
185 PLOG(ERROR) << "Error reading message from the client";
189 base::ScopedFD temporary_ipc(fds[0]->Pass());
191 Pickle pickle(buf, msg_len);
192 PickleIterator iter(pickle);
194 if (pickle.ReadInt(&iter, &command_type)) {
196 // Go through all the possible IPC messages.
197 switch (command_type) {
200 // We reply on the file descriptor sent to us via the IPC channel.
201 r = HandleRemoteCommand(broker_policy_,
202 static_cast<IPCCommand>(command_type),
215 LOG(ERROR) << "Error parsing IPC request";
219 } // namespace syscall_broker
221 } // namespace sandbox