Repository navigation
Expand file tree
/
Copy pathCVE-2017-18207.diff
More file actions
365 lines (349 loc) · 15 KB
/
Copy pathCVE-2017-18207.diff
File metadata and controls
365 lines (349 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
diff --git a/Lib/aifc.py b/Lib/aifc.py
index 981f8010690e00..880ad50a48da67 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -135,6 +135,7 @@
"""
import struct
+import warnings
import __builtin__
__all__ = ["Error","open","openfp"]
@@ -316,16 +317,16 @@ def initfp(self, file):
except EOFError:
break
chunkname = chunk.getname()
- if chunkname == 'COMM':
+ if chunkname == b'COMM':
self._read_comm_chunk(chunk)
self._comm_chunk_read = 1
- elif chunkname == 'SSND':
+ elif chunkname == b'SSND':
self._ssnd_chunk = chunk
dummy = chunk.read(8)
self._ssnd_seek_needed = 0
- elif chunkname == 'FVER':
+ elif chunkname == b'FVER':
self._version = _read_ulong(chunk)
- elif chunkname == 'MARK':
+ elif chunkname == b'MARK':
self._readmark(chunk)
chunk.skip()
if not self._comm_chunk_read or not self._ssnd_chunk:
@@ -465,13 +466,18 @@ def _read_comm_chunk(self, chunk):
self._nframes = _read_long(chunk)
self._sampwidth = (_read_short(chunk) + 7) // 8
self._framerate = int(_read_float(chunk))
+ if self._sampwidth <= 0:
+ raise Error, 'bad sample width'
+ if self._nchannels <= 0:
+ raise Error, 'bad # of channels'
+
self._framesize = self._nchannels * self._sampwidth
if self._aifc:
#DEBUG: SGI's soundeditor produces a bad size :-(
kludge = 0
if chunk.chunksize == 18:
kludge = 1
- print 'Warning: bad COMM chunk size'
+ warnings.warn("bad COMM chunk size")
chunk.chunksize = 23
#DEBUG end
self._comptype = chunk.read(4)
@@ -535,11 +541,13 @@ def _readmark(self, chunk):
# a position 0 and name ''
self._markers.append((id, pos, name))
except EOFError:
- print 'Warning: MARK chunk contains only',
- print len(self._markers),
- if len(self._markers) == 1: print 'marker',
- else: print 'markers',
- print 'instead of', nmarkers
+ warning_message = 'MARK chunk contains only ' + str(len(self._markers))
+ if len(self._markers) == 1:
+ warning_message += ' marker '
+ else:
+ warning_message += ' markers '
+ warning_message += 'instead of ' + str(nmarkers)
+ warnings.warn(warning_message)
class Aifc_write:
# Variables used in this class:
diff --git a/Lib/sunau.py b/Lib/sunau.py
index b53044d22b1648..b5d83ea4b464c6 100644
--- a/Lib/sunau.py
+++ b/Lib/sunau.py
@@ -194,6 +194,8 @@ def initfp(self, file):
raise Error, 'unknown encoding'
self._framerate = int(_read_u32(file))
self._nchannels = int(_read_u32(file))
+ if not self._nchannels:
+ raise Error('bad # of channels')
self._framesize = self._framesize * self._nchannels
if self._hdr_size > 24:
self._info = file.read(self._hdr_size - 24)
diff --git a/Lib/test/test_aifc.py b/Lib/test/test_aifc.py
index 92bbe7bc75da93..f21798dfe4bc7c 100644
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -7,6 +7,7 @@
import sys
import struct
import aifc
+from test.test_support import check_warnings
class AifcTest(audiotests.AudioWriteTests,
@@ -216,47 +217,68 @@ def test_read_no_comm_chunk(self):
def test_read_no_ssnd_chunk(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 38, 0, 0, 0, 0, 0, 0)
+ b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
with self.assertRaisesRegexp(aifc.Error, 'COMM chunk and/or SSND chunk'
' missing'):
aifc.open(io.BytesIO(b))
def test_read_wrong_compression_type(self):
- b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
- b += 'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0)
- b += 'WRNG' + struct.pack('B', 0)
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 23, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'WRNG' + struct.pack('B', 0)
self.assertRaises(aifc.Error, aifc.open, io.BytesIO(b))
+ def test_read_wrong_number_of_channels(self):
+ for nchannels in 0, -1:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 38, nchannels, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ with self.assertRaisesRegexp(aifc.Error, 'bad # of channels'):
+ aifc.open(io.BytesIO(b))
+
+ def test_read_wrong_sample_width(self):
+ for sampwidth in 0, -1:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 38, 1, 0, sampwidth,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'NONE' + struct.pack('B', 14) + b'not compressed' + b'\x00'
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ with self.assertRaisesRegexp(aifc.Error, 'bad sample width'):
+ aifc.open(io.BytesIO(b))
+
def test_read_wrong_marks(self):
- b = 'FORM' + struct.pack('>L', 4) + 'AIFF'
- b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
- b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
- b += 'MARK' + struct.pack('>LhB', 3, 1, 1)
- with captured_stdout() as s:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFF'
+ b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ b += b'MARK' + struct.pack('>LhB', 3, 1, 1)
+ with check_warnings(('MARK chunk contains only 0 markers instead of 1', UserWarning)):
f = aifc.open(io.BytesIO(b))
- self.assertEqual(s.getvalue(), 'Warning: MARK chunk contains '
- 'only 0 markers instead of 1\n')
- self.assertEqual(f.getmarkers(), None)
+ self.assertEqual(f.getmarkers(), None)
def test_read_comm_kludge_compname_even(self):
- b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
- b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
- b += 'NONE' + struct.pack('B', 4) + 'even' + '\x00'
- b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
- with captured_stdout() as s:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'NONE' + struct.pack('B', 4) + b'even' + b'\x00'
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ with check_warnings(('bad COMM chunk size', UserWarning)):
f = aifc.open(io.BytesIO(b))
- self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n')
self.assertEqual(f.getcompname(), 'even')
def test_read_comm_kludge_compname_odd(self):
- b = 'FORM' + struct.pack('>L', 4) + 'AIFC'
- b += 'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
- b += 'NONE' + struct.pack('B', 3) + 'odd'
- b += 'SSND' + struct.pack('>L', 8) + '\x00' * 8
- with captured_stdout() as s:
+ b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
+ b += b'COMM' + struct.pack('>LhlhhLL', 18, 1, 0, 8,
+ 0x4000 | 12, 11025<<18, 0)
+ b += b'NONE' + struct.pack('B', 3) + b'odd'
+ b += b'SSND' + struct.pack('>L', 8) + b'\x00' * 8
+ with check_warnings(('bad COMM chunk size', UserWarning)):
f = aifc.open(io.BytesIO(b))
- self.assertEqual(s.getvalue(), 'Warning: bad COMM chunk size\n')
self.assertEqual(f.getcompname(), 'odd')
def test_write_params_raises(self):
diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py
index f6828683521df1..1550600495fa78 100644
--- a/Lib/test/test_sunau.py
+++ b/Lib/test/test_sunau.py
@@ -1,6 +1,8 @@
from test.test_support import TESTFN, run_unittest
import unittest
from test import audiotests
+import io
+import struct
import sys
import sunau
@@ -96,5 +98,41 @@ def test_main():
run_unittest(SunauPCM8Test, SunauPCM16Test, SunauPCM16Test,
SunauPCM32Test, SunauULAWTest)
+
+class SunauLowLevelTest(unittest.TestCase):
+
+ def test_read_bad_magic_number(self):
+ b = b'SPA'
+ with self.assertRaises(EOFError):
+ sunau.open(io.BytesIO(b))
+ b = b'SPAM'
+ with self.assertRaisesRegex(sunau.Error, 'bad magic number'):
+ sunau.open(io.BytesIO(b))
+
+ def test_read_too_small_header(self):
+ b = struct.pack('>LLLLL', sunau.AUDIO_FILE_MAGIC, 20, 0,
+ sunau.AUDIO_FILE_ENCODING_LINEAR_8, 11025)
+ with self.assertRaisesRegex(sunau.Error, 'header size too small'):
+ sunau.open(io.BytesIO(b))
+
+ def test_read_too_large_header(self):
+ b = struct.pack('>LLLLLL', sunau.AUDIO_FILE_MAGIC, 124, 0,
+ sunau.AUDIO_FILE_ENCODING_LINEAR_8, 11025, 1)
+ b += b'\0' * 100
+ with self.assertRaisesRegex(sunau.Error, 'header size ridiculously large'):
+ sunau.open(io.BytesIO(b))
+
+ def test_read_wrong_encoding(self):
+ b = struct.pack('>LLLLLL', sunau.AUDIO_FILE_MAGIC, 24, 0, 0, 11025, 1)
+ with self.assertRaisesRegex(sunau.Error, r'encoding not \(yet\) supported'):
+ sunau.open(io.BytesIO(b))
+
+ def test_read_wrong_number_of_channels(self):
+ b = struct.pack('>LLLLLL', sunau.AUDIO_FILE_MAGIC, 24, 0,
+ sunau.AUDIO_FILE_ENCODING_LINEAR_8, 11025, 0)
+ with self.assertRaisesRegex(sunau.Error, 'bad # of channels'):
+ sunau.open(io.BytesIO(b))
+
+
if __name__ == "__main__":
test_main()
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index 9513df459386b2..9d01eacc539a43 100644
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -1,6 +1,9 @@
from test.test_support import TESTFN, run_unittest
import unittest
from test import audiotests
+from test import support
+import io
+import struct
import sys
import wave
@@ -119,5 +122,66 @@ def test_unseekable_incompleted_write(self):
def test_main():
run_unittest(WavePCM8Test, WavePCM16Test, WavePCM24Test, WavePCM32Test)
+
+class WaveLowLevelTest(unittest.TestCase):
+
+ def test_read_no_chunks(self):
+ b = b'SPAM'
+ with self.assertRaises(EOFError):
+ wave.open(io.BytesIO(b))
+
+ def test_read_no_riff_chunk(self):
+ b = b'SPAM' + struct.pack('<L', 0)
+ with self.assertRaisesRegex(wave.Error,
+ 'file does not start with RIFF id'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_not_wave(self):
+ b = b'RIFF' + struct.pack('<L', 4) + b'SPAM'
+ with self.assertRaisesRegex(wave.Error,
+ 'not a WAVE file'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_no_fmt_no_data_chunk(self):
+ b = b'RIFF' + struct.pack('<L', 4) + b'WAVE'
+ with self.assertRaisesRegex(wave.Error,
+ 'fmt chunk and/or data chunk missing'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_no_data_chunk(self):
+ b = b'RIFF' + struct.pack('<L', 28) + b'WAVE'
+ b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 1, 11025, 11025, 1, 8)
+ with self.assertRaisesRegex(wave.Error,
+ 'fmt chunk and/or data chunk missing'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_no_fmt_chunk(self):
+ b = b'RIFF' + struct.pack('<L', 12) + b'WAVE'
+ b += b'data' + struct.pack('<L', 0)
+ with self.assertRaisesRegex(wave.Error, 'data chunk before fmt chunk'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_wrong_form(self):
+ b = b'RIFF' + struct.pack('<L', 36) + b'WAVE'
+ b += b'fmt ' + struct.pack('<LHHLLHH', 16, 2, 1, 11025, 11025, 1, 1)
+ b += b'data' + struct.pack('<L', 0)
+ with self.assertRaisesRegex(wave.Error, 'unknown format: 2'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_wrong_number_of_channels(self):
+ b = b'RIFF' + struct.pack('<L', 36) + b'WAVE'
+ b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 0, 11025, 11025, 1, 8)
+ b += b'data' + struct.pack('<L', 0)
+ with self.assertRaisesRegex(wave.Error, 'bad # of channels'):
+ wave.open(io.BytesIO(b))
+
+ def test_read_wrong_sample_width(self):
+ b = b'RIFF' + struct.pack('<L', 36) + b'WAVE'
+ b += b'fmt ' + struct.pack('<LHHLLHH', 16, 1, 1, 11025, 11025, 1, 0)
+ b += b'data' + struct.pack('<L', 0)
+ with self.assertRaisesRegex(wave.Error, 'bad sample width'):
+ wave.open(io.BytesIO(b))
+
+
if __name__ == '__main__':
test_main()
diff --git a/Lib/wave.py b/Lib/wave.py
index 28acaa6cb43ec8..2d34ae756270db 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -266,12 +266,22 @@ def readframes(self, nframes):
#
def _read_fmt_chunk(self, chunk):
- wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack('<HHLLH', chunk.read(14))
+ try:
+ wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
+ except struct.error:
+ raise EOFError
if wFormatTag == WAVE_FORMAT_PCM:
- sampwidth = struct.unpack('<H', chunk.read(2))[0]
+ try:
+ sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
+ except struct.error:
+ raise EOFError
self._sampwidth = (sampwidth + 7) // 8
+ if not self._sampwidth:
+ raise Error('bad sample width')
else:
- raise Error, 'unknown format: %r' % (wFormatTag,)
+ raise Error('unknown format: %r' % (wFormatTag,))
+ if not self._nchannels:
+ raise Error('bad # of channels')
self._framesize = self._nchannels * self._sampwidth
self._comptype = 'NONE'
self._compname = 'not compressed'
diff --git a/Misc/NEWS.d/next/Library/2018-03-01-17-49-56.bpo-32056.IlpfgE.rst b/Misc/NEWS.d/next/Library/2018-03-01-17-49-56.bpo-32056.IlpfgE.rst
new file mode 100644
index 00000000000000..421aa3767794e0
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-03-01-17-49-56.bpo-32056.IlpfgE.rst
@@ -0,0 +1,3 @@
+Improved exceptions raised for invalid number of channels and sample width
+when read an audio file in modules :mod:`aifc`, :mod:`wave` and
+:mod:`sunau`.