Author minhrau
Recipients minhrau
Date 2016-09-06.02:10:46
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1473127846.83.0.0299239583888.issue27963@psf.upfronthosting.co.za>
In-reply-to
Content
Description:
------------
Null dereference in function set_conversion_mode due uncheck _ctypes_conversion_errors:

static PyObject *
set_conversion_mode(PyObject *self, PyObject *args)
{
    char *coding, *mode;
    PyObject *result;
...
    PyMem_Free(_ctypes_conversion_errors);
    _ctypes_conversion_errors = PyMem_Malloc(strlen(mode) + 1); //if memory is not enough, _ctypes_conversion_errors will be null
    strcpy(_ctypes_conversion_errors, mode); // crash here
    return result;
}



Test script:
---------------

import ctypes

s = 'a'*(0xffffffff/2-0xffff)
sss = 'a'*(0xffffffff/4)
ctypes.set_conversion_mode('a', s)


Expected result:
----------------
No Crash

Actual result:
--------------
Starting program: /home/minhrau/cpython-2.7/python ~/pythontestcase/test.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.
0xf7def209 in __strcpy_sse2 () from /usr/lib32/libc.so.6
(gdb) bt
#0  0xf7def209 in __strcpy_sse2 () from /usr/lib32/libc.so.6
#1  0xf7fba5f2 in set_conversion_mode (self=0x0, args=0xf7cd602c) at /home/minhrau/cpython-2.7/Modules/_ctypes/callproc.c:1706
#2  0x080f6dfc in call_function (oparg=<optimized out>, pp_stack=0xffffd45c) at Python/ceval.c:4350
#3  PyEval_EvalFrameEx (f=<optimized out>, throwflag=<optimized out>) at Python/ceval.c:2987
#4  0x080f964e in PyEval_EvalCodeEx (co=0xf7cc94e8, globals=0xf7d5b714, locals=0xf7d5b714, args=0x0, argcount=0, kws=0x0, kwcount=0, defs=0x0, defcount=0, closure=0x0) at Python/ceval.c:3582
#5  0x080f9942 in PyEval_EvalCode (co=0xf7cc94e8, globals=0xf7d5b714, locals=0xf7d5b714) at Python/ceval.c:669
#6  0x0811e928 in run_mod (arena=0x8264f18, flags=0xffffd64c, locals=0xf7d5b714, globals=0xf7d5b714, filename=0xffffd96e "/home/minhrau/pythontestcase/test.py", mod=0x826daa0) at Python/pythonrun.c:1376
#7  PyRun_FileExFlags (fp=0x826c788, filename=0xffffd96e "/home/minhrau/pythontestcase/test.py", start=257, globals=0xf7d5b714, locals=0xf7d5b714, closeit=1, flags=0xffffd64c) at Python/pythonrun.c:1362
#8  0x081202f4 in PyRun_SimpleFileExFlags (fp=0x826c788, filename=0xffffd96e "/home/minhrau/pythontestcase/test.py", closeit=1, flags=0xffffd64c) at Python/pythonrun.c:948
#9  0x0805a37d in Py_Main (argc=2, argv=0xffffd794) at Modules/main.c:640
#10 0x080594cb in main (argc=2, argv=0xffffd794) at ./Modules/python.c:20
(gdb) 


Patch:
--------------
file: cpython-2.7/Modules/_ctypes/callproc.c
1706,1707d1705
<     if (_ctypes_conversion_errors == NULL)
<         return PyErr_NoMemory();
History
Date User Action Args
2016-09-06 02:10:46minhrausetrecipients: + minhrau
2016-09-06 02:10:46minhrausetmessageid: <1473127846.83.0.0299239583888.issue27963@psf.upfronthosting.co.za>
2016-09-06 02:10:46minhraulinkissue27963 messages
2016-09-06 02:10:46minhraucreate