1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li // https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li
15*ec63e07aSXin Li // Implementation of the sandbox2::IPC class
16*ec63e07aSXin Li
17*ec63e07aSXin Li #include "sandboxed_api/sandbox2/ipc.h"
18*ec63e07aSXin Li
19*ec63e07aSXin Li #include <sys/socket.h>
20*ec63e07aSXin Li #include <unistd.h>
21*ec63e07aSXin Li
22*ec63e07aSXin Li #include <memory>
23*ec63e07aSXin Li #include <string>
24*ec63e07aSXin Li #include <tuple>
25*ec63e07aSXin Li #include <vector>
26*ec63e07aSXin Li #include <thread>
27*ec63e07aSXin Li
28*ec63e07aSXin Li #include "absl/log/log.h"
29*ec63e07aSXin Li #include "absl/strings/string_view.h"
30*ec63e07aSXin Li #include "sandboxed_api/sandbox2/comms.h"
31*ec63e07aSXin Li #include "sandboxed_api/sandbox2/logserver.h"
32*ec63e07aSXin Li #include "sandboxed_api/sandbox2/logsink.h"
33*ec63e07aSXin Li #include "sandboxed_api/util/raw_logging.h"
34*ec63e07aSXin Li
35*ec63e07aSXin Li namespace sandbox2 {
36*ec63e07aSXin Li
SetUpServerSideComms(int fd)37*ec63e07aSXin Li void IPC::SetUpServerSideComms(int fd) { comms_ = std::make_unique<Comms>(fd); }
38*ec63e07aSXin Li
MapFd(int local_fd,int remote_fd)39*ec63e07aSXin Li void IPC::MapFd(int local_fd, int remote_fd) {
40*ec63e07aSXin Li VLOG(3) << "Will send: " << local_fd << ", to overwrite: " << remote_fd;
41*ec63e07aSXin Li fd_map_.push_back(std::make_tuple(local_fd, remote_fd, ""));
42*ec63e07aSXin Li }
43*ec63e07aSXin Li
MapDupedFd(int local_fd,int remote_fd)44*ec63e07aSXin Li void IPC::MapDupedFd(int local_fd, int remote_fd) {
45*ec63e07aSXin Li const int dup_local_fd = dup(local_fd);
46*ec63e07aSXin Li if (dup_local_fd == -1) {
47*ec63e07aSXin Li PLOG(FATAL) << "dup(" << local_fd << ")";
48*ec63e07aSXin Li }
49*ec63e07aSXin Li VLOG(3) << "Will send: " << dup_local_fd << " (dup of " << local_fd
50*ec63e07aSXin Li << "), to overwrite: " << remote_fd;
51*ec63e07aSXin Li fd_map_.push_back(std::make_tuple(dup_local_fd, remote_fd, ""));
52*ec63e07aSXin Li }
53*ec63e07aSXin Li
ReceiveFd(int remote_fd)54*ec63e07aSXin Li int IPC::ReceiveFd(int remote_fd) { return ReceiveFd(remote_fd, ""); }
55*ec63e07aSXin Li
ReceiveFd(absl::string_view name)56*ec63e07aSXin Li int IPC::ReceiveFd(absl::string_view name) { return ReceiveFd(-1, name); }
57*ec63e07aSXin Li
ReceiveFd(int remote_fd,absl::string_view name)58*ec63e07aSXin Li int IPC::ReceiveFd(int remote_fd, absl::string_view name) {
59*ec63e07aSXin Li int sv[2];
60*ec63e07aSXin Li if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) {
61*ec63e07aSXin Li PLOG(FATAL) << "socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0)";
62*ec63e07aSXin Li }
63*ec63e07aSXin Li VLOG(3) << "Created a socketpair (" << sv[0] << "/" << sv[1] << "), "
64*ec63e07aSXin Li << "which will overwrite remote_fd: " << remote_fd;
65*ec63e07aSXin Li fd_map_.push_back(std::make_tuple(sv[1], remote_fd, std::string(name)));
66*ec63e07aSXin Li return sv[0];
67*ec63e07aSXin Li }
68*ec63e07aSXin Li
SendFdsOverComms()69*ec63e07aSXin Li bool IPC::SendFdsOverComms() {
70*ec63e07aSXin Li if (!(comms_->SendUint32(fd_map_.size()))) {
71*ec63e07aSXin Li LOG(ERROR) << "Couldn't send IPC fd size";
72*ec63e07aSXin Li return false;
73*ec63e07aSXin Li }
74*ec63e07aSXin Li
75*ec63e07aSXin Li for (const auto& fd_tuple : fd_map_) {
76*ec63e07aSXin Li if (!(comms_->SendInt32(std::get<1>(fd_tuple)))) {
77*ec63e07aSXin Li LOG(ERROR) << "SendInt32: Couldn't send " << std::get<1>(fd_tuple);
78*ec63e07aSXin Li return false;
79*ec63e07aSXin Li }
80*ec63e07aSXin Li if (!(comms_->SendFD(std::get<0>(fd_tuple)))) {
81*ec63e07aSXin Li LOG(ERROR) << "SendFd: Couldn't send " << std::get<0>(fd_tuple);
82*ec63e07aSXin Li return false;
83*ec63e07aSXin Li }
84*ec63e07aSXin Li
85*ec63e07aSXin Li if (!(comms_->SendString(std::get<2>(fd_tuple)))) {
86*ec63e07aSXin Li LOG(ERROR) << "SendString: Couldn't send " << std::get<2>(fd_tuple);
87*ec63e07aSXin Li return false;
88*ec63e07aSXin Li }
89*ec63e07aSXin Li VLOG(3) << "IPC: local_fd: " << std::get<0>(fd_tuple)
90*ec63e07aSXin Li << ", remote_fd: " << std::get<1>(fd_tuple) << " sent";
91*ec63e07aSXin Li }
92*ec63e07aSXin Li
93*ec63e07aSXin Li return true;
94*ec63e07aSXin Li }
95*ec63e07aSXin Li
InternalCleanupFdMap()96*ec63e07aSXin Li void IPC::InternalCleanupFdMap() {
97*ec63e07aSXin Li for (const auto& fd_tuple : fd_map_) {
98*ec63e07aSXin Li close(std::get<0>(fd_tuple));
99*ec63e07aSXin Li }
100*ec63e07aSXin Li fd_map_.clear();
101*ec63e07aSXin Li }
102*ec63e07aSXin Li
EnableLogServer()103*ec63e07aSXin Li void IPC::EnableLogServer() {
104*ec63e07aSXin Li int fd = ReceiveFd(LogSink::kLogFDName);
105*ec63e07aSXin Li auto logger = [fd] {
106*ec63e07aSXin Li LogServer log_server(fd);
107*ec63e07aSXin Li log_server.Run();
108*ec63e07aSXin Li };
109*ec63e07aSXin Li std::thread log_thread{logger};
110*ec63e07aSXin Li log_thread.detach();
111*ec63e07aSXin Li }
112*ec63e07aSXin Li
113*ec63e07aSXin Li } // namespace sandbox2
114