The Wayback Machine - https://web.archive.org/web/20200609120916/https://github.com/Gallopsled/pwntools/issues/1411
Skip to content
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

serialtube misbehaves when opening the serial port fails #1411

Open
krzys-h opened this issue Jan 17, 2020 · 1 comment
Open

serialtube misbehaves when opening the serial port fails #1411

krzys-h opened this issue Jan 17, 2020 · 1 comment
Labels

Comments

@krzys-h
Copy link

@krzys-h krzys-h commented Jan 17, 2020

Hi! I noticed two minor bugs with how failure to open serialtube() is handled:

  1. The exception thrown is serial.serialutil.SerialException rather than PwnlibException (which is the one thrown by all other tubes when opening fails)
  2. After the program exits, another error is thrown in automatically called close() which is caused by earlier unsuccessful initialization

Here is a small test case (make sure the port is actually busy before running this):

pi@raspberrypi:~ $ cat a.py 
#!/usr/bin/python
from pwn import *
import serial
import traceback

log.info("Trying to open a serial port")
try:
    s = serialtube("/dev/serial0", 115200)
except serial.serialutil.SerialException as e:  # <- note wrong exception type here
    log.warn("Failed to open a serial port!")
    log.warn(traceback.format_exc(e))
log.info("After opening serial port")

pi@raspberrypi:~ $ PWNLIB_DEBUG=1 python a.py 
[!] Pwntools does not support 32-bit Python.  Use a 64-bit release.
[*] Trying to open a serial port
[!] Failed to open a serial port!
[!] Traceback (most recent call last):
      File "a.py", line 8, in <module>
        s = serialtube("/dev/serial0", 115200)
      File "/home/pi/.local/lib/python2.7/site-packages/pwnlib/tubes/serialtube.py", line 41, in __init__
        interCharTimeout = 0
      File "/home/pi/.local/lib/python2.7/site-packages/serial/serialutil.py", line 240, in __init__
        self.open()
      File "/home/pi/.local/lib/python2.7/site-packages/serial/serialposix.py", line 268, in open
        raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg))
    SerialException: [Errno 16] could not open port /dev/serial0: [Errno 16] Device or resource busy: '/dev/serial0'
[*] After opening serial port
Traceback (most recent call last):
  File "/home/pi/.local/lib/python2.7/site-packages/pwnlib/tubes/serialtube.py", line 87, in close
    if self.conn:
AttributeError: 'serialtube' object has no attribute 'conn'

Additionally, documentation for the serialtube module is quite lacking (as in, there is basically no documentation and you have to look at the source code to see e.g. how convert_newlines is handled)

@Arusekk
Copy link
Collaborator

@Arusekk Arusekk commented Jan 18, 2020

Would you mind submitting a PR solving some of this? And possibly adding some doctests to ensure this does not break again.

@Arusekk Arusekk added the easy label Feb 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.