summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDoug Flick <dougflick@microsoft.com>2024-10-03 10:16:57 -0700
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>2025-04-09 00:13:21 +0000
commitb90693965b6b1566bcac4652ad1bb436e1bb461f (patch)
tree4b0134b5af95c1ae7acd6794bd83d875b4e08a7a
parent5f08635ee7c176f78f788aa6528b43f18536a80b (diff)
downloadedk2-b90693965b6b1566bcac4652ad1bb436e1bb461f.zip
edk2-b90693965b6b1566bcac4652ad1bb436e1bb461f.tar.gz
edk2-b90693965b6b1566bcac4652ad1bb436e1bb461f.tar.bz2
SecurityPkg: Improving HashPeImageByType () logic
Namely: (1) The TWO_BYTE_ENCODE check is independent of Index. If it evalutes to TRUE for Index==0, then it will evaluate to TRUE for all other Index values as well. As a result, the (Index == HASHALG_MAX) condition will fire after the loop, and we'll return EFI_UNSUPPORTED. While this is correct, functionally speaking, it is wasteful to keep re-checking TWO_BYTE_ENCODE in the loop body. The check should be made at the top of the function, and EFI_UNSUPPORTED should be returned at once, if appropriate. (2) If the hash algorithm selected by Index has such a large OID that the OID comparison cannot even be performed (because AuthDataSize is not large enough for containing the OID in question, starting at offset 32), then the function returns EFI_UNSUPPORTED at once. This is bogus; this case should simply be treated as an OID mismatch, and the loop should advance to the next Index value / hash algorithm candidate. A remaining hash algo may have a shorter OID and yield an OID match. Signed-off-by: Doug Flick <DougFlick@microsoft.com>
-rw-r--r--SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c37
1 files changed, 19 insertions, 18 deletions
diff --git a/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c b/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
index 2afa2c9..2eca39d 100644
--- a/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
+++ b/SecurityPkg/Library/DxeImageVerificationLib/DxeImageVerificationLib.c
@@ -618,6 +618,7 @@ Done:
@param[in] AuthDataSize Size of the Authenticode Signature in bytes.
@retval EFI_UNSUPPORTED Hash algorithm is not supported.
+ @retval EFI_BAD_BUFFER_SIZE AuthData provided is invalid size.
@retval EFI_SUCCESS Hash successfully.
**/
@@ -629,28 +630,28 @@ HashPeImageByType (
{
UINT8 Index;
- for (Index = 0; Index < HASHALG_MAX; Index++) {
+ //
+ // Check the Hash algorithm in PE/COFF Authenticode.
+ // According to PKCS#7 Definition:
+ // SignedData ::= SEQUENCE {
+ // version Version,
+ // digestAlgorithms DigestAlgorithmIdentifiers,
+ // contentInfo ContentInfo,
+ // .... }
+ // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
+ // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
+ // Fixed offset (+32) is calculated based on two bytes of length encoding.
+ //
+ if ((AuthDataSize > 1) && ((*(AuthData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE)) {
//
- // Check the Hash algorithm in PE/COFF Authenticode.
- // According to PKCS#7 Definition:
- // SignedData ::= SEQUENCE {
- // version Version,
- // digestAlgorithms DigestAlgorithmIdentifiers,
- // contentInfo ContentInfo,
- // .... }
- // The DigestAlgorithmIdentifiers can be used to determine the hash algorithm in PE/COFF hashing
- // This field has the fixed offset (+32) in final Authenticode ASN.1 data.
- // Fixed offset (+32) is calculated based on two bytes of length encoding.
+ // Only support two bytes of Long Form of Length Encoding.
//
- if ((AuthDataSize > 1) && ((*(AuthData + 1) & TWO_BYTE_ENCODE) != TWO_BYTE_ENCODE)) {
- //
- // Only support two bytes of Long Form of Length Encoding.
- //
- continue;
- }
+ return EFI_BAD_BUFFER_SIZE;
+ }
+ for (Index = 0; Index < HASHALG_MAX; Index++) {
if (AuthDataSize < 32 + mHash[Index].OidLength) {
- return EFI_UNSUPPORTED;
+ continue;
}
if (CompareMem (AuthData + 32, mHash[Index].OidValue, mHash[Index].OidLength) == 0) {