From e9e42c541f89924022103cc7d47cd964a2714f26 Mon Sep 17 00:00:00 2001 From: Arturo Bernal Date: Tue, 1 Dec 2026 10:01:31 +0100 Subject: [PATCH] mod_authn_socache: Make the cache key injective to keep protection spaces apart. construct_key() joined the context, user and realm with a ':' delimiter. A field that itself contains a ':' -- a client supplied Digest or form username, or a realm -- could shift a field boundary, so two distinct (user, realm) tuples produced the same cache key. A credential cached for one protection space could then be returned for a request in another, letting a request authenticate as a different user. RFC 9110 section 11.5 scopes credentials to their protection space, so the key must be injective: join each field with its byte length prefixed. * modules/aaa/mod_authn_socache.c (construct_key): Length-prefix each field when building the key. * test/modules/aaa/test_019_authn_socache_dbd_key.py: New test for the cross-realm collision, using mod_authn_dbd so the backing store keeps user and realm apart and only the cache key is under test. Backport of trunk 753762df63 (#1064). 2.4.x does not have the provider and auth type fields of the trunk key (b3e1ac2eda, 9c70606411), so only the context, user and realm are length-prefixed. PR: 70332 Reported by: saku0512sec@gmail.com (cherry picked from commit 753762df63710901318cd9da93a947392973949a) --- modules/aaa/mod_authn_socache.c | 32 +++- .../aaa/test_019_authn_socache_dbd_key.py | 172 ++++++++++++++++++ 2 files changed, 199 insertions(+), 5 deletions(-) create mode 100644 test/modules/aaa/test_019_authn_socache_dbd_key.py diff --git a/modules/aaa/mod_authn_socache.c b/modules/aaa/mod_authn_socache.c index b1f6f5a7f8e..0bc726cfcbc 100644 --- a/modules/aaa/mod_authn_socache.c +++ b/modules/aaa/mod_authn_socache.c @@ -285,11 +285,33 @@ static const char *construct_key(request_rec *r, const char *context, } /* any other context value is literal */ - if (realm == NULL) { /* basic auth */ - return apr_pstrcat(r->pool, context, ":", user, NULL); - } - else { /* digest auth */ - return apr_pstrcat(r->pool, context, ":", user, ":", realm, NULL); + /* Each field is joined with its byte length prefixed so that the + * key is injective: a field that itself contains the ':' delimiter + * (a form-submitted or Digest username, a realm, or a context) + * cannot shift a field boundary and collide with a different + * (user, realm) tuple in another protection space. + */ + { + const char *fields[3]; + struct iovec vec[6]; + apr_size_t i, n = 0; + + fields[n++] = context; + fields[n++] = user; + if (realm != NULL) { + fields[n++] = realm; + } + for (i = 0; i < n; i++) { + apr_size_t len = strlen(fields[i]); + char *prefix = apr_psprintf(r->pool, "%" APR_SIZE_T_FMT ":", + len); + + vec[2 * i].iov_base = prefix; + vec[2 * i].iov_len = strlen(prefix); + vec[2 * i + 1].iov_base = (void *)fields[i]; + vec[2 * i + 1].iov_len = len; + } + return apr_pstrcatv(r->pool, vec, 2 * n, NULL); } } diff --git a/test/modules/aaa/test_019_authn_socache_dbd_key.py b/test/modules/aaa/test_019_authn_socache_dbd_key.py new file mode 100644 index 00000000000..51db8861509 --- /dev/null +++ b/test/modules/aaa/test_019_authn_socache_dbd_key.py @@ -0,0 +1,172 @@ +"""mod_authn_socache: the cache key must keep protection spaces apart. + +construct_key() builds the socache key from the context, provider, auth +type, user and realm. When those fields are joined with a plain ':' +delimiter, a field that itself contains a ':' can shift a field boundary, +so two different (user, realm) tuples can produce the same key. With Digest +authentication the username is client-supplied and may contain a ':', so a +low-privilege account named "victim:admin" in the realm "zone" collides with +the user "victim" in the realm "admin:zone": + + :dbd:digest:victim:admin:zone (user "victim:admin", realm "zone") + :dbd:digest:victim:admin:zone (user "victim", realm "admin:zone") + +A backing provider that keeps the fields apart (mod_authn_dbd, with the user +and realm in separate columns) authenticates the two correctly, but once the +first is cached the second is answered from the colliding entry. The +attacker primes the entry from the realm it controls, then authenticates as +the target user in the target realm with the wrong password. RFC 9110 +Section 11.5 scopes credentials to their protection space (origin plus +realm), so this must not happen: the key has to be injective. +""" + +import hashlib +import os +import sqlite3 + +import pytest + +from pyhttpd.conf import HttpdConf +from . import digest_client as dc + +BODY = "authn-socache-dbd-key-secret\n" + +# A low-privilege account whose name contains the ':' delimiter, and the +# target account it must not be able to impersonate. +ATTACKER = "victim:admin" +ATTACKER_REALM = "zone" +ATTACKER_PASSWORD = "lowpass2617" + +VICTIM = "victim" +VICTIM_REALM = "admin:zone" +VICTIM_PASSWORD = "highpass2617" + +# auth_digest/authn_dbd log a password mismatch and a missing user. +AUTH_FAILURES = ["AH01790", "AH01794", "AH01760"] + + +def _ha1(user, realm, password): + return hashlib.md5(f"{user}:{realm}:{password}".encode()).hexdigest() + + +def _docdir(env, name): + return os.path.join(env.server_docs_dir, "skey", name) + + +def _make_docdir(env, name): + d = _docdir(env, name) + os.makedirs(d, exist_ok=True) + with open(os.path.join(d, "secret.txt"), 'wb') as fd: + fd.write(BODY.encode()) + return d + + +def _digest_dir(env, name, realm, require, context, dbpath): + return [ + f'', + ' AuthType Digest', + f' AuthName "{realm}"', + ' AuthDigestProvider socache dbd', + ' AuthDBDUserRealmQuery "SELECT hash FROM users ' + 'WHERE user = %s AND realm = %s"', + ' AuthnCacheProvideFor dbd', + f' AuthnCacheContext {context}', + f' {require}', + '', + ] + + +@pytest.fixture(scope='class') +def _expect_auth_failures(env): + env.httpd_error_log.add_ignored_lognos(AUTH_FAILURES) + yield + env.httpd_error_log.remove_ignored_lognos(AUTH_FAILURES) + + +@pytest.mark.usefixtures("_expect_auth_failures") +class TestAuthnSocacheDbdKey: + + @pytest.fixture(autouse=True, scope='class') + @classmethod + def _class_scope(cls, env): + for mod in ("dbd", "authn_dbd"): + env.require(env.has_shared_module(mod), f"mod_{mod} not available", + dep=f"mod_{mod}", + detected=f"no mod_{mod}.so in {env.libexec_dir}") + # 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. + driver = "sqlite3" + r = env.configtest(["Require all granted"], + extra_top_lines=[f"DBDriver {driver}"]) + env.require(r.exit_code == 0 or driver not in r.stderr, + f"no {driver} DBD driver in apr-util", + dep=f"apr_dbd_{driver}", detected=r.stderr.strip()) + assert r.exit_code == 0, r.stderr + # A credential database that, unlike a flat htdigest/htpasswd file, + # keeps user and realm in separate columns and so is not itself + # ambiguous: only the socache key is under test. + dbpath = os.path.join(env.server_dir, "authn-skey.sqlite") + if os.path.exists(dbpath): + os.remove(dbpath) + db = sqlite3.connect(dbpath) + db.execute("CREATE TABLE users (user TEXT, realm TEXT, hash TEXT)") + db.executemany("INSERT INTO users VALUES (?, ?, ?)", [ + (ATTACKER, ATTACKER_REALM, + _ha1(ATTACKER, ATTACKER_REALM, ATTACKER_PASSWORD)), + (VICTIM, VICTIM_REALM, + _ha1(VICTIM, VICTIM_REALM, VICTIM_PASSWORD)), + ]) + db.commit() + db.close() + + conf = HttpdConf(env) + conf.add(f'DBDriver sqlite3') + conf.add(f'DBDParams "{dbpath}"') + conf.add('AuthnCacheSOCache shmcb') + # Collision pair: both sides share one AuthnCacheContext. + conf.add(_digest_dir(env, "zone_a", ATTACKER_REALM, + "Require valid-user", "collide", dbpath)) + conf.add(_digest_dir(env, "admin_b", VICTIM_REALM, + f"Require user {VICTIM}", "collide", dbpath)) + # Control side in its own context, so the legitimate checks are + # independent of the collision test's cache state. + conf.add(_digest_dir(env, "ctl_b", VICTIM_REALM, + f"Require user {VICTIM}", "control", dbpath)) + conf.install() + assert env.apache_restart() == 0 + + def _digest(self, env, name, user, realm, password): + """Status of a Digest GET answering a fresh challenge.""" + uri = f"/skey/{name}/secret.txt" + url = env.mkurl("http", "aaa", uri) + r = env.curl_get(url) + assert r.response["status"] == 401 + challenge = dc.DigestChallenge.parse( + r.response["header"]["www-authenticate"]) + assert challenge.realm == realm + auth = dc.build_authorization(user, challenge, password, + method="GET", uri=uri) + r = env.curl_get(url, options=["-H", f"Authorization: {auth}"]) + assert r.exit_code == 0, r.stderr + return r.response["status"] + + def test_019_01_legitimate_user(self, env): + """The real victim authenticates in its own realm.""" + assert self._digest(env, "ctl_b", VICTIM, VICTIM_REALM, + VICTIM_PASSWORD) == 200 + + def test_019_02_wrong_password_rejected(self, env): + """The attacker's password is not the victim's password.""" + assert self._digest(env, "ctl_b", VICTIM, VICTIM_REALM, + ATTACKER_PASSWORD) == 401 + + def test_019_03_cross_realm_bypass_closed(self, env): + """Priming the attacker's colliding entry must not authenticate + the victim with the wrong password in the target realm.""" + # Prime: the attacker authenticates in the realm it controls. + assert self._digest(env, "zone_a", ATTACKER, ATTACKER_REALM, + ATTACKER_PASSWORD) == 200 + # Attack: victim / attacker's password in the target realm. + assert self._digest(env, "admin_b", VICTIM, VICTIM_REALM, + ATTACKER_PASSWORD) == 401