Skip to content

_ldap.str2dn() segfaults when called with empty string #549

Open
@tiran

Description

@tiran

Issue description:

_ldap.str2dn segfaults with a NULL pointer deref when called with an empty string as argument. ldap_bv2dn return success for an empty string and sets dn pointer to NULL. Debug builds of OpenLDAP fail with an assertion error getdn.c:710: ldap_bv2dn_x: Assertion bvin->bv_val != NULL failed.. Our code does not handle NULL dn and empty string correctly. Fix is trivial.

>>> _ldap.str2dn("")

Program received signal SIGSEGV, Segmentation fault.
l_ldap_str2dn (unused=<optimized out>, args=<optimized out>) at Modules/functions.c:118
Downloading source file /usr/src/debug/python-ldap-3.4.3-2.fc38.x86_64/Modules/functions.c
118         for (i = 0; dn[i]; i++) {                                                                                                                            
(gdb) bt
#0  l_ldap_str2dn (unused=<optimized out>, args=<optimized out>) at Modules/functions.c:118
#1  0x00007ffff7bcef08 in cfunction_call (func=0x7fffe9e2f240, args=<optimized out>, kwargs=<optimized out>)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Objects/methodobject.c:553
#2  0x00007ffff7bb2173 in _PyObject_MakeTpCall (tstate=0x7ffff7f10538 <_PyRuntime+166328>, callable=0x7fffe9e2f240, args=<optimized out>, nargs=1, 
    keywords=<optimized out>) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Objects/call.c:214
#3  0x00007ffff7bba996 in _PyEval_EvalFrameDefault (tstate=<optimized out>, frame=<optimized out>, throwflag=<optimized out>)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/ceval.c:4760
#4  0x00007ffff7bb6bba in _PyEval_EvalFrame (throwflag=0, frame=0x7ffff763d020, tstate=0x7ffff7f10538 <_PyRuntime+166328>)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Include/internal/pycore_ceval.h:73
#5  _PyEval_Vector (tstate=tstate@entry=0x7ffff7f10538 <_PyRuntime+166328>, func=func@entry=0x7fffe9f8c540, locals=locals@entry=0x7ffff773b200, 
    args=args@entry=0x0, argcount=argcount@entry=0, kwnames=kwnames@entry=0x0) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/ceval.c:6425
#6  0x00007ffff7c3b15c in PyEval_EvalCode (co=0x7fffe9d9fb40, globals=<optimized out>, locals=0x7ffff773b200)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/ceval.c:1140
#7  0x00007ffff7c58413 in run_eval_code_obj (tstate=tstate@entry=0x7ffff7f10538 <_PyRuntime+166328>, co=co@entry=0x7fffe9d9fb40, 
    globals=globals@entry=0x7ffff773b200, locals=locals@entry=0x7ffff773b200) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/pythonrun.c:1710
#8  0x00007ffff7c54a7a in run_mod (mod=mod@entry=0x55555572eba8, filename=filename@entry=0x7ffff76ec230, globals=0x7ffff773b200, locals=0x7ffff773b200, 
    flags=flags@entry=0x7fffffffd068, arena=arena@entry=0x7ffff765f990) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/pythonrun.c:1731
#9  0x00007ffff7b99732 in PyRun_InteractiveOneObjectEx (fp=fp@entry=0x7ffff79f6aa0 <_IO_2_1_stdin_>, filename=filename@entry=0x7ffff76ec230, 
    flags=flags@entry=0x7fffffffd068) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/pythonrun.c:261
#10 0x00007ffff7b998d2 in _PyRun_InteractiveLoopObject (fp=0x7ffff79f6aa0 <_IO_2_1_stdin_>, filename=0x7ffff76ec230, flags=0x7fffffffd068)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/pythonrun.c:138
#11 0x00007ffff7b2688b in _PyRun_AnyFileObject (fp=0x7ffff79f6aa0 <_IO_2_1_stdin_>, filename=0x7ffff76ec230, closeit=0, flags=0x7fffffffd068)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/pythonrun.c:73
#12 0x00007ffff7b99a4f in PyRun_AnyFileExFlags (fp=0x7ffff79f6aa0 <_IO_2_1_stdin_>, filename=<optimized out>, closeit=0, flags=0x7fffffffd068)
    at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Python/pythonrun.c:105
#13 0x00007ffff7b22793 in pymain_run_stdin (config=0x7ffff7ef6580 <_PyRuntime+59904>) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Modules/main.c:509
#14 pymain_run_python (exitcode=0x7fffffffd060) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Modules/main.c:604
#15 Py_RunMain () at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Modules/main.c:680
#16 0x00007ffff7c2b48b in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at /usr/src/debug/python3.11-3.11.6-1.fc38.x86_64/Modules/main.c:734
#17 0x00007ffff7849b8a in __libc_start_call_main (main=main@entry=0x555555555160 <main>, argc=argc@entry=1, argv=argv@entry=0x7fffffffd2c8)
    at ../sysdeps/nptl/libc_start_call_main.h:58
#18 0x00007ffff7849c4b in __libc_start_main_impl (main=0x555555555160 <main>, argc=1, argv=0x7fffffffd2c8, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=<optimized out>, stack_end=0x7fffffffd2b8) at ../csu/libc-start.c:360
#19 0x0000555555555095 in _start ()
(gdb) p dn
$1 = (LDAPDN) 0x0

Steps to reproduce:

import _ldap
_ldap.str2dn("")

Operating system:
any

Python version:
any

python-ldap version:
3.4.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions