File python-3.6-CVE-2017-18207.patch of Package python3.35632
From 58753f8510bcbc848bfbd885f3cb2b4c9628ac23 Mon Sep 17 00:00:00 2001
From: Serhiy Storchaka <storchaka@gmail.com>
Date: Thu, 1 Mar 2018 17:50:01 +0200
Subject: [PATCH] [CVE-2017-18207] add check for channels of wav file in sound
related modules
Source is gh#python/cpython!5951 (released upstream in 3.7.0)
Fixes: bsc#1083507
Patch: python-3.6-CVE-2017-18207.patch
---
Lib/aifc.py | 4 ++
Lib/sunau.py | 2 +
Lib/test/test_aifc.py | 35 +++++++++--
Lib/test/test_sunau.py | 37 +++++++++++
Lib/test/test_wave.py | 62 +++++++++++++++++++
Lib/wave.py | 14 ++++-
.../2018-03-01-17-49-56.bpo-32056.IlpfgE.rst | 3 +
7 files changed, 150 insertions(+), 7 deletions(-)
create mode 100644 Misc/NEWS.d/next/Library/2018-03-01-17-49-56.bpo-32056.IlpfgE.rst
diff --git a/Lib/aifc.py b/Lib/aifc.py
index 9df6cd1d5e9..ef55f6b78fe 100644
--- a/Lib/aifc.py
+++ b/Lib/aifc.py
@@ -467,6 +467,10 @@ class Aifc_read:
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 :-(
diff --git a/Lib/sunau.py b/Lib/sunau.py
index 0473e9e4ca1..4b65d48796a 100644
--- a/Lib/sunau.py
+++ b/Lib/sunau.py
@@ -207,6 +207,8 @@ class Au_read:
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 9e8cd17be14..a50b8636893 100644
--- a/Lib/test/test_aifc.py
+++ b/Lib/test/test_aifc.py
@@ -265,7 +265,8 @@ class AIFCLowLevelTest(unittest.TestCase):
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.assertRaisesRegex(aifc.Error, 'COMM chunk and/or SSND chunk'
' missing'):
@@ -273,13 +274,35 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_wrong_compression_type(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 23, 0, 0, 0, 0, 0, 0)
+ 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.assertRaisesRegex(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.assertRaisesRegex(aifc.Error, 'bad sample width'):
+ aifc.open(io.BytesIO(b))
+
def test_read_wrong_marks(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFF'
- b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
+ 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 self.assertWarns(UserWarning) as cm:
@@ -290,7 +313,8 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_comm_kludge_compname_even(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
+ 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 self.assertWarns(UserWarning) as cm:
@@ -300,7 +324,8 @@ class AIFCLowLevelTest(unittest.TestCase):
def test_read_comm_kludge_compname_odd(self):
b = b'FORM' + struct.pack('>L', 4) + b'AIFC'
- b += b'COMM' + struct.pack('>LhlhhLL', 18, 0, 0, 0, 0, 0, 0)
+ 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 self.assertWarns(UserWarning) as cm:
diff --git a/Lib/test/test_sunau.py b/Lib/test/test_sunau.py
index bc1f46c0ebd..7f1c0a5cbde 100644
--- a/Lib/test/test_sunau.py
+++ b/Lib/test/test_sunau.py
@@ -1,6 +1,8 @@
import unittest
from test import audiotests
from audioop import byteswap
+import io
+import struct
import sys
import sunau
@@ -117,5 +119,40 @@ class SunauULAWTest(SunauTest, unittest.TestCase):
frames = byteswap(frames, 2)
+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__":
unittest.main()
diff --git a/Lib/test/test_wave.py b/Lib/test/test_wave.py
index 8666f7269cb..eb231cb19c6 100644
--- a/Lib/test/test_wave.py
+++ b/Lib/test/test_wave.py
@@ -2,6 +2,8 @@ import unittest
from test import audiotests
from test import support
from audioop import byteswap
+import io
+import struct
import sys
import wave
@@ -109,5 +111,65 @@ class MiscTestCase(unittest.TestCase):
support.check__all__(self, wave, blacklist=blacklist)
+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__':
unittest.main()
diff --git a/Lib/wave.py b/Lib/wave.py
index f71f7e5bf94..ccb5205102e 100644
--- a/Lib/wave.py
+++ b/Lib/wave.py
@@ -252,12 +252,22 @@ class Wave_read:
#
def _read_fmt_chunk(self, chunk):
- wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
+ try:
+ wFormatTag, self._nchannels, self._framerate, dwAvgBytesPerSec, wBlockAlign = struct.unpack_from('<HHLLH', chunk.read(14))
+ except struct.error:
+ raise EOFError from None
if wFormatTag == WAVE_FORMAT_PCM:
- sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
+ try:
+ sampwidth = struct.unpack_from('<H', chunk.read(2))[0]
+ except struct.error:
+ raise EOFError from None
self._sampwidth = (sampwidth + 7) // 8
+ if not self._sampwidth:
+ raise Error('bad sample width')
else:
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 00000000000..421aa376779
--- /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`.
--
2.45.0