1 // Copyright 2019 Google LLC 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SANDBOXED_API_VAR_ABSTRACT_H_ 16 #define SANDBOXED_API_VAR_ABSTRACT_H_ 17 18 #include <ctime> 19 #include <memory> 20 #include <string> 21 #include <type_traits> 22 #include <utility> 23 24 #include "absl/base/attributes.h" 25 #include "absl/base/macros.h" 26 #include "absl/status/status.h" 27 #include "sandboxed_api/var_type.h" 28 29 namespace sandbox2 { 30 class Comms; 31 } // namespace sandbox2 32 33 namespace sapi { 34 class RPCChannel; 35 class Sandbox; 36 } // namespace sapi 37 38 namespace sapi::v { 39 40 class Ptr; 41 42 class ABSL_DEPRECATED( 43 "Use the Var::PtrXXX() family of methods instead") Pointable { 44 public: 45 enum SyncType { 46 // Do not synchronize the underlying object after/before calls. 47 kSyncNone = 0x0, 48 // Synchronize the underlying object (send the data to the sandboxee) 49 // before the call takes place. 50 kSyncBefore = 0x1, 51 // Synchronize the underlying object (retrieve data from the sandboxee) 52 // after the call has finished. 53 kSyncAfter = 0x2, 54 // Synchronize the underlying object with the remote object, by sending the 55 // data to the sandboxee before the call, and retrieving it from the 56 // sandboxee after the call has finished. 57 kSyncBoth = kSyncBefore | kSyncAfter, 58 }; 59 60 virtual ~Pointable() = default; 61 }; 62 63 // An abstract class representing variables. 64 class Var : public Pointable { 65 public: 66 Var(const Var&) = delete; 67 Var& operator=(const Var&) = delete; 68 69 virtual ~Var(); 70 71 // Returns the address of the storage (remote side). GetRemote()72 virtual void* GetRemote() const { return remote_; } 73 74 // Sets the address of the remote storage. SetRemote(void * remote)75 virtual void SetRemote(void* remote) { remote_ = remote; } 76 77 // Returns the address of the storage (local side). GetLocal()78 virtual void* GetLocal() const { return local_; } 79 80 // Returns the size of the local variable storage. 81 virtual size_t GetSize() const = 0; 82 83 // Returns the type of the variable. 84 virtual Type GetType() const = 0; 85 86 // Returns a string representation of the variable type. 87 virtual std::string GetTypeString() const = 0; 88 89 // Returns a string representation of the variable value. 90 virtual std::string ToString() const = 0; 91 92 // Functions to get pointers with certain type of synchronization schemes. 93 Ptr* PtrNone(); 94 Ptr* PtrBoth(); 95 Ptr* PtrBefore(); 96 Ptr* PtrAfter(); 97 98 protected: 99 Var() = default; 100 Var(Var && other)101 Var(Var&& other) { *this = std::move(other); } 102 Var& operator=(Var&& other); 103 104 // Set pointer to local storage class. SetLocal(void * local)105 void SetLocal(void* local) { local_ = local; } 106 107 // Setter/Getter for the address of a Comms object which can be used to 108 // remotely free allocated memory backing up this variable, upon this 109 // object's end of life-time SetFreeRPCChannel(RPCChannel * rpc_channel)110 void SetFreeRPCChannel(RPCChannel* rpc_channel) { 111 free_rpc_channel_ = rpc_channel; 112 } GetFreeRPCChannel()113 RPCChannel* GetFreeRPCChannel() { return free_rpc_channel_; } 114 115 // Allocates the local variable on the remote side. The 'automatic_free' 116 // argument dictates whether the remote memory should be freed upon end of 117 // this object's lifetime. 118 virtual absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free); 119 120 // Frees the local variable on the remote side. 121 virtual absl::Status Free(RPCChannel* rpc_channel); 122 123 // Transfers the variable to the sandboxee's address space, has to be 124 // allocated there first. 125 virtual absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid); 126 127 // Transfers the variable from the sandboxee's address space. 128 virtual absl::Status TransferFromSandboxee(RPCChannel* rpc_channel, 129 pid_t pid); 130 131 private: 132 // Needed so that we can use unique_ptr with incomplete type. 133 struct PtrDeleter { 134 void operator()(Ptr* p); 135 }; 136 137 // Invokes Allocate()/Free()/Transfer*Sandboxee(). 138 friend class ::sapi::Sandbox; 139 140 std::unique_ptr<Ptr, PtrDeleter> ptr_none_; 141 std::unique_ptr<Ptr, PtrDeleter> ptr_both_; 142 std::unique_ptr<Ptr, PtrDeleter> ptr_before_; 143 std::unique_ptr<Ptr, PtrDeleter> ptr_after_; 144 145 // Pointer to local storage of the variable. 146 void* local_ = nullptr; 147 // Pointer to remote storage of the variable. 148 void* remote_ = nullptr; 149 150 // Comms which can be used to free resources allocated in the sandboxer upon 151 // this process' end of lifetime. 152 RPCChannel* free_rpc_channel_ = nullptr; 153 }; 154 155 } // namespace sapi::v 156 157 #endif // SANDBOXED_API_VAR_ABSTRACT_H_ 158