| 1 |
//! Bindings for the FreeBSD `procctl` system call. |
| 2 |
//! |
| 3 |
//! There are similarities (but also differences) with Linux's `prctl` system |
| 4 |
//! call, whose interface is located in the `prctl.rs` file. |
| 5 |
|
| 6 |
#![allow(unsafe_code)] |
| 7 |
|
| 8 |
#[cfg(feature = "alloc")] |
| 9 |
use alloc::{vec, vec::Vec}; |
| 10 |
use core::mem::MaybeUninit; |
| 11 |
use core::num::NonZeroI32; |
| 12 |
use core::ptr; |
| 13 |
|
| 14 |
use bitflags::bitflags; |
| 15 |
|
| 16 |
use crate::backend::process::syscalls; |
| 17 |
use crate::backend::process::types::RawId; |
| 18 |
use crate::ffi::{c_int, c_uint, c_void}; |
| 19 |
use crate::io; |
| 20 |
use crate::process::{Pid, RawPid}; |
| 21 |
use crate::signal::Signal; |
| 22 |
use crate::utils::{as_mut_ptr, as_ptr}; |
| 23 |
|
| 24 |
// |
| 25 |
// Helper functions. |
| 26 |
// |
| 27 |
|
| 28 |
/// Subset of `idtype_t` C enum, with only the values allowed by `procctl`. |
| 29 |
#[repr(i32)] |
| 30 |
pub enum IdType { |
| 31 |
/// Process id. |
| 32 |
Pid = 0, |
| 33 |
/// Process group id. |
| 34 |
Pgid = 2, |
| 35 |
} |
| 36 |
|
| 37 |
/// A process selector for use with the `procctl` interface. |
| 38 |
/// |
| 39 |
/// `None` represents the current process. `Some((IdType::Pid, pid))` |
| 40 |
/// represents the process with pid `pid`. `Some((IdType::Pgid, pgid))` |
| 41 |
/// represents the control processes belonging to the process group with id |
| 42 |
/// `pgid`. |
| 43 |
pub type ProcSelector = Option<(IdType, Pid)>; |
| 44 |
fn proc_selector_to_raw(selector: ProcSelector) -> (IdType, RawPid) { |
| 45 |
match selector { |
| 46 |
Some((idtype, id)) => (idtype, id.as_raw_nonzero().get()), |
| 47 |
None => (IdType::Pid, 0), |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
#[inline] |
| 52 |
pub(crate) unsafe fn procctl( |
| 53 |
option: c_int, |
| 54 |
process: ProcSelector, |
| 55 |
data: *mut c_void, |
| 56 |
) -> io::Result<()> { |
| 57 |
let (idtype, id) = proc_selector_to_raw(process); |
| 58 |
syscalls::procctl(idtype as c_uint, id as RawId, option, data) |
| 59 |
} |
| 60 |
|
| 61 |
#[inline] |
| 62 |
pub(crate) unsafe fn procctl_set<P>( |
| 63 |
option: c_int, |
| 64 |
process: ProcSelector, |
| 65 |
data: &P, |
| 66 |
) -> io::Result<()> { |
| 67 |
procctl(option, process, (as_ptr(data) as *mut P).cast()) |
| 68 |
} |
| 69 |
|
| 70 |
#[inline] |
| 71 |
pub(crate) unsafe fn procctl_get_optional<P>( |
| 72 |
option: c_int, |
| 73 |
process: ProcSelector, |
| 74 |
) -> io::Result<P> { |
| 75 |
let mut value: MaybeUninit<P> = MaybeUninit::uninit(); |
| 76 |
procctl(option, process, value.as_mut_ptr().cast())?; |
| 77 |
Ok(value.assume_init()) |
| 78 |
} |
| 79 |
|
| 80 |
// |
| 81 |
// PROC_PDEATHSIG_STATUS/PROC_PDEATHSIG_CTL |
| 82 |
// |
| 83 |
|
| 84 |
const PROC_PDEATHSIG_STATUS: c_int = 12; |
| 85 |
|
| 86 |
/// Get the current value of the parent process death signal. |
| 87 |
/// |
| 88 |
/// # References |
| 89 |
/// - [Linux: `prctl(PR_GET_PDEATHSIG,…)`] |
| 90 |
/// - [FreeBSD: `procctl(PROC_PDEATHSIG_STATUS,…)`] |
| 91 |
/// |
| 92 |
/// [Linux: `prctl(PR_GET_PDEATHSIG,…)`]: https://man7.org/linux/man-pages/man2/prctl.2.html |
| 93 |
/// [FreeBSD: `procctl(PROC_PDEATHSIG_STATUS,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 94 |
#[inline] |
| 95 |
pub fn parent_process_death_signal() -> io::Result<Option<Signal>> { |
| 96 |
let raw = unsafe { procctl_get_optional::<c_int>(PROC_PDEATHSIG_STATUS, None) }?; |
| 97 |
if let Some(non_zero) = NonZeroI32::new(raw) { |
| 98 |
// SAFETY: The only way to get a libc-reserved signal number in |
| 99 |
// here would be to do something equivalent to |
| 100 |
// `set_parent_process_death_signal`, but that would have required |
| 101 |
// using a `Signal` with a libc-reserved value. |
| 102 |
Ok(Some(unsafe { |
| 103 |
Signal::from_raw_nonzero_unchecked(non_zero) |
| 104 |
})) |
| 105 |
} else { |
| 106 |
Ok(None) |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
const PROC_PDEATHSIG_CTL: c_int = 11; |
| 111 |
|
| 112 |
/// Set the parent-death signal of the calling process. |
| 113 |
/// |
| 114 |
/// # References |
| 115 |
/// - [Linux: `prctl(PR_SET_PDEATHSIG,…)`] |
| 116 |
/// - [FreeBSD: `procctl(PROC_PDEATHSIG_CTL,…)`] |
| 117 |
/// |
| 118 |
/// [Linux: `prctl(PR_SET_PDEATHSIG,…)`]: https://man7.org/linux/man-pages/man2/prctl.2.html |
| 119 |
/// [FreeBSD: `procctl(PROC_PDEATHSIG_CTL,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 120 |
#[inline] |
| 121 |
pub fn set_parent_process_death_signal(signal: Option<Signal>) -> io::Result<()> { |
| 122 |
let signal = signal.map_or(0, |signal| signal.as_raw()); |
| 123 |
unsafe { procctl_set::<c_int>(PROC_PDEATHSIG_CTL, None, &signal) } |
| 124 |
} |
| 125 |
|
| 126 |
// |
| 127 |
// PROC_TRACE_CTL |
| 128 |
// |
| 129 |
|
| 130 |
const PROC_TRACE_CTL: c_int = 7; |
| 131 |
|
| 132 |
const PROC_TRACE_CTL_ENABLE: i32 = 1; |
| 133 |
const PROC_TRACE_CTL_DISABLE: i32 = 2; |
| 134 |
const PROC_TRACE_CTL_DISABLE_EXEC: i32 = 3; |
| 135 |
|
| 136 |
/// `PROC_TRACE_CTL_*` |
| 137 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 138 |
#[repr(i32)] |
| 139 |
pub enum DumpableBehavior { |
| 140 |
/// Not dumpable. |
| 141 |
NotDumpable = PROC_TRACE_CTL_DISABLE, |
| 142 |
/// Dumpable. |
| 143 |
Dumpable = PROC_TRACE_CTL_ENABLE, |
| 144 |
/// Not dumpable, and this behaviour is preserved across `execve` calls. |
| 145 |
NotDumpableExecPreserved = PROC_TRACE_CTL_DISABLE_EXEC, |
| 146 |
} |
| 147 |
|
| 148 |
/// Set the state of the `dumpable` attribute for the process indicated by |
| 149 |
/// `idtype` and `id`. |
| 150 |
/// |
| 151 |
/// This determines whether the process can be traced and whether core dumps |
| 152 |
/// are produced for the process upon delivery of a signal whose default |
| 153 |
/// behavior is to produce a core dump. |
| 154 |
/// |
| 155 |
/// This is similar to `set_dumpable_behavior` on Linux, with the exception |
| 156 |
/// that on FreeBSD there is an extra argument `process`. When `process` is set |
| 157 |
/// to `None`, the operation is performed for the current process, like on |
| 158 |
/// Linux. |
| 159 |
/// |
| 160 |
/// # References |
| 161 |
/// - [FreeBSD `procctl(PROC_TRACE_CTL,…)`] |
| 162 |
/// |
| 163 |
/// [FreeBSD `procctl(PROC_TRACE_CTL,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 164 |
#[inline] |
| 165 |
pub fn set_dumpable_behavior(process: ProcSelector, config: DumpableBehavior) -> io::Result<()> { |
| 166 |
unsafe { procctl(PROC_TRACE_CTL, process, config as usize as *mut _) } |
| 167 |
} |
| 168 |
|
| 169 |
// |
| 170 |
// PROC_TRACE_STATUS |
| 171 |
// |
| 172 |
|
| 173 |
const PROC_TRACE_STATUS: c_int = 8; |
| 174 |
|
| 175 |
/// Tracing status as returned by [`trace_status`]. |
| 176 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 177 |
pub enum TracingStatus { |
| 178 |
/// Tracing is disabled for the process. |
| 179 |
NotTraceble, |
| 180 |
/// Tracing is not disabled for the process, but not debugger/tracer is |
| 181 |
/// attached. |
| 182 |
Tracable, |
| 183 |
/// The process is being traced by the process whose pid is stored in the |
| 184 |
/// first component of this variant. |
| 185 |
BeingTraced(Pid), |
| 186 |
} |
| 187 |
|
| 188 |
/// Get the tracing status of the process indicated by `idtype` and `id`. |
| 189 |
/// |
| 190 |
/// # References |
| 191 |
/// - [FreeBSD `procctl(PROC_TRACE_STATUS,…)`] |
| 192 |
/// |
| 193 |
/// [FreeBSD `procctl(PROC_TRACE_STATUS,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 194 |
#[inline] |
| 195 |
pub fn trace_status(process: ProcSelector) -> io::Result<TracingStatus> { |
| 196 |
let val = unsafe { procctl_get_optional::<c_int>(PROC_TRACE_STATUS, process) }?; |
| 197 |
match val { |
| 198 |
-1 => Ok(TracingStatus::NotTraceble), |
| 199 |
0 => Ok(TracingStatus::Tracable), |
| 200 |
pid => { |
| 201 |
let pid = Pid::from_raw(pid as RawPid).ok_or(io::Errno::RANGE)?; |
| 202 |
Ok(TracingStatus::BeingTraced(pid)) |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
// |
| 208 |
// PROC_REAP_* |
| 209 |
// |
| 210 |
|
| 211 |
const PROC_REAP_ACQUIRE: c_int = 2; |
| 212 |
const PROC_REAP_RELEASE: c_int = 3; |
| 213 |
|
| 214 |
/// Acquire or release the reaper status of the calling process. |
| 215 |
/// |
| 216 |
/// # References |
| 217 |
/// - [FreeBSD: `procctl(PROC_REAP_ACQUIRE/RELEASE,…)`] |
| 218 |
/// |
| 219 |
/// [FreeBSD: `procctl(PROC_REAP_ACQUIRE/RELEASE,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 220 |
#[inline] |
| 221 |
pub fn set_reaper_status(reaper: bool) -> io::Result<()> { |
| 222 |
unsafe { |
| 223 |
procctl( |
| 224 |
if reaper { |
| 225 |
PROC_REAP_ACQUIRE |
| 226 |
} else { |
| 227 |
PROC_REAP_RELEASE |
| 228 |
}, |
| 229 |
None, |
| 230 |
ptr::null_mut(), |
| 231 |
) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
const PROC_REAP_STATUS: c_int = 4; |
| 236 |
|
| 237 |
bitflags! { |
| 238 |
/// `REAPER_STATUS_*` |
| 239 |
#[repr(transparent)] |
| 240 |
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] |
| 241 |
pub struct ReaperStatusFlags: c_uint { |
| 242 |
/// The process has acquired reaper status. |
| 243 |
const OWNED = 1; |
| 244 |
/// The process is the root of the reaper tree ([`Pid::INIT`]). |
| 245 |
const REALINIT = 2; |
| 246 |
|
| 247 |
/// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags> |
| 248 |
const _ = !0; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
#[repr(C)] |
| 253 |
struct procctl_reaper_status { |
| 254 |
rs_flags: c_uint, |
| 255 |
rs_children: c_uint, |
| 256 |
rs_descendants: c_uint, |
| 257 |
rs_reaper: RawPid, |
| 258 |
rs_pid: RawPid, |
| 259 |
rs_pad0: [c_uint; 15], |
| 260 |
} |
| 261 |
|
| 262 |
/// Reaper status as returned by [`get_reaper_status`]. |
| 263 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 264 |
pub struct ReaperStatus { |
| 265 |
/// The flags. |
| 266 |
pub flags: ReaperStatusFlags, |
| 267 |
/// The number of children of the reaper among the descendants. |
| 268 |
pub children: usize, |
| 269 |
/// The total number of descendants of the reaper(s), not counting |
| 270 |
/// descendants of the reaper in the subtree. |
| 271 |
pub descendants: usize, |
| 272 |
/// The pid of the reaper for the specified process id. |
| 273 |
pub reaper: Pid, |
| 274 |
/// The pid of one reaper child if there are any descendants. |
| 275 |
pub pid: Option<Pid>, |
| 276 |
} |
| 277 |
|
| 278 |
/// Get information about the reaper of the specified process (or the process |
| 279 |
/// itself if it is a reaper). |
| 280 |
/// |
| 281 |
/// # References |
| 282 |
/// - [FreeBSD: `procctl(PROC_REAP_STATUS,…)`] |
| 283 |
/// |
| 284 |
/// [FreeBSD: `procctl(PROC_REAP_STATUS,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 285 |
#[inline] |
| 286 |
pub fn get_reaper_status(process: ProcSelector) -> io::Result<ReaperStatus> { |
| 287 |
let raw = unsafe { procctl_get_optional::<procctl_reaper_status>(PROC_REAP_STATUS, process) }?; |
| 288 |
Ok(ReaperStatus { |
| 289 |
flags: ReaperStatusFlags::from_bits_retain(raw.rs_flags), |
| 290 |
children: raw.rs_children as _, |
| 291 |
descendants: raw.rs_descendants as _, |
| 292 |
reaper: Pid::from_raw(raw.rs_reaper).ok_or(io::Errno::RANGE)?, |
| 293 |
pid: if raw.rs_pid == -1 { |
| 294 |
None |
| 295 |
} else { |
| 296 |
Some(Pid::from_raw(raw.rs_pid).ok_or(io::Errno::RANGE)?) |
| 297 |
}, |
| 298 |
}) |
| 299 |
} |
| 300 |
|
| 301 |
#[cfg(feature = "alloc")] |
| 302 |
const PROC_REAP_GETPIDS: c_int = 5; |
| 303 |
|
| 304 |
bitflags! { |
| 305 |
/// `REAPER_PIDINFO_*` |
| 306 |
#[repr(transparent)] |
| 307 |
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] |
| 308 |
pub struct PidInfoFlags: c_uint { |
| 309 |
/// This structure was filled by the kernel. |
| 310 |
const VALID = 1; |
| 311 |
/// The pid field identifies a direct child of the reaper. |
| 312 |
const CHILD = 2; |
| 313 |
/// The reported process is itself a reaper. Descendants of a |
| 314 |
/// subordinate reaper are not reported. |
| 315 |
const REAPER = 4; |
| 316 |
/// The reported process is in the zombie state. |
| 317 |
const ZOMBIE = 8; |
| 318 |
/// The reported process is stopped by |
| 319 |
/// [`Signal::Stop`]/[`Signal::Tstp`]. |
| 320 |
const STOPPED = 16; |
| 321 |
/// The reported process is in the process of exiting. |
| 322 |
const EXITING = 32; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
#[repr(C)] |
| 327 |
#[derive(Default, Clone)] |
| 328 |
struct procctl_reaper_pidinfo { |
| 329 |
pi_pid: RawPid, |
| 330 |
pi_subtree: RawPid, |
| 331 |
pi_flags: c_uint, |
| 332 |
pi_pad0: [c_uint; 15], |
| 333 |
} |
| 334 |
|
| 335 |
#[repr(C)] |
| 336 |
struct procctl_reaper_pids { |
| 337 |
rp_count: c_uint, |
| 338 |
rp_pad0: [c_uint; 15], |
| 339 |
rp_pids: *mut procctl_reaper_pidinfo, |
| 340 |
} |
| 341 |
|
| 342 |
/// A child process of a reaper. |
| 343 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 344 |
pub struct PidInfo { |
| 345 |
/// The flags of the process. |
| 346 |
pub flags: PidInfoFlags, |
| 347 |
/// The pid of the process. |
| 348 |
pub pid: Pid, |
| 349 |
/// The pid of the child of the reaper which is the (grand-…)parent of the |
| 350 |
/// process. |
| 351 |
pub subtree: Pid, |
| 352 |
} |
| 353 |
|
| 354 |
/// Get the list of descendants of the specified reaper process. |
| 355 |
/// |
| 356 |
/// # References |
| 357 |
/// - [FreeBSD: `procctl(PROC_REAP_GETPIDS,…)`] |
| 358 |
/// |
| 359 |
/// [FreeBSD: `procctl(PROC_REAP_GETPIDS,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 360 |
#[cfg(feature = "alloc")] |
| 361 |
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
| 362 |
pub fn get_reaper_pids(process: ProcSelector) -> io::Result<Vec<PidInfo>> { |
| 363 |
// Sadly no better way to guarantee that we get all the results than to |
| 364 |
// allocate ≈8MB of memory… |
| 365 |
const PID_MAX: usize = 99999; |
| 366 |
let mut pids: Vec<procctl_reaper_pidinfo> = vec![Default::default(); PID_MAX]; |
| 367 |
let mut pinfo = procctl_reaper_pids { |
| 368 |
rp_count: PID_MAX as _, |
| 369 |
rp_pad0: [0; 15], |
| 370 |
rp_pids: pids.as_mut_slice().as_mut_ptr(), |
| 371 |
}; |
| 372 |
unsafe { procctl(PROC_REAP_GETPIDS, process, as_mut_ptr(&mut pinfo).cast())? }; |
| 373 |
let mut result = Vec::new(); |
| 374 |
for raw in pids.into_iter() { |
| 375 |
let flags = PidInfoFlags::from_bits_retain(raw.pi_flags); |
| 376 |
if !flags.contains(PidInfoFlags::VALID) { |
| 377 |
break; |
| 378 |
} |
| 379 |
result.push(PidInfo { |
| 380 |
flags, |
| 381 |
subtree: Pid::from_raw(raw.pi_subtree).ok_or(io::Errno::RANGE)?, |
| 382 |
pid: Pid::from_raw(raw.pi_pid).ok_or(io::Errno::RANGE)?, |
| 383 |
}); |
| 384 |
} |
| 385 |
Ok(result) |
| 386 |
} |
| 387 |
|
| 388 |
const PROC_REAP_KILL: c_int = 6; |
| 389 |
|
| 390 |
bitflags! { |
| 391 |
/// `REAPER_KILL_*` |
| 392 |
#[repr(transparent)] |
| 393 |
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] |
| 394 |
struct KillFlags: c_uint { |
| 395 |
const CHILDREN = 1; |
| 396 |
const SUBTREE = 2; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
#[repr(C)] |
| 401 |
struct procctl_reaper_kill { |
| 402 |
rk_sig: c_int, |
| 403 |
rk_flags: c_uint, |
| 404 |
rk_subtree: RawPid, |
| 405 |
rk_killed: c_uint, |
| 406 |
rk_fpid: RawPid, |
| 407 |
rk_pad0: [c_uint; 15], |
| 408 |
} |
| 409 |
|
| 410 |
/// Reaper status as returned by [`get_reaper_status`]. |
| 411 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 412 |
pub struct KillResult { |
| 413 |
/// The number of processes that were signalled. |
| 414 |
pub killed: usize, |
| 415 |
/// The pid of the first process that wasn't successfully signalled. |
| 416 |
pub first_failed: Option<Pid>, |
| 417 |
} |
| 418 |
|
| 419 |
/// Deliver a signal to some subset of the descendants of the reaper. |
| 420 |
/// |
| 421 |
/// # References |
| 422 |
/// - [FreeBSD: `procctl(PROC_REAP_KILL,…)`] |
| 423 |
/// |
| 424 |
/// [FreeBSD: `procctl(PROC_REAP_KILL,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 425 |
pub fn reaper_kill( |
| 426 |
process: ProcSelector, |
| 427 |
signal: Signal, |
| 428 |
direct_children: bool, |
| 429 |
subtree: Option<Pid>, |
| 430 |
) -> io::Result<KillResult> { |
| 431 |
let mut flags = KillFlags::empty(); |
| 432 |
flags.set(KillFlags::CHILDREN, direct_children); |
| 433 |
flags.set(KillFlags::SUBTREE, subtree.is_some()); |
| 434 |
let mut req = procctl_reaper_kill { |
| 435 |
rk_sig: signal.as_raw(), |
| 436 |
rk_flags: flags.bits(), |
| 437 |
rk_subtree: subtree.map(|p| p.as_raw_nonzero().into()).unwrap_or(0), |
| 438 |
rk_killed: 0, |
| 439 |
rk_fpid: 0, |
| 440 |
rk_pad0: [0; 15], |
| 441 |
}; |
| 442 |
unsafe { procctl(PROC_REAP_KILL, process, as_mut_ptr(&mut req).cast())? }; |
| 443 |
Ok(KillResult { |
| 444 |
killed: req.rk_killed as _, |
| 445 |
first_failed: Pid::from_raw(req.rk_fpid), |
| 446 |
}) |
| 447 |
} |
| 448 |
|
| 449 |
// |
| 450 |
// PROC_TRAPCAP_STATUS/PROC_TRAPCAP_CTL |
| 451 |
// |
| 452 |
|
| 453 |
const PROC_TRAPCAP_CTL: c_int = 9; |
| 454 |
|
| 455 |
const PROC_TRAPCAP_CTL_ENABLE: i32 = 1; |
| 456 |
const PROC_TRAPCAP_CTL_DISABLE: i32 = 2; |
| 457 |
|
| 458 |
/// `PROC_TRAPCAP_CTL_*` |
| 459 |
#[derive(Copy, Clone, Debug, Eq, PartialEq)] |
| 460 |
#[repr(i32)] |
| 461 |
pub enum TrapCapBehavior { |
| 462 |
/// Disable the [`Signal::Trap`] signal delivery on capability mode access |
| 463 |
/// violations. |
| 464 |
Disable = PROC_TRAPCAP_CTL_DISABLE, |
| 465 |
/// Enable the [`Signal::Trap`] signal delivery on capability mode access |
| 466 |
/// violations. |
| 467 |
Enable = PROC_TRAPCAP_CTL_ENABLE, |
| 468 |
} |
| 469 |
|
| 470 |
/// Set the current value of the capability mode violation trapping behavior. |
| 471 |
/// |
| 472 |
/// If this behavior is enabled, the kernel would deliver a [`Signal::Trap`] |
| 473 |
/// signal on any return from a system call that would result in a |
| 474 |
/// [`io::Errno::NOTCAPABLE`] or [`io::Errno::CAPMODE`] error. |
| 475 |
/// |
| 476 |
/// This behavior is inherited by the children of the process and is kept |
| 477 |
/// across `execve` calls. |
| 478 |
/// |
| 479 |
/// # References |
| 480 |
/// - [FreeBSD: `procctl(PROC_TRAPCAP_CTL,…)`] |
| 481 |
/// |
| 482 |
/// [FreeBSD: `procctl(PROC_TRAPCAP_CTL,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 483 |
#[inline] |
| 484 |
pub fn set_trap_cap_behavior(process: ProcSelector, config: TrapCapBehavior) -> io::Result<()> { |
| 485 |
let config = config as c_int; |
| 486 |
unsafe { procctl_set::<c_int>(PROC_TRAPCAP_CTL, process, &config) } |
| 487 |
} |
| 488 |
|
| 489 |
const PROC_TRAPCAP_STATUS: c_int = 10; |
| 490 |
|
| 491 |
/// Get the current value of the capability mode violation trapping behavior. |
| 492 |
/// |
| 493 |
/// # References |
| 494 |
/// - [FreeBSD: `procctl(PROC_TRAPCAP_STATUS,…)`] |
| 495 |
/// |
| 496 |
/// [FreeBSD: `procctl(PROC_TRAPCAP_STATUS,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 497 |
#[inline] |
| 498 |
pub fn trap_cap_behavior(process: ProcSelector) -> io::Result<TrapCapBehavior> { |
| 499 |
let val = unsafe { procctl_get_optional::<c_int>(PROC_TRAPCAP_STATUS, process) }?; |
| 500 |
match val { |
| 501 |
PROC_TRAPCAP_CTL_DISABLE => Ok(TrapCapBehavior::Disable), |
| 502 |
PROC_TRAPCAP_CTL_ENABLE => Ok(TrapCapBehavior::Enable), |
| 503 |
_ => Err(io::Errno::RANGE), |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
// |
| 508 |
// PROC_NO_NEW_PRIVS_STATUS/PROC_NO_NEW_PRIVS_CTL |
| 509 |
// |
| 510 |
|
| 511 |
const PROC_NO_NEW_PRIVS_CTL: c_int = 19; |
| 512 |
|
| 513 |
const PROC_NO_NEW_PRIVS_ENABLE: c_int = 1; |
| 514 |
|
| 515 |
/// Enable the `no_new_privs` mode that ignores SUID and SGID bits on `execve` |
| 516 |
/// in the specified process and its future descendants. |
| 517 |
/// |
| 518 |
/// This is similar to `set_no_new_privs` on Linux, with the exception that on |
| 519 |
/// FreeBSD there is no argument `no_new_privs` argument as it's only possible |
| 520 |
/// to enable this mode and there's no going back. |
| 521 |
/// |
| 522 |
/// # References |
| 523 |
/// - [Linux: `prctl(PR_SET_NO_NEW_PRIVS,…)`] |
| 524 |
/// - [FreeBSD: `procctl(PROC_NO_NEW_PRIVS_CTL,…)`] |
| 525 |
/// |
| 526 |
/// [Linux: `prctl(PR_SET_NO_NEW_PRIVS,…)`]: https://man7.org/linux/man-pages/man2/prctl.2.html |
| 527 |
/// [FreeBSD: `procctl(PROC_NO_NEW_PRIVS_CTL,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 528 |
#[inline] |
| 529 |
pub fn set_no_new_privs(process: ProcSelector) -> io::Result<()> { |
| 530 |
unsafe { procctl_set::<c_int>(PROC_NO_NEW_PRIVS_CTL, process, &PROC_NO_NEW_PRIVS_ENABLE) } |
| 531 |
} |
| 532 |
|
| 533 |
const PROC_NO_NEW_PRIVS_STATUS: c_int = 20; |
| 534 |
|
| 535 |
/// Check the `no_new_privs` mode of the specified process. |
| 536 |
/// |
| 537 |
/// # References |
| 538 |
/// - [Linux: `prctl(PR_GET_NO_NEW_PRIVS,…)`] |
| 539 |
/// - [FreeBSD: `procctl(PROC_NO_NEW_PRIVS_STATUS,…)`] |
| 540 |
/// |
| 541 |
/// [Linux: `prctl(PR_GET_NO_NEW_PRIVS,…)`]: https://man7.org/linux/man-pages/man2/prctl.2.html |
| 542 |
/// [FreeBSD: `procctl(PROC_NO_NEW_PRIVS_STATUS,…)`]: https://man.freebsd.org/cgi/man.cgi?query=procctl&sektion=2 |
| 543 |
#[inline] |
| 544 |
pub fn no_new_privs(process: ProcSelector) -> io::Result<bool> { |
| 545 |
unsafe { procctl_get_optional::<c_int>(PROC_NO_NEW_PRIVS_STATUS, process) } |
| 546 |
.map(|x| x == PROC_NO_NEW_PRIVS_ENABLE) |
| 547 |
} |