• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "sandboxed_api/sandbox2/syscall_defs.h"
31 
32 namespace sandbox2 {
33 
34 class Syscall {
35  public:
36   // Maximum number of syscall arguments
37   static constexpr size_t kMaxArgs = 6;
38   using Args = std::array<uint64_t, kMaxArgs>;
39 
40   // Returns the host architecture, according to CpuArch.
GetHostArch()41   static constexpr sapi::cpu::Architecture GetHostArch() {
42     return sapi::host_cpu::Architecture();
43   }
44 
45   // Returns the host architecture, according to <linux/audit.h>.
46   static uint32_t GetHostAuditArch();
47 
48   // Returns a description of the architecture.
49   static std::string GetArchDescription(sapi::cpu::Architecture arch);
50 
51   Syscall() = default;
52   Syscall(sapi::cpu::Architecture arch, uint64_t nr, Args args = {})
arch_(arch)53       : arch_(arch), nr_(nr), args_(args) {}
54 
pid()55   pid_t pid() const { return pid_; }
nr()56   uint64_t nr() const { return nr_; }
arch()57   sapi::cpu::Architecture arch() const { return arch_; }
args()58   const Args& args() const { return args_; }
stack_pointer()59   uint64_t stack_pointer() const { return sp_; }
instruction_pointer()60   uint64_t instruction_pointer() const { return ip_; }
61 
62   std::string GetName() const;
63   std::vector<syscalls::ArgData> GetArgumentsData() const;
64   std::vector<std::string> GetArgumentsDescription() const;
65   std::string GetDescription() const;
66 
67  private:
68   friend class Regs;
69   friend class UnotifyMonitor;
70 
Syscall(pid_t pid)71   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)72   Syscall(sapi::cpu::Architecture arch, uint64_t nr, Args args, pid_t pid,
73           uint64_t sp, uint64_t ip)
74       : arch_(arch), nr_(nr), args_(args), pid_(pid), sp_(sp), ip_(ip) {}
75 
76   sapi::cpu::Architecture arch_ = sapi::cpu::kUnknown;
77   uint64_t nr_ = -1;
78   Args args_ = {};
79   pid_t pid_ = -1;
80   uint64_t sp_ = 0;
81   uint64_t ip_ = 0;
82 };
83 
84 }  // namespace sandbox2
85 
86 #endif  // SANDBOXED_API_SANDBOX2_SYSCALL_H__
87