Skip to content

Commit

Permalink
CVE-2021-20251 auth4: Avoid reading the database twice by precaculati…
Browse files Browse the repository at this point in the history
…ng some variables

These variables are not important to protect against a race with
and a double-read can easily be avoided by moving them up the file
a little.

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

Signed-off-by: Andrew Bartlett <[email protected]>
Reviewed-by: Joseph Sutton <[email protected]>
Reviewed-by: Andreas Schneider <[email protected]>
  • Loading branch information
abartlet committed Sep 12, 2022
1 parent 7121810 commit b5f78b7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
12 changes: 0 additions & 12 deletions selftest/knownfail.d/auth-sam

This file was deleted.

55 changes: 36 additions & 19 deletions source4/auth/sam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,8 @@ NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
struct timeval tv_now;
NTTIME now;
NTTIME lastLogonTimestamp;
int64_t lockOutObservationWindow;
NTTIME sync_interval_nt = 0;
bool am_rodc = false;
bool txn_active = false;
bool need_db_reread;
Expand Down Expand Up @@ -1436,6 +1438,36 @@ NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
return status;
}

if (interactive_or_kerberos == false) {
/*
* Avoid calculating this twice, it reads the PSO. A
* race on this is unimportant.
*/
lockOutObservationWindow
= samdb_result_msds_LockoutObservationWindow(
sam_ctx, mem_ctx, domain_dn, msg);
}

ret = samdb_rodc(sam_ctx, &am_rodc);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_INTERNAL_ERROR;
goto error;
}

if (!am_rodc) {
/*
* Avoid reading the main domain DN twice. A race on
* this is unimportant.
*/
status = authsam_calculate_lastlogon_sync_interval(
sam_ctx, mem_ctx, domain_dn, &sync_interval_nt);

if (!NT_STATUS_IS_OK(status)) {
status = NT_STATUS_INTERNAL_ERROR;
goto error;
}
}

get_transaction:

if (need_db_reread) {
Expand Down Expand Up @@ -1485,9 +1517,10 @@ NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
if (interactive_or_kerberos) {
badPwdCount = dbBadPwdCount;
} else {
int64_t lockOutObservationWindow =
samdb_result_msds_LockoutObservationWindow(
sam_ctx, mem_ctx, domain_dn, msg);
/*
* We get lockOutObservationWindow above, before the
* transaction
*/
badPwdCount = dsdb_effective_badPwdCount(
msg, lockOutObservationWindow, now);
}
Expand Down Expand Up @@ -1562,23 +1595,7 @@ NTSTATUS authsam_logon_success_accounting(struct ldb_context *sam_ctx,
}
}

ret = samdb_rodc(sam_ctx, &am_rodc);
if (ret != LDB_SUCCESS) {
status = NT_STATUS_INTERNAL_ERROR;
goto error;
}

if (!am_rodc) {
NTTIME sync_interval_nt;

status = authsam_calculate_lastlogon_sync_interval(
sam_ctx, mem_ctx, domain_dn, &sync_interval_nt);

if (!NT_STATUS_IS_OK(status)) {
status = NT_STATUS_INTERNAL_ERROR;
goto error;
}

status = authsam_update_lastlogon_timestamp(
sam_ctx,
msg_mod,
Expand Down

0 comments on commit b5f78b7

Please sign in to comment.