Skip to content

tty device used inside toolbox gets odd permissions on host causing pinentry/gpg-agent to not work #568

Open

Description

@dustymabe

Describe the bug

My setup includes running gpg-agent on my host and doing development (git commit, etc) inside my toolbox container. When I git-commit I sign the commits, which means the process talks to the agent that then runs a pinentry program to ask the user for the passphrase.

When running in the terminal I prefer for the passphrase to be asked to me in the terminal window and not in a popup. So I'll unset DISPLAY to force it to use the terminal window. This stopped working sometime in the past few months. I finally tracked down some more information on why.

When running inside toolbox the permissions on my tty device are as expected:

$ ls -l $(tty)
crw--w----. 1 root nobody 136, 14 Sep 27 13:37 /dev/pts/14

However outside of toolbox (from the perspective of the host) the
permissions look odd:

[dustymabe@media ~]$ id
uid=1001(dustymabe) gid=1001(dustymabe) groups=1001(dustymabe) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[dustymabe@media ~]$ ls -l /dev/pts/14
crw--w----. 1 100000 tty 136, 14 Sep 27 14:39 /dev/pts/14

I would expect the owner of the tty to be the user who launched the toolbox container (1001). If I change the ownership to the expected UID then my pinentry program appropriately asks for the passphrase on the same terminal window (inside toolbox) where I am running git commit.

I'm running Fedora 33 with:

$ rpm -q toolbox podman
toolbox-0.0.95-1.fc33.x86_64
podman-2.1.0-0.187.rc2.fc33.x86_64

Indeed if I go back to an old machine I haven't updated in a while I can see that the permissions are what I would expect them to be. This out of date machine has:

[dustymabe@hattop ~]$ rpm -q toolbox podman 
toolbox-0.0.18-1.fc31.noarch
podman-1.8.0-2.fc31.x86_64

Steps how to reproduce the behaviour

  1. Launch toolbox container
  2. Inspect owner inside the container: ls -l $(tty). Should show up as root since you are inside a user namespace.
  3. Inspect the owner of that same device outside the container and verify the permissions looks weird.

