Skip to content

systemd credentials: $CREDENTIALS_DIRECTORY empty with given service, no error in logs #31194

Closed

Description

@con-f-use

systemd version the issue has been seen with

255

Used distribution

NixOS 23.11

Linux kernel version used

6.1.75

CPU architectures issue was seen on

x86_64

Component

systemd

Expected behaviour you didn't see

Post run script has access to a decrypted credential file at $CREDENTIALS_DIRECTORY/<NAME>key.

Unexpected behaviour you saw

In the post run script, $CREDENTIALS_DIRECTORY is set, but not created/mounted by systemd and devoid of files.

Steps to reproduce the problem

  1. Created systemd service as pasted below
  2. Encrypted credential e.g. with systemd-creds --name=<NAME>key encrypt source.key /var/lib/secrets/<NAME>data/key.cred
  3. Started service

TMP2 is not used on the system. I suspect some unfortunate interaction between LoadCredentialEncrypted and one of these:

[Service]
BindPaths=... BindPaths=... BindPaths=... CapabilityBoundingSet=... DevicePolicy=... PrivateDevices=... PrivateTmp=... StateDirectory=... StateDirectory=... StateDirectory=... StateDirectory=... WorkingDirectory=...

There should at least be a log message or warning as to what exactly is happening and documentation in LoadCredential / LoadCredentialEncrypted what other options they might conflict with or what timing issues may occur.

Additional program output to the terminal or log subsystem illustrating the issue

$ cat /etc/systemd/system/acme-<DOMAIN>.service
[Unit]
After=network.target network-online.target acme-fixperms.service nss-lookup.target acme-selfsigned-<DOMAIN>.service acme-lockfiles.service
Description=Renew ACME certificate for <DOMAIN>
Wants=network-online.target acme-fixperms.service acme-selfsigned-<DOMAIN>.service acme-lockfiles.service

[Service]
Environment="LOCALE_ARCHIVE=/nix/store/q0rybps0wzwmfzs0b8j0fgqnalw9452d-glibc-locales-2.38-27/lib/locale/locale-archive"
Environment="PATH=/nix/store/1q6d0bhyj9ddvbg7bsxg74afr97hrkc2-lego-4.14.2/bin:/nix/store/5idwbbv23b6vnqdicx97s3hsgrwwnj7j-coreutils-9.4/bin:/nix/store/y4m3b33d240amsyd50d6mn0m9pyf987p-diffutils-3.10/bin:/nix/"
Environment="CUSTOMCA=/nix/store/sv1cmv6ym9v7hil1jagnx64m0c7r8gz7-CUSTOMCA_sha256.crt"
Environment="TZDIR=/nix/store/g4bk1chlkfw18f6zkljygxp3r4yi8m48-tzdata-2023d/share/zoneinfo"
BindPaths=/var/lib/acme/.lego/accounts/<HEXNUM>:/tmp/accounts
BindPaths=/var/lib/acme/<DOMAIN>:/tmp/out
BindPaths=/var/lib/acme/.lego/<DOMAIN>/<HEXNUM>:/tmp/certificates
CapabilityBoundingSet=
DevicePolicy=closed
EnvironmentFile=/var/lib/secrets/<NAME>data/acmecreds
ExecStart=/nix/store/jp8b9m3mgyhriviqfmww7cm4l09xadir-unit-script-acme-<DOMAIN>-start/bin/acme-<DOMAIN>-start 
ExecStartPost=+/nix/store/vkbid0ydr2qk6yxvbl9a0jqga5jq4rwl-acme-postrun
Group=<NAME>
LoadCredentialEncrypted=<NAME>key:/var/lib/secrets/<NAME>data/key.cred
LockPersonality=true
MemoryDenyWriteExecute=true
NoNewPrivileges=true
PrivateDevices=true
PrivateTmp=true
ProcSubset=pid
ProtectClock=true
ProtectControlGroups=true
ProtectHome=true
ProtectHostname=true
ProtectKernelLogs=true
ProtectKernelModules=true
ProtectKernelTunables=true
ProtectProc=invisible
ProtectSystem=strict
ReadWritePaths=/var/lib/acme
ReadWritePaths=/run/acme/
RemoveIPC=true
RestartSec=900
RestrictAddressFamilies=AF_INET
RestrictAddressFamilies=AF_INET6
RestrictNamespaces=true
RestrictRealtime=true
RestrictSUIDSGID=true
StateDirectory=acme/<DOMAIN>
StateDirectory=acme/.lego/<DOMAIN>
StateDirectory=acme/.lego/<DOMAIN>/<HEXNUM>
StateDirectory=acme/.lego/accounts/<HEXNUM>
StateDirectoryMode=750
SystemCallArchitectures=native
SystemCallFilter=@system-service @resources
SystemCallFilter=~@privileged
SystemCallFilter=@chown
Type=oneshot
UMask=0022
User=acme
WorkingDirectory=/tmp

