History

targa: fix potential buffer overreads

Jean-Daniel Dupas committed 15 years ago

Commit 7782cb21 parent 56cbc5f1 file · +34 −10Browse files

Unified · Split

libavcodec/targa.c+34−10
@@ -33,22 +33,35 @@ typedef struct TargaContext { · 32 unmodified lines
3333 int compression_type;
3434 } TargaContext;
3535
36−static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, uint8_t *dst, int w, int h, int stride, int bpp)
36+#define CHECK_BUFFER_SIZE(buf, buf_end, needed, where) \
37+ if(buf + needed > buf_end){ \
38+ av_log(avctx, AV_LOG_ERROR, "Problem: unexpected end of data while reading " where "\n"); \
39+ return -1; \
40+ } \
41+
42+static int targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8_t *src, int src_size, uint8_t *dst, int w, int h, int stride, int bpp)
3743 {
3844 int i, x, y;
3945 int depth = (bpp + 1) >> 3;
4046 int type, count;
4147 int diff;
48+ const uint8_t *src_end = src + src_size;
4249
4350 diff = stride - w * depth;
4451 x = y = 0;
4552 while(y < h){
53+ CHECK_BUFFER_SIZE(src, src_end, 1, "image type");
4654 type = *src++;
4755 count = (type & 0x7F) + 1;
4856 type &= 0x80;
4957 if((x + count > w) && (x + count + 1 > (h - y) * w)){
5058 av_log(avctx, AV_LOG_ERROR, "Packet went out of bounds: position (%i,%i) size %i\n", x, y, count);
51− return;
59+ return -1;
60+ }
61+ if(type){
62+ CHECK_BUFFER_SIZE(src, src_end, depth, "image data");
63+ }else{
64+ CHECK_BUFFER_SIZE(src, src_end, count * depth, "image data");
5265 }
5366 for(i = 0; i < count; i++){
5467 switch(depth){
@@ -81,6 +94,7 @@ static void targa_decode_rle(AVCodecContext *avctx, TargaContext *s, const uint8 · 26 unmodified lines
8194 if(type)
8295 src += depth;
8396 }
97+ return src_size;
8498 }
8599
86100 static int decode_frame(AVCodecContext *avctx,
@@ -88,7 +102,7 @@ static int decode_frame(AVCodecContext *avctx, · 1 unmodified line
88102 AVPacket *avpkt)
89103 {
90104 const uint8_t *buf = avpkt->data;
91− int buf_size = avpkt->size;
105+ const uint8_t *buf_end = avpkt->data + avpkt->size;
92106 TargaContext * const s = avctx->priv_data;
93107 AVFrame *picture = data;
94108 AVFrame * const p= (AVFrame*)&s->picture;
@@ -98,6 +112,7 @@ static int decode_frame(AVCodecContext *avctx, · 3 unmodified lines
98112 int first_clr, colors, csize;
99113
100114 /* parse image header */
115+ CHECK_BUFFER_SIZE(buf, buf_end, 18, "header");
101116 idlen = *buf++;
102117 pal = *buf++;
103118 compr = *buf++;
@@ -111,6 +126,7 @@ static int decode_frame(AVCodecContext *avctx, · 7 unmodified lines
111126 bpp = *buf++;
112127 flags = *buf++;
113128 //skip identifier if any
129+ CHECK_BUFFER_SIZE(buf, buf_end, idlen, "identifiers");
114130 buf += idlen;
115131 s->bpp = bpp;
116132 s->width = w;
@@ -163,6 +179,7 @@ static int decode_frame(AVCodecContext *avctx, · 46 unmodified lines
163179 }
164180 }
165181 if(colors){
182+ size_t pal_size;
166183 if((colors + first_clr) > 256){
167184 av_log(avctx, AV_LOG_ERROR, "Incorrect palette: %i colors with offset %i\n", colors, first_clr);
168185 return -1;
@@ -171,8 +188,10 @@ static int decode_frame(AVCodecContext *avctx, · 2 unmodified lines
171188 av_log(avctx, AV_LOG_ERROR, "Palette entry size %i bits is not supported\n", csize);
172189 return -1;
173190 }
191+ pal_size = colors * ((csize + 1) >> 3);
192+ CHECK_BUFFER_SIZE(buf, buf_end, pal_size, "color table");
174193 if(avctx->pix_fmt != PIX_FMT_PAL8)//should not occur but skip palette anyway
175− buf += colors * ((csize + 1) >> 3);
194+ buf += pal_size;
176195 else{
177196 int r, g, b, t;
178197 int32_t *pal = ((int32_t*)p->data[1]) + first_clr;
@@ -188,9 +207,14 @@ static int decode_frame(AVCodecContext *avctx, · 9 unmodified lines
188207 if((compr & (~TGA_RLE)) == TGA_NODATA)
189208 memset(p->data[0], 0, p->linesize[0] * s->height);
190209 else{
191− if(compr & TGA_RLE)
192− targa_decode_rle(avctx, s, buf, dst, avctx->width, avctx->height, stride, bpp);
193− else{
210+ if(compr & TGA_RLE){
211+ int res = targa_decode_rle(avctx, s, buf, buf_end - buf, dst, avctx->width, avctx->height, stride, bpp);
212+ if (res < 0)
213+ return -1;
214+ buf += res;
215+ }else{
216+ size_t img_size = s->width * ((s->bpp + 1) >> 3);
217+ CHECK_BUFFER_SIZE(buf, buf_end, img_size, "image data");
194218 for(y = 0; y < s->height; y++){
195219 #if HAVE_BIGENDIAN
196220 if((s->bpp + 1) >> 3 == 2){
@@ -203,10 +227,10 @@ static int decode_frame(AVCodecContext *avctx, · 6 unmodified lines
203227 dst32[x] = AV_RL32(buf + x * 4);
204228 }else
205229 #endif
206− memcpy(dst, buf, s->width * ((s->bpp + 1) >> 3));
230+ memcpy(dst, buf, img_size);
207231
208232 dst += stride;
209− buf += s->width * ((s->bpp + 1) >> 3);
233+ buf += img_size;
210234 }
211235 }
212236 }
@@ -214,7 +238,7 @@ static int decode_frame(AVCodecContext *avctx, · 1 unmodified line
214238 *picture= *(AVFrame*)&s->picture;
215239 *data_size = sizeof(AVPicture);
216240
217− return buf_size;
241+ return avpkt->size;
218242 }
219243
220244 static av_cold int targa_init(AVCodecContext *avctx){

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t