Skip to content

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

Open
SAY-5 wants to merge 1 commit intofree5gc:mainfrom
SAY-5:fix/multipart-nil-panic-free5gc-1026
Open

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

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

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

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.

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.
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.

1 participant