Debian Codegraph @

src/filter_attr.rs

raw symbols SCIP
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 filter attributes.
12 ///
13 /// You can set/get the attributes of a filter context with
14 /// [`ScmpFilterContext::set_filter_attr`](crate::ScmpFilterContext::set_filter_attr)
15 /// and [`ScmpFilterContext::get_filter_attr`](crate::ScmpFilterContext::get_filter_attr)
16 /// methods.
17 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
18 #[non_exhaustive]
19 pub enum ScmpFilterAttr {
20 /// The default filter action as specified in the call to seccomp reset.
21 ActDefault,
22 /// The filter action taken when the loaded filter does not
23 /// match the architecture of the executing application.
24 ActBadArch,
25 /// A flag to specify if the NO_NEW_PRIVS functionality should
26 /// be enabled before loading the seccomp filter into the kernel.
27 CtlNnp,
28 /// A flag to specify if the kernel should attempt to
29 /// synchronize the filters across all threads on seccomp load.
30 CtlTsync,
31 /// A flag to specify if the libseccomp should allow filter rules
32 /// to be created for the -1 syscall.
33 ApiTskip,
34 /// A flag to specify if the kernel should log all filter
35 /// actions taken except for the [`ScmpAction::Allow`](crate::ScmpAction::Allow) action.
36 CtlLog,
37 /// A flag to disable Speculative Store Bypass mitigations for
38 /// this filter.
39 CtlSsb,
40 /// A flag to specify the optimization level of the seccomp
41 /// filter.
42 CtlOptimize,
43 /// A flag to specify if the libseccomp should pass system error
44 /// codes back to the caller instead of the default -ECANCELED.
45 ApiSysRawRc,
46 /// A flag to specify if libseccomp should request wait killable
47 /// semantics when possible.
48 CtlWaitkill,
49 }
50
51 impl ScmpFilterAttr {
52 pub(crate) fn to_sys(self) -> scmp_filter_attr {
53 match self {
54 Self::ActDefault => scmp_filter_attr::SCMP_FLTATR_ACT_DEFAULT,
55 Self::ActBadArch => scmp_filter_attr::SCMP_FLTATR_ACT_BADARCH,
56 Self::CtlNnp => scmp_filter_attr::SCMP_FLTATR_CTL_NNP,
57 Self::CtlTsync => scmp_filter_attr::SCMP_FLTATR_CTL_TSYNC,
58 Self::ApiTskip => scmp_filter_attr::SCMP_FLTATR_API_TSKIP,
59 Self::CtlLog => scmp_filter_attr::SCMP_FLTATR_CTL_LOG,
60 Self::CtlSsb => scmp_filter_attr::SCMP_FLTATR_CTL_SSB,
61 Self::CtlOptimize => scmp_filter_attr::SCMP_FLTATR_CTL_OPTIMIZE,
62 Self::ApiSysRawRc => scmp_filter_attr::SCMP_FLTATR_API_SYSRAWRC,
63 Self::CtlWaitkill => scmp_filter_attr::SCMP_FLTATR_CTL_WAITKILL,
64 }
65 }
66 }
67
68 impl FromStr for ScmpFilterAttr {
69 type Err = SeccompError;
70
71 /// Converts string seccomp filter attribute to `ScmpFilterAttr`.
72 ///
73 /// # Arguments
74 ///
75 /// * `attr` - A string filter attribute, e.g. `SCMP_FLTATR_*`.
76 ///
77 /// See the [`seccomp_attr_set(3)`] man page for details on valid filter attribute values.
78 ///
79 /// [`seccomp_attr_set(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_attr_set.3.html
80 ///
81 /// # Errors
82 ///
83 /// If an invalid filter attribute is specified, an error will be returned.
84 fn from_str(attr: &str) -> Result<Self> {
85 match attr {
86 "SCMP_FLTATR_ACT_DEFAULT" => Ok(Self::ActDefault),
87 "SCMP_FLTATR_ACT_BADARCH" => Ok(Self::ActBadArch),
88 "SCMP_FLTATR_CTL_NNP" => Ok(Self::CtlNnp),
89 "SCMP_FLTATR_CTL_TSYNC" => Ok(Self::CtlTsync),
90 "SCMP_FLTATR_API_TSKIP" => Ok(Self::ApiTskip),
91 "SCMP_FLTATR_CTL_LOG" => Ok(Self::CtlLog),
92 "SCMP_FLTATR_CTL_SSB" => Ok(Self::CtlSsb),
93 "SCMP_FLTATR_CTL_OPTIMIZE" => Ok(Self::CtlOptimize),
94 "SCMP_FLTATR_API_SYSRAWRC" => Ok(Self::ApiSysRawRc),
95 "SCMP_FLTATR_CTL_WAITKILL" => Ok(Self::CtlWaitkill),
96 _ => Err(SeccompError::new(FromStr(attr.to_string()))),
97 }
98 }
99 }
100
101 #[cfg(test)]
102 mod tests {
103 use super::*;
104
105 #[test]
106 fn test_parse_filter_attr() {
107 let test_data = &[
108 ("SCMP_FLTATR_ACT_DEFAULT", ScmpFilterAttr::ActDefault),
109 ("SCMP_FLTATR_ACT_BADARCH", ScmpFilterAttr::ActBadArch),
110 ("SCMP_FLTATR_CTL_NNP", ScmpFilterAttr::CtlNnp),
111 ("SCMP_FLTATR_CTL_TSYNC", ScmpFilterAttr::CtlTsync),
112 ("SCMP_FLTATR_API_TSKIP", ScmpFilterAttr::ApiTskip),
113 ("SCMP_FLTATR_CTL_LOG", ScmpFilterAttr::CtlLog),
114 ("SCMP_FLTATR_CTL_SSB", ScmpFilterAttr::CtlSsb),
115 ("SCMP_FLTATR_CTL_OPTIMIZE", ScmpFilterAttr::CtlOptimize),
116 ("SCMP_FLTATR_API_SYSRAWRC", ScmpFilterAttr::ApiSysRawRc),
117 ("SCMP_FLTATR_CTL_WAITKILL", ScmpFilterAttr::CtlWaitkill),
118 ];
119 for data in test_data {
120 assert_eq!(
121 ScmpFilterAttr::from_str(data.0).unwrap().to_sys(),
122 data.1.to_sys()
123 );
124 }
125 assert!(ScmpFilterAttr::from_str("SCMP_INVALID_FLAG").is_err());
126 }
127 }