Skip to content

way to give read-only access to certain root-owned files to a DynamicUser unit #16060

Closed

Description

@cpitclaudel

Hi there,

Is your feature request related to a problem? Please describe.

I'm using systemd to run a simple HTTPS server (quart with hypercorn).

I get certificates from LetsEncrypt, which puts them in /etc/letsencrypt/live/website/*.pem. These certificates are root-owned and not readable by anyone but root, but I need to give the server access to them somehow.

Usually this isn't a problem because webservers run as root and read certificates before dropping privileges, but I'd like to avoid this.

Here are the concrete unit definitions:

  • hypercorn.socket

    [Unit]
    Description=Hypercorn socket
    PartOf=hypercorn.service
    
    [Socket]
    ListenStream=0.0.0.0:80
    
    [Install]
    WantedBy=sockets.target
    
  • hypercorn.service

    [Unit]
    Description=Hypercorn website
    Requires=network.target local-fs.target
    Requires=hypercorn.socket
    
    [Service]
    User=hypercorn
    DynamicUser=yes
    StandardInput=socket
    ExecStart=/var/www/hypercorn/.venv/bin/hypercorn --bind fd://0 website:app
    Restart=on-failure
    Type=simple
    
    [Install]
    WantedBy=multi-user.target
    

Describe the solution you'd like

I'd like a way to copy and chown /etc/letsencrypt/live/website/ into a directory owned by the newly created dynamic user, before the server starts.

I think this is similar to https://serverfault.com/questions/987688/give-systemd-service-access-to-certificates-in-protected-folder , with the additional difficulty of running with a DynamicUser.

Describe alternatives you've considered

  • I've tried using ExecStartPre to copy the files into a RuntimeDirectory and chown them, but I don't know which user to chown them to, since I'm using a DynamicUser.

  • I've tried using a StateDirectory for the dynamic user and putting a copy of the certificates in there, but @poettering says Of course, services shouldn't using StateDirectory= if they can't start with an empty state directory in the first place... in systemd does not change the user and group ownership of RuntimeDirectory when root is not explicitly assigned to User #6391, so I ruled that idea out.

  • I've tried using a BindReadOnlyPaths, but there doesn't seem to be a way to change the permissions of the files in the mount, to make them e.g. world-readable.

I'm sure there exists a solution, so this may be more of a documentation-update request than a feature request.

Thanks a lot!

Activity

  1. boucman commented on Jun 3, 2020

    @boucman
    Contributor

    IIRC you can decide the DynamicUser's name by setting User= to a (non existant) name. and systemd will allocate an unused UID to that particular name.
    Would that+your ExecStartPre method work ?

  2. boucman commented on Jun 3, 2020

    @boucman
    Contributor

    Also, if you don't set the User= the user name should be trivial to guess from the unit name.

    (all this require nss-systemd to be properly configured, by the way, which might be your problem)

  3. cpitclaudel commented on Jun 3, 2020

    @cpitclaudel
    Author

    I might have done something wrong, but even with User= I think the chown complains that the username cannot be found

  4. cpitclaudel commented on Jun 3, 2020

    @cpitclaudel
    Author

    But your suggestion is great, because I can stat the target directory to get the right user name (stat -c %%u:%%g) Feel free to close this if that's the right approach.

  5. poettering commented on Jun 9, 2020

    @poettering
    Member

    This sounds like something #15778 would neatly solve. I am tempted to close this as a duplicate of that RFE bug?

  6. added
    RFE 🎁
    on Jul 23, 2020
  7. changed the title [-]How does one give read-only access to certain root-owned files to a DynamicUser unit?[/-] [+]way to give read-only access to certain root-owned files to a DynamicUser unit[/+] on Jul 23, 2020
  8. keszybz commented on Sep 1, 2020

    @keszybz
    Member

    Fixed in bb0c0d6.

  9. exFalso commented on Nov 9, 2021

    @exFalso

    If anyone stumbles on this and is using an older systemd version, the way to make it work following cpitclaudel's method:

    ...
    ExecStartPre=+cp -r /path/to/source /run/my-unit/target
    ExecStartPre=+bash -c "chown -R $(stat -c %%u /run/my-unit) /run/my-unit/target"
    ExecStartPre=+chmod u=r,a+X /run/my-unit/target
    ExecStart=...
    ...
    DynamicUser=true
    ...
    RuntimeDirectory=my-unit
    

    Generally speaking, retrieving the dynamic user's uid when running a command as root can be done with the RuntimeDirectory/StateDirectory+stat trick

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

    RFE 🎁pid1

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions