Skip to content

Commit 9da6f80

Browse files
committed
crypto: fix use-after-free risk in ManagedX509 assignment
Fixes a potential double-free issue where ManagedX509::operator= resets the underlying smart pointer using a raw pointer from another instance before incrementing the reference count. If both instances were managing the same underlying OpenSSL object, the reset could decrement the reference count to 0 and free the object before the reference count could be incremented. This fixes Coverity issue 367349 where different smart pointers were seemingly managing the same raw pointer. Fixes: #56926
1 parent ed05549 commit 9da6f80

1 file changed

Lines changed: 6 additions & 3 deletions

File tree

src/crypto/crypto_x509.cc

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,12 @@ ManagedX509::ManagedX509(const ManagedX509& that) {
5959
}
6060

6161
ManagedX509& ManagedX509::operator=(const ManagedX509& that) {
62-
cert_.reset(that.get());
63-
if (cert_) [[likely]]
64-
X509_up_ref(cert_.get());
62+
if (this == &that) return *this;
63+
64+
X509* cert = that.get();
65+
if (cert) [[likely]]
66+
X509_up_ref(cert);
67+
cert_.reset(cert);
6568
return *this;
6669
}
6770

0 commit comments

Comments
 (0)