Skip to content

Permission denied for credentials after first command line when DynamicUser is enabled #24291

Closed

Description

@ElvishJerricco

systemd version the issue has been seen with

251.3

Used distribution

NixOS Unstable 22.11.20220808.39d7f92

Linux kernel version used

5.15.59

CPU architectures issue was seen on

x86_64

Component

systemd

Expected behaviour you didn't see

When running a oneshot service with multiple ExecStart command lines and DynamicUser=true, credentials should be available to each command line.

Unexpected behaviour you saw

Credentials were only available to the first command line. The second one got Permission denied. But if you don't enable DynamicUser, then both command lines will have access as expected.

Steps to reproduce the problem

# foo.service
[Unit]

[Service]
Environment="LOCALE_ARCHIVE=/nix/store/mhbhiykjxvl5cn0f4g0zxcii4shz4rq8-glibc-locales-2.35-163/lib/locale/locale-archive"
Environment="PATH=/nix/store/xp5z3k851fs7haqbcwqax1hh4pynzla9-coreutils-9.1/bin:/nix/store/b1c0270m7fcppqpga49fqy7n8hj3vbw2-findutils-4.9.0/bin:/nix/store/74lg75l5wdxkiab73pc7x3983lcppk86-gnugrep-3.7/bin:/nix/store/cr41h6cg093d7z6w0nnaiqpsfc2fxd54-gnused-4.8/bin:/nix/store/yzycn28101y9v6disd23r86g1q3n4dsj-systemd-251.3/bin:/nix/store/xp5z3k851fs7haqbcwqax1hh4pynzla9-coreutils-9.1/sbin:/nix/store/b1c0270m7fcppqpga49fqy7n8hj3vbw2-findutils-4.9.0/sbin:/nix/store/74lg75l5wdxkiab73pc7x3983lcppk86-gnugrep-3.7/sbin:/nix/store/cr41h6cg093d7z6w0nnaiqpsfc2fxd54-gnused-4.8/sbin:/nix/store/yzycn28101y9v6disd23r86g1q3n4dsj-systemd-251.3/sbin"
Environment="TZDIR=/nix/store/3m1aakciplbv5mymyl9dmis35kw71fib-tzdata-2022a/share/zoneinfo"



DynamicUser=true
ExecStart=/nix/store/xp5z3k851fs7haqbcwqax1hh4pynzla9-coreutils-9.1/bin/stat ${CREDENTIALS_DIRECTORY}/foo
ExecStart=/nix/store/xp5z3k851fs7haqbcwqax1hh4pynzla9-coreutils-9.1/bin/stat ${CREDENTIALS_DIRECTORY}/foo
LoadCredential=foo:/etc/shadow
Type=oneshot

You can disregard all the /nix/store/* noise and replace it with /usr to replicate on other distros.

This service will fail when you start it. The first stat command has access, but the second one doesn't.

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

Aug 11 21:13:12 nixos systemd[1]: Starting foo.service...
Aug 11 21:13:12 nixos stat[926]:   File: /run/credentials/foo.service/foo
Aug 11 21:13:12 nixos stat[926]:   Size: 802               Blocks: 8          IO Block: 4096   regular file
Aug 11 21:13:12 nixos stat[926]: Device: 0,45        Inode: 19325       Links: 1
Aug 11 21:13:12 nixos stat[926]: Access: (0400/-r--------)  Uid: (63016/     foo)   Gid: (    0/    root)
Aug 11 21:13:12 nixos stat[926]: Access: 2022-08-11 21:13:12.448000000 +0000
Aug 11 21:13:12 nixos stat[926]: Modify: 2022-08-11 21:13:12.448000000 +0000
Aug 11 21:13:12 nixos stat[926]: Change: 2022-08-11 21:13:12.448000000 +0000
Aug 11 21:13:12 nixos stat[926]:  Birth: -
Aug 11 21:13:12 nixos stat[928]: /nix/store/xp5z3k851fs7haqbcwqax1hh4pynzla9-coreutils-9.1/bin/stat: cannot statx '/run/credentials/foo.service/foo': Permission denied
Aug 11 21:13:12 nixos systemd[1]: foo.service: Main process exited, code=exited, status=1/FAILURE
Aug 11 21:13:12 nixos systemd[1]: foo.service: Failed with result 'exit-code'.
Aug 11 21:13:12 nixos systemd[1]: Failed to start foo.service.

Activity

  1. added
    bug 🐛
    on Aug 11, 2022
  2. ElvishJerricco commented on Aug 12, 2022

    @ElvishJerricco
    ContributorAuthor

    I also reproduced this on Arch Linux with systemd 251.4

    [Unit]
    [Service]
    DynamicUser=true
    LoadCredential=foo:/etc/passwd
    Type=oneshot
    ExecStart=/usr/bin/stat ${CREDENTIALS_DIRECTORY}/foo
    ExecStart=/usr/bin/stat ${CREDENTIALS_DIRECTORY}/foo
    

    I also tried this on Debian Bullseye with systemd 247 and did not see the bug.

  3. ElvishJerricco commented on Aug 13, 2022

    @ElvishJerricco
    ContributorAuthor

    I've bisected the issue down to a631cbf by @poettering, though to be honest it looks like that is just exposing a separate problem. I can't imagine why adding && FLAGS_SET(params->flags, EXEC_WRITE_CREDENTIALS) would be a problem unless the flags were somehow incorrect.

  4. jiphex commented on May 18, 2023

    @jiphex

    I have also seen this on Rocky9 (systemd v252-13.el9_2)

    This works (the cat is able to read the credential), but the ls gets permission denied. If I move the cat command to be anything but the first command executed, it is unable to read the credential.

    [Service]
    LoadCredentialEncrypted=test:/etc/credstore/test
    Type=oneshot
    ProtectSystem=strict
    DynamicUser=true
    ExecStart=/usr/bin/cat ${CREDENTIALS_DIRECTORY}/test
    ExecStart=-/usr/bin/id
    ExecStart=-/usr/bin/ls ${CREDENTIALS_DIRECTORY}
    
  5. YHNdnzj commented on May 18, 2023

    @YHNdnzj
    Member

    Already fixed in #27279.

  6. Gunni commented on Feb 11, 2025

    @Gunni

    Already fixed in #27279.

    NACK? Works without DynamicUser=yes...

    $ systemd-run -p 'LoadCredential=x:/etc/hostname' -p 'ExecStartPre=true' -p 'ExecStartPre=systemd-creds cat x' --wait --pipe true
    Running as unit: run-u58.service
    example
    Finished with result: success
    Main processes terminated with: code=exited/status=0
    Service runtime: 30ms
    CPU time consumed: 11ms
    

    Does not work with DynamicUser=yes...

    $ systemd-run -p DynamicUser=yes -p 'LoadCredential=x:/etc/hostname' -p 'ExecStartPre=true' -p 'ExecStartPre=systemd-creds cat x' --wait --pipe true
    Running as unit: run-u57.service
    Failed to open credentials directory '/run/credentials/run-u57.service': Permission denied
    Failed to open credentials directory: Permission denied
    Finished with result: exit-code
    Service runtime: 24ms
    CPU time consumed: 15ms
    

    Works with DynamicUser=yes if the first command uses the credential...

    $ systemd-run -p DynamicUser=yes -p 'LoadCredential=x:/etc/hostname' -p 'ExecStartPre=systemd-creds cat x' --wait --pipe true
    Running as unit: run-u63.service
    example
    Finished with result: success
    Main processes terminated with: code=exited/status=0
    Service runtime: 28ms
    CPU time consumed: 16ms
    

    Systemd 252

    $ systemctl --version
    systemd 252 (252-46.el9_5.2.alma.1)
    +PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN -IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY +P11KIT -QRENCODE +TPM2 +BZIP2 +LZ4 +XZ +ZLIB +ZSTD -BPF_FRAMEWORK +XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified
    
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