Skip to content

Fix(LDAPObject): Prevent search attrlist memory error #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jkoeniger
Copy link

@jkoeniger jkoeniger commented Jan 7, 2024

Problem

Providing a dictionary to attrlist (LDAPObject.search_ext) results in a memory error.

See Issue #556

Root cause

Function PySequence_Length can return -1 on iterables like dict. The following PyMem_NEW still succeeds due PyMem_NEW(char *, -1 + 1) being equivalent to char** PyMem_Malloc(1), which then can result in a
segmentation fault later on.

Solution

The expected behavior should be either a raised TypeError or that any iterable which contains only strings can be passed.
Commit e18c567 enables the latter which is in itself very unlikely to break anything. I would actually prefer to raise a TypeError in the C extension on encountering a dictionary as it is most likely an error on the callers side, maybe we can add that later.

The fix in e18c567 is to use seq and PySequence_Fast_GET_SIZE to determine the size of the sequence. This way any iterable which contains only strings can be used. I added two tests in a1bdf47 which will fail without the fix in e18c567.

Add tests test_valid_attrlist_parameter_types and
test_invalid_attrlist_parameter_types which test the behaviour
when passing different Python types.

All iterables which return only strings should pass, everything else should
raise a TypeError.
@jkoeniger jkoeniger changed the title Fix(LDAPObject): ldapsearch attrlist memory error Fix(LDAPObject): Prevent search attrlist memory error Jan 7, 2024
Copy link
Member

@tiran tiran left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent finding. Thanks for the bug report and patch with tests. Much appreciated.

I suggest a slightly different fix to align the change with other upcoming changes.

Copy link
Contributor

@droideck droideck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Function `PySequence_Length` can return -1 on iterables like `dict`.
The following PyMem_NEW still succeeds due `PyMem_NEW(char *, -1 + 1)`
being equivalent to `char** PyMem_Malloc(1)`, which then can result in a
segmentation fault later on.

Solution: Use `seq` and `PySequence_Size` to determine the size of
the sequence. This way any iterable which contains only strings can be used.

Co-authored-by: Christian Heimes <christian@python.org>
@jkoeniger jkoeniger force-pushed the jkoeniger/fix-ldapsearch-attrlist-memory-error branch from d1358f6 to 0859ed8 Compare January 30, 2024 09:35
@jkoeniger
Copy link
Author

Sorry, lost track of this for a bit. I squashed the commits and adjusted the commit message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants