Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Infinite loop possible in label parsing #18

Open
willscott opened this issue Oct 11, 2014 · 1 comment
Open

Infinite loop possible in label parsing #18

willscott opened this issue Oct 11, 2014 · 1 comment

Comments

@willscott
Copy link

Packet demonstrating this behavior:

var bin = new Buffer('1c5b818000010001000000000f636f6e74656e740264650000010001c00c000100010001517e0004d43e4dcd', 'hex');
var dns = require('native-dns-packet');
dns.parse(bin);

Looks like the packet has a corrupted / malicious label, with a continuation pointer back to itself.

In particular, the loop happens on the question label beginning at offset 12.

@maxmouchet
Copy link

The following patch fixes it, inspired by kapetan/dns#87:

diff --git a/packet.js b/packet.js
index 56b17d9..d8b5c07 100644
--- a/packet.js
+++ b/packet.js
@@ -71,11 +71,17 @@ function nameUnpack(buff) {
   comp = false;
   end = buff.tell();
 
+  var visited = new Set();
+
   while (len !== 0) {
     if (isPointer(len)) {
       len -= LABEL_POINTER;
       len = len << 8;
       pos = len + buff.readUInt8();
+      if (visited.has(pos)) {
+	     throw new Error('loop detected while unpacking name');
+      }
+      visited.add(pos);
       if (!comp)
         end = buff.tell();
       buff.seek(pos);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants