A maximum-length hostname must produce a well-formed SOCKS5 request. The request is assembled in a fixed buffer, and the hostname was copied into it without checking that it fit, which overran the buffer and corrupted the adjacent length field. That length was then used for the wri
(node_factory)
| 5105 | |
| 5106 | |
| 5107 | def test_connect_proxy_maxlen_hostname(node_factory): |
| 5108 | """A maximum-length hostname must produce a well-formed SOCKS5 request. |
| 5109 | |
| 5110 | The request is assembled in a fixed buffer, and the hostname was copied |
| 5111 | into it without checking that it fit, which overran the buffer and |
| 5112 | corrupted the adjacent length field. That length was then used for the |
| 5113 | write, so the proxy got a wildly oversized read of connectd's memory |
| 5114 | instead of the request (and connectd died on the way). |
| 5115 | |
| 5116 | A gossiped DNS address reaches the same builder, so this covers that |
| 5117 | path too. |
| 5118 | """ |
| 5119 | # A minimal SOCKS5 server: accept the "no authentication" greeting, |
| 5120 | # then collect whatever request connectd sends us. |
| 5121 | received = [] |
| 5122 | listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 5123 | listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 5124 | listener.bind(('127.0.0.1', 0)) |
| 5125 | listener.listen(1) |
| 5126 | proxyport = listener.getsockname()[1] |
| 5127 | |
| 5128 | def serve(): |
| 5129 | data = b'' |
| 5130 | conn, _ = listener.accept() |
| 5131 | with conn: |
| 5132 | try: |
| 5133 | conn.settimeout(TIMEOUT) |
| 5134 | conn.recv(len(b'\x05\x01\x00')) |
| 5135 | conn.sendall(b'\x05\x00') |
| 5136 | # Deliberately ask for far more than the largest legal |
| 5137 | # request (262 bytes), so an oversized write is visible |
| 5138 | # here rather than silently truncated by us. We stop on the |
| 5139 | # timeout, once connectd has finished writing and is waiting |
| 5140 | # for a reply we're never going to send. |
| 5141 | conn.settimeout(2) |
| 5142 | while len(data) < 65536: |
| 5143 | more = conn.recv(65536) |
| 5144 | if not more: |
| 5145 | break |
| 5146 | data += more |
| 5147 | except OSError: |
| 5148 | pass |
| 5149 | received.append(data) |
| 5150 | |
| 5151 | server = threading.Thread(target=serve, daemon=True) |
| 5152 | server.start() |
| 5153 | |
| 5154 | l1 = node_factory.get_node(options={'proxy': '127.0.0.1:{}'.format(proxyport), |
| 5155 | 'always-use-proxy': 'true'}, |
| 5156 | # Without the fix connectd dies here, and we |
| 5157 | # want that to be a test failure rather than |
| 5158 | # a teardown error. |
| 5159 | may_fail=True, broken_log='.*') |
| 5160 | |
| 5161 | # unresolved.name[256] is what limits us: 255 is the longest we can ask for. |
| 5162 | hostname = 'a' * (255 - len('.example.com')) + '.example.com' |
| 5163 | assert len(hostname) == 255 |
| 5164 |