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
|
|
|
|
//
|
|
|
|
// http://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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/unwind/ptrace_hook.h"
|
|
|
|
|
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2021-04-22 21:56:22 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-22 21:56:22 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-22 21:56:22 +08:00
|
|
|
// Register size is `long` for the supported architectures according to the
|
|
|
|
// kernel.
|
|
|
|
using RegType = long; // NOLINT
|
|
|
|
constexpr size_t kRegSize = sizeof(RegType);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-22 21:56:22 +08:00
|
|
|
// Contains the register values in a ptrace specified format. This format is
|
|
|
|
// pretty opaque which is why we just forward the raw bytes (up to a certain
|
|
|
|
// limit).
|
|
|
|
auto* g_registers = new std::vector<RegType>();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-22 21:56:22 +08:00
|
|
|
// Whether ptrace() emulation is in effect. This can only be enabled (per
|
|
|
|
// thread), never disabled.
|
|
|
|
thread_local bool g_emulate_ptrace = false;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void EnablePtraceEmulationWithUserRegs(absl::string_view regs) {
|
|
|
|
g_registers->resize((regs.size() + 1) / kRegSize);
|
|
|
|
memcpy(&g_registers->front(), regs.data(), regs.size());
|
|
|
|
g_emulate_ptrace = true;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replaces the libc version of ptrace.
|
|
|
|
// This wrapper makes use of process_vm_readv to read process memory instead of
|
|
|
|
// issuing ptrace syscalls. Accesses to registers will be emulated, for this the
|
2021-04-22 21:56:22 +08:00
|
|
|
// register values should be set via EnablePtraceEmulationWithUserRegs().
|
2019-03-19 00:21:48 +08:00
|
|
|
extern "C" long int ptrace_wrapped( // NOLINT
|
2021-04-22 21:56:22 +08:00
|
|
|
enum __ptrace_request request, pid_t pid, void* addr, void* data) {
|
|
|
|
if (!g_emulate_ptrace) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return ptrace(request, pid, addr, data);
|
|
|
|
}
|
2021-04-22 21:56:22 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
switch (request) {
|
|
|
|
case PTRACE_PEEKDATA: {
|
|
|
|
long int read_data; // NOLINT
|
2021-04-22 21:56:22 +08:00
|
|
|
struct iovec local = {
|
|
|
|
.iov_base = &read_data,
|
|
|
|
.iov_len = sizeof(long int), // NOLINT
|
|
|
|
};
|
|
|
|
struct iovec remote = {
|
|
|
|
.iov_base = addr,
|
|
|
|
.iov_len = sizeof(long int), // NOLINT
|
|
|
|
};
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-22 21:56:22 +08:00
|
|
|
if (process_vm_readv(pid, &local, 1, &remote, 1, 0) <= 0) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return -1;
|
|
|
|
}
|
2021-04-22 21:56:22 +08:00
|
|
|
return read_data;
|
|
|
|
}
|
|
|
|
case PTRACE_PEEKUSER:
|
2019-03-19 00:21:48 +08:00
|
|
|
// Make sure read is in-bounds and aligned.
|
2021-04-22 21:56:22 +08:00
|
|
|
if (uintptr_t offset = reinterpret_cast<uintptr_t>(addr);
|
|
|
|
offset + kRegSize > g_registers->size() * kRegSize ||
|
|
|
|
offset % kRegSize != 0) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return -1;
|
2021-04-22 21:56:22 +08:00
|
|
|
} else {
|
|
|
|
return (*g_registers)[offset / kRegSize];
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
2021-04-22 21:56:22 +08:00
|
|
|
default:
|
|
|
|
fprintf(stderr, "ptrace_wrapped(): operation not permitted: %d\n",
|
2019-03-19 00:21:48 +08:00
|
|
|
request);
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-04-22 21:56:22 +08:00
|
|
|
|
|
|
|
} // namespace sandbox2
|