[Python-checkins] r82496 - in python/branches/release31-maint: Lib/test/test_audioop.py Misc/NEWS Modules/audioop.c

victor.stinner python-checkins at python.org
Sat Jul 3 15:46:02 CEST 2010


Author: victor.stinner
Date: Sat Jul  3 15:46:01 2010
New Revision: 82496

Log:
Merged revisions 82495 via svnmerge from 
svn+ssh://pythondev@svn.python.org/python/branches/py3k

................
  r82495 | victor.stinner | 2010-07-03 15:44:22 +0200 (sam., 03 juil. 2010) | 10 lines
  
  Merged revisions 82492 via svnmerge from 
  svn+ssh://pythondev@svn.python.org/python/trunk
  
  ........
    r82492 | victor.stinner | 2010-07-03 15:36:19 +0200 (sam., 03 juil. 2010) | 3 lines
    
    Issue #7673: Fix security vulnerability (CVE-2010-2089) in the audioop module,
    ensure that the input string length is a multiple of the frame size
  ........
................


Modified:
   python/branches/release31-maint/   (props changed)
   python/branches/release31-maint/Lib/test/test_audioop.py
   python/branches/release31-maint/Misc/NEWS
   python/branches/release31-maint/Modules/audioop.c

Modified: python/branches/release31-maint/Lib/test/test_audioop.py
==============================================================================
--- python/branches/release31-maint/Lib/test/test_audioop.py	(original)
+++ python/branches/release31-maint/Lib/test/test_audioop.py	Sat Jul  3 15:46:01 2010
@@ -20,6 +20,12 @@
 
 data = [gendata1(), gendata2(), gendata4()]
 
+INVALID_DATA = [
+    ('abc', 0),
+    ('abc', 2),
+    ('abc', 4),
+]
+
 
 class TestAudioop(unittest.TestCase):
 
@@ -168,6 +174,33 @@
         self.assertRaises(audioop.error,
             audioop.findmax, ''.join(chr(x) for x in range(256)), -2392392)
 
+    def test_issue7673(self):
+        state = None
+        for data, size in INVALID_DATA:
+            size2 = size
+            self.assertRaises(audioop.error, audioop.getsample, data, size, 0)
+            self.assertRaises(audioop.error, audioop.max, data, size)
+            self.assertRaises(audioop.error, audioop.minmax, data, size)
+            self.assertRaises(audioop.error, audioop.avg, data, size)
+            self.assertRaises(audioop.error, audioop.rms, data, size)
+            self.assertRaises(audioop.error, audioop.avgpp, data, size)
+            self.assertRaises(audioop.error, audioop.maxpp, data, size)
+            self.assertRaises(audioop.error, audioop.cross, data, size)
+            self.assertRaises(audioop.error, audioop.mul, data, size, 1.0)
+            self.assertRaises(audioop.error, audioop.tomono, data, size, 0.5, 0.5)
+            self.assertRaises(audioop.error, audioop.tostereo, data, size, 0.5, 0.5)
+            self.assertRaises(audioop.error, audioop.add, data, data, size)
+            self.assertRaises(audioop.error, audioop.bias, data, size, 0)
+            self.assertRaises(audioop.error, audioop.reverse, data, size)
+            self.assertRaises(audioop.error, audioop.lin2lin, data, size, size2)
+            self.assertRaises(audioop.error, audioop.ratecv, data, size, 1, 1, 1, state)
+            self.assertRaises(audioop.error, audioop.lin2ulaw, data, size)
+            self.assertRaises(audioop.error, audioop.ulaw2lin, data, size)
+            self.assertRaises(audioop.error, audioop.lin2alaw, data, size)
+            self.assertRaises(audioop.error, audioop.alaw2lin, data, size)
+            self.assertRaises(audioop.error, audioop.lin2adpcm, data, size, state)
+            self.assertRaises(audioop.error, audioop.adpcm2lin, data, size, state)
+
 def test_main():
     run_unittest(TestAudioop)
 

Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS	(original)
+++ python/branches/release31-maint/Misc/NEWS	Sat Jul  3 15:46:01 2010
@@ -75,7 +75,10 @@
 Library
 -------
 
