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_LENVAL_H_ 16 #define SANDBOXED_API_VAR_LENVAL_H_ 17 18 #include <sys/types.h> 19 #include <sys/uio.h> 20 21 #include <cstdint> 22 #include <cstring> 23 #include <memory> 24 #include <string> 25 #include <vector> 26 27 #include "absl/base/macros.h" 28 #include "absl/status/status.h" 29 #include "sandboxed_api/lenval_core.h" 30 #include "sandboxed_api/var_abstract.h" 31 #include "sandboxed_api/var_array.h" 32 #include "sandboxed_api/var_ptr.h" 33 #include "sandboxed_api/var_struct.h" 34 35 namespace sapi::v { 36 37 template <class T> 38 class Proto; 39 40 // Length + value container. Represents a pointer to a LenValStruct inside the 41 // sandboxee which allows the bidirectional synchronization data structures with 42 // changing lengths (e.g. protobuf structures). You probably want to directly 43 // use protobufs as they are easier to handle. 44 class LenVal : public Var { 45 public: LenVal(const char * data,uint64_t size)46 explicit LenVal(const char* data, uint64_t size) 47 : array_(const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(data)), 48 size), 49 struct_(size, nullptr) {} 50 LenVal(const std::vector<uint8_t> & data)51 explicit LenVal(const std::vector<uint8_t>& data) 52 : array_(data.size()), struct_(data.size(), nullptr) { 53 memcpy(array_.GetData(), data.data(), data.size()); 54 } 55 LenVal(size_t size)56 explicit LenVal(size_t size) : array_(size), struct_(size, nullptr) {} 57 58 LenVal(LenVal&& other) = default; 59 LenVal& operator=(LenVal&& other) = default; 60 GetType()61 Type GetType() const final { return Type::kLenVal; } GetTypeString()62 std::string GetTypeString() const final { return "LengthValue"; } ToString()63 std::string ToString() const final { return "LenVal"; } 64 65 absl::Status ResizeData(RPCChannel* rpc_channel, size_t size); GetDataSize()66 size_t GetDataSize() const { return array_.GetSize(); } GetData()67 uint8_t* GetData() const { return array_.GetData(); } GetRemote()68 void* GetRemote() const final { return struct_.GetRemote(); } 69 70 protected: 71 template <class T> 72 friend class Proto; 73 GetSize()74 size_t GetSize() const final { return 0; } 75 76 absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free) override; 77 absl::Status Free(RPCChannel* rpc_channel) override; 78 absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) override; 79 absl::Status TransferFromSandboxee(RPCChannel* rpc_channel, 80 pid_t pid) override; 81 82 Array<uint8_t> array_; 83 Struct<LenValStruct> struct_; 84 }; 85 86 } // namespace sapi::v 87 88 #endif // SANDBOXED_API_VAR_LENVAL_H_ 89