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