CVE-2020-19497 (published 2021-07-21T18:15Z)

Integer overflow vulnerability in Mat_VarReadNextInfo5 in mat5.c in tbeu matio (aka MAT File I/O Library) 1.5.17, allows attackers to cause a Denial of Service or possibly other unspecified impacts.

Severity: MEDIUM
CVSS v2 base score: 6.8 (AV:N/AC:M/Au:N/C:P/I:P/A:P)
CVSS v3 base score: 8.8, HIGH (CVSS:3.1/AV:N/AC:L/PR:N/UI:R/S:U/C:H/I:H/A:H)

Weakness information:
cwe_id: CWE-190
cwe_name: Integer Overflow or Wraparound
description: The product performs a calculation that can
         produce an integer overflow or wraparound when the logic
         assumes that the resulting value will always be larger than
         the original value. This occurs when an integer value is
         incremented to a value that is too large to store in the
         associated representation. When this occurs, the value may
         become a very small or negative number.
extended_description: 
            
            
            
            
         
         

Fix information:
repo_url: https://github.com/tbeu/matio
commit_details: repo_url: https://github.com/tbeu/matio
author: tbeu
author_date: 2019-07-29 14:25:56+02:00
msg: Fix integer addition overflow

As reported by https://github.com/tbeu/matio/issues/121
repository: repo_url: https://github.com/tbeu/matio
repo_name: tbeu/matio
description: MATLAB MAT File I/O Library
repo_language: C
owner: tbeu
file_changes: filename: mat5.c
change_type: ModificationType.MODIFY
diff: @@ -1009,9 +1009,16 @@ ReadNextCell( mat_t *mat, matvar_t *matvar )
                     if ( uncomp_buf[0] == MAT_T_INT8 ) {    /* Name not in tag */
                         mat_uint32_t len = uncomp_buf[1];
 
-                        if ( len % 8 > 0 )
-                            len = len+(8-(len % 8));
-                        cells[i]->name = (char*)malloc(len+1);
+                        if ( len % 8 > 0 ) {
+                            if ( len < UINT32_MAX - 8 + (len % 8) )
+                              len = len + 8 - (len % 8);
+                            else {
+                                Mat_VarFree(cells[i]);
+                                cells[i] = NULL;
+                                break;
+                            }
+                        }
+                        cells[i]->name = (char*)malloc(len + 1);
                         nbytes -= len;
                         if ( NULL != cells[i]->name ) {
                             /* Variable name */
@@ -1079,7 +1086,8 @@ ReadNextCell( mat_t *mat, matvar_t *matvar )
         mat_uint32_t array_flags;
 
         for ( i = 0; i < nelems; i++ ) {
-            int cell_bytes_read,name_len;
+            int cell_bytes_read;
+            mat_uint32_t name_len;
             cells[i] = Mat_VarCalloc();
             if ( !cells[i] ) {
                 Mat_Critical("Couldn't allocate memory for cell %" SIZE_T_FMTSTR, i);
@@ -1153,7 +1161,13 @@ ReadNextCell( mat_t *mat, matvar_t *matvar )
                 if ( buf[0] == MAT_T_INT8 ) {    /* Name not in tag */
                     name_len = buf[1];
                     if ( name_len % 8 > 0 )
-                        name_len = name_len+(8-(name_len % 8));
+                        if ( name_len < UINT32_MAX - 8 + (name_len % 8) )
+                            name_len = name_len + 8 - (name_len % 8);
+                        else {
+                            Mat_VarFree(cells[i]);
+                            cells[i] = NULL;
+                            break;
+                        }
                     nBytes -= name_len;
                     (void)fseek((FILE*)mat->fp,name_len,SEEK_CUR);
                 }
@@ -4872,8 +4886,13 @@ Mat_VarReadNextInfo5( mat_t *mat )
 
                     if ( len % 8 == 0 )
                         len_pad = len;
-                    else
+                    else if ( len < UINT32_MAX - 8 + (len % 8) )
                         len_pad = len + 8 - (len % 8);
+                    else {
+                        Mat_VarFree(matvar);
+                        matvar = NULL;
+                        break;
+                    }
                     matvar->name = (char*)malloc(len_pad + 1);
                     if ( NULL != matvar->name ) {
                         /* Variable name */
@@ -4974,8 +4993,14 @@ Mat_VarReadNextInfo5( mat_t *mat )
                     len = buf[1];
                 if ( len % 8 == 0 )
                     len_pad = len;
-                else
+                else if ( len < UINT32_MAX - 8 + (len % 8) )
                     len_pad = len + 8 - (len % 8);
+                else {
+                    Mat_VarFree(matvar);
+                    matvar = NULL;
+                    (void)fseek((FILE*)mat->fp, fpos, SEEK_SET);
+                    break;
+                }
                 matvar->name = (char*)malloc(len_pad + 1);
                 if ( NULL != matvar->name ) {
                     readresult = fread(matvar->name, 1, len_pad, (FILE*)mat->fp);

programming_language: C