Skip to content

Unix domain sockets return spurious data from getsockopt(SO_PEERCRED) if the peer has already departed, which breaks ssh-agent forwarding for Ubuntu 18.04 #3183

Closed

Description

@0xabu

Since upgrading from Ubuntu 16.04 to 18.04, I've noticed that my ssh-agent setup no longer works reliably. For the purposes of debugging, I ran ssh-agent as:

/usr/bin/ssh-agent -s -d -a /tmp/.sshagent.sock

After doing this, I can add keys to the agent, and make exactly one SSH connection. Later, trying to connect to the agent fails with Error connecting to agent: Connection refused. Before the SSH connection (when everything still works), getsockopt appears to behave as expected, returning the pid/uid/gid of the process connecting to the socket:

accept(3</unknown>, {sa_family=AF_UNIX}, [110->2]) = 4</unknown>
getsockopt(4</unknown>, SOL_SOCKET, SO_PEERCRED, {pid=369, uid=1000, gid=1000}, [12]) = 0
getuid()                                = 1000

However later, getsockopt returns -1 as uid/gid which obviously makes ssh-agent unhappy:

accept(3</unknown>, {sa_family=AF_UNIX}, [110->2]) = 4</unknown>
getsockopt(4</unknown>, SOL_SOCKET, SO_PEERCRED, {pid=0, uid=-1, gid=-1}, [12]) = 0
getuid()                                = 1000
getuid()                                = 1000
write(2</dev/pts/1>, "uid mismatch: peer euid 42949672"..., 48) = 48
close(4</unknown>)                      = 0
close(3</unknown>)                      = 0

I'm on RS4 build 17134. /tmp is on an lxfs mountpoint.

