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_SANDBOX2_BUFFER_H_ 16 #define SANDBOXED_API_SANDBOX2_BUFFER_H_ 17 18 #include <cstddef> 19 #include <cstdint> 20 #include <memory> 21 22 #include "absl/status/statusor.h" 23 24 namespace sandbox2 { 25 26 // Buffer provides a way for executor and sandboxee to share data. 27 // It is useful to share large buffers instead of communicating and copying. 28 // The executor must distrust the content of this buffer, like everything 29 // else that comes under control of the sandboxee. 30 class Buffer final { 31 public: 32 ~Buffer(); 33 34 Buffer(const Buffer&) = delete; 35 Buffer& operator=(const Buffer&) = delete; 36 37 // Creates a new Buffer that is backed by the specified file descriptor. 38 // The Buffer takes ownership of the descriptor and will close it when 39 // destroyed. 40 static absl::StatusOr<std::unique_ptr<Buffer>> CreateFromFd(int fd); 41 42 // Creates a new Buffer of the specified size, backed by a temporary file that 43 // will be immediately deleted. 44 static absl::StatusOr<std::unique_ptr<Buffer>> CreateWithSize(size_t size); 45 46 // Returns a pointer to the buffer, which is read/write. data()47 uint8_t* data() const { return buf_; } 48 49 // Gets the size of the buffer in bytes. size()50 size_t size() const { return size_; } 51 52 // Gets the file descriptor backing the buffer. fd()53 int fd() const { return fd_; } 54 55 private: 56 Buffer() = default; 57 58 uint8_t* buf_ = nullptr; 59 int fd_ = -1; 60 size_t size_ = 0; 61 }; 62 63 } // namespace sandbox2 64 65 #endif // SANDBOXED_API_SANDBOX2_BUFFER_H_ 66