| 1 | // SPDX-License-Identifier: Apache-2.0 or MIT |
| 2 | // |
| 3 | // Copyright 2021 Sony Group Corporation |
| 4 | // |
| 5 | |
| 6 | //! Rust Language Bindings for the libseccomp Library |
| 7 | //! |
| 8 | //! The libseccomp library provides an easy to use, platform independent, interface to |
| 9 | //! the Linux Kernel's syscall filtering mechanism. The libseccomp API is designed to |
| 10 | //! abstract away the underlying BPF based syscall filter language and present a more |
| 11 | //! conventional function-call based filtering interface that should be familiar to, and |
| 12 | //! easily adopted by, application developers. |
| 13 | //! |
| 14 | //! The libseccomp crate is a high-level safe API for the libseccomp library. |
| 15 | //! |
| 16 | //! # Examples |
| 17 | //! |
| 18 | //! ```rust |
| 19 | //! use libseccomp::*; |
| 20 | //! |
| 21 | //! fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 22 | //! let mut filter = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 23 | //! let syscall = ScmpSyscall::from_name("getuid")?; |
| 24 | //! |
| 25 | //! filter.add_arch(ScmpArch::X8664)?; |
| 26 | //! filter.add_rule(ScmpAction::Errno(1), syscall)?; |
| 27 | //! filter.set_ctl_log(true)?; |
| 28 | //! filter.set_syscall_priority(syscall, 100)?; |
| 29 | //! filter.load()?; |
| 30 | //! |
| 31 | //! Ok(()) |
| 32 | //! } |
| 33 | //! ``` |
| 34 | //! |
| 35 | //! The above example can be replaced with builder pattern. |
| 36 | //! |
| 37 | //! ```rust |
| 38 | //! use libseccomp::*; |
| 39 | //! |
| 40 | //! fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 41 | //! let syscall = ScmpSyscall::from_name("getuid")?; |
| 42 | //! |
| 43 | //! ScmpFilterContext::new(ScmpAction::Allow)? |
| 44 | //! .add_arch(ScmpArch::X8664)? |
| 45 | //! .add_rule(ScmpAction::Errno(1), syscall)? |
| 46 | //! .set_ctl_log(true)? |
| 47 | //! .set_syscall_priority(syscall, 100)? |
| 48 | //! .load()?; |
| 49 | //! |
| 50 | //! Ok(()) |
| 51 | //! } |
| 52 | //! ``` |
| 53 | //! |
| 54 | //! # Features |
| 55 | //! |
| 56 | //! - `const-syscall`: Allow creating of `ScmpSyscall` in a `const`-context. |
| 57 | |
| 58 | #![warn(rust_2018_idioms)] |
| 59 | #![deny(missing_debug_implementations)] |
| 60 | #![deny(missing_docs)] |
| 61 | #![deny(unsafe_op_in_unsafe_fn)] |
| 62 | #![warn(clippy::inefficient_to_string)] |
| 63 | #![warn(clippy::string_to_string)] |
| 64 | #![warn(clippy::semicolon_if_nothing_returned)] |
| 65 | #![warn(clippy::clone_on_ref_ptr)] |
| 66 | #![warn(clippy::unwrap_in_result)] |
| 67 | #![cfg_attr(docsrs, feature(doc_cfg))] |
| 68 | |
| 69 | /// Errors |
| 70 | pub mod error; |
| 71 | |
| 72 | mod action; |
| 73 | mod api; |
| 74 | mod arch; |
| 75 | mod arg_compare; |
| 76 | mod compare_op; |
| 77 | mod filter_attr; |
| 78 | mod filter_context; |
| 79 | mod functions; |
| 80 | mod notify; |
| 81 | mod syscall; |
| 82 | mod version; |
| 83 | |
| 84 | use error::{Result, SeccompError}; |
| 85 | |
| 86 | pub use action::ScmpAction; |
| 87 | pub use api::{check_api, get_api, set_api}; |
| 88 | pub use arch::ScmpArch; |
| 89 | pub use arg_compare::ScmpArgCompare; |
| 90 | pub use compare_op::ScmpCompareOp; |
| 91 | pub use filter_attr::ScmpFilterAttr; |
| 92 | pub use filter_context::ScmpFilterContext; |
| 93 | pub use functions::*; |
| 94 | pub use notify::*; |
| 95 | pub use syscall::{RawSyscall, ScmpSyscall}; |
| 96 | pub use version::{check_version, ScmpVersion}; |
| 97 | |
| 98 | fn cvt(ret: i32) -> Result<()> { |
| 99 | if ret == 0 { |
| 100 | Ok(()) |
| 101 | } else { |
| 102 | Err(SeccompError::from_errno(ret)) |
| 103 | } |
| 104 | } |