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