feat(hardening-basic): add nullable inotify sysctl limits #60

Merged
claudiomaradonna merged 2 commits from feat/inotify-sysctl into main 2026-11-27 22:45:54 +01:00
Collaborator

Why

hardening-basic writes /etc/sysctl.d/99-hardening.conf from fixed
keys, then applies it and reads every key back from the kernel. A
container host whose workloads watch many files can exhaust the kernel's
per-user inotify defaults, and the role had no way to raise them: a value
set by hand in another drop-in would be neither managed nor checked.

What changed

Two variables, hardening_sysctl_fs_inotify_max_user_instances and
hardening_sysctl_fs_inotify_max_user_watches, default to null. When
set, each adds its fs.inotify.* line to 99-hardening.conf, where the
existing apply-and-verify step applies and reads it back. When unset, the
drop-in renders byte-for-byte as before, so hosts that do not set them
see no change. They follow the role's other nullable sysctls, such as
hardening_sysctl_vm_mmap_rnd_bits, and the role README documents them.

The workstation molecule scenario sets both and asserts their lines;
the default scenario asserts no fs.inotify line when unset.

How to verify

Prerequisites: Setup and rootless podman.

  1. ./tests/molecule/run.sh roles/hardening-basic passes on the branch;
    on main the workstation scenario's "Assert set inotify limits land
    in the drop-in" fails, while the default pin passes on both.
  2. Render main's and the branch's template with the role defaults and
    the variables unset, then diff: no difference. With
    hardening_sysctl_fs_inotify_max_user_instances=4096 and
    hardening_sysctl_fs_inotify_max_user_watches=1048576 the branch adds
    only those two lines.

Run: scenario 2, as written, with ansible's template module (no diff
unset; two added lines set). make check passed on every commit and on
the tip. Untested: scenario 1 and make test, because the authoring
session had no podman; expected to pass.

Considerations

Each inotify watch in use costs about 1 KB of kernel memory, so
1048576 watches can reach about 1 GB if all are used; the defaults stay
off for that reason.

## Why `hardening-basic` writes `/etc/sysctl.d/99-hardening.conf` from fixed keys, then applies it and reads every key back from the kernel. A container host whose workloads watch many files can exhaust the kernel's per-user inotify defaults, and the role had no way to raise them: a value set by hand in another drop-in would be neither managed nor checked. ## What changed Two variables, `hardening_sysctl_fs_inotify_max_user_instances` and `hardening_sysctl_fs_inotify_max_user_watches`, default to `null`. When set, each adds its `fs.inotify.*` line to `99-hardening.conf`, where the existing apply-and-verify step applies and reads it back. When unset, the drop-in renders byte-for-byte as before, so hosts that do not set them see no change. They follow the role's other nullable sysctls, such as `hardening_sysctl_vm_mmap_rnd_bits`, and the role README documents them. The `workstation` molecule scenario sets both and asserts their lines; the `default` scenario asserts no `fs.inotify` line when unset. ## How to verify Prerequisites: [Setup](CONTRIBUTING.md#setup) and rootless podman. 1. `./tests/molecule/run.sh roles/hardening-basic` passes on the branch; on main the `workstation` scenario's "Assert set inotify limits land in the drop-in" fails, while the `default` pin passes on both. 2. Render main's and the branch's template with the role defaults and the variables unset, then diff: no difference. With `hardening_sysctl_fs_inotify_max_user_instances=4096` and `hardening_sysctl_fs_inotify_max_user_watches=1048576` the branch adds only those two lines. Run: scenario 2, as written, with ansible's `template` module (no diff unset; two added lines set). `make check` passed on every commit and on the tip. Untested: scenario 1 and `make test`, because the authoring session had no podman; expected to pass. ## Considerations Each inotify watch in use costs about 1 KB of kernel memory, so 1048576 watches can reach about 1 GB if all are used; the defaults stay off for that reason.
feat(hardening-basic): add nullable inotify sysctl limits
All checks were successful
check / check (pull_request) Successful in 21m56s
check / check (push) Successful in 20m33s
d7934291b8
A container host can exhaust the kernel's per-user inotify defaults.
Two variables, null by default, let such a host raise
fs.inotify.max_user_instances and max_user_watches in 99-hardening.conf,
where the existing apply-and-verify step reads them back. Hosts that
set neither render the same drop-in as before.
claudiomaradonna deleted branch feat/inotify-sysctl 2026-11-27 22:46:05 +01:00
First-time contributor

Review ready: d7934291b8

Review ready: d7934291b8150fa02732647778a4176624f4608e
aramaki approved these changes 2026-11-27 22:56:24 +01:00
aramaki left a comment

Review: approved

Reviewed d7934291b815: 0 blocking and 1 minor findings.

Reproduced

  • Rendered main's and the branch's template with the role defaults and both variables unset: byte-for-byte identical; setting 4096/1048576 adds exactly the two fs.inotify lines. Both molecule assertions match these renders.
  • make setup and make check pass on the tip (exit 0). The pin commit bac9cf1 lands before the feature commit, and main's render holds no inotify line, so the default pin passes on main.
  • make test, the molecule scenarios, and check_tag_propagation.sh were not run: podman is unavailable here, the same gap the PR discloses. The change adds no tasks or includes, so tag rules are untouched.

Questions and details

  • Is the read-back's coverage of the new keys (declared keys are derived from the rendered files) worth a molecule case that sets one variable only? The mmap pair has none either, so parity is defensible.
## Review: approved Reviewed `d7934291b815`: 0 blocking and 1 minor findings. **Reproduced** - Rendered main's and the branch's template with the role defaults and both variables unset: byte-for-byte identical; setting 4096/1048576 adds exactly the two fs.inotify lines. Both molecule assertions match these renders. - make setup and make check pass on the tip (exit 0). The pin commit bac9cf1 lands before the feature commit, and main's render holds no inotify line, so the default pin passes on main. - make test, the molecule scenarios, and check_tag_propagation.sh were not run: podman is unavailable here, the same gap the PR discloses. The change adds no tasks or includes, so tag rules are untouched. **Questions and details** - Is the read-back's coverage of the new keys (declared keys are derived from the rendered files) worth a molecule case that sets one variable only? The mmap pair has none either, so parity is defensible.
@ -74,0 +76,4 @@
{% endif %}
{% if hardening_sysctl_fs_inotify_max_user_watches is not none %}
fs.inotify.max_user_watches={{ hardening_sysctl_fs_inotify_max_user_watches }}
{% endif %}
First-time contributor

Minor: When both values are set, the watches line sits flush against the net.ipv6 group; every other rendered group is blank-line separated. An empty line inside a combined is-not-none guard would restore it without changing the unset render.

**Minor:** When both values are set, the watches line sits flush against the net.ipv6 group; every other rendered group is blank-line separated. An empty line inside a combined is-not-none guard would restore it without changing the unset render.
Sign in to join this conversation.
No description provided.