Remove OsErrorMessage in favor of Abseil's new ErrnoToStatus

#Cleanup

PiperOrigin-RevId: 443359044
Change-Id: I2b3e385a1846feac79edd28fcbf6e85b1429a44a
This commit is contained in:
Christian Blichmann 2022-04-21 06:15:07 -07:00 committed by Copybara-Service
parent d8d7d74ae2
commit a60ff1a95c
18 changed files with 62 additions and 123 deletions

View File

@ -291,6 +291,7 @@ cc_library(
"//sandboxed_api/util:status", "//sandboxed_api/util:status",
"@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/memory", "@com_google_absl//absl/memory",
"@com_google_absl//absl/status",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
"@com_google_absl//absl/types:span", "@com_google_absl//absl/types:span",
], ],
@ -408,7 +409,6 @@ cc_library(
"//sandboxed_api/util:fileops", "//sandboxed_api/util:fileops",
"//sandboxed_api/util:raw_logging", "//sandboxed_api/util:raw_logging",
"//sandboxed_api/util:status", "//sandboxed_api/util:status",
"//sandboxed_api/util:strerror",
"@com_google_absl//absl/base:core_headers", "@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/status", "@com_google_absl//absl/status",

View File

@ -282,6 +282,7 @@ target_link_libraries(sandbox2_executor
sapi::base sapi::base
sapi::status_proto sapi::status_proto
PUBLIC absl::span PUBLIC absl::span
absl::status
absl::strings absl::strings
glog::glog glog::glog
sapi::config sapi::config

View File

@ -25,7 +25,6 @@
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util.h"
#include "sandboxed_api/util/os_error.h"
namespace sandbox2 { namespace sandbox2 {
@ -35,8 +34,7 @@ absl::StatusOr<std::unique_ptr<Buffer>> Buffer::CreateFromFd(int fd) {
struct stat stat_buf; struct stat stat_buf;
if (fstat(fd, &stat_buf) != 0) { if (fstat(fd, &stat_buf) != 0) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Could not stat buffer fd");
sapi::OsErrorMessage(errno, "Could not stat buffer fd"));
} }
size_t size = stat_buf.st_size; size_t size = stat_buf.st_size;
int prot = PROT_READ | PROT_WRITE; int prot = PROT_READ | PROT_WRITE;
@ -45,8 +43,7 @@ absl::StatusOr<std::unique_ptr<Buffer>> Buffer::CreateFromFd(int fd) {
buffer->buf_ = buffer->buf_ =
reinterpret_cast<uint8_t*>(mmap(nullptr, size, prot, flags, fd, offset)); reinterpret_cast<uint8_t*>(mmap(nullptr, size, prot, flags, fd, offset));
if (buffer->buf_ == MAP_FAILED) { if (buffer->buf_ == MAP_FAILED) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Could not map buffer fd");
sapi::OsErrorMessage(errno, "Could not map buffer fd"));
} }
buffer->fd_ = fd; buffer->fd_ = fd;
buffer->size_ = size; buffer->size_ = size;
@ -61,8 +58,7 @@ absl::StatusOr<std::unique_ptr<Buffer>> Buffer::CreateWithSize(size_t size) {
return absl::InternalError("Could not create buffer temp file"); return absl::InternalError("Could not create buffer temp file");
} }
if (ftruncate(fd, size) != 0) { if (ftruncate(fd, size) != 0) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Could not extend buffer fd");
sapi::OsErrorMessage(errno, "Could not extend buffer fd"));
} }
return CreateFromFd(fd); return CreateFromFd(fd);
} }

View File

@ -20,7 +20,6 @@
#include "sandboxed_api/sandbox2/comms.h" #include "sandboxed_api/sandbox2/comms.h"
#include "sandboxed_api/sandbox2/network_proxy/client.h" #include "sandboxed_api/sandbox2/network_proxy/client.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
ABSL_FLAG(bool, connect_with_handler, true, "Connect using automatic mode."); ABSL_FLAG(bool, connect_with_handler, true, "Connect using automatic mode.");
@ -64,9 +63,8 @@ absl::StatusOr<struct sockaddr_in6> CreateAddres(int port) {
saddr.sin6_family = AF_INET6; saddr.sin6_family = AF_INET6;
saddr.sin6_port = htons(port); saddr.sin6_port = htons(port);
int err = inet_pton(AF_INET6, "::1", &saddr.sin6_addr); if (int err = inet_pton(AF_INET6, "::1", &saddr.sin6_addr); err <= 0) {
if (err <= 0) { return absl::ErrnoToStatus(errno, "socket()");
return absl::InternalError(sapi::OsErrorMessage(errno, "socket() failed"));
} }
return saddr; return saddr;
} }
@ -91,7 +89,7 @@ absl::StatusOr<int> ConnectToServer(int port) {
sapi::file_util::fileops::FDCloser s(socket(AF_INET6, SOCK_STREAM, 0)); sapi::file_util::fileops::FDCloser s(socket(AF_INET6, SOCK_STREAM, 0));
if (s.get() < 0) { if (s.get() < 0) {
return absl::InternalError(sapi::OsErrorMessage(errno, "socket() failed")); return absl::ErrnoToStatus(errno, "socket()");
} }
if (absl::GetFlag(FLAGS_connect_with_handler)) { if (absl::GetFlag(FLAGS_connect_with_handler)) {

View File

@ -26,6 +26,7 @@
#include <string_view> #include <string_view>
#include "absl/memory/memory.h" #include "absl/memory/memory.h"
#include "absl/status/status.h"
#include "absl/strings/match.h" #include "absl/strings/match.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
@ -36,7 +37,6 @@
#include "sandboxed_api/sandbox2/ipc.h" #include "sandboxed_api/sandbox2/ipc.h"
#include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
namespace sandbox2 { namespace sandbox2 {
@ -92,10 +92,10 @@ absl::StatusOr<Executor::Process> Executor::StartSubProcess(
exec_fd_ = file_util::fileops::FDCloser(open(path_.c_str(), O_PATH)); exec_fd_ = file_util::fileops::FDCloser(open(path_.c_str(), O_PATH));
if (exec_fd_.get() < 0) { if (exec_fd_.get() < 0) {
if (errno == ENOENT) { if (errno == ENOENT) {
return absl::NotFoundError(sapi::OsErrorMessage(errno, path_)); return absl::ErrnoToStatus(errno, path_);
} }
return absl::InternalError( return absl::ErrnoToStatus(errno,
sapi::OsErrorMessage(errno, "Could not open file ", path_)); absl::StrCat("Could not open file ", path_));
} }
} }
@ -159,8 +159,8 @@ absl::StatusOr<Executor::Process> Executor::StartSubProcess(
absl::StrCat("/proc/", libunwind_sbox_for_pid_, "/ns/user"); absl::StrCat("/proc/", libunwind_sbox_for_pid_, "/ns/user");
ns_fd = file_util::fileops::FDCloser(open(ns_path.c_str(), O_RDONLY)); ns_fd = file_util::fileops::FDCloser(open(ns_path.c_str(), O_RDONLY));
if (ns_fd.get() == -1) { if (ns_fd.get() == -1) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, "Could not open user ns fd (", ns_path, ")")); errno, absl::StrCat("Could not open user ns fd (", ns_path, ")"));
} }
} }

View File

@ -59,7 +59,6 @@
#include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util.h"
#include "sandboxed_api/sandbox2/util/bpf_helper.h" #include "sandboxed_api/sandbox2/util/bpf_helper.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/raw_logging.h"
#include "sandboxed_api/util/strerror.h" #include "sandboxed_api/util/strerror.h"
@ -172,8 +171,7 @@ absl::Status SendPid(int signaling_fd) {
// on the socket. // on the socket.
char dummy = ' '; char dummy = ' ';
if (TEMP_FAILURE_RETRY(send(signaling_fd, &dummy, 1, 0)) != 1) { if (TEMP_FAILURE_RETRY(send(signaling_fd, &dummy, 1, 0)) != 1) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Sending PID: send()");
sapi::OsErrorMessage(errno, "Sending PID: send()"));
} }
return absl::OkStatus(); return absl::OkStatus();
} }
@ -197,15 +195,14 @@ absl::StatusOr<pid_t> ReceivePid(int signaling_fd) {
iov.iov_len = sizeof(char); iov.iov_len = sizeof(char);
if (TEMP_FAILURE_RETRY(recvmsg(signaling_fd, &msgh, MSG_WAITALL)) != 1) { if (TEMP_FAILURE_RETRY(recvmsg(signaling_fd, &msgh, MSG_WAITALL)) != 1) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Receiving pid failed: recvmsg");
sapi::OsErrorMessage(errno, "Receiving pid failed: recvmsg"));
} }
struct cmsghdr* cmsgp = CMSG_FIRSTHDR(&msgh); struct cmsghdr* cmsgp = CMSG_FIRSTHDR(&msgh);
if (cmsgp->cmsg_len != CMSG_LEN(sizeof(struct ucred)) || if (cmsgp->cmsg_len != CMSG_LEN(sizeof(struct ucred)) ||
cmsgp->cmsg_level != SOL_SOCKET || cmsgp->cmsg_type != SCM_CREDENTIALS) { cmsgp->cmsg_level != SOL_SOCKET || cmsgp->cmsg_type != SCM_CREDENTIALS) {
return absl::InternalError("Receiving pid failed"); return absl::InternalError("Receiving pid failed");
} }
struct ucred* ucredp = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsgp)); auto* ucredp = reinterpret_cast<struct ucred*>(CMSG_DATA(cmsgp));
return ucredp->pid; return ucredp->pid;
} }

View File

@ -47,7 +47,6 @@
#include "sandboxed_api/sandbox2/forkserver_bin_embed.h" #include "sandboxed_api/sandbox2/forkserver_bin_embed.h"
#include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/raw_logging.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
@ -139,9 +138,9 @@ absl::StatusOr<std::unique_ptr<GlobalForkClient>> StartGlobalForkServer() {
if (exec_fd < 0) { if (exec_fd < 0) {
// For Android we expect the forkserver_bin in the flag // For Android we expect the forkserver_bin in the flag
if constexpr (sapi::host_os::IsAndroid()) { if constexpr (sapi::host_os::IsAndroid()) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, errno,
"Open init binary passed via --sandbox2_forkserver_binary_path")); "Open init binary passed via --sandbox2_forkserver_binary_path");
} }
// Extract the fd when it's owned by EmbedFile // Extract the fd when it's owned by EmbedFile
exec_fd = sapi::EmbedFile::instance()->GetDupFdForFileToc( exec_fd = sapi::EmbedFile::instance()->GetDupFdForFileToc(
@ -156,15 +155,13 @@ absl::StatusOr<std::unique_ptr<GlobalForkClient>> StartGlobalForkServer() {
int sv[2]; int sv[2];
if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) { if (socketpair(AF_LOCAL, SOCK_STREAM | SOCK_CLOEXEC, 0, sv) == -1) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Creating socket pair failed");
sapi::OsErrorMessage(errno, "Creating socket pair failed"));
} }
// Fork the fork-server, and clean-up the resources (close remote sockets). // Fork the fork-server, and clean-up the resources (close remote sockets).
pid_t pid = util::ForkWithFlags(SIGCHLD); pid_t pid = util::ForkWithFlags(SIGCHLD);
if (pid == -1) { if (pid == -1) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "Forking forkserver process failed");
sapi::OsErrorMessage(errno, "Forking forkserver process failed"));
} }
// Child. // Child.

View File

@ -28,7 +28,6 @@
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "sandboxed_api/config.h" #include "sandboxed_api/config.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
namespace sandbox2 { namespace sandbox2 {
@ -120,8 +119,7 @@ absl::Status NetworkProxyClient::ReceiveRemoteResult() {
} }
if (result != 0) { if (result != 0) {
errno = result; errno = result;
return absl::InternalError( return absl::ErrnoToStatus(errno, "Error in network proxy server");
sapi::OsErrorMessage(errno, "Error in network proxy server"));
} }
return absl::OkStatus(); return absl::OkStatus();
} }

View File

@ -23,7 +23,6 @@
#include "absl/strings/numbers.h" #include "absl/strings/numbers.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/str_split.h" #include "absl/strings/str_split.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
namespace sandbox2 { namespace sandbox2 {
@ -66,15 +65,12 @@ absl::StatusOr<std::string> AddrToString(const struct sockaddr* saddr) {
static absl::Status IPStringToAddr(const std::string& ip, int address_family, static absl::Status IPStringToAddr(const std::string& ip, int address_family,
void* addr) { void* addr) {
int err = inet_pton(address_family, ip.c_str(), addr); if (int err = inet_pton(address_family, ip.c_str(), addr); err == 0) {
if (err == 0) {
return absl::InvalidArgumentError(absl::StrCat("Invalid address: ", ip)); return absl::InvalidArgumentError(absl::StrCat("Invalid address: ", ip));
} else if (err == -1) {
return absl::ErrnoToStatus(errno,
absl::StrCat("inet_pton() failed for ", ip));
} }
if (err == -1) {
return absl::InternalError(sapi::OsErrorMessage(
errno, absl::StrCat("inet_pton() failed for ", ip)));
}
return absl::OkStatus(); return absl::OkStatus();
} }

View File

@ -27,7 +27,6 @@
#include "absl/status/status.h" #include "absl/status/status.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "sandboxed_api/config.h" #include "sandboxed_api/config.h"
#include "sandboxed_api/util/os_error.h"
namespace sandbox2 { namespace sandbox2 {
@ -38,8 +37,8 @@ namespace sandbox2 {
absl::Status Regs::Fetch() { absl::Status Regs::Fetch() {
#ifdef SAPI_X86_64 #ifdef SAPI_X86_64
if (ptrace(PTRACE_GETREGS, pid_, 0, &user_regs_) == -1L) { if (ptrace(PTRACE_GETREGS, pid_, 0, &user_regs_) == -1L) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, absl::StrCat("ptrace(PTRACE_GETREGS, pid=", pid_, ") failed"))); errno, absl::StrCat("ptrace(PTRACE_GETREGS, pid=", pid_, ") failed"));
} }
#endif #endif
if constexpr (sapi::host_cpu::IsPPC64LE() || sapi::host_cpu::IsArm64() || if constexpr (sapi::host_cpu::IsPPC64LE() || sapi::host_cpu::IsArm64() ||
@ -47,9 +46,9 @@ absl::Status Regs::Fetch() {
iovec pt_iov = {&user_regs_, sizeof(user_regs_)}; iovec pt_iov = {&user_regs_, sizeof(user_regs_)};
if (ptrace(PTRACE_GETREGSET, pid_, NT_PRSTATUS, &pt_iov) == -1L) { if (ptrace(PTRACE_GETREGSET, pid_, NT_PRSTATUS, &pt_iov) == -1L) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, errno,
absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_, ") failed"))); absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_, ") failed"));
} }
if (pt_iov.iov_len != sizeof(user_regs_)) { if (pt_iov.iov_len != sizeof(user_regs_)) {
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrCat(
@ -63,9 +62,9 @@ absl::Status Regs::Fetch() {
iovec sys_iov = {&syscall_number_, sizeof(syscall_number_)}; iovec sys_iov = {&syscall_number_, sizeof(syscall_number_)};
if (ptrace(PTRACE_GETREGSET, pid_, NT_ARM_SYSTEM_CALL, &sys_iov) == -1L) { if (ptrace(PTRACE_GETREGSET, pid_, NT_ARM_SYSTEM_CALL, &sys_iov) == -1L) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_, errno, absl::StrCat("ptrace(PTRACE_GETREGSET, pid=", pid_,
", NT_ARM_SYSTEM_CALL)"))); ", NT_ARM_SYSTEM_CALL)"));
} }
if (sys_iov.iov_len != sizeof(syscall_number_)) { if (sys_iov.iov_len != sizeof(syscall_number_)) {
return absl::InternalError(absl::StrCat( return absl::InternalError(absl::StrCat(
@ -82,8 +81,8 @@ absl::Status Regs::Fetch() {
absl::Status Regs::Store() { absl::Status Regs::Store() {
#ifdef SAPI_X86_64 #ifdef SAPI_X86_64
if (ptrace(PTRACE_SETREGS, pid_, 0, &user_regs_) == -1) { if (ptrace(PTRACE_SETREGS, pid_, 0, &user_regs_) == -1) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, absl::StrCat("ptrace(PTRACE_SETREGS, pid=", pid_, ")"))); errno, absl::StrCat("ptrace(PTRACE_SETREGS, pid=", pid_, ")"));
} }
#endif #endif
if constexpr (sapi::host_cpu::IsPPC64LE() || sapi::host_cpu::IsArm64() || if constexpr (sapi::host_cpu::IsPPC64LE() || sapi::host_cpu::IsArm64() ||
@ -91,9 +90,9 @@ absl::Status Regs::Store() {
iovec pt_iov = {&user_regs_, sizeof(user_regs_)}; iovec pt_iov = {&user_regs_, sizeof(user_regs_)};
if (ptrace(PTRACE_SETREGSET, pid_, NT_PRSTATUS, &pt_iov) == -1L) { if (ptrace(PTRACE_SETREGSET, pid_, NT_PRSTATUS, &pt_iov) == -1L) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, errno,
absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_, ") failed"))); absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_, ") failed"));
} }
// Store syscall number on AArch64. // Store syscall number on AArch64.
@ -101,9 +100,9 @@ absl::Status Regs::Store() {
iovec sys_iov = {&syscall_number_, sizeof(syscall_number_)}; iovec sys_iov = {&syscall_number_, sizeof(syscall_number_)};
if (ptrace(PTRACE_SETREGSET, pid_, NT_ARM_SYSTEM_CALL, &sys_iov) == -1L) { if (ptrace(PTRACE_SETREGSET, pid_, NT_ARM_SYSTEM_CALL, &sys_iov) == -1L) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_, errno, absl::StrCat("ptrace(PTRACE_SETREGSET, pid=", pid_,
", NT_ARM_SYSTEM_CALL) failed"))); ", NT_ARM_SYSTEM_CALL) failed"));
} }
} }
} }

View File

@ -16,6 +16,8 @@
#include "sandboxed_api/sandbox2/sanitizer.h" #include "sandboxed_api/sandbox2/sanitizer.h"
#include "absl/status/status.h"
#if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \ #if defined(ABSL_HAVE_ADDRESS_SANITIZER) || \
defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \ defined(ABSL_HAVE_HWADDRESS_SANITIZER) || \
defined(ABSL_HAVE_LEAK_SANITIZER) || \ defined(ABSL_HAVE_LEAK_SANITIZER) || \
@ -45,10 +47,8 @@
#include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util.h"
#include "sandboxed_api/util/file_helpers.h" #include "sandboxed_api/util/file_helpers.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/raw_logging.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
#include "sandboxed_api/util/strerror.h"
namespace sandbox2::sanitizer { namespace sandbox2::sanitizer {
namespace { namespace {
@ -126,12 +126,13 @@ absl::Status MarkAllFDsAsCOEExcept(
int flags = fcntl(fd, F_GETFD); int flags = fcntl(fd, F_GETFD);
if (flags == -1) { if (flags == -1) {
return absl::InternalError( return absl::ErrnoToStatus(
sapi::OsErrorMessage(errno, "fcntl(", fd, ", F_GETFD) failed")); errno, absl::StrCat("fcntl(", fd, ", F_GETFD) failed"));
} }
if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) { if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) == -1) {
return absl::InternalError(sapi::OsErrorMessage( return absl::ErrnoToStatus(
errno, "fcntl(", fd, ", F_SETFD, ", flags, " | FD_CLOEXEC) failed")); errno, absl::StrCat("fcntl(", fd, ", F_SETFD, ", flags,
" | FD_CLOEXEC) failed"));
} }
} }
@ -184,8 +185,8 @@ absl::Status SanitizeCurrentProcess(
// If the parent goes down, so should we. // If the parent goes down, so should we.
if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) { if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) {
return absl::InternalError( return absl::ErrnoToStatus(errno,
sapi::OsErrorMessage(errno, "prctl(PR_SET_PDEATHSIG, SIGKILL) failed")); "prctl(PR_SET_PDEATHSIG, SIGKILL) failed");
} }
// Close or mark as close-on-exec open file descriptors. // Close or mark as close-on-exec open file descriptors.

View File

@ -50,7 +50,6 @@
#include "sandboxed_api/config.h" #include "sandboxed_api/config.h"
#include "sandboxed_api/util/file_helpers.h" #include "sandboxed_api/util/file_helpers.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/path.h" #include "sandboxed_api/util/path.h"
#include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/raw_logging.h"
@ -278,7 +277,7 @@ absl::StatusOr<int> Communicate(const std::vector<std::string>& argv,
posix_spawn_file_actions_t action; posix_spawn_file_actions_t action;
if (pipe(cout_pipe) == -1) { if (pipe(cout_pipe) == -1) {
return absl::UnknownError(sapi::OsErrorMessage(errno, "creating pipe")); return absl::ErrnoToStatus(errno, "creating pipe");
} }
file_util::fileops::FDCloser cout_closer{cout_pipe[1]}; file_util::fileops::FDCloser cout_closer{cout_pipe[1]};
@ -301,7 +300,7 @@ absl::StatusOr<int> Communicate(const std::vector<std::string>& argv,
if (posix_spawnp(&pid, args.array()[0], &action, nullptr, if (posix_spawnp(&pid, args.array()[0], &action, nullptr,
const_cast<char**>(args.data()), const_cast<char**>(args.data()),
const_cast<char**>(envp.data())) != 0) { const_cast<char**>(envp.data())) != 0) {
return absl::UnknownError(sapi::OsErrorMessage(errno, "posix_spawnp()")); return absl::ErrnoToStatus(errno, "posix_spawnp()");
} }
// Close child end of the pipe. // Close child end of the pipe.
@ -312,8 +311,7 @@ absl::StatusOr<int> Communicate(const std::vector<std::string>& argv,
int bytes_read = int bytes_read =
TEMP_FAILURE_RETRY(read(cout_pipe[0], &buffer[0], buffer.length())); TEMP_FAILURE_RETRY(read(cout_pipe[0], &buffer[0], buffer.length()));
if (bytes_read < 0) { if (bytes_read < 0) {
return absl::InternalError( return absl::ErrnoToStatus(errno, "reading from cout pipe");
sapi::OsErrorMessage(errno, "reading from cout pipe"));
} }
if (bytes_read == 0) { if (bytes_read == 0) {
break; // Nothing left to read break; // Nothing left to read
@ -412,13 +410,12 @@ absl::StatusOr<std::string> ReadCPathFromPid(pid_t pid, uintptr_t ptr) {
SAPI_RAW_VLOG(4, "ReadCPathFromPid (iovec): len1: %zu, len2: %zu", len1, SAPI_RAW_VLOG(4, "ReadCPathFromPid (iovec): len1: %zu, len2: %zu", len1,
len2); len2);
ssize_t sz = process_vm_readv(pid, local_iov, ABSL_ARRAYSIZE(local_iov), if (process_vm_readv(pid, local_iov, ABSL_ARRAYSIZE(local_iov), remote_iov,
remote_iov, ABSL_ARRAYSIZE(remote_iov), 0); ABSL_ARRAYSIZE(remote_iov), 0) < 0) {
if (sz < 0) { return absl::ErrnoToStatus(
return absl::InternalError(sapi::OsErrorMessage(
errno, errno,
absl::StrFormat("process_vm_readv() failed for PID: %d at address: %#x", absl::StrFormat("process_vm_readv() failed for PID: %d at address: %#x",
pid, reinterpret_cast<uintptr_t>(ptr)))); pid, reinterpret_cast<uintptr_t>(ptr)));
} }
// Check for whether there's a NUL byte in the buffer. If not, it's an // Check for whether there's a NUL byte in the buffer. If not, it's an

View File

@ -27,7 +27,6 @@
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "sandboxed_api/config.h" #include "sandboxed_api/config.h"
#include "sandboxed_api/sandbox2/util.h" #include "sandboxed_api/sandbox2/util.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/raw_logging.h" #include "sandboxed_api/util/raw_logging.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
@ -62,8 +61,7 @@ namespace {
// NOLINTNEXTLINE // NOLINTNEXTLINE
absl::Status CheckedFSeek(FILE* f, long offset, int whence) { absl::Status CheckedFSeek(FILE* f, long offset, int whence) {
if (fseek(f, offset, whence)) { if (fseek(f, offset, whence)) {
return absl::FailedPreconditionError( return absl::ErrnoToStatus(errno, "Fseek on ELF failed");
sapi::OsErrorMessage(errno, "Fseek on ELF failed"));
} }
return absl::OkStatus(); return absl::OkStatus();
} }
@ -72,8 +70,7 @@ absl::Status CheckedFRead(void* dst, size_t size, size_t nmemb, FILE* f) {
if (std::fread(dst, size, nmemb, f) == nmemb) { if (std::fread(dst, size, nmemb, f) == nmemb) {
return absl::OkStatus(); return absl::OkStatus();
} }
return absl::FailedPreconditionError( return absl::ErrnoToStatus(errno, "Reading ELF data failed");
sapi::OsErrorMessage(errno, "Reading ELF data failed"));
} }
absl::Status CheckedRead(std::string* s, FILE* f) { absl::Status CheckedRead(std::string* s, FILE* f) {
@ -479,8 +476,8 @@ absl::StatusOr<ElfFile> ElfParser::Parse(const std::string& filename,
uint32_t features) { uint32_t features) {
ElfParser parser; ElfParser parser;
if (parser.elf_ = std::fopen(filename.c_str(), "r"); !parser.elf_) { if (parser.elf_ = std::fopen(filename.c_str(), "r"); !parser.elf_) {
return absl::UnknownError(sapi::OsErrorMessage( return absl::ErrnoToStatus(errno,
errno, absl::StrCat("cannot open file: ", filename))); absl::StrCat("cannot open file: ", filename));
} }
// Basic sanity check. // Basic sanity check.

View File

@ -149,7 +149,6 @@ cc_library(
name = "status", name = "status",
srcs = ["status.cc"], srcs = ["status.cc"],
hdrs = [ hdrs = [
"os_error.h",
"status.h", "status.h",
"status_macros.h", "status_macros.h",
], ],

View File

@ -105,7 +105,6 @@ target_link_libraries(sapi_util_status_proto
# sandboxed_api/util:status # sandboxed_api/util:status
add_library(sapi_util_status ${SAPI_LIB_TYPE} add_library(sapi_util_status ${SAPI_LIB_TYPE}
os_error.h
status.cc status.cc
status.h status.h
status_macros.h status_macros.h

View File

@ -1,35 +0,0 @@
// Copyright 2021 Google LLC
//
// 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
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// 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_UTIL_OS_ERROR_H_
#define SANDBOXED_API_UTIL_OS_ERROR_H_
#include <utility>
#include "absl/strings/str_cat.h"
#include "sandboxed_api/util/strerror.h"
namespace sapi {
// Returns a formatted message with the result of `StrError(error_number)`
// appended.
template <typename... Arg>
std::string OsErrorMessage(int error_number, const Arg&... args) {
return absl::StrCat(std::forward<const Arg&>(args)..., ": ",
::sapi::StrError(error_number));
}
} // namespace sapi
#endif // SANDBOXED_API_UTIL_OS_ERROR_H_

View File

@ -26,7 +26,6 @@
#include "absl/status/statusor.h" #include "absl/status/statusor.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "sandboxed_api/util/fileops.h" #include "sandboxed_api/util/fileops.h"
#include "sandboxed_api/util/os_error.h"
#include "sandboxed_api/util/status_macros.h" #include "sandboxed_api/util/status_macros.h"
namespace sapi { namespace sapi {
@ -40,7 +39,7 @@ absl::StatusOr<std::pair<std::string, int>> CreateNamedTempFile(
std::string name_template = absl::StrCat(prefix, kMktempSuffix); std::string name_template = absl::StrCat(prefix, kMktempSuffix);
int fd = mkstemp(&name_template[0]); int fd = mkstemp(&name_template[0]);
if (fd < 0) { if (fd < 0) {
return absl::UnknownError(sapi::OsErrorMessage(errno, "mkstemp()")); return absl::ErrnoToStatus(errno, "mkstemp()");
} }
return std::pair<std::string, int>{std::move(name_template), fd}; return std::pair<std::string, int>{std::move(name_template), fd};
} }
@ -55,7 +54,7 @@ absl::StatusOr<std::string> CreateNamedTempFileAndClose(
absl::StatusOr<std::string> CreateTempDir(absl::string_view prefix) { absl::StatusOr<std::string> CreateTempDir(absl::string_view prefix) {
std::string name_template = absl::StrCat(prefix, kMktempSuffix); std::string name_template = absl::StrCat(prefix, kMktempSuffix);
if (mkdtemp(&name_template[0]) == nullptr) { if (mkdtemp(&name_template[0]) == nullptr) {
return absl::UnknownError(sapi::OsErrorMessage(errno, "mkdtemp()")); return absl::ErrnoToStatus(errno, "mkdtemp()");
} }
return name_template; return name_template;
} }

View File

@ -39,7 +39,7 @@ TEST(TempFileTest, CreateTempDirTest) {
EXPECT_THAT(path, StartsWith(prefix)); EXPECT_THAT(path, StartsWith(prefix));
EXPECT_THAT(file_util::fileops::Exists(path, false), IsTrue()); EXPECT_THAT(file_util::fileops::Exists(path, false), IsTrue());
EXPECT_THAT(CreateTempDir("non_existing_dir/prefix"), EXPECT_THAT(CreateTempDir("non_existing_dir/prefix"),
StatusIs(absl::StatusCode::kUnknown)); StatusIs(absl::StatusCode::kNotFound));
} }
TEST(TempFileTest, MakeTempFileTest) { TEST(TempFileTest, MakeTempFileTest) {
@ -54,7 +54,7 @@ TEST(TempFileTest, MakeTempFileTest) {
EXPECT_THAT(fcntl(fd, F_GETFD), Ne(-1)); EXPECT_THAT(fcntl(fd, F_GETFD), Ne(-1));
EXPECT_THAT(close(fd), Eq(0)); EXPECT_THAT(close(fd), Eq(0));
EXPECT_THAT(CreateNamedTempFile("non_existing_dir/prefix"), EXPECT_THAT(CreateNamedTempFile("non_existing_dir/prefix"),
StatusIs(absl::StatusCode::kUnknown)); StatusIs(absl::StatusCode::kNotFound));
} }
} // namespace } // namespace