Skip to content

Commit 988cfcf

Browse files
pks-tgitster
authored andcommitted
refs/files: detect race when generating reflog entry for HEAD
When updating a reference that is being pointed to HEAD we don't only write a reflog message for that particular reference, but also generate one for HEAD. This logic is handled by `split_head_update()`, where we: 1. Verify that the condition actually triggered. This is done by reading HEAD at the start of the transaction so that we can then check whether a given reference update refers to its target. 2. Queue a new log-only update for HEAD in case it did. But the logic is unfortunately not free of races, as we do not lock the HEAD reference after we have read its target. This can lead to the following two scenarios: - HEAD gets concurrently updated to point to one of the references we have already processed. This causes us not writing a reflog message even though we should have done so. - HEAD gets concurrently updated to point to not point to a reference anymore that we have already processed. This causes us to write a reflog message even though we should _not_ have done so. Improve the situation by introducing a new `REF_LOG_VIA_SPLIT` flag that is specific to the "files" backend. If set, we will double check that the HEAD reference still points to the reference that we are creating the reflog entry for after we have locked HEAD. Furthermore, instead of manually resolving the old object ID of that entry, we now use the same old state as for the parent update. Unfortunately, this change only helps with the second race. We cannot reliably plug the first race without locking the HEAD reference at the start of the transaction. Locking HEAD unconditionally would effectively serialize all writes though, and that doesn't seem like an option. Also, double checking its value at the end of the transaction is not an option either, as its target may have flip-flopped during the transaction. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cddd130 commit 988cfcf

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

refs/files-backend.c

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
*/
6969
#define REF_DELETED_RMDIR (1 << 9)
7070

71+
/*
72+
* Used to indicate that the reflog-only update has been created via
73+
* `split_head_update()`.
74+
*/
75+
#define REF_LOG_VIA_SPLIT (1 << 14)
76+
7177
struct ref_lock {
7278
char *ref_name;
7379
struct lock_file lk;
@@ -2420,9 +2426,10 @@ static enum ref_transaction_error split_head_update(struct ref_update *update,
24202426

24212427
new_update = ref_transaction_add_update(
24222428
transaction, "HEAD",
2423-
update->flags | REF_LOG_ONLY | REF_NO_DEREF,
2429+
update->flags | REF_LOG_ONLY | REF_NO_DEREF | REF_LOG_VIA_SPLIT,
24242430
&update->new_oid, &update->old_oid,
24252431
NULL, NULL, update->committer_info, update->msg);
2432+
new_update->parent_update = update;
24262433

24272434
/*
24282435
* Add "HEAD". This insertion is O(N) in the transaction
@@ -2600,7 +2607,36 @@ static enum ref_transaction_error lock_ref_for_update(struct files_ref_store *re
26002607

26012608
update->backend_data = lock;
26022609

2603-
if (update->type & REF_ISSYMREF) {
2610+
if (update->flags & REF_LOG_VIA_SPLIT) {
2611+
struct ref_lock *parent_lock;
2612+
2613+
if (!update->parent_update)
2614+
BUG("split update without a parent");
2615+
2616+
parent_lock = update->parent_update->backend_data;
2617+
2618+
/*
2619+
* Check that "HEAD" didn't racily change since we have looked
2620+
* it up. If it did we must refuse to write the reflog entry.
2621+
*
2622+
* Note that this does not catch all races: if "HEAD" was
2623+
* racily changed to point to one of the refs part of the
2624+
* transaction then we would miss writing the split reflog
2625+
* entry for "HEAD".
2626+
*/
2627+
if (!(update->type & REF_ISSYMREF) ||
2628+
strcmp(update->parent_update->refname, referent.buf)) {
2629+
strbuf_addstr(err, "HEAD has been racily updated");
2630+
ret = REF_TRANSACTION_ERROR_GENERIC;
2631+
goto out;
2632+
}
2633+
2634+
if (update->flags & REF_HAVE_OLD) {
2635+
oidcpy(&lock->old_oid, &update->old_oid);
2636+
} else {
2637+
oidcpy(&lock->old_oid, &parent_lock->old_oid);
2638+
}
2639+
} else if (update->type & REF_ISSYMREF) {
26042640
if (update->flags & REF_NO_DEREF) {
26052641
/*
26062642
* We won't be reading the referent as part of

0 commit comments

Comments
 (0)