Debian Codegraph @

src/notify.rs

raw symbols SCIP
1 // SPDX-License-Identifier: Apache-2.0 or MIT
2 //
3 // Copyright 2021 Sony Group Corporation
4 //
5
6 use superrust-analyzer cargo libseccomp 0.4.0 crate/::cvtrust-analyzer cargo libseccomp 0.4.0 cvt().;
7 use craterust-analyzer cargo libseccomp 0.4.0 crate/::apirust-analyzer cargo libseccomp 0.4.0 api/::ensure_supported_apirust-analyzer cargo libseccomp 0.4.0 api/ensure_supported_api().;
8 use craterust-analyzer cargo libseccomp 0.4.0 crate/::errorrust-analyzer cargo libseccomp 0.4.0 error/::{Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#, SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#};
9 use craterust-analyzer cargo libseccomp 0.4.0 crate/::{ScmpArchrust-analyzer cargo libseccomp 0.4.0 arch/ScmpArch#, ScmpFilterContextrust-analyzer cargo libseccomp 0.4.0 filter_context/ScmpFilterContext#, ScmpSyscallrust-analyzer cargo libseccomp 0.4.0 syscall/ScmpSyscall#, ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#};
10 use bitflagsrust-analyzer cargo bitflags 2.13.1 crate/::bitflagsrust-analyzer cargo bitflags 2.13.1 bitflags!;
11 use libseccomp_sysrust-analyzer cargo libseccomp-sys 0.3.0 crate/::*;
12 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::BitOrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/BitOr#;
13 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/io/::RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#;
14
15 fn get_errnorust-analyzer cargo libseccomp 0.4.0 notify/get_errno().() -> i32 {
16 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::last_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]last_os_error().().raw_os_errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]raw_os_error().().unwrap_or(0)
17 }
18
19 /// Checks the libseccomp API level and the libseccomp version for
20 /// the userspace notification.
21 ///
22 /// This function succeeds if the libseccomp API level and the libseccomp
23 /// version being used are equal to or greater than 6 and 2.5.0.
24 ///
25 /// # Errors
26 ///
27 /// If both the libseccomp API level and the libseccomp version being used are
28 /// less than 6 and 2.5.0, an error will be returned.
29 fn notify_supportedrust-analyzer cargo libseccomp 0.4.0 notify/notify_supported().() -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<()> {
30 ensure_supported_apirust-analyzer cargo libseccomp 0.4.0 api/ensure_supported_api().("seccomp notification", 6, ScmpVersionrust-analyzer cargo libseccomp 0.4.0 version/ScmpVersion#::fromrust-analyzer cargo libseccomp 0.4.0 version/impl#[ScmpVersion][`From<(u32, u32, u32)>`]from().((2, 5, 0)))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
31
32 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
33 }
34
35 /// Represents a file descriptor used for the userspace notification.
36 pub type ScmpFdrust-analyzer cargo libseccomp 0.4.0 notify/ScmpFd# = RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#;
37
38 bitflagsrust-analyzer cargo bitflags 2.13.1 bitflags!! {
39 /// Userspace notification response flags
40 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd:, Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
41 pub struct ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#: u32 {
42 /// Tells the kernel to continue executing the system call that triggered the
43 /// notification. Must only be used when the notification response's error and value is 0.
44 const CONTINUErust-analyzer cargo libseccomp 0.4.0 notify/__bitflags_flag_names/CONTINUE. = SECCOMP_USER_NOTIF_FLAG_CONTINUErust-analyzer cargo libseccomp-sys 0.3.0 SECCOMP_USER_NOTIF_FLAG_CONTINUE.;
45 }
46 }
47
48 /// Userspace notification response flag
49 ///
50 /// Tells the kernel to continue executing the system call that triggered the
51 /// notification. Must only be used when the notification response's error is 0.
52 #[deprecated(
53 since = "0.3.0",
54 note = "Use ScmpNotifRespFlags::CONTINUE or ScmpNotifRespFlags::CONTINUE.bits()"
55 )]
56 pub const NOTIF_FLAG_CONTINUErust-analyzer cargo libseccomp 0.4.0 notify/NOTIF_FLAG_CONTINUE.: u32 = ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::CONTINUErust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]CONTINUE..bitsrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]bits().();
57
58 impl ScmpFilterContextrust-analyzer cargo libseccomp 0.4.0 filter_context/ScmpFilterContext# {
59 /// Gets a file descriptor for the userspace notification associated with the
60 /// given filter context.
61 ///
62 /// Such a file descriptor is only valid after the filter has been loaded
63 /// and only when the filter uses the [`crate::ScmpAction::Notify`] action.
64 /// The file descriptor can be used to retrieve and respond to notifications
65 /// associated with the filter (see [`ScmpNotifReq::receive()`],
66 /// [`ScmpNotifResp::respond()`], and [`notify_id_valid()`]).
67 ///
68 /// **Note**: This file descriptor is shared between all threads.
69 ///
70 /// This function returns a raw file descriptor for the userspace notification.
71 /// The ownership of this file descriptor is not moved and you must not close it.
72 ///
73 /// This function corresponds to
74 /// [`seccomp_notify_fd`](https://man7.org/linux/man-pages/man3/seccomp_notify_fd.3.html).
75 ///
76 /// # Errors
77 ///
78 /// If an issue is encountered getting the file descriptor,
79 /// an error will be returned.
80 /// # Examples
81 ///
82 /// ```
83 /// # use libseccomp::*;
84 /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
85 /// ctx.add_rule(ScmpAction::Notify, ScmpSyscall::from_name("getpid")?)?;
86 /// ctx.load()?;
87 /// let fd = ctx.get_notify_fd()?;
88 /// # Ok::<(), Box<dyn std::error::Error>>(())
89 /// ```
90 pub fn get_notify_fdrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpFilterContext]get_notify_fd().(&self) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<ScmpFdrust-analyzer cargo libseccomp 0.4.0 notify/ScmpFd#> {
91 notify_supportedrust-analyzer cargo libseccomp 0.4.0 notify/notify_supported().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
92
93 let ret = unsafe { seccomp_notify_fdrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_fd().(self.as_ptrrust-analyzer cargo libseccomp 0.4.0 filter_context/impl#[ScmpFilterContext]as_ptr().()) };
94 if ret rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialOrd<Self>`]lt().0 {
95 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#::from_errnorust-analyzer cargo libseccomp 0.4.0 error/impl#[SeccompError]from_errno().(ret));
96 }
97
98 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(ret)
99 }
100 }
101
102 /// Describes the system call context that triggered a notification.
103 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
104 #[non_exhaustive]
105 pub struct ScmpNotifDatarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData# {
106 /// The syscall number
107 pub syscallrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#syscall.: ScmpSyscallrust-analyzer cargo libseccomp 0.4.0 syscall/ScmpSyscall#,
108 /// The filter architecture
109 pub archrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#arch.: ScmpArchrust-analyzer cargo libseccomp 0.4.0 arch/ScmpArch#,
110 /// Address of the instruction that triggered a notification
111 pub instr_pointerrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#instr_pointer.: u64,
112 /// Arguments (up to 6) for the syscall
113 pub argsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#args.: [u64; 6],
114 }
115
116 impl ScmpNotifDatarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData# {
117 fn from_sysrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifData]from_sys().(data: seccomp_datarust-analyzer cargo libseccomp-sys 0.3.0 seccomp_data#) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifData]> {
118 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifData] {
119 syscallrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#syscall.: ScmpSyscallrust-analyzer cargo libseccomp 0.4.0 syscall/ScmpSyscall#::fromrust-analyzer cargo libseccomp 0.4.0 syscall/impl#[ScmpSyscall][`From<i32>`]from().(data.nrrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_data#nr.),
120 archrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#arch.: ScmpArchrust-analyzer cargo libseccomp 0.4.0 arch/ScmpArch#::from_sysrust-analyzer cargo libseccomp 0.4.0 arch/impl#[ScmpArch]from_sys().(data.archrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_data#arch.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
121 instr_pointerrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#instr_pointer.: data.instruction_pointerrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_data#instruction_pointer.,
122 argsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#args.: data.argsrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_data#args.,
123 })
124 }
125 }
126
127 /// Represents a userspace notification request.
128 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
129 #[non_exhaustive]
130 pub struct ScmpNotifReqrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq# {
131 /// Notification ID
132 pub idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#id.: u64,
133 /// Process that triggered the notification event
134 pub pidrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#pid.: u32,
135 /// Filter flags (see seccomp(2))
136 pub flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#flags.: u32,
137 /// System call context that triggered the notification
138 pub datarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#data.: ScmpNotifDatarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#,
139 }
140
141 impl ScmpNotifReqrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq# {
142 fn from_sysrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]from_sys().(req: seccomp_notifrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]> {
143 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq] {
144 idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#id.: req.idrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#id.,
145 pidrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#pid.: req.pidrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#pid.,
146 flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#flags.: req.flagsrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#flags.,
147 datarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifReq#data.: ScmpNotifDatarust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifData#::from_sysrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifData]from_sys().(req.datarust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#data.)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().,
148 })
149 }
150
151 /// Retrieves a userspace notification from a filter whose
152 /// [`crate::ScmpAction::Notify`] action has triggered.
153 ///
154 /// The caller is expected to process the notification and return a
155 /// response via [`ScmpNotifResp::respond()`]. Each invocation of
156 /// this function returns one notification.
157 /// As multiple notifications may be pending at any time, this function is
158 /// normally called within a polling loop.
159 ///
160 /// This function returns a userspace notification request.
161 ///
162 /// This function corresponds to
163 /// [`seccomp_notify_receive`](https://man7.org/linux/man-pages/man3/seccomp_notify_receive.3.html).
164 ///
165 /// # Arguments
166 ///
167 /// * `fd` - A file descriptor for the userspace notification
168 ///
169 /// # Errors
170 ///
171 /// If an issue is encountered getting a notification request,
172 /// an error will be returned.
173 ///
174 /// # Examples
175 ///
176 /// ```
177 /// # use libseccomp::*;
178 /// # use std::process::exit;
179 /// // Get the current process ID
180 /// let parent_pid = std::process::id();
181 /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
182 /// ctx.add_rule(ScmpAction::Notify, ScmpSyscall::from_name("getpid")?)?;
183 /// ctx.load()?;
184 /// let fd = ctx.get_notify_fd()?;
185 /// let pid = unsafe { libc::fork() };
186 /// if pid == 0 {
187 /// // Child process
188 ///
189 /// let child_pid = std::process::id();
190 /// // The child PID will be same as the parent PID by the user notification.
191 /// assert_eq!(child_pid, parent_pid);
192 /// exit(0);
193 /// }
194 /// let req = ScmpNotifReq::receive(fd)?;
195 /// let resp = ScmpNotifResp::new_val(req.id, parent_pid as i64, ScmpNotifRespFlags::empty());
196 /// resp.respond(fd);
197 /// # Ok::<(), Box<dyn std::error::Error>>(())
198 /// ```
199 pub fn receiverust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]receive().(fd: ScmpFdrust-analyzer cargo libseccomp 0.4.0 notify/ScmpFd#) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]> {
200 notify_supportedrust-analyzer cargo libseccomp 0.4.0 notify/notify_supported().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
201
202 let mut req_ptr: *mut seccomp_notifrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif# = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
203
204 // We only use the request here; the response is unused.
205 cvtrust-analyzer cargo libseccomp 0.4.0 cvt().(unsafe { seccomp_notify_allocrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_alloc().(&mut req_ptr, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().()) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
206
207 loop {
208 let ret = unsafe { seccomp_notify_receiverust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_receive().(fd, req_ptr) };
209 let errno = get_errnorust-analyzer cargo libseccomp 0.4.0 notify/get_errno().();
210
211 if ret rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 {
212 break;
213 } else if errno rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().libcrust-analyzer cargo libc 0.2.189 crate/::EINTRrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/EINTR. {
214 continue;
215 } else {
216 unsafe { seccomp_notify_freerust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_free().(req_ptr, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().()) };
217 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#::from_errnorust-analyzer cargo libseccomp 0.4.0 error/impl#[SeccompError]from_errno().(ret));
218 }
219 }
220
221 // Copy notify request before freeing the memory.
222 let req = seccomp_notifrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif# {
223 idrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#id.: unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().req_ptr).idrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#id. },
224 pidrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#pid.: unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().req_ptr).pidrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#pid. },
225 flagsrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#flags.: unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().req_ptr).flagsrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#flags. },
226 datarust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#data.: unsafe { (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().req_ptr).datarust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif#data. },
227 };
228
229 unsafe { seccomp_notify_freerust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_free().(req_ptr, stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().()) };
230
231 Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]::from_sysrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifReq]from_sys().(req)
232 }
233 }
234
235 /// Represents a userspace notification response.
236 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
237 #[non_exhaustive]
238 pub struct ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp# {
239 /// Notification ID (must match the corresponding `ScmpNotifReq` ID)
240 pub idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#id.: u64,
241 /// Return value for the syscall that created the notification.
242 /// Only relevant if the error is 0.
243 pub valrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#val.: i64,
244 /// An error code.
245 /// Must be 0 if no error occurred, or an error constant from package
246 /// syscall (e.g., `libc::EPERM`, etc). In the latter case, it's used
247 /// as an error return from the syscall that created the notification.
248 pub errorrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#error.: i32,
249 /// Userspace notification response flag
250 pub flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#flags.: u32,
251 }
252
253 impl ScmpNotifResprust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp# {
254 unsafe fn to_sysrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]to_sys().(self, resp: *mut seccomp_notif_resprust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif_resp#) {
255 unsafe {
256 (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().resp).idrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif_resp#id. = self.idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#id.;
257 (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().resp).valrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif_resp#val. = self.valrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#val.;
258 (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().resp).errorrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif_resp#error. = self.errorrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#error.;
259 (*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#deref().resp).flagsrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif_resp#flags. = self.flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#flags.;
260 }
261 }
262
263 /// Creates `ScmpNotifResp` from the specified arguments.
264 ///
265 /// It is recommended to use the convenient functions [`new_val`](ScmpNotifResp::new_val),
266 /// [`new_error`](ScmpNotifResp::new_error) and [`new_continue`](ScmpNotifResp::new_continue)
267 /// rather than this function.
268 ///
269 /// # Arguments
270 ///
271 /// * `id` - Notification ID
272 /// * `val` - Return value for the syscall that created the notification
273 /// * `error` - An error code
274 /// * `flags` - Userspace notification response flag
275 ///
276 /// # Examples
277 ///
278 /// ```ignore
279 /// let resp = ScmpNotifResp::new(req.id, val, error, flags);
280 /// ```
281 #[must_use]
282 pub fn newrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new().(id: u64, val: i64, error: i32, flags: u32) -> Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
283 Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
284 idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#id.,
285 valrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#val.,
286 errorrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#error.,
287 flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#flags.,
288 }
289 }
290
291 /// Creates `ScmpNotifResp` for a spoofed success response.
292 ///
293 /// # Arguments
294 ///
295 /// * `id` - Notification ID
296 /// * `val` - Return value for the syscall that created the notification
297 /// * `flags` - Reserved for future use, specify as `ScmpNotifRespFlags::empty()`.
298 ///
299 /// # Examples
300 ///
301 /// ```ignore
302 /// let resp = ScmpNotifResp::new_val(req.id, val, ScmpNotifRespFlags::empty());
303 /// ```
304 #[must_use]
305 pub fn new_valrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new_val().(id: u64, val: i64, flags: ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#) -> Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
306 Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
307 idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#id.,
308 valrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#val.,
309 errorrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#error.: 0,
310 flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#flags.: flags.bitsrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]bits().(),
311 }
312 }
313
314 /// Creates `ScmpNotifResp` for a spoofed error response.
315 ///
316 /// # Arguments
317 ///
318 /// * `id` - Notification ID
319 /// * `error` - An *negative* error code
320 /// * `flags` - Reserved for future use, specify as `ScmpNotifRespFlags::empty()`.
321 ///
322 /// # Examples
323 ///
324 /// ```ignore
325 /// let resp = ScmpNotifResp::new_error(req.id, error, ScmpNotifRespFlags::empty());
326 /// ```
327 #[must_use]
328 pub fn new_errorrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new_error().(id: u64, error: i32, flags: ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#) -> Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
329 debug_assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/debug_assert!!(error.is_negativerust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]is_negative().());
330 Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
331 idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#id.,
332 valrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#val.: 0,
333 errorrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#error.,
334 flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#flags.: flags.bitsrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]bits().(),
335 }
336 }
337
338 /// Creates `ScmpNotifResp` which continues the syscall execution.
339 ///
340 /// # Arguments
341 ///
342 /// * `id` - Notification ID
343 /// * `flags` - Reserved for future use, specify as `ScmpNotifRespFlags::empty()`.
344 ///
345 /// # Examples
346 ///
347 /// ```ignore
348 /// let resp = ScmpNotifResp::new_continue(req.id, ScmpNotifRespFlags::empty());
349 /// ```
350 #[must_use]
351 pub fn new_continuerust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]new_continue().(id: u64, flags: ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#) -> Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
352 Selfrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp] {
353 idrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#id.,
354 valrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#val.: 0,
355 errorrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#error.: 0,
356 flagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifResp#flags.: ScmpNotifRespFlagsrust-analyzer cargo libseccomp 0.4.0 notify/ScmpNotifRespFlags#::CONTINUErust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]CONTINUE..bitorrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags][`BitOr<Self>`]bitor().(flags).bitsrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifRespFlags]bits().(),
357 }
358 }
359
360 /// Responds to a userspace notification retrieved via [`ScmpNotifReq::receive()`].
361 ///
362 /// The response ID must match that of the corresponding notification retrieved
363 /// via [`ScmpNotifReq::receive()`].
364 ///
365 /// This function corresponds to
366 /// [`seccomp_notify_respond`](https://man7.org/linux/man-pages/man3/seccomp_notify_respond.3.html).
367 ///
368 /// # Arguments
369 ///
370 /// * `fd` - A file descriptor for the userspace notification
371 ///
372 /// # Errors
373 ///
374 /// If an issue is encountered responding a notification,
375 /// an error will be returned.
376 ///
377 /// # Examples
378 ///
379 /// ```
380 /// # use libseccomp::*;
381 /// # use std::process::exit;
382 /// // Get the current process ID
383 /// let parent_pid = std::process::id();
384 /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
385 /// ctx.add_rule(ScmpAction::Notify, ScmpSyscall::from_name("getpid")?)?;
386 /// ctx.load()?;
387 /// let fd = ctx.get_notify_fd()?;
388 /// let pid = unsafe { libc::fork() };
389 /// if pid == 0 {
390 /// // Child process
391 ///
392 /// let child_pid = std::process::id();
393 /// // The child PID will be same as the parent PID by the user notification.
394 /// assert_eq!(child_pid, parent_pid);
395 /// exit(0);
396 /// }
397 /// let req = ScmpNotifReq::receive(fd)?;
398 /// let resp = ScmpNotifResp::new_val(req.id, parent_pid as i64, ScmpNotifRespFlags::empty());
399 /// resp.respond(fd);
400 /// # Ok::<(), Box<dyn std::error::Error>>(())
401 /// ```
402 pub fn respondrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]respond().(&self, fd: ScmpFdrust-analyzer cargo libseccomp 0.4.0 notify/ScmpFd#) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<()> {
403 notify_supportedrust-analyzer cargo libseccomp 0.4.0 notify/notify_supported().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
404
405 let mut resp_ptr: *mut seccomp_notif_resprust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notif_resp# = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().();
406
407 // We only use the response here; the request is unused.
408 cvtrust-analyzer cargo libseccomp 0.4.0 cvt().(unsafe { seccomp_notify_allocrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_alloc().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(), &mut resp_ptr) })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
409
410 unsafe { self.to_sysrust-analyzer cargo libseccomp 0.4.0 notify/impl#[ScmpNotifResp]to_sys().(resp_ptr) };
411
412 loop {
413 let ret = unsafe { seccomp_notify_respondrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_respond().(fd, resp_ptr) };
414 let errno = get_errnorust-analyzer cargo libseccomp 0.4.0 notify/get_errno().();
415
416 if ret rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 {
417 break;
418 } else if errno rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().libcrust-analyzer cargo libc 0.2.189 crate/::EINTRrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/EINTR. {
419 continue;
420 } else {
421 unsafe { seccomp_notify_freerust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_free().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(), resp_ptr) };
422 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#::from_errnorust-analyzer cargo libseccomp 0.4.0 error/impl#[SeccompError]from_errno().(ret));
423 }
424 }
425
426 unsafe { seccomp_notify_freerust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_free().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::null_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/null_mut().(), resp_ptr) };
427
428 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
429 }
430 }
431
432 /// Checks if a userspace notification is still valid.
433 ///
434 /// A return value of `Ok` means the notification is still valid.
435 /// Otherwise the notification is not valid. This can be used to mitigate
436 /// time-of-check-time-of-use (TOCTOU) attacks as described in [`seccomp_notify_id_valid(2)`].
437 ///
438 /// [`seccomp_notify_id_valid(2)`]: https://man7.org/linux/man-pages/man3/seccomp_notify_id_valid.3.html
439 ///
440 /// This function corresponds to
441 /// [`seccomp_notify_id_valid`](https://man7.org/linux/man-pages/man3/seccomp_notify_id_valid.3.html).
442 ///
443 /// # Arguments
444 ///
445 /// * `fd` - A file descriptor for the userspace notification
446 /// * `id` - Notification ID
447 ///
448 /// # Errors
449 ///
450 /// If the notification ID is invalid, an error will be returned.
451 ///
452 /// # Examples
453 ///
454 /// ```
455 /// # use libseccomp::*;
456 /// # use std::{fs::File, io::Read, process::Command, thread};
457 /// let notify_fd = thread::spawn(|| -> Result<ScmpFd, error::SeccompError> {
458 /// let mut ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
459 /// ctx.add_rule(ScmpAction::Notify, ScmpSyscall::from_name("openat")?)?
460 /// .load()?;
461 /// let notify_fd = ctx.get_notify_fd()?;
462 ///
463 /// /// Example command execution
464 /// let _ = Command::new("ls").spawn();
465 /// Ok(notify_fd)
466 /// })
467 /// .join()
468 /// .unwrap()?;
469 ///
470 /// let req = ScmpNotifReq::receive(notify_fd)?;
471 /// assert_eq!(req.data.arch, ScmpArch::native());
472 /// assert_eq!(req.data.syscall, ScmpSyscall::from_name("openat")?);
473 ///
474 /// let mut stat = File::open(&format!("/proc/{}/stat", req.pid))?;
475 ///
476 /// // Checks if a notification is still valid.
477 /// notify_id_valid(notify_fd, req.id)?;
478 ///
479 /// let mut stat_data = String::new();
480 /// let _ = stat.read_to_string(&mut stat_data)?;
481 /// println!("{}", stat_data);
482 ///
483 /// // Checks if a notification is still valid.
484 /// notify_id_valid(notify_fd, req.id)?;
485 ///
486 /// let resp = ScmpNotifResp::new_continue(req.id, ScmpNotifRespFlags::empty());
487 /// resp.respond(notify_fd)?;
488 /// # Ok::<(), Box<dyn std::error::Error>>(())
489 /// ```
490 pub fn notify_id_validrust-analyzer cargo libseccomp 0.4.0 notify/notify_id_valid().(fd: ScmpFdrust-analyzer cargo libseccomp 0.4.0 notify/ScmpFd#, id: u64) -> Resultrust-analyzer cargo libseccomp 0.4.0 error/Result#<()> {
491 notify_supportedrust-analyzer cargo libseccomp 0.4.0 notify/notify_supported().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
492
493 loop {
494 let ret = unsafe { seccomp_notify_id_validrust-analyzer cargo libseccomp-sys 0.3.0 seccomp_notify_id_valid().(fd, id) };
495 let errno = get_errnorust-analyzer cargo libseccomp 0.4.0 notify/get_errno().();
496
497 if ret rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().0 {
498 break;
499 } else if errno rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[i32][`PartialEq<Self>`]eq().libcrust-analyzer cargo libc 0.2.189 crate/::EINTRrust-analyzer cargo libc 0.2.189 unix/linux_like/linux_l4re_shared/EINTR. {
500 continue;
501 } else {
502 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(SeccompErrorrust-analyzer cargo libseccomp 0.4.0 error/SeccompError#::from_errnorust-analyzer cargo libseccomp 0.4.0 error/impl#[SeccompError]from_errno().(ret));
503 }
504 }
505
506 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
507 }