packet lib: don't crash with truncated dhcp packet

Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
FUJITA Tomonori 2014-10-26 20:29:52 +09:00
parent 1f2b24e834
commit df2cf837ab
2 changed files with 12 additions and 1 deletions

View File

@ -174,7 +174,7 @@ class dhcp(packet_base.PacketBase):
self.options = options
@classmethod
def parser(cls, buf):
def _parser(cls, buf):
(op, htype, hlen) = struct.unpack_from(cls._HLEN_UNPACK_STR, buf)
buf = buf[cls._HLEN_UNPACK_LEN:]
unpack_str = cls._DHCP_UNPACK_STR % (hlen,
@ -195,6 +195,13 @@ class dhcp(packet_base.PacketBase):
addrconv.ipv4.bin_to_text(giaddr), sname, boot_file),
None, buf[length:])
@classmethod
def parser(cls, buf):
try:
return cls._parser(buf)
except:
return None, None, buf
def serialize(self, payload, prev):
seri_opt = self.options.serialize()
pack_str = '%s%ds' % (self._DHCP_PACK_STR,

View File

@ -128,6 +128,10 @@ class Test_dhcp_offer(unittest.TestCase):
eq_(self.boot_file.ljust(128, '\x00'), res.boot_file)
eq_(str(self.options), str(res.options))
def test_parser_corrupted(self):
buf = self.buf[:128 - (14 + 20 + 8)]
_res = self.dh.parser(buf)
def test_serialize(self):
data = bytearray()
prev = None