|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""A whole Mobility Header whose Header Len is short of its message is refused. |
| 3 | +
|
| 4 | +GitHub issue #2669. The message is handed ``Header Len * 8 + 2`` octets, and a |
| 5 | +message type whose fixed fields need more read the rest as zeros. The rebuild |
| 6 | +then wrote a longer Header Len, the zeros, and the octets past it again: a |
| 7 | +Home Test declaring 16 of its 24 octets came back as 32, and its 64-octet IPv6 |
| 8 | +packet as 72. :rfc:`6275#section-9.2` has such a message discarded, as IPv6 |
| 9 | +Routing type 2 and HIP refuse a header length short of their fields. |
| 10 | +
|
| 11 | +A capture that ends inside the header is a cut rather than a short Header Len: |
| 12 | +it is kept, and rebuilt with the Header Len it declared. |
| 13 | +
|
| 14 | +Every registered message type is built whole, each octet of it changed in turn, |
| 15 | +and the result must be refused or rebuild byte for byte, alone and inside IPv6; |
| 16 | +each is cut at every offset of its message, too. |
| 17 | +
|
| 18 | +Every case builds its own octets in memory and reads no capture. |
| 19 | +
|
| 20 | +""" |
| 21 | + |
| 22 | +import struct |
| 23 | +import unittest |
| 24 | +import warnings |
| 25 | + |
| 26 | +from tests._support import reimport_once_per_class |
| 27 | + |
| 28 | +#: Test tier (#1958): a sweep over every registered message type. |
| 29 | +__tier__ = 'LATEST' |
| 30 | + |
| 31 | +#: A Home Test of :rfc:`6275#section-6.1.5`: 18 octets of message data, so |
| 32 | +#: Header Len 2. |
| 33 | +HOME_TEST = bytes([59, 2, 3, 0, 0, 0]) + struct.pack('!H', 7) + b'C' * 8 + b'T' * 8 |
| 34 | + |
| 35 | + |
| 36 | +def _ipv6(body: bytes, length: 'int | None' = None) -> bytes: |
| 37 | + """An IPv6 header declaring ``length`` octets of payload, ``len(body)`` by default.""" |
| 38 | + length = len(body) if length is None else length |
| 39 | + return (bytes([0x60, 0, 0, 0]) + struct.pack('!HBB', length, 135, 64) |
| 40 | + + bytes(15) + b'\x01' + bytes(15) + b'\x02' + body) |
| 41 | + |
| 42 | + |
| 43 | +def _mutants(packet: bytes): |
| 44 | + """Each one-octet change: a spread of values, and Header Len from 0 to 31 too.""" |
| 45 | + for pos, octet in enumerate(packet): |
| 46 | + values = {0x00, 0xff, octet ^ 0x01, octet ^ 0x80, (octet + 1) & 0xff, (octet - 1) & 0xff} |
| 47 | + if pos == 1: |
| 48 | + values.update(range(32)) |
| 49 | + for value in sorted(values - {octet}): |
| 50 | + yield pos, packet[:pos] + bytes([value]) + packet[pos + 1:] |
| 51 | + |
| 52 | + |
| 53 | +class TestMHHeaderLengthMutation(unittest.TestCase): |
| 54 | + """Pin byte-exact rebuilds of MH messages changed in one octet or cut short.""" |
| 55 | + |
| 56 | + def setUp(self) -> None: |
| 57 | + reimport_once_per_class(self) |
| 58 | + catcher = warnings.catch_warnings() |
| 59 | + catcher.__enter__() |
| 60 | + self.addCleanup(catcher.__exit__, None, None, None) |
| 61 | + warnings.simplefilter('ignore') |
| 62 | + |
| 63 | + def _rebuilds(self, raw: bytes, packet: bytes) -> None: |
| 64 | + """``packet`` holds ``raw`` inside IPv6, and rebuilds byte for byte.""" |
| 65 | + from pcapkit.protocols.internet.ipv6 import IPv6 |
| 66 | + |
| 67 | + ipv6 = IPv6(packet, len(packet)) |
| 68 | + self.assertEqual(IPv6.from_data(ipv6.info).data, packet) |
| 69 | + self.assertEqual(IPv6.from_data(ipv6.info.to_dict()).data, packet) |
| 70 | + for ext in ipv6.extension_headers.values(): |
| 71 | + self.assertEqual(type(ext).from_data(ext.info).data, ext.data) |
| 72 | + self.assertEqual(type(ext).from_data(ext.info.to_dict()).data, ext.data) |
| 73 | + |
| 74 | + def test_home_test_short_header_length(self) -> None: |
| 75 | + from pcapkit.protocols.internet.ipv6 import IPv6 |
| 76 | + from pcapkit.protocols.internet.mh import MH |
| 77 | + from pcapkit.protocols.misc.raw import Raw |
| 78 | + from pcapkit.utilities.exceptions import ProtocolError |
| 79 | + |
| 80 | + mh = MH(HOME_TEST, len(HOME_TEST)) |
| 81 | + self.assertEqual(mh.info.length, 24) |
| 82 | + self.assertEqual(mh.info.token, b'T' * 8) |
| 83 | + |
| 84 | + for length in (0, 1): |
| 85 | + raw = HOME_TEST[:1] + bytes([length]) + HOME_TEST[2:] |
| 86 | + with self.subTest(header_len=length): |
| 87 | + with self.assertRaisesRegex(ProtocolError, 'invalid header length'): |
| 88 | + MH(raw, len(raw)) |
| 89 | + |
| 90 | + packet = _ipv6(raw) |
| 91 | + ipv6 = IPv6(packet, len(packet)) |
| 92 | + self.assertIsInstance(ipv6.payload, Raw) |
| 93 | + self.assertRegex(str(ipv6.payload.info.error), 'invalid header length') |
| 94 | + self.assertEqual(IPv6.from_data(ipv6.info).data, packet) |
| 95 | + |
| 96 | + def test_short_header_length_cut_inside_it(self) -> None: |
| 97 | + # Header Len 1 declares 16 of the 24 octets; a capture ending before |
| 98 | + # the 16th is a cut, whose rebuild keeps Header Len 1 |
| 99 | + from pcapkit.protocols.internet.mh import MH |
| 100 | + from pcapkit.utilities.exceptions import ProtocolError |
| 101 | + |
| 102 | + whole = HOME_TEST[:1] + b'\x01' + HOME_TEST[2:] |
| 103 | + for keep in range(3, len(whole) + 1): |
| 104 | + raw = whole[:keep] |
| 105 | + with self.subTest(keep=keep): |
| 106 | + if keep >= 16: |
| 107 | + with self.assertRaisesRegex(ProtocolError, 'invalid header length'): |
| 108 | + MH(raw, len(raw)) |
| 109 | + else: |
| 110 | + mh = MH(raw, len(raw)) |
| 111 | + self.assertEqual(mh.info.length, 16) |
| 112 | + self.assertEqual(MH.from_data(mh.info).data, raw) |
| 113 | + self.assertEqual(MH.from_data(mh.info.to_dict()).data, raw) |
| 114 | + self._rebuilds(raw, _ipv6(raw, len(whole))) |
| 115 | + |
| 116 | + def test_every_message_cut_inside_it(self) -> None: |
| 117 | + # a valid Header Len cut short is kept: only a field reading past the |
| 118 | + # declared header, not past the capture, is refused |
| 119 | + from pcapkit.protocols.internet.ipv6 import IPv6 |
| 120 | + from pcapkit.protocols.internet.mh import MH |
| 121 | + from pcapkit.utilities.exceptions import BaseError |
| 122 | + |
| 123 | + for code in sorted(MH.__message__, key=int): |
| 124 | + whole = MH(type=code, next=59, chksum=b'\x00\x00', data={}).data |
| 125 | + options = MH(whole, len(whole)).info.get('options') |
| 126 | + for keep in range(6, len(whole)): |
| 127 | + raw = whole[:keep] |
| 128 | + with self.subTest(type=code.name, keep=keep): |
| 129 | + try: |
| 130 | + mh = MH(raw, len(raw)) |
| 131 | + except BaseError as exc: |
| 132 | + # a cut in or before mobility options, which their field refuses |
| 133 | + self.assertTrue(options, exc) |
| 134 | + self.assertNotIn('invalid header length', str(exc)) |
| 135 | + parsed = False |
| 136 | + else: |
| 137 | + self.assertEqual(MH.from_data(mh.info).data, raw) |
| 138 | + self.assertEqual(MH.from_data(mh.info.to_dict()).data, raw) |
| 139 | + parsed = True |
| 140 | + |
| 141 | + packet = _ipv6(raw, len(whole)) |
| 142 | + self._rebuilds(raw, packet) |
| 143 | + if parsed and keep >= 8: |
| 144 | + ipv6 = IPv6(packet, len(packet)) |
| 145 | + self.assertIn(135, [int(key) for key in ipv6.extension_headers]) |
| 146 | + |
| 147 | + def test_every_message_changed_in_one_octet(self) -> None: |
| 148 | + from pcapkit.protocols.internet.mh import MH |
| 149 | + from pcapkit.utilities.exceptions import BaseError |
| 150 | + |
| 151 | + for code in sorted(MH.__message__, key=int): |
| 152 | + whole = MH(type=code, next=59, chksum=b'\x00\x00', data={}).data |
| 153 | + for pos, raw in _mutants(whole): |
| 154 | + with self.subTest(type=code.name, pos=pos, raw=raw.hex()): |
| 155 | + try: |
| 156 | + mh = MH(raw, len(raw)) |
| 157 | + except BaseError: |
| 158 | + pass |
| 159 | + else: |
| 160 | + self.assertEqual(mh.data, raw) |
| 161 | + self.assertEqual(MH.from_data(mh.info).data, raw) |
| 162 | + self.assertEqual(MH.from_data(mh.info.to_dict()).data, raw) |
| 163 | + self._rebuilds(raw, _ipv6(raw)) |
| 164 | + |
| 165 | + |
| 166 | +if __name__ == '__main__': |
| 167 | + unittest.main() |
0 commit comments