Activity

  1. added
    1. Bug
    on Sep 27, 2020
  2. debarshiray commented on Oct 14, 2020

    @debarshiray
    Member

    Thanks for narrowing it down to those working and broken versions.

    I wonder if this might be solved by using podman create ... --mount type=devpts,destination=/dev/pts as introduced in podman-container-tools/podman#7209

    I don't think Toolbox itself does anything funky with the TTY devices, but it does bind mount the entire /dev from the host into the container. So I wonder if we can reproduce the same with podman run ... ?

  3. dustymabe commented on Oct 14, 2020

    @dustymabe
    CollaboratorAuthor

    Thanks for narrowing it down to those working and broken versions.

    No problem :) - It's worth noting those were just two data points. I don't know exactly when the issue was introduced.

    I wonder if this might be solved by using podman create ... --mount type=devpts,target=/dev/pts as introduced in containers/podman#7209

    Can you recreate locally? I'm assuming pretty much anyone running latest toolbox/podman should see a weird owner when running ls -l on one of the tty devices used for a toolbox session. If you see the problem maybe you can try that in a development version of toolbox?

    I don't think Toolbox itself does anything funky with the TTY devices, but it does bind mount the entire /dev from the host into the container. So I wonder if we can reproduce the same with podman run ... ?

    Right, I suspect it was a change in podman that caused the regression, just wasn't sure so I reported here first.

  4. debarshiray commented on Oct 14, 2020

    @debarshiray
    Member

    Adding --mount type=devpts,destination=/dev/pts to podman create only changes the group ownership of the TTY device inside the container from nobody to tty:

    $ ls -l $(tty)
    crw--w----. 1 root tty 136, 0 Oct 14 20:07 /dev/pts/0
    

    That seems to be a step in the right direction in terms of matching the TTY device's ownership to that on the host, but I don't know if it solves your problem.

    I don't know if you figured it out already, but the strange ownership that you are seeing is related to the use of --userns=keep-id. The 100000 UID on the host is the UID of root inside the container's user namespace, the nobody GID inside the container is due to the host's tty group being absent inside the container.

  5. dustymabe commented on Oct 14, 2020

    @dustymabe
    CollaboratorAuthor

    Right, but what we need is for the owner of the tty device on the host to be the user who started toolbox. For example, to fix this problem right now for me I sudo chown dustymabe /dev/pts/14 on the host.

    Before changing the ownership whenever I git commit it will fail because I have it configured to sign commits and pinentry from the gpg-agent running on the host can't access the tty toolbox is using. After correcting the ownership I can type my password in to the pinentry program that pops up in my terminal.

  6. debarshiray commented on Oct 14, 2020

    @debarshiray
    Member

    By the way, I submitted #581 for the --mount type=devpts,destination=/dev/pts change. @giuseppe had been asking me to do so for a while, so better late than never.

  7. debarshiray commented on Oct 14, 2020

    @debarshiray
    Member

    Right, but what we need is for the owner of the tty device on the host
    to be the user who started toolbox. For example, to fix this problem right
    now for me I sudo chown dustymabe /dev/pts/14 on the host.

    Yes, I understand.

    Right now I can think of a (crude?) way to fix this. /usr/bin/toolbox is the entry point of the containers. So, instead of merely sleeping, we can install an inotify watch on /dev/pts and have it do chown <user> every time a new file is created there.

  8. dustymabe commented on Oct 14, 2020

    @dustymabe
    CollaboratorAuthor

    I'm not sure if this can be done inside the container or if it needs to be done as part of container setup (i.e. podman). I can't do the sudo chown dustymabe /dev/pts/14 from inside the container (inside the container you get chown: changing ownership of '/dev/pts/14': Operation not permitted). It has to be from the host.

  9. debarshiray commented on Oct 14, 2020

    @debarshiray
    Member

    If you have a new devpts file system mounted on /dev/pts in the container (ie., --mount type=devpts,destination=/dev/pts), then I think you can. :)

    Otherwise, the /dev/pts is shared with the host, and the nobody group owner (because the host's tty group is absent inside the container) blocks the chown inside the container.

  10. giuseppe commented on Oct 27, 2021

    @giuseppe
    Member

    what are the permissions in /dev/pts/?

    Is the container running with --mount type=devpts,destination=/dev/pts?

  11. giuseppe commented on Oct 28, 2021

    @giuseppe
    Member

    that seems correct. If I understand correctly though, you would like it to be owned by your ID?

    I've opened a PR for Podman to allow passing down more options like uid= and gid=: podman-container-tools/podman#12124

    so you'll be abe to do something like --mount type=devpts,destination=/dev/pts,uid=$UID,gid=$UID

  12. 1 remaining item

  13. giuseppe commented on Oct 28, 2021

    @giuseppe
    Member

    /dev:/dev will bind mount /dev/pts from the host as well, that could make a difference.

    I don't think it is correct though, could you give a try to the patch I've proposed for Podman?

  14. debarshiray commented on Mar 2, 2023

    @debarshiray
    Member

    Revisiting this one after a long time ...

    Right, but what we need is for the owner of the tty device on the host
    to be the user who started toolbox. For example, to fix this problem right
    now for me I sudo chown dustymabe /dev/pts/14 on the host.

    Yes, I understand.

    Right now I can think of a (crude?) way to fix this. /usr/bin/toolbox is the
    entry point of the containers. So, instead of merely sleeping, we can install
    an inotify watch on /dev/pts and have it do chown <user> every time a
    new file is created there.

    Oh, I am sorry. I am an idiot. :)

    You wanted the owner of the (nested pseudo) terminal device on the host to be the user who started toolbox, instead of something funky; and not the owner of the device inside the container, which is what I was rambling about.

    Sorry about that.

    Note that entering a container (ie., podman exec --tty) creates a nested pseudo-terminal device inside the container's mount and user namespaces, and there's another one that already exists on the host for the terminal tab or window. ie., there are two terminal devices at play.

    I think that one consequence of using:
    podman create --mount type=devpts,destination=/dev/pts

    (which is commit 494007b6cadc5fe3 or #581)

    ... is that it separates the devices in /dev/pts on the host and the container.

    The nested pseudo-terminal device inside the container gets created as root:tty. This isn't visible from the host, and so there's no device with 100000:tty on the host.

    The other consequence of that change is that the nested pseudo-terminal device inside the container gets created as root:tty, not root:nobody, which is what's documented in the commit message.

    @dustymabe Does this set-up help your gpg-agent and pinentry set-up?

    I'm not sure if this can be done inside the container or if it needs to be done
    as part of container setup (i.e. podman). I can't do the
    sudo chown dustymabe /dev/pts/14 from inside the container (inside the
    container you get chown: changing ownership of '/dev/pts/14': Operation
    not permitted). It has to be from the host.

    If you have a new devpts file system mounted on /dev/pts in the container
    (ie., --mount type=devpts,destination=/dev/pts), then I think you can. :)

    Otherwise, the /dev/pts is shared with the host, and the nobody group
    owner (because the host's tty group is absent inside the container) blocks
    the chown inside the container.

    Except, with a separate devpts file system inside the container, the container and the host can't see each other's devices. So, I don't know where this leaves you, @dustymabe :)

    Note that #1016 is about changing the ownership of the nested pseudo-terminal device inside the container to $UID:tty, from the current root:tty.

  15. debarshiray commented on Mar 2, 2023

    @debarshiray
    Member

    You can check if your container was created with:

    podman create --mount type=devpts,destination=/dev/pts
    

    ... by looking at CreateCommand in the podman inspect --type container ... output.

  16. debarshiray commented on Mar 6, 2023

    @debarshiray
    Member

    I spent some time debugging gpg-agent and pinentry with @dustymabe -- it's still broken and the previous workaround of fixing the ownership of the terminal device on the host doesn't work anymore.

    It seems to me that the gpg-agent and pinentry processes running on the host, really want access to the secondary end of the nested pseudo-terminal device inside the container's mount and user namespaces (ie., the output of tty(1) or the /dev/pts/N device inside the container). Note how the gpg-agent(1) manual says that the GPG_AGENT environment variable should always reflect the output of tty(1).

    That doesn't work at the moment because commit 494007b6cadc5fe3 or #581 mounted a separate devpts file system inside the container's mount and user and namespaces. That made the /dev/pts/N devices inside the container different from those on the host.

    Back when @dustymabe originally filed this issue, the containers didn't have their own devpts file system. So, the host could see the nested pseudo-terminal device inside the container's user namespace, but it was owned by a funky user ID. Correcting the ownership meant that gpg-agent and pinentry could use the device.

    Now, the host can't even see the device, so there's no ownership to be corrected.

  17. debarshiray commented on Mar 6, 2023

    @debarshiray
    Member

    I filed containers/crun#1158 for some clarifications and advice.

  18. alexpdp7 commented on Apr 4, 2023

    @alexpdp7

    Sorry to intrude, but this seems related to a problem I'm having running distrobox/containerbox nested inside an LXC container, see:

    --mount type=devpts,destination=/dev/pts is causing issues in my environment. I suspect my environment is "bad" and should be "unsupported", but perhaps it's related to the problems you're seeing lately in this issue?

  19. heyakyra commented on Apr 18, 2023

    @heyakyra

    I don't have much to contribute here, but i wanted to offer a decent workaround for anyone who, like myself, just wants to sign git commits within a toolbox container, but always ends up hitting:

    error: gpg failed to sign the data
    fatal: failed to write commit object
    

    and having to run gpgconf --kill gpg-agent and exiting the toolbox container to try again.

    Since signing works as long as the gpg-agent was started on the host machine outside of the toolbox, you can just run gpg-agent --daemon before toolbox enter. If you do this often enough, create an alias like: alias tbx='gpg-agent --daemon; toolbox enter [container name]' so you can just run tbx

  20. dustymabe commented on Apr 18, 2023

    @dustymabe
    CollaboratorAuthor

    @heyakyra, I'm not sure that's a sufficient workaround for the problem I experience. My gpg agent is always run from outside of toolbox. The problem I have is that when I'm not on my local system (i.e. I'm SSHing into it) I can't sign commits.

    When I am on my local system (i.e. DISPLAY=:0) things are fine because a window popup asks me for my password (doesn't need the terminal tty). When I'm remote (SSH) I need the terminal tty and hit this issue.

  21. losuler commented on Jun 14, 2023

    @losuler

    I have no idea why this workaround works, but if you run this a few times in quick succession from within the toolbox (never works if just ran once):

    systemctl --user restart gpg-agent.socket

    Then for some reason the popup window in GNOME will start working, and I can put in my password for my GPG key I use to sign my git commits.

    $ rpm -q toolbox podman
    toolbox-0.0.99.4-1.fc38.x86_64
    podman-4.5.1-1.fc38.x86_64
  22. dustymabe commented on Feb 13, 2026

    @dustymabe
    CollaboratorAuthor

    ok i'm back here after a few years. Unfortunately I don't have a real solution for the underlying problem which I'll now characterize as:

    host gpg-agent can't access tty devices in other devpts namespaces

    I think in order to solve that problem the upstream gpg-agent software may need to be modified to understand namespaces such that we could also set GPG_TTY_NAMESPACE or something.

    Absent of that solution existing I've come up with a workaround, which is to use the loopback pinentry mode. This just asks for the passphrase in the shell where the action was invoked. It's not fancy, but seems to work.

    Unfortunately this can't be controlled by an environment variable, but we can use an alternate wrapper program for gpg and configure git to use that:

    EDIT: credit for inspiration from https://betakuang.medium.com/using-command-line-passphrase-input-for-gpg-with-git-for-windows-f78ae2c7cd2e

    #!/usr/bin/bash
    # Simply call `gpg` with the same options but with added
    # --pinentry-mode loopback. This allows us to bypass the
    # use of a pinentry program and just use the calling terminal
    # for typing the passphrase. The reason for this is because the
    # tty devices inside a toolbox container are under a different
    # devpts namespace and the gpg-agent running on the host can't
    # access those devices. This is the simplest way to workaround
    # the problem. Pair this with gpg.program=/path/to/this/file
    # to have git ask for the password in local terminal.
    exec gpg --pinentry-mode loopback $@
    

    Another solution that could work but I haven't tested is to use one of the pinentry-tmux alternatives (since I also use tmux):

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

    1. Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions