Skip to content

gnmi_server: fix nil-resp panic when CRL fetch fails#658

Open
SAY-5 wants to merge 1 commit into
sonic-net:masterfrom
SAY-5:fix/crlcheck-nil-resp-deref-630
Open

gnmi_server: fix nil-resp panic when CRL fetch fails#658
SAY-5 wants to merge 1 commit into
sonic-net:masterfrom
SAY-5:fix/crlcheck-nil-resp-deref-630

Conversation

@SAY-5
Copy link
Copy Markdown

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

Closes #630.

Problem

TryDownload in gnmi_server/clientCertAuth.go unconditionally dereferences resp.StatusCode after http.Get(url), but http.Get returns (nil, err) on network-level failures (DNS resolution, connection refused, TLS handshake failure, timeout before response, etc.). Any of these paths panics the gNMI server with nil pointer dereference.

resp, err := http.Get(url)

if resp != nil {
    defer resp.Body.Close()
}

if err != nil || resp.StatusCode != http.StatusOK {  // resp may be nil here

Fix

Short-circuit on err != nil before touching resp, matching the standard Go pattern. Log the HTTP status on non-200 responses instead of a stale err. Behavior is otherwise unchanged on the happy path.

Test

gofmt -d clean.

http.Get() returns (nil, err) on network-level failures (DNS,
connection refused, TLS handshake, timeout before any response).
TryDownload() saved the defer inside `if resp != nil` but then
unconditionally dereferenced `resp.StatusCode` in the next line, so
any non-transport failure panicked the gNMI server with a nil pointer
dereference.

Return early on err != nil, then defer close and check StatusCode.
Behavior otherwise unchanged.

Closes sonic-net#630.

Signed-off-by: SAY-5 <say.apm35@gmail.com>
@linux-foundation-easycla
Copy link
Copy Markdown

CLA Not Signed

@mssonicbld
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

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.

[clientCertAuth] Nil pointer dereference on HTTP response when http.Get() fails

3 participants