| 1 | // SPDX-License-Identifier: Apache-2.0 or MIT |
| 2 | // |
| 3 | // Copyright 2021 Sony Group Corporation |
| 4 | // |
| 5 | |
| 6 | use crate::error::{Result, SeccompError}; |
| 7 | use crate::ScmpArch; |
| 8 | use libseccomp_sys::*; |
| 9 | use std::ffi::CStr; |
| 10 | use std::fmt; |
| 11 | use std::os::raw::c_char; |
| 12 | |
| 13 | #[cfg(feature = "const-syscall")] |
| 14 | cfg_if::cfg_if! { |
| 15 | if #[cfg(target_arch = "x86_64")] { |
| 16 | mod x86_64; |
| 17 | use x86_64::SYSCALLS; |
| 18 | } else if #[cfg(target_arch = "aarch64")] { |
| 19 | mod aarch64; |
| 20 | use aarch64::SYSCALLS; |
| 21 | } else if #[cfg(target_arch = "arm")] { |
| 22 | mod arm; |
| 23 | use arm::SYSCALLS; |
| 24 | } else if #[cfg(target_arch = "loongarch64")] { |
| 25 | mod loongarch64; |
| 26 | use loongarch64::SYSCALLS; |
| 27 | } else if #[cfg(target_arch = "m68k")] { |
| 28 | mod m68k; |
| 29 | use m68k::SYSCALLS; |
| 30 | } else if #[cfg(target_arch = "mips")] { |
| 31 | mod mips; |
| 32 | use mips::SYSCALLS; |
| 33 | } else if #[cfg(target_arch = "mips64")] { |
| 34 | mod mips64; |
| 35 | use mips64::SYSCALLS; |
| 36 | } else if #[cfg(target_arch = "powerpc")] { |
| 37 | mod powerpc; |
| 38 | use powerpc::SYSCALLS; |
| 39 | } else if #[cfg(target_arch = "powerpc64")] { |
| 40 | mod powerpc64; |
| 41 | use powerpc64::SYSCALLS; |
| 42 | } else if #[cfg(target_arch = "riscv64")] { |
| 43 | mod riscv64; |
| 44 | use riscv64::SYSCALLS; |
| 45 | } else if #[cfg(target_arch = "s390x")] { |
| 46 | mod s390x; |
| 47 | use s390x::SYSCALLS; |
| 48 | } else if #[cfg(target_arch = "x86")] { |
| 49 | mod x86; |
| 50 | use x86::SYSCALLS; |
| 51 | } else { |
| 52 | compile_error!("Looks like your target_arch is not supported by libseccomp."); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | /// A raw syscall as used by the OS. |
| 57 | pub type RawSyscall = libc::c_int; |
| 58 | |
| 59 | /// Represents a syscall number. |
| 60 | #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 61 | pub struct ScmpSyscall { |
| 62 | nr: i32, |
| 63 | } |
| 64 | impl ScmpSyscall { |
| 65 | pub(crate) fn to_sys(self) -> i32 { |
| 66 | self.nr |
| 67 | } |
| 68 | |
| 69 | pub(crate) fn from_sys(nr: i32) -> Self { |
| 70 | Self { nr } |
| 71 | } |
| 72 | |
| 73 | /// Resolves a syscall name to `ScmpSyscall`. |
| 74 | /// |
| 75 | /// This function returns a `ScmpSyscall` that can be passed to |
| 76 | /// [`add_rule`](crate::ScmpFilterContext::add_rule) like functions. |
| 77 | /// Or `ScmpSyscall::from(libseccomp_sys::__NR_SCMP_ERROR)` if name is unknown. |
| 78 | /// |
| 79 | /// Unlike [`from_name`](Self::from_name) this function does not any FFI call |
| 80 | /// and can therefore be `const`. |
| 81 | /// |
| 82 | /// # Arguments |
| 83 | /// |
| 84 | /// * `name` - The name of a syscall |
| 85 | /// |
| 86 | /// # Examples |
| 87 | /// |
| 88 | /// ``` |
| 89 | /// # use libseccomp::*; |
| 90 | /// let syscall = ScmpSyscall::new("chroot"); |
| 91 | /// ``` |
| 92 | #[cfg(feature = "const-syscall")] |
| 93 | #[cfg_attr(docsrs, doc(cfg(feature = "const-syscall")))] |
| 94 | pub const fn new(name: &str) -> Self { |
| 95 | let mut i = 0; |
| 96 | let nr = loop { |
| 97 | if i >= SYSCALLS.len() { |
| 98 | break libseccomp_sys::__NR_SCMP_ERROR; |
| 99 | } |
| 100 | if strcmp(SYSCALLS[i].0, name) { |
| 101 | break SYSCALLS[i].1; |
| 102 | } |
| 103 | i += 1; |
| 104 | }; |
| 105 | |
| 106 | Self { nr } |
| 107 | } |
| 108 | |
| 109 | /// Resolves a syscall name to `ScmpSyscall`. |
| 110 | /// |
| 111 | /// This function returns a `ScmpSyscall` that can be passed to |
| 112 | /// [`add_rule`](crate::ScmpFilterContext::add_rule) like functions. |
| 113 | /// |
| 114 | /// This function corresponds to |
| 115 | /// [`seccomp_syscall_resolve_name`](https://man7.org/linux/man-pages/man3/seccomp_syscall_resolve_name.3.html). |
| 116 | /// |
| 117 | /// # Arguments |
| 118 | /// |
| 119 | /// * `name` - The name of a syscall |
| 120 | /// |
| 121 | /// # Errors |
| 122 | /// |
| 123 | /// If an invalid string for the syscall name is specified or a syscall with that |
| 124 | /// name is not found, an error will be returned. |
| 125 | /// |
| 126 | /// # Examples |
| 127 | /// |
| 128 | /// ``` |
| 129 | /// # use libseccomp::*; |
| 130 | /// let syscall = ScmpSyscall::from_name("chroot")?; |
| 131 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 132 | /// ``` |
| 133 | pub fn from_name(name: &str) -> Result<Self> { |
| 134 | Self::from_name_by_arch(name, ScmpArch::Native) |
| 135 | } |
| 136 | |
| 137 | /// Resolves a syscall name to `ScmpSyscall`. |
| 138 | /// |
| 139 | /// NOTE: If you call this function with a foreign architecture token and pass the result |
| 140 | /// to [`add_rule*`](crate::ScmpFilterContext::add_rule) functions you get unexpected results. |
| 141 | /// |
| 142 | /// This function returns a `ScmpSyscall` for the specified architecture. |
| 143 | /// |
| 144 | /// This function corresponds to |
| 145 | /// [`seccomp_syscall_resolve_name_arch`](https://man7.org/linux/man-pages/man3/seccomp_syscall_resolve_name_arch.3.html). |
| 146 | /// |
| 147 | /// # Arguments |
| 148 | /// |
| 149 | /// * `name` - The name of a syscall |
| 150 | /// * `arch` - An architecture token |
| 151 | /// |
| 152 | /// # Errors |
| 153 | /// |
| 154 | /// If an invalid string for the syscall name is specified or a syscall with that |
| 155 | /// name is not found, an error will be returned. |
| 156 | /// |
| 157 | /// # Examples |
| 158 | /// |
| 159 | /// ``` |
| 160 | /// # use libseccomp::*; |
| 161 | /// let syscall = ScmpSyscall::from_name_by_arch("chroot", ScmpArch::Aarch64)?; |
| 162 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 163 | /// ``` |
| 164 | pub fn from_name_by_arch(name: &str, arch: ScmpArch) -> Result<Self> { |
| 165 | Self::from_name_by_impl(seccomp_syscall_resolve_name_arch, name, arch) |
| 166 | } |
| 167 | |
| 168 | /// Resolves a syscall name to `ScmpSyscall`. |
| 169 | /// |
| 170 | /// This function returns a `ScmpSyscall` for the specified architecture |
| 171 | /// rewritten if necessary. |
| 172 | /// |
| 173 | /// This function corresponds to |
| 174 | /// [`seccomp_syscall_resolve_name_rewrite`](https://man7.org/linux/man-pages/man3/seccomp_syscall_resolve_name_rewrite.3.html). |
| 175 | /// |
| 176 | /// # Arguments |
| 177 | /// |
| 178 | /// * `name` - The name of a syscall |
| 179 | /// * `arch` - An architecture token |
| 180 | /// |
| 181 | /// # Errors |
| 182 | /// |
| 183 | /// If an invalid string for the syscall name is specified or a syscall with that |
| 184 | /// name is not found, an error will be returned. |
| 185 | /// |
| 186 | /// # Examples |
| 187 | /// |
| 188 | /// ``` |
| 189 | /// # use libseccomp::*; |
| 190 | /// let syscall = ScmpSyscall::from_name_by_arch_rewrite("socketcall", ScmpArch::X32)?; |
| 191 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 192 | /// ``` |
| 193 | pub fn from_name_by_arch_rewrite(name: &str, arch: ScmpArch) -> Result<Self> { |
| 194 | Self::from_name_by_impl(seccomp_syscall_resolve_name_rewrite, name, arch) |
| 195 | } |
| 196 | |
| 197 | fn from_name_by_impl( |
| 198 | resolve_name_func: unsafe extern "C" fn(arch_token: u32, name: *const c_char) -> i32, |
| 199 | name: &str, |
| 200 | arch: ScmpArch, |
| 201 | ) -> Result<Self> { |
| 202 | let mut buf = [0_u8; 64]; |
| 203 | if name.as_bytes().contains(&b'\0') { |
| 204 | return Err(SeccompError::with_msg(format!( |
| 205 | "Bad syscall name '{name}': Contains nul-byte.", |
| 206 | ))); |
| 207 | } |
| 208 | if buf.len() <= name.len() { |
| 209 | return Err(SeccompError::with_msg(format!( |
| 210 | "Bad syscall name '{name}': Too long.", |
| 211 | ))); |
| 212 | } |
| 213 | buf[..name.len()].copy_from_slice(name.as_bytes()); |
| 214 | debug_assert_eq!(buf[name.len()], b'\0'); |
| 215 | |
| 216 | let nr = unsafe { resolve_name_func(arch.to_sys(), buf.as_slice().as_ptr().cast()) }; |
| 217 | if nr == __NR_SCMP_ERROR { |
| 218 | return Err(SeccompError::with_msg(format!( |
| 219 | "Could not resolve syscall name {}", |
| 220 | name |
| 221 | ))); |
| 222 | } |
| 223 | |
| 224 | Ok(Self { nr }) |
| 225 | } |
| 226 | |
| 227 | /// Resolves this `ScmpSyscall` to it's name for the native architecture. |
| 228 | /// |
| 229 | /// This function returns a string containing the name of the syscall. |
| 230 | /// |
| 231 | /// This function corresponds to |
| 232 | /// [`seccomp_syscall_resolve_num_arch`](https://man7.org/linux/man-pages/man3/seccomp_syscall_resolve_num_arch.3.html). |
| 233 | /// |
| 234 | /// # Errors |
| 235 | /// |
| 236 | /// If the syscall is unrecognized or an issue is encountered getting the |
| 237 | /// name of the syscall, an error will be returned. |
| 238 | /// |
| 239 | /// # Examples |
| 240 | /// |
| 241 | /// ``` |
| 242 | /// # use libseccomp::*; |
| 243 | /// assert_eq!( |
| 244 | /// ScmpSyscall::from_name("mount")?.get_name()?, |
| 245 | /// String::from("mount") |
| 246 | /// ); |
| 247 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 248 | /// ``` |
| 249 | pub fn get_name(self) -> Result<String> { |
| 250 | Self::get_name_by_arch(self, ScmpArch::Native) |
| 251 | } |
| 252 | |
| 253 | /// Resolves this `ScmpSyscall` to it's name for a given architecture. |
| 254 | /// |
| 255 | /// This function returns a string containing the name of the syscall. |
| 256 | /// |
| 257 | /// This function corresponds to |
| 258 | /// [`seccomp_syscall_resolve_num_arch`](https://man7.org/linux/man-pages/man3/seccomp_syscall_resolve_num_arch.3.html). |
| 259 | /// |
| 260 | /// # Arguments |
| 261 | /// |
| 262 | /// * `arch` - A valid architecture token |
| 263 | /// |
| 264 | /// # Errors |
| 265 | /// |
| 266 | /// If the syscall is unrecognized or an issue is encountered getting the |
| 267 | /// name of the syscall, an error will be returned. |
| 268 | /// |
| 269 | /// # Examples |
| 270 | /// |
| 271 | /// ``` |
| 272 | /// # use libseccomp::*; |
| 273 | /// assert_eq!( |
| 274 | /// ScmpSyscall::from_name_by_arch("mount", ScmpArch::Mips)? |
| 275 | /// .get_name_by_arch(ScmpArch::Mips)?, |
| 276 | /// String::from("mount"), |
| 277 | /// ); |
| 278 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 279 | /// ``` |
| 280 | pub fn get_name_by_arch(self, arch: ScmpArch) -> Result<String> { |
| 281 | let ret = unsafe { seccomp_syscall_resolve_num_arch(arch.to_sys(), self.to_sys()) }; |
| 282 | if ret.is_null() { |
| 283 | return Err(SeccompError::with_msg(format!( |
| 284 | "Could not resolve syscall number {}", |
| 285 | self.nr |
| 286 | ))); |
| 287 | } |
| 288 | |
| 289 | let name = unsafe { CStr::from_ptr(ret) }.to_str()?.to_string(); |
| 290 | unsafe { libc::free(ret as *mut libc::c_void) }; |
| 291 | |
| 292 | Ok(name) |
| 293 | } |
| 294 | |
| 295 | /// Returns `true` if this syscall is `__NR_SCMP_ERROR` (`-1`), otherwise `false`. |
| 296 | pub fn is_error(&self) -> bool { |
| 297 | self.nr == libseccomp_sys::__NR_SCMP_ERROR |
| 298 | } |
| 299 | |
| 300 | /// Returns `true` if this syscall is `__NR_SCMP_UNDEF` (`-2`), otherwise `false`. |
| 301 | pub fn is_undef(&self) -> bool { |
| 302 | self.nr == libseccomp_sys::__NR_SCMP_UNDEF |
| 303 | } |
| 304 | |
| 305 | /// Creates a `ScmpSyscall` from a `RawSyscall`. |
| 306 | pub fn from_raw_syscall(raw_syscall: RawSyscall) -> Self { |
| 307 | Self::from(raw_syscall) |
| 308 | } |
| 309 | |
| 310 | /// Returns the `RawSyscall` of this `ScmpSyscall`. |
| 311 | pub fn as_raw_syscall(self) -> RawSyscall { |
| 312 | self.nr |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | impl From<RawSyscall> for ScmpSyscall { |
| 317 | /// Creates a `ScmpSyscall` from the specified syscall number. |
| 318 | /// |
| 319 | /// # Arguments |
| 320 | /// |
| 321 | /// * `nr` - The number of syscall |
| 322 | fn from(nr: RawSyscall) -> Self { |
| 323 | Self::from_sys(nr) |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | impl From<ScmpSyscall> for RawSyscall { |
| 328 | /// Gets the syscall number of a syscall. |
| 329 | /// |
| 330 | /// # Arguments |
| 331 | /// |
| 332 | /// * `syscall` - The syscall |
| 333 | fn from(syscall: ScmpSyscall) -> RawSyscall { |
| 334 | syscall.nr |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | impl PartialEq<RawSyscall> for ScmpSyscall { |
| 339 | fn eq(&self, other: &RawSyscall) -> bool { |
| 340 | self.nr == *other |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | impl PartialEq<ScmpSyscall> for RawSyscall { |
| 345 | fn eq(&self, other: &ScmpSyscall) -> bool { |
| 346 | *self == other.nr |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | impl fmt::Display for ScmpSyscall { |
| 351 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 352 | write!(f, "{}", self.nr) |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | /// Compare two strings |
| 357 | /// |
| 358 | /// This is a helper function because `&str == &str` is not `const` yet. |
| 359 | /// |
| 360 | /// This function returns the same as `lhs == rhs`. |
| 361 | #[cfg(feature = "const-syscall")] |
| 362 | const fn strcmp(lhs: &str, rhs: &str) -> bool { |
| 363 | if lhs.len() != rhs.len() { |
| 364 | return false; |
| 365 | } |
| 366 | let (lhs, rhs) = (lhs.as_bytes(), rhs.as_bytes()); |
| 367 | let mut i = 0; |
| 368 | while i < lhs.len() && i < rhs.len() { |
| 369 | if lhs[i] != rhs[i] { |
| 370 | return false; |
| 371 | } |
| 372 | i += 1; |
| 373 | } |
| 374 | true |
| 375 | } |