diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-10-26 20:29:52 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2014-10-26 20:29:52 +0900 |
commit | df2cf837abd32bbe04c0916c758ceadf8b3cdff6 (patch) | |
tree | dc1efded509c40cb6ab541a91c8277e1fd865f47 | |
parent | 1f2b24e8343669a6775cfd57a6755c43a09bb420 (diff) |
packet lib: don't crash with truncated dhcp packet
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | ryu/lib/packet/dhcp.py | 9 | ||||
-rw-r--r-- | ryu/tests/unit/packet/test_dhcp.py | 4 |
2 files changed, 12 insertions, 1 deletions
diff --git a/ryu/lib/packet/dhcp.py b/ryu/lib/packet/dhcp.py index c8fb16a9..5320079a 100644 --- a/ryu/lib/packet/dhcp.py +++ b/ryu/lib/packet/dhcp.py @@ -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, diff --git a/ryu/tests/unit/packet/test_dhcp.py b/ryu/tests/unit/packet/test_dhcp.py index 2581e7e1..44a10c50 100644 --- a/ryu/tests/unit/packet/test_dhcp.py +++ b/ryu/tests/unit/packet/test_dhcp.py @@ -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 |