Skip to content

Commit 388db14

Browse files
committed
Constrain WKS bitmap size.
(cherry picked from commit a62e1be)
1 parent 7e1832f commit 388db14

3 files changed

Lines changed: 43 additions & 2 deletions

File tree

‎dns/rdtypes/IN/WKS.py‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ def __init__(self, rdclass, rdtype, address, protocol, bitmap):
4545
self.address = self._as_ipv4_address(address)
4646
self.protocol = self._as_uint8(protocol)
4747
self.bitmap = self._as_bytes(bitmap)
48+
if len(self.bitmap) > 8192:
49+
raise ValueError(
50+
"service bitmap must be between 0 and 8192 bytes inclusive"
51+
)
4852

4953
def to_styled_text(self, style: dns.rdata.RdataStyle) -> str:
5054
bits = []
@@ -78,11 +82,12 @@ def from_text(
7882
else:
7983
protocol_text = "tcp"
8084
serv = socket.getservbyname(value, protocol_text)
85+
if serv < 0 or serv > 65535:
86+
raise ValueError("service must be between 0 and 65535 inclusive")
8187
i = serv // 8
8288
l = len(bitmap)
8389
if l < i + 1:
84-
for _ in range(l, i + 1):
85-
bitmap.append(0)
90+
bitmap.extend(bytes(i + 1 - l))
8691
bitmap[i] = bitmap[i] | (0x80 >> (serv % 8))
8792
bitmap = dns.rdata._truncate_bitmap(bitmap)
8893
return cls(rdclass, rdtype, address, protocol, bitmap)

‎doc/whatsnew.rst‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,28 @@
33
What's New in dnspython
44
=======================
55

6+
2.9.2
7+
-----
8+
9+
* A maliciously crafted zone file could use a WKS record to consume excessive
10+
memory and CPU, possibly causing the process to exit due to memory exhaustion.
11+
Thanks to Jace (GitHub @manus-use) for discovering and reporting this issue.
12+
613
2.9.1
714
-----
815

16+
* LOC text parsing now converts the altitude, size, horizontal precision, and
17+
vertical precision fields to centimeters exactly, and requires them to be
18+
decimal numbers of meters with at most two fractional digits, as RFC 1876
19+
section 3 specifies. They were converted with ``float()``, so ``9.29m``
20+
became 928 cm rather than 929 cm and the text and wire forms of the record
21+
disagreed, forms such as ``1e3m``, ``1_000m``, ``+10m``, ``nanm``, and
22+
``infm`` were accepted, extra fractional digits were silently dropped, and an
23+
altitude outside the representable range was accepted and then failed in
24+
``to_wire()`` with a non-DNS exception. ``dns.exception.SyntaxError`` is
25+
now raised for all of these.
26+
>>>>>>> a62e1be0 (Constrain WKS bitmap size.)
27+
928
* The resolver now assigns a new random query id to every query.
1029

1130
* For transports that require a 0 query id, set it in a way that does not break

‎tests/test_rdata.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from dns.rdtypes.ANY.GPOS import GPOS
4646
from dns.rdtypes.ANY.LOC import LOC
4747
from dns.rdtypes.ANY.OPT import OPT
48+
from dns.rdtypes.IN.WKS import WKS
4849
from tests.util import here
4950

5051

@@ -511,6 +512,22 @@ def test_misc_bad_WKS_text(self):
511512
except dns.exception.SyntaxError as e:
512513
self.assertIsInstance(e.__cause__, NotImplementedError)
513514

515+
def test_wks_bounds(self):
516+
rd = dns.rdata.from_text("IN", "WKS", "10.0.0.1 udp 0")
517+
self.assertEqual(rd.bitmap, b"\x80")
518+
rd = dns.rdata.from_text("IN", "WKS", "10.0.0.1 udp 65535")
519+
self.assertEqual(len(rd.bitmap), 8192)
520+
self.assertEqual(rd.bitmap[-1], 1)
521+
with self.assertRaises(dns.exception.SyntaxError):
522+
rd = dns.rdata.from_text("IN", "WKS", "10.0.0.1 udp -1")
523+
with self.assertRaises(dns.exception.SyntaxError):
524+
rd = dns.rdata.from_text("IN", "WKS", "10.0.0.1 udp 65536")
525+
# test direct invocation of the constructor too
526+
rd = WKS(dns.rdataclass.IN, dns.rdatatype.WKS, "10.0.0.1", 1, bytes())
527+
rd = WKS(dns.rdataclass.IN, dns.rdatatype.WKS, "10.0.0.1", 1, bytes(8192))
528+
with self.assertRaises(ValueError):
529+
WKS(dns.rdataclass.IN, dns.rdatatype.WKS, "10.0.0.1", 1, bytes(8193))
530+
514531
def test_GPOS_float_converters(self):
515532
rd = dns.rdata.from_text("in", "gpos", "49 0 0")
516533
self.assertEqual(rd.float_latitude, 49.0)

0 commit comments

Comments
 (0)