This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
Created on 2009年05月22日 08:18 by billm, last changed 2022年04月11日 14:56 by admin.
| Files | ||||
|---|---|---|---|---|
| File name | Uploaded | Description | Edit | |
| python-bug-01.patch | billm, 2009年05月22日 08:18 | Patch to fix the problem | ||
| test-resource.py | abacabadabacaba, 2010年11月16日 12:33 | Test for Modules/resource.c | ||
| test-ctypes.py | abacabadabacaba, 2010年11月16日 12:34 | Test for Modules/_ctypes/_ctypes.c | ||
| test-functools.py | abacabadabacaba, 2010年11月16日 12:35 | Test for Modules/_functoolsmodule.c (py3k only) | ||
| issue6083.diff | belopolsky, 2011年01月14日 06:42 | review | ||
| PyArg_ParseTuple_refcount.patch | serhiy.storchaka, 2012年10月14日 20:18 | review | ||
| Messages (25) | |||
|---|---|---|---|
| msg88181 - (view) | Author: Bill McCloskey (billm) | Date: 2009年05月22日 08:18 | |
The code for resource_setrlimit in Modules/resource.c does not handle reference counting properly. The following Python code segfaults for me on Ubuntu 8.10 in Python 2.5.2 and also a custom-built 2.6.1. -- import resource l = [0, 0] class MyNum: def __int__(self): l[1] = 20 return 10 def __del__(self): print 'byebye', self l[0] = MyNum() l[1] = MyNum() resource.setrlimit(resource.RLIMIT_CPU, l) -- The problem is that setrlimit gets its arguments by calling: PyArg_ParseTuple(args, "i(OO):setrlimit", &resource, &curobj, &maxobj) The references curobj and maxobj are borrowed. The second argument can be passed as a mutable list rather than a tuple, so it's possible to update the list in the middle of setrlimit, causing maxobj to be destroyed before setrlimit is done with it. I've attached a patch that INCREFs both variables immediately after parsing them to avoid this problem. In my opinion it seems dangerous to allow format strings with the 'O' specifier appearing in parentheses. You normally expect that objects returned from PyArg_ParseTuple are pretty safe, but the fact that the inner sequence may be mutable violates this assumption. Might it make sense to ban this use case? I only found one other instance of it in the Python source tree, inside ctypes. This one may also be a crashing bug--I didn't look at it carefully enough. |
|||
| msg88204 - (view) | Author: Georg Brandl (georg.brandl) * (Python committer) | Date: 2009年05月22日 17:09 | |
That is a good point. IMHO we'll be fine with a warning in the docs, and fixing our own two instances. Martin, what do you think? |
|||
| msg88215 - (view) | Author: Martin v. Löwis (loewis) * (Python committer) | Date: 2009年05月22日 20:14 | |
IMO, any refcounting bug has the potential as a security risk. So I think we should deprecate this with a warning, and eventually remove it, as billm proposes. It's probably debatable whether to backport the warning to 2.6 or earlier; I think we shouldn't, as many applications are probably valid. |
|||
| msg121285 - (view) | Author: Evgeny Kapun (abacabadabacaba) | Date: 2010年11月16日 12:33 | |
Actually, this can't be fixed without modifying C API methods PyArg_ParseTuple and PyArg_ParseTupleAndKeywords, because it's possible to make an object deallocated before PyArg_ParseTuple returns, so Py_INCREF immediately after parsing would be already too late. Here are my test cases: test-resource.py - in Modules/resource.c, and python-bug-01.patch won't work against it. test-ctypes.py - in Modules/_ctypes/_ctypes.c. test-functools.py - in Modules/_functoolsmodule.c (py3k only). |
|||
| msg126226 - (view) | Author: Alexander Belopolsky (belopolsky) * (Python committer) | Date: 2011年01月14日 04:55 | |
Let me summarize the issue: the PyArg_ParseTuple format code 'O' returns a borrowed reference. However, when the 'O' code appears inside parenthesis, there may not be an object to hold the reference to borrow from. This is what happens in the test-functools.py crasher: partial.__setstate__() takes a 4-tuple argument that is unpacked using a "(OOOO)" format. The test case passes an instance instead of a tuple that supports the sequence methods, but does not hold the reference to the "items" that its []-operator returns. This is not a problem at the top level because args argument to PyArg_ParseTuple is always a real tuple. I think that rather than deprecating the use of 'O' format inside parentheses, "(..O..)" unpacking should reject to unpack arguments other than tuples or maybe lists. |
|||
| msg126234 - (view) | Author: Alexander Belopolsky (belopolsky) * (Python committer) | Date: 2011年01月14日 06:42 | |
Attached patch passes the regrtest and makes test-functools.py raise an exception rather than crash. The proposed change will make functions like partial.__setstate__ require tuple argument even though currently it would accept any container. This is not an issue with __setstate__ because it should only be called with arguments produced by __reduce__ and in the case of partial, __reduce__ produces state as a tuple. Other functions may need to be modified if they need to continue to accept arbitrary sequences. |
|||
| msg172912 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2012年10月14日 20:18 | |
Here is a patch which get rid of all three PyArg_ParseTuple usage with parsing nested sequences. Thanks Evgeny for reproducers. |
|||
| msg181297 - (view) | Author: Gregory P. Smith (gregory.p.smith) * (Python committer) | Date: 2013年02月03日 20:31 | |
Serhiy's patch looks good to me. |
|||
| msg181316 - (view) | Author: Roundup Robot (python-dev) (Python triager) | Date: 2013年02月04日 11:05 | |
New changeset a4c85f9b8f58 by Serhiy Storchaka in branch '2.7': Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple http://hg.python.org/cpython/rev/a4c85f9b8f58 New changeset 4bac47eb444c by Serhiy Storchaka in branch '3.2': Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple http://hg.python.org/cpython/rev/4bac47eb444c New changeset e0ee10f27e5f by Serhiy Storchaka in branch '3.3': Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple http://hg.python.org/cpython/rev/e0ee10f27e5f New changeset 3e3a7d825736 by Serhiy Storchaka in branch 'default': Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple http://hg.python.org/cpython/rev/3e3a7d825736 |
|||
| msg181327 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2013年02月04日 13:33 | |
I do not have possibility and desires blind-repair a test on alien platform, so just temporarily disable a new test in Lib/ctypes/test/test_returnfuncptrs.py on Windows. If someone has a desire to fix it fell free to do this. I do not close this issue because committed patch only fix existing crashes in Python. There should be plenty of such bugs in third-party code. We have to deprecate this unsafe feature or reject any sequences except tuple as Alexander proposed. |
|||
| msg181592 - (view) | Author: Stefan Krah (skrah) * (Python committer) | Date: 2013年02月07日 11:41 | |
The FreeBSD 6.4 bot is failing, too. Note that the other functions in test_returnfuncptrs.py do this in order to get strchr(): dll = CDLL(_ctypes_test.__file__) get_strchr = dll.get_strchr get_strchr.restype = CFUNCTYPE(c_char_p, c_char_p, c_char) strchr = get_strchr() |
|||
| msg181600 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2013年02月07日 13:05 | |
There are 6 different ways to get a function (see comment around PyCFuncPtr_new() in Modules/_ctypes/_ctypes.c). The other tests just use other ways. I'm more carefully read ctype code and found my mistake. Need to import "my_strchr", and not "strchr". |
|||
| msg182108 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2013年02月14日 16:11 | |
FreeBSD 6.4 and Windows test failures was fixed in changesets 8fb98fb758e8 and ec70abe8c886. |
|||
| msg182110 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2013年02月14日 16:16 | |
Oh, I shouldn't close this until this dangerous feature will be deprecated. |
|||
| msg201454 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2013年10月27日 12:49 | |
Accepting an arbitrary sequence when "(...)" is used in the format string was introduced in changeset 0ef1071cb7fe. |
|||
| msg314264 - (view) | Author: Ivan Zakharyaschev (imz) | Date: 2018年03月22日 14:54 | |
> New changeset a4c85f9b8f58 by Serhiy Storchaka in branch '2.7': Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple http://hg.python.org/cpython/rev/a4c85f9b8f58 This test has a problem: though it tests not the ability to set a CPU hard limit, it fails if the hard limit is limited. Perhaps, ignore any exception there? Could you please help me re-write it correctly, so that I can run it on gyle--ALT's builder host--successfully): # Issue 6083: Reference counting bug def test_setrusage_refcount(self): try: limits = resource.getrlimit(resource.RLIMIT_CPU) except AttributeError: self.skipTest('RLIMIT_CPU not available') class BadSequence: def __len__(self): return 2 def __getitem__(self, key): if key in (0, 1): return len(tuple(range(1000000))) raise IndexError resource.setrlimit(resource.RLIMIT_CPU, BadSequence()) The failure: [builder@team ~]$ python /usr/lib64/python2.7/test/test_resource.py test_args (__main__.ResourceTest) ... ok test_fsize_enforced (__main__.ResourceTest) ... ok test_fsize_ismax (__main__.ResourceTest) ... ok test_fsize_toobig (__main__.ResourceTest) ... ok test_getrusage (__main__.ResourceTest) ... ok test_setrusage_refcount (__main__.ResourceTest) ... ERROR ====================================================================== ERROR: test_setrusage_refcount (__main__.ResourceTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/usr/lib64/python2.7/test/test_resource.py", line 117, in test_setrusage_refcount resource.setrlimit(resource.RLIMIT_CPU, BadSequence()) ValueError: not allowed to raise maximum limit ---------------------------------------------------------------------- Ran 6 tests in 0.085s FAILED (errors=1) Traceback (most recent call last): File "/usr/lib64/python2.7/test/test_resource.py", line 123, in <module> test_main() File "/usr/lib64/python2.7/test/test_resource.py", line 120, in test_main test_support.run_unittest(ResourceTest) File "/usr/lib64/python2.7/test/support/__init__.py", line 1577, in run_unittest _run_suite(suite) File "/usr/lib64/python2.7/test/support/__init__.py", line 1542, in _run_suite raise TestFailed(err) test.support.TestFailed: Traceback (most recent call last): File "/usr/lib64/python2.7/test/test_resource.py", line 117, in test_setrusage_refcount resource.setrlimit(resource.RLIMIT_CPU, BadSequence()) ValueError: not allowed to raise maximum limit [builder@team ~]$ |
|||
| msg314268 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2018年03月22日 15:59 | |
What does resource.getrlimit(resource.RLIMIT_CPU) return? |
|||
| msg314273 - (view) | Author: Ivan Zakharyaschev (imz) | Date: 2018年03月22日 16:59 | |
>>> import resource >>> resource.getrlimit(resource.RLIMIT_CPU) (7200, 7260) |
|||
| msg314285 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2018年03月22日 18:39 | |
The simplest way is to try passing the limit as a tuple resource.setrlimit(resource.RLIMIT_CPU, (1000000, 1000000)) and skip the test if it failed. |
|||
| msg314286 - (view) | Author: Ivan Zakharyaschev (imz) | Date: 2018年03月22日 18:47 | |
Thanks! I also thought about this simplest way. What about this: diff --git a/Python/Lib/test/test_resource.py b/Python/Lib/test/test_resource.py index de29d3b..bec4440 100644 --- a/Python/Lib/test/test_resource.py +++ b/Python/Lib/test/test_resource.py @@ -102,16 +102,21 @@ class ResourceTest(unittest.TestCase): # Issue 6083: Reference counting bug def test_setrusage_refcount(self): + howmany = 1000000 try: limits = resource.getrlimit(resource.RLIMIT_CPU) except AttributeError: self.skipTest('RLIMIT_CPU not available') + try: + resource.setrlimit(resource.RLIMIT_CPU, (howmany, howmany)) + except _: + self.skipTest('Setting RLIMIT_CPU not possible') class BadSequence: def __len__(self): return 2 def __getitem__(self, key): if key in (0, 1): - return len(tuple(range(1000000))) + return len(tuple(range(howmany))) raise IndexError resource.setrlimit(resource.RLIMIT_CPU, BadSequence()) What should I write instead of _? |
|||
| msg314287 - (view) | Author: Ivan Zakharyaschev (imz) | Date: 2018年03月22日 18:48 | |
And will the next call be effective (do anything), if we have already set the limit with the testing call? |
|||
| msg314289 - (view) | Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) | Date: 2018年03月22日 20:23 | |
LGTM. > What should I write instead of _? (ValueError, OSError) > And will the next call be effective (do anything), if we have already set the limit with the testing call? This doesn't matter. We test that it doesn't crash when parse arguments. |
|||
| msg322381 - (view) | Author: Tal Einat (taleinat) * (Python committer) | Date: 2018年07月25日 18:16 | |
Ivan, can you supply a PR or would you like someone else to do so? |
|||
| msg362299 - (view) | Author: Ananthakrishnan (Ananthakrishnan) * | Date: 2020年02月20日 07:20 | |
I want to do a PR,if this is still needeed. |
|||
| msg362302 - (view) | Author: Tal Einat (taleinat) * (Python committer) | Date: 2020年02月20日 07:51 | |
Please do, Ananthakrishnan! |
|||
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2022年04月11日 14:56:49 | admin | set | github: 50333 |
| 2020年02月20日 07:51:55 | taleinat | set | messages: + msg362302 |
| 2020年02月20日 07:20:48 | Ananthakrishnan | set | nosy:
+ Ananthakrishnan messages: + msg362299 |
| 2018年07月25日 18:16:01 | taleinat | set | nosy:
+ taleinat messages: + msg322381 |
| 2018年03月22日 20:23:31 | serhiy.storchaka | set | messages: + msg314289 |
| 2018年03月22日 18:48:20 | imz | set | messages: + msg314287 |
| 2018年03月22日 18:47:34 | imz | set | messages: + msg314286 |
| 2018年03月22日 18:39:18 | serhiy.storchaka | set | messages: + msg314285 |
| 2018年03月22日 16:59:08 | imz | set | messages: + msg314273 |
| 2018年03月22日 15:59:24 | serhiy.storchaka | set | messages: + msg314268 |
| 2018年03月22日 14:54:49 | imz | set | nosy:
+ imz messages: + msg314264 |
| 2016年12月11日 03:13:20 | martin.panter | set | dependencies: + resource.prlimit(int, int, str) crashs |
| 2014年10月14日 16:30:29 | skrah | set | nosy:
- skrah |
| 2014年01月25日 09:03:32 | serhiy.storchaka | set | priority: normal -> high |
| 2013年10月27日 12:49:10 | serhiy.storchaka | set | messages:
+ msg201454 versions: - Python 3.2 |
| 2013年02月14日 16:16:05 | serhiy.storchaka | set | status: closed -> open messages: + msg182110 assignee: serhiy.storchaka -> resolution: fixed -> stage: resolved -> needs patch |
| 2013年02月14日 16:11:01 | serhiy.storchaka | set | status: open -> closed messages: + msg182108 assignee: serhiy.storchaka resolution: fixed stage: needs patch -> resolved |
| 2013年02月07日 13:05:08 | serhiy.storchaka | set | messages: + msg181600 |
| 2013年02月07日 11:41:03 | skrah | set | nosy:
+ skrah messages: + msg181592 |
| 2013年02月04日 13:34:37 | serhiy.storchaka | set | assignee: serhiy.storchaka -> (no value) |
| 2013年02月04日 13:33:24 | serhiy.storchaka | set | stage: patch review -> needs patch messages: + msg181327 versions: - Python 3.1 |
| 2013年02月04日 11:05:35 | python-dev | set | nosy:
+ python-dev messages: + msg181316 |
| 2013年02月03日 20:31:35 | gregory.p.smith | set | nosy:
+ gregory.p.smith messages: + msg181297 |
| 2012年12月29日 22:19:42 | serhiy.storchaka | set | assignee: serhiy.storchaka |
| 2012年10月14日 20:19:02 | serhiy.storchaka | set | versions: + Python 3.3, Python 3.4 |
| 2012年10月14日 20:18:27 | serhiy.storchaka | set | files:
+ PyArg_ParseTuple_refcount.patch nosy: + serhiy.storchaka messages: + msg172912 |
| 2011年01月14日 06:42:07 | belopolsky | set | files:
+ issue6083.diff messages: + msg126234 nosy: loewis, georg.brandl, belopolsky, billm, abacabadabacaba stage: needs patch -> patch review |
| 2011年01月14日 04:55:18 | belopolsky | set | nosy:
+ belopolsky messages: + msg126226 stage: needs patch |
| 2010年11月16日 12:35:15 | abacabadabacaba | set | files: + test-functools.py |
| 2010年11月16日 12:34:29 | abacabadabacaba | set | files: + test-ctypes.py |
| 2010年11月16日 12:33:46 | abacabadabacaba | set | files:
+ test-resource.py title: Reference counting bug in setrlimit -> Reference counting bug in PyArg_ParseTuple and PyArg_ParseTupleAndKeywords nosy: + abacabadabacaba messages: + msg121285 components: + Interpreter Core, - Extension Modules |
| 2010年08月03日 20:53:23 | terry.reedy | set | versions: + Python 3.1, Python 2.7, Python 3.2, - Python 2.6, Python 2.5 |
| 2009年05月22日 20:14:53 | loewis | set | assignee: loewis -> (no value) |
| 2009年05月22日 20:14:45 | loewis | set | messages: + msg88215 |
| 2009年05月22日 17:09:01 | georg.brandl | set | assignee: loewis messages: + msg88204 nosy: + loewis, georg.brandl |
| 2009年05月22日 08:18:55 | billm | create | |