-- Issue #6589: cleanup asyncore.socket_map in case smtpd.SMTPServer constructor 
+- Issue #7673: Fix security vulnerability (CVE-2010-2089) in the audioop
+  module, ensure that the input string length is a multiple of the frame size
+
+- Issue #6589: cleanup asyncore.socket_map in case smtpd.SMTPServer constructor
   raises an exception.
 
 - Issue #9075: In the ssl module, remove the setting of a ``debug`` flag

Modified: python/branches/release31-maint/Modules/audioop.c
==============================================================================
--- python/branches/release31-maint/Modules/audioop.c	(original)
+++ python/branches/release31-maint/Modules/audioop.c	Sat Jul  3 15:46:01 2010
@@ -295,6 +295,29 @@
 
 static PyObject *AudioopError;
 
+static int
+audioop_check_size(int size)
+{
+    if (size != 1 && size != 2 && size != 4) {
+        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+        return 0;
+    }
+    else
+        return 1;
+}
+
+static int
+audioop_check_parameters(int len, int size)
+{
+    if (!audioop_check_size(size))
+        return 0;
+    if (len % size != 0) {
+        PyErr_SetString(AudioopError, "not a whole number of frames");
+        return 0;
+    }
+    return 1;
+}
+
 static PyObject *
 audioop_getsample(PyObject *self, PyObject *args)
 {
@@ -304,10 +327,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#ii:getsample", &cp, &len, &size, &i) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     if ( i < 0 || i >= len/size ) {
         PyErr_SetString(AudioopError, "Index out of range");
         return 0;
@@ -328,10 +349,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#i:max", &cp, &len, &size) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     for ( i=0; i<len; i+= size) {
         if ( size == 1 )      val = (int)*CHARP(cp, i);
         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
@@ -352,10 +371,8 @@
 
     if (!PyArg_ParseTuple(args, "s#i:minmax", &cp, &len, &size))
         return NULL;
-    if (size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+    if (!audioop_check_parameters(len, size))
         return NULL;
-    }
     for (i = 0; i < len; i += size) {
         if (size == 1) val = (int) *CHARP(cp, i);
         else if (size == 2) val = (int) *SHORTP(cp, i);
@@ -376,10 +393,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#i:avg", &cp, &len, &size) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     for ( i=0; i<len; i+= size) {
         if ( size == 1 )      val = (int)*CHARP(cp, i);
         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
@@ -403,10 +418,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#i:rms", &cp, &len, &size) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     for ( i=0; i<len; i+= size) {
         if ( size == 1 )      val = (int)*CHARP(cp, i);
         else if ( size == 2 ) val = (int)*SHORTP(cp, i);
@@ -614,10 +627,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#i:avgpp", &cp, &len, &size) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     /* Compute first delta value ahead. Also automatically makes us
     ** skip the first extreme value
     */
@@ -671,10 +682,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#i:maxpp", &cp, &len, &size) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     /* Compute first delta value ahead. Also automatically makes us
     ** skip the first extreme value
     */
@@ -722,10 +731,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#i:cross", &cp, &len, &size) )
         return 0;
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
     ncross = -1;
     prevval = 17; /* Anything <> 0,1 */
     for ( i=0; i<len; i+= size) {
@@ -750,6 +757,8 @@
 
     if ( !PyArg_ParseTuple(args, "s#id:mul", &cp, &len, &size, &factor ) )
         return 0;
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     if ( size == 1 ) maxval = (double) 0x7f;
     else if ( size == 2 ) maxval = (double) 0x7fff;
@@ -795,6 +804,12 @@
         return 0;
     cp = pcp.buf;
     len = pcp.len;
+    if (!audioop_check_parameters(len, size))
+        return NULL;
+    if (((len / size) & 1) != 0) {
+        PyErr_SetString(AudioopError, "not a whole number of frames");
+        return NULL;
+    }
 
     if ( size == 1 ) maxval = (double) 0x7f;
     else if ( size == 2 ) maxval = (double) 0x7fff;
@@ -842,6 +857,8 @@
     if ( !PyArg_ParseTuple(args, "s#idd:tostereo",
                            &cp, &len, &size, &fac1, &fac2 ) )
         return 0;
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     if ( size == 1 ) maxval = (double) 0x7f;
     else if ( size == 2 ) maxval = (double) 0x7fff;
@@ -900,7 +917,8 @@
     if ( !PyArg_ParseTuple(args, "s#s#i:add",
                       &cp1, &len1, &cp2, &len2, &size ) )
         return 0;
-
+    if (!audioop_check_parameters(len1, size))
+        return NULL;
     if ( len1 != len2 ) {
         PyErr_SetString(AudioopError, "Lengths should be the same");
         return 0;
@@ -955,10 +973,8 @@
                       &cp, &len, &size , &bias) )
         return 0;
 
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     rv = PyBytes_FromStringAndSize(NULL, len);
     if ( rv == 0 )
@@ -991,10 +1007,8 @@
                       &cp, &len, &size) )
         return 0;
 
-    if ( size != 1 && size != 2 && size != 4 ) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     rv = PyBytes_FromStringAndSize(NULL, len);
     if ( rv == 0 )
@@ -1028,11 +1042,10 @@
                       &cp, &len, &size, &size2) )
         return 0;
 
-    if ( (size != 1 && size != 2 && size != 4) ||
-         (size2 != 1 && size2 != 2 && size2 != 4)) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
+    if (!audioop_check_size(size2))
+        return NULL;
 
     if (len/size > INT_MAX/size2) {
         PyErr_SetString(PyExc_MemoryError,
@@ -1082,10 +1095,8 @@
                           &nchannels, &inrate, &outrate, &state,
                           &weightA, &weightB))
         return NULL;
-    if (size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
+    if (!audioop_check_size(size))
         return NULL;
-    }
     if (nchannels < 1) {
         PyErr_SetString(AudioopError, "# of channels should be >= 1");
         return NULL;
@@ -1261,10 +1272,8 @@
                            &cp, &len, &size) )
         return 0 ;
 
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     rv = PyBytes_FromStringAndSize(NULL, len/size);
     if ( rv == 0 )
@@ -1295,10 +1304,8 @@
                            &cp, &len, &size) )
         return 0;
 
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     if (len > INT_MAX/size) {
         PyErr_SetString(PyExc_MemoryError,
@@ -1334,10 +1341,8 @@
                            &cp, &len, &size) )
         return 0;
 
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     rv = PyBytes_FromStringAndSize(NULL, len/size);
     if ( rv == 0 )
@@ -1368,10 +1373,8 @@
                            &cp, &len, &size) )
         return 0;
 
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     if (len > INT_MAX/size) {
         PyErr_SetString(PyExc_MemoryError,
@@ -1408,11 +1411,8 @@
                            &cp, &len, &size, &state) )
         return 0;
 
-
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     str = PyBytes_FromStringAndSize(NULL, len/(size*2));
     if ( str == 0 )
@@ -1516,10 +1516,8 @@
                            &cp, &len, &size, &state) )
         return 0;
 
-    if ( size != 1 && size != 2 && size != 4) {
-        PyErr_SetString(AudioopError, "Size should be 1, 2 or 4");
-        return 0;
-    }
+    if (!audioop_check_parameters(len, size))
+        return NULL;
 
     /* Decode state, should have (value, step) */
     if ( state == Py_None ) {


More information about the Python-checkins mailing list