From 2547844b101867e62a3501bf8e76b13a9823960b Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Mon, 9 Nov 2026 14:35:55 +0100 Subject: [PATCH] mod_authn_dbd, mod_authn_socache: Preserve extra query columns on a cache hit AuthDBDUserPWQuery and AuthDBDUserRealmQuery may select columns beyond the password, which mod_authn_dbd exports as AUTHENTICATE_. With mod_authn_socache in front of it ("socache dbd"), only the first, uncached login got them: a cache hit is answered from the cached hash alone. Add the optional function ap_authn_cache_store_env(), which takes the variables a lookup set along with the value to cache. mod_authn_socache keeps them in the same cache entry, so that they expire with it, and sets them again on a hit. mod_authn_dbd passes its extra columns through it, and does not cache a lookup with extra columns in a cache without it. An entry too long to be read back whole is not cached. PR 65172. --- changes-entries/65172.txt | 6 ++ docs/log-message-tags/next-number | 2 +- include/ap_mmn.h | 4 +- include/mod_auth.h | 15 +++ modules/aaa/mod_authn_dbd.c | 43 ++++++-- modules/aaa/mod_authn_socache.c | 104 +++++++++++++++++-- test/modules/aaa/env.py | 39 ++++++- test/modules/aaa/htdocs/dbd/cgi/whoami.py | 6 ++ test/modules/aaa/test_017_authn_dbd_cache.py | 92 ++++++++++++++++ 9 files changed, 293 insertions(+), 18 deletions(-) create mode 100644 changes-entries/65172.txt create mode 100755 test/modules/aaa/htdocs/dbd/cgi/whoami.py create mode 100644 test/modules/aaa/test_017_authn_dbd_cache.py diff --git a/changes-entries/65172.txt b/changes-entries/65172.txt new file mode 100644 index 00000000000..c6c9ddb20da --- /dev/null +++ b/changes-entries/65172.txt @@ -0,0 +1,6 @@ + *) mod_authn_dbd, mod_authn_socache: Preserve the extra AuthDBDUserPWQuery + and AuthDBDUserRealmQuery columns, exported as AUTHENTICATE_ + environment variables, on a login answered from the cache + (AuthBasicProvider/AuthDigestProvider "socache dbd"). Previously only + the first, uncached login for a user had them. PR 65172. + [Arturo Bernal] diff --git a/docs/log-message-tags/next-number b/docs/log-message-tags/next-number index 5d1df1abb36..0a29e80c5e8 100644 --- a/docs/log-message-tags/next-number +++ b/docs/log-message-tags/next-number @@ -1 +1 @@ -10687 +10703 diff --git a/include/ap_mmn.h b/include/ap_mmn.h index 3fcfbcf7308..f8046bbe8a0 100644 --- a/include/ap_mmn.h +++ b/include/ap_mmn.h @@ -744,6 +744,8 @@ * connection_added() and ap_mpm_note_extra_ * connection_removed(), and ap_mpm_wait_for_extra_ * connections(), to mpm_common.h + * 20211221.36 (2.5.1-dev) Add the optional function ap_authn_cache_store_env() + * to mod_auth.h */ #define MODULE_MAGIC_COOKIE 0x41503235UL /* "AP25" */ @@ -751,7 +753,7 @@ #ifndef MODULE_MAGIC_NUMBER_MAJOR #define MODULE_MAGIC_NUMBER_MAJOR 20211221 #endif -#define MODULE_MAGIC_NUMBER_MINOR 35 /* 0...n */ +#define MODULE_MAGIC_NUMBER_MINOR 36 /* 0...n */ /** * Determine if the server's current MODULE_MAGIC_NUMBER is at least a diff --git a/include/mod_auth.h b/include/mod_auth.h index e2b0f774616..8b7edd90bcb 100644 --- a/include/mod_auth.h +++ b/include/mod_auth.h @@ -172,6 +172,21 @@ APR_DECLARE_OPTIONAL_FN(void, ap_authn_cache_store, (request_rec*, const char*, const char*, const char*, const char*)); +/* ap_authn_cache_store_env: As ap_authn_cache_store, for a provider + * whose lookup also sets variables in r->subprocess_env. The cache keeps + * them with the value and sets them again on a cache hit, as the lookup + * itself would have done. + * @param r The request rec + * @param module Module identifier + * @param user User name to authenticate + * @param realm Digest authn realm (NULL for basic authn) + * @param data The value looked up by the authn provider, to cache + * @param env The variables set by the lookup, or NULL for none + */ +APR_DECLARE_OPTIONAL_FN(void, ap_authn_cache_store_env, + (request_rec*, const char*, const char*, + const char*, const char*, const apr_table_t*)); + #ifdef __cplusplus } #endif diff --git a/modules/aaa/mod_authn_dbd.c b/modules/aaa/mod_authn_dbd.c index 83828697ac0..a27b0634949 100644 --- a/modules/aaa/mod_authn_dbd.c +++ b/modules/aaa/mod_authn_dbd.c @@ -38,9 +38,7 @@ typedef struct { static ap_dbd_t *(*authn_dbd_acquire_fn)(request_rec*) = NULL; static void (*authn_dbd_prepare_fn)(server_rec*, const char*, const char*) = NULL; static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store) *authn_cache_store = NULL; -#define AUTHN_CACHE_STORE(r,user,realm,data) \ - if (authn_cache_store != NULL) \ - authn_cache_store((r), "dbd", (user), (realm), (data)) +static APR_OPTIONAL_FN_TYPE(ap_authn_cache_store_env) *authn_cache_store_env = NULL; static void *authn_dbd_cr_conf(apr_pool_t *pool, char *dummy) { @@ -92,11 +90,28 @@ static const command_rec authn_dbd_cmds[] = {NULL} }; +/* Cache a lookup, with the extra columns it exported as env. A cache + * which cannot keep them would answer a later login without them, so + * such a lookup is not cached there. + */ +static void authn_dbd_cache_store(request_rec *r, const char *user, + const char *realm, const char *data, + const apr_table_t *env) +{ + if (authn_cache_store_env != NULL) { + authn_cache_store_env(r, "dbd", user, realm, data, env); + } + else if (authn_cache_store != NULL && env == NULL) { + authn_cache_store(r, "dbd", user, realm, data); + } +} + static authn_status authn_dbd_password(request_rec *r, const char *user, const char *password) { apr_status_t rv; const char *dbd_password = NULL; + apr_table_t *env = NULL; apr_dbd_prepared_t *statement; apr_dbd_results_t *res = NULL; apr_dbd_row_t *row = NULL; @@ -163,7 +178,10 @@ static authn_status authn_dbd_password(request_rec *r, const char *user, } j++; } - apr_table_set(r->subprocess_env, str, + if (env == NULL) { + env = apr_table_make(r->pool, 2); + } + apr_table_set(env, str, apr_dbd_get_entry(dbd->driver, row, i)); i++; } @@ -174,10 +192,13 @@ static authn_status authn_dbd_password(request_rec *r, const char *user, /* we can't break out here or row won't get cleaned up */ } + if (env != NULL) { + apr_table_overlap(r->subprocess_env, env, APR_OVERLAP_TABLES_SET); + } if (!dbd_password) { return AUTH_USER_NOT_FOUND; } - AUTHN_CACHE_STORE(r, user, NULL, dbd_password); + authn_dbd_cache_store(r, user, NULL, dbd_password, env); rv = ap_password_validate(r, user, password, dbd_password); @@ -193,6 +214,7 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user, { apr_status_t rv; const char *dbd_hash = NULL; + apr_table_t *env = NULL; apr_dbd_prepared_t *statement; apr_dbd_results_t *res = NULL; apr_dbd_row_t *row = NULL; @@ -258,7 +280,10 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user, } j++; } - apr_table_set(r->subprocess_env, str, + if (env == NULL) { + env = apr_table_make(r->pool, 2); + } + apr_table_set(env, str, apr_dbd_get_entry(dbd->driver, row, i)); i++; } @@ -269,10 +294,13 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user, /* we can't break out here or row won't get cleaned up */ } + if (env != NULL) { + apr_table_overlap(r->subprocess_env, env, APR_OVERLAP_TABLES_SET); + } if (!dbd_hash) { return AUTH_USER_NOT_FOUND; } - AUTHN_CACHE_STORE(r, user, realm, dbd_hash); + authn_dbd_cache_store(r, user, realm, dbd_hash, env); *rethash = apr_pstrdup(r->pool, dbd_hash); return AUTH_USER_FOUND; } @@ -280,6 +308,7 @@ static authn_status authn_dbd_realm(request_rec *r, const char *user, static void opt_retr(void) { authn_cache_store = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store); + authn_cache_store_env = APR_RETRIEVE_OPTIONAL_FN(ap_authn_cache_store_env); } static void authn_dbd_hooks(apr_pool_t *p) diff --git a/modules/aaa/mod_authn_socache.c b/modules/aaa/mod_authn_socache.c index 9b183041db1..121c93e8f7f 100644 --- a/modules/aaa/mod_authn_socache.c +++ b/modules/aaa/mod_authn_socache.c @@ -333,14 +333,86 @@ static const char *construct_key(request_rec *r, const char *context, } } -static void ap_authn_cache_store(request_rec *r, const char *module, - const char *user, const char *realm, - const char* data) +/* Size of the buffer the lookups below read a cached value into, + * room for its terminating NUL included. + */ +#define MAX_VAL_LEN 256 + +/* The cached value is the provider's data followed, for each variable + * in env, by a NUL, its name, a NUL and its value. None of them can hold + * a NUL, and a value without one is the data alone. + */ +static unsigned char *cache_value(apr_pool_t *p, const char *data, + const apr_table_t *env, apr_size_t *len) +{ + const apr_array_header_t *arr; + const apr_table_entry_t *elts; + apr_size_t datalen = strlen(data); + unsigned char *val, *pos; + int i; + + if (env == NULL || apr_is_empty_table(env)) { + *len = datalen; + return (unsigned char *)data; + } + arr = apr_table_elts(env); + elts = (const apr_table_entry_t *)arr->elts; + + *len = datalen; + for (i = 0; i < arr->nelts; i++) { + *len += 1 + strlen(elts[i].key) + 1 + strlen(elts[i].val); + } + + pos = val = apr_palloc(p, *len); + memcpy(pos, data, datalen); + pos += datalen; + for (i = 0; i < arr->nelts; i++) { + apr_size_t keylen = strlen(elts[i].key); + apr_size_t vallen = strlen(elts[i].val); + + *pos++ = '\0'; + memcpy(pos, elts[i].key, keylen); + pos += keylen; + *pos++ = '\0'; + memcpy(pos, elts[i].val, vallen); + pos += vallen; + } + return val; +} + +/* Terminate the data of a cached value of vallen bytes in place, with + * val[vallen] available for that, and set the variables stored with it. + */ +static void restore_cached_env(request_rec *r, unsigned char *val, + unsigned int vallen) +{ + const char *end = (const char *)val + vallen; + const char *pos; + + val[vallen] = '\0'; + pos = (const char *)val + strlen((const char *)val); + while (pos < end) { + const char *key = pos + 1; + const char *value = key + strlen(key) + 1; + + if (value > end) { + break; + } + apr_table_set(r->subprocess_env, key, value); + pos = value + strlen(value); + } +} + +static void ap_authn_cache_store_env(request_rec *r, const char *module, + const char *user, const char *realm, + const char *data, const apr_table_t *env) { apr_status_t rv; authn_cache_dircfg *dcfg; const char *key; apr_time_t expiry; + unsigned char *val; + apr_size_t vallen; /* first check whether we're caching for this module */ dcfg = ap_get_module_config(r->per_dir_config, &authn_socache_module); @@ -351,6 +423,16 @@ static void ap_authn_cache_store(request_rec *r, const char *module, return; } + val = cache_value(r->pool, data, env, &vallen); + if (env != NULL && vallen >= MAX_VAL_LEN) { + /* Cached, it would be read back without its variables */ + ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(10701) + "authn credentials for %s not cached: %" APR_SIZE_T_FMT + " bytes with their variables, over the cache value " + "limit", user, vallen); + return; + } + /* OK, we're on. Grab mutex to do our business */ rv = apr_global_mutex_trylock(authn_cache_mutex); if (APR_STATUS_IS_EBUSY(rv)) { @@ -374,7 +456,7 @@ static void ap_authn_cache_store(request_rec *r, const char *module, /* store it */ rv = socache_provider->store(socache_instance, r->server, (unsigned char*)key, strlen(key), expiry, - (unsigned char*)data, strlen(data), r->pool); + val, vallen, r->pool); if (rv == APR_SUCCESS) { ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01681) "Cached authn credentials for %s in %s", @@ -393,7 +475,13 @@ static void ap_authn_cache_store(request_rec *r, const char *module, } } -#define MAX_VAL_LEN 256 +static void ap_authn_cache_store(request_rec *r, const char *module, + const char *user, const char *realm, + const char* data) +{ + ap_authn_cache_store_env(r, module, user, realm, data, NULL); +} + static authn_status check_password(request_rec *r, const char *user, const char *password) { @@ -444,7 +532,7 @@ static authn_status check_password(request_rec *r, const char *user, /* OK, we got a value */ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01685) "Authn cache: found credentials for %s", user); - val[vallen] = 0; + restore_cached_env(r, val, vallen); } else { /* error: give up and pass the buck */ @@ -499,6 +587,7 @@ static authn_status get_realm_hash(request_rec *r, const char *user, /* OK, we got a value */ ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(01688) "Authn cache: found credentials for %s", user); + restore_cached_env(r, val, vallen); } else { /* error: give up and pass the buck */ @@ -507,7 +596,7 @@ static authn_status get_realm_hash(request_rec *r, const char *user, "Error accessing authentication cache"); return AUTH_USER_NOT_FOUND; } - *rethash = apr_pstrmemdup(r->pool, (char *)val, vallen); + *rethash = apr_pstrdup(r->pool, (char *)val); return AUTH_USER_FOUND; } @@ -524,6 +613,7 @@ static void register_hooks(apr_pool_t *p) AUTHN_PROVIDER_VERSION, &authn_cache_provider, AP_AUTH_INTERNAL_PER_CONF); APR_REGISTER_OPTIONAL_FN(ap_authn_cache_store); + APR_REGISTER_OPTIONAL_FN(ap_authn_cache_store_env); ap_hook_pre_config(authn_cache_precfg, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_post_config(authn_cache_post_config, NULL, NULL, APR_HOOK_MIDDLE); ap_hook_child_init(authn_cache_child_init, NULL, NULL, APR_HOOK_MIDDLE); diff --git a/test/modules/aaa/env.py b/test/modules/aaa/env.py index 24fc431424e..95b0cd09639 100644 --- a/test/modules/aaa/env.py +++ b/test/modules/aaa/env.py @@ -4,6 +4,7 @@ import inspect import logging import os +import sqlite3 from typing import Dict, List, Optional from pyhttpd.env import HttpdTestEnv, HttpdTestSetup @@ -19,7 +20,9 @@ def __init__(self, env: 'HttpdTestEnv'): self.add_source_dir(os.path.dirname(inspect.getfile(AAATestSetup))) self.add_modules(["auth_digest", "authn_file", "authn_core", "authz_core", "authz_user", "proxy", "proxy_http", - "authn_socache"]) + "authn_socache", "dbd", "authn_dbd", "auth_basic", + "socache_shmcb"]) + self.add_cgi_module() class AAATestEnv(HttpdTestEnv): @@ -33,16 +36,48 @@ class AAATestEnv(HttpdTestEnv): # accounts under the same realm. DIGEST_PASSWORD_VHOST2 = "vhost2pass2617" + DBD_USER = "dbduser" + DBD_PASSWORD = "dbdpass2617" + DBD_ROLE = "editor" + # A role too long to be cached with the password hash. + DBD_LONG_USER = "dbdlonguser" + DBD_LONG_ROLE = "r" * 300 + def __init__(self, pytestconfig=None): super().__init__(pytestconfig=pytestconfig) self.add_httpd_log_modules(["auth_digest", "authn_file", "authz_core", - "authn_socache"]) + "authn_socache", "authn_dbd", "dbd"]) self._digest_pwfile = os.path.join(self.server_dir, "digest.passwd") self._digest_pwfile2 = os.path.join(self.server_dir, "digest2.passwd") + self._dbd_file = os.path.join(self.server_dir, "authn_dbd.sqlite3") def setup_httpd(self, setup: HttpdTestSetup = None): super().setup_httpd(setup=AAATestSetup(env=self)) self._write_digest_pwfile() + self._write_dbd_file() + + def _write_dbd_file(self): + if os.path.exists(self._dbd_file): + os.remove(self._dbd_file) + con = sqlite3.connect(self._dbd_file) + try: + con.execute("CREATE TABLE users (user TEXT, password TEXT, role TEXT)") + # "{SHA}" + base64(sha1(password)): the format ap_password_validate() + # checks itself, without depending on which crypt(3) algorithms the + # host libc offers. + digest = hashlib.sha1(self.DBD_PASSWORD.encode()).digest() + pw_hash = "{SHA}" + base64.b64encode(digest).decode() + for user, role in [(self.DBD_USER, self.DBD_ROLE), + (self.DBD_LONG_USER, self.DBD_LONG_ROLE)]: + con.execute("INSERT INTO users (user, password, role) " + "VALUES (?, ?, ?)", (user, pw_hash, role)) + con.commit() + finally: + con.close() + + @property + def dbd_file(self) -> str: + return self._dbd_file def _write_digest_pwfile(self): def ha1(user, password): diff --git a/test/modules/aaa/htdocs/dbd/cgi/whoami.py b/test/modules/aaa/htdocs/dbd/cgi/whoami.py new file mode 100755 index 00000000000..e337ccd5554 --- /dev/null +++ b/test/modules/aaa/htdocs/dbd/cgi/whoami.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python3 +import os + +print("Content-Type: text/plain\n") +print(f"user={os.environ.get('REMOTE_USER', '')}") +print(f"role={os.environ.get('AUTHENTICATE_ROLE', '')}") diff --git a/test/modules/aaa/test_017_authn_dbd_cache.py b/test/modules/aaa/test_017_authn_dbd_cache.py new file mode 100644 index 00000000000..70ed2e6333c --- /dev/null +++ b/test/modules/aaa/test_017_authn_dbd_cache.py @@ -0,0 +1,92 @@ +"""mod_authn_dbd's extra query columns must survive a socache cache hit. + +AuthDBDUserPWQuery may select columns beyond the password (documented +feature: modules/aaa/mod_authn_dbd.c exports each extra column as +AUTHENTICATE_ in r->subprocess_env). With AuthBasicProvider +"socache dbd", the first (uncached) login goes through +authn_dbd_password() and gets those variables; mod_authn_socache then +caches only the password hash. Every following login for the same user, +while the cache entry is still fresh, is answered by mod_authn_socache's +own check_password() directly from the cached hash -- authn_dbd_password() +is never called again, so AUTHENTICATE_* is silently absent (PR 65172). +""" + +import pytest + +from pyhttpd.conf import HttpdConf + +from .env import AAATestEnv + + +class TestAuthnDbdSocacheCache: + + @pytest.fixture(autouse=True, scope='class') + @classmethod + def _class_scope(cls, env): + # DBDriver loads the driver while reading the configuration, so an + # apr-util without the sqlite3 driver (e.g. vcpkg's on Windows) + # would fail the whole server: check it alone first. + r = env.configtest(["Require all granted"], + extra_top_lines=["DBDriver sqlite3"]) + env.require(r.exit_code == 0, "no sqlite3 DBD driver in apr-util", + dep="apr_dbd_sqlite3", detected=r.stderr.strip()) + # A Basic-auth location backed by the "socache dbd" provider pair. + conf = HttpdConf(env) + conf.add([ + 'DBDriver sqlite3', + f'DBDParams "{env.dbd_file}"', + 'AuthnCacheSOCache shmcb', + 'AddHandler cgi-script .py', + f'', + ' Options +ExecCGI', + ' AuthType Basic', + ' AuthName "AAA DBD Socache Realm"', + ' AuthBasicProvider socache dbd', + ' AuthnCacheProvideFor dbd', + ' AuthDBDUserPWQuery "SELECT password, role FROM users WHERE user = %s"', + ' AuthnCacheTimeout 300', + ' Require valid-user', + '', + ]) + with env.preserved_test_conf(): + conf.install() + assert env.apache_restart() == 0 + yield + + def url(self, env): + return env.mkurl("http", "aaa", "/dbd/cgi/whoami.py") + + def whoami(self, env, user=AAATestEnv.DBD_USER): + r = env.curl_get(self.url(env), options=[ + "-u", f"{user}:{AAATestEnv.DBD_PASSWORD}", + ]) + assert r.exit_code == 0, f"{r}" + assert r.response["status"] == 200, f"{r}" + return r.response["body"].decode() + + def test_authn_dbd_017_role_survives_cached_login(self, env): + # First login: a cache miss, goes through the real DBD query. + body1 = self.whoami(env) + assert f"user={AAATestEnv.DBD_USER}" in body1 + assert f"role={AAATestEnv.DBD_ROLE}" in body1, \ + f"AUTHENTICATE_ROLE missing on the first (uncached) login: {body1!r}" + + # Second login with the same credentials, within AuthnCacheTimeout: + # a cache hit. Unpatched, AUTHENTICATE_ROLE silently disappears here + # because authn_dbd_password() (the only code that sets it) is + # never invoked for a cache hit. + body2 = self.whoami(env) + assert f"user={AAATestEnv.DBD_USER}" in body2 + assert f"role={AAATestEnv.DBD_ROLE}" in body2, \ + f"AUTHENTICATE_ROLE lost on a cached login: {body2!r}" + + def test_authn_dbd_017_long_role_not_cached(self, env): + # The role does not fit in a cache value with the hash: the login + # must not be cached, so that every one goes through the DBD query + # and gets the role, rather than a cache hit without it. + user = AAATestEnv.DBD_LONG_USER + for _ in range(2): + body = self.whoami(env, user=user) + assert f"user={user}" in body + assert f"role={AAATestEnv.DBD_LONG_ROLE}" in body, \ + f"AUTHENTICATE_ROLE missing: {body!r}"