Xuqing Yang writes: The LDB implementation contains two instances of the same free-before-use bug: ldb_modify_default_callback() frees ares and then reads ares->type to construct an error message. ldb_extended_default_callback() does the same thing. Both defects occur when a lower LDB module returns a non-NULL reply with ares->error == LDB_SUCCESS and a reply type that is not valid for the operation. The callbacks free the reply object before reporting the invalid type, so the subsequent field access is a heap use-after-free. The immediate impact is a process crash or other memory-safety failure; exploitability has not been established. These are two independent instances of the same coding error and can be fixed with the same change: format the error message before freeing ares, or save the reply type in a local variable before freeing it. Affected tree: Samba 4.25.0 / current master as analyzed. Affected code 1. ldb_modify_default_callback File: lib/ldb/common/ldb.c Function: ldb_modify_default_callback() Current location: approximately lines 1388–1389 default: talloc_free(ares); ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type); return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR); ares->type is read after talloc_free(ares), which makes the read a use-after-free. This callback is used by product code that builds asynchronous add, modify, delete, and rename requests, including: source4/ldap_server/ldap_backend.c source4/dsdb/samdb/ldb_modules/util.c several DSDB LDB modules that build internal requests The public convenience function ldb_modify() does not use this callback; it uses ldb_op_default_callback(), which formats the error before freeing the reply. This distinction limits the directly affected API paths but does not remove the defect from the Samba AD DC/LDAP server code. 2. ldb_extended_default_callback File: lib/ldb/common/ldb.c Function: ldb_extended_default_callback() Current location: approximately lines 1665–1666 talloc_free(ares); ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type); return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR); This has the same free-then-use defect. The callback is installed by the public ldb_extended() API and is therefore used by extended operations in the DSDB stack, including operations such as RID allocation, schema updates, and replicated-object processing. It is also reachable through LDB/Python helper paths that call ldb_extended(). Root cause The callbacks assume that an LDB module will only return reply types expected for the current operation. When that contract is violated, they attempt to report the invalid type. However, the reply object is released before its type is read: read ares->type to select the error path free ares read ares->type again while formatting the error message The expected reply-type validation must preserve the data needed for the error message until after the reply object has been released. Trigger precondition The vulnerable branch requires all of the following: an LDB request reaches one of the affected callbacks; the lower LDB module supplies a reply object; ares->error is LDB_SUCCESS; and ares->type is not an accepted reply type for that callback. An ordinary LDAP client does not directly control ares->type. The condition normally requires a faulty, corrupted, modified, or compromised LDB module. Accordingly, this report does not claim unauthenticated remote reachability, privilege escalation, or information disclosure. Impact and severity assessment The confirmed issue is a real heap use-after-free in shared Samba/LDB code. In the normal consequence of an invalid module reply, it can terminate the handling process or cause an otherwise unsafe memory access, affecting availability. Based on the currently known trigger condition, this appears to be a low-severity memory-safety/robustness issue rather than a demonstrated high-severity remotely exploitable vulnerability. The Samba security team should determine whether the defect qualifies for the security advisory process, particularly for deployments that load third-party or dynamically configured LDB modules. Recommended fix Apply the same ordering fix to both callbacks: default: - talloc_free(ares); ldb_asprintf_errstring(req->handle->ldb, "Invalid LDB reply type %d", ares->type); + talloc_free(ares); return ldb_request_done(req, LDB_ERR_OPERATIONS_ERROR); Alternatively, capture ares->type in a local integer before calling talloc_free(ares) and use the local value in the error message. The implementation of ldb_op_default_callback() in the same file already uses the safe ordering and can serve as the reference pattern. Additional cleanup observation ldb_extended_default_callback() currently returns immediately when ares->error != LDB_SUCCESS without freeing ares: if (ares->error != LDB_SUCCESS) { return ldb_request_done(req, ares->error); } This appears to be a separate reply-object leak, not part of the use-after-free described above. It may be worth correcting independently while the callback is being updated.
I think this is not a security issue because: 1. no trigger has been identified. 2. there is no gap between free and use to allow re-use. 3. the 4-byte read into a log message is not especially worrying. The only real risk is a crash if the free causes the page to be unmapped.
Created attachment 19150 [details] patch for master
There have been reports of "Invalid LDB reply type " but the string is used in a few other places. https://lists.samba.org/archive/samba/2017-November/212050.html https://lists.samba.org/archive/samba-technical/2016-July/114940.html
[Bulk message on a number of bugs being made public at once] The embargo is being removed from this bug. That is not to say that the issue is not a real bug, but some things are better dealt with in public, where the normal process is both more available to external contributors and much, much more efficient. If you have a patch for this issue, please do contribute it via our normal GitLab process: https://wiki.samba.org/index.php/Contribute#Submitting_Patches_via_GitLab All patches to Samba must follow our copyright policy: https://www.samba.org/samba/devel/copyright-policy.html Thanks to all who report issues to Samba, we value your patience as we, along with the rest of the Open Source and Free Software communities, work though the reported issues.