Debian Codegraph @

src/action.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::convert::TryInto;
10
11 /// Represents an action to be taken on a filter rule match in the libseccomp.
12 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
13 #[non_exhaustive]
14 pub enum ScmpAction {
15 /// Kills the process.
16 KillProcess,
17 /// Kills the thread.
18 KillThread,
19 /// Throws a SIGSYS signal.
20 Trap,
21 /// Triggers a userspace notification.
22 /// NOTE: This action is only usable when the libseccomp API level 6
23 /// or higher is supported.
24 Notify,
25 /// Returns the specified error code.
26 /// NOTE: You can only use integers from 0 to `u16::MAX`.
27 Errno(i32),
28 /// Notifies a tracing process with the specified value.
29 Trace(u16),
30 /// Allows the syscall to be executed after the action has been logged.
31 Log,
32 /// Allows the syscall to be executed.
33 Allow,
34 }
35
36 impl ScmpAction {
37 pub(crate) fn to_sys(self) -> u32 {
38 match self {
39 Self::KillProcess => SCMP_ACT_KILL_PROCESS,
40 Self::KillThread => SCMP_ACT_KILL_THREAD,
41 Self::Trap => SCMP_ACT_TRAP,
42 Self::Notify => SCMP_ACT_NOTIFY,
43 Self::Errno(x) => SCMP_ACT_ERRNO(x as u16),
44 Self::Trace(x) => SCMP_ACT_TRACE(x),
45 Self::Log => SCMP_ACT_LOG,
46 Self::Allow => SCMP_ACT_ALLOW,
47 }
48 }
49
50 pub(crate) fn from_sys(val: u32) -> Result<Self> {
51 match val & SCMP_ACT_MASK {
52 SCMP_ACT_KILL_PROCESS => Ok(Self::KillProcess),
53 SCMP_ACT_KILL_THREAD => Ok(Self::KillThread),
54 SCMP_ACT_TRAP => Ok(Self::Trap),
55 SCMP_ACT_NOTIFY => Ok(Self::Notify),
56 SCMP_ACT_ERRNO_MASK => Ok(Self::Errno(val as u16 as i32)),
57 SCMP_ACT_TRACE_MASK => Ok(Self::Trace(val as u16)),
58 SCMP_ACT_LOG => Ok(Self::Log),
59 SCMP_ACT_ALLOW => Ok(Self::Allow),
60 inv => Err(SeccompError::new(InvalidAction(inv))),
61 }
62 }
63
64 /// Converts string seccomp action to `ScmpAction`.
65 ///
66 /// # Arguments
67 ///
68 /// * `action` - A string action, e.g. `SCMP_ACT_*`.
69 ///
70 /// See the [`seccomp_rule_add(3)`] man page for details on valid action values.
71 ///
72 /// [`seccomp_rule_add(3)`]: https://www.man7.org/linux/man-pages/man3/seccomp_rule_add.3.html
73 ///
74 /// # Errors
75 ///
76 /// If an invalid action is specified or a value on `"SCMP_ACT_TRACE"` is not in the
77 /// range from 0 to `u16::MAX`, an error will be returned.
78 pub fn from_str(action: &str, val: Option<i32>) -> Result<Self> {
79 match action {
80 "SCMP_ACT_KILL_PROCESS" => Ok(Self::KillProcess),
81 "SCMP_ACT_KILL_THREAD" | "SCMP_ACT_KILL" => Ok(Self::KillThread),
82 "SCMP_ACT_TRAP" => Ok(Self::Trap),
83 "SCMP_ACT_NOTIFY" => Ok(Self::Notify),
84 "SCMP_ACT_ERRNO" => match val {
85 Some(v) => Ok(Self::Errno(v)),
86 None => Err(SeccompError::new(Common(
87 "Missing an errno value of SCMP_ACT_ERRNO in ScmpAction::from_str".into(),
88 ))),
89 },
90 "SCMP_ACT_TRACE" => match val {
91 Some(v) => Ok(Self::Trace(v.try_into()?)),
92 None => Err(SeccompError::new(Common(
93 "Missing a message value of SCMP_ACT_TRACE in ScmpAction::from_str".into(),
94 ))),
95 },
96 "SCMP_ACT_LOG" => Ok(Self::Log),
97 "SCMP_ACT_ALLOW" => Ok(Self::Allow),
98 _ => Err(SeccompError::new(FromStr(action.to_string()))),
99 }
100 }
101 }
102
103 #[cfg(test)]
104 mod tests {
105 use super::*;
106
107 #[test]
108 fn test_parse_action() {
109 let test_data = &[
110 ("SCMP_ACT_KILL_PROCESS", ScmpAction::KillProcess),
111 ("SCMP_ACT_KILL_THREAD", ScmpAction::KillThread),
112 ("SCMP_ACT_KILL", ScmpAction::KillThread),
113 ("SCMP_ACT_TRAP", ScmpAction::Trap),
114 ("SCMP_ACT_NOTIFY", ScmpAction::Notify),
115 ("SCMP_ACT_ERRNO", ScmpAction::Errno(10)),
116 ("SCMP_ACT_TRACE", ScmpAction::Trace(10)),
117 ("SCMP_ACT_LOG", ScmpAction::Log),
118 ("SCMP_ACT_ALLOW", ScmpAction::Allow),
119 ];
120
121 for data in test_data {
122 if data.0 == "SCMP_ACT_ERRNO" || data.0 == "SCMP_ACT_TRACE" {
123 assert_eq!(
124 ScmpAction::from_sys(ScmpAction::from_str(data.0, Some(10)).unwrap().to_sys())
125 .unwrap(),
126 data.1
127 );
128 } else {
129 assert_eq!(
130 ScmpAction::from_sys(ScmpAction::from_str(data.0, None).unwrap().to_sys())
131 .unwrap(),
132 data.1
133 );
134 }
135 }
136 assert!(ScmpAction::from_str("SCMP_ACT_ERRNO", None).is_err());
137 assert!(ScmpAction::from_str("SCMP_ACT_TRACE", None).is_err());
138 assert!(ScmpAction::from_str("SCMP_INVALID_FLAG", None).is_err());
139 assert!(ScmpAction::from_sys(0x00010000).is_err());
140 }
141 }