fix(systemd_hardening): disable and mask the socket of a socket-activated service too #308

Closed
opened 2026-11-23 08:49:46 +00:00 by Dragonish · 0 comments
Collaborator

Role

systemd_hardening

Release tag

v1.10.1

ansible-core version

2.21.6

Platform

Ubuntu 24.04

Minimal playbook

- hosts: all
  vars:
    systemd_hardening_system_type: ubuntu_vm
  roles:
    - role: mortis.kamino.systemd_hardening

On a host where a unit from the default disable list is socket-activated and its socket unit is enabled, for example rpcbind (the stock Ubuntu package ships rpcbind.service and rpcbind.socket, and the socket listens on 0.0.0.0:111). avahi-daemon and cups are, to my knowledge, socket-activated on Ubuntu too (not checked for this issue).

Expected behaviour

After the role ran, a service in systemd_hardening_disable_services is not reachable: not running, and not started again by a connection to its port. The README says the role "disables and masks risky services", so a unit that a connection can start again is not disabled in any useful sense. The same goes for a name in systemd_hardening_mask_services that has a socket unit.

Actual behaviour

The role looks up and acts on <name>.service only (__systemd_hardening_probe_units appends .service; disable_services.yml and mask_services.yml act on the service). Stopping a service whose socket unit is still active does not close the port: systemd starts the service again on the next connection.

I reproduced the systemd behaviour with a throwaway socket and service pair on an Ubuntu 24.04 VM (systemd 255), units in /run/systemd/system, listening on 127.0.0.1:23111, removed afterwards:

== stop the .service only (what the role does)
  after 'stop service':      socket=active   service=inactive listening=1
  next connection:           pid=89006       socket=active   service=active listening=1

== stop the socket first, then the service
  after 'stop socket, stop service': socket=inactive service=inactive listening=0
  next connection:           connect failed: ConnectionRefusedError

systemctl stop <name>.service even prints Stopping '<name>.service', but its triggering units are still active: <name>.socket.

Proposal

For every name in the disable and mask lists, look up both <name>.socket and <name>.service (one systemctl show, as today, with the two unit types per name), and act on every one that is installed, socket first:

  • disable list: enabled: false, state: stopped on the socket, then on the service.
  • mask list: masked: true, state: stopped on the socket, then on the service.
  • A name may be given with or without .service or .socket (strip both suffixes, as .service is stripped today).
  • Keep the result paired by position, as today, since an alias reports its target's Id.

Behaviour change to call out in the README and the fragment (bugfixes, patch): a socket unit that belongs to a listed name is now stopped and disabled, or masked, where before it was left alone. With the default lists that means rpcbind.socket, avahi-daemon.socket and cups.socket where they exist.

Acceptance criteria

  • With a stub socket and service pair in the Molecule scenario, after the role the socket and the service are both stopped and disabled (systemctl is-active and is-enabled in verify.yml).
  • With a stub socket and service pair on the mask list, both are masked.
  • A listed name that has only a service, or only a socket, or neither, is handled: the missing unit is skipped (LoadState=not-found), nothing fails.
  • A second run changes nothing (Molecule idempotence).
  • ansible-test sanity, ansible-lint and yamllint are clean; README (Notes) and a bugfixes changelog fragment describe the change.

Not verified

  • Which of the default-list units ship a socket unit on a given release: I checked none of them for this issue.
  • I did not run this role against a real rpcbind; the reproduction above is the systemd mechanism with a stand-in socket and service, using systemctl directly and not the role.
  • Whether systemctl disable <name>.service (the role's enabled: false) also disables the socket for the next boot through Also=. That would not stop it now, and I did not test it with this role.
  • Whether the Molecule image can run a real socket unit on a loopback port; if not, the state assertions above are the check.

Assisted-by: Claude Code

### Role systemd_hardening ### Release tag v1.10.1 ### ansible-core version 2.21.6 ### Platform Ubuntu 24.04 ### Minimal playbook ```yaml - hosts: all vars: systemd_hardening_system_type: ubuntu_vm roles: - role: mortis.kamino.systemd_hardening ``` On a host where a unit from the default disable list is socket-activated and its socket unit is enabled, for example `rpcbind` (the stock Ubuntu package ships `rpcbind.service` and `rpcbind.socket`, and the socket listens on `0.0.0.0:111`). `avahi-daemon` and `cups` are, to my knowledge, socket-activated on Ubuntu too (not checked for this issue). ### Expected behaviour After the role ran, a service in `systemd_hardening_disable_services` is not reachable: not running, and not started again by a connection to its port. The README says the role "disables and masks risky services", so a unit that a connection can start again is not disabled in any useful sense. The same goes for a name in `systemd_hardening_mask_services` that has a socket unit. ### Actual behaviour The role looks up and acts on `<name>.service` only (`__systemd_hardening_probe_units` appends `.service`; `disable_services.yml` and `mask_services.yml` act on the service). Stopping a service whose socket unit is still active does not close the port: systemd starts the service again on the next connection. I reproduced the systemd behaviour with a throwaway socket and service pair on an Ubuntu 24.04 VM (systemd 255), units in `/run/systemd/system`, listening on `127.0.0.1:23111`, removed afterwards: ``` == stop the .service only (what the role does) after 'stop service': socket=active service=inactive listening=1 next connection: pid=89006 socket=active service=active listening=1 == stop the socket first, then the service after 'stop socket, stop service': socket=inactive service=inactive listening=0 next connection: connect failed: ConnectionRefusedError ``` `systemctl stop <name>.service` even prints `Stopping '<name>.service', but its triggering units are still active: <name>.socket`. ## Proposal For every name in the disable and mask lists, look up both `<name>.socket` and `<name>.service` (one `systemctl show`, as today, with the two unit types per name), and act on every one that is installed, socket first: - disable list: `enabled: false`, `state: stopped` on the socket, then on the service. - mask list: `masked: true`, `state: stopped` on the socket, then on the service. - A name may be given with or without `.service` or `.socket` (strip both suffixes, as `.service` is stripped today). - Keep the result paired by position, as today, since an alias reports its target's `Id`. Behaviour change to call out in the README and the fragment (`bugfixes`, patch): a socket unit that belongs to a listed name is now stopped and disabled, or masked, where before it was left alone. With the default lists that means `rpcbind.socket`, `avahi-daemon.socket` and `cups.socket` where they exist. ## Acceptance criteria - [ ] With a stub socket and service pair in the Molecule scenario, after the role the socket and the service are both stopped and disabled (`systemctl is-active` and `is-enabled` in `verify.yml`). - [ ] With a stub socket and service pair on the mask list, both are masked. - [ ] A listed name that has only a service, or only a socket, or neither, is handled: the missing unit is skipped (`LoadState=not-found`), nothing fails. - [ ] A second run changes nothing (Molecule idempotence). - [ ] `ansible-test sanity`, ansible-lint and yamllint are clean; README (Notes) and a `bugfixes` changelog fragment describe the change. ## Not verified - Which of the default-list units ship a socket unit on a given release: I checked none of them for this issue. - I did not run this role against a real `rpcbind`; the reproduction above is the systemd mechanism with a stand-in socket and service, using `systemctl` directly and not the role. - Whether `systemctl disable <name>.service` (the role's `enabled: false`) also disables the socket for the next boot through `Also=`. That would not stop it now, and I did not test it with this role. - Whether the Molecule image can run a real socket unit on a loopback port; if not, the state assertions above are the check. Assisted-by: Claude Code
Dragonish added the Kind/Bug
Status
Ready for Human
labels 2026-11-23 08:49:46 +00:00
Dragonish referenced this issue from a commit 2026-11-23 12:38:06 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Mortis/Kamino#308