]> xenbits.xensource.com Git - ovmf.git/commitdiff
BaseTools/VfrCompile: Fix potential buffer overwrites
authorMichael Kubacki <michael.kubacki@microsoft.com>
Wed, 9 Nov 2022 16:40:10 +0000 (11:40 -0500)
committermergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Mon, 3 Apr 2023 15:29:08 +0000 (15:29 +0000)
While more portable methods exist to handle these cases, this change
does not attempt to do more than fix the immediate problem and
follow the conventions already established in this code.

`snprintf()` is introduced as the minimum improvement apart from
making the buffers larger.

Fixes the following CodeQL alerts:

1. Failure on line 2339 in
   BaseTools/Source/C/VfrCompile/Pccts/antlr/gen.c

   - Type: Potentially overrunning write
   - Severity: Critical
   - Problem: This 'call to sprintf' operation requires 17 bytes but
     the destination is only 16 bytes.

2. Failure on line 2341 in
   BaseTools/Source/C/VfrCompile/Pccts/antlr/gen.c

   - Type: Potentially overrunning write
   - Severity: Critical
   - Problem: This 'call to sprintf' operation requires 17 bytes but
     the destination is only 16 bytes.

3. Failure on line 1309 in
   BaseTools/Source/C/VfrCompile/Pccts/antlr/main.c

   - Type: Potentially overrunning write
   - Severity: Critical
   - Problem: This 'call to sprintf' operation requires 25 bytes but
     the destination is only 20 bytes.

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Cc: Sean Brogan <sean.brogan@microsoft.com>
Cc: Yuwei Chen <yuwei.chen@intel.com>
Signed-off-by: Michael Kubacki <michael.kubacki@microsoft.com>
Reviewed-by: Liming Gao <gaoliming@byosoft.com.cn>
Reviewed-by: Michael D Kinney <michael.d.kinney@intel.com>
Reviewed-by: Oliver Smith-Denny <osd@smith-denny.com>
BaseTools/Source/C/VfrCompile/Pccts/antlr/gen.c
BaseTools/Source/C/VfrCompile/Pccts/antlr/main.c

index 8e41239f4751504a6aab36e2b2b9657347827cd8..33d9cac4c7deb82e11ae90924daf579d277176e2 100644 (file)
@@ -2331,14 +2331,14 @@ TokNode *p;
                        set_nameErrSet = bufErrSet;                                                             /* MR23 */\r
                }\r
                else {                                  /* wild card */\r
-                       static char buf[sizeof("zzerr")+10];\r
-                       static char bufErrSet[sizeof("zzerr")+10];\r
+                       static char buf[sizeof("zzerr")+11];\r
+                       static char bufErrSet[sizeof("zzerr")+11];\r
                        int n = DefErrSet( &b, 0, NULL );\r
                        int nErrSet = DefErrSetWithSuffix(0, &bErrSet, 1, NULL, "_set");\r
-                       if ( GenCC ) sprintf(buf, "err%d", n);\r
-                       else sprintf(buf, "zzerr%d", n);\r
+                       if ( GenCC ) snprintf(buf, 11, "err%d", n);\r
+                       else snprintf(buf, 11, "zzerr%d", n);\r
                        if ( GenCC ) sprintf(bufErrSet, "err%d", nErrSet);\r
-                       else sprintf(bufErrSet, "zzerr%d", nErrSet);\r
+                       else snprintf(bufErrSet, 11, "zzerr%d", nErrSet);\r
                        set_name = buf;\r
                        set_nameErrSet = bufErrSet;\r
                }\r
index 051ee4ec5d28b7b499f8f03aac2fee5b84fb528f..488b4b90461c6abb2472e1adeb47985d0129782a 100644 (file)
@@ -1295,7 +1295,7 @@ int token;
 #endif\r
 {\r
        int     j;\r
-        static    char    imag_name[20];\r
+        static    char    imag_name[25];\r
 \r
        /* look in all lexclasses for the token */\r
        if ( TokenString(token) != NULL ) return TokenString(token);\r
@@ -1306,7 +1306,7 @@ int token;
        }\r
 \r
     if (1) {\r
-      sprintf(imag_name,"UnknownToken#%d",token);           /* MR13 */\r
+      snprintf(imag_name, 25, "UnknownToken#%d", token);    /* MR13 */\r
       return imag_name;                                     /* MR13 */\r
     }\r
 \r