Skip to content

Fix UAF in OpenSSL.fixed_length_secure_compare#16392

Closed
apocalypse9949 wants to merge 1 commit intoruby:masterfrom
apocalypse9949:UAF
Closed

Fix UAF in OpenSSL.fixed_length_secure_compare#16392
apocalypse9949 wants to merge 1 commit intoruby:masterfrom
apocalypse9949:UAF

Conversation

@apocalypse9949
Copy link

This PR fixes a Use-After-Free (UAF) vuln in OpenSSL.fixed_length_secure_compare.

Description

The vuln existed because StringValuePtr(str) and RSTRING_PTR(str) macro calls were performed on the first argument before StringValue() was called on the second argument.

Since StringValue() implicitly calls #to_str on arguments that are not already Strings, an attacker could supply an object whose #to_str method modifies the other argument's content. If the first string argument is sufficiently mutated, its buffer will be reallocated. This leaves the initial p1 pointer pointing to freed memory, leading to a UAF when CRYPTO_memcmp is called.

Fix

This patch resolves the UAF by calling StringValue() on both string arguments first, ensuring they are fully evaluated and any #to_str side effects occur before taking the C pointers to their underlying string buffers.

A regression test case has also been included in test/openssl/test_ossl.rb to cover this exact scenario.

@github-actions
Copy link

The following files are maintained in the following upstream repositories:

Please file a pull request to the above instead. Thank you!

@rhenium
Copy link
Member

rhenium commented Mar 14, 2026

Moved to ruby/openssl: ruby/openssl#1016

@rhenium rhenium closed this Mar 14, 2026
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.

2 participants