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.
|
|
|
|
|
|
|
|
// This file defines the sandbox2::Regs class stores context of a process
|
|
|
|
// during ptrace stop events
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_REGS_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_REGS_H_
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
#include "absl/status/status.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/syscall.h"
|
2019-03-20 20:19:28 +08:00
|
|
|
#include "sandboxed_api/sandbox2/violation.pb.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
|
|
|
// Helper class to get and modify running processes registers. Uses ptrace and
|
|
|
|
// assumes the process is already attached.
|
|
|
|
class Regs {
|
|
|
|
public:
|
|
|
|
explicit Regs(pid_t pid) : pid_(pid) {}
|
|
|
|
|
|
|
|
// Copies register values from the process
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Fetch();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Copies register values to the process
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status Store();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Causes the process to skip current syscall and return given value instead
|
2020-12-17 01:17:53 +08:00
|
|
|
absl::Status SkipSyscallReturnValue(uintptr_t value);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Converts raw register values obtained on syscall entry to syscall info
|
2021-01-14 01:25:25 +08:00
|
|
|
Syscall ToSyscall(sapi::cpu::Architecture syscall_arch) const;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-01-13 22:48:44 +08:00
|
|
|
// Returns the content of the register that holds a syscall's return value
|
|
|
|
int64_t GetReturnValue(sapi::cpu::Architecture syscall_arch) const;
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
pid_t pid() const { return pid_; }
|
|
|
|
|
|
|
|
// Stores register values in a protobuf structure.
|
|
|
|
void StoreRegisterValuesInProtobuf(RegisterValues* values) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class StackTracePeer;
|
|
|
|
|
|
|
|
struct PtraceRegisters {
|
2020-09-10 20:47:32 +08:00
|
|
|
#if defined(SAPI_X86_64)
|
2019-03-19 00:21:48 +08:00
|
|
|
uint64_t r15;
|
|
|
|
uint64_t r14;
|
|
|
|
uint64_t r13;
|
|
|
|
uint64_t r12;
|
|
|
|
uint64_t rbp;
|
|
|
|
uint64_t rbx;
|
|
|
|
uint64_t r11;
|
|
|
|
uint64_t r10;
|
|
|
|
uint64_t r9;
|
|
|
|
uint64_t r8;
|
|
|
|
uint64_t rax;
|
|
|
|
uint64_t rcx;
|
|
|
|
uint64_t rdx;
|
|
|
|
uint64_t rsi;
|
|
|
|
uint64_t rdi;
|
|
|
|
uint64_t orig_rax;
|
|
|
|
uint64_t rip;
|
|
|
|
uint64_t cs;
|
|
|
|
uint64_t eflags;
|
|
|
|
uint64_t rsp;
|
|
|
|
uint64_t ss;
|
|
|
|
uint64_t fs_base;
|
|
|
|
uint64_t gs_base;
|
|
|
|
uint64_t ds;
|
|
|
|
uint64_t es;
|
|
|
|
uint64_t fs;
|
|
|
|
uint64_t gs;
|
2020-09-10 20:47:32 +08:00
|
|
|
#elif defined(SAPI_PPC64_LE)
|
2019-03-19 00:21:48 +08:00
|
|
|
uint64_t gpr[32];
|
|
|
|
uint64_t nip;
|
|
|
|
uint64_t msr;
|
|
|
|
uint64_t orig_gpr3;
|
|
|
|
uint64_t ctr;
|
|
|
|
uint64_t link;
|
|
|
|
uint64_t xer;
|
|
|
|
uint64_t ccr;
|
|
|
|
uint64_t softe;
|
|
|
|
uint64_t trap;
|
|
|
|
uint64_t dar;
|
|
|
|
uint64_t dsisr;
|
|
|
|
uint64_t result;
|
|
|
|
// elf.h's ELF_NGREG says it's 48 registers, so kernel fills it in with some
|
|
|
|
// zeroes.
|
|
|
|
uint64_t zero0;
|
|
|
|
uint64_t zero1;
|
|
|
|
uint64_t zero2;
|
|
|
|
uint64_t zero3;
|
2020-09-11 21:33:57 +08:00
|
|
|
#elif defined(SAPI_ARM64)
|
|
|
|
uint64_t regs[31];
|
|
|
|
uint64_t sp;
|
|
|
|
uint64_t pc;
|
|
|
|
uint64_t pstate;
|
2020-12-17 01:17:53 +08:00
|
|
|
#elif defined(SAPI_ARM)
|
|
|
|
uint32_t regs[15];
|
|
|
|
uint32_t pc;
|
|
|
|
uint32_t cpsr;
|
|
|
|
uint32_t orig_x0;
|
2020-09-10 20:47:32 +08:00
|
|
|
#else
|
|
|
|
static_assert(false, "Host CPU architecture not supported, see config.h");
|
2019-03-19 00:21:48 +08:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// PID for which registers are fetched/stored
|
|
|
|
pid_t pid_ = 0;
|
|
|
|
|
|
|
|
// Registers fetched with ptrace(PR_GETREGS/GETREGSET, pid).
|
|
|
|
PtraceRegisters user_regs_ = {};
|
2020-09-11 21:33:57 +08:00
|
|
|
|
|
|
|
// On AArch64, obtaining the syscall number needs a specific call to ptrace()
|
|
|
|
int syscall_number_ = 0;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_REGS_H_
|