https://bz.apache.org/bugzilla/show_bug.cgi?id=70319

            Bug ID: 70319
           Summary: mod_ldap: util_ald_create_caches() leaks the three new
                    caches when the URL cache insert fails
           Product: Apache httpd-2
           Version: 2.5-HEAD
          Hardware: PC
                OS: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: mod_ldap
          Assignee: [email protected]
          Reporter: [email protected]
  Target Milestone: ---

With "LDAPSharedCacheSize 0" and "LDAPCacheEntries 1", every request for
a second LDAP URL (while the first one is still cached) leaks the three
caches that mod_ldap creates for that URL, about 7 KB per request.


VERSION

httpd trunk 612cc170653093940c0958af1d4811b146f2221a
(Apache/2.5.1-dev), APR 1.7.5, APR-util 1.6.3 with LDAP,
Debian 13 x86_64, gcc 14.2.0.
The function is the same in the 2.4.x branch.


CAUSE

https://github.com/apache/httpd/blob/612cc170653093940c0958af1d4811b146f2221a/modules/ldap/util_ldap_cache_mgr.c#L314-L326

1. util_ald_create_caches() creates the search, compare and DN-compare
   caches for the URL (lines 289, 297 and 305). It then returns
   util_ald_cache_insert(st->util_ldap_cache, &curl) directly
   (line 325). Only the URL node stored by that insert refers to the
   new caches.

2. util_ald_cache_insert() returns NULL without storing anything when
   the URL cache is full and the purge frees nothing
   ("AH01323: Purge of LDAP cache failed"):

  
https://github.com/apache/httpd/blob/612cc170653093940c0958af1d4811b146f2221a/modules/ldap/util_ldap_cache_mgr.c#L478-L487

   With LDAPCacheEntries 1, the purge only removes URLs older than
   LDAPCacheTTL (600 s by default). A request for another URL within
   that time gets NULL here, and nothing frees the three caches.


HOW TO REPRODUCE

1. ASan build of trunk. mod_ldap and mod_authnz_ldap are linked in
   statically so that LeakSanitizer can name their frames after httpd
   exits:

    ./buildconf --with-apr=apr-1-config
    CC=gcc CFLAGS="-fsanitize=address -fno-omit-frame-pointer -g -O0" \
      LDFLAGS=-fsanitize=address \
      ./configure --with-mpm=prefork --enable-mods-static=few \
        --enable-ldap=static --enable-authnz-ldap=static
    make install

2. httpd.conf. No LDAP server is needed, because the caches are created
   before connecting:

    Listen 127.0.0.1:8080
    ServerName localhost
    ErrorLog /dev/stderr
    LDAPSharedCacheSize 0
    LDAPCacheEntries 1
    <Location /a>
      AuthType Basic
      AuthName a
      AuthBasicProvider ldap
      AuthLDAPURL "ldap://127.0.0.1:1/dc=a?uid";
      Require valid-user
    </Location>
    <Location /b>
      AuthType Basic
      AuthName b
      AuthBasicProvider ldap
      AuthLDAPURL "ldap://127.0.0.1:1/dc=b?uid";
      Require valid-user
    </Location>

3. Run in the directory that holds httpd.conf:

    httpd -X -f "$PWD/httpd.conf" & sleep 2
    for u in a b b a; do
      curl -s -o /dev/null -w "/$u/ %{http_code}\n" -u user:pw \
        http://127.0.0.1:8080/$u/
    done
    kill $!; wait


ACTUAL RESULT

The status is 500 because there is no LDAP server. The LeakSanitizer
report is trimmed and its source paths are shortened:

    /a/ 500
    [ldap:error] [pid ...] AH01323: Purge of LDAP cache failed
    /b/ 500
    [ldap:error] [pid ...] AH01323: Purge of LDAP cache failed
    /b/ 500
    /a/ 500

    ==ERROR: LeakSanitizer: detected memory leaks
    Direct leak of 352 byte(s) in 2 object(s) allocated from:
        #0 in calloc
        #1 in util_ald_create_cache util_ldap_cache_mgr.c:358
        #2 in util_ald_create_caches util_ldap_cache_mgr.c:305
        #3 in uldap_cache_checkuserid util_ldap.c:1775
        #4 in authn_ldap_check_password mod_authnz_ldap.c:614
        #5 in authenticate_basic_user mod_auth_basic.c:375
    (the same for util_ldap_cache_mgr.c:289 and :297)
    Indirect leak of 5872 byte(s) in 2 object(s) allocated from:
        #0 in calloc
        #1 in util_ald_alloc util_ldap_cache_mgr.c:110
        #2 in util_ald_create_cache util_ldap_cache_mgr.c:383
        #3 in util_ald_create_caches util_ldap_cache_mgr.c:305
        #4 in uldap_cache_checkuserid util_ldap.c:1775
    (also 5872 bytes for :297 and 1168 bytes for :289)
    SUMMARY: AddressSanitizer: 13968 byte(s) leaked in 12 allocation(s).

The 13968 bytes are the three caches (6984 bytes) for each of the two
/b/ requests.


EXPECTED RESULT

No leak. With the default LDAPCacheEntries (1024), the same requests log
no AH01323 and leak nothing.


I found this with the Infer static analyzer (Pulse), reproduced it with
LeakSanitizer, and used Claude Opus 5.5 to help read the code and write
this report.

-- 
You are receiving this mail because:
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to