Skip to content

Commit 832f54f

Browse files
authored
fix(protocols)!: refuse an MH message or MPTCP DSS option whose length does not fit its fields (#2669) (#2683)
MH hands its message Header Len * 8 + 2 octets, and the DSS option the octets its Length declares. A message whose fixed fields need more, or a DSS Length short of the fields its flags give, read the rest as zeros; a DSS Length past them left octets no field took. MH.make and _make_mptcp_dss wrote the length the fields give, so the rebuild differed: a Home Test with Header Len 1 rebuilt from 24 octets to 32, and its 64-octet IPv6 packet to 72. - MH.read refuses a whole message its Header Len cuts short, as RFC 6275 section 9.2 has it discarded and as IPv6-Route type 2 and HIP refuse a short header length. Inside IPv6 the octets stay a Raw payload. - MH.make writes the Header Len a cut capture's info declares, so its short read replays the header as captured, short of the message or not. - TCP._read_mptcp_dss refuses any Length but the RFC 8684 one for its flags, with or without the checksum, as every other registered MPTCP subtype refuses a length the RFC does not give it. - Breaking: MH(raw) and TCP(raw) raise ProtocolError on such input, where they returned a mis-parse. - Tests: one-octet mutations of every registered MH message type, alone and in IPv6, and each cut at every offset of its message; complete DSS options; every DSS flag combination at every length, against RFC 8684 figure 9. The pypcapfile trial's kept-header control cuts a valid MH rather than shortening its Header Len. New modules fail with pcapkit/ reverted; protocol, toolkit, foundation, interface and guard modules pass; captures parse identically to main.
1 parent dd30f0f commit 832f54f

5 files changed

Lines changed: 334 additions & 7 deletions

File tree

‎pcapkit/protocols/internet/mh.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,11 +1395,28 @@ def read(self, length: 'Optional[int]' = None, *, version: 'Literal[4, 6]' = 4,
13951395
Returns:
13961396
Parsed packet data.
13971397

1398+
Raises:
1399+
ProtocolError: If ``Header Len`` declares fewer octets than the fixed
1400+
fields of the message type take, a message :rfc:`6275#section-9.2`
1401+
has the receiver discard.
1402+
1403+
.. versionchanged:: 1.6.0
1404+
1405+
* Such a message is refused, rather than read with the missing fields
1406+
as zeros and rebuilt longer than captured (:issue:`2669`).
1407+
* A header cut short is rebuilt with the ``Header Len`` it declared,
1408+
rather than one worked out from the message (:issue:`2669`).
1409+
13981410
"""
13991411
if length is None:
14001412
length = len(self)
14011413
schema = self.__header__
14021414

1415+
# NOTE: A whole message whose fields run past its Header Len reads the rest as
1416+
# zeros, and its rebuild writes them and a longer Header Len back (#2669).
1417+
if '__short_read__' not in schema.__dict__ and '__short_read__' in schema.data.__dict__:
1418+
raise ProtocolError(f'{self.alias}: [Type {schema.type}] invalid header length: {schema.length}')
1419+
14031420
name = self._lookup_registry(self.__message__, schema.type)
14041421
if isinstance(name, str):
14051422
meth_name = f'_read_msg_{name}'
@@ -1497,9 +1514,15 @@ def make(self,
14971514
# whatever happens to be past the end of the buffer.
14981515
data_val = self._pad_mh_message(data_val)
14991516

1517+
# NOTE: A capture cut short rebuilds with the Header Len it declared, not one
1518+
# worked out from the message, and its short read cuts the rest (#2669).
1519+
header_len = (len(data_val) + 6) // 8 - 1
1520+
if isinstance(data, Data_MH) and data.get('__short_read__') is not None:
1521+
header_len = data.length // 8 - 1
1522+
15001523
return Schema_MH(
15011524
next=next_val,
1502-
length=self._make_uint((len(data_val) + 6) // 8 - 1, bits=8, name='header length'),
1525+
length=self._make_uint(header_len, bits=8, name='header length'),
15031526
type=type_val,
15041527
reserved=self._make_uint(reserved, bits=8, name='reserved octet'),
15051528
chksum=chksum,

‎pcapkit/protocols/transport/tcp.py‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,13 +2020,28 @@ def _read_mptcp_dss(self, schema: 'Schema_MPTCPDSS', *, options: 'Option') -> 'D
20202020
Returns:
20212021
Parsed option data.
20222022
2023+
Raises:
2024+
ProtocolError: If the length is not the one the ``A``, ``a``, ``M`` and
2025+
``m`` flags give, or, with ``M`` set, that plus the 2-octet checksum.
2026+
20232027
Note:
20242028
The reserved field spans the 4 low bits of the subtype octet and the
20252029
3 high bits of the flags octet; it is kept as one 7-bit value. The
20262030
checksum is absent when the option length leaves no room for it,
20272031
i.e., when checksums were not negotiated.
20282032
2033+
.. versionchanged:: 1.6.0
2034+
2035+
A length the flags do not give is refused, rather than read with the
2036+
missing fields as zeros or the extra octets dropped (:issue:`2669`).
2037+
20292038
"""
2039+
flags = schema.flags
2040+
size = 4 + ((8 if flags['a'] else 4) if flags['A'] else 0) \
2041+
+ ((8 if flags['m'] else 4) + 6 if flags['M'] else 0)
2042+
if schema.length != size and not (flags['M'] and schema.length == size + 2):
2043+
raise ProtocolError(f'{self.alias}: [OptNo {schema.kind}] invalid format')
2044+
20302045
data = Data_MPTCPDSS(
20312046
kind=Enum_Option.Multipath_TCP, # type: ignore[arg-type]
20322047
length=schema.length,
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
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()
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
# -*- coding: utf-8 -*-
2+
"""A Multipath TCP DSS option takes only the length its flags give.
3+
4+
GitHub issue #2669. The DSS schema is handed the octets its ``Length``
5+
declares, and the flags say which fields it holds. A length short of them read
6+
the rest as zeros, and one past them left octets no field took; either way the
7+
rebuild wrote the length the flags give, so the segment came back different.
8+
DSS refuses a length :rfc:`8684` does not give it, as every other registered
9+
Multipath TCP subtype does.
10+
11+
Every flag combination is tried at every length, against :rfc:`8684` section 3.3
12+
figure 9, and complete DSS options are changed in one octet at a time; each
13+
segment must be refused or rebuild byte for byte.
14+
15+
Every case builds its own octets in memory and reads no capture.
16+
17+
"""
18+
19+
import itertools
20+
import struct
21+
import unittest
22+
import warnings
23+
24+
from tests._support import reimport_once_per_class
25+
26+
#: Test tier (#1958): a sweep over the DSS flags and lengths.
27+
__tier__ = 'LATEST'
28+
29+
30+
def _tcp(options: bytes) -> bytes:
31+
"""An ACK segment carrying ``options``, padded with NOPs to a 4-octet boundary."""
32+
options += b'\x01' * (-len(options) % 4)
33+
return struct.pack('>HHIIBBHHH', 50000, 80, 1, 0, (5 + len(options) // 4) << 4, 0x10, 8192, 0, 0) + options
34+
35+
36+
def _rfc_length(A: int, a: int, M: int, m: int) -> int:
37+
"""The DSS length of :rfc:`8684` section 3.3 figure 9, without the checksum."""
38+
return 4 + ((8 if a else 4) if A else 0) + ((8 if m else 4) + 4 + 2 if M else 0)
39+
40+
41+
def _mutants(packet: bytes):
42+
"""Each one-octet change to the options, over a spread of values."""
43+
for pos in range(20, len(packet)):
44+
octet = packet[pos]
45+
values = {0x00, 0xff, octet ^ 0x01, octet ^ 0x80, (octet + 1) & 0xff, (octet - 1) & 0xff}
46+
for value in sorted(values - {octet}):
47+
yield pos, packet[:pos] + bytes([value]) + packet[pos + 1:]
48+
49+
50+
class TestTCPMPTCPDSSLength(unittest.TestCase):
51+
"""Pin the DSS lengths parsed, and byte-exact rebuilds of the rest."""
52+
53+
def setUp(self) -> None:
54+
reimport_once_per_class(self)
55+
catcher = warnings.catch_warnings()
56+
catcher.__enter__()
57+
self.addCleanup(catcher.__exit__, None, None, None)
58+
warnings.simplefilter('ignore')
59+
60+
def test_every_flag_combination_at_every_length(self) -> None:
61+
from pcapkit.const.tcp.option import Option as Enum_Option
62+
from pcapkit.protocols.transport.tcp import TCP
63+
from pcapkit.utilities.exceptions import BaseError
64+
65+
for F, m, M, a, A in itertools.product((0, 1), repeat=5):
66+
flags = F << 4 | m << 3 | M << 2 | a << 1 | A
67+
rfc = _rfc_length(A, a, M, m)
68+
lengths = {rfc, rfc + 2} if M else {rfc}
69+
for length in range(41):
70+
# the octets past the declared length read as NOPs
71+
option = bytes([0x1e, length, 0x20, flags]) + b'\x01' * (max(length, rfc + 2) - 4)
72+
raw = _tcp(option)
73+
with self.subTest(flags=f'{flags:#04x}', length=length):
74+
try:
75+
tcp = TCP(raw, len(raw))
76+
except BaseError:
77+
self.assertNotIn(length, lengths)
78+
continue
79+
self.assertIn(length, lengths)
80+
dss = tcp.info.options[Enum_Option.Multipath_TCP]
81+
self.assertEqual(dss.length, length)
82+
self.assertEqual(dss.ack is not None, bool(A))
83+
self.assertEqual(dss.dsn is not None, bool(M))
84+
self.assertEqual(dss.checksum is not None, length == rfc + 2)
85+
self.assertEqual(TCP.from_data(tcp.info).data, raw)
86+
self.assertEqual(TCP.from_data(tcp.info.to_dict()).data, raw)
87+
88+
def test_every_option_changed_in_one_octet(self) -> None:
89+
from pcapkit.const.tcp.mp_tcp_option import \
90+
MPTCPOption as Enum_MPTCPOption
91+
from pcapkit.const.tcp.option import Option as Enum_Option
92+
from pcapkit.protocols.transport.tcp import TCP
93+
from pcapkit.utilities.exceptions import BaseError
94+
95+
shapes = (
96+
{'ack': 1},
97+
{'ack': 1, 'ack_wide': True},
98+
{'dsn': 2, 'ssn': 3, 'dl_len': 4},
99+
{'dsn': 2, 'dsn_wide': True, 'ssn': 3, 'dl_len': 4, 'checksum': b'\x12\x34'},
100+
{'ack': 1, 'dsn': 2, 'ssn': 3, 'dl_len': 4, 'checksum': b'\x12\x34'},
101+
{'ack': 1, 'ack_wide': True, 'dsn': 2, 'dsn_wide': True, 'ssn': 3, 'dl_len': 4},
102+
)
103+
tail = [(Enum_Option.No_Operation, {}), (Enum_Option.No_Operation, {}),
104+
(Enum_Option.Timestamps, {'value': 5, 'reply': 6})]
105+
for shape in shapes:
106+
option = (Enum_Option.Multipath_TCP, dict(shape, subtype=Enum_MPTCPOption.DSS))
107+
for options in ([option], [option] + tail):
108+
whole = TCP(srcport=50000, dstport=80, ack=True, options=options).data
109+
for pos, raw in _mutants(whole):
110+
with self.subTest(shape=shape, pos=pos, raw=raw.hex()):
111+
try:
112+
tcp = TCP(raw, len(raw))
113+
except BaseError:
114+
continue
115+
self.assertEqual(tcp.data, raw)
116+
self.assertEqual(TCP.from_data(tcp.info).data, raw)
117+
self.assertEqual(TCP.from_data(tcp.info.to_dict()).data, raw)
118+
119+
120+
if __name__ == '__main__':
121+
unittest.main()

‎tests/toolkit/test_pypcapfile_extension_header_trial_2526_unit.py‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131
HIP, MOBILITY_HEADER = 139, 135
3232
#: The first 8 octets of a HIP header, whose fixed part is 40.
3333
HIP_CUT = bytes.fromhex('b0ff89ff0042ff1c')
34-
#: A Mobility Header of 8 octets, 16 short of the Binding Error message it
35-
#: carries, then 1 octet of its UDP payload.
36-
MH_BINDING_ERROR = bytes.fromhex('11000742d5bd297b99')
37-
#: The warning the Mobility Header's message reports.
34+
#: A Mobility Header declaring 24 octets, cut to its first 8, so 16 short of
35+
#: the Binding Error message it carries. A Header Len short of the message
36+
#: would be refused (#2669).
37+
MH_BINDING_ERROR = bytes.fromhex('11020742d5bd297b')
38+
#: The warning the cut Mobility Header reports.
3839
MH_WARNING = 'packet length < 0: -16'
3940

4041

@@ -79,7 +80,7 @@ def test_error_filter_answers_none(self) -> None:
7980
self.assertEqual(caught, [])
8081

8182
def test_kept_header_warns_from_the_callers_line(self) -> None:
82-
"""Control: a Mobility Header the trial keeps reports its message's warning."""
83+
"""Control: a Mobility Header the trial keeps reports its warning."""
8384
from pcapkit.toolkit.pypcapfile import _default_extension_header
8485
from pcapkit.utilities.warnings import SchemaWarning
8586

@@ -88,7 +89,7 @@ def test_kept_header_warns_from_the_callers_line(self) -> None:
8889
warnings.simplefilter('always')
8990
line = inspect.currentframe().f_lineno + 1 # type: ignore[union-attr]
9091
answer = _default_extension_header(MOBILITY_HEADER, MH_BINDING_ERROR)
91-
self.assertEqual(answer, (17, 8))
92+
self.assertEqual(answer, (17, 24))
9293
self.assertEqual([(item.category, str(item.message), item.filename, item.lineno) for item in caught],
9394
[(SchemaWarning, MH_WARNING, __file__, line)])
9495

0 commit comments

Comments
 (0)