Activity

  1. WSLUser commented on May 11, 2018

    @WSLUser

    How did you upgrade? do-release-upgrade or installing the new 18.04 app?

  2. 0xabu commented on May 11, 2018

    @0xabu
    Author

    Nikita Ushakov (@DarthSpock), do-release-upgrade -d

  3. 0xabu commented on May 11, 2018

    @0xabu
    Author

    Some more diagnosis: this bug is caused by initiating an SSH connection that uses agent connection forwarding (ssh -O ForwardAgent=yes). When that connects, it does a test connection to the auth sock:

    connect(9</unknown>, {sa_family=AF_UNIX, sun_path="/tmp/.sshagent.sock"}, 110) = 0
    close(9</unknown>)                      = 0
    

    On the SSH agent side, this one connect/close pair causes the bogus data returned from getsockopt SO_PEERCRED (probably because the peer is already gone by the time of the getsockopt call):

    accept(3</unknown>, {sa_family=AF_UNIX}, [110->2]) = 5</unknown>
    getsockopt(5</unknown>, SOL_SOCKET, SO_PEERCRED, {pid=0, uid=-1, gid=-1}, [12]) = 0
    getuid()                                = 1000
    getuid()                                = 1000
    write(2</dev/pts/1>, "uid mismatch: peer euid 42949672"..., 48) = 48
    close(5</unknown>)                      = 0
    close(3</unknown>)                      = 0
    

    … and after that time the ssh-agent remains alive but is unusable because it closed its end of the socket. This is the only thing that differs between Ubuntu 16.04 and 18.04 -- in earlier versions, the agent would log the error message and drop that specific connection but keep its listening socket open; now (probably for security paranoia) it becomes unusable after one failed connection.

    So there's a simple workaround for now: don't use agent forwarding.

  4. changed the title [-]Unix domain sockets sometimes return spurious data from getsockopt(SO_PEERCRED), breaks ssh-agent for Ubuntu 18.04[/-] [+]Unix domain sockets return spurious data from getsockopt(SO_PEERCRED) if the peer has already departed, which breaks ssh-agent forwarding for Ubuntu 18.04[/+] on May 11, 2018
  5. mdkeehan commented on May 12, 2018

    @mdkeehan

    I have noticed my ssh-agent exhibiting similar behaviour after upgrading to 18.04 using
    do-release-upgrade -d

  6. rarenerd commented on May 15, 2018

    @rarenerd

    I can confirm having the same issue, using Ubuntu 18.04 from the Microsoft Store on a new Windows 10 installation.

  7. rarenerd commented on May 15, 2018

    @rarenerd

    My config is identical to the example in your link. This does not resolve the issue for me.

  8. 0xabu commented on May 15, 2018

    @0xabu
    Author

    Nikita Ushakov (@DarthSpock), I already have the metadata flag on my drvfs mounts, but I don't think it's relevant here anyway because the ssh-agent socket is on a different lxfs mountpoint.

  9. 0xabu commented on May 15, 2018

    @0xabu
    Author

    Nikita Ushakov (@DarthSpock) the issue is the new socket close call in the error-handling path of the ssh-agent binary. Why would that be any different from a clean install? I'm pretty sure this is just another minor compatibility bug in the implementation of getsockopt.

  10. rarenerd commented on May 15, 2018

    @rarenerd

    Nikita Ushakov (@DarthSpock) I'm doing this on a clean Windows 10 install with a clean Ubuntu 18.04 install. I've since removed the 18.04 install and installed 16.04 where I don't have this issue.

  11. 0xabu commented on May 15, 2018

    @0xabu
    Author

    It's a bug in WSL.

  12. rarenerd commented on Jun 5, 2018

    @rarenerd

    Is there anything I can do to help progress this ticket? Anything needed to reproduce the issue?

  13. WSLUser commented on Jun 5, 2018

    @WSLUser
  14. d-rez commented on Jun 26, 2018

    @d-rez

    I can confirm a clean installation of Ubuntu 18.04 on 1803 build of Windows 10 results in this problem (WSL was installed after updating to 1803)

  15. Brian-Perkins commented on Jun 28, 2018

    @Brian-Perkins
    Contributor

    There were changes in Insider Build 17704 that may address this.

  16. 24 remaining items

  17. petersalomonsen commented on Aug 9, 2019

    @petersalomonsen

    Thanks ADotOut . This was really helpful.

  18. HarikrishnanBalagopal commented on Sep 18, 2019

    @HarikrishnanBalagopal

    ssh-agent is still broken in the latest ubuntu 18 from the windows store.

    $ echo $SSH_AUTH_SOCK
    
    

    the environment variable is not being set when you close a Ubuntu terminal and reopen it.
    I tried the script ADotOut posted but the environment variable is still not being set.
    This is annoying for a lot of reasons, especially because you cannot use ssh-keys to log into servers anymore.

  19. tmillican commented on Oct 7, 2019

    @tmillican

    Hari (@HarikrishnanBalagopal) :

    If you're still having this issue, double-check that ssh-agent is actually running and listening to the socket specified by $SSH_AUTH_SOCK. In my .bashrc I have:

    if [ ! -S ~/.ssh/ssh_auth_sock ]; then
      eval `ssh-agent`
      ln -sf "$SSH_AUTH_SOCK" ~/.ssh/ssh_auth_sock
    fi
    export SSH_AUTH_SOCK=~/.ssh/ssh_auth_sock
    [ -z "`ssh-add -l | grep 'somekey'`" ] && ssh-add ~/.ssh/somekey
    

    I started getting "Error connecting to agent: Connection refused" after an automatic system reboot, so I checked, and sure enough, the socket in /tmp had somehow persisted across a reboot. Obviously the listening ssh-agent instance didn't, hence the error. I don't yet know if this is a reproducible issue, but if you have similar logic in your .bashrc, it might be the cause of your problem.

  20. shaunthegeek commented on Oct 8, 2019

    @shaunthegeek

    add these code to ~/.bashrc

    [ -x /usr/bin/ssh-agent ] && eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    
  21. N0xFF commented on Dec 24, 2019

    @N0xFF

    add these code to ~/.bashrc

    [ -x /usr/bin/ssh-agent ] && eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    

    You create a new process each time you open bash. We can reuse one ssh-agent instance:

    if ! pgrep ssh-agent > /dev/null; then
      rm -f /tmp/ssh-auth-sock
      eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
    else
      export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
    fi
    ssh-add
  22. Morgy93 commented on Jan 15, 2020

    @Morgy93

    You create a new process each time you open bash. We can reuse one ssh-agent instance:

    if [ ! -S /tmp/ssh-auth-sock ]; then
      eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
    else
      export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
    fi
    ssh-add

    This worked fine until I restarted the pc.

    Now I'm only left with:
    Error connecting to agent: Connection refused

    $ cat /tmp/ssh-auth-sock 
    cat: /tmp/ssh-auth-sock: No such device or address
    
    $ eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
    bind: Address already in use
    unix_listener: cannot bind to path: /tmp/ssh-auth-sock
    

    Any idea?

  23. N0xFF commented on Jan 15, 2020

    @N0xFF

    Thomas Hauschild (@Morgy93) I updated script above and replace socket file lookup with a process.

  24. lmachens commented on Feb 7, 2020

    @lmachens

    I can approve that the script is working fine to reuse one ssh-agent.
    But you are calling ssh-add even if the ssh-agent exists.
    I would only call it only once:

    if ! pgrep ssh-agent > /dev/null; then
      rm -f /tmp/ssh-auth-sock
      eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
      ssh-add
    else
      export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
    fi

    There is still the issue that you have to enter the password after a restart. Any idea how to solve that?

  25. Morgy93 commented on Feb 7, 2020

    @Morgy93

    There's by the way some "official" script in the docs:

    Linux:

    To start the SSH Agent in the background, run:

    eval "$(ssh-agent -s)"

    To start the SSH Agent automatically on login, add these lines to your ~/.bash_profile:

    if [ -z "$SSH_AUTH_SOCK" ]
    then
       # Check for a currently running instance of the agent
       RUNNING_AGENT="`ps -ax | grep 'ssh-agent -s' | grep -v grep | wc -l | tr -d '[:space:]'`"
       if [ "$RUNNING_AGENT" = "0" ]
       then
            # Launch a new instance of the agent
            ssh-agent -s &> .ssh/ssh-agent
       fi
       eval `cat .ssh/ssh-agent`
    fi
    

    Source: https://code.visualstudio.com/docs/remote/troubleshooting#_setting-up-the-ssh-agent

  26. pearcec commented on Mar 26, 2020

    @pearcec

    Try this:

    if ! pgrep ssh-agent > /dev/null; then
    rm -f /tmp/ssh-auth-sock
    eval "$(ssh-agent -s -a /tmp/ssh-auth-sock)"
    fi
    export SSH_AUTH_SOCK=/tmp/ssh-auth-sock
    if ! ssh-add -l; then
    ssh-add
    fi

  27. istvan-ujjmeszaros commented on Oct 6, 2020

    @istvan-ujjmeszaros

    Actually non of the above scripts are working in WSL2 Ubuntu 18.04.
    The only good solution for me so far was when I simply added eval "$(ssh-agent -s)" to my .bash_profile.

  28. CoolCold commented on Oct 7, 2020

    @CoolCold

    this snippet I currently use in my .bashrc.local (as I often copy .bashrc and other dot files to servers, splitting into separate .bashrc.local ensures desktop specific settings doesn't hit remote servers)

    #setting ssh agent
    ssh-add -l &>/dev/null
    if [ "$?" == 2 ]; then
      test -r ~/.ssh-agent && \
        eval "$(<~/.ssh-agent)" >/dev/null
    fi
    ssh-add -l &>/dev/null
    if [ "$?" == 2 ]; then
        (umask 066; ssh-agent > ~/.ssh-agent)
        eval "$(<~/.ssh-agent)" >/dev/null
    fi
    ssh-add -l &>/dev/null
    if [ "$?" == 1 ]; then
        ssh-add .ssh/mykey.id_rsa.key
    fi
    

    .bashrc part to include local file:

    if [ -f ~/.bashrc.local ]; then
        . ~/.bashrc.local
    fi
    

    .ssh/config to learn new keys once used:

    Host *
      ControlMaster auto
      ControlPath /tmp/%r@%h:%p
      ServerAliveInterval 30
      ServerAliveCountMax 10
      AddKeysToAgent yes
    

    all that works fine for me on WSL2 with Ubuntu 20.04

  29. lucas-langa commented on Jul 27, 2022

    @lucas-langa

    Actually non of the above scripts are working in WSL2 Ubuntu 18.04. The only good solution for me so far was when I simply added eval "$(ssh-agent -s)" to my .bash_profile.

    Anyone in 2022 and beyond running wsl2, can confirm this worked for me!

  30. Adriano-Dos-Santos commented on Oct 22, 2022

    @Adriano-Dos-Santos

    Actually non of the above scripts are working in WSL2 Ubuntu 18.04. The only good solution for me so far was when I simply added eval "$(ssh-agent -s)" to my .bash_profile.

    This worked for me on 2022 using wsl2

  31. JJDSNT commented on Jan 29, 2025

    @JJDSNT

    under WSL:
    add to your ~/.bashrc or ~/.zshrc

    eval $(ssh-agent -s) > /dev/null
    ssh-add ~/.ssh/github_key &> /dev/null
    

    change github_key for the name of the ssh key you want to use

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions