| 1 | # libseccomp-rs |
| 2 | |
| 3 | [](https://github.com/libseccomp-rs/libseccomp-rs/actions/workflows/build-test.yaml) |
| 4 | [](https://crates.io/crates/libseccomp) |
| 5 | [](https://docs.rs/libseccomp) |
| 6 | [](https://codecov.io/gh/libseccomp-rs/libseccomp-rs) |
| 7 | [](https://blog.rust-lang.org/2023/01/26/Rust-1.67.0.html) |
| 8 | |
| 9 | Rust Language Bindings for the libseccomp Library |
| 10 | |
| 11 | The libseccomp library provides an easy to use, platform independent, interface to |
| 12 | the Linux Kernel's syscall filtering mechanism. The libseccomp API is designed to |
| 13 | abstract away the underlying BPF based syscall filter language and present a more |
| 14 | conventional function-call based filtering interface that should be familiar to, and |
| 15 | easily adopted by, application developers. |
| 16 | |
| 17 | The libseccomp-rs provides a Rust based interface to the libseccomp library. |
| 18 | This repository contains libseccomp and libseccomp-sys crates that enable developers |
| 19 | to use the libseccomp API in Rust. |
| 20 | |
| 21 | * **libseccomp**: High-level safe API |
| 22 | * **libseccomp-sys**: Low-level unsafe API |
| 23 | |
| 24 | [CHANGELOG](https://github.com/libseccomp-rs/libseccomp-rs/blob/main/CHANGELOG.md) |
| 25 | |
| 26 | ## Example |
| 27 | |
| 28 | Create and load a single seccomp rule: |
| 29 | |
| 30 | ```rust |
| 31 | use libseccomp::*; |
| 32 | |
| 33 | fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 34 | // Creates and returns a new filter context. |
| 35 | let mut filter = ScmpFilterContext::new(ScmpAction::Allow)?; |
| 36 | |
| 37 | // Adds an architecture to the filter. |
| 38 | filter.add_arch(ScmpArch::X8664)?; |
| 39 | |
| 40 | // Returns the number of a syscall by name. |
| 41 | let syscall = ScmpSyscall::from_name("dup3")?; |
| 42 | |
| 43 | // Adds a single rule for an unconditional action on the syscall. |
| 44 | filter.add_rule(ScmpAction::Errno(10), syscall)?; |
| 45 | |
| 46 | // Loads the filter context into the kernel. |
| 47 | filter.load()?; |
| 48 | |
| 49 | // The syscall fails by the seccomp rule. |
| 50 | assert_eq!(unsafe { libc::dup3(0, 100, libc::O_CLOEXEC) }, -libc::EPERM); |
| 51 | // The errno is the number specified by the seccomp action. |
| 52 | assert_eq!(std::io::Error::last_os_error().raw_os_error().unwrap(), 10); |
| 53 | |
| 54 | Ok(()) |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ## Requirements |
| 59 | Before using the libseccomp crate, you need to install the libseccomp library for your system. |
| 60 | The libseccomp library version 2.5.0 or newer is required. |
| 61 | |
| 62 | ### Installing the libseccomp library from a package |
| 63 | |
| 64 | e.g. Debian-based Linux |
| 65 | |
| 66 | ``` sh |
| 67 | $ sudo apt install libseccomp-dev |
| 68 | ``` |
| 69 | |
| 70 | ### Building and installing the libseccomp library from sources |
| 71 | If you want to build the libseccomp library from an official release tarball instead of the package, |
| 72 | you should follow the quick step. |
| 73 | |
| 74 | ```sh |
| 75 | $ LIBSECCOMP_VERSION=2.6.0 |
| 76 | $ wget https://github.com/seccomp/libseccomp/releases/download/v${LIBSECCOMP_VERSION}/libseccomp-${LIBSECCOMP_VERSION}.tar.gz |
| 77 | $ tar xvf libseccomp-${LIBSECCOMP_VERSION}.tar.gz |
| 78 | $ cd libseccomp-${LIBSECCOMP_VERSION} |
| 79 | $ ./configure |
| 80 | $ make |
| 81 | $ sudo make install |
| 82 | ``` |
| 83 | |
| 84 | For more details, see the [libseccomp library repository](https://github.com/seccomp/libseccomp). |
| 85 | |
| 86 | ## Setup |
| 87 | If you use the libseccomp crate with dynamically linked the libseccomp library, |
| 88 | you do not need additional settings. |
| 89 | |
| 90 | However, if you want to use the libseccomp crate against musl-libc with statically linked the libseccomp library, |
| 91 | you have to set the `LIBSECCOMP_LINK_TYPE` and `LIBSECCOMP_LIB_PATH` environment variables as follows. |
| 92 | |
| 93 | ```sh |
| 94 | $ export LIBSECCOMP_LINK_TYPE=static |
| 95 | $ export LIBSECCOMP_LIB_PATH="the path of the directory containing libseccomp.a (e.g. /usr/lib)" |
| 96 | ``` |
| 97 | |
| 98 | > **Note**: |
| 99 | > To build the libseccomp crate against musl-libc, you need to build the libseccomp library manually for musl-libc |
| 100 | > or use a musl-based distribution that provides a package for the statically-linked libseccomp library |
| 101 | |
| 102 | Now, add the following to your `Cargo.toml` to start building the libseccomp crate. |
| 103 | |
| 104 | ```toml |
| 105 | [dependencies] |
| 106 | libseccomp = "0.4.0" |
| 107 | ``` |
| 108 | |
| 109 | ## Minimum Supported Rust Version (MSRV) policy |
| 110 | |
| 111 | The 0.3.x line has 1.46 as MSRV. |
| 112 | |
| 113 | Starting with 0.4.0, our MSRV is increased when necessary or appropriate. |
| 114 | However, it will never be increased to a version greater than the last stable rust version minus two. |
| 115 | |
| 116 | A MSRV change is not considered a breaking change. |
| 117 | |
| 118 | ## Testing the crate |
| 119 | The libseccomp crate provides a number of unit tests. |
| 120 | If you want to run the standard regression tests, you can execute the following command. |
| 121 | |
| 122 | ``` sh |
| 123 | $ make test |
| 124 | ``` |
| 125 | |
| 126 | ## How to contribute |
| 127 | Anyone is welcome to join and contribute code, documentation, and use cases. |
| 128 | |
| 129 | For details on how to contribute to the libseccomp-rs project, please see the |
| 130 | [contributing document](https://github.com/libseccomp-rs/libseccomp-rs/blob/main/CONTRIBUTING.md). |
| 131 | |
| 132 | ## License |
| 133 | This crate is licensed under: |
| 134 | |
| 135 | - MIT License (see LICENSE-MIT); or |
| 136 | - Apache 2.0 License (see LICENSE-APACHE), |
| 137 | |
| 138 | at your option. |
| 139 | |
| 140 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 141 | for inclusion in libseccomp-rs by you, as defined in the Apache-2.0 license, |
| 142 | shall be dual licensed as above, without any additional terms or conditions. |