Skip to content

Fix double exception in tube.remote when getaddrinfo raise gaierror #2581

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Apr 25, 2025

Before:

[-] Opening connection to pwn.ctf.umasscybersec.org on port 9000: Failed
[ERROR] Could not resolve hostname: 'pwn.ctf.umasscybersec.org'
Traceback (most recent call last):
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 85, in __init__
    self.sock   = self._connect(fam, typ)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 113, in _connect
    for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE):
  File "/usr/lib/python3.10/socket.py", line 955, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/ctfs/2025/umass/pwn/calc/run.py", line 76, in <module>
    r = conn()
  File "/home/user/ctfs/2025/umass/pwn/calc/run.py", line 50, in conn
    r = remote(host, int(port), ssl=args.SSL)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 89, in __init__
    self.error('Could not resolve hostname: %r', host)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/log.py", line 438, in error
    raise PwnlibException(message % args)
pwnlib.exception.PwnlibException: Could not resolve hostname: 'pwn.ctf.umasscybersec.org'

After:

[-] Opening connection to pwn.ctf.umasscybersec.org on port 9000: Failed
[ERROR] Could not resolve hostname: 'pwn.ctf.umasscybersec.org'
Traceback (most recent call last):
  File "/home/user/ctfs/2025/umass/pwn/calc/run.py", line 76, in <module>
    r = conn()
  File "/home/user/ctfs/2025/umass/pwn/calc/run.py", line 50, in conn
    r = remote(host, int(port), ssl=args.SSL)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 89, in __init__
    self.error('Could not resolve hostname: %r', host)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/log.py", line 438, in error
    raise PwnlibException(message % args) from original_exc
pwnlib.exception.PwnlibException: Could not resolve hostname: 'pwn.ctf.umasscybersec.org'

If using log.exception inside except block:

[-] Opening connection to pwn.ctf.umasscybersec.org on port 9000: Failed
[ERROR] Could not resolve hostname: 'pwn.ctf.umasscybersec.org'
    Traceback (most recent call last):
      File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 85, in __init__
        self.sock   = self._connect(fam, typ)
      File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 113, in _connect
        for res in socket.getaddrinfo(self.rhost, self.rport, fam, typ, 0, socket.AI_PASSIVE):
      File "/usr/lib/python3.10/socket.py", line 955, in getaddrinfo
        for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
    socket.gaierror: [Errno -2] Name or service not known
Traceback (most recent call last):
  File "/home/user/ctfs/2025/umass/pwn/calc/run.py", line 76, in <module>
    r = conn()
  File "/home/user/ctfs/2025/umass/pwn/calc/run.py", line 50, in conn
    r = remote(host, int(port), ssl=args.SSL)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 89, in __init__
    self.exception('Could not resolve hostname: %r', host)
  File "/home/user/venv/lib/python3.10/site-packages/pwnlib/tubes/remote.py", line 85, in __init__
    self.sock   = self._connect(fam, typ)
socket.gaierror: [Errno -2] Name or service not known

@tesuji tesuji marked this pull request as ready for review April 25, 2025 15:09
@@ -427,15 +427,15 @@ def warn(self, *args, **kwargs):
"""Alias for :meth:`warning`."""
return self.warning(*args, **kwargs)

def error(self, message, *args, **kwargs):
def error(self, message, *args, original_exc=None, **kwargs):
"""error(message, *args, **kwargs)

To be called outside an exception handler.
Copy link
Contributor Author

@tesuji tesuji Apr 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Edit: We could use log.exception inside except block instead.
Based on this comment, maybe we move the log.error out of except block:

self.error('Could not resolve hostname: %r', host)

@tesuji tesuji changed the title Fix double exception in when getaddrinfo raise gaierror Fix double exception in tube.remote when getaddrinfo raise gaierror Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant