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.
|
|
|
|
|
|
|
|
// Implementation of sandbox2::Comms class.
|
|
|
|
//
|
|
|
|
// Warning: This class is not multi-thread safe (for callers). It uses a single
|
|
|
|
// communications channel (an AF_UNIX socket), so it requires exactly one sender
|
|
|
|
// and one receiver. If you plan to use it from many threads, provide external
|
|
|
|
// exclusive locking.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/comms.h"
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <syscall.h>
|
|
|
|
#include <unistd.h>
|
2019-04-24 01:29:37 +08:00
|
|
|
|
2024-02-14 23:12:01 +08:00
|
|
|
#include <algorithm>
|
2021-05-18 20:48:30 +08:00
|
|
|
#include <atomic>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cerrno>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cstdint>
|
2022-10-11 00:38:14 +08:00
|
|
|
#include <cstdlib>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <cstring>
|
|
|
|
#include <functional>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <string>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-02-01 23:10:43 +08:00
|
|
|
#include "absl/base/dynamic_annotations.h"
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/strings/numbers.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/str_format.h"
|
2023-09-19 20:13:40 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2023-08-25 14:54:54 +08:00
|
|
|
#include "google/protobuf/message_lite.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util.h"
|
2023-09-19 20:13:40 +08:00
|
|
|
#include "sandboxed_api/util/fileops.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/util/raw_logging.h"
|
|
|
|
#include "sandboxed_api/util/status.h"
|
2023-08-25 21:49:42 +08:00
|
|
|
#include "sandboxed_api/util/status.pb.h"
|
2023-09-19 20:13:40 +08:00
|
|
|
#include "sandboxed_api/util/status_macros.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-01-26 18:28:37 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
class PotentiallyBlockingRegion {
|
|
|
|
public:
|
|
|
|
~PotentiallyBlockingRegion() {
|
|
|
|
// Do nothing. Not defaulted to avoid "unused variable" warnings.
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
2023-09-19 20:13:40 +08:00
|
|
|
|
|
|
|
using sapi::file_util::fileops::FDCloser;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
bool IsFatalError(int saved_errno) {
|
|
|
|
return saved_errno != EAGAIN && saved_errno != EWOULDBLOCK &&
|
|
|
|
saved_errno != EFAULT && saved_errno != EINTR &&
|
|
|
|
saved_errno != EINVAL && saved_errno != ENOMEM;
|
|
|
|
}
|
2022-10-11 00:38:14 +08:00
|
|
|
|
|
|
|
int GetDefaultCommsFd() {
|
|
|
|
if (const char* var = getenv(Comms::kSandbox2CommsFDEnvVar); var) {
|
|
|
|
int fd;
|
|
|
|
SAPI_RAW_CHECK(absl::SimpleAtoi(var, &fd), "cannot parse comms fd var");
|
|
|
|
unsetenv(Comms::kSandbox2CommsFDEnvVar);
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
return Comms::kSandbox2ClientCommsFD;
|
|
|
|
}
|
2023-09-19 20:13:40 +08:00
|
|
|
|
|
|
|
socklen_t CreateSockaddrUn(const std::string& socket_name, bool abstract_uds,
|
|
|
|
sockaddr_un* sun) {
|
|
|
|
sun->sun_family = AF_UNIX;
|
|
|
|
bzero(sun->sun_path, sizeof(sun->sun_path));
|
|
|
|
socklen_t slen = sizeof(sun->sun_family) + strlen(socket_name.c_str());
|
|
|
|
if (abstract_uds) {
|
|
|
|
// Create an 'abstract socket address' by specifying a leading null byte.
|
|
|
|
// The remainder of the path is used as a unique name, but no file is
|
|
|
|
// created on the filesystem. No need to NUL-terminate the string. See `man
|
|
|
|
// 7 unix` for further explanation.
|
|
|
|
strncpy(&sun->sun_path[1], socket_name.c_str(), sizeof(sun->sun_path) - 1);
|
|
|
|
// Len is complicated - it's essentially size of the path, plus initial
|
|
|
|
// NUL-byte, minus size of the sun.sun_family.
|
|
|
|
slen++;
|
|
|
|
} else {
|
|
|
|
// Create the socket address as it was passed from the constructor.
|
|
|
|
strncpy(&sun->sun_path[0], socket_name.c_str(), sizeof(sun->sun_path));
|
|
|
|
}
|
|
|
|
|
|
|
|
// This takes care of the socket address overflow.
|
|
|
|
if (slen > sizeof(sockaddr_un)) {
|
|
|
|
SAPI_RAW_LOG(ERROR, "Socket address is too long, will be truncated");
|
|
|
|
slen = sizeof(sockaddr_un);
|
|
|
|
}
|
|
|
|
return slen;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
Comms::Comms(int fd, absl::string_view name) : connection_fd_(fd) {
|
2019-03-19 00:21:48 +08:00
|
|
|
// Generate a unique and meaningful socket name for this FD.
|
|
|
|
// Note: getpid()/gettid() are non-blocking syscalls.
|
2023-09-19 20:13:40 +08:00
|
|
|
if (name.empty()) {
|
|
|
|
name_ = absl::StrFormat("sandbox2::Comms:FD=%d/PID=%d/TID=%ld", fd,
|
|
|
|
getpid(), syscall(__NR_gettid));
|
|
|
|
} else {
|
|
|
|
name_ = std::string(name);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// File descriptor is already connected.
|
|
|
|
state_ = State::kConnected;
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
Comms::Comms(Comms::DefaultConnectionTag) : Comms(GetDefaultCommsFd()) {}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
Comms::~Comms() { Terminate(); }
|
|
|
|
|
|
|
|
int Comms::GetConnectionFD() const {
|
2023-09-19 20:13:40 +08:00
|
|
|
return connection_fd_.get();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
absl::StatusOr<ListeningComms> ListeningComms::Create(
|
|
|
|
absl::string_view socket_name, bool abstract_uds) {
|
|
|
|
ListeningComms comms(std::string(socket_name), abstract_uds);
|
|
|
|
SAPI_RETURN_IF_ERROR(comms.Listen());
|
|
|
|
return comms;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
absl::Status ListeningComms::Listen() {
|
|
|
|
bind_fd_ = FDCloser(socket(AF_UNIX, SOCK_STREAM, 0)); // Non-blocking
|
|
|
|
if (bind_fd_.get() == -1) {
|
|
|
|
return absl::ErrnoToStatus(errno, "socket(AF_UNIX) failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sockaddr_un sus;
|
2023-09-19 20:13:40 +08:00
|
|
|
socklen_t slen = CreateSockaddrUn(socket_name_, abstract_uds_, &sus);
|
2019-03-19 00:21:48 +08:00
|
|
|
// bind() is non-blocking.
|
2023-09-19 20:13:40 +08:00
|
|
|
if (bind(bind_fd_.get(), reinterpret_cast<sockaddr*>(&sus), slen) == -1) {
|
|
|
|
return absl::ErrnoToStatus(errno, "bind failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// listen() non-blocking.
|
2023-09-19 20:13:40 +08:00
|
|
|
if (listen(bind_fd_.get(), 0) == -1) {
|
|
|
|
return absl::ErrnoToStatus(errno, "listen failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Listening at: %s", socket_name_.c_str());
|
2023-09-19 20:13:40 +08:00
|
|
|
return absl::OkStatus();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
absl::StatusOr<Comms> ListeningComms::Accept() {
|
2019-03-19 00:21:48 +08:00
|
|
|
sockaddr_un suc;
|
|
|
|
socklen_t len = sizeof(suc);
|
2023-09-19 20:13:40 +08:00
|
|
|
int connection_fd;
|
2019-03-19 00:21:48 +08:00
|
|
|
{
|
|
|
|
PotentiallyBlockingRegion region;
|
2023-09-19 20:13:40 +08:00
|
|
|
connection_fd = TEMP_FAILURE_RETRY(
|
|
|
|
accept(bind_fd_.get(), reinterpret_cast<sockaddr*>(&suc), &len));
|
|
|
|
}
|
|
|
|
if (connection_fd == -1) {
|
|
|
|
return absl::ErrnoToStatus(errno, "accept failed");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Accepted connection at: %s, fd: %d", socket_name_.c_str(),
|
2023-09-19 20:13:40 +08:00
|
|
|
connection_fd);
|
|
|
|
return Comms(connection_fd, socket_name_);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
absl::StatusOr<Comms> Comms::Connect(const std::string& socket_name,
|
|
|
|
bool abstract_uds) {
|
|
|
|
FDCloser connection_fd(socket(AF_UNIX, SOCK_STREAM, 0)); // Non-blocking
|
|
|
|
if (connection_fd.get() == -1) {
|
|
|
|
return absl::ErrnoToStatus(errno, "socket(AF_UNIX)");
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
sockaddr_un suc;
|
2023-09-19 20:13:40 +08:00
|
|
|
socklen_t slen = CreateSockaddrUn(socket_name, abstract_uds, &suc);
|
2019-03-19 00:21:48 +08:00
|
|
|
int ret;
|
|
|
|
{
|
|
|
|
PotentiallyBlockingRegion region;
|
|
|
|
ret = TEMP_FAILURE_RETRY(
|
2023-09-19 20:13:40 +08:00
|
|
|
connect(connection_fd.get(), reinterpret_cast<sockaddr*>(&suc), slen));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
if (ret == -1) {
|
2023-09-19 20:13:40 +08:00
|
|
|
return absl::ErrnoToStatus(errno, "connect(connection_fd)");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
SAPI_RAW_VLOG(1, "Connected to: %s, fd: %d", socket_name.c_str(),
|
|
|
|
connection_fd.get());
|
|
|
|
return Comms(connection_fd.Release(), socket_name);
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
void Comms::Terminate() {
|
2023-09-19 20:13:40 +08:00
|
|
|
state_ = State::kTerminated;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-09-19 20:13:40 +08:00
|
|
|
connection_fd_.Close();
|
|
|
|
listening_comms_.reset();
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::SendTLV(uint32_t tag, size_t length, const void* value) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (length > GetMaxMsgSize()) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Maximum TLV message size exceeded: (%zu > %zu)",
|
|
|
|
length, GetMaxMsgSize());
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (length > kWarnMsgSize) {
|
2021-01-26 18:28:37 +08:00
|
|
|
// TODO(cblichmann): Use LOG_FIRST_N once Abseil logging is released.
|
2021-05-18 20:48:30 +08:00
|
|
|
static std::atomic<int> times_warned = 0;
|
|
|
|
if (times_warned.fetch_add(1, std::memory_order_relaxed) < 10) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(
|
|
|
|
WARNING,
|
|
|
|
"TLV message of size %zu detected. Please consider switching "
|
|
|
|
"to Buffer API instead.",
|
|
|
|
length);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_VLOG(3, "Sending a TLV message, tag: 0x%08x, length: %zu", tag,
|
2019-03-19 00:21:48 +08:00
|
|
|
length);
|
2023-09-01 04:49:41 +08:00
|
|
|
|
|
|
|
// To maintain consistency with `RecvTL()`, we wrap `tag` and `length` in a TL
|
|
|
|
// struct.
|
|
|
|
const InternalTLV tl = {
|
|
|
|
.tag = tag,
|
|
|
|
.len = length,
|
|
|
|
};
|
|
|
|
|
2024-02-14 23:12:01 +08:00
|
|
|
const size_t inline_size =
|
|
|
|
std::min(length, kSendTLVTempBufferSize - sizeof(tl));
|
2023-09-01 04:49:41 +08:00
|
|
|
uint8_t tlv[kSendTLVTempBufferSize];
|
|
|
|
memcpy(tlv, &tl, sizeof(tl));
|
2024-02-14 23:12:01 +08:00
|
|
|
memcpy(&tlv[sizeof(tl)], value, inline_size);
|
|
|
|
if (!Send(&tlv, sizeof(tl) + inline_size)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (inline_size < length) {
|
|
|
|
return Send(reinterpret_cast<const uint8_t*>(value) + inline_size,
|
|
|
|
length - inline_size);
|
|
|
|
}
|
|
|
|
return true;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::RecvString(std::string* v) {
|
2020-09-18 17:22:54 +08:00
|
|
|
uint32_t tag;
|
|
|
|
if (!RecvTLV(&tag, v)) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:22:54 +08:00
|
|
|
if (tag != kTagString) {
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Expected (kTagString == 0x%x), got: 0x%x", kTagString,
|
2020-09-18 17:22:54 +08:00
|
|
|
tag);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::SendString(const std::string& v) {
|
2020-09-18 21:48:30 +08:00
|
|
|
return SendTLV(kTagString, v.length(), v.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::RecvBytes(std::vector<uint8_t>* buffer) {
|
2020-09-18 17:22:54 +08:00
|
|
|
uint32_t tag;
|
|
|
|
if (!RecvTLV(&tag, buffer)) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-18 17:22:54 +08:00
|
|
|
if (tag != kTagBytes) {
|
|
|
|
buffer->clear();
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Expected (kTagBytes == 0x%x), got: 0x%u", kTagBytes,
|
2020-09-18 17:22:54 +08:00
|
|
|
tag);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::SendBytes(const uint8_t* v, size_t len) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return SendTLV(kTagBytes, len, v);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::SendBytes(const std::vector<uint8_t>& buffer) {
|
|
|
|
return SendBytes(buffer.data(), buffer.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::RecvCreds(pid_t* pid, uid_t* uid, gid_t* gid) {
|
|
|
|
ucred uc;
|
|
|
|
socklen_t sls = sizeof(uc);
|
|
|
|
int rc;
|
|
|
|
{
|
|
|
|
// Not completely sure if getsockopt() can block on SO_PEERCRED, but let's
|
|
|
|
// play it safe.
|
|
|
|
PotentiallyBlockingRegion region;
|
|
|
|
rc = getsockopt(GetConnectionFD(), SOL_SOCKET, SO_PEERCRED, &uc, &sls);
|
|
|
|
}
|
|
|
|
if (rc == -1) {
|
|
|
|
SAPI_RAW_PLOG(ERROR, "getsockopt(SO_PEERCRED)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*pid = uc.pid;
|
|
|
|
*uid = uc.uid;
|
|
|
|
*gid = uc.gid;
|
|
|
|
|
|
|
|
SAPI_RAW_VLOG(2, "Received credentials from PID/UID/GID: %d/%u/%u", *pid,
|
|
|
|
*uid, *gid);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::RecvFD(int* fd) {
|
|
|
|
char fd_msg[8192];
|
|
|
|
cmsghdr* cmsg = reinterpret_cast<cmsghdr*>(fd_msg);
|
|
|
|
|
|
|
|
InternalTLV tlv;
|
2021-01-26 18:28:37 +08:00
|
|
|
iovec iov = {.iov_base = &tlv, .iov_len = sizeof(tlv)};
|
|
|
|
|
|
|
|
msghdr msg = {
|
|
|
|
.msg_name = nullptr,
|
|
|
|
.msg_namelen = 0,
|
|
|
|
.msg_iov = &iov,
|
|
|
|
.msg_iovlen = 1,
|
|
|
|
.msg_control = cmsg,
|
|
|
|
.msg_controllen = sizeof(fd_msg),
|
|
|
|
.msg_flags = 0,
|
|
|
|
};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
const auto op = [&msg](int fd) -> ssize_t {
|
|
|
|
PotentiallyBlockingRegion region;
|
2020-09-11 18:13:46 +08:00
|
|
|
// Use syscall, otherwise we would need to allow socketcall() on PPC.
|
2019-03-19 00:21:48 +08:00
|
|
|
return TEMP_FAILURE_RETRY(
|
|
|
|
util::Syscall(__NR_recvmsg, fd, reinterpret_cast<uintptr_t>(&msg), 0));
|
|
|
|
};
|
|
|
|
ssize_t len;
|
2023-09-19 20:13:40 +08:00
|
|
|
len = op(connection_fd_.get());
|
2019-03-19 00:21:48 +08:00
|
|
|
if (len < 0) {
|
|
|
|
if (IsFatalError(errno)) {
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
SAPI_RAW_PLOG(ERROR, "recvmsg(SCM_RIGHTS)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (len == 0) {
|
|
|
|
Terminate();
|
|
|
|
SAPI_RAW_VLOG(1, "RecvFD: end-point terminated the connection.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (len != sizeof(tlv)) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Expected size: %zu, got %zd", sizeof(tlv), len);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// At this point, we know that op() has been called successfully, therefore
|
|
|
|
// msg struct has been fully populated. Apparently MSAN is not aware of
|
|
|
|
// syscall(__NR_recvmsg) semantics so we need to suppress the error (here and
|
|
|
|
// everywhere below).
|
2020-06-18 18:18:37 +08:00
|
|
|
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(&tlv, sizeof(tlv));
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
if (tlv.tag != kTagFd) {
|
2022-01-13 22:20:15 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Expected (kTagFD: 0x%x), got: 0x%x", kTagFd, tlv.tag);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cmsg = CMSG_FIRSTHDR(&msg);
|
2020-06-18 18:18:37 +08:00
|
|
|
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(cmsg, sizeof(cmsghdr));
|
2019-03-19 00:21:48 +08:00
|
|
|
while (cmsg) {
|
|
|
|
if (cmsg->cmsg_level == SOL_SOCKET && cmsg->cmsg_type == SCM_RIGHTS) {
|
|
|
|
if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
|
|
|
|
SAPI_RAW_VLOG(1,
|
|
|
|
"recvmsg(SCM_RIGHTS): cmsg->cmsg_len != "
|
|
|
|
"CMSG_LEN(sizeof(int)), skipping");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
|
|
|
|
*fd = fds[0];
|
2020-06-18 18:18:37 +08:00
|
|
|
ABSL_ANNOTATE_MEMORY_IS_INITIALIZED(fd, sizeof(int));
|
2019-03-19 00:21:48 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
cmsg = CMSG_NXTHDR(&msg, cmsg);
|
|
|
|
}
|
|
|
|
SAPI_RAW_LOG(ERROR,
|
|
|
|
"Haven't received the SCM_RIGHTS message, process is probably "
|
|
|
|
"out of free file descriptors");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::SendFD(int fd) {
|
|
|
|
char fd_msg[CMSG_SPACE(sizeof(int))] = {0};
|
|
|
|
cmsghdr* cmsg = reinterpret_cast<cmsghdr*>(fd_msg);
|
|
|
|
cmsg->cmsg_level = SOL_SOCKET;
|
|
|
|
cmsg->cmsg_type = SCM_RIGHTS;
|
|
|
|
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
|
|
|
|
|
|
|
|
int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
|
|
|
|
fds[0] = fd;
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
InternalTLV tlv = {kTagFd, 0};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
iovec iov;
|
|
|
|
iov.iov_base = &tlv;
|
|
|
|
iov.iov_len = sizeof(tlv);
|
|
|
|
|
|
|
|
msghdr msg;
|
|
|
|
msg.msg_name = nullptr;
|
|
|
|
msg.msg_namelen = 0;
|
|
|
|
msg.msg_iov = &iov;
|
|
|
|
msg.msg_iovlen = 1;
|
|
|
|
msg.msg_control = cmsg;
|
|
|
|
msg.msg_controllen = sizeof(fd_msg);
|
|
|
|
msg.msg_flags = 0;
|
|
|
|
|
|
|
|
const auto op = [&msg](int fd) -> ssize_t {
|
|
|
|
PotentiallyBlockingRegion region;
|
|
|
|
// Use syscall, otherwise we would need to whitelist socketcall() on PPC.
|
|
|
|
return TEMP_FAILURE_RETRY(
|
|
|
|
util::Syscall(__NR_sendmsg, fd, reinterpret_cast<uintptr_t>(&msg), 0));
|
|
|
|
};
|
|
|
|
ssize_t len;
|
2023-09-19 20:13:40 +08:00
|
|
|
len = op(connection_fd_.get());
|
2019-03-19 00:21:48 +08:00
|
|
|
if (len == -1 && errno == EPIPE) {
|
|
|
|
Terminate();
|
|
|
|
SAPI_RAW_LOG(ERROR, "sendmsg(SCM_RIGHTS): Peer disconnected");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (len < 0) {
|
|
|
|
if (IsFatalError(errno)) {
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
SAPI_RAW_PLOG(ERROR, "sendmsg(SCM_RIGHTS)");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (len != sizeof(tlv)) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Expected to send %zu bytes, sent %zd", sizeof(tlv),
|
|
|
|
len);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-03 20:47:46 +08:00
|
|
|
bool Comms::RecvProtoBuf(google::protobuf::MessageLite* message) {
|
2020-09-18 17:22:54 +08:00
|
|
|
uint32_t tag;
|
|
|
|
std::vector<uint8_t> bytes;
|
|
|
|
if (!RecvTLV(&tag, &bytes)) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (IsConnected()) {
|
2023-09-19 20:13:40 +08:00
|
|
|
SAPI_RAW_PLOG(ERROR, "RecvProtoBuf failed for (%s)", name_);
|
2019-03-19 00:21:48 +08:00
|
|
|
} else {
|
|
|
|
Terminate();
|
2023-09-19 20:13:40 +08:00
|
|
|
SAPI_RAW_VLOG(2, "Connection terminated (%s)", name_.c_str());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:22:54 +08:00
|
|
|
if (tag != kTagProto2) {
|
|
|
|
SAPI_RAW_LOG(ERROR, "Expected tag: 0x%x, got: 0x%u", kTagProto2, tag);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-18 17:22:54 +08:00
|
|
|
return message->ParseFromArray(bytes.data(), bytes.size());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2022-03-03 20:47:46 +08:00
|
|
|
bool Comms::SendProtoBuf(const google::protobuf::MessageLite& message) {
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string str;
|
|
|
|
if (!message.SerializeToString(&str)) {
|
|
|
|
SAPI_RAW_LOG(ERROR, "Couldn't serialize the ProtoBuf");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SendTLV(kTagProto2, str.length(),
|
|
|
|
reinterpret_cast<const uint8_t*>(str.data()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// *****************************************************************************
|
|
|
|
// All methods below are private, for internal use only.
|
|
|
|
// *****************************************************************************
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::Send(const void* data, size_t len) {
|
|
|
|
size_t total_sent = 0;
|
2020-09-18 21:48:30 +08:00
|
|
|
const char* bytes = reinterpret_cast<const char*>(data);
|
2019-03-19 00:21:48 +08:00
|
|
|
const auto op = [bytes, len, &total_sent](int fd) -> ssize_t {
|
|
|
|
PotentiallyBlockingRegion region;
|
|
|
|
return TEMP_FAILURE_RETRY(write(fd, &bytes[total_sent], len - total_sent));
|
|
|
|
};
|
|
|
|
while (total_sent < len) {
|
|
|
|
ssize_t s;
|
2023-09-19 20:13:40 +08:00
|
|
|
s = op(connection_fd_.get());
|
2019-03-19 00:21:48 +08:00
|
|
|
if (s == -1 && errno == EPIPE) {
|
|
|
|
Terminate();
|
|
|
|
// We do not expect the other end to disappear.
|
|
|
|
SAPI_RAW_LOG(ERROR, "Send: end-point terminated the connection");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (s == -1) {
|
2021-07-29 22:11:32 +08:00
|
|
|
SAPI_RAW_PLOG(ERROR, "write");
|
2019-03-19 00:21:48 +08:00
|
|
|
if (IsFatalError(errno)) {
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (s == 0) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR,
|
|
|
|
"Couldn't write more bytes, wrote: %zu, requested: %zu",
|
2019-03-19 00:21:48 +08:00
|
|
|
total_sent, len);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
total_sent += s;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::Recv(void* data, size_t len) {
|
|
|
|
size_t total_recv = 0;
|
2020-09-18 21:48:30 +08:00
|
|
|
char* bytes = reinterpret_cast<char*>(data);
|
2019-03-19 00:21:48 +08:00
|
|
|
const auto op = [bytes, len, &total_recv](int fd) -> ssize_t {
|
|
|
|
PotentiallyBlockingRegion region;
|
|
|
|
return TEMP_FAILURE_RETRY(read(fd, &bytes[total_recv], len - total_recv));
|
|
|
|
};
|
|
|
|
while (total_recv < len) {
|
|
|
|
ssize_t s;
|
2023-09-19 20:13:40 +08:00
|
|
|
s = op(connection_fd_.get());
|
2019-03-19 00:21:48 +08:00
|
|
|
if (s == -1) {
|
2021-07-29 22:11:32 +08:00
|
|
|
SAPI_RAW_PLOG(ERROR, "read");
|
2019-03-19 00:21:48 +08:00
|
|
|
if (IsFatalError(errno)) {
|
|
|
|
Terminate();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (s == 0) {
|
|
|
|
Terminate();
|
|
|
|
// The other end might have finished its work.
|
|
|
|
SAPI_RAW_VLOG(2, "Recv: end-point terminated the connection.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
total_recv += s;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Internal helper method (low level).
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::RecvTL(uint32_t* tag, size_t* length) {
|
2023-09-01 04:49:41 +08:00
|
|
|
InternalTLV tl;
|
|
|
|
if (!Recv(reinterpret_cast<uint8_t*>(&tl), sizeof(tl))) {
|
|
|
|
SAPI_RAW_VLOG(2, "RecvTL: Can't read tag and length");
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-09-01 04:49:41 +08:00
|
|
|
*tag = tl.tag;
|
|
|
|
*length = tl.len;
|
2019-03-19 00:21:48 +08:00
|
|
|
if (*length > GetMaxMsgSize()) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Maximum TLV message size exceeded: (%zu > %zd)",
|
|
|
|
*length, GetMaxMsgSize());
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (*length > kWarnMsgSize) {
|
2021-05-18 20:48:30 +08:00
|
|
|
static std::atomic<int> times_warned = 0;
|
|
|
|
if (times_warned.fetch_add(1, std::memory_order_relaxed) < 10) {
|
2019-03-19 00:21:48 +08:00
|
|
|
SAPI_RAW_LOG(
|
|
|
|
WARNING,
|
2021-05-18 20:48:30 +08:00
|
|
|
"TLV message of size: %zu detected. Please consider switching to "
|
2019-03-19 00:21:48 +08:00
|
|
|
"Buffer API instead.",
|
|
|
|
*length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-18 17:22:54 +08:00
|
|
|
bool Comms::RecvTLV(uint32_t* tag, std::vector<uint8_t>* value) {
|
|
|
|
return RecvTLVGeneric(tag, value);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-09-18 17:22:54 +08:00
|
|
|
bool Comms::RecvTLV(uint32_t* tag, std::string* value) {
|
|
|
|
return RecvTLVGeneric(tag, value);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 17:22:54 +08:00
|
|
|
template <typename T>
|
|
|
|
bool Comms::RecvTLVGeneric(uint32_t* tag, T* value) {
|
2020-09-18 22:44:34 +08:00
|
|
|
size_t length;
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!RecvTL(tag, &length)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
value->resize(length);
|
2020-09-18 17:22:54 +08:00
|
|
|
return length == 0 || Recv(reinterpret_cast<uint8_t*>(value->data()), length);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::RecvTLV(uint32_t* tag, size_t* length, void* buffer,
|
|
|
|
size_t buffer_size) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!RecvTL(tag, length)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
if (*length == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
if (*length > buffer_size) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Buffer size too small (0x%zx > 0x%zx)", *length,
|
2019-03-19 00:21:48 +08:00
|
|
|
buffer_size);
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-18 22:44:34 +08:00
|
|
|
|
|
|
|
return Recv(reinterpret_cast<uint8_t*>(buffer), *length);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2020-09-18 22:44:34 +08:00
|
|
|
bool Comms::RecvInt(void* buffer, size_t len, uint32_t tag) {
|
2019-03-19 00:21:48 +08:00
|
|
|
uint32_t received_tag;
|
2020-09-18 22:44:34 +08:00
|
|
|
size_t received_length;
|
2019-03-19 00:21:48 +08:00
|
|
|
if (!RecvTLV(&received_tag, &received_length, buffer, len)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (received_tag != tag) {
|
|
|
|
SAPI_RAW_LOG(ERROR, "Expected tag: 0x%08x, got: 0x%x", tag, received_tag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (received_length != len) {
|
2020-12-14 19:33:29 +08:00
|
|
|
SAPI_RAW_LOG(ERROR, "Expected length: %zu, got: %zu", len, received_length);
|
2019-03-19 00:21:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
bool Comms::RecvStatus(absl::Status* status) {
|
|
|
|
sapi::StatusProto proto;
|
|
|
|
if (!RecvProtoBuf(&proto)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*status = sapi::MakeStatusFromProto(proto);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Comms::SendStatus(const absl::Status& status) {
|
|
|
|
sapi::StatusProto proto;
|
|
|
|
sapi::SaveStatusToProto(status, &proto);
|
|
|
|
return SendProtoBuf(proto);
|
|
|
|
}
|
|
|
|
|
2022-10-11 00:38:14 +08:00
|
|
|
void Comms::MoveToAnotherFd() {
|
2023-09-19 20:13:40 +08:00
|
|
|
SAPI_RAW_CHECK(connection_fd_.get() != -1,
|
2022-10-11 00:38:14 +08:00
|
|
|
"Cannot move comms fd as it's not connected");
|
2023-09-19 20:13:40 +08:00
|
|
|
FDCloser new_fd(dup(connection_fd_.get()));
|
|
|
|
SAPI_RAW_CHECK(new_fd.get() != -1, "Failed to move comms to another fd");
|
|
|
|
connection_fd_.Swap(new_fd);
|
2022-10-11 00:38:14 +08:00
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sandbox2
|