Skip to content

Commit 3ea549b

Browse files
authored
examples,test: fix integer overflows in calculating alloc sizes
Allocating memory with a size controlled by an external user can result in integer overflow.
1 parent 3acb43e commit 3ea549b

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

examples/server/pnmshow.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ int main(int argc,char** argv)
8181
rfbScreen->httpDir = "../webclients";
8282

8383
/* allocate picture and read it */
84+
if (bytesPerPixel!=0 && paddedWidth>SIZE_MAX/bytesPerPixel) {
85+
exit(1);
86+
}
87+
if (height!=0 && paddedWidth*bytesPerPixel>SIZE_MAX/height) {
88+
exit(1);
89+
}
8490
rfbScreen->frameBuffer = (char*)malloc(paddedWidth*bytesPerPixel*height);
8591
if(!rfbScreen->frameBuffer)
8692
exit(1);

examples/server/pnmshow24.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ int main(int argc,char** argv)
8080
rfbScreen->httpDir = "../webclients";
8181

8282
/* allocate picture and read it */
83+
if (paddedWidth>SIZE_MAX/3 || (height!=0 && paddedWidth*3>SIZE_MAX/height))
84+
return 1;
8385
rfbScreen->frameBuffer = (char*)malloc(paddedWidth*3*height);
8486
if(!rfbScreen->frameBuffer)
8587
return 1;

test/bmp.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <stdlib.h>
2121
#include <stdio.h>
2222
#include <string.h>
23+
#include <stdint.h>
2324
#ifdef _WIN32
2425
#include <io.h>
2526
#else
@@ -139,7 +140,7 @@ int loadppm(int *fd, unsigned char **buf, int *w, int *h,
139140
if((*w)<1 || (*h)<1 || scalefactor<1) _throw("Corrupt PPM header");
140141

141142
dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1));
142-
if((*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL)
143+
if((*buf=(unsigned char *)calloc(dstpitch, *h))==NULL)
143144
_throw("Memory allocation error");
144145
if(ascii)
145146
{
@@ -159,7 +160,7 @@ int loadppm(int *fd, unsigned char **buf, int *w, int *h,
159160
{
160161
if(scalefactor!=255)
161162
_throw("Binary PPMs must have 8-bit components");
162-
if((tempbuf=(unsigned char *)malloc((*w)*(*h)*3))==NULL)
163+
if(*h>SIZE_MAX/3 || (tempbuf=(unsigned char *)calloc(*w, (*h)*3))==NULL)
163164
_throw("Memory allocation error");
164165
if(fread(tempbuf, (*w)*(*h)*3, 1, fs)!=1) _throw("Read error");
165166
pixelconvert(tempbuf, BMP_RGB, (*w)*3, *buf, f, dstpitch, *w, *h, dstbottomup);
@@ -249,8 +250,8 @@ int loadbmp(char *filename, unsigned char **buf, int *w, int *h,
249250
dstpitch=(((*w)*ps[f])+(align-1))&(~(align-1));
250251

251252
if(srcpitch*(*h)+bh.bfOffBits!=bh.bfSize) _throw("Corrupt bitmap header");
252-
if((tempbuf=(unsigned char *)malloc(srcpitch*(*h)))==NULL
253-
|| (*buf=(unsigned char *)malloc(dstpitch*(*h)))==NULL)
253+
if((tempbuf=(unsigned char *)calloc(srcpitch, *h))==NULL
254+
|| (*buf=(unsigned char *)calloc(dstpitch, *h))==NULL)
254255
_throw("Memory allocation error");
255256
if(lseek(fd, (long)bh.bfOffBits, SEEK_SET)!=(long)bh.bfOffBits)
256257
_throw(strerror(errno));

0 commit comments

Comments
 (0)