summaryrefslogtreecommitdiff
path: root/hook.h
diff options
context:
space:
mode:
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>2022-03-07 12:33:46 (GMT)
committerJunio C Hamano <gitster@pobox.com>2022-03-07 21:00:53 (GMT)
commita8cc594333848713b8e772cccf8159196ea85ede (patch)
treeb7854950191d4e21f90bddeb1cb54a1d0475a1c6 /hook.h
parent9f6e63b966e9876ca6f990819fabafc473a3c9b0 (diff)
downloadgit-a8cc594333848713b8e772cccf8159196ea85ede.zip
git-a8cc594333848713b8e772cccf8159196ea85ede.tar.gz
git-a8cc594333848713b8e772cccf8159196ea85ede.tar.bz2
hooks: fix an obscure TOCTOU "did we just run a hook?" race
Fix a Time-of-check to time-of-use (TOCTOU) race in code added in 680ee550d72 (commit: skip discarding the index if there is no pre-commit hook, 2017-08-14). This obscure race condition can occur if we e.g. ran the "pre-commit" hook and it modified the index, but hook_exists() returns false later on (e.g., because the hook itself went away, the directory became unreadable, etc.). Then we won't call discard_cache() when we should have. The race condition itself probably doesn't matter, and users would have been unlikely to run into it in practice. This problem has been noted on-list when 680ee550d72 was discussed[1], but had not been fixed. This change is mainly intended to improve the readability of the code involved, and to make reasoning about it more straightforward. It wasn't as obvious what we were trying to do here, but by having an "invoked_hook" it's clearer that e.g. our discard_cache() is happening because of the earlier hook execution. Let's also change this for the push-to-checkout hook. Now instead of checking if the hook exists and either doing a push to checkout or a push to deploy we'll always attempt a push to checkout. If the hook doesn't exist we'll fall back on push to deploy. The same behavior as before, without the TOCTOU race. See 0855331941b (receive-pack: support push-to-checkout hook, 2014-12-01) for the introduction of the previous behavior. This leaves uses of hook_exists() in two places that matter. The "reference-transaction" check in refs.c, see 67541597670 (refs: implement reference transaction hook, 2020-06-19), and the "prepare-commit-msg" hook, see 66618a50f9c (sequencer: run 'prepare-commit-msg' hook, 2018-01-24). In both of those cases we're saving ourselves CPU time by not preparing data for the hook that we'll then do nothing with if we don't have the hook. So using this "invoked_hook" pattern doesn't make sense in those cases. The "reference-transaction" and "prepare-commit-msg" hook also aren't racy. In those cases we'll skip the hook runs if we race with a new hook being added, whereas in the TOCTOU races being fixed here we were incorrectly skipping the required post-hook logic. 1. https://lore.kernel.org/git/20170810191613.kpmhzg4seyxy3cpq@sigill.intra.peff.net/ Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'hook.h')
-rw-r--r--hook.h12
1 files changed, 12 insertions, 0 deletions
diff --git a/hook.h b/hook.h
index 18d90ae..4258b13 100644
--- a/hook.h
+++ b/hook.h
@@ -18,6 +18,18 @@ struct run_hooks_opt
* translates to "struct child_process"'s "dir" member.
*/
const char *dir;
+
+ /**
+ * A pointer which if provided will be set to 1 or 0 depending
+ * on if a hook was started, regardless of whether or not that
+ * was successful. I.e. if the underlying start_command() was
+ * successful this will be set to 1.
+ *
+ * Used for avoiding TOCTOU races in code that would otherwise
+ * call hook_exist() after a "maybe hook run" to see if a hook
+ * was invoked.
+ */
+ int *invoked_hook;
};
#define RUN_HOOKS_OPT_INIT { \