Skip to content

Connection attempts aborted prematurely due to unforeseen EWHATEVERS while iterating over addresses #1167

Open

Description

@smheidrich

At my place of work, there is an admittedly weird network configuration that causes the first few addresses returned by getaddrinfo for a certain host to yield EACCES ("Permission denied") errors when trying to connect:

smheidrich@host:~$ ssh -v CENSORED_USER@CENSORED_HOST
OpenSSH_7.4p1 Debian-10+deb9u2, OpenSSL 1.0.2l  25 May 2017
debug1: Reading configuration data /home/smheidrich/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 19: Applying options for *
debug1: Connecting to CENSORED_HOST [CENSORED_IPV6_ADDRESS1] port 22.
debug1: connect to address CENSORED_IPV6_ADDRESS1 port 22: Permission denied
debug1: Connecting to CENSORED_HOST [CENSORED_IPV6_ADDRESS2] port 22.
debug1: connect to address CENSORED_IPV6_ADDRESS2 port 22: Permission denied
debug1: Connecting to CENSORED_HOST [CENSORED_IPV6_ADDRESS3] port 22.
debug1: connect to address CENSORED_IPV6_ADDRESS3 port 22: Permission denied
debug1: Connecting to CENSORED_HOST [CENSORED_IPV6_ADDRESS4] port 22.
debug1: connect to address CENSORED_IPV6_ADDRESS4 port 22: Permission denied
debug1: Connecting to CENSORED_HOST [CENSORED_IPV6_ADDRESS5] port 22.
debug1: connect to address CENSORED_IPV6_ADDRESS5 port 22: Permission denied
debug1: Connecting to CENSORED_HOST [CENSORED_IPV6_ADDRESS6] port 22.
debug1: Connection established.

As you can see, OpenSSH's client just keeps iterating over addresses returned by getaddrinfo when this happens, until it finally finds one that doesn't yield an error. The relevant section of the code is sshconnect.c, ll. 433-475, from which it is evident - as far as I can tell - that it only ever breaks out of the loop if a connection attempt was successful.

Paramiko, on the other hand, gives up as soon as a connection attempt yields something other than ECONNREFUSED or EHOSTUNREACH (relevant line: client.py, l. 344), which means I can't use it to connect to the host in question:

>>> import paramiko
>>> client = paramiko.SSHClient()
>>> client.load_system_host_keys()
>>> client.connect("CENSORED_HOST", username="CENSORED_USER")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/smheidrich/.local/lib/python3.5/site-packages/paramiko/client.py", line 338, in connect
    retry_on_signal(lambda: sock.connect(addr))
  File "/home/smheidrich/.local/lib/python3.5/site-packages/paramiko/util.py", line 279, in retry_on_signal
    return function()
  File "/home/smheidrich/.local/lib/python3.5/site-packages/paramiko/client.py", line 338, in <lambda>
    retry_on_signal(lambda: sock.connect(addr))
PermissionError: [Errno 13] Permission denied

So I would like to propose either adding EACCES to the list of "allowed" errors in client.py, l. 344, or perhaps even doing what OpenSSH does and continuing the iteration over addresses no matter what kinds of errors occur - as long as they are related in any way to the socket, of course. Or is there a problem I'm not seeing here?

Unfortunately, I can't give you a way to reproduce this (trying to induce the situation artificially at home with iptables didn't work out), but I hope the argument that Paramiko's behaviour differs from OpenSSH's is convincing enough.

Thanks in advance!


Version info:

  • Python 3.5.3
  • Paramiko 2.4.0, c43202d

Activity

  1. changed the title [-]Connection attempts aborted prematurely due to EACCES while iterating over addresses[/-] [+]Connection attempts aborted prematurely due to unforeseen EWHATEVERS while iterating over addresses[/+] on Aug 1, 2018
  2. bitprophet commented on Aug 1, 2018

    @bitprophet
    Member

    This seems to have come up on IRC too, in a slightly different wrinkle:

    [09:11:31] <plundra> Hi, surely Paramiko is supposed to work with IPv6?
    [09:11:45] <plundra> Doing a simple paramiko.SSHClient().connect(...) I get "OSError: [Errno 49] Can't assign requested address"
    [09:12:53] <plundra> This machine is IPv6 only and hostname in question has both A and AAAA.
    [05:08:39] <plundra> Ok, so on Linux it errors out with ENETUNREACH and OpenBSD EADDRNOTAVAIL, when the host doesn't have IPv4 and there is both an A and AAAA record for the host in question.
    [05:09:29] <plundra> Not having dug deep into it, I'd assume both those should be added to the if e.errno not in-handling next to ECONNREFUSED and EHOSTUNREACH in client.py.

    Sounds real similar - IPv6 is causing errors besides ECONNREFUSED/EHOSTUNREACH to pop up, and Paramiko not knowing that these errors are effectively also recoverable/skip-overable, results in exceptions instead of "🙄 ok, sure thing OS, let's just try the next one eh?"

    Of course at this point I wonder if the original code is in fact way too paranoid and we should be a lot more lenient about what we consider ignorable/continue-able (this is probably very old code, for one thing). But let's take it one step at a time, probably.

  3. added this to the Next bugfix milestone on Aug 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions