[Git][NTPsec/ntpsec][master] Added detection for cropped packets to decode_packet()

Ian Bruene gitlab at mg.gitlab.com
Wed Jul 19 01:51:37 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
a2fcf68c by Ian Bruene at 2017-07-18T20:41:54-05:00
Added detection for cropped packets to decode_packet()

- - - - -


2 changed files:

- pylib/agentx.py
- tests/pylib/test_agentx.py


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -626,8 +626,10 @@ def decode_context(data, flags):
 
 
 def decode_packet(data):
-    header, data = slicedata(data, 20)
+    header, newData = slicedata(data, 20)
     header = decode_pduheader(header)
+    if header["length"] > len(newData):
+        raise IndexError("Packet data too short")
     if header["version"] != 1:
         raise ValueError("Unknown packet version", header["version"])
     pktType = header["type"]
@@ -637,10 +639,10 @@ def decode_packet(data):
     if decoder is None:
         parsedPkt = None
     else:
-        packetSlice, data = slicedata(data, header["length"])
+        packetSlice, newData = slicedata(newData, header["length"])
         parsedPkt = decoder(packetSlice, header["flags"])
     result = {"header": header, "body": parsedPkt}
-    return result, data
+    return result, newData
 
 
 # Value types


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -1713,7 +1713,7 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
                            "\x0D\xF0\xCE\xFA\x00\x00\x00\x00"),
                          {"version": 1,
                           "type": a.PDU_OPEN,
-                          "flags": {"instReg ": False,
+                          "flags": {"instReg": False,
                                     "newIndex": False,
                                     "anyIndex": False,
                                     "contextP": False,
@@ -2069,6 +2069,16 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
                            "body": {"sys_uptime": 4, "res_err": 5,
                                     "res_index": 6, "varbinds": None}},
                           ""))
+        # Test insufficient data
+        try:
+            f("\x01\x11\x10\x00"
+              "\x00\x00\x00\x01\x00\x00\x00\x02"
+              "\x00\x00\x00\x03\x00\x00\x00\x10"
+              "\x03\x00\x00\x00\x00\x00\x00\x04")
+            fail = False
+        except IndexError:
+            fail = True
+        self.assertEqual(fail, True)
 
 
 if __name__ == "__main__":



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/a2fcf68cd762b478907d1c1288b76c3d16d25c69

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/a2fcf68cd762b478907d1c1288b76c3d16d25c69
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170719/42c0bfe2/attachment.html>


More information about the vc mailing list