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_INT_H_ 16 #define SANDBOXED_API_VAR_INT_H_ 17 18 #include <sys/types.h> 19 20 #include <cstdint> 21 #include <memory> 22 23 #include "absl/status/status.h" 24 #include "sandboxed_api/sandbox2/comms.h" 25 #include "sandboxed_api/var_ptr.h" 26 #include "sandboxed_api/var_reg.h" 27 28 namespace sapi { 29 class RPCChannel; 30 } // namespace sapi 31 32 namespace sapi::v { 33 34 // Intermediate class for register sized variables so we don't have to implement 35 // ptr() everywhere. 36 template <class T> 37 class IntBase : public Reg<T> { 38 public: 39 explicit IntBase(T value = {}) { this->SetValue(value); } 40 }; 41 42 using Bool = IntBase<bool>; 43 using Char = IntBase<char>; 44 using UChar = IntBase<unsigned char>; 45 using SChar = IntBase<signed char>; 46 47 using Short = IntBase<short>; // NOLINT 48 using UShort = IntBase<unsigned short>; // NOLINT 49 using SShort = IntBase<signed short>; // NOLINT 50 51 using Int = IntBase<int>; 52 using UInt = IntBase<unsigned int>; 53 using SInt = IntBase<signed int>; 54 55 using Long = IntBase<long>; // NOLINT 56 using ULong = IntBase<unsigned long>; // NOLINT 57 using SLong = IntBase<signed long>; // NOLINT 58 using LLong = IntBase<long long>; // NOLINT 59 using ULLong = IntBase<unsigned long long>; // NOLINT 60 using SLLong = IntBase<signed long long>; // NOLINT 61 62 class GenericPtr : public IntBase<uintptr_t> { 63 public: GenericPtr()64 GenericPtr() { SetValue(0); } GenericPtr(uintptr_t val)65 explicit GenericPtr(uintptr_t val) { SetValue(val); } GenericPtr(void * val)66 explicit GenericPtr(void* val) { SetValue(reinterpret_cast<uintptr_t>(val)); } 67 }; 68 69 class Fd : public Int { 70 public: GetType()71 Type GetType() const override { return Type::kFd; } Fd(int val)72 explicit Fd(int val) { SetValue(val); } 73 ~Fd() override; 74 75 // Getter and setter of remote file descriptor. SetRemoteFd(int remote_fd)76 void SetRemoteFd(int remote_fd) { remote_fd_ = remote_fd; } GetRemoteFd()77 int GetRemoteFd() { return remote_fd_; } 78 79 // Sets remote and local fd ownership, true by default. 80 // Owned fd will be closed during object destruction. OwnRemoteFd(bool owned)81 void OwnRemoteFd(bool owned) { own_remote_ = owned; } OwnLocalFd(bool owned)82 void OwnLocalFd(bool owned) { own_local_ = owned; } 83 84 // Close remote fd in the sadboxee. 85 absl::Status CloseRemoteFd(RPCChannel* rpc_channel); 86 // Close local fd. 87 void CloseLocalFd(); 88 89 protected: 90 // Sends local fd to sandboxee, takes ownership of the fd. 91 absl::Status TransferFromSandboxee(RPCChannel* rpc_channel, 92 pid_t pid) override; 93 94 // Retrieves remote file descriptor, does not own fd. 95 absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) override; 96 97 private: 98 int remote_fd_ = -1; 99 bool own_local_ = true; 100 bool own_remote_ = true; 101 }; 102 103 } // namespace sapi::v 104 105 #endif // SANDBOXED_API_VAR_INT_H_ 106