You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Detail: after Py_DECREFmodule_attr and class_attr may be released, module_name and class_name are possible freed, Causing Access released Memory/Use After Free.
Finding that module_name has been freed. In this case, I manually call gc.collect() to explain it. In real python environment, GC could free module_name at any time, Causing Use After Free Bug.
How to Fix: I think it's better to use these string before Py_DECREF:
{
PyObject*class_attr=PyObject_GetAttrString(_pymsg, "__class__");
if (class_attr) {
PyObject*name_attr=PyObject_GetAttrString(class_attr, "__name__");
if (name_attr) {
class_name= (char*)PyUnicode_1BYTE_DATA(name_attr);
}
PyObject*module_attr=PyObject_GetAttrString(class_attr, "__module__");
if (module_attr) {
module_name= (char*)PyUnicode_1BYTE_DATA(module_attr);
}
if (!class_name|| !module_name) {
return false;
}
snprintf(full_classname_dest, sizeof(full_classname_dest), "%s.%s", module_name, class_name);
Py_XDECREF(module_attr);
Py_XDECREF(name_attr);
Py_DECREF(class_attr);
}
}
Bug Type: Access released Memory/Use After Free
...
How to Fix: I think it's better to use these string before Py_DECREF:
Yes, completely agreed, that is better.
Bug Type: Non-Zero Dead Object/Memory Leak
...
Fix: I think it's better to add Py_DECREF(_pymessage) before return NULL;
That won't completely fix the issue. It will fix the issue with _pymessage itself, but it won't fix the fact that we may allocate some fields, but then fail later on. In which case, we have to drop the work we've already done. The error handling in this function needs to be significantly rethought.
Bug report
Clang Static Analyzer(CSA), pyrefcon @Snape3058 (http://lcs.ios.ac.cn/~maxt/PyRefcon/ASE-2023.pdf)
Bug Type: Access released Memory/Use After Free
File: _msg_support.c.em
Commit:
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 179 to 193 in 1fbd99b
Detail: after
Py_DECREF
module_attr
andclass_attr
may be released,module_name
andclass_name
are possible freed, Causing Access released Memory/Use After Free.Prove of Content(POC):
And this is the correct result.
However, If the garbege collect was triggered between
Py_DECREF(object)
and usage of stringmodule_name
, things will become troublesome.This is the result:
Finding that module_name has been freed. In this case, I manually call gc.collect() to explain it. In real python environment, GC could free module_name at any time, Causing Use After Free Bug.
How to Fix: I think it's better to use these string before Py_DECREF:
Bug Type: Non-Zero Dead Object/Memory Leak
File: _msg_support.c.em
Commit:
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Line 538 in 1fbd99b
Detail: If
_pymessage
has been correctly allocated, function may return NULL without freeing_pymessage
, Causing Non-Zero Dead Object/Memory Leak.Code:
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 560 to 563 in 1fbd99b
Detail: if
PyObject_GetAttrString
fail and returnNULL
, function will returnNULL
causing_pymessage
leak.Same in any code block fail and return NULL or false:
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 576 to 579 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 584 to 592 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 594 to 598 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 603 to 608 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 620 to 626 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 642 to 645 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 653 to 657 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 664 to 667 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 677 to 680 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 691 to 694 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 700 to 703 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 743 to 747 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 748 to 752 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 754 to 760 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 763 to 769 in 1fbd99b
rosidl_python/rosidl_generator_py/resource/_msg_support.c.em
Lines 795 to 799 in 1fbd99b
Fix: I think it's better to add
Py_DECREF(_pymessage)
beforereturn NULL
;The text was updated successfully, but these errors were encountered: