BasePrintLib: Fix Buffer Overflow issue.

BaseMemoryLib: Fix error in CopyMem.S for BaseMemoryLibMmx & BaseMemoryLibRepStr instance.


git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@938 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
qhuang8 2006-07-13 01:53:27 +00:00
parent c6c0039c57
commit 4f7f1f5fa8
6 changed files with 56 additions and 50 deletions

View File

@ -394,7 +394,8 @@ AsciiSPrintUnicodeFormat (
Unicode string. Unicode string.
@param Flags The bitmask of flags that specify left justification, zero pad, and commas. @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
@param Value The 64-bit signed value to convert to a string. @param Value The 64-bit signed value to convert to a string.
@param Width The maximum number of Unicode characters to place in Buffer. @param Width The maximum number of Unicode characters to place in Buffer, not including
the Null-terminator.
@return The number of Unicode characters in Buffer not including the Null-terminator. @return The number of Unicode characters in Buffer not including the Null-terminator.
@ -438,7 +439,8 @@ UnicodeValueToString (
ASCII string. ASCII string.
@param Flags The bitmask of flags that specify left justification, zero pad, and commas. @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
@param Value The 64-bit signed value to convert to a string. @param Value The 64-bit signed value to convert to a string.
@param Width The maximum number of ASCII characters to place in Buffer. @param Width The maximum number of ASCII characters to place in Buffer, not including
the Null-terminator.
@return The number of ASCII characters in Buffer not including the Null-terminator. @return The number of ASCII characters in Buffer not including the Null-terminator.

View File

@ -85,6 +85,6 @@ L2:
movsb movsb
cld cld
movl 12(%esp), %eax movl 12(%esp), %eax
push %esi pop %esi
push %edi pop %edi
ret ret

View File

@ -53,6 +53,6 @@ L0:
movsb # Copy bytes backward movsb # Copy bytes backward
cld cld
movl 12(%esp),%eax # eax <- Destination as return value movl 12(%esp),%eax # eax <- Destination as return value
push %edi pop %edi
push %esi pop %esi
ret ret

View File

@ -80,6 +80,7 @@ BasePrintLibVSPrint (
) )
{ {
CHAR8 *OriginalBuffer; CHAR8 *OriginalBuffer;
CHAR8 *EndBuffer;
CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS]; CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
UINTN BytesPerOutputCharacter; UINTN BytesPerOutputCharacter;
UINTN BytesPerFormatCharacter; UINTN BytesPerFormatCharacter;
@ -110,13 +111,22 @@ BasePrintLibVSPrint (
} }
ASSERT (Buffer != NULL); ASSERT (Buffer != NULL);
OriginalBuffer = Buffer;
if ((Flags & OUTPUT_UNICODE) != 0) { if ((Flags & OUTPUT_UNICODE) != 0) {
BytesPerOutputCharacter = 2; BytesPerOutputCharacter = 2;
} else { } else {
BytesPerOutputCharacter = 1; BytesPerOutputCharacter = 1;
} }
//
// Reserve space for the Null terminator.
//
BufferSize--;
OriginalBuffer = Buffer;
//
// Set the tag for the end of the input Buffer.
//
EndBuffer = Buffer + BufferSize * BytesPerOutputCharacter;
if ((Flags & FORMAT_UNICODE) != 0) { if ((Flags & FORMAT_UNICODE) != 0) {
// //
// Make sure format string cannot contain more than PcdMaximumUnicodeStringLength // Make sure format string cannot contain more than PcdMaximumUnicodeStringLength
@ -135,10 +145,7 @@ BasePrintLibVSPrint (
FormatMask = 0xff; FormatMask = 0xff;
} }
//
// Reserve space for the Null terminator.
//
BufferSize--;
// //
// Get the first character from the format string // Get the first character from the format string
@ -148,7 +155,7 @@ BasePrintLibVSPrint (
// //
// Loop until the end of the format string is reached or the output buffer is full // Loop until the end of the format string is reached or the output buffer is full
// //
while (FormatCharacter != 0 && BufferSize > 0) { while (FormatCharacter != 0 && Buffer < EndBuffer) {
// //
// Clear all the flag bits except those that may have been passed in // Clear all the flag bits except those that may have been passed in
// //
@ -244,13 +251,6 @@ BasePrintLibVSPrint (
} }
} }
//
// Limit the maximum field width to the remaining characters in the output buffer
//
if (Width > BufferSize) {
Width = BufferSize;
}
// //
// Handle each argument type // Handle each argument type
// //
@ -477,12 +477,6 @@ BasePrintLibVSPrint (
} }
} }
//
// Limit the length of the string to append to the remaining characters in the output buffer
//
if (Count > BufferSize) {
Count = BufferSize;
}
if (Precision < Count) { if (Precision < Count) {
Precision = Count; Precision = Count;
} }
@ -491,18 +485,18 @@ BasePrintLibVSPrint (
// Pad before the string // Pad before the string
// //
if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) { if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH)) {
Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
} }
if (ZeroPad) { if (ZeroPad) {
if (Prefix != 0) { if (Prefix != 0) {
Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
} }
Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, '0', BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, '0', BytesPerOutputCharacter);
} else { } else {
Buffer = BasePrintLibFillBuffer (Buffer, Precision - Count, ' ', BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Precision - Count, ' ', BytesPerOutputCharacter);
if (Prefix != 0) { if (Prefix != 0) {
Buffer = BasePrintLibFillBuffer (Buffer, 1, Prefix, BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, Prefix, BytesPerOutputCharacter);
} }
} }
@ -520,7 +514,7 @@ BasePrintLibVSPrint (
while (Index < Count) { while (Index < Count) {
ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask; ArgumentCharacter = ((*ArgumentString & 0xff) | (*(ArgumentString + 1) << 8)) & ArgumentMask;
Buffer = BasePrintLibFillBuffer (Buffer, 1, ArgumentCharacter, BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ArgumentCharacter, BytesPerOutputCharacter);
ArgumentString += BytesPerArgumentCharacter; ArgumentString += BytesPerArgumentCharacter;
Index++; Index++;
if (Comma) { if (Comma) {
@ -529,7 +523,7 @@ BasePrintLibVSPrint (
Digits = 0; Digits = 0;
Index++; Index++;
if (Index < Count) { if (Index < Count) {
Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', BytesPerOutputCharacter);
} }
} }
} }
@ -539,14 +533,9 @@ BasePrintLibVSPrint (
// Pad after the string // Pad after the string
// //
if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) { if ((Flags & (PAD_TO_WIDTH | LEFT_JUSTIFY)) == (PAD_TO_WIDTH | LEFT_JUSTIFY)) {
Buffer = BasePrintLibFillBuffer (Buffer, Width - Precision, ' ', BytesPerOutputCharacter); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Precision, ' ', BytesPerOutputCharacter);
} }
//
// Reduce the number of characters
//
BufferSize -= Count;
// //
// Get the next character from the format string // Get the next character from the format string
// //
@ -561,7 +550,7 @@ BasePrintLibVSPrint (
// //
// Null terminate the Unicode or ASCII string // Null terminate the Unicode or ASCII string
// //
BasePrintLibFillBuffer (Buffer, 1, 0, BytesPerOutputCharacter); BasePrintLibFillBuffer (Buffer, EndBuffer, 1, 0, BytesPerOutputCharacter);
// //
// Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength // Make sure output buffer cannot contain more than PcdMaximumUnicodeStringLength
// Unicode characters if PcdMaximumUnicodeStringLength is not zero. // Unicode characters if PcdMaximumUnicodeStringLength is not zero.
@ -999,7 +988,8 @@ AsciiSPrintUnicodeFormat (
Unicode string. Unicode string.
@param Flags The bitmask of flags that specify left justification, zero pad, and commas. @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
@param Value The 64-bit signed value to convert to a string. @param Value The 64-bit signed value to convert to a string.
@param Width The maximum number of Unicode characters to place in Buffer. @param Width The maximum number of Unicode characters to place in Buffer, not including
the Null-terminator.
@return The number of Unicode characters in Buffer not including the Null-terminator. @return The number of Unicode characters in Buffer not including the Null-terminator.
@ -1046,7 +1036,8 @@ UnicodeValueToString (
ASCII string. ASCII string.
@param Flags The bitmask of flags that specify left justification, zero pad, and commas. @param Flags The bitmask of flags that specify left justification, zero pad, and commas.
@param Value The 64-bit signed value to convert to a string. @param Value The 64-bit signed value to convert to a string.
@param Width The maximum number of ASCII characters to place in Buffer. @param Width The maximum number of ASCII characters to place in Buffer, not including
the Null-terminator.
@return The number of ASCII characters in Buffer not including the Null-terminator. @return The number of ASCII characters in Buffer not including the Null-terminator.

View File

@ -25,6 +25,8 @@ static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B',
Internal function that places ASCII or Unicode character into the Buffer. Internal function that places ASCII or Unicode character into the Buffer.
@param Buffer Buffer to place the Unicode or ASCII string. @param Buffer Buffer to place the Unicode or ASCII string.
@param EndBuffer The end of the input Buffer. No characters will be
placed after that.
@param Length Count of character to be placed into Buffer. @param Length Count of character to be placed into Buffer.
@param Character Character to be placed into Buffer. @param Character Character to be placed into Buffer.
@param Increment Character increment in Buffer. @param Increment Character increment in Buffer.
@ -35,6 +37,7 @@ static CONST CHAR8 mHexStr[] = {'0','1','2','3','4','5','6','7','8','9','A','B',
CHAR8 * CHAR8 *
BasePrintLibFillBuffer ( BasePrintLibFillBuffer (
CHAR8 *Buffer, CHAR8 *Buffer,
CHAR8 *EndBuffer,
INTN Length, INTN Length,
UINTN Character, UINTN Character,
INTN Increment INTN Increment
@ -42,7 +45,7 @@ BasePrintLibFillBuffer (
{ {
INTN Index; INTN Index;
for (Index = 0; Index < Length; Index++) { for (Index = 0; Index < Length && Buffer < EndBuffer; Index++) {
*Buffer = (CHAR8) Character; *Buffer = (CHAR8) Character;
*(Buffer + 1) = (CHAR8) (Character >> 8); *(Buffer + 1) = (CHAR8) (Character >> 8);
Buffer += Increment; Buffer += Increment;
@ -117,7 +120,8 @@ BasePrintLibValueToString (
@param Flags The bitmask of flags that specify left justification, zero pad, @param Flags The bitmask of flags that specify left justification, zero pad,
and commas. and commas.
@param Value The 64-bit signed value to convert to a string. @param Value The 64-bit signed value to convert to a string.
@param Width The maximum number of characters to place in Buffer. @param Width The maximum number of characters to place in Buffer, not including
the Null-terminator.
@param Increment Character increment in Buffer. @param Increment Character increment in Buffer.
@return The number of characters in Buffer not including the Null-terminator. @return The number of characters in Buffer not including the Null-terminator.
@ -133,6 +137,7 @@ BasePrintLibConvertValueToString (
) )
{ {
CHAR8 *OriginalBuffer; CHAR8 *OriginalBuffer;
CHAR8 *EndBuffer;
CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS]; CHAR8 ValueBuffer[MAXIMUM_VALUE_CHARACTERS];
UINTN Count; UINTN Count;
UINTN Digits; UINTN Digits;
@ -154,17 +159,21 @@ BasePrintLibConvertValueToString (
if (Width == 0) { if (Width == 0) {
Width = MAXIMUM_VALUE_CHARACTERS - 1; Width = MAXIMUM_VALUE_CHARACTERS - 1;
} }
//
// Set the tag for the end of the input Buffer.
//
EndBuffer = Buffer + Width * Increment;
if (Value < 0) { if (Value < 0) {
Value = -Value; Value = -Value;
Buffer = BasePrintLibFillBuffer (Buffer, 1, '-', Increment); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, '-', Increment);
Width--; Width--;
} }
Count = BasePrintLibValueToString (ValueBuffer, Value, 10); Count = BasePrintLibValueToString (ValueBuffer, Value, 10);
if ((Flags & PREFIX_ZERO) != 0) { if ((Flags & PREFIX_ZERO) != 0) {
Buffer = BasePrintLibFillBuffer (Buffer, Width - Count, '0', Increment); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, Width - Count, '0', Increment);
} }
Digits = Count % 3; Digits = Count % 3;
@ -172,19 +181,19 @@ BasePrintLibConvertValueToString (
Digits = 3 - Digits; Digits = 3 - Digits;
} }
for (Index = 0; Index < Count; Index++) { for (Index = 0; Index < Count; Index++) {
Buffer = BasePrintLibFillBuffer (Buffer, 1, ValueBuffer[Count - Index], Increment); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ValueBuffer[Count - Index], Increment);
if ((Flags & COMMA_TYPE) != 0) { if ((Flags & COMMA_TYPE) != 0) {
Digits++; Digits++;
if (Digits == 3) { if (Digits == 3) {
Digits = 0; Digits = 0;
if ((Index + 1) < Count) { if ((Index + 1) < Count) {
Buffer = BasePrintLibFillBuffer (Buffer, 1, ',', Increment); Buffer = BasePrintLibFillBuffer (Buffer, EndBuffer, 1, ',', Increment);
} }
} }
} }
} }
BasePrintLibFillBuffer (Buffer, 1, 0, Increment); BasePrintLibFillBuffer (Buffer, EndBuffer, 1, 0, Increment);
return ((Buffer - OriginalBuffer) / Increment); return ((Buffer - OriginalBuffer) / Increment);
} }

View File

@ -85,6 +85,8 @@ BasePrintLibSPrint (
Internal function that places ASCII or Unicode character into the Buffer. Internal function that places ASCII or Unicode character into the Buffer.
@param Buffer Buffer to place the Unicode or ASCII string. @param Buffer Buffer to place the Unicode or ASCII string.
@param EndBuffer The end of the input Buffer. No characters will be
placed after that.
@param Length Count of character to be placed into Buffer. @param Length Count of character to be placed into Buffer.
@param Character Character to be placed into Buffer. @param Character Character to be placed into Buffer.
@param Increment Character increment in Buffer. @param Increment Character increment in Buffer.
@ -95,6 +97,7 @@ BasePrintLibSPrint (
CHAR8 * CHAR8 *
BasePrintLibFillBuffer ( BasePrintLibFillBuffer (
CHAR8 *Buffer, CHAR8 *Buffer,
CHAR8 *EndBuffer,
INTN Length, INTN Length,
UINTN Character, UINTN Character,
INTN Increment INTN Increment
@ -151,7 +154,8 @@ BasePrintLibValueToString (
@param Flags The bitmask of flags that specify left justification, zero pad, @param Flags The bitmask of flags that specify left justification, zero pad,
and commas. and commas.
@param Value The 64-bit signed value to convert to a string. @param Value The 64-bit signed value to convert to a string.
@param Width The maximum number of characters to place in Buffer. @param Width The maximum number of characters to place in Buffer, not including
the Null-terminator.
@param Increment Character increment in Buffer. @param Increment Character increment in Buffer.
@return Total number of characters required to perform the conversion. @return Total number of characters required to perform the conversion.