fix(postgres,darwin): close the loopback superuser path and scope agenix ownership (#401)

* fix(postgres): require a password for superuser over TCP

* test: assert the postgres loopback auth rules

* fix(darwin): keep root-only agenix secrets root-only

* docs(security): record the local auth boundaries and retire WA-015
This commit is contained in:
ryan4yin | 二花 authored and GitHub committed 2026-10-07 15:22:03 +08:00
1 parent 3acb5d4ee5
commit 3694961e74
6 files changed
+123 -33

No files matched your search

+21 -17
View File
@@ -233,9 +233,13 @@ export, credential, DNS record or firmware configuration was changed during the
desktops. Same-user agents are not isolated from these credentials by Unix file permissions. Mode
`0000` also does not stop privileged root. Other host/runtime ownership checks and recipient
membership were not completed; no private recipient repository or secret value was inspected.
- Darwin's activation has a blanket `chown` over `/etc/agenix/*`; on symlink targets this can
override declared root ownership. Runtime ownership and consumer requirements must be verified
before replacing it with explicit per-file ownership. Track this source concern as WA-015.
- Darwin's activation now re-applies each secret's declared ownership explicitly instead of a
blanket `chown` over `/etc/agenix/*`, so the root-only `rclone.conf` stays root-owned. Runtime
ownership (`ls -l /etc/agenix`) on each Darwin host still needs a check after the next activation.
- PostgreSQL's loopback TCP listener still trusts application roles by password (Immich and the
exporter use it), but `postgres` and replication now require `scram-sha-256`, closing the
local-superuser path. Provisioning per-role passwords for full loopback `scram-sha-256` is the
follow-up (the Grafana datasource file already tracks this as its item 1.5).
### Sandbox and agent boundaries
@@ -303,20 +307,20 @@ separate verification.
## Remaining prioritized TODOs
| Priority | Work | Acceptance / constraints |
| -------- | -------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| P1 | Roll out and verify the implemented baseline/exporter rules | Runtime sysctl readback, allowed IPv4 scrape and denied IPv6/non-monitoring scrape tests; preserve Nix/Podman/Cilium/NFS functionality |
| P1 | Review router mappings and tailnet ACLs; narrow service exposure where warranted | Audit was limited to sampled host listeners; test intended and denied access without locking out deployment |
| P1 | Review NFS client scope and root identity requirements | Restrict to required VM hosts/CSI nodes where practical; test provisioning, permissions and existing PVCs before changing squash semantics |
| P1 | Narrow operator/secret access where justified; finish permission checks | Selected effective probes and ai/youko modes audited; validate broad secret-reader needs, remaining hosts, recipient scope and Darwin per-file ownership |
| P1 | Adopt the documented cadence and verify the booted kernel after rollouts | Routine documented; compare booted and selected kernel paths and recheck API/workloads after reboot; no automated metric is deployed |
| P1 | Exercise backup restore and credential rotation | Use an approved isolated restore target and recovery access; never overwrite live data for a drill |
| P2 | Promote selected AppArmor profiles to enforce; harden exposed systemd services | Per-app positive/negative tests, store-path coverage, reviewed capabilities and reversible rollout |
| P2 | Maintain ai boot integrity and assess other host roles | Review sbctl configuration migration, recovery boot and signed custom/NVIDIA modules; ai Secure Boot state already verified |
| P2 | Review unused-module deny list against real workloads and verified advisories | Check loaded/built-in modules and autoload dependencies; do not block required filesystems, WiFi, VM or Cilium/NFS functionality |
| P2 | Evaluate stronger controls only against a concrete threat | Account for hibernation, crash recovery, applications and performance; no blanket io_uring/SMT/userns ban or hardened-kernel switch |
| P2 | Validate narrower sandbox/agent boundaries where needed | Source and selected SSH/journal metadata audited; use disposable data/socket probes and preserve core-desktop diagnosis, browserpass and Homelab root workflows |
| P3 | Improve security-event alerts and configuration drift checks | Actionable redacted alerts and periodic runtime control readback |
| Priority | Work | Acceptance / constraints |
| -------- | -------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| P1 | Roll out and verify the implemented baseline/exporter rules | Runtime sysctl readback, allowed IPv4 scrape and denied IPv6/non-monitoring scrape tests; preserve Nix/Podman/Cilium/NFS functionality |
| P1 | Review router mappings and tailnet ACLs; narrow service exposure where warranted | Audit was limited to sampled host listeners; test intended and denied access without locking out deployment |
| P1 | Review NFS client scope and root identity requirements | Restrict to required VM hosts/CSI nodes where practical; test provisioning, permissions and existing PVCs before changing squash semantics |
| P1 | Narrow operator/secret access where justified; finish permission checks | Selected effective probes and ai/youko modes audited; validate broad secret-reader needs, remaining hosts and recipient scope; Darwin per-file ownership is now explicit and needs a runtime check |
| P1 | Adopt the documented cadence and verify the booted kernel after rollouts | Routine documented; compare booted and selected kernel paths and recheck API/workloads after reboot; no automated metric is deployed |
| P1 | Exercise backup restore and credential rotation | Use an approved isolated restore target and recovery access; never overwrite live data for a drill |
| P2 | Promote selected AppArmor profiles to enforce; harden exposed systemd services | Per-app positive/negative tests, store-path coverage, reviewed capabilities and reversible rollout |
| P2 | Maintain ai boot integrity and assess other host roles | Review sbctl configuration migration, recovery boot and signed custom/NVIDIA modules; ai Secure Boot state already verified |
| P2 | Review unused-module deny list against real workloads and verified advisories | Check loaded/built-in modules and autoload dependencies; do not block required filesystems, WiFi, VM or Cilium/NFS functionality |
| P2 | Evaluate stronger controls only against a concrete threat | Account for hibernation, crash recovery, applications and performance; no blanket io_uring/SMT/userns ban or hardened-kernel switch |
| P2 | Validate narrower sandbox/agent boundaries where needed | Source and selected SSH/journal metadata audited; use disposable data/socket probes and preserve core-desktop diagnosis, browserpass and Homelab root workflows |
| P3 | Improve security-event alerts and configuration drift checks | Actionable redacted alerts and periodic runtime control readback |
Record temporary exceptions in [WORKAROUNDS.md](WORKAROUNDS.md) with a removal condition. Revisit
this architecture after a new exposed service, network/storage change, host addition or significant
-1
View File
@@ -52,7 +52,6 @@ A known limitation with no fix yet.
| ID | What & where | Why | Removal condition | Added | Revisit | Status |
| ------ | ---------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------- | ---------- | -------------------------------------- | ------ |
| WA-012 | `muvm` (x86_64 apps/games via microVM) disabled — `hosts/12kingdoms-shoukei/apple-silicon.nix` | Apple Silicon's 16k page size means x86_64 apps need `muvm`; it is off because it does not build/run on nixos-apple-silicon yet ([nixos-apple-silicon#237](https://github.com/nix-community/nixos-apple-silicon/issues/237)) | Re-enable once `muvm` builds and works | — | on nixos-apple-silicon or muvm updates | active |
| WA-015 | Darwin secret ownership — `secrets/darwin.nix` | Blanket `chown /etc/agenix/*` can override declared root ownership through symlinks; source concern, not a verified runtime leak | Verify consumers/runtime ownership, replace blanket ownership with explicit per-file ownership, and recheck target modes | 2026-10-03 | next Darwin activation review | active |
| WA-016 | subscription rule snapshots — `modules/nixos/desktop/networking/mihomo/` (`rules_from:`) | mihomo's `proxy-providers` imports proxies only, so the `rules` and `proxy-groups` a subscription ships with are dropped by the core; the generator re-imports them from a local YAML copy that has to be refreshed by hand when the provider changes its routing policy | Drop the snapshots once mihomo can import rules from a proxy-provider, or the providers publish their rules as rule-providers | 2026-10-04 | when mihomo gains rule import | active |
## WIP
@@ -95,16 +95,20 @@ in
# "local" is for Unix domain socket connections only
local all all peer map=superuser_map
# IPv4 local connections:
# TCP loopback: application roles may still connect without a password
# (Immich, the exporter), but the superuser and replication may not. That
# is the path a compromised local service would take. No password is set
# for postgres, so these lines deny it over TCP.
host all postgres 127.0.0.1/32 scram-sha-256
host all postgres ::1/128 scram-sha-256
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
# Replication connections from loopback require a password too.
local replication all peer map=superuser_map
host replication all 127.0.0.1/32 scram-sha-256
host replication all ::1/128 scram-sha-256
# Other Remote Access - allow access only the database with the same name as the user
host sameuser all ${myvars.networking.lanCidr} scram-sha-256
@@ -0,0 +1,5 @@
{
lib,
outputs,
}:
lib.genAttrs (builtins.attrNames outputs.nixosConfigurations) (_: true)
@@ -0,0 +1,67 @@
{
lib,
outputs,
}:
let
cfg = outputs.nixosConfigurations.youko.config;
auth = cfg.services.postgresql.authentication;
lines = lib.splitString "\n" auth;
fields = line: builtins.filter (s: s != "") (lib.splitString " " line);
hasLine = want: builtins.any (line: fields line == want) lines;
indexOf =
want:
let
go =
i: xs:
if xs == [ ] then
null
else if fields (builtins.head xs) == want then
i
else
go (i + 1) (builtins.tail xs);
in
go 0 lines;
in
lib.genAttrs (builtins.attrNames outputs.nixosConfigurations) (
name:
if name != "youko" then
true
else
# The superuser and replication rows must require a password, and the
# postgres row must precede the generic loopback trust row (first match wins).
hasLine [
"host"
"all"
"postgres"
"127.0.0.1/32"
"scram-sha-256"
]
&& hasLine [
"host"
"replication"
"all"
"127.0.0.1/32"
"scram-sha-256"
]
&& hasLine [
"host"
"all"
"all"
"127.0.0.1/32"
"trust"
]
&&
indexOf [
"host"
"all"
"postgres"
"127.0.0.1/32"
"scram-sha-256"
] < indexOf [
"host"
"all"
"all"
"127.0.0.1/32"
"trust"
]
)
+19 -8
View File
@@ -117,14 +117,25 @@
};
};
# both the original file and the symlink should be readable and executable by the user
#
# activationScripts are executed every time you run `nixos-rebuild` / `darwin-rebuild` or boot your system
# nix-darwin ignores environment.etc.<name>.mode/owner, and a blanket chown
# makes the root-only secrets readable by the login user. Re-apply each
# secret's declared policy instead.
system.activationScripts.postActivation.text = ''
${pkgs.nushell}/bin/nu -c '
if (ls /etc/agenix/ | length) > 0 {
sudo chown ${myvars.username} /etc/agenix/*
}
'
if [ -d /etc/agenix ]; then
${pkgs.coreutils}/bin/chown ${myvars.username} \
/etc/agenix/ssh-key-romantic \
/etc/agenix/nushell-secrets.nu \
/etc/agenix/work-gitconfig
${pkgs.coreutils}/bin/chmod 0500 \
/etc/agenix/ssh-key-romantic \
/etc/agenix/nushell-secrets.nu \
/etc/agenix/work-gitconfig
${pkgs.coreutils}/bin/chown root /etc/agenix/rclone.conf
${pkgs.coreutils}/bin/chmod 0500 /etc/agenix/rclone.conf
${pkgs.coreutils}/bin/chown root /etc/agenix/ryan4yin-gpg-subkeys.priv.age
${pkgs.coreutils}/bin/chmod 0000 /etc/agenix/ryan4yin-gpg-subkeys.priv.age
fi
'';
}