Fix corruption when searching for CRLs in hashed directories
authorHugo Landau <hlandau@openssl.org>
Wed, 18 Jan 2023 15:43:56 +0000 (15:43 +0000)
committerPauli <pauli@openssl.org>
Tue, 24 Jan 2023 00:23:17 +0000 (11:23 +1100)
commit3147785eb23bb27080a0b7accbbff46ac471e86c
treec0a69701e2572b293664b07928d5ba9eae230bc6
parent2fda45d5eb85e5d939fbbba0dd6562ebf01abd2a
Fix corruption when searching for CRLs in hashed directories

The by_dir certificate/CRL lookup code uses an OPENSSL_STACK to track
how many sequentially numbered CRL files have been loaded for a given
X509_NAME hash which is being requested. This avoids loading already
loaded CRL files and repeated stat() calls.

This OPENSSL_STACK is searched using sk_find, however this mutates
the OPENSSL_STACK unless it is known to be sorted. This operation
therefore requires a write lock, which was not taken.

Fix this issue by sorting the OPENSSL_STACK whenever it is mutated. This
guarantees no mutation will occur during sk_find. This is chosen over
taking a write lock during sk_find as retrieving a CRL by X509_NAME is
assumed to be a hotter path than the case where a new CRL is installed.

Also optimise the code by avoiding creating the structure to track the
last CRL file sequence number in the circumstance where it would match
the initial value, namely where no CRL with the given hash is installed.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
Reviewed-by: Paul Dale <pauli@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/20076)
crypto/x509/by_dir.c