Skip to content

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.

@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.

droideck
droideck previously approved these changes Jan 17, 2024
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!

@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.

@droideck
Copy link
Contributor

droideck commented Aug 6, 2025

@jkoeniger I think it should be good to merge. Could you please rebase it?

@spaceone
Copy link
Contributor

spaceone commented Aug 7, 2025

@droideck @jkoeniger is in vacation for the next two weeks.

@spaceone
Copy link
Contributor

spaceone commented Aug 7, 2025

@tiran needs to unset the requested changes flag.

@jkoeniger jkoeniger force-pushed the jkoeniger/fix-ldapsearch-attrlist-memory-error branch 3 times, most recently from be2210b to 1e46c73 Compare September 1, 2025 04:56
@spaceone
Copy link
Contributor

spaceone commented Sep 1, 2025

@droideck Rebase done. Can be merged.

jkoeniger and others added 2 commits November 17, 2025 16:56
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.
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 1e46c73 to 450842d Compare November 17, 2025 15:56
@jkoeniger jkoeniger requested a review from droideck November 17, 2025 15:57
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.

4 participants