2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_VAR_ABSTRACT_H_
|
|
|
|
#define SANDBOXED_API_VAR_ABSTRACT_H_
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <ctime>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <memory>
|
2019-03-26 22:54:02 +08:00
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <type_traits>
|
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
#include "absl/base/attributes.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/macros.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/var_type.h"
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
class Comms;
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sandbox2
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sapi {
|
|
|
|
class RPCChannel;
|
2023-08-24 21:23:03 +08:00
|
|
|
class Sandbox;
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
class Ptr;
|
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
class ABSL_DEPRECATED(
|
|
|
|
"Use the Var::PtrXXX() family of methods instead") Pointable {
|
|
|
|
public:
|
|
|
|
enum SyncType {
|
|
|
|
// Do not synchronize the underlying object after/before calls.
|
|
|
|
kSyncNone = 0x0,
|
|
|
|
// Synchronize the underlying object (send the data to the sandboxee)
|
|
|
|
// before the call takes place.
|
|
|
|
kSyncBefore = 0x1,
|
|
|
|
// Synchronize the underlying object (retrieve data from the sandboxee)
|
|
|
|
// after the call has finished.
|
|
|
|
kSyncAfter = 0x2,
|
|
|
|
// Synchronize the underlying object with the remote object, by sending the
|
|
|
|
// data to the sandboxee before the call, and retrieving it from the
|
|
|
|
// sandboxee after the call has finished.
|
|
|
|
kSyncBoth = kSyncBefore | kSyncAfter,
|
|
|
|
};
|
|
|
|
|
|
|
|
virtual ~Pointable() = default;
|
|
|
|
};
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// An abstract class representing variables.
|
2021-12-02 01:54:09 +08:00
|
|
|
class Var : public Pointable {
|
2019-03-19 00:21:48 +08:00
|
|
|
public:
|
|
|
|
Var(const Var&) = delete;
|
|
|
|
Var& operator=(const Var&) = delete;
|
|
|
|
|
2021-12-02 17:41:26 +08:00
|
|
|
virtual ~Var();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Returns the address of the storage (remote side).
|
|
|
|
virtual void* GetRemote() const { return remote_; }
|
|
|
|
|
|
|
|
// Sets the address of the remote storage.
|
|
|
|
virtual void SetRemote(void* remote) { remote_ = remote; }
|
|
|
|
|
|
|
|
// Returns the address of the storage (local side).
|
|
|
|
virtual void* GetLocal() const { return local_; }
|
|
|
|
|
|
|
|
// Returns the size of the local variable storage.
|
|
|
|
virtual size_t GetSize() const = 0;
|
|
|
|
|
|
|
|
// Returns the type of the variable.
|
|
|
|
virtual Type GetType() const = 0;
|
|
|
|
|
2019-03-26 22:54:02 +08:00
|
|
|
// Returns a string representation of the variable type.
|
2019-03-19 00:21:48 +08:00
|
|
|
virtual std::string GetTypeString() const = 0;
|
|
|
|
|
2019-03-26 22:54:02 +08:00
|
|
|
// Returns a string representation of the variable value.
|
2019-03-19 00:21:48 +08:00
|
|
|
virtual std::string ToString() const = 0;
|
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
// Functions to get pointers with certain type of synchronization schemes.
|
|
|
|
Ptr* PtrNone();
|
|
|
|
Ptr* PtrBoth();
|
|
|
|
Ptr* PtrBefore();
|
|
|
|
Ptr* PtrAfter();
|
|
|
|
|
2021-12-02 17:41:26 +08:00
|
|
|
protected:
|
|
|
|
Var() = default;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Set pointer to local storage class.
|
|
|
|
void SetLocal(void* local) { local_ = local; }
|
|
|
|
|
|
|
|
// Setter/Getter for the address of a Comms object which can be used to
|
2019-11-20 20:39:44 +08:00
|
|
|
// remotely free allocated memory backing up this variable, upon this
|
|
|
|
// object's end of life-time
|
2019-03-19 00:21:48 +08:00
|
|
|
void SetFreeRPCChannel(RPCChannel* rpc_channel) {
|
|
|
|
free_rpc_channel_ = rpc_channel;
|
|
|
|
}
|
|
|
|
RPCChannel* GetFreeRPCChannel() { return free_rpc_channel_; }
|
|
|
|
|
|
|
|
// Allocates the local variable on the remote side. The 'automatic_free'
|
|
|
|
// argument dictates whether the remote memory should be freed upon end of
|
|
|
|
// this object's lifetime.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status Allocate(RPCChannel* rpc_channel, bool automatic_free);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Frees the local variable on the remote side.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status Free(RPCChannel* rpc_channel);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Transfers the variable to the sandboxee's address space, has to be
|
|
|
|
// allocated there first.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Transfers the variable from the sandboxee's address space.
|
2020-02-28 01:23:44 +08:00
|
|
|
virtual absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid);
|
|
|
|
|
|
|
|
private:
|
2021-12-02 01:54:09 +08:00
|
|
|
// Needed so that we can use unique_ptr with incomplete type.
|
|
|
|
struct PtrDeleter {
|
|
|
|
void operator()(Ptr* p);
|
|
|
|
};
|
|
|
|
|
2020-07-20 18:07:15 +08:00
|
|
|
// Invokes Allocate()/Free()/Transfer*Sandboxee().
|
|
|
|
friend class ::sapi::Sandbox;
|
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
std::unique_ptr<Ptr, PtrDeleter> ptr_none_;
|
|
|
|
std::unique_ptr<Ptr, PtrDeleter> ptr_both_;
|
|
|
|
std::unique_ptr<Ptr, PtrDeleter> ptr_before_;
|
|
|
|
std::unique_ptr<Ptr, PtrDeleter> ptr_after_;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Pointer to local storage of the variable.
|
2020-07-20 18:07:15 +08:00
|
|
|
void* local_ = nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
// Pointer to remote storage of the variable.
|
2020-07-20 18:07:15 +08:00
|
|
|
void* remote_ = nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Comms which can be used to free resources allocated in the sandboxer upon
|
|
|
|
// this process' end of lifetime.
|
2020-07-20 18:07:15 +08:00
|
|
|
RPCChannel* free_rpc_channel_ = nullptr;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
} // namespace sapi::v
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#endif // SANDBOXED_API_VAR_ABSTRACT_H_
|