Skip to content

fix use-after-free in OpenSSL Hash copy constructor - #703

Open
Bountybette wants to merge 1 commit into
USBGuard:mainfrom
Bountybette:hash-openssl-copy-ctor-uaf
Open

Bountybette wants to merge 1 commit into
USBGuard:mainfrom
Bountybette:hash-openssl-copy-ctor-uaf

Conversation

@Bountybette

Copy link
Copy Markdown

With the OpenSSL backend, usbguard::Hash holds the digest context as an
owning EVP_MD_CTX*, but the copy constructor copied it by value:

_state = rhs._state;

so a copy and its source share one heap context. The hashing path copies a
Hash (Hash hash(_hash); in DevicePrivate::updateHash); when that copy is
destroyed while the source is still live, EVP_MD_CTX_free releases the
context the source still points at.

ASan on the copy-then-use path:

==ERROR: AddressSanitizer: heap-use-after-free ... in EVP_DigestFinal_ex
  #2 usbguard::Hash::getBase64() Hash.cpp:210
freed by thread T0 here:
  #1 EVP_MD_CTX_free
  #2 usbguard::Hash::~Hash() Hash.cpp:119

The libgcrypt backend already deep-copies with gcry_md_copy and libsodium
keeps the state by value; only OpenSSL aliased. The copy constructor now
allocates a fresh context and copies into it with EVP_MD_CTX_copy_ex,
matching the default constructor. Added test_Hash.cpp asserting a copy is
independent of its source (fails under the asan job before this change).

Signed-off-by: waheeda bette <waheeda@research.digiscrypt.com>

This branch has not been deployed

No deployments
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.

1 participant