diff mbox series

[FFmpeg-devel] avcodec/dvdsub_parser: Fix length check for short packets

Message ID 20220930142009.5862-1-aidanmacdonald.0x0@gmail.com
State Accepted
Commit a4405cc0cc16413fe5222c888b3d4bb04cedc83f
Headers show
Series [FFmpeg-devel] avcodec/dvdsub_parser: Fix length check for short packets | expand

Commit Message

Aidan MacDonald Sept. 30, 2022, 2:20 p.m. UTC
The DVD subtitle parser handles two types of packets: "normal"
packets with a 16-bit length, and HD-DVD packets that set the
16-bit length to 0 and encode a 32-bit length in the next four
bytes. This implies that HD-DVD packets are at least six bytes
long, but the code didn't actually verify this.

The faulty length check results in an out of bounds read for
zero-length "normal" packets that occur in the input, which are
only 2 bytes long, but get misinterpreted as an HD-DVD packet.
When this happens the parser reads packet_len from beyond the
end of the input buffer. The subtitle stream is not correctly
decoded after this point due to the garbage packet_len.

Fixing this is pretty simple: fix the length check so packets
less than 6 bytes long will not be mistakenly parsed as HD-DVD
packets.

Signed-off-by: Aidan MacDonald <aidanmacdonald.0x0@gmail.com>
---
 libavcodec/dvdsub_parser.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Anton Khirnov Nov. 12, 2022, 1:49 p.m. UTC | #1
Quoting Aidan MacDonald (2022-09-30 16:20:09)
> The DVD subtitle parser handles two types of packets: "normal"
> packets with a 16-bit length, and HD-DVD packets that set the
> 16-bit length to 0 and encode a 32-bit length in the next four
> bytes. This implies that HD-DVD packets are at least six bytes
> long, but the code didn't actually verify this.
> 
> The faulty length check results in an out of bounds read for
> zero-length "normal" packets that occur in the input, which are
> only 2 bytes long, but get misinterpreted as an HD-DVD packet.
> When this happens the parser reads packet_len from beyond the
> end of the input buffer. The subtitle stream is not correctly
> decoded after this point due to the garbage packet_len.
> 
> Fixing this is pretty simple: fix the length check so packets
> less than 6 bytes long will not be mistakenly parsed as HD-DVD
> packets.
> 
> Signed-off-by: Aidan MacDonald <aidanmacdonald.0x0@gmail.com>
> ---
>  libavcodec/dvdsub_parser.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/libavcodec/dvdsub_parser.c b/libavcodec/dvdsub_parser.c
> index 44738a73d6..8871b6a383 100644
> --- a/libavcodec/dvdsub_parser.c
> +++ b/libavcodec/dvdsub_parser.c
> @@ -43,7 +43,7 @@ static int dvdsub_parse(AVCodecParserContext *s,
>      *poutbuf_size = buf_size;
>  
>      if (pc->packet_index == 0) {
> -        if (buf_size < 2 || AV_RB16(buf) && buf_size < 6) {
> +        if (buf_size < 2 || (AV_RB16(buf) == 0 && buf_size < 6)) {

Looks good, will push.
diff mbox series

Patch

diff --git a/libavcodec/dvdsub_parser.c b/libavcodec/dvdsub_parser.c
index 44738a73d6..8871b6a383 100644
--- a/libavcodec/dvdsub_parser.c
+++ b/libavcodec/dvdsub_parser.c
@@ -43,7 +43,7 @@  static int dvdsub_parse(AVCodecParserContext *s,
     *poutbuf_size = buf_size;
 
     if (pc->packet_index == 0) {
-        if (buf_size < 2 || AV_RB16(buf) && buf_size < 6) {
+        if (buf_size < 2 || (AV_RB16(buf) == 0 && buf_size < 6)) {
             if (buf_size)
                 av_log(avctx, AV_LOG_DEBUG, "Parser input %d too small\n", buf_size);
             return buf_size;