|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""The SCTP schema on its own packs a final chunk cut inside its padding as captured (#2680). |
| 3 | +
|
| 4 | +``Schema_SCTP.unpack(raw)`` and ``unpack(raw, len(raw))`` read a final chunk cut inside |
| 5 | +its padding, and packed it back at full width: a DATA chunk of Length 21 cut after one of |
| 6 | +its three pad octets gave 34 octets in and 36 out. ``SCTP.unpack`` recorded the cut only |
| 7 | +when the length given ran past the octets (#2673), so nothing kept the padding short. |
| 8 | +
|
| 9 | +Each final chunk below has a Length that leaves one, two or three pad octets to follow |
| 10 | +(RFC 9260, section 3.2), and each packet is cut at every offset inside that padding. |
| 11 | +Every case builds its own octets. |
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | +import io |
| 16 | +import struct |
| 17 | +import unittest |
| 18 | +import warnings |
| 19 | + |
| 20 | +from tests._support import reimport_once_per_class |
| 21 | +from tests.foundation import _roundtrip as wire |
| 22 | + |
| 23 | +#: Test tier (#1958): a sweep that cuts each packet at every offset of its padding. |
| 24 | +__tier__ = 'LATEST' |
| 25 | + |
| 26 | +#: SCTP common header from port 5000 to 5001 (RFC 9260, section 3.1), its checksum |
| 27 | +#: zero, which nothing here checks. |
| 28 | +COMMON = struct.pack('!HHII', 5000, 5001, 0x11223344, 0) |
| 29 | +#: DATA chunk TSN, stream identifier, stream sequence number and PPID (RFC 9260, |
| 30 | +#: section 3.3.1). |
| 31 | +DATA_HEAD = struct.pack('!IHHI', 7, 1, 2, 0) |
| 32 | +#: INIT fixed part (RFC 9260, section 3.3.2), then a whole IPv4 Address parameter. |
| 33 | +INIT_HEAD = struct.pack('!IIHHI', 0xaabbccdd, 65536, 10, 10, 1) + struct.pack('!HH4s', 5, 8, b'\xc0\x00\x02\x01') |
| 34 | + |
| 35 | + |
| 36 | +def _tlv(code: int, value: bytes) -> bytes: |
| 37 | + """A parameter or error cause, unpadded, its Length excluding the padding (section 3.2.1).""" |
| 38 | + return struct.pack('!HH', code, 4 + len(value)) + value |
| 39 | + |
| 40 | + |
| 41 | +def _chunk(code: int, flags: int, value: bytes) -> bytes: |
| 42 | + """A chunk, unpadded, its Length excluding the padding and the last parameter's (section 3.2).""" |
| 43 | + return struct.pack('!BBH', code, flags, 4 + len(value)) + value |
| 44 | + |
| 45 | + |
| 46 | +def _pad(octets: bytes) -> bytes: |
| 47 | + """``octets`` with the zeros that round them up to four (section 3.2).""" |
| 48 | + return octets + bytes(-len(octets) % 4) |
| 49 | + |
| 50 | + |
| 51 | +def _final(width: int) -> 'dict[str, bytes]': |
| 52 | + """Final chunks whose Length leaves ``width`` pad octets, unpadded, by kind.""" |
| 53 | + size = 4 - width |
| 54 | + return { |
| 55 | + 'data': _chunk(0, 0x03, DATA_HEAD + b'd' * size), |
| 56 | + 'cookie-echo': _chunk(10, 0, b'c' * size), |
| 57 | + # Host Name Address (11), its name ending in a NUL (section 3.3.2.1) |
| 58 | + 'init': _chunk(1, 0, INIT_HEAD + _tlv(11, b'h' * (size - 1) + b'\x00')), |
| 59 | + # Heartbeat Info (1) |
| 60 | + 'heartbeat': _chunk(4, 0, _tlv(1, b'i' * size)), |
| 61 | + # Unrecognized Chunk Type (6) |
| 62 | + 'error': _chunk(9, 0, _tlv(6, b'e' * size)), |
| 63 | + # User-Initiated Abort (12) |
| 64 | + 'abort': _chunk(6, 0x01, _tlv(12, b'a' * size)), |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | +#: Whole chunks ahead of the final one in a multi-chunk packet: a SACK (section |
| 69 | +#: 3.3.4) with no gap blocks, then a DATA chunk with its three pad octets. |
| 70 | +LEADING = _chunk(3, 0, struct.pack('!IIHH', 9, 4096, 0, 0)) + _pad(_chunk(0, 0x03, DATA_HEAD + b'x')) |
| 71 | + |
| 72 | + |
| 73 | +def _cases() -> 'list[tuple[str, int, int, bytes]]': |
| 74 | + """Name, padding width, pad octets kept and octets, for every cut inside the padding.""" |
| 75 | + cases = [] |
| 76 | + for width in (1, 2, 3): |
| 77 | + for kind, chunk in _final(width).items(): |
| 78 | + for shape, ahead in (('single', b''), ('multi', LEADING)): |
| 79 | + whole = COMMON + ahead + _pad(chunk) |
| 80 | + for kept in range(width): |
| 81 | + cases.append((f'{shape}-{kind}', width, kept, whole[:len(whole) - width + kept])) |
| 82 | + return cases |
| 83 | + |
| 84 | + |
| 85 | +def _wholes() -> 'list[tuple[str, int, bytes]]': |
| 86 | + """Name, padding width and octets of every packet above, with all its padding.""" |
| 87 | + return [(f'{shape}-{kind}', width, COMMON + ahead + _pad(chunk)) |
| 88 | + for width in (1, 2, 3) for kind, chunk in _final(width).items() |
| 89 | + for shape, ahead in (('single', b''), ('multi', LEADING))] |
| 90 | + |
| 91 | + |
| 92 | +class SCTPSchemaPaddingCutTests(unittest.TestCase): |
| 93 | + """SCTP packets cut inside the padding of their final chunk, at the schema level.""" |
| 94 | + |
| 95 | + def setUp(self) -> None: |
| 96 | + reimport_once_per_class(self) |
| 97 | + catcher = warnings.catch_warnings() |
| 98 | + catcher.__enter__() |
| 99 | + self.addCleanup(catcher.__exit__, None, None, None) |
| 100 | + warnings.simplefilter('ignore') |
| 101 | + |
| 102 | + def assert_packs_as_captured(self, schema: 'object', raw: bytes) -> None: |
| 103 | + """``schema`` records the cut inside its chunks and packs ``raw`` back.""" |
| 104 | + from pcapkit.protocols.schema.transport.sctp import SCTP |
| 105 | + |
| 106 | + assert isinstance(schema, SCTP) |
| 107 | + self.assertEqual(schema.__dict__['__short_read__'], ('chunks', len(raw) - 12)) |
| 108 | + self.assertEqual(schema.trailer, b'') |
| 109 | + self.assertEqual(bytes(schema), raw) |
| 110 | + self.assertEqual(schema.pack(), raw) |
| 111 | + self.assertEqual(SCTP.from_dict(schema.to_dict()).pack(), raw) |
| 112 | + |
| 113 | + # a field ahead of the cut, changed, still packs no further than the octets read |
| 114 | + schema.vtag = 0x55667788 |
| 115 | + self.assertEqual(schema.pack(), raw[:4] + b'\x55\x66\x77\x88' + raw[8:]) |
| 116 | + |
| 117 | + def test_bytes_cut_inside_the_padding(self) -> None: |
| 118 | + from pcapkit.protocols.schema.transport.sctp import SCTP |
| 119 | + |
| 120 | + for name, width, kept, raw in _cases(): |
| 121 | + for given in (None, len(raw)): |
| 122 | + with self.subTest(case=name, width=width, kept=kept, length=given): |
| 123 | + schema = SCTP.unpack(raw, given) |
| 124 | + # every chunk parsed, the last one's Length short of a multiple of |
| 125 | + # four by its padding (RFC 9260, section 3.2) |
| 126 | + self.assertEqual(len(schema.chunks), 3 if name.startswith('multi') else 1) |
| 127 | + self.assertEqual(-schema.chunks[-1].length % 4, width) |
| 128 | + self.assert_packs_as_captured(schema, raw) |
| 129 | + |
| 130 | + def test_stream_cut_inside_the_padding(self) -> None: |
| 131 | + from pcapkit.protocols.schema.transport.sctp import SCTP |
| 132 | + |
| 133 | + for name, width, kept, raw in _cases(): |
| 134 | + with self.subTest(case=name, width=width, kept=kept, length=None): |
| 135 | + # read from the middle of a stream, to its end |
| 136 | + stream = io.BytesIO(b'\xff' * 3 + raw) |
| 137 | + stream.seek(3) |
| 138 | + schema = SCTP.unpack(stream) |
| 139 | + self.assertEqual(stream.tell(), 3 + len(raw)) |
| 140 | + self.assert_packs_as_captured(schema, raw) |
| 141 | + |
| 142 | + with self.subTest(case=name, width=width, kept=kept, length=len(raw)): |
| 143 | + # the length given stops short of the octets after it |
| 144 | + stream = io.BytesIO(b'\xff' * 3 + raw + b'\xee' * 4) |
| 145 | + stream.seek(3) |
| 146 | + schema = SCTP.unpack(stream, len(raw)) |
| 147 | + self.assertEqual(stream.tell(), 3 + len(raw)) |
| 148 | + self.assert_packs_as_captured(schema, raw) |
| 149 | + |
| 150 | + def test_protocol_info_carries_the_record(self) -> None: |
| 151 | + # as it does when an envelope declares more than the octets (#2673) |
| 152 | + from pcapkit.protocols.transport.sctp import SCTP |
| 153 | + |
| 154 | + for name, width, kept, raw in _cases(): |
| 155 | + with self.subTest(case=name, width=width, kept=kept): |
| 156 | + sctp = SCTP(raw, len(raw)) |
| 157 | + self.assertEqual(sctp.info['__short_read__'], ('chunks', len(raw) - 12)) |
| 158 | + self.assertEqual(sctp.data, raw) |
| 159 | + self.assertEqual(bytes(sctp.__header__), raw) |
| 160 | + self.assertEqual(SCTP.from_data(sctp.info).data, raw) |
| 161 | + self.assertEqual(SCTP.from_data(sctp.info.to_dict()).data, raw) |
| 162 | + |
| 163 | + def test_envelope_declaring_the_octets(self) -> None: |
| 164 | + # an IPv4 Total Length or IPv6 Payload Length that ends where the capture does |
| 165 | + from pcapkit.protocols.internet.ipv4 import IPv4 |
| 166 | + from pcapkit.protocols.internet.ipv6 import IPv6 |
| 167 | + from pcapkit.protocols.transport.sctp import SCTP |
| 168 | + |
| 169 | + for name, width, kept, raw in _cases(): |
| 170 | + for cls, packet in ((IPv4, wire.ipv4(raw, proto=132)), (IPv6, wire.ipv6(raw, nxt=132))): |
| 171 | + with self.subTest(case=name, width=width, kept=kept, via=cls.__name__): |
| 172 | + outer = cls(packet, len(packet)) |
| 173 | + sctp = outer.payload |
| 174 | + self.assertIsInstance(sctp, SCTP) |
| 175 | + self.assertEqual(sctp.info['__short_read__'], ('chunks', len(raw) - 12)) |
| 176 | + self.assertEqual(sctp.__header__.pack(), raw) |
| 177 | + self.assertEqual(cls.from_data(outer.info).data, packet) |
| 178 | + self.assertEqual(cls.from_data(outer.info.to_dict()).data, packet) |
| 179 | + |
| 180 | + def test_whole_padding_is_the_control(self) -> None: |
| 181 | + from pcapkit.protocols.schema.transport.sctp import SCTP |
| 182 | + from pcapkit.protocols.transport.sctp import SCTP as Protocol_SCTP |
| 183 | + |
| 184 | + for name, width, whole in _wholes(): |
| 185 | + for given in (None, len(whole)): |
| 186 | + with self.subTest(case=name, width=width, length=given): |
| 187 | + schema = SCTP.unpack(whole, given) |
| 188 | + self.assertNotIn('__short_read__', schema.__dict__) |
| 189 | + self.assertEqual(schema.trailer, b'') |
| 190 | + self.assertEqual(schema.pack(), whole) |
| 191 | + |
| 192 | + with self.subTest(case=name, width=width, via='protocol'): |
| 193 | + self.assertNotIn('__short_read__', Protocol_SCTP(whole, len(whole)).info) |
| 194 | + |
| 195 | + def test_issue_2680_repro(self) -> None: |
| 196 | + from pcapkit.protocols.schema.transport.sctp import SCTP |
| 197 | + |
| 198 | + data = struct.pack('!BBHIHHI', 0, 3, 21, 7, 1, 2, 0) + b'abcde' |
| 199 | + raw = (struct.pack('!HHI', 5000, 5001, 1) + bytes(4) + data + bytes(3))[:34] |
| 200 | + for given in (None, 34): |
| 201 | + with self.subTest(length=given): |
| 202 | + schema = SCTP.unpack(raw, given) |
| 203 | + self.assertEqual(len(bytes(schema)), 34) |
| 204 | + self.assertEqual(len(schema.pack()), 34) |
| 205 | + |
| 206 | + |
| 207 | +if __name__ == '__main__': |
| 208 | + unittest.main() |
0 commit comments