xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/ipc.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 // The sandbox2::IPC class provides routines for exchanging data between sandbox
16*ec63e07aSXin Li // and the sandboxee.
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #ifndef SANDBOXED_API_SANDBOX2_IPC_H_
19*ec63e07aSXin Li #define SANDBOXED_API_SANDBOX2_IPC_H_
20*ec63e07aSXin Li 
21*ec63e07aSXin Li #include <memory>
22*ec63e07aSXin Li #include <string>
23*ec63e07aSXin Li #include <tuple>
24*ec63e07aSXin Li #include <vector>
25*ec63e07aSXin Li 
26*ec63e07aSXin Li #include "absl/base/attributes.h"
27*ec63e07aSXin Li #include "absl/strings/string_view.h"
28*ec63e07aSXin Li #include "sandboxed_api/sandbox2/comms.h"
29*ec63e07aSXin Li 
30*ec63e07aSXin Li namespace sandbox2 {
31*ec63e07aSXin Li 
32*ec63e07aSXin Li class IPC final {
33*ec63e07aSXin Li  public:
34*ec63e07aSXin Li   IPC() = default;
35*ec63e07aSXin Li 
36*ec63e07aSXin Li   IPC(const IPC&) = delete;
37*ec63e07aSXin Li   IPC& operator=(const IPC&) = delete;
38*ec63e07aSXin Li 
~IPC()39*ec63e07aSXin Li   ~IPC() { InternalCleanupFdMap(); }
40*ec63e07aSXin Li 
41*ec63e07aSXin Li   ABSL_DEPRECATED("Use Sandbox2::comms() instead")
comms()42*ec63e07aSXin Li   Comms* comms() const { return comms_.get(); }
43*ec63e07aSXin Li 
44*ec63e07aSXin Li   // Marks local_fd so that it should be sent to the remote process (sandboxee),
45*ec63e07aSXin Li   // and duplicated onto remote_fd in it. The local_fd will be closed after
46*ec63e07aSXin Li   // being sent (in SendFdsOverComms() which is called by the Monitor class when
47*ec63e07aSXin Li   // Sandbox2::RunAsync() is called), so local_fd should not be used from that
48*ec63e07aSXin Li   // point on. The application must not close local_fd after calling MapFd().
49*ec63e07aSXin Li   void MapFd(int local_fd, int remote_fd);
50*ec63e07aSXin Li 
51*ec63e07aSXin Li   // Similar to MapFd(), except local_fd remains available for use in the
52*ec63e07aSXin Li   // application even after Sandbox2::RunAsync() is called; the application
53*ec63e07aSXin Li   // retains responsibility for closing local_fd and may do so at any time after
54*ec63e07aSXin Li   // calling MapDupedFd().
55*ec63e07aSXin Li   void MapDupedFd(int local_fd, int remote_fd);
56*ec63e07aSXin Li 
57*ec63e07aSXin Li   // Creates and returns a socketpair endpoint. The other endpoint of the
58*ec63e07aSXin Li   // socketpair is marked as to be sent to the remote process (sandboxee) with
59*ec63e07aSXin Li   // SendFdsOverComms() as with MapFd().
60*ec63e07aSXin Li   // If a name is specified, uses the Client::GetMappedFD api to retrieve the
61*ec63e07aSXin Li   // corresponding file descriptor in the sandboxee.
62*ec63e07aSXin Li   int ReceiveFd(int remote_fd, absl::string_view name);
63*ec63e07aSXin Li   int ReceiveFd(int remote_fd);
64*ec63e07aSXin Li   int ReceiveFd(absl::string_view name);
65*ec63e07aSXin Li 
66*ec63e07aSXin Li   // Enable sandboxee logging, this will start a thread that waits for log
67*ec63e07aSXin Li   // messages from the sandboxee. You'll also have to call
68*ec63e07aSXin Li   // Client::SendLogsToSupervisor in the sandboxee.
69*ec63e07aSXin Li   void EnableLogServer();
70*ec63e07aSXin Li 
71*ec63e07aSXin Li  private:
72*ec63e07aSXin Li   friend class Executor;
73*ec63e07aSXin Li   friend class MonitorBase;
74*ec63e07aSXin Li   friend class IpcPeer;  // For testing
75*ec63e07aSXin Li 
76*ec63e07aSXin Li   // Uses a pre-connected file descriptor.
77*ec63e07aSXin Li   void SetUpServerSideComms(int fd);
78*ec63e07aSXin Li 
79*ec63e07aSXin Li   // Sends file descriptors to the sandboxee. Close the local FDs (e.g. passed
80*ec63e07aSXin Li   // in MapFd()) - they cannot be used anymore.
81*ec63e07aSXin Li   bool SendFdsOverComms();
82*ec63e07aSXin Li 
83*ec63e07aSXin Li   void InternalCleanupFdMap();
84*ec63e07aSXin Li 
85*ec63e07aSXin Li   // Tuple of file descriptor pairs which will be sent to the sandboxee: in the
86*ec63e07aSXin Li   // form of tuple<local_fd, remote_fd, name>:
87*ec63e07aSXin Li   //   local_fd: local fd which should be sent to sandboxee
88*ec63e07aSXin Li   //   remote_fd: it will be overwritten by local_fd.
89*ec63e07aSXin Li   std::vector<std::tuple<int, int, std::string>> fd_map_;
90*ec63e07aSXin Li 
91*ec63e07aSXin Li   // Comms channel used to exchange data with the sandboxee.
92*ec63e07aSXin Li   std::unique_ptr<Comms> comms_;
93*ec63e07aSXin Li };
94*ec63e07aSXin Li 
95*ec63e07aSXin Li }  // namespace sandbox2
96*ec63e07aSXin Li 
97*ec63e07aSXin Li #endif  // SANDBOXED_API_SANDBOX2_IPC_H_
98