Skip to content
Documentation

EndpointSecurity sensor

This page is published from the agent repository. Edit on GitHub View source

sensor-macos (crates/sensors/macos/endpoint-security) is the primary macOS sensor. It is an EndpointSecurity (ES) client, the supported successor to kexts, kauth and OpenBSM. It subscribes to NOTIFY events only: nothing is blocked (no AUTH responses yet).

Layers

flowchart LR
    ES["libEndpointSecurity<br/>es_message_t"] --> SHIM["C shim es_shim.c<br/>flatten to syn_es_event"]
    SHIM -->|"callback (ES dispatch thread)"| FFI["ffi.rs<br/>SynEsEvent::to_raw (copies)"]
    FFI --> RAW["raw.rs<br/>RawEsEvent (owned)"]
    RAW --> NORM["normalize.rs<br/>normalize()"]
    NORM --> SINK[(EventSink)]
LayerFilePlatform
C shim: client creation, subscription, flattening, filteringshim/es_shim.c, shim/es_shim.hmacOS, built by build.rs with -fblocks and warnings as errors
Rust mirror of the shim structssrc/ffi.rsmacOS
Owned recordssrc/raw.rsall
Mapping to schemasrc/normalize.rsall
Sensor implementationsrc/sensor.rsmacOS

ffi.rs must stay field-for-field identical to es_shim.h. The shim's syn_es_event is a flat struct rather than a union: a few hundred spare bytes per transient event remove any union-offset risk on the Rust side.

String fields in a syn_es_event point into the ES message or into shim scratch buffers and are valid only during the callback. to_raw copies everything it keeps before returning.

Subscriptions

The Rust side asks for groups; the shim owns the mapping to concrete es_event_type_t values (syn_es_subscribe). Subscribing to an event type the running OS does not know fails the whole es_subscribe call, so newer families are gated with __builtin_available and silently skipped on older hosts.

GroupES eventsEmitsOS
execNOTIFY_EXECExec with argv, parent image and code-signing stateall
fileNOTIFY_OPEN, CREATE, RENAME, UNLINK, MMAPFileOpen, FileRename, FileDeleteall
persistenceNOTIFY_BTM_LAUNCH_ITEM_ADDFileOpen + FLAG_PERSISTENCE_BTM_ARTIFACT13+
sessionsNOTIFY_OPENSSH_LOGIN, LOGIN_LOGIN, LW_SESSION_LOGINAuth13+
provenanceNOTIFY_SETEXTATTR (quarantine only)FileQuarantineall
mountNOTIFY_MOUNT, UNMOUNTMountall
tamperNOTIFY_SIGNAL (to ES clients only)Signalall
xpcNOTIFY_XPC_CONNECTXpcConnect14+

Filtering done in the shim, before Rust sees anything:

  • mmap: only PROT_WRITE on a MAP_SHARED mapping is forwarded, the one mmap case that changes a file. Everything else (dyld mapping libraries) is volume without signal.
  • setextattr: only com.apple.quarantine. The shim then reads back the quarantine string and com.apple.metadata:kMDItemWhereFroms with getxattr at event time. Either can be absent when the read races the writer.
  • signal: only signals whose target is an ES client (this agent, other security tools). meta describes the sender; the target's pid and image are in the event.
  • argv: at most 128 entries (SYN_ES_MAX_ARGV); the real count is passed so truncation is flagged.

Normalization

normalize maps each RawEsEvent to one schema event.

  • Open flags. FileOpenEvent::flags is platform-native by contract, but this sensor emits POSIX O_* values (shared with Linux for the bits schema defines) so schema::has_write_intent and the path+write rules work unchanged. The kernel fflag (FREAD/FWRITE) is translated in open_fflag_to_flags, never passed through.
  • Signature at the source. ES provides the kernel's code-signing flags on each exec. signature_from_cs_flags fills ExecEvent::signature: no flags is Unsigned, CS_VALID set is Valid, flags without CS_VALID is Invalid (signed, but the kernel no longer considers it valid, for example after in-memory tampering). This is the kernel's dynamic validity view, not a full chain evaluation. The signing id, team id and platform-binary bit are copied into RawEsEvent::Exec but not carried into the schema event yet.
  • argv. The shim bounds the entry count, but macOS allows about 1 MiB of arguments, so schema::bound_argv also applies a byte budget; either cut sets cmdline_truncated. cmdline is the space-joined argv. argv_omitted_bytes is None (ES reports a count of dropped arguments, not their bytes).
  • Parent. parent_image_path is the pre-exec image of the exec-ing process, the usual lineage view for fork+exec.
  • comm is the basename of the executable path (the kernel's p_comm is cut at 16 bytes).
  • User is User::Unix with the effective uid and gid from the audit token.
  • Quarantine. The agent field of the quarantine string (flags;timestamp;agent;uuid) is extracted (quarantine_agent), and the origin and referrer URLs are parsed from the binary plist in kMDItemWhereFroms with the plist crate (wherefroms_urls). Unparseable bytes give no URLs but never drop the event.
  • BTM. The item type is kept as its raw value plus a decoded view (BtmItemType), so a new SDK value degrades to Other instead of being lost. The launchd plist's executable, when resolved, is the persistence payload.

Threading and lifecycle

MacosSensor::run:

  1. Leaks a boxed callback context holding the sink and creates the client with syn_es_client_new. ES spins up its own dispatch threads, which call on_event.
  2. Mutes the agent's own process (es_mute_process) so its spool and alert writes do not feed back. A failure is logged and not fatal.
  3. Subscribes to all eight groups.
  4. Parks on a condvar until StopHandle::stop, using wait_timeout(500 ms) so a missed notify can only delay shutdown by one tick.
  5. Destroys the client on the same thread that created it (a libEndpointSecurity requirement). Destroy waits for in-flight handlers, after which the context is freed.

The sink is called directly from ES dispatch threads, so it must be thread-safe (EventSink takes &self). on_event wraps its body in catch_unwind: a panic must not unwind into C, which would abort the agent. A panicking event is logged and dropped. The sensor adds no queue of its own, so a slow sink delays ES delivery to this client.

Errors

MacosSensorError maps the es_new_client result to the action that fixes it:

ResultVariantFix
3NotEntitledsign with com.apple.developer.endpoint-security.client
4NotPermittedgrant Full Disk Access (TCC)
5NotPrivilegedrun as root
otherClientCreation(n)
es_subscribe failedSubscribe(n)

Unsigned development builds with a restricted entitlement are killed at exec by amfid before TCC is involved, even with SIP disabled; a lab VM needs both SIP disabled and amfi_get_out_of_my_way=1.

Capabilities

capabilities claims exec, file, auth, user attribution and parent lineage. connect_events is false: network comes from the Network Extension. Auth families need macOS 13, which is below the supported floor, so the static claim holds where the agent ships.

Testing

  • normalize.rs and raw.rs unit tests run on every OS (flag translation, signature mapping, argv bounds, quarantine string and plist parsing, BTM type decoding).
  • The shim builds with warnings as errors against the SDK on macOS CI.
  • End-to-end runs with an entitled client are not automated; see #78.

Gaps

  • NOTIFY only: no AUTH-based blocking.
  • Not subscribed yet: injection and tamper process events (task ports, ptrace, CS invalidation, #37), security-subsystem events such as XProtect, Gatekeeper overrides, TCC changes (#38), and anti-forensics file operations such as quarantine removal, timestamp changes and hidden flags (#20).
  • Signing id and team id are collected but not emitted.
  • No silence heartbeat for this sensor (#34).