Skip to content

client: fix nil-part panic in MultipartDeserialize on malformed body#70

Closed
SAY-5 wants to merge 2 commits into
free5gc:mainfrom
SAY-5:fix/multipart-nil-panic-free5gc-1026
Closed

client: fix nil-part panic in MultipartDeserialize on malformed body#70
SAY-5 wants to merge 2 commits into
free5gc:mainfrom
SAY-5:fix/multipart-nil-panic-free5gc-1026

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 23, 2026

Copy link
Copy Markdown
Contributor

Refs free5gc/free5gc#1026.

Problem

MultipartDeserialize in client.go only checked r.NextPart() for io.EOF. When the declared Content-Type: multipart/related body is not valid MIME (missing/malformed boundary, truncated body, plain text, empty) NextPart() returns (nil, err), and the subsequent part.Header.Get("Content-Type") dereferences nil and panics the handler goroutine.

Because the shared openapi client lives behind every NF's MultipartRelatedBinding path, an unauthenticated attacker reaching the SBI port (e.g. SMF's POST /nsmf-pdusession/v1/sm-contexts) can deterministically crash the handler with a single malformed POST.

Fix

  • Propagate non-EOF errors from NextPart() instead of dereferencing nil.
  • Fix the inverted part.Read error check (was if err == nil { return err }) so a legitimate short read doesn't silently return a nil error and so unexpected read errors bubble up.

Test

go build ./... and go test ./... clean. The package has no existing tests for MultipartDeserialize; happy to add a regression test here if maintainers prefer.

SAY-5 added 2 commits April 23, 2026 04:27
MultipartDeserialize() handled r.NextPart() by only checking for io.EOF
and otherwise using the returned *Part unconditionally. When the caller
declares Content-Type: multipart/related but the body is not valid MIME
multipart data (missing/malformed boundary, truncated body, plain text,
etc.), NextPart() returns (nil, <non-EOF error>). The next line
dereferenced nil via part.Header.Get("Content-Type") and panicked the
goroutine handling the request.

The request path starts unauthenticated (POST /nsmf-pdusession/v1/sm-contexts
on SMF and similar MultipartRelatedBinding paths on every NF that uses
this library), so any attacker reaching the SBI port can deterministically
crash the handling goroutine.

Return the NextPart() error to the caller instead of reading nil.Header.
Also fix the part.Read() error check that was inverted (compared err to
nil when it should return on err != nil and tolerate io.EOF).

Refs free5gc/free5gc issue 1026.
Signed-off-by: SAY-5 <SAY-5@users.noreply.github.com>
@SAY-5

SAY-5 commented May 6, 2026

Copy link
Copy Markdown
Contributor Author

Done, trimmed verbose comments and moved context to the PR description. Thanks @roundspring2003!

@roundspring2003

Copy link
Copy Markdown
Contributor

@SAY-5 plz help fix the conflict

@SAY-5

SAY-5 commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the ping. Checking against current main: the nil-part guard and the MultipartDeserialize error wrapping from this PR are already present verbatim in client.go, so the change has effectively landed through another path. Rebasing here would only produce empty commits. This PR can be closed as superseded unless you want it kept for tracking.

@SAY-5 SAY-5 force-pushed the fix/multipart-nil-panic-free5gc-1026 branch from 1b4bc13 to 11361a5 Compare May 31, 2026 02:37
@SAY-5

SAY-5 commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by 615098a (MultipartDeserialize: guard against nil part on malformed body) on main, which already adds the same nil-part / non-EOF-error guard in MultipartDeserialize. Closing as the fix is upstream.

@SAY-5 SAY-5 closed this Jun 2, 2026
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