| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2026 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
import os |
| 10 |
import time |
| 11 |
import shutil |
| 12 |
import logging |
| 13 |
import subprocess |
| 14 |
import threading |
| 15 |
import pytest |
| 16 |
import ldap |
| 17 |
|
| 18 |
from test389.topologies import topology_m2 |
| 19 |
from lib389._mapped_object import DSLdapObject |
| 20 |
from lib389._constants import DEFAULT_SUFFIX |
| 21 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 22 |
from lib389.idm.user import UserAccounts |
| 23 |
from lib389.replica import ReplicationManager |
| 24 |
from lib389.utils import get_default_db_lib |
| 25 |
from lib389.config import BDB_LDBMConfig, LMDB_LDBMConfig |
| 26 |
|
| 27 |
DEBUGGING = os.getenv('DEBUGGING', default=False) |
| 28 |
if DEBUGGING: |
| 29 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 30 |
else: |
| 31 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 32 |
log = logging.getLogger(__name__) |
| 33 |
|
| 34 |
PLUGIN_SOURCE = r""" |
| 35 |
/* |
| 36 |
* dn-hash-corruption plugin |
| 37 |
* |
| 38 |
* For every ADD under the configured container (default ou=test), |
| 39 |
* prepends 'x' to the uid RDN value: |
| 40 |
* uid=w1u42,ou=test,dc=example,dc=com -> uid=xw1u42,ou=test,dc=example,dc=com |
| 41 |
*/ |
| 42 |
|
| 43 |
#include <stdio.h> |
| 44 |
#include <string.h> |
| 45 |
#include <strings.h> |
| 46 |
#include <stdlib.h> |
| 47 |
#include "slapi-plugin.h" |
| 48 |
|
| 49 |
#define PLUGIN_NAME "dn-hash-corruption" |
| 50 |
|
| 51 |
/* TRIGGER_PARENT_RDN is the parent RDN prefix that selects which |
| 52 |
* entries the plugin modifies. |
| 53 |
*/ |
| 54 |
#ifndef TRIGGER_PARENT_RDN |
| 55 |
#define TRIGGER_PARENT_RDN "ou=test" |
| 56 |
#endif |
| 57 |
#define TRIGGER_PARENT_RDN_LEN (sizeof(TRIGGER_PARENT_RDN) - 1) |
| 58 |
|
| 59 |
static Slapi_PluginDesc plugin_desc = { |
| 60 |
PLUGIN_NAME, "reproducer", "1.0", |
| 61 |
"Triggers entry cache DN hash corruption" |
| 62 |
}; |
| 63 |
static Slapi_ComponentId *plugin_id = NULL; |
| 64 |
|
| 65 |
/* |
| 66 |
* betxn pre-add callback. |
| 67 |
* Runs after `cache_add_tentative()` inserted the entry into `c_dntable` |
| 68 |
* under the original DN, but before `CACHE_ADD()` re-inserts it. |
| 69 |
* Changing the DN here puts the entry in two different hash slots. |
| 70 |
*/ |
| 71 |
static int dnhashcorr_betxn_pre_add(Slapi_PBlock *pb) |
| 72 |
{ |
| 73 |
Slapi_Entry *entry = NULL; |
| 74 |
const char *dn = NULL; |
| 75 |
char *parent_dn = NULL; |
| 76 |
char *old_uid = NULL; |
| 77 |
char *new_uid = NULL; |
| 78 |
char *new_dn = NULL; |
| 79 |
Slapi_DN *sdn = NULL; |
| 80 |
|
| 81 |
slapi_pblock_get(pb, SLAPI_ADD_ENTRY, &entry); |
| 82 |
if (!entry) return 0; |
| 83 |
|
| 84 |
dn = slapi_entry_get_dn_const(entry); |
| 85 |
if (!dn) return 0; |
| 86 |
|
| 87 |
/* Only target entries under the configured container */ |
| 88 |
parent_dn = slapi_dn_parent(dn); |
| 89 |
if (!parent_dn) return 0; |
| 90 |
if (strncasecmp(parent_dn, TRIGGER_PARENT_RDN, TRIGGER_PARENT_RDN_LEN) != 0) { |
| 91 |
slapi_ch_free_string(&parent_dn); |
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
/* Get the current uid attribute value */ |
| 96 |
old_uid = slapi_entry_attr_get_charptr(entry, "uid"); |
| 97 |
if (!old_uid) { |
| 98 |
slapi_ch_free_string(&parent_dn); |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
/* |
| 103 |
* Build new DN by prepending 'x' to the uid value. |
| 104 |
* This changes the NDN hash -> entry ends up in two hash slots. |
| 105 |
*/ |
| 106 |
new_dn = slapi_ch_smprintf("uid=x%s,%s", old_uid, parent_dn); |
| 107 |
slapi_ch_free_string(&parent_dn); |
| 108 |
if (!new_dn) { |
| 109 |
slapi_ch_free_string(&old_uid); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
/* |
| 114 |
* Replace the entry's SDN in-place. |
| 115 |
* The entry is still linked in c_dntable under the old NDN hash (slot A). |
| 116 |
* After this CACHE_ADD will insert it under the new NDN hash (slot B). |
| 117 |
*/ |
| 118 |
sdn = slapi_entry_get_sdn(entry); |
| 119 |
slapi_sdn_set_dn_passin(sdn, new_dn); |
| 120 |
slapi_sdn_get_ndn(sdn); |
| 121 |
|
| 122 |
/* Update uid attribute to match new RDN to avoid naming violation */ |
| 123 |
new_uid = slapi_ch_smprintf("x%s", old_uid); |
| 124 |
slapi_entry_attr_set_charptr(entry, "uid", new_uid); |
| 125 |
slapi_ch_free_string(&new_uid); |
| 126 |
slapi_ch_free_string(&old_uid); |
| 127 |
|
| 128 |
return 0; |
| 129 |
} |
| 130 |
|
| 131 |
static int dnhashcorr_start(Slapi_PBlock *pb) |
| 132 |
{ |
| 133 |
slapi_log_error(SLAPI_LOG_FATAL, PLUGIN_NAME, "DN hash corruption reproducer plugin started\n"); |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
int dnhashcorr_init(Slapi_PBlock *pb) |
| 138 |
{ |
| 139 |
int rc = 0; |
| 140 |
rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_VERSION, SLAPI_PLUGIN_VERSION_03); |
| 141 |
rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_DESCRIPTION, (void *)&plugin_desc); |
| 142 |
rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_START_FN, (void *)dnhashcorr_start); |
| 143 |
rc |= slapi_pblock_set(pb, SLAPI_PLUGIN_BE_TXN_PRE_ADD_FN, (void *)dnhashcorr_betxn_pre_add); |
| 144 |
rc |= slapi_pblock_get(pb, SLAPI_PLUGIN_IDENTITY, &plugin_id); |
| 145 |
if (rc) return -1; |
| 146 |
return 0; |
| 147 |
} |
| 148 |
""" |
| 149 |
|
| 150 |
CONTAINER_OU = 'test' |
| 151 |
WORKLOAD_DURATION = 300 |
| 152 |
NUM_SEED = 100 |
| 153 |
NUM_ADD_WORKERS = 4 |
| 154 |
NUM_SEARCH_WORKERS = 4 |
| 155 |
NUM_DELETE_WORKERS = 2 |
| 156 |
|
| 157 |
PLUGIN_DN = 'cn=DN Hash Corruption,cn=plugins,cn=config' |
| 158 |
|
| 159 |
|
| 160 |
def _compile_and_install_plugin(inst, tmp_dir): |
| 161 |
"""Compile the reproducer plugin and install it into the instance's |
| 162 |
plugin directory. |
| 163 |
""" |
| 164 |
src_file = os.path.join(tmp_dir, 'dn_hash_corruption_plugin.c') |
| 165 |
obj_file = os.path.join(tmp_dir, 'dn_hash_corruption_plugin.o') |
| 166 |
so_file = os.path.join(tmp_dir, 'libdn-hash-corruption-plugin.so') |
| 167 |
|
| 168 |
with open(src_file, 'w') as f: |
| 169 |
f.write(PLUGIN_SOURCE) |
| 170 |
|
| 171 |
cmd = ( |
| 172 |
f'/usr/bin/gcc -I/usr/include/dirsrv -I/usr/include/nspr4 ' |
| 173 |
f'-I/usr/include/nss3 -g -O2 -Wall ' |
| 174 |
f'-DTRIGGER_PARENT_RDN=\'"ou={CONTAINER_OU}"\' ' |
| 175 |
f'-c {src_file} -fPIC -DPIC -o {obj_file}' |
| 176 |
) |
| 177 |
subprocess.run(cmd, shell=True, check=True) |
| 178 |
|
| 179 |
cmd = ( |
| 180 |
f'/usr/bin/gcc -shared -fPIC -DPIC {obj_file} ' |
| 181 |
f'-Wl,-rpath -Wl,/usr/lib64/dirsrv -L/usr/lib64/dirsrv/ ' |
| 182 |
f'/usr/lib64/dirsrv/libslapd.so.0 -lldap -llber -lc ' |
| 183 |
f'-Wl,-z,now -Wl,-soname -Wl,libdn-hash-corruption-plugin.so ' |
| 184 |
f'-o {so_file}' |
| 185 |
) |
| 186 |
subprocess.run(cmd, shell=True, check=True) |
| 187 |
|
| 188 |
plugin_dir = inst.get_plugin_dir() |
| 189 |
installed_path = os.path.join(plugin_dir, |
| 190 |
'libdn-hash-corruption-plugin.so') |
| 191 |
shutil.copy(so_file, installed_path) |
| 192 |
os.chmod(installed_path, 0o755) |
| 193 |
subprocess.run(['restorecon', installed_path], check=False) |
| 194 |
|
| 195 |
|
| 196 |
def _load_plugin(inst): |
| 197 |
"""Load the DN hash corruption plugin.""" |
| 198 |
plugin = DSLdapObject(inst, PLUGIN_DN) |
| 199 |
plugin._rdn_attribute = 'cn' |
| 200 |
plugin._create_objectclasses = [ |
| 201 |
'top', |
| 202 |
'nsSlapdPlugin', |
| 203 |
'extensibleObject', |
| 204 |
] |
| 205 |
plugin._protected = False |
| 206 |
plugin.create(properties={ |
| 207 |
'cn': 'DN Hash Corruption', |
| 208 |
'nsslapd-pluginPath': 'libdn-hash-corruption-plugin', |
| 209 |
'nsslapd-pluginInitfunc': 'dnhashcorr_init', |
| 210 |
'nsslapd-pluginType': 'betxnpreoperation', |
| 211 |
'nsslapd-pluginEnabled': 'on', |
| 212 |
'nsslapd-plugin-depends-on-type': 'database', |
| 213 |
'nsslapd-pluginId': 'dn-hash-corruption', |
| 214 |
'nsslapd-pluginVersion': '1.0', |
| 215 |
'nsslapd-pluginVendor': '389 Project', |
| 216 |
'nsslapd-pluginDescription': 'Triggers entry cache DN hash corruption', |
| 217 |
}) |
| 218 |
|
| 219 |
|
| 220 |
def _shrink_entry_cache(inst): |
| 221 |
"""Shrink entry cache to create pressure.""" |
| 222 |
if get_default_db_lib() == 'bdb': |
| 223 |
config_ldbm = BDB_LDBMConfig(inst) |
| 224 |
else: |
| 225 |
config_ldbm = LMDB_LDBMConfig(inst) |
| 226 |
config_ldbm.set('nsslapd-cache-autosize', '0') |
| 227 |
|
| 228 |
inst.restart() |
| 229 |
|
| 230 |
userroot = DSLdapObject(inst, 'cn=userRoot,cn=ldbm database,cn=plugins,cn=config') |
| 231 |
userroot.replace('nsslapd-cachememsize', '512000') |
| 232 |
|
| 233 |
|
| 234 |
def _add_worker(inst, suffix, worker_id, stop_event, error_list): |
| 235 |
"""Continuously adds entries under the test container.""" |
| 236 |
users = UserAccounts(inst, suffix, rdn=f'ou={CONTAINER_OU}') |
| 237 |
n = 0 |
| 238 |
while not stop_event.is_set(): |
| 239 |
n += 1 |
| 240 |
uid = f'w{worker_id}u{n}' |
| 241 |
try: |
| 242 |
users.create(properties={ |
| 243 |
'uid': uid, |
| 244 |
'cn': uid, |
| 245 |
'sn': uid, |
| 246 |
'uidNumber': str(10000 + worker_id * 10000 + n), |
| 247 |
'gidNumber': '1000', |
| 248 |
'homeDirectory': f'/home/{uid}', |
| 249 |
}) |
| 250 |
except (ldap.ALREADY_EXISTS, ldap.LDAPError): |
| 251 |
pass |
| 252 |
except ldap.SERVER_DOWN: |
| 253 |
error_list.append(f'SERVER_DOWN during add on {inst.serverid}') |
| 254 |
return |
| 255 |
|
| 256 |
|
| 257 |
def _search_worker(inst, suffix, stop_event, error_list): |
| 258 |
"""Continuously searches entries to walk the DN hash chains.""" |
| 259 |
users = UserAccounts(inst, suffix, rdn=f'ou={CONTAINER_OU}') |
| 260 |
while not stop_event.is_set(): |
| 261 |
try: |
| 262 |
users.filter('(uid=w*)') |
| 263 |
except ldap.SERVER_DOWN: |
| 264 |
error_list.append(f'SERVER_DOWN during search on {inst.serverid}') |
| 265 |
return |
| 266 |
except ldap.LDAPError: |
| 267 |
pass |
| 268 |
|
| 269 |
try: |
| 270 |
users.filter('(uid=x*)') |
| 271 |
except ldap.SERVER_DOWN: |
| 272 |
error_list.append(f'SERVER_DOWN during search on {inst.serverid}') |
| 273 |
return |
| 274 |
except ldap.LDAPError: |
| 275 |
pass |
| 276 |
|
| 277 |
|
| 278 |
def _delete_worker(inst, suffix, stop_event, error_list): |
| 279 |
"""Finds and deletes plugin-renamed entries (uid=x*). |
| 280 |
""" |
| 281 |
users = UserAccounts(inst, suffix, rdn=f'ou={CONTAINER_OU}') |
| 282 |
while not stop_event.is_set(): |
| 283 |
try: |
| 284 |
results = users.filter('(uid=x*)') |
| 285 |
for entry in results[:50]: |
| 286 |
try: |
| 287 |
entry.delete() |
| 288 |
except (ldap.NO_SUCH_OBJECT, ldap.LDAPError): |
| 289 |
pass |
| 290 |
except ldap.SERVER_DOWN: |
| 291 |
error_list.append( |
| 292 |
f'SERVER_DOWN during delete on {inst.serverid}') |
| 293 |
return |
| 294 |
except ldap.SERVER_DOWN: |
| 295 |
error_list.append( |
| 296 |
f'SERVER_DOWN during delete-search on {inst.serverid}') |
| 297 |
return |
| 298 |
except ldap.LDAPError: |
| 299 |
pass |
| 300 |
time.sleep(0.1) |
| 301 |
|
| 302 |
|
| 303 |
@pytest.fixture(scope="function") |
| 304 |
def setup_plugin(topology_m2, request): |
| 305 |
"""Compile, install and load the reproducer plugin on both suppliers.""" |
| 306 |
S1 = topology_m2.ms["supplier1"] |
| 307 |
S2 = topology_m2.ms["supplier2"] |
| 308 |
|
| 309 |
tmp_dir = f'/tmp/dn_hash_corruption_test_{os.getpid()}' |
| 310 |
os.makedirs(tmp_dir, exist_ok=True) |
| 311 |
|
| 312 |
log.info("Compiling and installing reproducer plugin") |
| 313 |
for inst in [S1, S2]: |
| 314 |
_compile_and_install_plugin(inst, tmp_dir) |
| 315 |
|
| 316 |
log.info("Shrinking entry cache") |
| 317 |
for inst in [S1, S2]: |
| 318 |
_shrink_entry_cache(inst) |
| 319 |
|
| 320 |
log.info(f"Creating test container ou={CONTAINER_OU}") |
| 321 |
ous = OrganizationalUnits(S1, DEFAULT_SUFFIX) |
| 322 |
ous.create(properties={'ou': CONTAINER_OU}) |
| 323 |
|
| 324 |
log.info(f"Creating {NUM_SEED} seed entries") |
| 325 |
users = UserAccounts(S1, DEFAULT_SUFFIX, rdn=f'ou={CONTAINER_OU}') |
| 326 |
for i in range(1, NUM_SEED + 1): |
| 327 |
uid = f'seed{i}' |
| 328 |
users.create(properties={ |
| 329 |
'uid': uid, |
| 330 |
'cn': uid, |
| 331 |
'sn': uid, |
| 332 |
'uidNumber': str(i), |
| 333 |
'gidNumber': '1000', |
| 334 |
'homeDirectory': f'/home/{uid}', |
| 335 |
}) |
| 336 |
|
| 337 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 338 |
repl.wait_for_replication(S1, S2, timeout=300) |
| 339 |
|
| 340 |
log.info("Loading reproducer plugin and restarting") |
| 341 |
for inst in [S1, S2]: |
| 342 |
_load_plugin(inst) |
| 343 |
inst.restart() |
| 344 |
time.sleep(2) |
| 345 |
|
| 346 |
for inst in [S1, S2]: |
| 347 |
plugin = DSLdapObject(inst, PLUGIN_DN) |
| 348 |
assert plugin.exists(), f"Plugin not found on {inst.serverid}" |
| 349 |
log.info(f"Plugin loaded on {inst.serverid}") |
| 350 |
|
| 351 |
def fin(): |
| 352 |
log.info("Cleaning up plugin test...") |
| 353 |
if os.path.isdir(tmp_dir): |
| 354 |
shutil.rmtree(tmp_dir) |
| 355 |
for inst in [S1, S2]: |
| 356 |
inst.restart() |
| 357 |
|
| 358 |
request.addfinalizer(fin) |
| 359 |
return S1, S2 |
| 360 |
|
| 361 |
|
| 362 |
def test_entrycache_dn_hash_corruption(setup_plugin): |
| 363 |
"""Test that entry cache DN hash corruption via in-place DN change |
| 364 |
does not crash the server. |
| 365 |
|
| 366 |
:id: 7d1c0af5-1f0f-454a-8114-ea419a93a410 |
| 367 |
:setup: Two supplier replication topology with reproducer plugin |
| 368 |
:steps: |
| 369 |
1. Run concurrent add/search/delete workload for WORKLOAD_DURATION seconds |
| 370 |
2. Verify both servers survived without crashing |
| 371 |
:expectedresults: |
| 372 |
1. Workload runs without SERVER_DOWN errors |
| 373 |
2. Both servers are alive and responding |
| 374 |
""" |
| 375 |
S1, S2 = setup_plugin |
| 376 |
|
| 377 |
log.info(f"Starting concurrent workload for {WORKLOAD_DURATION}s") |
| 378 |
stop_event = threading.Event() |
| 379 |
errors = [] |
| 380 |
threads = [] |
| 381 |
|
| 382 |
for inst in [S1, S2]: |
| 383 |
for w in range(1, NUM_ADD_WORKERS + 1): |
| 384 |
t = threading.Thread( |
| 385 |
target=_add_worker, |
| 386 |
args=(inst, DEFAULT_SUFFIX, w, stop_event, errors), |
| 387 |
daemon=True) |
| 388 |
t.start() |
| 389 |
threads.append(t) |
| 390 |
|
| 391 |
for _ in range(NUM_SEARCH_WORKERS): |
| 392 |
t = threading.Thread( |
| 393 |
target=_search_worker, |
| 394 |
args=(inst, DEFAULT_SUFFIX, stop_event, errors), |
| 395 |
daemon=True) |
| 396 |
t.start() |
| 397 |
threads.append(t) |
| 398 |
|
| 399 |
for _ in range(NUM_DELETE_WORKERS): |
| 400 |
t = threading.Thread( |
| 401 |
target=_delete_worker, |
| 402 |
args=(inst, DEFAULT_SUFFIX, stop_event, errors), |
| 403 |
daemon=True) |
| 404 |
t.start() |
| 405 |
threads.append(t) |
| 406 |
|
| 407 |
log.info(f"Started {len(threads)} worker threads") |
| 408 |
|
| 409 |
check_interval = 5 |
| 410 |
elapsed = 0 |
| 411 |
while elapsed < WORKLOAD_DURATION: |
| 412 |
time.sleep(check_interval) |
| 413 |
elapsed += check_interval |
| 414 |
|
| 415 |
if errors: |
| 416 |
log.error(f"SERVER_DOWN detected after ~{elapsed}s: {errors}") |
| 417 |
break |
| 418 |
|
| 419 |
for inst in [S1, S2]: |
| 420 |
try: |
| 421 |
inst.rootdse.get_attr_val_utf8('supportedLDAPVersion') |
| 422 |
except ldap.SERVER_DOWN: |
| 423 |
errors.append(f'{inst.serverid} crashed after ~{elapsed}s') |
| 424 |
break |
| 425 |
except ldap.LDAPError: |
| 426 |
pass |
| 427 |
if errors: |
| 428 |
break |
| 429 |
|
| 430 |
if elapsed % 30 == 0: |
| 431 |
log.info(f" {elapsed}s elapsed - servers still running...") |
| 432 |
|
| 433 |
stop_event.set() |
| 434 |
for t in threads: |
| 435 |
t.join(timeout=10) |
| 436 |
|
| 437 |
assert not errors, (f"Server crashed during workload! Errors: {errors}") |
| 438 |
|
| 439 |
for inst in [S1, S2]: |
| 440 |
assert inst.status(), f"{inst.serverid} is not responding" |
| 441 |
log.info(f"{inst.serverid} survived the workload") |
| 442 |
|
| 443 |
log.info(f"Test PASSED: Both servers survived {WORKLOAD_DURATION}s workload") |