Summary
free5GC's NRF root SBI endpoint POST /oauth2/token contained a parser-level type confusion issue where attacker-controlled form fields were reflected into struct fields using an incorrect assumed type.
By sending a crafted, unauthenticated form-encoded request, an attacker could trigger repeated panics in the access token handler, causing consistent HTTP 500 responses and making the NRF endpoint remotely crashable on demand.
Affected context
The vulnerability occurs in the Network Repository Function (NRF) of free5GC, specifically at the root SBI OAuth2 token endpoint POST /oauth2/token.
It affects deployments where:
- The NRF service is running a version prior to 4.2.2
- The
/oauth2/tokenendpoint is reachable over the network (e.g., from other NFs or potentially less trusted segments) - Form-encoded POST requests are processed using the reflection-based handler in
NFs/nrf/internal/sbi/api_accesstoken.go
The issue is localized to the parsing of access token request fields into the models.NrfAccessTokenAccessTokenReq struct using reflection.
Exploit preconditions
- Attack surface: The NRF SBI HTTP interface, specifically the unauthenticated
POST /oauth2/tokenendpoint. - Required access: Network-level ability to send HTTP requests to the NRF; no valid credentials or prior authentication are required.
- Trigger point: Submission of a crafted
application/x-www-form-urlencodedbody that includes arbitrary field names mapped to values that are parsed asmodels.PlmnId. - Why exploitation is possible:
- The handler reflects over
models.NrfAccessTokenAccessTokenReq, only special-casing plainstringandNrfNfManagementNfTypefields. - All other fields are treated as if they are a single
models.PlmnId, regardless of their actual type. - The parsed
*models.PlmnIdis then assigned viareflect.Value.Set()into whatever field name the attacker provided. - When the real type of the destination field is incompatible (e.g., slice, different struct, primitive), the
reflect.Value.Set()call panics. - Gin's recovery middleware converts these panics into HTTP 500 responses while keeping the endpoint callable, allowing repeated triggering.
Why this happens
The underlying bug stems from unsafe use of reflection combined with insufficient input type validation, aligning with:
- CWE-843: Access of Resource Using Incompatible Type — the code assumes that any non-special-cased field in the access token request struct is a
models.PlmnId, regardless of its declared type. - CWE-20: Improper Input Validation — attacker-controlled field names and values from a form body are not validated against the actual struct schema before assignment.
- CWE-755: Improper Handling of Exceptional Conditions — reflective assignment panics are used as de facto error handling, and while Gin recovers them into HTTP 500 responses, the logic still permits attacker-controlled panics.
At a design level, the handler uses a generic reflection-based mapper instead of a schema-aware parser or explicit binding logic. By treating "all other fields" as models.PlmnId and assigning via reflect.Value.Set() without checking the target field kind (struct vs. slice vs. primitive), the code creates a type confusion path where malformed or unexpected input causes runtime panics that can be triggered remotely.
Potential impact
- Remote triggering of panics in the
/oauth2/tokenhandler, resulting in repeated HTTP 500 responses. - Degradation of NRF reliability and availability due to attacker-controlled bursts of failing token requests.
- Potential cascading impact on other network functions that depend on the NRF for discovery or authorization, causing broader service disruption.
- Increased log noise and operational overhead as monitoring and alerting systems react to repeated 500-level errors.
- A more favorable environment for further probing, where attackers can use repeated panics as a side-channel to infer internal parsing behavior or error paths.
General mitigation guidance
Mitigation focuses on making request parsing schema-aware, type-safe, and resilient to malformed or unexpected input:
- Replace ad-hoc reflection-based assignment with explicit binding logic that maps known request fields to known struct types.
- Introduce strict input validation that:
- Confirms each incoming field name is expected.
- Verifies that data can be safely parsed into the declared field type before assignment.
- When reflection is unavoidable:
- Inspect the
reflect.Kindand concrete type of the destination field and only callSet()when the source value is type-compatible. - Prefer defensive patterns such as
CanSet()checks, type switches, and safe constructors that return errors instead of panics. - Treat parse failures as normal errors and return structured client errors (e.g., 400 Bad Request) rather than allowing runtime panics to occur.
- Apply fail-closed behavior around critical control-plane endpoints so that invalid input is rejected early and cannot degrade the entire service.
- Use principle of least privilege for network exposure, ensuring that sensitive NRF endpoints are reachable only from required network functions or trusted segments.
How teams detect issues like this
Detection can rely on a combination of code-centric and behavior-centric techniques:
- Static analysis and code review:
- Flag use of
reflect.Value.Set(or similar APIs) where the source type is not explicitly checked against the destination field type. - Highlight reflection loops over struct fields that treat "all other fields" generically without schema validation.
- Look for panic-prone parsing logic in request handlers, especially those that handle unauthenticated or public-facing endpoints.
- Runtime and operational monitoring:
- Monitor for unusual spikes in HTTP 500 responses on
/oauth2/tokenor other SBI endpoints. - Use application performance monitoring (APM) or logging to track panic stack traces originating from parsing functions like
api_accesstoken.go. - Testing and fuzzing:
- Apply fuzzing to HTTP handlers that parse form-encoded or JSON bodies, focusing on random field names and types to surface type confusion and panics.
- Incorporate negative tests in CI that submit malformed or unexpected field combinations to critical control-plane endpoints.
- Automated tooling:
- Integrate linters or SAST tools oriented toward Go that can identify unsafe reflection patterns and panic-prone error handling in request paths.
- Enforce policy checks in pull requests to gate changes introducing new reflection-based parsers or unbounded error recovery.

