xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_abstract.cc (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 // Implementation of sapi::v::Var
16*ec63e07aSXin Li 
17*ec63e07aSXin Li #include "sandboxed_api/var_abstract.h"
18*ec63e07aSXin Li 
19*ec63e07aSXin Li #include <sys/types.h>
20*ec63e07aSXin Li #include <sys/uio.h>
21*ec63e07aSXin Li 
22*ec63e07aSXin Li #include <memory>
23*ec63e07aSXin Li #include <string>
24*ec63e07aSXin Li 
25*ec63e07aSXin Li #include "absl/log/log.h"
26*ec63e07aSXin Li #include "absl/status/status.h"
27*ec63e07aSXin Li #include "absl/strings/str_cat.h"
28*ec63e07aSXin Li #include "sandboxed_api/rpcchannel.h"
29*ec63e07aSXin Li #include "sandboxed_api/util/raw_logging.h"
30*ec63e07aSXin Li #include "sandboxed_api/util/status_macros.h"
31*ec63e07aSXin Li #include "sandboxed_api/var_ptr.h"
32*ec63e07aSXin Li 
33*ec63e07aSXin Li namespace sapi::v {
34*ec63e07aSXin Li 
~Var()35*ec63e07aSXin Li Var::~Var() {
36*ec63e07aSXin Li   if (free_rpc_channel_ && GetRemote()) {
37*ec63e07aSXin Li     this->Free(free_rpc_channel_).IgnoreError();
38*ec63e07aSXin Li   }
39*ec63e07aSXin Li }
40*ec63e07aSXin Li 
operator ()(Ptr * p)41*ec63e07aSXin Li void Var::PtrDeleter::operator()(Ptr* p) { delete p; }
42*ec63e07aSXin Li 
PtrNone()43*ec63e07aSXin Li Ptr* Var::PtrNone() {
44*ec63e07aSXin Li   if (!ptr_none_) {
45*ec63e07aSXin Li     ptr_none_.reset(new Ptr(this, kSyncNone));
46*ec63e07aSXin Li   }
47*ec63e07aSXin Li   return ptr_none_.get();
48*ec63e07aSXin Li }
49*ec63e07aSXin Li 
PtrBoth()50*ec63e07aSXin Li Ptr* Var::PtrBoth() {
51*ec63e07aSXin Li   if (!ptr_both_) {
52*ec63e07aSXin Li     ptr_both_.reset(new Ptr(this, kSyncBoth));
53*ec63e07aSXin Li   }
54*ec63e07aSXin Li   return ptr_both_.get();
55*ec63e07aSXin Li }
56*ec63e07aSXin Li 
PtrBefore()57*ec63e07aSXin Li Ptr* Var::PtrBefore() {
58*ec63e07aSXin Li   if (!ptr_before_) {
59*ec63e07aSXin Li     ptr_before_.reset(new Ptr(this, kSyncBefore));
60*ec63e07aSXin Li   }
61*ec63e07aSXin Li   return ptr_before_.get();
62*ec63e07aSXin Li }
63*ec63e07aSXin Li 
PtrAfter()64*ec63e07aSXin Li Ptr* Var::PtrAfter() {
65*ec63e07aSXin Li   if (!ptr_after_) {
66*ec63e07aSXin Li     ptr_after_.reset(new Ptr(this, kSyncAfter));
67*ec63e07aSXin Li   }
68*ec63e07aSXin Li   return ptr_after_.get();
69*ec63e07aSXin Li }
70*ec63e07aSXin Li 
Allocate(RPCChannel * rpc_channel,bool automatic_free)71*ec63e07aSXin Li absl::Status Var::Allocate(RPCChannel* rpc_channel, bool automatic_free) {
72*ec63e07aSXin Li   void* addr;
73*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(rpc_channel->Allocate(GetSize(), &addr));
74*ec63e07aSXin Li 
75*ec63e07aSXin Li   if (!addr) {
76*ec63e07aSXin Li     LOG(ERROR) << "Allocate: returned nullptr";
77*ec63e07aSXin Li     return absl::UnavailableError("Allocating memory failed");
78*ec63e07aSXin Li   }
79*ec63e07aSXin Li 
80*ec63e07aSXin Li   SetRemote(addr);
81*ec63e07aSXin Li   if (automatic_free) {
82*ec63e07aSXin Li     SetFreeRPCChannel(rpc_channel);
83*ec63e07aSXin Li   }
84*ec63e07aSXin Li 
85*ec63e07aSXin Li   return absl::OkStatus();
86*ec63e07aSXin Li }
87*ec63e07aSXin Li 
Free(RPCChannel * rpc_channel)88*ec63e07aSXin Li absl::Status Var::Free(RPCChannel* rpc_channel) {
89*ec63e07aSXin Li   SAPI_RETURN_IF_ERROR(rpc_channel->Free(GetRemote()));
90*ec63e07aSXin Li 
91*ec63e07aSXin Li   SetRemote(nullptr);
92*ec63e07aSXin Li   return absl::OkStatus();
93*ec63e07aSXin Li }
94*ec63e07aSXin Li 
TransferToSandboxee(RPCChannel * rpc_channel,pid_t pid)95*ec63e07aSXin Li absl::Status Var::TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) {
96*ec63e07aSXin Li   VLOG(3) << "TransferToSandboxee for: " << ToString()
97*ec63e07aSXin Li           << ", local: " << GetLocal() << ", remote: " << GetRemote()
98*ec63e07aSXin Li           << ", size: " << GetSize();
99*ec63e07aSXin Li 
100*ec63e07aSXin Li   if (remote_ == nullptr) {
101*ec63e07aSXin Li     LOG(WARNING) << "Object: " << GetType() << " has no remote object set";
102*ec63e07aSXin Li     return absl::FailedPreconditionError(
103*ec63e07aSXin Li         absl::StrCat("Object: ", GetType(), " has no remote object set"));
104*ec63e07aSXin Li   }
105*ec63e07aSXin Li 
106*ec63e07aSXin Li   struct iovec local = {
107*ec63e07aSXin Li       .iov_base = GetLocal(),
108*ec63e07aSXin Li       .iov_len = GetSize(),
109*ec63e07aSXin Li   };
110*ec63e07aSXin Li   struct iovec remote = {
111*ec63e07aSXin Li       .iov_base = GetRemote(),
112*ec63e07aSXin Li       .iov_len = GetSize(),
113*ec63e07aSXin Li   };
114*ec63e07aSXin Li 
115*ec63e07aSXin Li   ssize_t ret = process_vm_writev(pid, &local, 1, &remote, 1, 0);
116*ec63e07aSXin Li   if (ret == -1) {
117*ec63e07aSXin Li     PLOG(WARNING) << "process_vm_writev(pid: " << pid
118*ec63e07aSXin Li                   << " laddr: " << GetLocal() << " raddr: " << GetRemote()
119*ec63e07aSXin Li                   << " size: " << GetSize() << ")";
120*ec63e07aSXin Li     return absl::UnavailableError("process_vm_writev failed");
121*ec63e07aSXin Li   }
122*ec63e07aSXin Li   if (ret != GetSize()) {
123*ec63e07aSXin Li     LOG(WARNING) << "process_vm_writev(pid: " << pid << " laddr: " << GetLocal()
124*ec63e07aSXin Li                  << " raddr: " << GetRemote() << " size: " << GetSize() << ")"
125*ec63e07aSXin Li                  << " transferred " << ret << " bytes";
126*ec63e07aSXin Li     return absl::UnavailableError("process_vm_writev: partial success");
127*ec63e07aSXin Li   }
128*ec63e07aSXin Li 
129*ec63e07aSXin Li   return absl::OkStatus();
130*ec63e07aSXin Li }
131*ec63e07aSXin Li 
TransferFromSandboxee(RPCChannel * rpc_channel,pid_t pid)132*ec63e07aSXin Li absl::Status Var::TransferFromSandboxee(RPCChannel* rpc_channel, pid_t pid) {
133*ec63e07aSXin Li   VLOG(3) << "TransferFromSandboxee for: " << ToString()
134*ec63e07aSXin Li           << ", local: " << GetLocal() << ", remote: " << GetRemote()
135*ec63e07aSXin Li           << ", size: " << GetSize();
136*ec63e07aSXin Li 
137*ec63e07aSXin Li   if (local_ == nullptr) {
138*ec63e07aSXin Li     return absl::FailedPreconditionError(
139*ec63e07aSXin Li         absl::StrCat("Object: ", GetType(), " has no local storage set"));
140*ec63e07aSXin Li   }
141*ec63e07aSXin Li 
142*ec63e07aSXin Li   struct iovec local = {
143*ec63e07aSXin Li       .iov_base = GetLocal(),
144*ec63e07aSXin Li       .iov_len = GetSize(),
145*ec63e07aSXin Li   };
146*ec63e07aSXin Li   struct iovec remote = {
147*ec63e07aSXin Li       .iov_base = GetRemote(),
148*ec63e07aSXin Li       .iov_len = GetSize(),
149*ec63e07aSXin Li   };
150*ec63e07aSXin Li 
151*ec63e07aSXin Li   ssize_t ret = process_vm_readv(pid, &local, 1, &remote, 1, 0);
152*ec63e07aSXin Li   if (ret == -1) {
153*ec63e07aSXin Li     PLOG(WARNING) << "process_vm_readv(pid: " << pid << " laddr: " << GetLocal()
154*ec63e07aSXin Li                   << " raddr: " << GetRemote() << " size: " << GetSize() << ")";
155*ec63e07aSXin Li     return absl::UnavailableError("process_vm_readv failed");
156*ec63e07aSXin Li   }
157*ec63e07aSXin Li   if (ret != GetSize()) {
158*ec63e07aSXin Li     LOG(WARNING) << "process_vm_readv(pid: " << pid << " laddr: " << GetLocal()
159*ec63e07aSXin Li                  << " raddr: " << GetRemote() << " size: " << GetSize() << ")"
160*ec63e07aSXin Li                  << " transferred " << ret << " bytes";
161*ec63e07aSXin Li     return absl::UnavailableError("process_vm_readv succeeded partially");
162*ec63e07aSXin Li   }
163*ec63e07aSXin Li 
164*ec63e07aSXin Li   return absl::OkStatus();
165*ec63e07aSXin Li }
166*ec63e07aSXin Li 
167*ec63e07aSXin Li }  // namespace sapi::v
168