Skip to content

Netdata fixes part 6 - #22906

Merged
stelfrag merged 2 commits into
netdata:masterfrom
stelfrag:netdata-fixes-6
Jun 30, 2026
Merged

Netdata fixes part 6#22906
stelfrag merged 2 commits into
netdata:masterfrom
stelfrag:netdata-fixes-6

Conversation

@stelfrag

@stelfrag stelfrag commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator
Summary

Summary by cubic

Fixes SSL/TLS resource leaks and cleanup paths: frees the peer certificate on OpenSSL 3.x and clears SSL/SNI state when TLS setup fails. Behavior is unchanged; reduces memory leaks on failed and verified connections.

  • Bug Fixes
    • security.c: Free the owned peer certificate with X509_free() after verification to prevent a per-connection X509 leak.
    • nd-sock.c: On TLS setup failure, close the SSL sub-structure and free the SNI hostname before closing the socket.

Written for commit 5418c02. Summary will update on new commits.

Review in cubic

ktsaou added 2 commits June 29, 2026 15:41
security_test_certificate() obtains the peer certificate with SSL_get_peer_certificate(). On OpenSSL 3.x this is an alias of SSL_get1_peer_certificate(), which returns an owned reference that must be released with X509_free(). The current SSL verification path checks the certificate and returns without releasing that reference, leaking one X509 object for each verified SSL connection that presents a peer certificate.

This is a current issue because nd_sock_open_ssl() calls security_test_certificate() when certificate verification is enabled. Free the owned certificate reference after SSL_get_verify_result() has been captured and before returning the existing result. The NULL-certificate path is unchanged and still returns immediately.

Performance impact is negligible: one X509_free() refcount decrement per checked SSL connection. Functional impact is limited to releasing the local OpenSSL reference; return values, logging, verification state, and connection behavior are unchanged. The required review gate passed with all reviewers voting production grade and absolutely necessary.

(cherry picked from commit 64ad6a8)
MEM-socket-003: nd_sock_connect_to_this() could retain an allocated SSL object after a TCP connect succeeded but TLS setup failed, leaving the object bound to a file descriptor that was then closed.

Classification: current bounded error-path lifetime bug.

Functional impact: failure return values, socket errors, retry behavior, certificate policy, and successful connections are unchanged.

Performance impact: successful paths are unchanged; failed TLS setup now performs immediate SSL/SNI cleanup instead of deferring it to later socket cleanup.

Clean the SSL sub-structure before closing the fd and clear the SNI hostname allocated for the failed TLS attempt.

(cherry picked from commit 8f4d5cc)

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.
Architecture diagram
sequenceDiagram
    participant App as Application
    participant ND_Sock as nd_sock
    participant OpenSSL as OpenSSL Library
    participant Security as security.c

    Note over App,Security: TLS Connection Lifecycle

    App->>ND_Sock: nd_sock_connect_to_this()
    ND_Sock->>ND_Sock: Create socket & set SNI hostname

    alt TLS enabled (ssl && s->ctx)
        ND_Sock->>ND_Sock: nd_sock_open_ssl()
        alt TLS setup succeeds
            ND_Sock->>Security: security_test_certificate(ssl)
            Security->>OpenSSL: SSL_get_peer_certificate()
            OpenSSL-->>Security: X509 cert (owned reference)
            Security->>OpenSSL: X509_verify_cert() etc.
            Security->>OpenSSL: X509_free(cert)
            Security-->>ND_Sock: ret (0 or 1)
            ND_Sock-->>App: success
        else TLS setup fails
            ND_Sock->>ND_Sock: netdata_ssl_close(&s->ssl)
            ND_Sock->>ND_Sock: close(s->fd)
            ND_Sock->>ND_Sock: freez(s->sni_hostname)
            ND_Sock->>ND_Sock: s->sni_hostname = NULL
            ND_Sock-->>App: false (connection failed)
        end
    else No TLS
        ND_Sock-->>App: plain socket connection
    end

    Note over Security: Verification completion (all paths)
    Security->>OpenSSL: X509_free(cert) — ensures cert freed<br/>even on failed verification
Loading

Re-trigger cubic

@stelfrag
stelfrag marked this pull request as ready for review June 29, 2026 18:26

@thiagoftsm thiagoftsm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found between runtime. LGTM!

@stelfrag
stelfrag merged commit c68c056 into netdata:master Jun 30, 2026
157 checks passed
@stelfrag
stelfrag deleted the netdata-fixes-6 branch June 30, 2026 05:15
@sonarqubecloud

Copy link
Copy Markdown

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.

3 participants