diff --git a/Cargo.lock b/Cargo.lock index 0868d6f..812b4fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -486,7 +486,9 @@ dependencies = [ "pin-project-lite", "prost", "serde", + "strum", "strum_macros", + "syscalls", "tokio", "tokio-stream", "tonic", @@ -2029,6 +2031,12 @@ dependencies = [ "syn 2.0.110", ] +[[package]] +name = "syscalls" +version = "0.6.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d0e35dc7d73976a53c7e6d7d177ef804a0c0ee774ec77bcc520c2216fd7cbe" + [[package]] name = "tar" version = "0.4.46" diff --git a/Cargo.toml b/Cargo.toml index 0256fb6..e414ce5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -55,7 +55,9 @@ log = "0.4.30" nix = { version = "0.31.3", features = ["signal"] } pin-project-lite = "0.2.17" prost = "0.14.3" +strum = "0.28" strum_macros = "0.28" +syscalls = { version = "0.6", default-features = false } tokio = { version = "1.52.3", features = ["full"] } tokio-stream = { version="0.1.18", features = ["sync"] } tonic = "0.14.6" diff --git a/docs/design/zone-process-tracking.md b/docs/design/zone-process-tracking.md new file mode 100644 index 0000000..1edc876 --- /dev/null +++ b/docs/design/zone-process-tracking.md @@ -0,0 +1,145 @@ +# Design: zone process tracking (issue #154) + +Status: proposed, 2026-11-20. Fixes +[edera-dev/falco_plugin#154](https://github.com/edera-dev/falco_plugin/issues/154). + +## Problem + +Zone `open` events reached Falco with `fd.name=NA`, `proc.exe=NA` and +`evt.res=`. Events from processes started after the zone snapshot were +logged as `no thread found ... dropping event`. No file rule on `edera_zone` +could fire. + +## Root cause + +Captured on a Xen worker (Edera 1.13.1, Falco 0.45.2) with a diagnostic build: + +1. Every zone syscall event arrives with `event_category = "EC_TRACEPOINT"`. +2. `parsers::has_retval()` required `SYSCALL` or `EC_METAEVENT` in that string, + so it returned false for every event, even though the return value was + present (`retval=3` on the `/etc/shadow` open). +3. Every handler then treated the syscall as failed. Clone and fork children, + including the child-side events that did arrive, were never added. `execve` + never set the process; `open` never recorded the fd. The thread table stayed + at its snapshot size (25 entries). + +The wrong label comes from the zone. protect +`crates/libscap-rs/src/wrapper/scap/mod.rs` formats `ppm_event_info.category` +with `{:?}`. The value is a C bit set (`EC_FILE | EC_SYSCALL`), but +libscap-bindings types it as a Rust enum, so a combined value is not a valid +variant and prints as a wrong name. + +## Changes + +### 1. Return values come from the event, not the category string + +`has_retval()` is true for any exit event with parameters, except +`PPME_GENERIC_X`. libsinsp also checks the category bits from its static event +table. Every exit event libscap defines is a syscall or metaevent exit whose +first parameter is the return value, so the result is the same. The plugin is +built without libscap's event table (`default-features = false`), so it can't +check the bits itself, and the zone's string can't be trusted. + +### 2. Zones send process exits + +With the return-value fix, every new process is added. A thread leaves the +table only on a process-exit event, and the zone's default syscall set has +none. So the table would grow for as long as Falco runs, and stale entries +would mislabel reused PIDs. + +The plugin now sends an explicit set instead of an empty request (empty means +"zone default"): the zone default set plus `PPM_SC_SCHED_PROCESS_EXIT`. In +mirror mode it adds process exit to the set read from Falco, and if that map +can't be read it falls back to the same explicit set. The +copy matches protect `crates/zone/src/kernel_events.rs` in v1.13.1, v1.13.2 and +v1.13.4. This works with zones already deployed, without a protect release. + +Trade-off: the plugin now carries a copy of the zone's default set, and the +copy can drift. Before, the plugin asked for "the default", so Falco users got +whatever protect's default said. From now on, if a future zone default adds or +removes events, Falco users won't see that change until someone updates the +plugin's copy. A unit test fences only the process-lifecycle events in the set, +not the full list. + +Plan to remove the copy: + +1. protect adds `PPM_SC_SCHED_PROCESS_EXIT` to the zone default set + ([edera-dev/protect#5079](https://github.com/edera-dev/protect/pull/5079)). +2. Once every supported zone version includes it, the plugin goes back to + sending an empty request ("zone default") and drops its copy. + +Until then, whoever changes protect's zone default should update +`default_zone_syscalls()` in `src/source_plugin/client/enabled_syscalls.rs`. + +### 3. Children move to the reaper on exit + +`parse_thread_exit()` meant to reparent the exiting thread's children, but it +compared each thread's `ptid` with its own `tid`, so nothing moved. The next +purge then deleted running children whose parent had exited. This code never +ran before because no exit events arrived. It now matches libsinsp: children +of the exiting thread get the event's reaper tid, or no parent. + +### 4. The syscall set goes in the field released daemons read + +Found by the soak test. Since the #157 proto sync, the plugin sends its +syscall set only in `ZoneKernelEventStreamUpdate.source` (tag 3). Protect +daemons up to v1.13.x read only the old top-level `enabled_syscalls` (tag 2) +and ignore `source`, so they saw an empty set and zones used their default. On +a 1.13.1 worker, the table grew by about 850 threads a minute with zero exit +events. This also means mirror mode's set never reached zones on any released +Protect. + +The public proto no longer has tag 2, so it is hand-added to the generated +control types (marked `HAND-ADDED`), and the plugin fills both fields. A test +decodes the request the way a v1.13.x daemon does. Whoever re-syncs the protos +must keep the field until no supported daemon needs it; the test fails to +compile if it is dropped. + +After the fix, on the same worker: about 8,000 process exits per 5 minutes +under load, and the table held at 27 entries. + +## Not changed + +- **Mirror mode sends the wrong syscall set** ([#165](https://github.com/edera-dev/falco_plugin/issues/165), separate PR). With + `mirror_host_syscalls: true` (the repo's Helm example, + `examples/falco-helm-values.yaml`), the plugin reads Falco's + `interesting_syscalls_table_64bit` map as libscap `ppm_sc` codes. That map is + indexed by the kernel's syscall number, so zones get the wrong set. Fixing it + needs the syscall-number to `ppm_sc` table. This change only makes sure mirror + mode, and its fallback when the map can't be read, also ask for process exit. + Falco's map never holds tracepoints, so mirror mode would otherwise reintroduce + the unbounded thread table. +- **`evt.category` and `evt.count.error.memory`** still show the zone's wrong + string until zones send correct categories (protect fix below). +- **Lost exit events** (ring-buffer drops) still leave entries behind. libsinsp + covers this with periodic `/proc` rescans; the zone snapshot on reconnect is + the nearest equivalent here. The soak test measures whether it matters. + +## protect changes (separate PRs) + +- [edera-dev/protect#5078](https://github.com/edera-dev/protect/pull/5078), + `fix(libscap-rs): send syscall event category and flags as bit sets`, reads + the raw bits and formats them as libscap writes them (`EC_FILE | EC_SYSCALL`). + It fixes `evt.category` for new zones. +- [edera-dev/protect#5079](https://github.com/edera-dev/protect/pull/5079), + `fix(zone): include process exit in the default syscall set`, adds + `PPM_SC_SCHED_PROCESS_EXIT` to the zone default, which removes the need for + the plugin's copy (see the plan above). + +This plugin change does not depend on either one. + +## Verification + +- Unit tests (61 pass): return values with any category string; a replay of the + captured sequence (fork child in a PID namespace, `execve cat`, `open + /etc/shadow`); exited children leaving the table; running children surviving + their parent. The replay and reparent tests fail on the old code. +- Before release, on one Xen worker: + 1. Stress test, about 30 minutes: about 1,000 short-lived processes per + minute in an Edera pod. Pass: process-exit events arrive, and the table + stays near the live process count. + 2. PID reuse: run enough processes to reuse PIDs. Pass: Falco reports the new + program's name. + 3. 24-hour soak with normal workload. Pass: Falco memory is flat after + warm-up, and the `/etc/shadow` rule fires with the right file and + program. diff --git a/src/parsers.rs b/src/parsers.rs index c2c690c..2324eba 100644 --- a/src/parsers.rs +++ b/src/parsers.rs @@ -184,18 +184,17 @@ pub fn has_retval(evt: &ZoneKernelSyscallEvent) -> bool { return false; } // The event has a return value if: - // * it is a syscall event (or a syscall-like metaevent). // * it is an exit event. // * it has at least one parameter. Some exit events are not instrumented, see // `PPME_SOCKET_GETSOCKNAME_X` - if (evt.event_category.contains("SYSCALL") || evt.event_category.contains("EC_METAEVENT")) - && !is_enter(evt) - && !evt.event_params.is_empty() - { - return true; - } - - false + // + // libsinsp also requires the EC_SYSCALL or EC_METAEVENT category bit, read from its + // static event table. We deliberately don't consult `event_category`: it's a string + // the zone formats, and zones on Edera 1.13.x send "EC_TRACEPOINT" for every syscall + // event, which made every return value disappear (edera-dev/falco_plugin#154). + // Every exit event libscap defines is a syscall or metaevent exit whose first + // parameter is the return value, so the direction check is equivalent. + !is_enter(evt) && !evt.event_params.is_empty() } pub fn get_retval(evt: &ZoneKernelSyscallEvent) -> Option { @@ -439,6 +438,28 @@ mod tests { assert_eq!(syscall_failed(&evt), Some(false)); } + #[test] + fn retval_does_not_depend_on_category_string() { + // Zones on Edera 1.13.x label every syscall event "EC_TRACEPOINT" + // (edera-dev/falco_plugin#154). The return value must still be read. + for category in ["EC_TRACEPOINT", "", "garbage"] { + let mut evt = exit_event_with_retval(event_codes::PPME_SYSCALL_OPEN_X, 3); + evt.event_category = category.to_string(); + assert!(has_retval(&evt), "category {category:?}"); + assert_eq!(get_retval(&evt), Some(3), "category {category:?}"); + } + } + + #[test] + fn generic_exit_and_paramless_exit_have_no_retval() { + let evt = exit_event_with_retval(event_codes::PPME_GENERIC_X, 0); + assert!(!has_retval(&evt)); + + let mut evt = exit_event_with_retval(event_codes::PPME_SOCKET_GETSOCKNAME_X, 0); + evt.event_params.clear(); + assert!(!has_retval(&evt)); + } + #[test] fn failed_open_enoent_is_failed() { let evt = exit_event_with_retval(event_codes::PPME_SYSCALL_OPENAT_2_X, -2); diff --git a/src/proto/generated/protect/control/v1/protect.control.v1.rs b/src/proto/generated/protect/control/v1/protect.control.v1.rs index cac5eba..503de20 100644 --- a/src/proto/generated/protect/control/v1/protect.control.v1.rs +++ b/src/proto/generated/protect/control/v1/protect.control.v1.rs @@ -2401,6 +2401,11 @@ pub mod monitor_zone_kernel_event_reply { /// stream whose source it does not know. A stream keeps the source it started with. #[derive(Clone, PartialEq, Eq, Hash, ::prost::Message)] pub struct ZoneKernelEventStreamUpdate { + /// HAND-ADDED, not in the public proto: daemons up to Protect v1.13.x read the syscall + /// set only from this field (tag 2) and ignore `source`. Keep it when re-syncing protos; + /// see docs/design/zone-process-tracking.md. + #[prost(uint32, repeated, tag="2")] + pub enabled_syscalls: ::prost::alloc::vec::Vec, #[prost(oneof="zone_kernel_event_stream_update::Source", tags="3, 4")] pub source: ::core::option::Option, } diff --git a/src/proto/generated/protect/control/v1/protect.control.v1.serde.rs b/src/proto/generated/protect/control/v1/protect.control.v1.serde.rs index c92cfea..139f6d4 100644 --- a/src/proto/generated/protect/control/v1/protect.control.v1.serde.rs +++ b/src/proto/generated/protect/control/v1/protect.control.v1.serde.rs @@ -25914,6 +25914,8 @@ impl<'de> serde::Deserialize<'de> for ZoneKernelEventStreamUpdate { } } Ok(ZoneKernelEventStreamUpdate { + // HAND-ADDED: see the field in protect.control.v1.rs. + enabled_syscalls: Default::default(), source: source__, }) } diff --git a/src/source_plugin/client/enabled_syscalls.rs b/src/source_plugin/client/enabled_syscalls.rs index 295c181..4ab0645 100644 --- a/src/source_plugin/client/enabled_syscalls.rs +++ b/src/source_plugin/client/enabled_syscalls.rs @@ -2,8 +2,10 @@ use anyhow::{Context, Result, anyhow}; use aya::maps::{Array, Map, MapData}; use libscap_bindings::types::ppm_sc_code as syscall_codes; use log::{debug, info}; -use std::collections::HashSet; +use std::collections::{HashMap, HashSet}; use std::fs; +use strum::IntoEnumIterator; +use syscalls::Sysno; /// Falco (running on the host) does not pin the bpf map it creates on launch containing /// the syscalls its filtering engine cares about. @@ -92,15 +94,200 @@ pub fn read_enabled_syscalls() -> Result> { let array: Array<_, u8> = Array::try_from(map).context("couldn't read interesting syscalls map")?; - for syscall_id in 0u32..syscall_codes::PPM_SC_MAX as u32 { - if let Ok(value) = array.get(&syscall_id, 0) + // The map is indexed by the kernel's native syscall number, not by `ppm_sc` code. + for native_id in 0u32..array.len() { + if let Ok(value) = array.get(&native_id, 0) && value != 0 { - enabled.insert(syscall_id); + enabled.insert(native_id); } } - info!("read {} enabled syscalls", enabled.len()); + let ppm_sc = native_to_ppm_sc(&enabled); + info!( + "read {} enabled native syscalls, {} as ppm_sc codes", + enabled.len(), + ppm_sc.len() + ); - Ok(enabled) + Ok(ppm_sc) +} + +/// Converts native syscall numbers for this host's architecture to libscap `ppm_sc` codes. +/// libscap names each code after the kernel syscall (`PPM_SC_`), so match by name. +fn native_to_ppm_sc(native: &HashSet) -> HashSet { + let by_name: HashMap = syscall_codes::iter() + .map(|code| (code.to_string(), code as u32)) + .collect(); + + native + .iter() + .filter_map(|&native_id| { + let sysno = Sysno::new(native_id as usize)?; + let code = by_name + .get(&format!("PPM_SC_{}", sysno.name().to_uppercase())) + .copied(); + if code.is_none() { + debug!( + "no ppm_sc code for native syscall {native_id} ({})", + sysno.name() + ); + } + code + }) + .collect() +} + +/// The syscall set we ask zones for when not mirroring the host's set: the zone's own +/// default set (protect `crates/zone/src/kernel_events.rs`, unchanged from v1.13.1 to +/// v1.13.4) plus process exit, which the default set lacks. Without process exit, the +/// thread table never drops a thread, so it grows for as long as Falco runs and stale +/// entries mislabel reused PIDs. +pub fn default_zone_syscalls() -> Vec { + [ + syscall_codes::PPM_SC_ACCEPT, + syscall_codes::PPM_SC_ACCEPT4, + syscall_codes::PPM_SC_BIND, + syscall_codes::PPM_SC_BPF, + syscall_codes::PPM_SC_CAPSET, + syscall_codes::PPM_SC_CHDIR, + syscall_codes::PPM_SC_CHMOD, + syscall_codes::PPM_SC_CHROOT, + syscall_codes::PPM_SC_CLONE, + syscall_codes::PPM_SC_CLONE3, + syscall_codes::PPM_SC_CLOSE, + syscall_codes::PPM_SC_CONNECT, + syscall_codes::PPM_SC_COPY_FILE_RANGE, + syscall_codes::PPM_SC_CREAT, + syscall_codes::PPM_SC_DUP, + syscall_codes::PPM_SC_DUP2, + syscall_codes::PPM_SC_DUP3, + syscall_codes::PPM_SC_EVENTFD, + syscall_codes::PPM_SC_EVENTFD2, + syscall_codes::PPM_SC_EXECVE, + syscall_codes::PPM_SC_EXECVEAT, + syscall_codes::PPM_SC_FCHDIR, + syscall_codes::PPM_SC_FCHMOD, + syscall_codes::PPM_SC_FCHMODAT, + syscall_codes::PPM_SC_FCNTL, + syscall_codes::PPM_SC_FLOCK, + syscall_codes::PPM_SC_FORK, + syscall_codes::PPM_SC_INOTIFY_INIT, + syscall_codes::PPM_SC_INOTIFY_INIT1, + syscall_codes::PPM_SC_IOCTL, + syscall_codes::PPM_SC_KILL, + syscall_codes::PPM_SC_LINK, + syscall_codes::PPM_SC_LINKAT, + syscall_codes::PPM_SC_LISTEN, + syscall_codes::PPM_SC_MKDIR, + syscall_codes::PPM_SC_MKDIRAT, + syscall_codes::PPM_SC_MOUNT, + syscall_codes::PPM_SC_OPEN, + syscall_codes::PPM_SC_OPEN_BY_HANDLE_AT, + syscall_codes::PPM_SC_OPENAT, + syscall_codes::PPM_SC_OPENAT2, + syscall_codes::PPM_SC_PIPE, + syscall_codes::PPM_SC_PIPE2, + syscall_codes::PPM_SC_PRLIMIT64, + syscall_codes::PPM_SC_PTRACE, + syscall_codes::PPM_SC_QUOTACTL, + syscall_codes::PPM_SC_RECVFROM, + syscall_codes::PPM_SC_RECVMSG, + syscall_codes::PPM_SC_RENAME, + syscall_codes::PPM_SC_RENAMEAT, + syscall_codes::PPM_SC_RENAMEAT2, + syscall_codes::PPM_SC_RMDIR, + syscall_codes::PPM_SC_SECCOMP, + syscall_codes::PPM_SC_SENDMMSG, + syscall_codes::PPM_SC_SENDTO, + syscall_codes::PPM_SC_SETGID, + syscall_codes::PPM_SC_SETNS, + syscall_codes::PPM_SC_SETPGID, + syscall_codes::PPM_SC_SETRESGID, + syscall_codes::PPM_SC_SETRESUID, + syscall_codes::PPM_SC_SETRLIMIT, + syscall_codes::PPM_SC_SETSID, + syscall_codes::PPM_SC_SETSOCKOPT, + syscall_codes::PPM_SC_SETUID, + syscall_codes::PPM_SC_SHUTDOWN, + syscall_codes::PPM_SC_SIGNALFD, + syscall_codes::PPM_SC_SIGNALFD4, + syscall_codes::PPM_SC_SOCKET, + syscall_codes::PPM_SC_SOCKETPAIR, + syscall_codes::PPM_SC_SYMLINK, + syscall_codes::PPM_SC_SYMLINKAT, + syscall_codes::PPM_SC_TGKILL, + syscall_codes::PPM_SC_TIMERFD_CREATE, + syscall_codes::PPM_SC_TKILL, + syscall_codes::PPM_SC_UMOUNT2, + syscall_codes::PPM_SC_UNLINK, + syscall_codes::PPM_SC_UNLINKAT, + syscall_codes::PPM_SC_UNSHARE, + syscall_codes::PPM_SC_USERFAULTFD, + syscall_codes::PPM_SC_VFORK, + syscall_codes::PPM_SC_SCHED_PROCESS_EXIT, + ] + .into_iter() + .map(|code| code as u32) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn native_syscall_numbers_map_to_ppm_sc_codes() { + let native: HashSet = [libc::SYS_openat, libc::SYS_clone3, libc::SYS_execve] + .into_iter() + .map(|nr| nr as u32) + .collect(); + let expected: HashSet = [ + syscall_codes::PPM_SC_OPENAT, + syscall_codes::PPM_SC_CLONE3, + syscall_codes::PPM_SC_EXECVE, + ] + .into_iter() + .map(|code| code as u32) + .collect(); + assert_eq!(native_to_ppm_sc(&native), expected); + } + + // Every zone-default syscall this architecture has must survive the name match, so + // mirror mode never silently loses one of them. + #[test] + fn zone_default_syscalls_round_trip_through_native_numbers() { + let by_code: HashMap = syscall_codes::iter() + .map(|code| (code as u32, code.to_string())) + .collect(); + let mut checked = 0; + for code in default_zone_syscalls() { + let name = by_code[&code].trim_start_matches("PPM_SC_").to_lowercase(); + let Ok(sysno) = name.parse::() else { + continue; // not a syscall on this architecture (or a tracepoint) + }; + let native = HashSet::from([sysno.id() as u32]); + assert_eq!(native_to_ppm_sc(&native), HashSet::from([code]), "{name}"); + checked += 1; + } + // Guards against the name match quietly checking nothing. + assert!(checked >= 60, "only {checked} syscalls checked"); + } + + #[test] + fn default_zone_syscalls_track_process_lifecycle() { + let set = default_zone_syscalls(); + for code in [ + syscall_codes::PPM_SC_SCHED_PROCESS_EXIT, + syscall_codes::PPM_SC_CLONE, + syscall_codes::PPM_SC_CLONE3, + syscall_codes::PPM_SC_FORK, + syscall_codes::PPM_SC_VFORK, + syscall_codes::PPM_SC_EXECVE, + syscall_codes::PPM_SC_EXECVEAT, + ] { + assert!(set.contains(&(code as u32)), "{code:?} missing"); + } + assert_eq!(set.len(), 81); + } } diff --git a/src/source_plugin/client/mod.rs b/src/source_plugin/client/mod.rs index c5730f0..e1af9f4 100644 --- a/src/source_plugin/client/mod.rs +++ b/src/source_plugin/client/mod.rs @@ -12,6 +12,7 @@ use crate::proto::generated::protect::control::v1::{ use edera_client::{client::ControlClientProvider, dial::ControlDialAddress, events::EventStream}; mod zone_creation_watcher; +use libscap_bindings::types::ppm_sc_code as syscall_codes; use log::{debug, error, info, warn}; use std::collections::HashMap; use tokio::{ @@ -199,19 +200,25 @@ impl Client { // // By the time falco has finished plugin init and we see zones, the map we scan to get this // info should be there. + // + // Falco's map never includes tracepoints, so add process exit: without it the + // thread table never drops exited threads. let res = enabled_syscalls::read_enabled_syscalls() .ok() - .map(|syscalls| syscalls.into_iter().collect()); + .map(|mut syscalls| { + syscalls.insert(syscall_codes::PPM_SC_SCHED_PROCESS_EXIT as u32); + syscalls.into_iter().collect() + }); if res.is_none() { - // if we failed to read the map, fall back on None and the default set will be used + // if we failed to read the map, fall back on our default set warn!( "could not read falco's enabled syscalls eBPF map, is the modern eBPF host driver enabled?" ) } - res + res.or_else(|| Some(enabled_syscalls::default_zone_syscalls())) } else { - None + Some(enabled_syscalls::default_zone_syscalls()) }; // Get the stored client and sender @@ -246,6 +253,23 @@ impl Client { Ok(()) } + /// Daemons up to Protect v1.13.x read the syscall set only from the top-level + /// `enabled_syscalls` field; newer daemons read `source`. Send both, or released + /// daemons see an empty set and zones fall back to their default. + fn syscall_stream_request(zone_id: &str, syscalls: Vec) -> MonitorZoneKernelEventRequest { + MonitorZoneKernelEventRequest { + zone_id: zone_id.to_string(), + request: Some(zk_req::Request::Update(ZoneKernelEventStreamUpdate { + enabled_syscalls: syscalls.clone(), + source: Some(zone_kernel_event_stream_update::Source::Syscalls( + ZoneKernelSyscallSource { + enabled_syscalls: syscalls, + }, + )), + })), + } + } + async fn run_syscall_stream( mut client: ControlServiceClient, handler: mpsc::UnboundedSender, @@ -255,16 +279,7 @@ impl Client { zone_exit_tx: mpsc::UnboundedSender, ) -> Result> { debug!("listening for kernel events from zone {}", zone_id); - let event = MonitorZoneKernelEventRequest { - zone_id: zone_id.clone(), - request: Some(zk_req::Request::Update(ZoneKernelEventStreamUpdate { - source: Some(zone_kernel_event_stream_update::Source::Syscalls( - ZoneKernelSyscallSource { - enabled_syscalls: enabled_syscalls.unwrap_or_default(), - }, - )), - })), - }; + let event = Self::syscall_stream_request(&zone_id, enabled_syscalls.unwrap_or_default()); let stream = Self::input_stream_kernel_events(event, cancel_rx); debug!("built stream"); @@ -359,3 +374,35 @@ impl Client { None } } + +#[cfg(test)] +mod tests { + use super::*; + use prost::Message; + + /// The update message as Protect v1.13.x daemons decode it. + #[derive(Clone, PartialEq, ::prost::Message)] + struct LegacyUpdate { + #[prost(uint32, repeated, tag = "2")] + enabled_syscalls: Vec, + } + + #[test] + fn released_daemons_read_the_requested_syscalls() { + let syscalls = enabled_syscalls::default_zone_syscalls(); + let request = Client::syscall_stream_request("zone", syscalls.clone()); + let Some(zk_req::Request::Update(update)) = request.request else { + panic!("expected an update request"); + }; + let legacy = LegacyUpdate::decode(update.encode_to_vec().as_slice()).unwrap(); + assert_eq!(legacy.enabled_syscalls, syscalls); + assert_eq!( + update.source, + Some(zone_kernel_event_stream_update::Source::Syscalls( + ZoneKernelSyscallSource { + enabled_syscalls: syscalls + } + )) + ); + } +} diff --git a/src/threadstate.rs b/src/threadstate.rs index 4563048..0eb244e 100644 --- a/src/threadstate.rs +++ b/src/threadstate.rs @@ -1775,6 +1775,7 @@ impl ZoneInfo { ) .try_into() .ok() + .filter(|&reaper: &u64| reaper > 0) } else { None } @@ -1783,9 +1784,10 @@ impl ZoneInfo { // Move any children of this thread to the reaper thread, if the event defines one. // Otherwise, orphan them. Note that as per libsinsp you cannot derive reaper thread // info from /proc scans, so this event is very much best effort/the only place to get reaper info + let exiting_tid = event.thread_id; self.with_mut_threadsnaps_ctx(|threadsnaps| { for tinfo in threadsnaps.values_mut() { - if tinfo.ptid.is_some() && tinfo.ptid == tinfo.tid { + if tinfo.ptid == Some(exiting_tid) { tinfo.ptid = reaper_tid; // if reapertid is Some(u64), set that. Otherwise, set to None/orphan. } } @@ -3738,6 +3740,314 @@ mod tests { assert!(tinfo.args.contains("echo test")); } + /// Builds an event the way zones on Edera 1.13.x send it: every syscall event + /// is labelled "EC_TRACEPOINT" (edera-dev/falco_plugin#154). + fn zone_event( + thread_id: u64, + event_type: event_codes, + timestamp: u64, + params: Vec, + ) -> ZoneKernelSyscallEvent { + ZoneKernelSyscallEvent { + zone_id: "test-zone".to_string(), + thread_id, + timestamp, + event_type: event_type as u32, + event_name: format!("{event_type:?}"), + event_category: "EC_TRACEPOINT".to_string(), + event_params: params, + ..Default::default() + } + } + + fn i64p(v: i64) -> ZoneKernelEventParam { + create_param(0, v.to_ne_bytes().to_vec(), v.to_string()) + } + fn u64p(v: u64) -> ZoneKernelEventParam { + create_param(0, v.to_ne_bytes().to_vec(), v.to_string()) + } + fn u32p(v: u32) -> ZoneKernelEventParam { + create_param(0, v.to_ne_bytes().to_vec(), v.to_string()) + } + fn strp(s: &str) -> ZoneKernelEventParam { + let mut data = s.as_bytes().to_vec(); + data.push(0); + create_param(0, data, s.to_string()) + } + fn emptyp() -> ZoneKernelEventParam { + create_param(0, vec![], String::new()) + } + + /// fork() as seen by the child, which runs in a PID namespace: [res, exe, args, tid, + /// pid, ptid, cwd, fdlimit, pgft_maj, pgft_min, vm_size, vm_rss, vm_swap, comm, + /// cgroups, flags, uid, gid, vtid, vpid, pidns_init_start_ts] + fn fork_child_event(child: u64, parent: u64, vtid: i64, ts: u64) -> ZoneKernelSyscallEvent { + zone_event( + child, + event_codes::PPME_SYSCALL_FORK_20_X, + ts, + vec![ + i64p(0), + strp("/bin/styrolite"), + strp("styrolite"), + i64p(child as i64), + i64p(child as i64), + i64p(parent as i64), + emptyp(), + i64p(1024), + u64p(0), + u64p(0), + u32p(0), + u32p(0), + u32p(0), + strp("styrolite"), + emptyp(), + u32p(ppm_consts::PPM_CL_CHILD_IN_PIDNS), + u32p(0), + u32p(0), + i64p(vtid), + i64p(vtid), + u64p(0), + ], + ) + } + + /// Process exit: [status, ret, sig, core, reaper_tid] + fn procexit_event(tid: u64, reaper: i64, ts: u64) -> ZoneKernelSyscallEvent { + zone_event( + tid, + event_codes::PPME_PROCEXIT_1_E, + ts, + vec![ + i64p(0), + i64p(0), + create_param(0, vec![0], String::new()), + create_param(0, vec![0], String::new()), + i64p(reaper), + ], + ) + } + + fn snapshot_with_parent(state: &mut ThreadState) { + let mut parent = create_thread_info(100, 100); + parent.exe = "/bin/styrolite".to_string(); + let mut thread_info = HashMap::new(); + thread_info.insert(100, parent); + // An unrelated process whose exit triggers the dead-thread purge. + thread_info.insert(200, create_thread_info(200, 200)); + state.init_zone_with_snap(ZoneKernelThreadSnapshotEvent { + zone_id: "test-zone".to_string(), + thread_info, + zone_boot_epoch: 0, + }); + } + + fn known(state: &ThreadState, tid: u64) -> bool { + state + .with_threadinfo("test-zone", &tid, |_| Some(())) + .is_some() + } + + #[test] + fn exited_processes_leave_the_thread_table() { + let mut state = ThreadState::default(); + snapshot_with_parent(&mut state); + + for (i, child) in (101..111u64).enumerate() { + let ts = 1_000 + i as u64 * 10; + state + .process_event(&fork_child_event(child, 100, child as i64, ts)) + .unwrap(); + state + .process_event(&procexit_event(child, 0, ts + 1)) + .unwrap(); + } + // The last exiting thread stays until the next exit, for rule context. + state.process_event(&procexit_event(200, 0, 5_000)).unwrap(); + + let table_len = state + .with_zoneinfo("test-zone", |z| Some(z.seen_threadsnaps.len())) + .unwrap(); + // 100 is alive; 200 just exited and stays until the next exit. + assert_eq!(table_len, 2, "every exited child should be gone"); + } + + #[test] + fn children_of_an_exited_process_move_to_the_reaper_and_stay() { + let mut state = ThreadState::default(); + snapshot_with_parent(&mut state); + state + .process_event(&fork_child_event(101, 100, 6, 1_000)) + .unwrap(); + + state.process_event(&procexit_event(100, 1, 2_000)).unwrap(); + assert_eq!( + state.with_threadinfo("test-zone", &101, |t| t.ptid), + Some(1) + ); + + // A later exit purges the dead parent; its running child must survive. + state.process_event(&procexit_event(200, 0, 3_000)).unwrap(); + assert!(!known(&state, 100)); + assert!(known(&state, 101)); + } + + /// execve(): [res, exe, args, tid, pid, ptid, cwd, fdlimit, pgft_maj, pgft_min, + /// vm_size, vm_rss, vm_swap, comm, ...optional params left empty] + fn execve_event(tid: u64, parent: u64, exe: &str, ts: u64) -> ZoneKernelSyscallEvent { + let comm = exe.rsplit('/').next().unwrap_or(exe); + let mut params = vec![ + i64p(0), + strp(exe), + strp(comm), + i64p(tid as i64), + u64p(tid), + u64p(parent), + emptyp(), + i64p(1024), + u64p(0), + u64p(0), + u32p(0), + u32p(0), + u32p(0), + strp(comm), + ]; + params.resize_with(30, emptyp); + zone_event(tid, event_codes::PPME_SYSCALL_EXECVE_19_X, ts, params) + } + + fn exe_of(state: &ThreadState, tid: u64) -> Option { + state.with_threadinfo("test-zone", &tid, |t| Some(t.exe.clone())) + } + + #[test] + fn reused_pid_reports_the_new_program() { + let mut state = ThreadState::default(); + snapshot_with_parent(&mut state); + + state + .process_event(&fork_child_event(101, 100, 6, 1_000)) + .unwrap(); + state + .process_event(&execve_event(101, 100, "/bin/cat", 2_000)) + .unwrap(); + state.process_event(&procexit_event(101, 0, 3_000)).unwrap(); + state.process_event(&procexit_event(200, 0, 4_000)).unwrap(); + assert!(!known(&state, 101), "exited process must be purged"); + + // The zone hands PID 101 to a new process, seconds later. + state + .process_event(&fork_child_event(101, 100, 7, 10_000_000_000)) + .unwrap(); + state + .process_event(&execve_event(101, 100, "/bin/ls", 10_000_001_000)) + .unwrap(); + assert_eq!(exe_of(&state, 101).as_deref(), Some("/bin/ls")); + } + + #[test] + fn reused_pid_replaces_a_stale_entry_when_the_exit_was_missed() { + let mut state = ThreadState::default(); + snapshot_with_parent(&mut state); + + state + .process_event(&fork_child_event(101, 100, 6, 1_000)) + .unwrap(); + state + .process_event(&execve_event(101, 100, "/bin/cat", 2_000)) + .unwrap(); + + // No exit event for the old 101 (e.g. dropped); a new process gets PID 101 + // more than two seconds after the old one was cloned. + state + .process_event(&fork_child_event(101, 100, 7, 10_000_000_000)) + .unwrap(); + assert_eq!(exe_of(&state, 101).as_deref(), Some("/bin/styrolite")); + state + .process_event(&execve_event(101, 100, "/bin/ls", 10_000_001_000)) + .unwrap(); + assert_eq!(exe_of(&state, 101).as_deref(), Some("/bin/ls")); + } + + /// Replays what w4 showed for `kubectl exec ... cat /etc/shadow` in an Edera zone: + /// a known process forks a child inside a PID namespace (child-side clone event, + /// retval 0), the child execs `cat`, and `cat` opens /etc/shadow. With every event + /// labelled "EC_TRACEPOINT", the plugin used to ignore all return values, so the + /// child was never added, and the open recorded no file name. + #[test] + fn new_process_in_pidns_is_tracked_despite_tracepoint_category() { + let mut state = ThreadState::default(); + snapshot_with_parent(&mut state); + + let fork_child = fork_child_event(101, 100, 6, 1_000); + state.process_event(&fork_child).unwrap(); + assert!( + state + .with_threadinfo("test-zone", &101, |_| Some(())) + .is_some(), + "child of fork() must be added to the thread table" + ); + + // execve(): [res, exe, args, tid, pid, ptid, cwd, fdlimit, pgft_maj, pgft_min, + // vm_size, vm_rss, vm_swap, comm, ...optional params left empty] + let mut execve_params = vec![ + i64p(0), + strp("/bin/cat"), + strp("/etc/shadow"), + i64p(101), + u64p(101), + u64p(100), + emptyp(), + i64p(1024), + u64p(0), + u64p(0), + u32p(0), + u32p(0), + u32p(0), + strp("cat"), + ]; + execve_params.resize_with(30, emptyp); + let execve = zone_event( + 101, + event_codes::PPME_SYSCALL_EXECVE_19_X, + 2_000, + execve_params, + ); + state.process_event(&execve).unwrap(); + assert_eq!( + state + .with_threadinfo("test-zone", &101, |t| Some(t.exe.clone())) + .as_deref(), + Some("/bin/cat") + ); + + // open(): [fd, name, flags, mode, dev, ino] + let mut fd_param = i64p(3); + fd_param.param_type = param_type::PT_FD as u32; + let open = zone_event( + 101, + event_codes::PPME_SYSCALL_OPEN_X, + 3_000, + vec![ + fd_param, + strp("/etc/shadow"), + u32p(ppm_consts::PPM_O_RDONLY), + u32p(0), + u32p(0), + u64p(0), + ], + ); + state.process_event(&open).unwrap(); + + let fdinfo = state + .with_zoneinfo("test-zone", |z| z.get_event_fdinfo(&open)) + .expect("open must record fd 3 for the new process"); + match fdinfo.info.and_then(|i| i.info_type) { + Some(InfoType::RegularFile(f)) => assert_eq!(f.name, "/etc/shadow"), + other => panic!("expected a regular file, got {other:?}"), + } + } + #[test] fn mark_fd_dropped() { let mut state = ThreadState::default();