xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/var_proto.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
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 // Provides a class to marshall protobufs in and out of the sandbox
16 
17 #ifndef SANDBOXED_API_VAR_PROTO_H_
18 #define SANDBOXED_API_VAR_PROTO_H_
19 
20 #include <cinttypes>
21 #include <cstdint>
22 #include <ctime>
23 #include <memory>
24 #include <string>
25 #include <type_traits>
26 #include <utility>
27 #include <vector>
28 
29 #include "absl/base/attributes.h"
30 #include "absl/base/macros.h"
31 #include "absl/log/log.h"
32 #include "absl/status/status.h"
33 #include "absl/status/statusor.h"
34 #include "absl/utility/utility.h"
35 #include "sandboxed_api/proto_helper.h"
36 #include "sandboxed_api/util/status_macros.h"
37 #include "sandboxed_api/var_lenval.h"
38 #include "sandboxed_api/var_ptr.h"
39 
40 namespace sapi::v {
41 
42 template <typename T>
43 class Proto : public Var {
44  public:
45   static_assert(std::is_base_of<google::protobuf::MessageLite, T>::value,
46                 "Template argument must be a proto message");
47 
48   ABSL_DEPRECATED("Use Proto<>::FromMessage() instead")
Proto(const T & proto)49   explicit Proto(const T& proto)
50       : wrapped_var_(SerializeProto(proto).value()) {}
51 
FromMessage(const T & proto)52   static absl::StatusOr<Proto<T>> FromMessage(const T& proto) {
53     SAPI_ASSIGN_OR_RETURN(std::vector<uint8_t> len_val, SerializeProto(proto));
54     return absl::StatusOr<Proto<T>>(absl::in_place, proto);
55   }
56 
GetSize()57   size_t GetSize() const final { return wrapped_var_.GetSize(); }
GetType()58   Type GetType() const final { return Type::kProto; }
GetTypeString()59   std::string GetTypeString() const final { return "Protobuf"; }
ToString()60   std::string ToString() const final { return "Protobuf"; }
61 
GetRemote()62   void* GetRemote() const override { return wrapped_var_.GetRemote(); }
GetLocal()63   void* GetLocal() const override { return wrapped_var_.GetLocal(); }
64 
65   // Returns a copy of the stored protobuf object.
GetMessage()66   absl::StatusOr<T> GetMessage() const {
67     return DeserializeProto<T>(
68         reinterpret_cast<const char*>(wrapped_var_.GetData()),
69         wrapped_var_.GetDataSize());
70   }
71 
72   ABSL_DEPRECATED("Use GetMessage() instead")
GetProtoCopy()73   std::unique_ptr<T> GetProtoCopy() const {
74     if (auto proto = GetMessage(); proto.ok()) {
75       return std::make_unique<T>(*std::move(proto));
76     }
77     return nullptr;
78   }
79 
SetRemote(void *)80   void SetRemote(void* /* remote */) override {
81     // We do not support that much indirection (pointer to a pointer to a
82     // protobuf) as it is unlikely that this is wanted behavior. If you expect
83     // this to work, please get in touch with us.
84     LOG(FATAL) << "SetRemote not supported on protobufs.";
85   }
86 
87  protected:
88   // Forward a couple of function calls to the actual var.
Allocate(RPCChannel * rpc_channel,bool automatic_free)89   absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free) override {
90     return wrapped_var_.Allocate(rpc_channel, automatic_free);
91   }
92 
Free(RPCChannel * rpc_channel)93   absl::Status Free(RPCChannel* rpc_channel) override {
94     return absl::OkStatus();
95   }
96 
TransferToSandboxee(RPCChannel * rpc_channel,pid_t pid)97   absl::Status TransferToSandboxee(RPCChannel* rpc_channel,
98                                    pid_t pid) override {
99     return wrapped_var_.TransferToSandboxee(rpc_channel, pid);
100   }
101 
TransferFromSandboxee(RPCChannel * rpc_channel,pid_t pid)102   absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
103                                      pid_t pid) override {
104     return wrapped_var_.TransferFromSandboxee(rpc_channel, pid);
105   }
106 
107  private:
108   friend class absl::StatusOr<Proto<T>>;
109 
Proto(std::vector<uint8_t> data)110   explicit Proto(std::vector<uint8_t> data) : wrapped_var_(std::move(data)) {}
111 
112   // The management of reading/writing the data to the sandboxee is handled by
113   // the LenVal class.
114   LenVal wrapped_var_;
115 };
116 
117 }  // namespace sapi::v
118 
119 #endif  // SANDBOXED_API_VAR_PROTO_H_
120