s4:dsdb:acl: Fix NULL dereference of sAMAccountName in validated writes (BUG 16248)

https://bugzilla.samba.org/show_bug.cgi?id=16248

acl_check_spn() and acl_check_dns_host_name() validate a new servicePrincipalName / dNSHostName against the account's sAMAccountName, which they fetch with dsdb_msg_get_single_value(). That helper returns LDB_SUCCESS with a NULL value when

  • a modify request deletes sAMAccountName, or replaces it with no value, or
  • an add request (with AttributeAuthorizationOnLDAPAdd set in dSHeuristics) has no sAMAccountName.

Neither caller checks for NULL, so the LDAP server process crashes. The crash sites are acl_validate_spn_value() (via acl_check_spn()) and acl_check_dns_host_name(). The acl module runs before samldb and objectclass_attrs, which would otherwise reject removing the mandatory attribute. Elements of a modify are checked in client order, so the validated-write element can be placed before the unauthorised sAMAccountName element.

Only Validated Write (not Write Property) is needed. The default SD of the computer class grants that to PRINCIPAL SELF and CREATOR OWNER, so any computer account can crash an LDAP worker with one modify of its own object. Both sites came with the CVE-2022-32743 work in Samba 4.17: the dNSHostName check was introduced without a NULL check in b95431ab, and the SPN check started taking sAMAccountName from the request in e1c52ac0.

The fix keeps the stored sAMAccountName apart from the value after the request, and handles the two NULL cases differently:

  • The request removes an existing sAMAccountName (modify): return LDB_SUCCESS and let the later checks reject the illegal request. They do: the access check on the sAMAccountName element in the acl module (LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS for callers that may not write it), samldb (LDB_ERR_UNWILLING_TO_PERFORM, as Windows returns), or, for a replace followed by a delete in the same request, objectclass_attrs (LDB_ERR_UNWILLING_TO_PERFORM).
  • There never was a sAMAccountName (an add without one): reject with LDB_ERR_CONSTRAINT_VIOLATION, the code these validations already return on failure. Returning success here would not be caught later: samldb generates a random sAMAccountName for the add, and the account would be created with a dNSHostName / SPN that was never validated.

An existing object always has a sAMAccountName here: both validated-write rights apply only to computer and (group) managed service accounts (appliesTo), which must have one. Requests that carry a sAMAccountName are unaffected, as are callers with Write Property, who skip validation.

Commits:

  1. s4:dsdb:acl: Fix NULL dereference of sAMAccountName in SPN validation
  2. s4:dsdb:acl: Fix NULL dereference of sAMAccountName in dNSHostName check
  3. s4:dsdb:tests: Add tests for validated writes without sAMAccountName: 17 tests in acl.py:
    • 9 modify tests that delete or empty sAMAccountName: explicit SW grant and PRINCIPAL SELF get LDB_ERR_INSUFFICIENT_ACCESS_RIGHTS; CREATOR OWNER (also with the sAMAccountName element first, and with replace then delete) gets LDB_ERR_UNWILLING_TO_PERFORM. Each checks the account is unchanged.
    • 2 add tests without sAMAccountName: LDB_ERR_CONSTRAINT_VIOLATION, and the account is not created.
    • 6 positive controls: the same validated writes by the creator, the computer itself and on add with a matching sAMAccountName still succeed.

The tests come after the fixes rather than as a knownfail: before the fix, on the prefork schema_dc env, the crashes take down all four LDAP workers, and unrelated tests that follow (test_modify_u1..u7, test_modify_spn_matching_dns_host_name_*) then error while the workers back off.

Tested with make test TESTS=samba4.ldap.acl.python (fl2000dc, fl2003dc, fl2008dc, fl2008r2dc, schema_dc):

without the fix this MR unconditional LDB_SUCCESS
9 modify tests removing sAMAccountName LDAP worker crash pass pass
2 add tests without sAMAccountName LDAP worker crash pass fail: account created
6 positive controls pass pass pass

Checklist

  • Commits have BUG: and Signed-off-by:
  • Tests added
  • CI pipeline passes

🤖 Generated with Claude Code

Edited by Pavel Kopylov

Merge request reports

Loading
Loading