| 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 libseccomp_sys::*; |
| 8 | use std::fmt; |
| 9 | |
| 10 | /// Represents the version information of the libseccomp library. |
| 11 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 12 | pub struct ScmpVersion { |
| 13 | /// The major version |
| 14 | pub major: u32, |
| 15 | /// The minor version |
| 16 | pub minor: u32, |
| 17 | /// The micro version |
| 18 | pub micro: u32, |
| 19 | } |
| 20 | |
| 21 | impl ScmpVersion { |
| 22 | /// Gets the version of the currently loaded libseccomp library. |
| 23 | /// |
| 24 | /// This function returns `ScmpVersion` that represents the currently |
| 25 | /// loaded libseccomp version. |
| 26 | /// |
| 27 | /// This function corresponds to |
| 28 | /// [`seccomp_version`](https://man7.org/linux/man-pages/man3/seccomp_version.3.html). |
| 29 | /// |
| 30 | /// # Errors |
| 31 | /// |
| 32 | /// If this function encounters an issue while getting the version, |
| 33 | /// an error will be returned. |
| 34 | pub fn current() -> Result<Self> { |
| 35 | if let Some(version) = unsafe { seccomp_version().as_ref() } { |
| 36 | Ok(Self { |
| 37 | major: version.major, |
| 38 | minor: version.minor, |
| 39 | micro: version.micro, |
| 40 | }) |
| 41 | } else { |
| 42 | Err(SeccompError::with_msg("Could not get libseccomp version")) |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl From<(u32, u32, u32)> for ScmpVersion { |
| 48 | /// Creates a `ScmpVersion` from the specified arbitrary version. |
| 49 | /// |
| 50 | /// # Arguments |
| 51 | /// |
| 52 | /// * `version` - A tuple that represents the version of the libseccomp library. |
| 53 | /// The index 0, 1, and 2 represent `major`, `minor`, and `micro` respectively. |
| 54 | fn from(version: (u32, u32, u32)) -> Self { |
| 55 | Self { |
| 56 | major: version.0, |
| 57 | minor: version.1, |
| 58 | micro: version.2, |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl fmt::Display for ScmpVersion { |
| 64 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 65 | write!(f, "{}.{}.{}", self.major, self.minor, self.micro) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /// Checks that the libseccomp version being used is equal to or greater than |
| 70 | /// the specified version. |
| 71 | /// |
| 72 | /// This function returns `Ok(true)` if the libseccomp version is equal to |
| 73 | /// or greater than the specified version, `Ok(false)` otherwise. |
| 74 | /// |
| 75 | /// # Arguments |
| 76 | /// |
| 77 | /// * `expected` - The libseccomp version you want to check |
| 78 | /// |
| 79 | /// # Errors |
| 80 | /// |
| 81 | /// If an issue is encountered getting the current version, an error will be returned. |
| 82 | /// |
| 83 | /// # Examples |
| 84 | /// |
| 85 | /// ``` |
| 86 | /// # use libseccomp::*; |
| 87 | /// check_version(ScmpVersion::from((2, 4, 0)))?; |
| 88 | /// # Ok::<(), Box<dyn std::error::Error>>(()) |
| 89 | /// ``` |
| 90 | pub fn check_version(expected: ScmpVersion) -> Result<bool> { |
| 91 | let current = ScmpVersion::current()?; |
| 92 | |
| 93 | if current.major > expected.major |
| 94 | || (current.major == expected.major && current.minor > expected.minor) |
| 95 | || (current.major == expected.major |
| 96 | && current.minor == expected.minor |
| 97 | && current.micro >= expected.micro) |
| 98 | { |
| 99 | Ok(true) |
| 100 | } else { |
| 101 | Ok(false) |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /// Ensures that the libseccomp version is equal to or greater than the |
| 106 | /// specified version. |
| 107 | /// |
| 108 | /// # Arguments |
| 109 | /// |
| 110 | /// * `msg` - An arbitrary non-empty operation description, used as a part |
| 111 | /// of the error message returned. |
| 112 | /// * `expected` - The libseccomp version you want to check |
| 113 | /// |
| 114 | /// # Errors |
| 115 | /// |
| 116 | /// If the libseccomp version being used is less than the specified version, |
| 117 | /// an error will be returned. |
| 118 | pub(crate) fn ensure_supported_version(msg: &str, expected: ScmpVersion) -> Result<()> { |
| 119 | if check_version(expected)? { |
| 120 | Ok(()) |
| 121 | } else { |
| 122 | let current = ScmpVersion::current()?; |
| 123 | Err(SeccompError::with_msg(format!( |
| 124 | "{} requires libseccomp >= {} (current version: {})", |
| 125 | msg, expected, current, |
| 126 | ))) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | #[cfg(test)] |
| 131 | mod tests { |
| 132 | use super::*; |
| 133 | |
| 134 | struct ScmpVersionTest { |
| 135 | pub msg: &'static str, |
| 136 | pub ver: ScmpVersion, |
| 137 | pub is_ok: bool, |
| 138 | } |
| 139 | impl ScmpVersionTest { |
| 140 | pub fn new(msg: &'static str, ver: ScmpVersion, is_ok: bool) -> Self { |
| 141 | Self { msg, ver, is_ok } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | #[test] |
| 146 | fn test_ensure_supported_version() { |
| 147 | let ver = ScmpVersion::current().unwrap(); |
| 148 | let tests = &[ |
| 149 | ScmpVersionTest::new( |
| 150 | "VerCurrent", |
| 151 | ScmpVersion::from((ver.major, ver.minor, ver.micro)), |
| 152 | true, |
| 153 | ), |
| 154 | ScmpVersionTest::new("VerMajor-1", ScmpVersion::from((ver.major - 1, 0, 0)), true), |
| 155 | ScmpVersionTest::new( |
| 156 | // To avoid a failure at v*.0.* |
| 157 | "If VerMinor != 0 then VerMinor-1, otherwise VerMinor=0", |
| 158 | ScmpVersion::from((ver.major, if ver.minor != 0 { ver.minor - 1 } else { 0 }, 0)), |
| 159 | true, |
| 160 | ), |
| 161 | ScmpVersionTest::new( |
| 162 | // To avoid a failure at v*.*.0 |
| 163 | "If VerMicro != 0 then VerMicro-1, otherwise VerMicro=0", |
| 164 | ScmpVersion::from(( |
| 165 | ver.major, |
| 166 | ver.minor, |
| 167 | if ver.micro != 0 { ver.micro - 1 } else { 0 }, |
| 168 | )), |
| 169 | true, |
| 170 | ), |
| 171 | ScmpVersionTest::new( |
| 172 | "VerNew", |
| 173 | ScmpVersion::from((ver.major + 1, ver.minor + 1, ver.micro + 1)), |
| 174 | false, |
| 175 | ), |
| 176 | ScmpVersionTest::new( |
| 177 | "VerMajor+1", |
| 178 | ScmpVersion::from((ver.major + 1, 0, 0)), |
| 179 | false, |
| 180 | ), |
| 181 | ScmpVersionTest::new( |
| 182 | "VerMinor+1", |
| 183 | ScmpVersion::from((ver.major, ver.minor + 1, 0)), |
| 184 | false, |
| 185 | ), |
| 186 | ScmpVersionTest::new( |
| 187 | "VerMicro+1", |
| 188 | ScmpVersion::from((ver.major, ver.minor, ver.micro + 1)), |
| 189 | false, |
| 190 | ), |
| 191 | ]; |
| 192 | |
| 193 | for test in tests { |
| 194 | assert_eq!( |
| 195 | ensure_supported_version(test.msg, test.ver).is_ok(), |
| 196 | test.is_ok |
| 197 | ); |
| 198 | } |
| 199 | } |
| 200 | } |