Skip to content

Commit 5dbaec6

Browse files
phillipwoodgitster
authored andcommitted
sequencer: rework reflog message handling
It has been reported that "git rebase --rebase-merges" can create corrupted reflog entries like e9c962f2ea0 HEAD@{8}: <binary>�: Merged in <branch> (pull request #4441) This is due to a use-after-free bug that happens because reflog_message() uses a static `struct strbuf` and is not called to update the current reflog message stored in `ctx->reflog_message` when creating the merge. This means `ctx->reflog_message` points to a stale reflog message that has been freed by subsequent call to reflog_message() by a command such as `reset` that used the return value directly rather than storing the result in `ctx->reflog_message`. Fix this by creating the reflog message nearer to where the commit is created and storing it in a local variable which is passed as an additional parameter to run_git_commit() rather than storing the message in `struct replay_ctx`. This makes it harder to forget to call `reflog_message()` before creating a commit and using a variable with a narrower scope means that a stale value cannot carried across a from one iteration of the loop to the next which should prevent any similar use-after-free bugs in the future. A existing test is modified to demonstrate that merges are now created with the correct reflog message. Reported-by: Kristoffer Haugsbakk <[email protected]> Signed-off-by: Phillip Wood <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 7472721 commit 5dbaec6

File tree

2 files changed

+34
-27
lines changed

2 files changed

+34
-27
lines changed

sequencer.c

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,6 @@ struct replay_ctx {
224224
* current chain.
225225
*/
226226
struct strbuf current_fixups;
227-
/*
228-
* Stores the reflog message that will be used when creating a
229-
* commit. Points to a static buffer and should not be free()'d.
230-
*/
231-
const char *reflog_message;
232227
/*
233228
* The number of completed fixup and squash commands in the
234229
* current chain.
@@ -1133,10 +1128,10 @@ static int run_command_silent_on_success(struct child_process *cmd)
11331128
* author metadata.
11341129
*/
11351130
static int run_git_commit(const char *defmsg,
1131+
const char *reflog_action,
11361132
struct replay_opts *opts,
11371133
unsigned int flags)
11381134
{
1139-
struct replay_ctx *ctx = opts->ctx;
11401135
struct child_process cmd = CHILD_PROCESS_INIT;
11411136

11421137
if ((flags & CLEANUP_MSG) && (flags & VERBATIM_MSG))
@@ -1154,7 +1149,7 @@ static int run_git_commit(const char *defmsg,
11541149
gpg_opt, gpg_opt);
11551150
}
11561151

1157-
strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", ctx->reflog_message);
1152+
strvec_pushf(&cmd.env, GIT_REFLOG_ACTION "=%s", reflog_action);
11581153

11591154
if (opts->committer_date_is_author_date)
11601155
strvec_pushf(&cmd.env, "GIT_COMMITTER_DATE=%s",
@@ -1538,10 +1533,10 @@ static int parse_head(struct repository *r, struct commit **head)
15381533
*/
15391534
static int try_to_commit(struct repository *r,
15401535
struct strbuf *msg, const char *author,
1536+
const char *reflog_action,
15411537
struct replay_opts *opts, unsigned int flags,
15421538
struct object_id *oid)
15431539
{
1544-
struct replay_ctx *ctx = opts->ctx;
15451540
struct object_id tree;
15461541
struct commit *current_head = NULL;
15471542
struct commit_list *parents = NULL;
@@ -1703,7 +1698,7 @@ static int try_to_commit(struct repository *r,
17031698
goto out;
17041699
}
17051700

1706-
if (update_head_with_reflog(current_head, oid, ctx->reflog_message,
1701+
if (update_head_with_reflog(current_head, oid, reflog_action,
17071702
msg, &err)) {
17081703
res = error("%s", err.buf);
17091704
goto out;
@@ -1734,6 +1729,7 @@ static int write_rebase_head(struct object_id *oid)
17341729

17351730
static int do_commit(struct repository *r,
17361731
const char *msg_file, const char *author,
1732+
const char *reflog_action,
17371733
struct replay_opts *opts, unsigned int flags,
17381734
struct object_id *oid)
17391735
{
@@ -1749,7 +1745,7 @@ static int do_commit(struct repository *r,
17491745
msg_file);
17501746

17511747
res = try_to_commit(r, msg_file ? &sb : NULL,
1752-
author, opts, flags, &oid);
1748+
author, reflog_action, opts, flags, &oid);
17531749
strbuf_release(&sb);
17541750
if (!res) {
17551751
refs_delete_ref(get_main_ref_store(r), "",
@@ -1765,7 +1761,7 @@ static int do_commit(struct repository *r,
17651761
if (is_rebase_i(opts) && oid)
17661762
if (write_rebase_head(oid))
17671763
return -1;
1768-
return run_git_commit(msg_file, opts, flags);
1764+
return run_git_commit(msg_file, reflog_action, opts, flags);
17691765
}
17701766

17711767
return res;
@@ -2278,13 +2274,19 @@ static int do_pick_commit(struct repository *r,
22782274
const char *msg_file = should_edit(opts) ? NULL : git_path_merge_msg(r);
22792275
struct object_id head;
22802276
struct commit *base, *next, *parent;
2281-
const char *base_label, *next_label;
2277+
const char *base_label, *next_label, *reflog_action;
22822278
char *author = NULL;
22832279
struct commit_message msg = { NULL, NULL, NULL, NULL };
22842280
int res, unborn = 0, reword = 0, allow, drop_commit;
22852281
enum todo_command command = item->command;
22862282
struct commit *commit = item->commit;
22872283

2284+
if (is_rebase_i(opts))
2285+
reflog_action = reflog_message(
2286+
opts, command_to_string(item->command), NULL);
2287+
else
2288+
reflog_action = sequencer_reflog_action(opts);
2289+
22882290
if (opts->no_commit) {
22892291
/*
22902292
* We do not intend to commit immediately. We just want to
@@ -2536,14 +2538,15 @@ static int do_pick_commit(struct repository *r,
25362538
} /* else allow == 0 and there's nothing special to do */
25372539
if (!opts->no_commit && !drop_commit) {
25382540
if (author || command == TODO_REVERT || (flags & AMEND_MSG))
2539-
res = do_commit(r, msg_file, author, opts, flags,
2541+
res = do_commit(r, msg_file, author, reflog_action,
2542+
opts, flags,
25402543
commit? &commit->object.oid : NULL);
25412544
else
25422545
res = error(_("unable to parse commit author"));
25432546
*check_todo = !!(flags & EDIT_MSG);
25442547
if (!res && reword) {
25452548
fast_forward_edit:
2546-
res = run_git_commit(NULL, opts, EDIT_MSG |
2549+
res = run_git_commit(NULL, reflog_action, opts, EDIT_MSG |
25472550
VERIFY_MSG | AMEND_MSG |
25482551
(flags & ALLOW_EMPTY));
25492552
*check_todo = 1;
@@ -4092,6 +4095,7 @@ static int do_merge(struct repository *r,
40924095
int merge_arg_len, oneline_offset, can_fast_forward, ret, k;
40934096
static struct lock_file lock;
40944097
const char *p;
4098+
const char *reflog_action = reflog_message(opts, "merge", NULL);
40954099

40964100
if (repo_hold_locked_index(r, &lock, LOCK_REPORT_ON_ERROR) < 0) {
40974101
ret = -1;
@@ -4370,14 +4374,15 @@ static int do_merge(struct repository *r,
43704374
* value (a negative one would indicate that the `merge`
43714375
* command needs to be rescheduled).
43724376
*/
4373-
ret = !!run_git_commit(git_path_merge_msg(r), opts,
4374-
run_commit_flags);
4377+
ret = !!run_git_commit(git_path_merge_msg(r), reflog_action,
4378+
opts, run_commit_flags);
43754379

43764380
if (!ret && flags & TODO_EDIT_MERGE_MSG) {
43774381
fast_forward_edit:
43784382
*check_todo = 1;
43794383
run_commit_flags |= AMEND_MSG | EDIT_MSG | VERIFY_MSG;
4380-
ret = !!run_git_commit(NULL, opts, run_commit_flags);
4384+
ret = !!run_git_commit(NULL, reflog_action, opts,
4385+
run_commit_flags);
43814386
}
43824387

43834388

@@ -4892,13 +4897,9 @@ static int pick_one_commit(struct repository *r,
48924897
struct replay_opts *opts,
48934898
int *check_todo, int* reschedule)
48944899
{
4895-
struct replay_ctx *ctx = opts->ctx;
48964900
int res;
48974901
struct todo_item *item = todo_list->items + todo_list->current;
48984902
const char *arg = todo_item_get_arg(todo_list, item);
4899-
if (is_rebase_i(opts))
4900-
ctx->reflog_message = reflog_message(
4901-
opts, command_to_string(item->command), NULL);
49024903

49034904
res = do_pick_commit(r, item, opts, is_final_fixup(todo_list),
49044905
check_todo);
@@ -4957,7 +4958,6 @@ static int pick_commits(struct repository *r,
49574958
struct replay_ctx *ctx = opts->ctx;
49584959
int res = 0, reschedule = 0;
49594960

4960-
ctx->reflog_message = sequencer_reflog_action(opts);
49614961
if (opts->allow_ff)
49624962
assert(!(opts->signoff || opts->no_commit ||
49634963
opts->record_origin || should_edit(opts) ||
@@ -5218,6 +5218,7 @@ static int commit_staged_changes(struct repository *r,
52185218
unsigned int flags = ALLOW_EMPTY | EDIT_MSG;
52195219
unsigned int final_fixup = 0, is_clean;
52205220
struct strbuf rev = STRBUF_INIT;
5221+
const char *reflog_action = reflog_message(opts, "continue", NULL);
52215222
int ret;
52225223

52235224
if (has_unstaged_changes(r, 1)) {
@@ -5380,7 +5381,7 @@ static int commit_staged_changes(struct repository *r,
53805381
}
53815382

53825383
if (run_git_commit(final_fixup ? NULL : rebase_path_message(),
5383-
opts, flags)) {
5384+
reflog_action, opts, flags)) {
53845385
ret = error(_("could not commit staged changes."));
53855386
goto out;
53865387
}
@@ -5412,7 +5413,6 @@ static int commit_staged_changes(struct repository *r,
54125413

54135414
int sequencer_continue(struct repository *r, struct replay_opts *opts)
54145415
{
5415-
struct replay_ctx *ctx = opts->ctx;
54165416
struct todo_list todo_list = TODO_LIST_INIT;
54175417
int res;
54185418

@@ -5433,7 +5433,6 @@ int sequencer_continue(struct repository *r, struct replay_opts *opts)
54335433
unlink(rebase_path_dropped());
54345434
}
54355435

5436-
ctx->reflog_message = reflog_message(opts, "continue", NULL);
54375436
if (commit_staged_changes(r, opts, &todo_list)) {
54385437
res = -1;
54395438
goto release_todo_list;
@@ -5485,7 +5484,6 @@ static int single_pick(struct repository *r,
54855484
TODO_PICK : TODO_REVERT;
54865485
item.commit = cmit;
54875486

5488-
opts->ctx->reflog_message = sequencer_reflog_action(opts);
54895487
return do_pick_commit(r, &item, opts, 0, &check_todo);
54905488
}
54915489

t/t3430-rebase-merges.sh

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ test_expect_success 'create completely different structure' '
8686
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
8787
test_tick &&
8888
git rebase -i -r A main &&
89-
test_cmp_graph <<-\EOF
89+
test_cmp_graph <<-\EOF &&
9090
* Merge the topic branch '\''onebranch'\''
9191
|\
9292
| * D
@@ -99,6 +99,15 @@ test_expect_success 'create completely different structure' '
9999
|/
100100
* A
101101
EOF
102+
103+
head="$(git show-ref --verify -s --abbrev HEAD)" &&
104+
cat >expect <<-EOF &&
105+
$head HEAD@{0}: rebase (finish): returning to refs/heads/main
106+
$head HEAD@{1}: rebase (merge): Merge the topic branch ${SQ}onebranch${SQ}
107+
EOF
108+
109+
git reflog -n2 HEAD >actual &&
110+
test_cmp expect actual
102111
'
103112

104113
test_expect_success 'generate correct todo list' '

0 commit comments

Comments
 (0)