Skip to content

Commit f65edd8

Browse files
committed
[CHERRY-PICK] MdePkg: DebugLib: Check Signature in CR in Release Builds
The CR macro is used to access an enclosing structure from a pointer within the structure. In DEBUG builds (i.e. when MDEPKG_NDEBUG is not set and debug asserts are enabled), this macro does signature validation checking to ensure that the structure that has been found is the correct structure, based on a signature passed in by the caller. However, if MDEPKG_NDEBUG is set or debug asserts are disabled, no signature validation is performed, meaning that CR may return an invalid structure that the caller believes is valid and has had signature validation on, causing undefined behavior (memory corruption). We should where at all possible have defined behavior, particularly in RELEASE builds, which are what typical platforms will ship to consumers. This patch updates CR to do the signature validation in all scenarios to provide defined behavior from the macro. In the event of a signature failure, CR will either 1) assert if !MDEPKG_NDEBUG and debug asserts are enabled (existing behavior) or 2) return NULL to indicate to the caller that signature validation failed. There exist consumers today who already, erroneously, rely on this behavior. Another macro, BASE_CR, exists for callers who do not wish to perform signature validation. Any code that wishes to avoid the signature validation should move to this macro. Signed-off-by: Oliver Smith-Denny <[email protected]>
1 parent ac1a0eb commit f65edd8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

MdePkg/Include/Library/DebugLib.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,12 @@ UnitTestDebugAssert (
587587
If MDEPKG_NDEBUG is defined or the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
588588
of PcdDebugProperyMask is clear, then this macro computes the offset, in bytes,
589589
of the field specified by Field from the beginning of the data structure specified
590-
by TYPE. This offset is subtracted from Record, and is used to return a pointer
591-
to a data structure of the type specified by TYPE.
590+
by TYPE. This offset is subtracted from Record, and is used to compute a pointer
591+
to a data structure of the type specified by TYPE. The Signature field of the
592+
data structure specified by TYPE is compared to TestSignature. If the signatures
593+
match, then a pointer to the pointer to a data structure of the type specified by
594+
TYPE is returned. If the signatures do not match, then NULL is returned to
595+
signify that the passed in data structure is invalid.
592596
593597
If MDEPKG_NDEBUG is not defined and the DEBUG_PROPERTY_DEBUG_ASSERT_ENABLED bit
594598
of PcdDebugProperyMask is set, then this macro computes the offset, in bytes,
@@ -622,9 +626,13 @@ UnitTestDebugAssert (
622626
#define CR(Record, TYPE, Field, TestSignature) \
623627
(DebugAssertEnabled () && (BASE_CR (Record, TYPE, Field)->Signature != TestSignature)) ? \
624628
(TYPE *) (_ASSERT (CR has Bad Signature), Record) : \
629+
(BASE_CR (Record, TYPE, Field)->Signature != TestSignature) ? \
630+
NULL : \
625631
BASE_CR (Record, TYPE, Field)
626632
#else
627633
#define CR(Record, TYPE, Field, TestSignature) \
634+
(BASE_CR (Record, TYPE, Field)->Signature != TestSignature) ? \
635+
NULL : \
628636
BASE_CR (Record, TYPE, Field)
629637
#endif
630638

0 commit comments

Comments
 (0)