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