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.
|
|
|
|
|
2023-03-23 22:33:49 +08:00
|
|
|
// The sandbox2::Comms class uses AF_UNIX sockets (man 7 unix) to send pieces of
|
|
|
|
// data between processes. It uses the TLV encoding and provides some useful
|
|
|
|
// helpers.
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// The endianess is platform-specific, but as it can be used over abstract
|
|
|
|
// sockets only, that's not a problem. Is some poor soul decides to rewrite it
|
|
|
|
// to work over AF_INET(6), the endianess will have to be dealt with (somehow).
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_COMMS_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_COMMS_H_
|
|
|
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-08-24 00:03:05 +08:00
|
|
|
#include <cstddef>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstdint>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <functional>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2023-08-18 19:40:43 +08:00
|
|
|
#include <utility>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
#include "absl/base/attributes.h"
|
2023-08-24 00:03:05 +08:00
|
|
|
#include "absl/log/die_if_null.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2023-09-04 22:00:19 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2023-08-25 21:49:42 +08:00
|
|
|
#include "google/protobuf/message_lite.h"
|
2023-09-19 20:13:40 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace proto2 {
|
|
|
|
class Message;
|
2021-01-22 22:01:05 +08:00
|
|
|
} // namespace proto2
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
class Client;
|
2023-09-19 20:13:40 +08:00
|
|
|
class ListeningComms;
|
2022-10-11 00:38:14 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
class Comms {
|
|
|
|
public:
|
2022-10-06 19:55:22 +08:00
|
|
|
struct DefaultConnectionTag {};
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Default tags, custom tags should be <0x80000000.
|
|
|
|
static constexpr uint32_t kTagBool = 0x80000001;
|
|
|
|
static constexpr uint32_t kTagInt8 = 0x80000002;
|
|
|
|
static constexpr uint32_t kTagUint8 = 0x80000003;
|
|
|
|
static constexpr uint32_t kTagInt16 = 0x80000004;
|
|
|
|
static constexpr uint32_t kTagUint16 = 0x80000005;
|
|
|
|
static constexpr uint32_t kTagInt32 = 0x80000006;
|
|
|
|
static constexpr uint32_t kTagUint32 = 0x80000007;
|
|
|
|
static constexpr uint32_t kTagInt64 = 0x80000008;
|
|
|
|
static constexpr uint32_t kTagUint64 = 0x80000009;
|
|
|
|
static constexpr uint32_t kTagString = 0x80000100;
|
|
|
|
static constexpr uint32_t kTagBytes = 0x80000101;
|
|
|
|
static constexpr uint32_t kTagProto2 = 0x80000102;
|
|
|
|
static constexpr uint32_t kTagFd = 0X80000201;
|
|
|
|
|
|
|
|
// Any payload size above this limit will LOG(WARNING).
|
2020-09-18 22:44:34 +08:00
|
|
|
static constexpr size_t kWarnMsgSize = (256ULL << 20);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-05-10 22:03:15 +08:00
|
|
|
// A high file descriptor number to be used with certain fork server request
|
|
|
|
// modes to map the target executable. This is considered to be an
|
|
|
|
// implementation detail.
|
|
|
|
// This number is chosen so that low FD numbers are not interfered with.
|
|
|
|
static constexpr int kSandbox2TargetExecFD = 1022;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Sandbox2-specific convention where FD=1023 is always passed to the
|
|
|
|
// sandboxed process as a communication channel (encapsulated in the
|
|
|
|
// sandbox2::Comms object at the server-side).
|
|
|
|
static constexpr int kSandbox2ClientCommsFD = 1023;
|
|
|
|
|
2023-09-01 04:49:41 +08:00
|
|
|
// Within SendTLV, a stack-allocated buffer is created to contiguously store
|
|
|
|
// the TLV in order to perform one call to Send.
|
|
|
|
// If the TLV is larger than the size below, two calls to Send are
|
|
|
|
// used.
|
|
|
|
static constexpr size_t kSendTLVTempBufferSize = 1024;
|
|
|
|
|
2022-10-06 19:55:22 +08:00
|
|
|
static constexpr DefaultConnectionTag kDefaultConnection = {};
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
static constexpr const char* kSandbox2CommsFDEnvVar = "SANDBOX2_COMMS_FD";
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
static absl::StatusOr<Comms> Connect(const std::string& socket_name,
|
|
|
|
bool abstract_uds = true);
|
|
|
|
|
2023-08-18 19:40:43 +08:00
|
|
|
Comms(Comms&& other) { *this = std::move(other); }
|
|
|
|
Comms& operator=(Comms&& other) {
|
|
|
|
if (this != &other) {
|
|
|
|
using std::swap;
|
|
|
|
swap(*this, other);
|
|
|
|
other.Terminate();
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
Comms(const Comms&) = delete;
|
|
|
|
Comms& operator=(const Comms&) = delete;
|
|
|
|
|
|
|
|
// Instantiates a pre-connected object.
|
|
|
|
// Takes ownership over fd, which will be closed on object's destruction.
|
2023-09-19 20:13:40 +08:00
|
|
|
explicit Comms(int fd, absl::string_view name = "");
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-10-06 19:55:22 +08:00
|
|
|
// Instantiates a pre-connected object using the default connection params.
|
2022-10-11 00:38:14 +08:00
|
|
|
explicit Comms(DefaultConnectionTag);
|
2022-10-06 19:55:22 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
~Comms();
|
|
|
|
|
|
|
|
// Terminates all underlying file descriptors, and sets the status of the
|
|
|
|
// Comms object to TERMINATED.
|
|
|
|
void Terminate();
|
|
|
|
|
|
|
|
// Returns the already connected FD.
|
|
|
|
int GetConnectionFD() const;
|
|
|
|
|
|
|
|
bool IsConnected() const { return state_ == State::kConnected; }
|
|
|
|
bool IsTerminated() const { return state_ == State::kTerminated; }
|
|
|
|
|
|
|
|
// Returns the maximum size of a message that can be send over the comms
|
|
|
|
// channel.
|
|
|
|
// Note: The actual size is "unlimited", although the Buffer API is more
|
|
|
|
// efficient for large transfers. There is an arbitrary limit to ~2GiB to
|
|
|
|
// avoid protobuf serialization issues.
|
2020-09-18 22:44:34 +08:00
|
|
|
size_t GetMaxMsgSize() const { return std::numeric_limits<int32_t>::max(); }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool SendTLV(uint32_t tag, size_t length, const void* value);
|
2019-03-19 00:21:48 +08:00
|
|
|
// Receive a TLV structure, the memory for the value will be allocated
|
|
|
|
// by std::vector.
|
|
|
|
bool RecvTLV(uint32_t* tag, std::vector<uint8_t>* value);
|
2020-09-18 17:22:54 +08:00
|
|
|
// Receive a TLV structure, the memory for the value will be allocated
|
|
|
|
// by std::string.
|
|
|
|
bool RecvTLV(uint32_t* tag, std::string* value);
|
2019-03-19 00:21:48 +08:00
|
|
|
// Receives a TLV value into a specified buffer without allocating memory.
|
2020-09-18 22:44:34 +08:00
|
|
|
bool RecvTLV(uint32_t* tag, size_t* length, void* buffer, size_t buffer_size);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Sends/receives various types of data.
|
|
|
|
bool RecvUint8(uint8_t* v) { return RecvIntGeneric(v, kTagUint8); }
|
|
|
|
bool SendUint8(uint8_t v) { return SendGeneric(v, kTagUint8); }
|
|
|
|
bool RecvInt8(int8_t* v) { return RecvIntGeneric(v, kTagInt8); }
|
|
|
|
bool SendInt8(int8_t v) { return SendGeneric(v, kTagInt8); }
|
|
|
|
bool RecvUint16(uint16_t* v) { return RecvIntGeneric(v, kTagUint16); }
|
|
|
|
bool SendUint16(uint16_t v) { return SendGeneric(v, kTagUint16); }
|
|
|
|
bool RecvInt16(int16_t* v) { return RecvIntGeneric(v, kTagInt16); }
|
|
|
|
bool SendInt16(int16_t v) { return SendGeneric(v, kTagInt16); }
|
|
|
|
bool RecvUint32(uint32_t* v) { return RecvIntGeneric(v, kTagUint32); }
|
|
|
|
bool SendUint32(uint32_t v) { return SendGeneric(v, kTagUint32); }
|
|
|
|
bool RecvInt32(int32_t* v) { return RecvIntGeneric(v, kTagInt32); }
|
|
|
|
bool SendInt32(int32_t v) { return SendGeneric(v, kTagInt32); }
|
2020-10-01 21:44:57 +08:00
|
|
|
bool RecvUint64(uint64_t* v) { return RecvIntGeneric(v, kTagUint64); }
|
|
|
|
bool SendUint64(uint64_t v) { return SendGeneric(v, kTagUint64); }
|
|
|
|
bool RecvInt64(int64_t* v) { return RecvIntGeneric(v, kTagInt64); }
|
|
|
|
bool SendInt64(int64_t v) { return SendGeneric(v, kTagInt64); }
|
2019-03-19 00:21:48 +08:00
|
|
|
bool RecvBool(bool* v) { return RecvIntGeneric(v, kTagBool); }
|
|
|
|
bool SendBool(bool v) { return SendGeneric(v, kTagBool); }
|
|
|
|
bool RecvString(std::string* v);
|
|
|
|
bool SendString(const std::string& v);
|
|
|
|
|
|
|
|
bool RecvBytes(std::vector<uint8_t>* buffer);
|
2020-09-18 22:44:34 +08:00
|
|
|
bool SendBytes(const uint8_t* v, size_t len);
|
2019-03-19 00:21:48 +08:00
|
|
|
bool SendBytes(const std::vector<uint8_t>& buffer);
|
|
|
|
|
|
|
|
// Receives remote process credentials.
|
|
|
|
bool RecvCreds(pid_t* pid, uid_t* uid, gid_t* gid);
|
|
|
|
|
|
|
|
// Receives/sends file descriptors.
|
|
|
|
bool RecvFD(int* fd);
|
|
|
|
bool SendFD(int fd);
|
|
|
|
|
|
|
|
// Receives/sends protobufs.
|
2022-03-03 20:47:46 +08:00
|
|
|
bool RecvProtoBuf(google::protobuf::MessageLite* message);
|
|
|
|
bool SendProtoBuf(const google::protobuf::MessageLite& message);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Receives/sends Status objects.
|
2020-02-28 01:23:44 +08:00
|
|
|
bool RecvStatus(absl::Status* status);
|
|
|
|
bool SendStatus(const absl::Status& status);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-18 19:40:43 +08:00
|
|
|
void Swap(Comms& other) {
|
|
|
|
if (this == &other) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
using std::swap;
|
2023-09-19 20:13:40 +08:00
|
|
|
swap(name_, other.name_);
|
2023-08-18 19:40:43 +08:00
|
|
|
swap(abstract_uds_, other.abstract_uds_);
|
|
|
|
swap(connection_fd_, other.connection_fd_);
|
|
|
|
swap(state_, other.state_);
|
2023-09-19 20:13:40 +08:00
|
|
|
swap(listening_comms_, other.listening_comms_);
|
2023-08-18 19:40:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
friend void swap(Comms& x, Comms& y) { return x.Swap(y); }
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
private:
|
2022-10-11 00:38:14 +08:00
|
|
|
friend class Client;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// State of the channel
|
|
|
|
enum class State {
|
|
|
|
kUnconnected = 0,
|
|
|
|
kConnected,
|
|
|
|
kTerminated,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Connection parameters.
|
2023-09-19 20:13:40 +08:00
|
|
|
std::string name_;
|
2023-03-23 22:33:49 +08:00
|
|
|
bool abstract_uds_ = true;
|
2023-09-19 20:13:40 +08:00
|
|
|
sapi::file_util::fileops::FDCloser connection_fd_;
|
|
|
|
|
|
|
|
std::unique_ptr<ListeningComms> listening_comms_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// State of the channel (enum), socket will have to be connected later on.
|
|
|
|
State state_ = State::kUnconnected;
|
|
|
|
|
2023-09-01 04:49:41 +08:00
|
|
|
// Special struct for passing credentials or FDs.
|
|
|
|
// When passing credentials or FDs, it inlines the value. This is important as
|
|
|
|
// the data is transmitted using sendmsg/recvmsg instead of send/recv.
|
|
|
|
// It is also used when sending/receiving through SendTLV/RecvTLV to reduce
|
|
|
|
// writes/reads, although the value is written/read separately.
|
2019-03-19 00:21:48 +08:00
|
|
|
struct ABSL_ATTRIBUTE_PACKED InternalTLV {
|
|
|
|
uint32_t tag;
|
2023-09-01 04:49:41 +08:00
|
|
|
size_t len;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
// Moves the comms fd to an other free file descriptor.
|
|
|
|
void MoveToAnotherFd();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Support for EINTR and size completion.
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Send(const void* data, size_t len);
|
|
|
|
bool Recv(void* data, size_t len);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-09-19 17:04:07 +08:00
|
|
|
// Receives tag and length.
|
2023-09-04 22:00:19 +08:00
|
|
|
bool RecvTL(uint32_t* tag, size_t* length);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-09-18 17:22:54 +08:00
|
|
|
// T has to be a ContiguousContainer
|
|
|
|
template <typename T>
|
|
|
|
bool RecvTLVGeneric(uint32_t* tag, T* value);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Receives arbitrary integers.
|
2020-09-18 22:44:34 +08:00
|
|
|
bool RecvInt(void* buffer, size_t len, uint32_t tag);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool RecvIntGeneric(T* output, uint32_t tag) {
|
|
|
|
return RecvInt(output, sizeof(T), tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
bool SendGeneric(T value, uint32_t tag) {
|
2020-09-18 21:48:30 +08:00
|
|
|
return SendTLV(tag, sizeof(T), &value);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
class ListeningComms {
|
|
|
|
public:
|
|
|
|
static absl::StatusOr<ListeningComms> Create(absl::string_view socket_name,
|
|
|
|
bool abstract_uds = true);
|
|
|
|
|
|
|
|
ListeningComms(ListeningComms&& other) = default;
|
|
|
|
ListeningComms& operator=(ListeningComms&& other) = default;
|
|
|
|
~ListeningComms() = default;
|
|
|
|
absl::StatusOr<Comms> Accept();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ListeningComms(absl::string_view socket_name, bool abstract_uds)
|
|
|
|
: socket_name_(socket_name), abstract_uds_(abstract_uds), bind_fd_(-1) {}
|
|
|
|
absl::Status Listen();
|
|
|
|
|
|
|
|
std::string socket_name_;
|
|
|
|
bool abstract_uds_;
|
|
|
|
sapi::file_util::fileops::FDCloser bind_fd_;
|
|
|
|
};
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_COMMS_H_
|