xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/syscall.cc (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 #include "sandboxed_api/sandbox2/syscall.h"
16 
17 #include <linux/audit.h>
18 
19 #include <cstdint>
20 #include <string>
21 #include <vector>
22 
23 #include "absl/strings/str_format.h"
24 #include "absl/strings/str_join.h"
25 #include "absl/strings/string_view.h"
26 #include "sandboxed_api/config.h"
27 #include "sandboxed_api/sandbox2/syscall_defs.h"
28 
29 #ifndef AUDIT_ARCH_PPC64LE
30 #define AUDIT_ARCH_PPC64LE (EM_PPC64 | __AUDIT_ARCH_64BIT | __AUDIT_ARCH_LE)
31 #endif
32 
33 namespace sandbox2 {
34 
GetArchDescription(sapi::cpu::Architecture arch)35 std::string Syscall::GetArchDescription(sapi::cpu::Architecture arch) {
36   switch (arch) {
37     case sapi::cpu::kX8664:
38       return "[X86-64]";
39     case sapi::cpu::kX86:
40       return "[X86-32]";
41     case sapi::cpu::kPPC64LE:
42       return "[PPC-64]";
43     case sapi::cpu::kArm64:
44       return "[Arm-64]";
45     case sapi::cpu::kArm:
46       return "[Arm-32]";
47     default:
48       return absl::StrFormat("[UNKNOWN_ARCH:%d]", arch);
49   }
50 }
51 
GetHostAuditArch()52 uint32_t Syscall::GetHostAuditArch() {
53   switch (sapi::host_cpu::Architecture()) {
54     case sapi::cpu::kX8664:
55       return AUDIT_ARCH_X86_64;
56     case sapi::cpu::kPPC64LE:
57       return AUDIT_ARCH_PPC64LE;
58     case sapi::cpu::kArm64:
59       return AUDIT_ARCH_AARCH64;
60     case sapi::cpu::kArm:
61       return AUDIT_ARCH_ARM;
62     default:
63       // The static_assert() in config.h should prevent us from ever getting
64       // here.
65       return 0;  // Not reached
66   }
67 }
68 
GetName() const69 std::string Syscall::GetName() const {
70   if (absl::string_view name = SyscallTable::get(arch_).GetName(nr_);
71       !name.empty()) {
72     return std::string(name);
73   }
74   return absl::StrFormat("UNKNOWN[%d/0x%x]", nr_, nr_);
75 }
76 
GetArgumentsDescription() const77 std::vector<std::string> Syscall::GetArgumentsDescription() const {
78   return SyscallTable::get(arch_).GetArgumentsDescription(nr_, args_.data(),
79                                                           pid_);
80 }
81 
GetDescription() const82 std::string Syscall::GetDescription() const {
83   const std::string arch = GetArchDescription(arch_);
84   const std::string args = absl::StrJoin(GetArgumentsDescription(), ", ");
85   return absl::StrFormat("%s %s [%d](%s) IP: %#x, STACK: %#x", arch, GetName(),
86                          nr_, args, ip_, sp_);
87 }
88 
89 }  // namespace sandbox2
90