Skip to content

fix(shm): copy shared memory out instead of holding references into it; ci: --locked - #288

Open
SteveGilvarry wants to merge 2 commits into
ZoneMinder:masterfrom
SteveGilvarry:fix/shm-volatile-ci-locked
Open

SteveGilvarry wants to merge 2 commits into
ZoneMinder:masterfrom
SteveGilvarry:fix/shm-volatile-ci-locked

Conversation

@SteveGilvarry

Copy link
Copy Markdown
Member

Two small hardening changes left over from an earlier security review whose other findings have since landed on master.

Shared memory (src/zm_shm.rs)

zmc and zma write a monitor's segment while zm-api reads it. MonitorShm handed out &SharedData and &mut TriggerData pointing into the mapping. A reference promises Rust the bytes won't change while it lives, which another process breaks whenever it writes: undefined behaviour, and room for the optimiser to cache or merge reads.

  • Reads take a read_volatile copy of the struct. Every field is an integer, float or byte array, so any bytes are a valid value.
  • Writes go field by field with write_volatile through a pointer from as_mut_ptr().
  • The release fence before the trigger state is unchanged, so score, cause and texts still land before TRIGGER_ON. cancel_alarm now writes the state last as well.
  • The unused shared_data_mut is removed.
  • New tests for cancel_alarm and disable_triggers, which had none, and for the padding helper.

CI (.github/workflows/test.yml)

Adds --locked to the cargo test, cargo clippy and cargo run steps. Without it cargo silently updates Cargo.lock when it's out of step with Cargo.toml, so CI can pass on dependency versions nobody committed, and cargo-audit (which reads Cargo.lock) checks different ones.

Testing

  • cargo fmt --all -- --check: clean
  • cargo clippy --locked --all-targets --all-features -- -D warnings: clean
  • cargo test --locked --all-features: 1436 passed, 0 failed. The DB-backed #[ignore] tests were not run locally.

🤖 Generated with Claude Code

SteveGilvarry and others added 2 commits November 29, 2026 17:12
zmc and zma write a monitor's segment while zm-api reads it. MonitorShm
handed out &SharedData and &mut TriggerData into the mapping, which tells
Rust the bytes cannot change while the reference lives: undefined
behaviour once another process writes, and room for the optimiser to cache
or merge reads.

Reads now take a volatile copy of the struct; writes go field by field with
write_volatile through a pointer from as_mut_ptr. The release fence before
the trigger state stays, so score, cause and texts still land before
TRIGGER_ON. Cancel now writes the state last too. The unused
shared_data_mut is gone.

Adds tests for cancel and disable, which had none.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Without it cargo quietly updates Cargo.lock when it is out of step with
Cargo.toml, so CI can pass on dependency versions nobody committed (and
cargo-audit, which reads Cargo.lock, checks different ones).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant