Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changes-entries/70319.txt
Original file line number Diff line number Diff line change
@@ -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]
13 changes: 12 additions & 1 deletion modules/ldap/util_ldap_cache_mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand All @@ -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. */
Expand Down
Empty file added test/modules/ldap/__init__.py
Empty file.
35 changes: 35 additions & 0 deletions test/modules/ldap/conftest.py
Original file line number Diff line number Diff line change
@@ -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
27 changes: 27 additions & 0 deletions test/modules/ldap/env.py
Original file line number Diff line number Diff line change
@@ -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))
146 changes: 146 additions & 0 deletions test/modules/ldap/test_001_cache_leak.py
Original file line number Diff line number Diff line change
@@ -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}": """
<Location /ldapcacheleak/a>
AuthType Basic
AuthName a
AuthBasicProvider ldap
AuthLDAPURL "ldap://127.0.0.1:1/dc=a?uid"
Require valid-user
</Location>
<Location /ldapcacheleak/b>
AuthType Basic
AuthName b
AuthBasicProvider ldap
AuthLDAPURL "ldap://127.0.0.1:1/dc=b?uid"
Require valid-user
</Location>
""",
})
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"
Loading