git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH v2 2/2] hooks: fix an obscure TOCTOU "did we just run a hook?" race

From
JTJonathan Tan <jonathantanmy@google.com>
Date
Mar 21, 2022, 20:30 UTC
Message-ID
<20220321203019.2614799-1-jonathantanmy@google.com>
In-Reply-To
<patch-v2-2.2-9d16984898c-20220307T123244Z-avarab@gmail.com>
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:
Show 25 quoted lines
> diff --git a/builtin/commit.c b/builtin/commit.c
> index 8b8bdad3959..009a1de0a3d 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -726,11 +726,13 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  	int clean_message_contents = (cleanup_mode != COMMIT_MSG_CLEANUP_NONE);
>  	int old_display_comment_prefix;
>  	int merge_contains_scissors = 0;
> +	int invoked_hook;
>  
>  	/* This checks and barfs if author is badly specified */
>  	determine_author_info(author_ident);
>  
> -	if (!no_verify && run_commit_hook(use_editor, index_file, "pre-commit", NULL))
> +	if (!no_verify && run_commit_hook(use_editor, index_file, &invoked_hook,
> +					  "pre-commit", NULL))
>  		return 0;
>  
>  	if (squash_message) {
> @@ -1053,10 +1055,10 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
>  		return 0;
>  	}
>  
> -	if (!no_verify && hook_exists("pre-commit")) {
> +	if (!no_verify && invoked_hook) {

This commit causes Git to fail Valgrind (tested using "cd t && sh t5537*.sh -i -v --valgrind-only=10"). You can see here that a caller of run_commit_hook() expects invoked_hook to be set, but...

Show 14 quoted lines
> diff --git a/commit.c b/commit.c
> index d400f5dfa2b..396e14d7b32 100644
> --- a/commit.c
> +++ b/commit.c
> @@ -1713,7 +1713,7 @@ size_t ignore_non_trailer(const char *buf, size_t len)
>  }
>  
>  int run_commit_hook(int editor_is_used, const char *index_file,
> -		    const char *name, ...)
> +		    int *invoked_hook, const char *name, ...)
>  {
>  	struct run_hooks_opt opt = RUN_HOOKS_OPT_INIT;
>  	va_list args;
> diff --git a/commit.h b/commit.h

The quoted part is the entire diff of commit.c. You can see that we have a new argument "int *invoked_hook", but don't actually do anything with it. Could you (Ævar) take a look?

Previous: Ævar Arnfjörð Bjarmason
Message 12 of 12 in “hooks: fix a race in hook execution”
  1. 0/2 hooks: fix a race in hook executionÆvar Arnfjörð Bjarmason, Feb 18, 2022
  2. 1/2 merge: don't run post-hook logic on --no-verifyÆvar Arnfjörð Bjarmason, Feb 18, 2022
  3. Junio C HamanoFeb 18, 2022
  4. 2/2 hooks: fix a TOCTOU in "did we run a hook?" heuristicÆvar Arnfjörð Bjarmason, Feb 18, 2022
  5. Junio C HamanoFeb 19, 2022
  6. Ævar Arnfjörð BjarmasonFeb 19, 2022
  7. Taylor BlauFeb 19, 2022
  8. Ævar Arnfjörð BjarmasonFeb 19, 2022
  9. 0/2 hooks: fix a race in hook executionÆvar Arnfjörð Bjarmason, Mar 7, 2022
  10. 1/2 merge: don't run post-hook logic on --no-verifyÆvar Arnfjörð Bjarmason, Mar 7, 2022
  11. 2/2 hooks: fix an obscure TOCTOU "did we just run a hook?" raceÆvar Arnfjörð Bjarmason, Mar 7, 2022
  12. Jonathan TanMar 21, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.