1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-21 10:55:51 +02:00

swfdec: do better validation of tag length

Avoids trying to read a packet with 0 or negative size.
Avoids a potential infinite loop due to seeking backwards.

Partially based on a patch by Michael Niedermayer.
This commit is contained in:
Justin Ruggles 2012-12-10 12:44:09 -05:00
parent 02823f6d71
commit e70c5b034c

View File

@ -100,6 +100,10 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
tag = get_swf_tag(pb, &len); tag = get_swf_tag(pb, &len);
if (tag < 0) if (tag < 0)
return AVERROR(EIO); return AVERROR(EIO);
if (len < 0) {
av_log(s, AV_LOG_ERROR, "invalid tag length: %d\n", len);
return AVERROR_INVALIDDATA;
}
if (tag == TAG_VIDEOSTREAM) { if (tag == TAG_VIDEOSTREAM) {
int ch_id = avio_rl16(pb); int ch_id = avio_rl16(pb);
len -= 2; len -= 2;
@ -161,7 +165,10 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
st = s->streams[i]; st = s->streams[i];
if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) { if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO && st->id == ch_id) {
frame = avio_rl16(pb); frame = avio_rl16(pb);
if ((res = av_get_packet(pb, pkt, len-2)) < 0) len -= 2;
if (len <= 0)
goto skip;
if ((res = av_get_packet(pb, pkt, len)) < 0)
return res; return res;
pkt->pos = pos; pkt->pos = pos;
pkt->pts = frame; pkt->pts = frame;
@ -175,9 +182,14 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) { if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO && st->id == -1) {
if (st->codec->codec_id == AV_CODEC_ID_MP3) { if (st->codec->codec_id == AV_CODEC_ID_MP3) {
avio_skip(pb, 4); avio_skip(pb, 4);
if ((res = av_get_packet(pb, pkt, len-4)) < 0) len -= 4;
if (len <= 0)
goto skip;
if ((res = av_get_packet(pb, pkt, len)) < 0)
return res; return res;
} else { // ADPCM, PCM } else { // ADPCM, PCM
if (len <= 0)
goto skip;
if ((res = av_get_packet(pb, pkt, len)) < 0) if ((res = av_get_packet(pb, pkt, len)) < 0)
return res; return res;
} }
@ -203,7 +215,10 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
st = vst; st = vst;
} }
avio_rl16(pb); /* BITMAP_ID */ avio_rl16(pb); /* BITMAP_ID */
if ((res = av_new_packet(pkt, len-2)) < 0) len -= 2;
if (len < 4)
goto skip;
if ((res = av_new_packet(pkt, len)) < 0)
return res; return res;
avio_read(pb, pkt->data, 4); avio_read(pb, pkt->data, 4);
if (AV_RB32(pkt->data) == 0xffd8ffd9 || if (AV_RB32(pkt->data) == 0xffd8ffd9 ||
@ -220,6 +235,7 @@ static int swf_read_packet(AVFormatContext *s, AVPacket *pkt)
return pkt->size; return pkt->size;
} }
skip: skip:
len = FFMAX(0, len);
avio_skip(pb, len); avio_skip(pb, len);
} }
} }