Skip to content

Commit

Permalink
do more careful bounds checking, and if checking fails, raise dns.exc…
Browse files Browse the repository at this point in the history
…eption.FormErr
  • Loading branch information
Bob Halley committed Apr 5, 2011
1 parent 0c32043 commit b0ce839
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 59 deletions.
1 change: 1 addition & 0 deletions dns/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@
'rdtypes',
'update',
'version',
'wiredata',
'zone',
]
3 changes: 2 additions & 1 deletion dns/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import dns.rrset
import dns.renderer
import dns.tsig
import dns.wiredata

class ShortHeader(dns.exception.FormError):
"""Raised if the DNS packet passed to from_wire() is too short."""
Expand Down Expand Up @@ -577,7 +578,7 @@ class _WireReader(object):

def __init__(self, wire, message, question_only=False,
one_rr_per_rrset=False):
self.wire = wire
self.wire = dns.wiredata.maybe_wrap(wire)
self.message = message
self.current = 0
self.updating = False
Expand Down
4 changes: 3 additions & 1 deletion dns/name.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import encodings.idna

import dns.exception
import dns.wiredata

NAMERELN_NONE = 0
NAMERELN_SUPERDOMAIN = 1
Expand Down Expand Up @@ -670,6 +671,7 @@ def from_wire(message, current):

if not isinstance(message, str):
raise ValueError("input to from_wire() must be a byte string")
message = dns.wiredata.maybe_wrap(message)
labels = []
biggest_pointer = current
hops = 0
Expand All @@ -678,7 +680,7 @@ def from_wire(message, current):
cused = 1
while count != 0:
if count < 64:
labels.append(message[current : current + count])
labels.append(message[current : current + count].unwrap())
current += count
if hops == 0:
cused += count
Expand Down
2 changes: 2 additions & 0 deletions dns/rdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import dns.rdataclass
import dns.rdatatype
import dns.tokenizer
import dns.wiredata

_hex_chunksize = 32

Expand Down Expand Up @@ -469,5 +470,6 @@ def from_wire(rdclass, rdtype, wire, current, rdlen, origin = None):
@type origin: dns.name.Name
@rtype: dns.rdata.Rdata instance"""

wire = dns.wiredata.maybe_wrap(wire)
cls = get_rdata_class(rdclass, rdtype)
return cls.from_wire(rdclass, rdtype, wire, current, rdlen, origin)
4 changes: 2 additions & 2 deletions dns/rdtypes/ANY/CERT.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ def to_wire(self, file, compress = None, origin = None):
file.write(self.certificate)

def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
prefix = wire[current : current + 5]
prefix = wire[current : current + 5].unwrap()
current += 5
rdlen -= 5
if rdlen < 0:
raise dns.exception.FormError
(certificate_type, key_tag, algorithm) = struct.unpack("!HHB", prefix)
certificate = wire[current : current + rdlen]
certificate = wire[current : current + rdlen].unwrap()
return cls(rdclass, rdtype, certificate_type, key_tag, algorithm,
certificate)

Expand Down
16 changes: 8 additions & 8 deletions dns/rdtypes/ANY/GPOS.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def _validate_float_string(what):
raise dns.exception.FormError
if not right == '' and not right.isdigit():
raise dns.exception.FormError

class GPOS(dns.rdata.Rdata):
"""GPOS record
Expand All @@ -42,7 +42,7 @@ class GPOS(dns.rdata.Rdata):
@see: RFC 1712"""

__slots__ = ['latitude', 'longitude', 'altitude']

def __init__(self, rdclass, rdtype, latitude, longitude, altitude):
super(GPOS, self).__init__(rdclass, rdtype)
if isinstance(latitude, float) or \
Expand All @@ -66,14 +66,14 @@ def __init__(self, rdclass, rdtype, latitude, longitude, altitude):

def to_text(self, origin=None, relativize=True, **kw):
return '%s %s %s' % (self.latitude, self.longitude, self.altitude)

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
latitude = tok.get_string()
longitude = tok.get_string()
altitude = tok.get_string()
tok.get_eol()
return cls(rdclass, rdtype, latitude, longitude, altitude)

from_text = classmethod(from_text)

def to_wire(self, file, compress = None, origin = None):
Expand All @@ -92,30 +92,30 @@ def to_wire(self, file, compress = None, origin = None):
byte = chr(l)
file.write(byte)
file.write(self.altitude)

def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
l = ord(wire[current])
current += 1
rdlen -= 1
if l > rdlen:
raise dns.exception.FormError
latitude = wire[current : current + l]
latitude = wire[current : current + l].unwrap()
current += l
rdlen -= l
l = ord(wire[current])
current += 1
rdlen -= 1
if l > rdlen:
raise dns.exception.FormError
longitude = wire[current : current + l]
longitude = wire[current : current + l].unwrap()
current += l
rdlen -= l
l = ord(wire[current])
current += 1
rdlen -= 1
if l != rdlen:
raise dns.exception.FormError
altitude = wire[current : current + l]
altitude = wire[current : current + l].unwrap()
return cls(rdclass, rdtype, latitude, longitude, altitude)

from_wire = classmethod(from_wire)
Expand Down
12 changes: 6 additions & 6 deletions dns/rdtypes/ANY/HINFO.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class HINFO(dns.rdata.Rdata):
@see: RFC 1035"""

__slots__ = ['cpu', 'os']

def __init__(self, rdclass, rdtype, cpu, os):
super(HINFO, self).__init__(rdclass, rdtype)
self.cpu = cpu
Expand All @@ -36,13 +36,13 @@ def __init__(self, rdclass, rdtype, cpu, os):
def to_text(self, origin=None, relativize=True, **kw):
return '"%s" "%s"' % (dns.rdata._escapify(self.cpu),
dns.rdata._escapify(self.os))

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
cpu = tok.get_string()
os = tok.get_string()
tok.get_eol()
return cls(rdclass, rdtype, cpu, os)

from_text = classmethod(from_text)

def to_wire(self, file, compress = None, origin = None):
Expand All @@ -56,22 +56,22 @@ def to_wire(self, file, compress = None, origin = None):
byte = chr(l)
file.write(byte)
file.write(self.os)

def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
l = ord(wire[current])
current += 1
rdlen -= 1
if l > rdlen:
raise dns.exception.FormError
cpu = wire[current : current + l]
cpu = wire[current : current + l].unwrap()
current += l
rdlen -= l
l = ord(wire[current])
current += 1
rdlen -= 1
if l != rdlen:
raise dns.exception.FormError
os = wire[current : current + l]
os = wire[current : current + l].unwrap()
return cls(rdclass, rdtype, cpu, os)

from_wire = classmethod(from_wire)
Expand Down
4 changes: 2 additions & 2 deletions dns/rdtypes/ANY/HIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
wire[current : current + 4])
current += 4
rdlen -= 4
hit = wire[current : current + lh]
hit = wire[current : current + lh].unwrap()
current += lh
rdlen -= lh
key = wire[current : current + lk]
key = wire[current : current + lk].unwrap()
current += lk
rdlen -= lk
servers = []
Expand Down
4 changes: 2 additions & 2 deletions dns/rdtypes/ANY/ISDN.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
rdlen -= 1
if l > rdlen:
raise dns.exception.FormError
address = wire[current : current + l]
address = wire[current : current + l].unwrap()
current += l
rdlen -= l
if rdlen > 0:
Expand All @@ -82,7 +82,7 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
rdlen -= 1
if l != rdlen:
raise dns.exception.FormError
subaddress = wire[current : current + l]
subaddress = wire[current : current + l].unwrap()
else:
subaddress = ''
return cls(rdclass, rdtype, address, subaddress)
Expand Down
2 changes: 1 addition & 1 deletion dns/rdtypes/ANY/NSEC.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
rdlen -= 2
if rdlen < octets:
raise dns.exception.FormError("bad NSEC bitmap length")
bitmap = wire[current : current + octets]
bitmap = wire[current : current + octets].unwrap()
current += octets
rdlen -= octets
windows.append((window, bitmap))
Expand Down
6 changes: 3 additions & 3 deletions dns/rdtypes/ANY/NSEC3.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
wire[current : current + 5])
current += 5
rdlen -= 5
salt = wire[current : current + slen]
salt = wire[current : current + slen].unwrap()
current += slen
rdlen -= slen
(nlen, ) = struct.unpack('!B', wire[current])
current += 1
rdlen -= 1
next = wire[current : current + nlen]
next = wire[current : current + nlen].unwrap()
current += nlen
rdlen -= nlen
windows = []
Expand All @@ -166,7 +166,7 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
rdlen -= 2
if rdlen < octets:
raise dns.exception.FormError("bad NSEC3 bitmap length")
bitmap = wire[current : current + octets]
bitmap = wire[current : current + octets].unwrap()
current += octets
rdlen -= octets
windows.append((window, bitmap))
Expand Down
2 changes: 1 addition & 1 deletion dns/rdtypes/ANY/NSEC3PARAM.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
wire[current : current + 5])
current += 5
rdlen -= 5
salt = wire[current : current + slen]
salt = wire[current : current + slen].unwrap()
current += slen
rdlen -= slen
if rdlen != 0:
Expand Down
2 changes: 1 addition & 1 deletion dns/rdtypes/ANY/NXT.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
(next, cused) = dns.name.from_wire(wire[: current + rdlen], current)
current += cused
rdlen -= cused
bitmap = wire[current : current + rdlen]
bitmap = wire[current : current + rdlen].unwrap()
if not origin is None:
next = next.relativize(origin)
return cls(rdclass, rdtype, next, bitmap)
Expand Down
6 changes: 3 additions & 3 deletions dns/rdtypes/ANY/SOA.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SOA(dns.rdata.Rdata):

__slots__ = ['mname', 'rname', 'serial', 'refresh', 'retry', 'expire',
'minimum']

def __init__(self, rdclass, rdtype, mname, rname, serial, refresh, retry,
expire, minimum):
super(SOA, self).__init__(rdclass, rdtype)
Expand All @@ -59,7 +59,7 @@ def to_text(self, origin=None, relativize=True, **kw):
return '%s %s %d %d %d %d %d' % (
mname, rname, self.serial, self.refresh, self.retry,
self.expire, self.minimum )

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
mname = tok.get_name()
rname = tok.get_name()
Expand All @@ -73,7 +73,7 @@ def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
tok.get_eol()
return cls(rdclass, rdtype, mname, rname, serial, refresh, retry,
expire, minimum )

from_text = classmethod(from_text)

def to_wire(self, file, compress = None, origin = None):
Expand Down
10 changes: 5 additions & 5 deletions dns/rdtypes/ANY/SSHFP.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class SSHFP(dns.rdata.Rdata):
@see: draft-ietf-secsh-dns-05.txt"""

__slots__ = ['algorithm', 'fp_type', 'fingerprint']

def __init__(self, rdclass, rdtype, algorithm, fp_type,
fingerprint):
super(SSHFP, self).__init__(rdclass, rdtype)
Expand All @@ -43,27 +43,27 @@ def to_text(self, origin=None, relativize=True, **kw):
self.fp_type,
dns.rdata._hexify(self.fingerprint,
chunksize=128))

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
algorithm = tok.get_uint8()
fp_type = tok.get_uint8()
fingerprint = tok.get_string()
fingerprint = fingerprint.decode('hex_codec')
tok.get_eol()
return cls(rdclass, rdtype, algorithm, fp_type, fingerprint)

from_text = classmethod(from_text)

def to_wire(self, file, compress = None, origin = None):
header = struct.pack("!BB", self.algorithm, self.fp_type)
file.write(header)
file.write(self.fingerprint)

def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
header = struct.unpack("!BB", wire[current : current + 2])
current += 2
rdlen -= 2
fingerprint = wire[current : current + rdlen]
fingerprint = wire[current : current + rdlen].unwrap()
return cls(rdclass, rdtype, header[0], header[1], fingerprint)

from_wire = classmethod(from_wire)
Expand Down
10 changes: 5 additions & 5 deletions dns/rdtypes/ANY/X25.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ class X25(dns.rdata.Rdata):
@see: RFC 1183"""

__slots__ = ['address']

def __init__(self, rdclass, rdtype, address):
super(X25, self).__init__(rdclass, rdtype)
self.address = address

def to_text(self, origin=None, relativize=True, **kw):
return '"%s"' % dns.rdata._escapify(self.address)

def from_text(cls, rdclass, rdtype, tok, origin = None, relativize = True):
address = tok.get_string()
tok.get_eol()
return cls(rdclass, rdtype, address)

from_text = classmethod(from_text)

def to_wire(self, file, compress = None, origin = None):
Expand All @@ -46,14 +46,14 @@ def to_wire(self, file, compress = None, origin = None):
byte = chr(l)
file.write(byte)
file.write(self.address)

def from_wire(cls, rdclass, rdtype, wire, current, rdlen, origin = None):
l = ord(wire[current])
current += 1
rdlen -= 1
if l != rdlen:
raise dns.exception.FormError
address = wire[current : current + l]
address = wire[current : current + l].unwrap()
return cls(rdclass, rdtype, address)

from_wire = classmethod(from_wire)
Expand Down
Loading

0 comments on commit b0ce839

Please sign in to comment.