CVE-2020-19497: Integer Overflow or Wraparound (CWE-190)
value: 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.
CWE 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.
CVSS v3 base score: 8.8
Published: 2021-07-21T18:15Z
Repository: https://github.com/tbeu/matio
Language: C
Files changed: src/mat5.c
Commit date: 2019-07-29 14:25:56 +0200
Commit message: Fix integer addition overflow

As reported by https://github.com/tbeu/matio/issues/121

Diff: diff --git a/src/mat5.c b/src/mat5.c
index abdb351..78e2f4f 100644
--- a/src/mat5.c
+++ b/src/mat5.c
@@ -1007,13 +1007,20 @@ ReadNextCell( mat_t *mat, matvar_t *matvar )
                 if ( uncomp_buf[1] > 0 ) {
                     /* Name of variable */
                     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 */
                             bytesread += InflateVarName(mat,matvar,cells[i]->name,len);
                             cells[i]->name[len] = '\0';
@@ -1077,11 +1084,12 @@ ReadNextCell( mat_t *mat, matvar_t *matvar )
         mat_uint32_t buf[6];
         int nBytes;
         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);
                 continue;
             }
@@ -1151,11 +1159,17 @@ ReadNextCell( mat_t *mat, matvar_t *matvar )
             if ( buf[1] > 0 ) {
                 /* Name of variable */
                 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);
                 }
             }
             cells[i]->internal->datapos = ftell((FILE*)mat->fp);
@@ -4870,12 +4884,17 @@ Mat_VarReadNextInfo5( mat_t *mat )
                     else
                         len = uncomp_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;
+                        break;
+                    }
                     matvar->name = (char*)malloc(len_pad + 1);
                     if ( NULL != matvar->name ) {
                         /* Variable name */
                         bytesread += InflateVarName(mat,matvar,matvar->name,len_pad);
                         matvar->name[len] = '\0';
@@ -4972,12 +4991,18 @@ Mat_VarReadNextInfo5( mat_t *mat )
                     len = Mat_uint32Swap(buf+1);
                 else
                     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);
                     if ( readresult == len_pad ) {
                         matvar->name[len] = '\0';

Vulnerable code:                         if ( len % 8 > 0 )
                            len = len+(8-(len % 8));
                        cells[i]->name = (char*)malloc(len+1);
            int cell_bytes_read,name_len;
                        name_len = name_len+(8-(name_len % 8));
                    else
                else
Fixed code:                         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);
            int cell_bytes_read;
            mat_uint32_t name_len;
                        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;
                        }
                    else if ( len < UINT32_MAX - 8 + (len % 8) )
                    else {
                        Mat_VarFree(matvar);
                        matvar = NULL;
                        break;
                    }
                else if ( len < UINT32_MAX - 8 + (len % 8) )
                else {
                    Mat_VarFree(matvar);
                    matvar = NULL;
                    (void)fseek((FILE*)mat->fp, fpos, SEEK_SET);
                    break;
                }