]> git.proxmox.com Git - mirror_edk2.git/commitdiff
MdeModulePkg/PrintLib: Avoid reading content beyond the format string
authorHao Wu <hao.a.wu@intel.com>
Mon, 22 May 2017 07:03:24 +0000 (15:03 +0800)
committerHao Wu <hao.a.wu@intel.com>
Thu, 1 Jun 2017 00:46:40 +0000 (08:46 +0800)
https://bugzilla.tianocore.org/show_bug.cgi?id=567

In functions DxePrintLibPrint2ProtocolVaListToBaseList() and
InternalPrintLibSPrintMarker(), when processing ASCII format strings, if
the format string walker pointer 'Format' is pointing at the end of the
format string (i.e. '\0'), the following expression:
*(Format + 1)
will read an undefined value.

Though this value won't affect the functionality, since it will be masked
by variable 'FormatMask':
(*(Format + 1) << 8)) & FormatMask
(FormatMask is 0xff for ASCII format string)

This commit adds additional logic to avoid reading undefined content.

Contributed-under: TianoCore Contribution Agreement 1.0
Signed-off-by: Hao Wu <hao.a.wu@intel.com>
Reviewed-by: Liming Gao <liming.gao@intel.com>
MdeModulePkg/Library/DxePrintLibPrint2Protocol/PrintLib.c

index 9f702c4fefcea5ba83f7cfd34460c4a68c18a590..b58db8e011a8517c741e947b2990c29522242488 100644 (file)
@@ -130,7 +130,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
   //\r
   // Get the first character from the format string\r
   //\r
-  FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+  FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
 \r
   while (FormatCharacter != 0) {\r
     if (FormatCharacter == '%') {\r
@@ -148,7 +148,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
         //\r
         // Get the next character from the format string\r
         //\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
 \r
         switch (FormatCharacter) {\r
         case '.': \r
@@ -239,7 +239,7 @@ DxePrintLibPrint2ProtocolVaListToBaseList (
     //\r
     // Get the next character from the format string\r
     //\r
-    FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+    FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
   }\r
   return TRUE;\r
 }\r
@@ -1596,7 +1596,7 @@ InternalPrintLibSPrintMarker (
   //\r
   // Get the first character from the format string\r
   //\r
-  FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+  FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
 \r
   //\r
   // Loop until the end of the format string is reached or the output buffer is full\r
@@ -1628,7 +1628,7 @@ InternalPrintLibSPrintMarker (
       //\r
       for (Done = FALSE; !Done; ) {\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
         switch (FormatCharacter) {\r
         case '.': \r
           Flags |= PRECISION; \r
@@ -1681,7 +1681,7 @@ InternalPrintLibSPrintMarker (
           for (Count = 0; ((FormatCharacter >= '0') &&  (FormatCharacter <= '9')); ){\r
             Count = (Count * 10) + FormatCharacter - '0';\r
             Format += BytesPerFormatCharacter;\r
-            FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+            FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
           }\r
           Format -= BytesPerFormatCharacter;\r
           if ((Flags & PRECISION) == 0) {\r
@@ -1960,7 +1960,7 @@ InternalPrintLibSPrintMarker (
 \r
       case '\r':\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
         if (FormatCharacter == '\n') {\r
           //\r
           // Translate '\r\n' to '\r\n'\r
@@ -1981,7 +1981,7 @@ InternalPrintLibSPrintMarker (
         //\r
         ArgumentString = "\r\n";\r
         Format += BytesPerFormatCharacter;\r
-        FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+        FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
         if (FormatCharacter != '\r') {\r
           Format   -= BytesPerFormatCharacter;\r
         }\r
@@ -2000,7 +2000,7 @@ InternalPrintLibSPrintMarker (
  \r
     case '\r':\r
       Format += BytesPerFormatCharacter;\r
-      FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+      FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
       if (FormatCharacter == '\n') {\r
         //\r
         // Translate '\r\n' to '\r\n'\r
@@ -2021,7 +2021,7 @@ InternalPrintLibSPrintMarker (
       //\r
       ArgumentString = "\r\n";\r
       Format += BytesPerFormatCharacter;\r
-      FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+      FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
       if (FormatCharacter != '\r') {\r
         Format   -= BytesPerFormatCharacter;\r
       }\r
@@ -2149,7 +2149,7 @@ InternalPrintLibSPrintMarker (
     //\r
     // Get the next character from the format string\r
     //\r
-    FormatCharacter = ((*Format & 0xff) | (*(Format + 1) << 8)) & FormatMask;\r
+    FormatCharacter = ((*Format & 0xff) | ((BytesPerFormatCharacter == 1) ? 0 : (*(Format + 1) << 8))) & FormatMask;\r
   }\r
 \r
   if ((Flags & COUNT_ONLY_NO_PRINT) != 0) {\r