Repository navigation
Conversation
Curl_mime_duppart() duplicates multipart structures recursively when cloning an easy handle via curl_easy_duphandle(). Previously, this traversal did not enforce a recursion limit, allowing an excessively deep MIME tree to cause unbounded recursion and stack exhaustion. Bound recursion in mime_duppart() to MAX_MIME_LEVELS, returning CURLE_TOO_LARGE when exceeded, following the precedent set for mime_prepare_headers() in c427e91. Extended test 3596 to verify that curl_easy_duphandle() safely fails for deeply nested MIME trees.
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
Rollback after exceeding the depth limit can invoke copied user free callbacks and leave the original MIME tree referencing freed resources.
Get a fresh assessment by requesting another Copilot review.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Bounds MIME duplication recursion to prevent stack exhaustion during easy-handle cloning.
Changes:
- Enforces
MAX_MIME_LEVELSduring MIME duplication. - Tests deeply nested duplication before and after transfer.
| File | Description |
|---|---|
lib/mime.c |
Adds bounded recursive MIME duplication. |
tests/libtest/lib3596.c |
Adds deep-nesting duplication checks. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Preflight the source MIME tree depth before allocating or duplicating any parts or copying user callbacks. If the tree exceeds MAX_MIME_LEVELS, fail immediately with CURLE_TOO_LARGE. This ensures error rollback never invokes copied user free callbacks on sibling parts before an over-deep branch, preventing premature release or double-free of user callback resources. Extended test 3596 with a callback sibling part to verify that free callbacks are preserved and not invoked during failed duphandle rollback.
filtede98
force-pushed
the
fix-mime-duppart-recursion
branch
from
September 22, 2026 07:03
697c148 to
50e4e4b
Compare
Member
|
Thanks! |
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.

Summary
When cloning an easy handle containing a multipart MIME structure via
curl_easy_duphandle(),dupset()invokesCurl_mime_duppart(), which recursively traverses and duplicates subparts. Previously, this recursive duplication had no call depth bound. An application handling deeply nested MIME trees could trigger unbounded recursion, leading to stack exhaustion (CWE-674) and process termination.This PR bounds recursion in
mime_duppart()toMAX_MIME_LEVELS(40), returningCURLE_TOO_LARGEwhen exceeded, matching the depth limit and failure behavior introduced in commit c427e91 formime_prepare_headers().Changes
lib/mime.c:static CURLcode mime_duppart(..., size_t call_depth)with a check againstMAX_MIME_LEVELS.call_depthwhen recursively duplicatingMIMEKIND_MULTIPARTsubparts.Curl_mime_duppart()as a wrapper passing initial depth 0, preserving the internal API signature.tests/libtest/lib3596.c:curl_easy_duphandle()safely fails (returnsNULL) on deeply nested MIME trees before and aftercurl_easy_perform().Verification
perl scripts/checksrc.plon both modified files (lib/mime.candtests/libtest/lib3596.c) with zero warnings.