From 97f63f450879f89097c1ceffc506be6937b1e240 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Tue, 17 Nov 2026 07:45:49 +0100 Subject: [PATCH] mod_ldap: Fix a memory leak in util_ald_create_caches() (PR 70319) util_ald_create_caches() creates the search, compare and DN-compare caches for a URL and then inserts a node referring to them into the shared URL cache. When that insert returns NULL because the URL cache is full and the purge freed nothing (AH01323), the three freshly-created caches were never referenced anywhere and leaked. Capture the insert's result and destroy the three caches when it is NULL, same as the existing cleanup path for when one of them fails to allocate in the first place. Reproduced on a real server with an ASan build (static mod_ldap and mod_authnz_ldap, per the report): LeakSanitizer reports 13968 bytes leaked for the exact repro in the Bugzilla report; after this fix, zero leaks. Added test/modules/ldap/test_001_cache_leak.py, which measures the RSS growth of one pinned worker process over repeated alternating requests to two AuthLDAPURLs with LDAPCacheEntries 1; fails on trunk, passes with this fix. The test is skipped off Linux (it reads /proc) and under AddressSanitizer, whose own allocations dwarf the leak in RSS. PR: https://bz.apache.org/bugzilla/show_bug.cgi?id=70319 --- changes-entries/70319.txt | 4 + modules/ldap/util_ldap_cache_mgr.c | 13 +- test/modules/ldap/__init__.py | 0 test/modules/ldap/conftest.py | 35 ++++++ test/modules/ldap/env.py | 27 +++++ test/modules/ldap/test_001_cache_leak.py | 146 +++++++++++++++++++++++ 6 files changed, 224 insertions(+), 1 deletion(-) create mode 100644 changes-entries/70319.txt create mode 100644 test/modules/ldap/__init__.py create mode 100644 test/modules/ldap/conftest.py create mode 100644 test/modules/ldap/env.py create mode 100644 test/modules/ldap/test_001_cache_leak.py diff --git a/changes-entries/70319.txt b/changes-entries/70319.txt new file mode 100644 index 00000000000..72cf9521a2e --- /dev/null +++ b/changes-entries/70319.txt @@ -0,0 +1,4 @@ + *) mod_ldap: Fix a memory leak: util_ald_create_caches() leaked the + three per-URL caches it had just created whenever the shared URL + cache was full and the purge could not free an entry. PR 70319 + [Arturo Bernal] diff --git a/modules/ldap/util_ldap_cache_mgr.c b/modules/ldap/util_ldap_cache_mgr.c index 0300a8b1852..bd49eb7042a 100644 --- a/modules/ldap/util_ldap_cache_mgr.c +++ b/modules/ldap/util_ldap_cache_mgr.c @@ -313,6 +313,8 @@ util_url_node_t *util_ald_create_caches(util_ldap_state_t *st, const char *url) /* check that all the caches initialised successfully */ if (search_cache && compare_cache && dn_compare_cache) { + util_url_node_t *node; + /* The contents of this structure will be duplicated in shared memory during the insert. So use stack memory rather than pool memory to avoid a memory leak. */ @@ -322,7 +324,16 @@ util_url_node_t *util_ald_create_caches(util_ldap_state_t *st, const char *url) curl.compare_cache = compare_cache; curl.dn_compare_cache = dn_compare_cache; - return util_ald_cache_insert(st->util_ldap_cache, &curl); + node = util_ald_cache_insert(st->util_ldap_cache, &curl); + if (node == NULL) { + /* The URL cache was full and the purge freed nothing; the + * insert never happened, so these three caches are not + * referenced from anywhere and must be destroyed here. */ + util_ald_destroy_cache(search_cache); + util_ald_destroy_cache(compare_cache); + util_ald_destroy_cache(dn_compare_cache); + } + return node; } else { /* util_ald_destroy_cache is a noop for a NULL argument. */ diff --git a/test/modules/ldap/__init__.py b/test/modules/ldap/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/modules/ldap/conftest.py b/test/modules/ldap/conftest.py new file mode 100644 index 00000000000..25cac8f3170 --- /dev/null +++ b/test/modules/ldap/conftest.py @@ -0,0 +1,35 @@ +import logging +import os + +import pytest +import sys + +from .env import LdapTestEnv + +sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) + + +def pytest_report_header(config, start_path): + env = LdapTestEnv() + return f"ldap [apache: {env.get_httpd_version()}, mpm: {env.mpm_module}, {env.prefix}]" + + +@pytest.fixture(scope="package") +def env(pytestconfig) -> LdapTestEnv: + level = logging.INFO + console = logging.StreamHandler() + console.setLevel(level) + console.setFormatter(logging.Formatter('%(levelname)s: %(message)s')) + logging.getLogger('').addHandler(console) + logging.getLogger('').setLevel(level=level) + env = LdapTestEnv(pytestconfig=pytestconfig) + env.setup_httpd() + env.apache_access_log_clear() + env.httpd_error_log.clear_log() + return env + + +@pytest.fixture(autouse=True, scope="package") +def _stop_package_scope(env): + yield + assert env.apache_stop() == 0 diff --git a/test/modules/ldap/env.py b/test/modules/ldap/env.py new file mode 100644 index 00000000000..ce18f35e535 --- /dev/null +++ b/test/modules/ldap/env.py @@ -0,0 +1,27 @@ +import inspect +import logging +import os + +from pyhttpd.env import HttpdTestEnv, HttpdTestSetup + +log = logging.getLogger(__name__) + + +class LdapTestSetup(HttpdTestSetup): + + def __init__(self, env: 'HttpdTestEnv'): + super().__init__(env=env) + self.add_source_dir(os.path.dirname(inspect.getfile(LdapTestSetup))) + # mod_ldap/mod_authnz_ldap need libldap, so they may not have + # been built; the tests skip when either is absent. + self.add_optional_modules(["ldap", "authnz_ldap"]) + + +class LdapTestEnv(HttpdTestEnv): + + def __init__(self, pytestconfig=None): + super().__init__(pytestconfig=pytestconfig) + self.add_httpd_log_modules(["ldap", "authnz_ldap"]) + + def setup_httpd(self, setup: HttpdTestSetup = None): + super().setup_httpd(setup=LdapTestSetup(env=self)) diff --git a/test/modules/ldap/test_001_cache_leak.py b/test/modules/ldap/test_001_cache_leak.py new file mode 100644 index 00000000000..1a898033c8a --- /dev/null +++ b/test/modules/ldap/test_001_cache_leak.py @@ -0,0 +1,146 @@ +import http.client +import os +import sys + +import pytest + +from pyhttpd.conf import HttpdConf + +# What mod_ldap logs when the per-URL cache is full and the purge +# frees nothing; expected here on every request, since LDAPCacheEntries 1 +# makes the purge (which only removes entries older than LDAPCacheTTL) +# never effective within the life of this test. +CACHE_FULL_LOGNO = ["AH01323"] + +REQUESTS_PER_PATH = 2000 + +# A real leak here is a few KB per request (three small caches); this +# is comfortably above allocator noise for an otherwise-idle process +# and comfortably below the multi-MB growth a real leak produces over +# REQUESTS_PER_PATH iterations. +LIMIT_KB = 4096 + + +def _children_rss_kb(ppid): + """Sum VmRSS (kB) across all direct children of the httpd main + process. The main process itself does not serve requests.""" + total = 0 + for entry in os.listdir("/proc"): + if not entry.isdigit(): + continue + try: + with open(f"/proc/{entry}/stat") as fd: + fields = fd.read().split() + if int(fields[3]) != ppid: + continue + with open(f"/proc/{entry}/status") as fd: + for line in fd: + if line.startswith("VmRSS:"): + total += int(line.split()[1]) + break + except (FileNotFoundError, ProcessLookupError, IndexError, ValueError): + continue + return total + + +def _built_with_asan(env): + """Whether httpd is AddressSanitizer instrumented: its quarantine and + shadow memory grow RSS by far more than any leak this test looks for.""" + with open(os.path.join(env.bin_dir, "httpd"), "rb") as fd: + return b"__asan_init" in fd.read() + + +# Class scoped, to outlast the error log check made after each test. +@pytest.fixture(scope='class') +def _expect_cache_full(env): + env.httpd_error_log.add_ignored_lognos(CACHE_FULL_LOGNO) + yield + env.httpd_error_log.remove_ignored_lognos(CACHE_FULL_LOGNO) + + +# With LDAPCacheEntries 1, alternating requests for two AuthLDAPURLs +# always find the shared URL cache full: whichever URL was cached by +# the previous request is still there (LDAPCacheTTL has not elapsed), +# so util_ald_cache_insert() returns NULL for the other one every +# time. util_ald_create_caches() must free the three per-URL caches +# it just created in that case, not leak them. BZ 70319. +# The growth is read from /proc, hence Linux only. +@pytest.mark.skipif(not sys.platform.startswith("linux"), + reason="reads process memory from /proc") +@pytest.mark.usefixtures("_expect_cache_full") +class TestLdapCacheLeak: + + @pytest.fixture(autouse=True, scope='class') + def _class_scope(self, env): + if _built_with_asan(env): + pytest.skip("RSS growth is meaningless under AddressSanitizer") + env.require(env.has_shared_module("ldap"), "mod_ldap not built", + dep="mod_ldap") + env.require(env.has_shared_module("authnz_ldap"), "mod_authnz_ldap not built", + dep="mod_authnz_ldap") + # Pin every request to a single worker process, so the RSS growth + # measured below reflects that one process's heap and is not + # confused by the MPM scaling up its number of children under load. + mpm_pin = "StartServers 1\nServerLimit 1\nMaxRequestWorkers 16\n" + if env.mpm_module in ("mpm_worker", "mpm_event"): + mpm_pin += "ThreadsPerChild 16\nMinSpareThreads 1\nMaxSpareThreads 16\n" + conf = HttpdConf(env, extras={ + 'base': f""" + LDAPSharedCacheSize 0 + LDAPCacheEntries 1 + {mpm_pin} + """, + f"test1.{env.http_tld}": """ + + AuthType Basic + AuthName a + AuthBasicProvider ldap + AuthLDAPURL "ldap://127.0.0.1:1/dc=a?uid" + Require valid-user + + + AuthType Basic + AuthName b + AuthBasicProvider ldap + AuthLDAPURL "ldap://127.0.0.1:1/dc=b?uid" + Require valid-user + + """, + }) + conf.add_vhost_test1() + conf.install() + assert env.apache_restart() == 0 + + def test_ldap_001_01_cache_insert_failure_leak(self, env): + host = f"test1.{env.http_tld}" + conn = http.client.HTTPConnection(env.http_addr, env.http_port, timeout=10) + try: + # Warm-up: settle the connection and the allocator before + # measuring, and confirm the cache-miss path is really hit. + for path in ("a", "b", "a", "b"): + conn.request("GET", f"/ldapcacheleak/{path}", headers={ + "Host": host, "Authorization": "Basic dXNlcjpwdw==", + }) + r = conn.getresponse() + r.read() + assert r.status == 500, r.status # no real LDAP server + + main_pid = env.read_pid_file() + before = _children_rss_kb(main_pid) + + for _ in range(REQUESTS_PER_PATH): + for path in ("a", "b"): + conn.request("GET", f"/ldapcacheleak/{path}", headers={ + "Host": host, "Authorization": "Basic dXNlcjpwdw==", + }) + r = conn.getresponse() + r.read() + assert r.status == 500, r.status + + after = _children_rss_kb(main_pid) + finally: + conn.close() + + growth = after - before + assert growth < LIMIT_KB, \ + f"{2 * REQUESTS_PER_PATH} requests grew httpd by {growth}kB"