Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(240)

Side by Side Diff: media/base/container_names.cc

Issue 659743004: Add extra checks to avoid integer overflow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/base/container_names.h" 5 #include "media/base/container_names.h"
6 6
7 #include <cctype> 7 #include <cctype>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 116
117 // Check sampling frequency index. 117 // Check sampling frequency index.
118 RCHECK(ReadBits(&reader, 4) != 15); // Forbidden. 118 RCHECK(ReadBits(&reader, 4) != 15); // Forbidden.
119 119
120 // Skip private stream, channel configuration, originality, home, 120 // Skip private stream, channel configuration, originality, home,
121 // copyrighted stream, and copyright_start. 121 // copyrighted stream, and copyright_start.
122 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1); 122 reader.SkipBits(1 + 3 + 1 + 1 + 1 + 1);
123 123
124 // Get frame length (includes header). 124 // Get frame length (includes header).
125 int size = ReadBits(&reader, 13); 125 int size = ReadBits(&reader, 13);
126 RCHECK(size > 0); 126 RCHECK(size > 0 && size < 8192);
DaleCurtis 2014/10/24 17:37:01 Why not < buffer_size ?
jrummell 2014/10/24 18:09:49 RCHECK() returns false if the condition is not met
xhwang 2014/10/24 21:04:28 Does the spec say the size can't be 8192?
xhwang 2014/10/24 21:04:59 This is an old comment. Please ignore.
127 offset += size; 127 offset += size;
128 } 128 }
129 return true; 129 return true;
130 } 130 }
131 131
132 const uint16 kAc3SyncWord = 0x0b77; 132 const uint16 kAc3SyncWord = 0x0b77;
133 133
134 // Checks for an AC3 container. 134 // Checks for an AC3 container.
135 static bool CheckAc3(const uint8* buffer, int buffer_size) { 135 static bool CheckAc3(const uint8* buffer, int buffer_size) {
136 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3) 136 // Reference: ATSC Standard: Digital Audio Compression (AC-3, E-AC-3)
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord); 183 RCHECK(ReadBits(&reader, 16) == kAc3SyncWord);
184 184
185 // Verify strmtyp. 185 // Verify strmtyp.
186 RCHECK(ReadBits(&reader, 2) != 3); 186 RCHECK(ReadBits(&reader, 2) != 3);
187 187
188 // Skip substreamid. 188 // Skip substreamid.
189 reader.SkipBits(3); 189 reader.SkipBits(3);
190 190
191 // Get frmsize. Include syncinfo size and convert to bytes. 191 // Get frmsize. Include syncinfo size and convert to bytes.
192 int frame_size = (ReadBits(&reader, 11) + 1) * 2; 192 int frame_size = (ReadBits(&reader, 11) + 1) * 2;
193 RCHECK(frame_size >= 7); 193 RCHECK(frame_size >= 7 && frame_size <= 4096);
DaleCurtis 2014/10/24 17:37:01 ditto?
194 194
195 // Skip fscod, fscod2, acmod, and lfeon. 195 // Skip fscod, fscod2, acmod, and lfeon.
196 reader.SkipBits(2 + 2 + 3 + 1); 196 reader.SkipBits(2 + 2 + 3 + 1);
197 197
198 // Verify bsid. 198 // Verify bsid.
199 int bit_stream_id = ReadBits(&reader, 5); 199 int bit_stream_id = ReadBits(&reader, 5);
200 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16); 200 RCHECK(bit_stream_id >= 11 && bit_stream_id <= 16);
201 201
202 offset += frame_size; 202 offset += frame_size;
203 } 203 }
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 reader.SkipBits(1 + 5); 288 reader.SkipBits(1 + 5);
289 289
290 // Verify CRC present flag. 290 // Verify CRC present flag.
291 RCHECK(ReadBits(&reader, 1) == 0); // CPF must be 0. 291 RCHECK(ReadBits(&reader, 1) == 0); // CPF must be 0.
292 292
293 // Verify number of PCM sample blocks. 293 // Verify number of PCM sample blocks.
294 RCHECK(ReadBits(&reader, 7) >= 5); 294 RCHECK(ReadBits(&reader, 7) >= 5);
295 295
296 // Verify primary frame byte size. 296 // Verify primary frame byte size.
297 int frame_size = ReadBits(&reader, 14); 297 int frame_size = ReadBits(&reader, 14);
298 RCHECK(frame_size >= 95); 298 RCHECK(frame_size >= 95 && frame_size < 16384);
299 299
300 // Skip audio channel arrangement. 300 // Skip audio channel arrangement.
301 reader.SkipBits(6); 301 reader.SkipBits(6);
302 302
303 // Verify core audio sampling frequency is an allowed value. 303 // Verify core audio sampling frequency is an allowed value.
304 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]); 304 RCHECK(kSamplingFrequencyValid[ReadBits(&reader, 4)]);
305 305
306 // Verify transmission bit rate is valid. 306 // Verify transmission bit rate is valid.
307 RCHECK(ReadBits(&reader, 5) <= 25); 307 RCHECK(ReadBits(&reader, 5) <= 25);
308 308
(...skipping 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
662 } else if (code >= 0xd0 && code <= 0xd7) { 662 } else if (code >= 0xd0 && code <= 0xd7) {
663 // RST (restart) codes must be in sequence. No other data with header. 663 // RST (restart) codes must be in sequence. No other data with header.
664 int restart = code & 0x07; 664 int restart = code & 0x07;
665 if (last_restart >= 0) 665 if (last_restart >= 0)
666 RCHECK(restart == (last_restart + 1) % 8); 666 RCHECK(restart == (last_restart + 1) % 8);
667 last_restart = restart; 667 last_restart = restart;
668 offset += 2; 668 offset += 2;
669 } else { 669 } else {
670 // All remaining marker codes are followed by a length of the header. 670 // All remaining marker codes are followed by a length of the header.
671 int length = Read16(buffer + offset + 2) + 2; 671 int length = Read16(buffer + offset + 2) + 2;
672 RCHECK(length > 0 && length < 65538);
672 673
673 // Special handling of SOS (start of scan) marker since the entropy 674 // Special handling of SOS (start of scan) marker since the entropy
674 // coded data follows the SOS. Any xFF byte in the data block must be 675 // coded data follows the SOS. Any xFF byte in the data block must be
675 // followed by x00 in the data. 676 // followed by x00 in the data.
676 if (code == 0xda) { 677 if (code == 0xda) {
677 int number_components = buffer[offset + 4]; 678 int number_components = buffer[offset + 4];
678 RCHECK(length == 8 + 2 * number_components); 679 RCHECK(length == 8 + 2 * number_components);
679 680
680 // Advance to the next marker. 681 // Advance to the next marker.
681 offset += length; 682 offset += length;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
779 else 780 else
780 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe); 781 RCHECK(stream_id != 0xfc && stream_id != 0xfd && stream_id != 0xfe);
781 782
782 // Some stream types are used for pack headers. 783 // Some stream types are used for pack headers.
783 if (stream_id == PACK_START_CODE) // back to outer loop. 784 if (stream_id == PACK_START_CODE) // back to outer loop.
784 break; 785 break;
785 if (stream_id == PROGRAM_END_CODE) // end of stream. 786 if (stream_id == PROGRAM_END_CODE) // end of stream.
786 return true; 787 return true;
787 788
788 int pes_length = Read16(buffer + offset + 4); 789 int pes_length = Read16(buffer + offset + 4);
789 RCHECK(pes_length > 0); 790 RCHECK(pes_length > 0 && pes_length < 32768);
790 offset = offset + 6 + pes_length; 791 offset = offset + 6 + pes_length;
791 } 792 }
792 } 793 }
793 // Success as we are off the end of the buffer and liked everything 794 // Success as we are off the end of the buffer and liked everything
794 // in the buffer. 795 // in the buffer.
795 return true; 796 return true;
796 } 797 }
797 798
798 const uint8 kMpeg2SyncWord = 0x47; 799 const uint8 kMpeg2SyncWord = 0x47;
799 800
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
978 return false; 979 return false;
979 } 980 }
980 if (atomsize == 1) { 981 if (atomsize == 1) {
981 // Indicates that the length is the next 64bits. 982 // Indicates that the length is the next 64bits.
982 if (offset + 16 > buffer_size) 983 if (offset + 16 > buffer_size)
983 break; 984 break;
984 if (Read32(buffer + offset + 8) != 0) 985 if (Read32(buffer + offset + 8) != 0)
985 break; // Offset is way past buffer size. 986 break; // Offset is way past buffer size.
986 atomsize = Read32(buffer + offset + 12); 987 atomsize = Read32(buffer + offset + 12);
987 } 988 }
988 if (atomsize <= 0) 989 if (atomsize <= 0 || atomsize > buffer_size)
989 break; // Indicates the last atom or length too big. 990 break; // Indicates the last atom or length too big.
990 offset += atomsize; 991 offset += atomsize;
991 } 992 }
992 return true; 993 return true;
993 } 994 }
994 995
995 enum MPEGVersion { 996 enum MPEGVersion {
996 VERSION_25 = 0, 997 VERSION_25 = 0,
997 VERSION_RESERVED, 998 VERSION_RESERVED,
998 VERSION_2, 999 VERSION_2,
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
1106 ++offset; 1107 ++offset;
1107 } 1108 }
1108 1109
1109 while (offset + 3 < buffer_size) { 1110 while (offset + 3 < buffer_size) {
1110 RCHECK(ValidMpegAudioFrameHeader( 1111 RCHECK(ValidMpegAudioFrameHeader(
1111 buffer + offset, buffer_size - offset, &framesize)); 1112 buffer + offset, buffer_size - offset, &framesize));
1112 1113
1113 // Have we seen enough valid headers? 1114 // Have we seen enough valid headers?
1114 if (++numSeen > 10) 1115 if (++numSeen > 10)
1115 return true; 1116 return true;
1117
1118 RCHECK(framesize > 0 && framesize < 8192);
1116 offset += framesize; 1119 offset += framesize;
1117 } 1120 }
1118 // Off the end of the buffer, return success if a few valid headers seen. 1121 // Off the end of the buffer, return success if a few valid headers seen.
1119 return numSeen > 2; 1122 return numSeen > 2;
1120 } 1123 }
1121 1124
1122 // Check that the next characters in |buffer| represent a number. The format 1125 // Check that the next characters in |buffer| represent a number. The format
1123 // accepted is optional whitespace followed by 1 or more digits. |max_digits| 1126 // accepted is optional whitespace followed by 1 or more digits. |max_digits|
1124 // specifies the maximum number of digits to process. Returns true if a valid 1127 // specifies the maximum number of digits to process. Returns true if a valid
1125 // number is found, false otherwise. 1128 // number is found, false otherwise.
(...skipping 538 matching lines...) Expand 10 before | Expand all | Expand 10 after
1664 if (CheckEac3(buffer + offset, buffer_size - offset)) 1667 if (CheckEac3(buffer + offset, buffer_size - offset))
1665 return CONTAINER_EAC3; 1668 return CONTAINER_EAC3;
1666 } 1669 }
1667 1670
1668 return CONTAINER_UNKNOWN; 1671 return CONTAINER_UNKNOWN;
1669 } 1672 }
1670 1673
1671 } // namespace container_names 1674 } // namespace container_names
1672 1675
1673 } // namespace media 1676 } // namespace media
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698