Skip to content

Commit 38ce9c2

Browse files
committed
backport the security fix part of r67246
1 parent 3a87f93 commit 38ce9c2

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Lib/test/test_descr.py

+19
Original file line numberDiff line numberDiff line change
@@ -4087,6 +4087,24 @@ def check(expr, x, y):
40874087
check(iexpr, c, N1)
40884088
check(iexpr, c, N2)
40894089

4090+
def test_lost_getattr():
4091+
# issue 4230
4092+
import gc
4093+
class EvilGetattribute(object):
4094+
def __getattr__(self, name):
4095+
raise AttributeError(name)
4096+
def __getattribute__(self, name):
4097+
del EvilGetattribute.__getattr__
4098+
for i in range(5):
4099+
gc.collect()
4100+
raise AttributeError(name)
4101+
4102+
try:
4103+
# This used to segfault
4104+
EvilGetattribute().attr
4105+
except AttributeError:
4106+
pass
4107+
40904108
def test_main():
40914109
weakref_segfault() # Must be first, somehow
40924110
wrapper_segfault()
@@ -4183,6 +4201,7 @@ def test_main():
41834201
vicious_descriptor_nonsense()
41844202
test_init()
41854203
notimplemented()
4204+
test_lost_getattr()
41864205

41874206
if verbose: print "All OK"
41884207

Misc/NEWS

+3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.4.6c1?
1212
Core and builtins
1313
-----------------
1414

15+
- Issue #4230: Fix a crash when a class has a custom __getattr__ and an
16+
__getattribute__ method that deletes the __getattr__ attribute.
17+
1518
- Apply security patches from Apple. CVE-2008-2315.
1619

1720
- Issue #2620: Overflow checking when allocating or reallocating memory

Objects/typeobject.c

+2
Original file line numberDiff line numberDiff line change
@@ -4594,6 +4594,7 @@ slot_tp_getattr_hook(PyObject *self, PyObject *name)
45944594
tp->tp_getattro = slot_tp_getattro;
45954595
return slot_tp_getattro(self, name);
45964596
}
4597+
Py_INCREF(getattr);
45974598
getattribute = _PyType_Lookup(tp, getattribute_str);
45984599
if (getattribute == NULL ||
45994600
(getattribute->ob_type == &PyWrapperDescr_Type &&
@@ -4606,6 +4607,7 @@ slot_tp_getattr_hook(PyObject *self, PyObject *name)
46064607
PyErr_Clear();
46074608
res = PyObject_CallFunction(getattr, "OO", self, name);
46084609
}
4610+
Py_DECREF(getattr);
46094611
return res;
46104612
}
46114613

0 commit comments

Comments
 (0)