fix(systemd_hardening): disable and mask the socket of a listed service too #314

Merged
Defame1297 merged 1 commits from fix/308-systemd-hardening-sockets into main 2026-11-23 12:13:56 +00:00
Collaborator

What and why

Closes #308

systemd_hardening looked up and acted on <name>.service only. A service whose socket unit stays active is started again by the next connection to its port, so disabling or masking only the service does not make it unreachable (rpcbind listens on port 111 through rpcbind.socket). The role now looks up every listed name as a socket and as a service in the same single systemctl show, and acts on each installed unit, socket first. A name may carry a .service or .socket suffix.

Behaviour change: a socket that belongs to a listed name (with the default lists: rpcbind.socket, and avahi-daemon.socket and cups.socket where they exist) is now stopped and disabled, or masked, where before it was left alone. A name with only a service, only a socket, or neither is fine. The README notes that systemctl disable does nothing for a static unit.

Verified

  • The systemd behaviour the issue describes, on an Ubuntu 24.04 VM (systemd 255) with a throwaway socket and service pair: stopping the service alone leaves the port listening and the next connection restarts it; stopping the socket first closes the port (ConnectionRefused). This used systemctl directly, not the role.
  • The lookup and loop expressions, rendered with Ansible's own engine: suffix variants and duplicates fold into one name, the socket comes first, only installed units remain, and the positional pairing keeps the right names.
  • All commit-stage hooks pass (yamllint, ansible-lint, REUSE, changelog lint, Conventional Commit). --syntax-check passes on the converge, prepare and verify playbooks.
  • An independent review of the diff found one real problem (a stub socket with no loaded service cannot be started), fixed before this push.

Not verified

  • Molecule (CI only): the stub sockets and the new verify assertions have not run on the image. The stubs are a socket and service pair for rpcbind and telnet, a service only for cups, and a socket only for bluetooth.
  • The role itself against a real rpcbind, and whether enabled: false on a service also disables its socket for the next boot.
  • ansible-test sanity ran in the pre-push hook only.

  • I confirm third-party material is recorded in NOTICE and REUSE.toml, or there is none.

Assisted-by: Claude Code

## What and why Closes #308 `systemd_hardening` looked up and acted on `<name>.service` only. A service whose socket unit stays active is started again by the next connection to its port, so disabling or masking only the service does not make it unreachable (`rpcbind` listens on port 111 through `rpcbind.socket`). The role now looks up every listed name as a socket and as a service in the same single `systemctl show`, and acts on each installed unit, socket first. A name may carry a `.service` or `.socket` suffix. **Behaviour change:** a socket that belongs to a listed name (with the default lists: `rpcbind.socket`, and `avahi-daemon.socket` and `cups.socket` where they exist) is now stopped and disabled, or masked, where before it was left alone. A name with only a service, only a socket, or neither is fine. The README notes that `systemctl disable` does nothing for a static unit. ## Verified - The systemd behaviour the issue describes, on an Ubuntu 24.04 VM (systemd 255) with a throwaway socket and service pair: stopping the service alone leaves the port listening and the next connection restarts it; stopping the socket first closes the port (ConnectionRefused). This used `systemctl` directly, not the role. - The lookup and loop expressions, rendered with Ansible's own engine: suffix variants and duplicates fold into one name, the socket comes first, only installed units remain, and the positional pairing keeps the right names. - All commit-stage hooks pass (yamllint, ansible-lint, REUSE, changelog lint, Conventional Commit). `--syntax-check` passes on the converge, prepare and verify playbooks. - An independent review of the diff found one real problem (a stub socket with no loaded service cannot be started), fixed before this push. ## Not verified - Molecule (CI only): the stub sockets and the new verify assertions have not run on the image. The stubs are a socket and service pair for `rpcbind` and `telnet`, a service only for `cups`, and a socket only for `bluetooth`. - The role itself against a real `rpcbind`, and whether `enabled: false` on a service also disables its socket for the next boot. - `ansible-test sanity` ran in the pre-push hook only. --- - [x] I confirm third-party material is recorded in `NOTICE` and `REUSE.toml`, or there is none. Assisted-by: Claude Code
Dragonish added 1 commit 2026-11-23 11:38:32 +00:00
fix(systemd_hardening): disable and mask the socket of a listed service too
Build / build (pull_request) Successful in 18s
Lint / lint (pull_request) Successful in 1m11s
Molecule / molecule (pull_request) Successful in 1m2s
f256c1c7e5
Look up every listed name as a socket and as a service and act on each
installed unit, the socket first. A service whose socket stays active is
started again by the next connection to its port.

Assisted-by: Claude Code
Defame1297 merged commit 0bdb0bf33f into main 2026-11-23 12:13:56 +00:00
Defame1297 deleted branch fix/308-systemd-hardening-sockets 2026-11-23 12:13:56 +00:00
Dragonish referenced this issue from a commit 2026-11-23 12:38:06 +00:00
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#314