Limit MIME nesting depth to prevent unbounded recursion#59
Open
iliaal wants to merge 1 commit into
Open
Conversation
A message that nests one child part per level (e.g. repeated "Content-Type: message/rfc822" parts, or singly-nested multipart containers) was bounded only by MAXPARTS, which counts children per part and never trips for a one-child-per-level chain. Parse depth was therefore attacker-controlled and unbounded. php_mimepart_enum_parts() / enum_parts_recurse() walk the tree recursively, so a deep enough message overflowed the C stack (SIGSEGV) on mailparse_msg_get_structure(), mailparse_msg_get_part() and the extract paths. The descent in php_mimepart_process_line() also made parsing such a message quadratic. Enforce the existing (previously unused) MAXLEVELS cap in alloc_new_child_part(): refuse to create a child past the limit and fail the parse, matching the existing MAXPARTS handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
mailparse never enforced a limit on MIME nesting depth. MAXLEVELS (20) has been defined but never referenced; the only structural check is MAXPARTS, which counts a single part's children and so never trips for a chain that nests one child per level. A message built from repeated "Content-Type: message/rfc822" parts, or singly-nested multipart containers, produces an arbitrarily deep tree. php_mimepart_enum_parts() walks that tree recursively, so mailparse_msg_get_structure(), mailparse_msg_get_part() and the extract paths overflow the C stack and crash. This enforces MAXLEVELS in alloc_new_child_part() and fails the parse once the limit is reached, the same way MAXPARTS is already handled.
Reproducer: mailparse_msg_get_structure() on a message of ~100k repeated "Content-Type: message/rfc822\n\n" parts segfaults before the fix.