| 1 |
[](https://crates.io/crates/selinux) |
| 2 |
[](https://docs.rs/selinux) |
| 3 |
[](https://raw.githubusercontent.com/koutheir/selinux/master/LICENSE.txt) |
| 4 |
|
| 5 |
# 🛡️ Safe Rust bindings for `libselinux` |
| 6 |
|
| 7 |
SELinux is a flexible Mandatory Access Control for Linux. |
| 8 |
|
| 9 |
This crate supports `libselinux` from version `2.8` to `3.11`. |
| 10 |
Later versions might still be compatible. |
| 11 |
This crate exposes neither *deprecated* nor *undocumented* SELinux API functions |
| 12 |
and types. |
| 13 |
|
| 14 |
⚠️ This crate is specific to Android and some Linux distributions. Building it for |
| 15 |
other platforms, or for the Linux kernel, results in an empty crate. |
| 16 |
|
| 17 |
This documentation is too brief to cover SELinux. |
| 18 |
Please refer to the [official SELinux documentation], the manual pages of |
| 19 |
the [`libselinux`] native library, and the [`selinux-sys`] crate for a more |
| 20 |
complete picture on how to use this crate. |
| 21 |
|
| 22 |
If you cannot find a feature you are looking for by its name, but you know |
| 23 |
which `libselinux` APIs relate to it, then try searching the documentation |
| 24 |
by that API name. |
| 25 |
|
| 26 |
## ⚓ Backward compatibility |
| 27 |
|
| 28 |
This crate requires `libselinux` version `2.8`, at least. |
| 29 |
However, this crate provides some functions that are based on `libselinux` |
| 30 |
functions implemented in later versions. |
| 31 |
When such newer functions are needed, this crate attempts to load them |
| 32 |
dynamically at runtime. |
| 33 |
If such functions are implemented by `libselinux`, then the called crate |
| 34 |
functions run as expected. |
| 35 |
If the needed functions are not implemented by `libselinux`, then an error is |
| 36 |
returned indicating that the called crate function is unsupported. |
| 37 |
|
| 38 |
## 🔢 Versioning |
| 39 |
|
| 40 |
This project adheres to [Semantic Versioning]. |
| 41 |
The `CHANGELOG.md` file details notable changes over time. |
| 42 |
|
| 43 |
## 🛠️ Development |
| 44 |
|
| 45 |
> This section is only relevant for developers contributing to this crate, |
| 46 |
> and not for users of this crate. |
| 47 |
|
| 48 |
💡 If you're developing this crate and feel important information is missing |
| 49 |
in this section, then please create an issue or a pull request to fix that. |
| 50 |
|
| 51 |
### Build system |
| 52 |
|
| 53 |
This crate uses only `cargo` as a build system. Usual commands are used to |
| 54 |
perform most operations, *e.g.*, `build`, `test`, `fmt`. |
| 55 |
|
| 56 |
> Code is read many times more that written, so this crate's code is always |
| 57 |
> formatted using `cargo fmt`. |
| 58 |
|
| 59 |
Operations requiring special handling are crafted as cargo [xtask] targets. |
| 60 |
The full list of these special operations can be determined by running: |
| 61 |
|
| 62 |
```shell |
| 63 |
cargo xtask |
| 64 |
``` |
| 65 |
|
| 66 |
Each special operation can be executed by running: |
| 67 |
|
| 68 |
```shell |
| 69 |
cargo xtask <operation> [parameters...] |
| 70 |
``` |
| 71 |
|
| 72 |
For example, to generate coverage information, run: |
| 73 |
|
| 74 |
```shell |
| 75 |
cargo xtask coverage |
| 76 |
``` |
| 77 |
|
| 78 |
### Testing |
| 79 |
|
| 80 |
This crate can only be tested on a Linux distribution that has SELinux |
| 81 |
supported and enabled at multiple levels: |
| 82 |
|
| 83 |
- The Linux kernel must support SELinux, and have it enabled. |
| 84 |
- The file system must be correctly configured. |
| 85 |
- The user space must have access to SELinux, usually via `libselinux`. |
| 86 |
|
| 87 |
[Red Hat Enterprise Linux]-like distributions (*e.g.*, [Fedora], [CentOS], |
| 88 |
[RockyLinux]) are suitable for testing this crate, either on hardware or inside |
| 89 |
virtual machines, but not in containers. |
| 90 |
|
| 91 |
Given that coverage information requires running tests, that information |
| 92 |
can only be successfully obtained on a system with SELinux enabled. |
| 93 |
|
| 94 |
### Behavior |
| 95 |
|
| 96 |
This crate uses the `libselinux` API as documented in the manual pages. |
| 97 |
It tries to avoid assumptions about implementation details as far as possible, |
| 98 |
even when performance might be improved with such knowledge. |
| 99 |
|
| 100 |
The structures and enumerations defined by this crate assume that their user |
| 101 |
might, at some point, decide to call *raw* `libselinux` APIs (possible using |
| 102 |
the `selinux-sys` crate) for features not yet provided by this crate, or for |
| 103 |
some other reasons. That is the reason why methods such as `as_ptr()` are |
| 104 |
implemented by these structures, exposing the raw values that `libselinux` |
| 105 |
APIs recognize. |
| 106 |
|
| 107 |
### Change log |
| 108 |
|
| 109 |
The [change log] is useful to get a picture of what is going on with the |
| 110 |
crate in the recent past. |
| 111 |
|
| 112 |
[Semantic Versioning]: https://semver.org/spec/v2.0.0.html |
| 113 |
[official SELinux documentation]: https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/using_selinux/index |
| 114 |
[`libselinux`]: https://man7.org/linux/man-pages/man8/selinux.8.html |
| 115 |
[`selinux-sys`]: https://docs.rs/selinux-sys/ |
| 116 |
[xtask]: https://github.com/matklad/cargo-xtask |
| 117 |
[Red Hat Enterprise Linux]: https://www.redhat.com/en/technologies/linux-platforms/enterprise-linux |
| 118 |
[Fedora]: https://getfedora.org/ |
| 119 |
[CentOS]: https://www.centos.org/ |
| 120 |
[RockyLinux]: https://rockylinux.org/ |
| 121 |
[change log]: https://codeberg.org/koutheir/selinux/src/branch/main/CHANGELOG.md |