| 1 | // SPDX-License-Identifier: Apache-2.0 or MIT |
| 2 | // |
| 3 | // Copyright 2021 Sony Group Corporation |
| 4 | // |
| 5 | |
| 6 | use crate::error::ErrorKind::*; |
| 7 | use crate::error::{Result, SeccompError}; |
| 8 | use libseccomp_sys::*; |
| 9 | use std::str::FromStr; |
| 10 | |
| 11 | /// Represents a comparison operator which can be used in a filter rule. |
| 12 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 13 | #[non_exhaustive] |
| 14 | pub enum ScmpCompareOp { |
| 15 | /// Not equal |
| 16 | NotEqual, |
| 17 | /// Less than |
| 18 | Less, |
| 19 | /// Less than or equal |
| 20 | LessOrEqual, |
| 21 | /// Equal |
| 22 | Equal, |
| 23 | /// Greater than or equal |
| 24 | GreaterEqual, |
| 25 | /// Greater than |
| 26 | Greater, |
| 27 | /// Masked equality |
| 28 | /// |
| 29 | /// This works like `Equal` with the exception that the syscall argument is |
| 30 | /// masked with `mask` via an bitwise AND (i.e. you can check specific bits in the |
| 31 | /// argument). |
| 32 | MaskedEqual(#[doc = "mask"] u64), |
| 33 | } |
| 34 | |
| 35 | impl ScmpCompareOp { |
| 36 | pub(crate) const fn to_sys(self) -> scmp_compare { |
| 37 | match self { |
| 38 | Self::NotEqual => scmp_compare::SCMP_CMP_NE, |
| 39 | Self::Less => scmp_compare::SCMP_CMP_LT, |
| 40 | Self::LessOrEqual => scmp_compare::SCMP_CMP_LE, |
| 41 | Self::Equal => scmp_compare::SCMP_CMP_EQ, |
| 42 | Self::GreaterEqual => scmp_compare::SCMP_CMP_GE, |
| 43 | Self::Greater => scmp_compare::SCMP_CMP_GT, |
| 44 | Self::MaskedEqual(_) => scmp_compare::SCMP_CMP_MASKED_EQ, |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl FromStr for ScmpCompareOp { |
| 50 | type Err = SeccompError; |
| 51 | |
| 52 | /// Converts string seccomp comparison operator to `ScmpCompareOp`. |
| 53 | /// |
| 54 | /// # Arguments |
| 55 | /// |
| 56 | /// * `cmp_op` - A string comparison operator, e.g. `SCMP_CMP_*`. |
| 57 | /// |
| 58 | /// See the [`seccomp_rule_add(3)`] man page for details on valid comparison operator values. |
| 59 | /// |
| 60 | /// [`seccomp_rule_add(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_rule_add.3.html |
| 61 | /// |
| 62 | /// # Errors |
| 63 | /// |
| 64 | /// If an invalid comparison operator is specified, an error will be returned. |
| 65 | fn from_str(cmp_op: &str) -> Result<Self> { |
| 66 | match cmp_op { |
| 67 | "SCMP_CMP_NE" => Ok(Self::NotEqual), |
| 68 | "SCMP_CMP_LT" => Ok(Self::Less), |
| 69 | "SCMP_CMP_LE" => Ok(Self::LessOrEqual), |
| 70 | "SCMP_CMP_EQ" => Ok(Self::Equal), |
| 71 | "SCMP_CMP_GE" => Ok(Self::GreaterEqual), |
| 72 | "SCMP_CMP_GT" => Ok(Self::Greater), |
| 73 | "SCMP_CMP_MASKED_EQ" => Ok(Self::MaskedEqual(u64::default())), |
| 74 | _ => Err(SeccompError::new(FromStr(cmp_op.to_string()))), |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | #[cfg(test)] |
| 80 | mod tests { |
| 81 | use super::*; |
| 82 | |
| 83 | #[test] |
| 84 | fn test_parse_compare_op() { |
| 85 | let test_data = &[ |
| 86 | ("SCMP_CMP_NE", ScmpCompareOp::NotEqual), |
| 87 | ("SCMP_CMP_LT", ScmpCompareOp::Less), |
| 88 | ("SCMP_CMP_LE", ScmpCompareOp::LessOrEqual), |
| 89 | ("SCMP_CMP_EQ", ScmpCompareOp::Equal), |
| 90 | ("SCMP_CMP_GE", ScmpCompareOp::GreaterEqual), |
| 91 | ("SCMP_CMP_GT", ScmpCompareOp::Greater), |
| 92 | ( |
| 93 | "SCMP_CMP_MASKED_EQ", |
| 94 | ScmpCompareOp::MaskedEqual(u64::default()), |
| 95 | ), |
| 96 | ]; |
| 97 | |
| 98 | for data in test_data { |
| 99 | assert_eq!( |
| 100 | ScmpCompareOp::from_str(data.0).unwrap().to_sys(), |
| 101 | data.1.to_sys() |
| 102 | ); |
| 103 | } |
| 104 | assert!(ScmpCompareOp::from_str("SCMP_INVALID_FLAG").is_err()); |
| 105 | } |
| 106 | } |