samba_kdc_trust_message2entry() uses password_blob.count as the loop bound for both the current and previous AuthenticationInformation arrays. It should use password_blob.current.count and password_blob.previous.count instead. When the top-level count is larger than the actual array, the loop reads past the heap allocation and the KDC worker crashes. The two loops are at source4/kdc/db-glue.c, lines ~3297 and ~3314: // Line ~3297 for (i=0; i < password_blob.count; i++) { struct AuthenticationInformation *a = &password_blob.current.array[i]; // OOB if count > current.count ... } // Line ~3314 for (i=0; i < password_blob.count; i++) { struct AuthenticationInformation *a = &password_blob.previous.array[i]; // OOB if count > previous.count ... } password_blob.count comes straight from the wire (trustAuthInOutBlob top-level field). The NDR parser derives current.count and previous.count independently by walking the subcontext -- they don't have to match. The function already uses password_blob.previous.count correctly at line ~3325, and samba_kdc_fill_trust_keys() in the same file uses aa->count. So the right pattern is already there. Second problem in the same area: the loop at ~3314 iterates over previous.array[] before the previous.count == 0 check at ~3325. A blob with count >= 1 and previous.count == 0 hits the loop, reads past the zero-length previous.array allocation. The LSA RPC path avoids this -- dcesrv_lsa.c copies current to previous when previous.count == 0 -- but blobs that enter the DB through DRS replication or direct ldb writes skip that normalization. Reproduction (Samba 4.19.5, Ubuntu 24.04 system packages, two DCs in POC.LOCAL): 1. On DC1, I created a trustedDomain object via the SamDB Python API with a trustAuthInOutBlob where count=0xFFFFFFFF and only 2 real entries in the parsed arrays. This required admin access on DC1. 2. Replicated to DC2 with samba-tool drs replicate. DRS stores trustAuth as raw octet-string bytes -- no NDR re-parsing, no count check. Replication succeeded. 3. Confirmed the blob on DC2 via ldbsearch. Same 64 bytes, count=4294967295. 4. On DC2, ran: kvno krbtgt/EVIL.LOCAL@POC.LOCAL 5. KDC workers died: prefork_child_pipe_handler: Parent 4216, Child 4225 terminated with signal 6 prefork_restart_fn: Restarting [kdc] pre-fork worker(1) prefork_child_pipe_handler: Parent 4216, Child 4221 terminated with signal 6 prefork_restart_fn: Restarting [kdc] pre-fork worker(0) The crash repeats on every cross-realm request for that trust -- the blob sits in AD, so workers keep dying until someone manually fixes or deletes the trust object. get_trustauth_inout_blob() in dcesrv_lsa.c validates count consistency on the RPC path, so normal trust creation doesn't produce this state. DRS replication skips that validation entirely. I have an ASAN standalone reproducer that confirms the heap-buffer-overflow: ==2744318==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x503000000068 READ of size 4 at 0x503000000068 thread T0 #0 in vulnerable_trust_key_selection poc_oob_standalone.c:106 0x503000000068 is located 8 bytes after 32-byte region [0x503000000040,0x503000000060) Full PoC scripts available (blob generator, planting script, two-DC reproduction, ASAN reproducer). Suggested fix: --- a/source4/kdc/db-glue.c +++ b/source4/kdc/db-glue.c @@ line 3297 - for (i=0; i < password_blob.count; i++) { + for (i=0; i < password_blob.current.count; i++) { struct AuthenticationInformation *a = &password_blob.current.array[i]; @@ line 3314 - for (i=0; i < password_blob.count; i++) { + for (i=0; i < password_blob.previous.count; i++) { struct AuthenticationInformation *a = &password_blob.previous.array[i]; Tested on Samba 4.19.5 (Ubuntu 24.04, crash confirmed) and verified the same code in master HEAD as of 2026-10-12.
Created attachment 19296 [details] Reproducer using samba-tool domain exportkeytab Reproducer using samba-tool domain exportkeytab (as suggested). Plants a trustedDomain with malformed trustAuthInOutBlob (count=0xFFFFFFFF, 2 real entries) via SamDB, then runs exportkeytab. Crashes with SIGSEGV on 4.23.6 and 4.19.5. Usage: sudo python3 reproducer_exportkeytab.py (on any provisioned DC). Cleans up after itself. Or can reproduce whith: sudo mkdir -p /tmp/samba-test sudo samba-tool domain provision \ --realm=TEST.LOCAL --domain=TEST \ --adminpass='TestPass123!' --server-role=dc \ --dns-backend=SAMBA_INTERNAL \ --targetdir=/tmp/samba-test sudo SMB_CONF_PATH=/tmp/samba-test/etc/smb.conf python3 reproducer_exportkeytab.py