xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/syscall.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // The sandbox2::Syscalls class defines mostly static helper methods which
16 // are used to analyze the status of the sandboxed process.
17 
18 #ifndef SANDBOXED_API_SANDBOX2_SYSCALL_H__
19 #define SANDBOXED_API_SANDBOX2_SYSCALL_H__
20 
21 #include <sys/types.h>
22 
23 #include <array>
24 #include <cstddef>
25 #include <cstdint>
26 #include <string>
27 #include <vector>
28 
29 #include "sandboxed_api/config.h"  // IWYU pragma: export
30 
31 namespace sandbox2 {
32 
33 class Syscall {
34  public:
35   // Maximum number of syscall arguments
36   static constexpr size_t kMaxArgs = 6;
37   using Args = std::array<uint64_t, kMaxArgs>;
38 
39   // Returns the host architecture, according to CpuArch.
GetHostArch()40   static constexpr sapi::cpu::Architecture GetHostArch() {
41     return sapi::host_cpu::Architecture();
42   }
43 
44   // Returns the host architecture, according to <linux/audit.h>.
45   static uint32_t GetHostAuditArch();
46 
47   // Returns a description of the architecture.
48   static std::string GetArchDescription(sapi::cpu::Architecture arch);
49 
50   Syscall() = default;
51   Syscall(sapi::cpu::Architecture arch, uint64_t nr, Args args = {})
arch_(arch)52       : arch_(arch), nr_(nr), args_(args) {}
53 
pid()54   pid_t pid() const { return pid_; }
nr()55   uint64_t nr() const { return nr_; }
arch()56   sapi::cpu::Architecture arch() const { return arch_; }
args()57   const Args& args() const { return args_; }
stack_pointer()58   uint64_t stack_pointer() const { return sp_; }
instruction_pointer()59   uint64_t instruction_pointer() const { return ip_; }
60 
61   std::string GetName() const;
62 
63   std::vector<std::string> GetArgumentsDescription() const;
64   std::string GetDescription() const;
65 
66  private:
67   friend class Regs;
68   friend class UnotifyMonitor;
69 
Syscall(pid_t pid)70   explicit Syscall(pid_t pid) : pid_(pid) {}
Syscall(sapi::cpu::Architecture arch,uint64_t nr,Args args,pid_t pid,uint64_t sp,uint64_t ip)71   Syscall(sapi::cpu::Architecture arch, uint64_t nr, Args args, pid_t pid,
72           uint64_t sp, uint64_t ip)
73       : arch_(arch), nr_(nr), args_(args), pid_(pid), sp_(sp), ip_(ip) {}
74 
75   sapi::cpu::Architecture arch_ = sapi::cpu::kUnknown;
76   uint64_t nr_ = -1;
77   Args args_ = {};
78   pid_t pid_ = -1;
79   uint64_t sp_ = 0;
80   uint64_t ip_ = 0;
81 };
82 
83 }  // namespace sandbox2
84 
85 #endif  // SANDBOXED_API_SANDBOX2_SYSCALL_H__
86