[Install]
WantedBy=multi-user.target

$ cat /nix/store/vkbid0ydr2qk6yxvbl9a0jqga5jq4rwl-acme-postrun
#!/usr/bin/env bash
set -o errexit -o pipefail -o nounset ${DEBUG:+-o xtrace}
ls -hal "${CREDENTIALS_DIRECTORY:-}"

$ systemctl -u acme-<DOMAIN>.service --no-pager | grep postrun
[...]
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Found cgroup2 on /sys/fs/cgroup/, full unified hierarchy
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting / on /run/systemd/mount-rootfs (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/run/acme
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /run/systemd/mount-rootfs/run/acme on /run/systemd/mount-rootfs/run/acme (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /run/systemd/mount-rootfs/run/acme to /run/systemd/mount-rootfs/run/acme
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/run/credentials
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /run/systemd/inaccessible/dir on /run/systemd/mount-rootfs/run/credentials (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /run/systemd/inaccessible/dir to /run/systemd/mount-rootfs/run/credentials
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/run/systemd/incoming
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /run/systemd/propagate/acme-<DOMAIN>.service → /run/systemd/propagate/acme-<DOMAIN>.service.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /run/systemd/propagate/acme-<DOMAIN>.service on /run/systemd/mount-rootfs/run/systemd/incoming (MS_BIND "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /run/systemd/propagate/acme-<DOMAIN>.service to /run/systemd/mount-rootfs/run/systemd/incoming
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/sys/fs/cgroup/system.slice/acme-<DOMAIN>.service/memory.pressure
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /run/systemd/mount-rootfs/sys/fs/cgroup/system.slice/acme-<DOMAIN>.service/memory.pressure on /run/systemd/mount-rootfs/sys/fs/cgroup/system.s.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /run/systemd/mount-rootfs/sys/fs/cgroup/system.slice/acme-<DOMAIN>.service/memory.pressure to /run/systemd/mount-rootfs/sys/fs/cgroup/se
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/tmp/accounts
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /var/lib/acme/.lego/accounts/<ACCOUNT> → /var/lib/acme/.lego/accounts/<ACCOUNT>.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/.lego/accounts/<ACCOUNT> on /run/systemd/mount-rootfs/tmp/accounts (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Failed to mount /var/lib/acme/.lego/accounts/<ACCOUNT> (type n/a) on /run/systemd/mount-rootfs/tmp/accounts (MS_BIND|MS_REC ""): No such file or directory
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/.lego/accounts/<ACCOUNT> on /run/systemd/mount-rootfs/tmp/accounts (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /var/lib/acme/.lego/accounts/<ACCOUNT> to /run/systemd/mount-rootfs/tmp/accounts
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/tmp/certificates
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /var/lib/acme/.lego/<DOMAIN>/<LEGOSTR> → /var/lib/acme/.lego/<DOMAIN>/<LEGOSTR>.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/.lego/<DOMAIN>/<LEGOSTR> on /run/systemd/mount-rootfs/tmp/certificates (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Failed to mount /var/lib/acme/.lego/<DOMAIN>/<LEGOSTR> (type n/a) on /run/systemd/mount-rootfs/tmp/certificates (MS_BIND|MS_REC ""): No such filey
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/.lego/<DOMAIN>/<LEGOSTR> on /run/systemd/mount-rootfs/tmp/certificates (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /var/lib/acme/.lego/<DOMAIN>/<LEGOSTR> to /run/systemd/mount-rootfs/tmp/certificates
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/tmp/out
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /var/lib/acme/<DOMAIN> → /var/lib/acme/<DOMAIN>.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/<DOMAIN> on /run/systemd/mount-rootfs/tmp/out (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Failed to mount /var/lib/acme/<DOMAIN> (type n/a) on /run/systemd/mount-rootfs/tmp/out (MS_BIND|MS_REC ""): No such file or directory
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/<DOMAIN> on /run/systemd/mount-rootfs/tmp/out (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /var/lib/acme/<DOMAIN> to /run/systemd/mount-rootfs/tmp/out
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/var/lib/acme
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /run/systemd/mount-rootfs/var/lib/acme on /run/systemd/mount-rootfs/var/lib/acme (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /run/systemd/mount-rootfs/var/lib/acme to /run/systemd/mount-rootfs/var/lib/acme
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/var/lib/acme/.lego/accounts/<ACCOUNT>
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /var/lib/acme/.lego/accounts/<ACCOUNT> → /var/lib/acme/.lego/accounts/<ACCOUNT>.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/.lego/accounts/<ACCOUNT> on /run/systemd/mount-rootfs/var/lib/acme/.lego/accounts/<ACCOUNT> (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /var/lib/acme/.lego/accounts/<ACCOUNT> to /run/systemd/mount-rootfs/var/lib/acme/.lego/accounts/<ACCOUNT>
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/var/lib/acme/.lego/<DOMAIN>
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /var/lib/acme/.lego/<DOMAIN> → /var/lib/acme/.lego/<DOMAIN>.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/.lego/<DOMAIN> on /run/systemd/mount-rootfs/var/lib/acme/.lego/<DOMAIN> (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /var/lib/acme/.lego/<DOMAIN> to /run/systemd/mount-rootfs/var/lib/acme/.lego/<DOMAIN>
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Applying namespace mount on /run/systemd/mount-rootfs/var/lib/acme/<DOMAIN>
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Followed source symlinks /var/lib/acme/<DOMAIN> → /var/lib/acme/<DOMAIN>.
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Bind-mounting /var/lib/acme/<DOMAIN> on /run/systemd/mount-rootfs/var/lib/acme/<DOMAIN> (MS_BIND|MS_REC "")...
Feb 03 11:33:31 <NAME> (-postrun)[1155]: Successfully mounted /var/lib/acme/<DOMAIN> to /run/systemd/mount-rootfs/var/lib/acme/<DOMAIN>
Feb 03 11:33:31 <NAME> (-postrun)[1155]: cannot access '/run/credentials/acme-DOMAIN.service': No such file or directory

$ ll /var/lib/secrets/<NAME>data/key.cred
-rw-r--r-- 1 root root 2.4K Feb  3 09:53 key.cred     # well under the 1M size limit total

$ cat /var/lib/secrets/<NAME>data/key.cred | head -n 1
Whxqht+dQJax1aZeCGLxmiAAAAABAAAADAAAABAAAADf9q4G5Bw7hutNgrAAAAAA7LuXcmYVsS22l2I

Redacted <NAME>, <DOMAIN>, and <HEXNUM> have values like mysub, mysub.example.com, 8438372bd9f151d8f10e2 repectively.

Activity

  1. added
    bug 🐛
    on Feb 3, 2024
  2. con-f-use commented on Feb 5, 2024

    @con-f-use
    Author

    @YHNdnzj thanks for your quick work in #31197 on a Sunday, nonetheless. Can you give me a timeline on when this will see an official release?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐛pid1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions