Skip to content

bounds-check dns response parsing against packet end#1628

Open
rootvector2 wants to merge 1 commit into
Moddable-OpenSource:publicfrom
rootvector2:dns-parser-packet-bounds
Open

bounds-check dns response parsing against packet end#1628
rootvector2 wants to merge 1 commit into
Moddable-OpenSource:publicfrom
rootvector2:dns-parser-packet-bounds

Conversation

@rootvector2

Copy link
Copy Markdown
Contributor

parseQname follows a compression pointer and copies label bytes with no packet-end check, and the TXT loop in parseQuestionOrAnswer underflows rdlength when a sub-string length exceeds it, so a malformed DNS response read from the resolver's UDP socket reads past the packet buffer.

@rootvector2

Copy link
Copy Markdown
Contributor Author

@phoddie

@phoddie

phoddie commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the PR.

  • It identifies some memory safety issues. That's great.
  • It missed some others - most notably the xs_dnspacket_get_* functions don't validate that the header is fully present.
  • Some of the changes to optimize refresh of the packet pointer will break, because packet can move during certain operations
  • Some of fixes are more invasive / complicate than strictly necessary.

I drafted an alternative solution. You can see that as a comparison with the current Moddable SDK version in this gist.

@rootvector2

Copy link
Copy Markdown
Contributor Author

you're right on all three. the refresh optimization is the real bug: xsCall1(..., xsID_push, ...) can move the backing store, so caching packet across the label loop leaves position pointing into freed memory. i was treating the buffer as fixed, which it isn't.

centralizing the < 12 check in getPacket is cleaner than guarding each xs_dnspacket_get_*, and the while (position < packetEnd) conditions read better than the explicit early returns i added.

your draft covers everything mine does plus the header case. happy to defer to it. want me to push it to this branch, or will you land it directly?

@phoddie

phoddie commented Jun 12, 2026

Copy link
Copy Markdown
Collaborator

Thanks for reviewing. If you want to update the PR, that would be fine. You started the effort.

@rootvector2 rootvector2 force-pushed the dns-parser-packet-bounds branch from 6bb1f66 to 5c77000 Compare June 13, 2026 06:43
@rootvector2

Copy link
Copy Markdown
Contributor Author

pushed your draft to the branch, squashed into the one commit so the diff stays just the bounds checks. the < 12 check in getPacket and re-reading packet/packetEnd after each getPacket cover the header and pointer-move cases mine missed. reads clean to me.

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

Successfully merging this pull request may close these issues.

2 participants