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_INT_H_
|
|
|
|
#define SANDBOXED_API_VAR_INT_H_
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <memory>
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
#include "sandboxed_api/var_ptr.h"
|
|
|
|
#include "sandboxed_api/var_reg.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
namespace sapi {
|
|
|
|
class RPCChannel;
|
|
|
|
} // namespace sapi
|
|
|
|
|
2019-11-20 20:39:44 +08:00
|
|
|
namespace sapi::v {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-12-02 01:54:09 +08:00
|
|
|
// Intermediate class for register sized variables so we don't have to implement
|
|
|
|
// ptr() everywhere.
|
2019-03-19 00:21:48 +08:00
|
|
|
template <class T>
|
2021-12-02 01:54:09 +08:00
|
|
|
class IntBase : public Reg<T> {
|
2019-03-19 00:21:48 +08:00
|
|
|
public:
|
2021-12-02 01:54:09 +08:00
|
|
|
explicit IntBase(T value = {}) { this->SetValue(value); }
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
using Bool = IntBase<bool>;
|
|
|
|
using Char = IntBase<char>;
|
|
|
|
using UChar = IntBase<unsigned char>;
|
|
|
|
using SChar = IntBase<signed char>;
|
|
|
|
|
|
|
|
using Short = IntBase<short>; // NOLINT
|
|
|
|
using UShort = IntBase<unsigned short>; // NOLINT
|
|
|
|
using SShort = IntBase<signed short>; // NOLINT
|
|
|
|
|
|
|
|
using Int = IntBase<int>;
|
|
|
|
using UInt = IntBase<unsigned int>;
|
|
|
|
using SInt = IntBase<signed int>;
|
|
|
|
|
|
|
|
using Long = IntBase<long>; // NOLINT
|
|
|
|
using ULong = IntBase<unsigned long>; // NOLINT
|
|
|
|
using SLong = IntBase<signed long>; // NOLINT
|
|
|
|
using LLong = IntBase<long long>; // NOLINT
|
|
|
|
using ULLong = IntBase<unsigned long long>; // NOLINT
|
|
|
|
using SLLong = IntBase<signed long long>; // NOLINT
|
|
|
|
|
|
|
|
class GenericPtr : public IntBase<uintptr_t> {
|
|
|
|
public:
|
|
|
|
GenericPtr() { SetValue(0); }
|
|
|
|
explicit GenericPtr(uintptr_t val) { SetValue(val); }
|
|
|
|
explicit GenericPtr(void* val) { SetValue(reinterpret_cast<uintptr_t>(val)); }
|
|
|
|
};
|
|
|
|
|
|
|
|
class Fd : public Int {
|
|
|
|
public:
|
|
|
|
Type GetType() const override { return Type::kFd; }
|
2020-07-20 18:07:15 +08:00
|
|
|
explicit Fd(int val) { SetValue(val); }
|
2019-03-19 00:21:48 +08:00
|
|
|
~Fd() override;
|
|
|
|
|
|
|
|
// Getter and setter of remote file descriptor.
|
|
|
|
void SetRemoteFd(int remote_fd) { remote_fd_ = remote_fd; }
|
|
|
|
int GetRemoteFd() { return remote_fd_; }
|
|
|
|
|
|
|
|
// Sets remote and local fd ownership, true by default.
|
|
|
|
// Owned fd will be closed during object destruction.
|
|
|
|
void OwnRemoteFd(bool owned) { own_remote_ = owned; }
|
|
|
|
void OwnLocalFd(bool owned) { own_local_ = owned; }
|
|
|
|
|
|
|
|
// Close remote fd in the sadboxee.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status CloseRemoteFd(RPCChannel* rpc_channel);
|
2019-03-19 00:21:48 +08:00
|
|
|
// Close local fd.
|
|
|
|
void CloseLocalFd();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// Sends local fd to sandboxee, takes ownership of the fd.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status TransferFromSandboxee(RPCChannel* rpc_channel,
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid) override;
|
|
|
|
|
|
|
|
// Retrieves remote file descriptor, does not own fd.
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status TransferToSandboxee(RPCChannel* rpc_channel, pid_t pid) override;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
private:
|
2020-07-20 18:07:15 +08:00
|
|
|
int remote_fd_ = -1;
|
|
|
|
bool own_local_ = true;
|
|
|
|
bool own_remote_ = true;
|
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_INT_H_
|