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