Skip to content

Fix X509 memory leak in connTLSGetPeerCert#14855

Merged
sundb merged 1 commit intoredis:unstablefrom
YangboLong:fix_x509_leak
Mar 10, 2026
Merged

Fix X509 memory leak in connTLSGetPeerCert#14855
sundb merged 1 commit intoredis:unstablefrom
YangboLong:fix_x509_leak

Conversation

@YangboLong
Copy link
Copy Markdown
Contributor

@YangboLong YangboLong commented Mar 7, 2026

connTLSGetPeerCert() calls SSL_get_peer_certificate() which increments the X509 reference count, but never calls X509_free() to release it. This leaks an X509 object on each call to RM_GetClientCertificate(). This PR adds X509_free(cert) before returns.

The existing test module getclientcert api test in tests/unit/moduleapi/misc.tcl exercises this code path. But the leak was not caught because the ASAN CI job does not enable TLS.


Note

Low Risk
Low risk: adds missing X509_free() calls on both success and error paths, with no behavioral change beyond preventing a leak.

Overview
Prevents a per-call memory leak when exporting the client certificate (e.g., via RM_GetClientCertificate) by freeing the X509 *cert obtained from SSL_get_peer_certificate() in connTLSGetPeerCert() on both the failure path and after building the PEM string.

Written by Cursor Bugbot for commit d962210. This will update automatically on new commits. Configure here.

`connTLSGetPeerCert()` calls `SSL_get_peer_certificate()` which
increments the X509 reference count, but never calls `X509_free()` to
release it. This leaks an X509 object on each call to
`RM_GetClientCertificate()`. This PR adds `X509_free(cert)` before
returns.

The existing `test module getclientcert api` test in
`tests/unit/moduleapi/misc.tcl` exercises this code path. But the leak
was not caught because the ASAN CI job does not enable TLS.
@augmentcode
Copy link
Copy Markdown

augmentcode Bot commented Mar 7, 2026

🤖 Augment PR Summary

Summary: Fixes an X509 reference-count leak in connTLSGetPeerCert() when exporting a TLS peer certificate to PEM.

Changes: Adds X509_free(cert) on both success and error paths after SSL_get_peer_certificate().

🤖 Was this summary useful? React with 👍 or 👎

Copy link
Copy Markdown

@augmentcode augmentcode Bot left a comment

Choose a reason for hiding this comment

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

Review completed. No suggestions at this time.

Comment augment review to trigger a new review at any time.

Copy link
Copy Markdown
Collaborator

@sundb sundb left a comment

Choose a reason for hiding this comment

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

we can reproduce this leak using the following commands.

make BUILD_TLS=yes valgrind
./runtest --single unit/moduleapi/misc --valgrind --tls

@sundb sundb added this to Redis 8.8 Mar 9, 2026
@github-project-automation github-project-automation Bot moved this to Todo in Redis 8.8 Mar 9, 2026
@sundb sundb moved this from Todo to Awaits Merge in Redis 8.8 Mar 9, 2026
@sundb sundb merged commit 753c9f6 into redis:unstable Mar 10, 2026
18 checks passed
@github-project-automation github-project-automation Bot moved this from Awaits Merge to Done in Redis 8.8 Mar 10, 2026
@sundb
Copy link
Copy Markdown
Collaborator

sundb commented Mar 10, 2026

It's been there for a long time, so only backport to 8.6.

@YaacovHazan YaacovHazan moved this from Todo to In Progress in Redis 8.6 Backport Mar 19, 2026
YaacovHazan pushed a commit to YaacovHazan/redis that referenced this pull request Mar 19, 2026
`connTLSGetPeerCert()` calls `SSL_get_peer_certificate()` which
increments the X509 reference count, but never calls `X509_free()` to
release it. This leaks an X509 object on each call to
`RM_GetClientCertificate()`. This PR adds `X509_free(cert)` before
returns.

The existing `test module getclientcert api` test in
`tests/unit/moduleapi/misc.tcl` exercises this code path. But the leak
was not caught because the ASAN CI job does not enable TLS.
YaacovHazan pushed a commit that referenced this pull request Mar 23, 2026
`connTLSGetPeerCert()` calls `SSL_get_peer_certificate()` which
increments the X509 reference count, but never calls `X509_free()` to
release it. This leaks an X509 object on each call to
`RM_GetClientCertificate()`. This PR adds `X509_free(cert)` before
returns.

The existing `test module getclientcert api` test in
`tests/unit/moduleapi/misc.tcl` exercises this code path. But the leak
was not caught because the ASAN CI job does not enable TLS.
@YaacovHazan YaacovHazan mentioned this pull request Mar 24, 2026
@YaacovHazan YaacovHazan moved this from In Progress to Done in Redis 8.6 Backport Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

3 participants