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>
8 files changed
tree: b7854950191d4e21f90bddeb1cb54a1d0475a1c6
  1. .github/
  2. block-sha1/
  3. builtin/
  4. ci/
  5. compat/
  6. contrib/
  7. Documentation/
  8. ewah/
  9. git-gui/
  10. gitk-git/
  11. gitweb/
  12. mergetools/
  13. negotiator/
  14. perl/
  15. po/
  16. ppc/
  17. refs/
  18. reftable/
  19. sha1dc/
  20. sha256/
  21. t/
  22. templates/
  23. trace2/
  24. xdiff/
  25. .cirrus.yml
  26. .clang-format
  27. .editorconfig
  28. .gitattributes
  29. .gitignore
  30. .gitmodules
  31. .mailmap
  32. .tsan-suppressions
  33. abspath.c
  34. aclocal.m4
  35. add-interactive.c
  36. add-interactive.h
  37. add-patch.c
  38. advice.c
  39. advice.h
  40. alias.c
  41. alias.h
  42. alloc.c
  43. alloc.h
  44. apply.c
  45. apply.h
  46. archive-tar.c
  47. archive-zip.c
  48. archive.c
  49. archive.h
  50. attr.c
  51. attr.h
  52. banned.h
  53. base85.c
  54. bisect.c
  55. bisect.h
  56. blame.c
  57. blame.h
  58. blob.c
  59. blob.h
  60. bloom.c
  61. bloom.h
  62. branch.c
  63. branch.h
  64. builtin.h
  65. bulk-checkin.c
  66. bulk-checkin.h
  67. bundle.c
  68. bundle.h
  69. cache-tree.c
  70. cache-tree.h
  71. cache.h
  72. cbtree.c
  73. cbtree.h
  74. chdir-notify.c
  75. chdir-notify.h
  76. check-builtins.sh
  77. checkout.c
  78. checkout.h
  79. chunk-format.c
  80. chunk-format.h
  81. CODE_OF_CONDUCT.md
  82. color.c
  83. color.h
  84. column.c
  85. column.h
  86. combine-diff.c
  87. command-list.txt
  88. commit-graph.c
  89. commit-graph.h
  90. commit-reach.c
  91. commit-reach.h
  92. commit-slab-decl.h
  93. commit-slab-impl.h
  94. commit-slab.h
  95. commit.c
  96. commit.h
  97. common-main.c
  98. config.c
  99. config.h
  100. config.mak.dev
  101. config.mak.in
  102. config.mak.uname
  103. configure.ac
  104. connect.c
  105. connect.h
  106. connected.c
  107. connected.h
  108. convert.c
  109. convert.h
  110. copy.c
  111. COPYING
  112. credential.c
  113. credential.h
  114. csum-file.c
  115. csum-file.h
  116. ctype.c
  117. daemon.c
  118. date.c
  119. date.h
  120. decorate.c
  121. decorate.h
  122. delta-islands.c
  123. delta-islands.h
  124. delta.h
  125. detect-compiler
  126. diff-delta.c
  127. diff-lib.c
  128. diff-merges.c
  129. diff-merges.h
  130. diff-no-index.c
  131. diff.c
  132. diff.h
  133. diffcore-break.c
  134. diffcore-delta.c
  135. diffcore-order.c
  136. diffcore-pickaxe.c
  137. diffcore-rename.c
  138. diffcore-rotate.c
  139. diffcore.h
  140. dir-iterator.c
  141. dir-iterator.h
  142. dir.c
  143. dir.h
  144. editor.c
  145. entry.c
  146. entry.h
  147. environment.c
  148. environment.h
  149. exec-cmd.c
  150. exec-cmd.h
  151. fetch-negotiator.c
  152. fetch-negotiator.h
  153. fetch-pack.c
  154. fetch-pack.h
  155. fmt-merge-msg.c
  156. fmt-merge-msg.h
  157. fsck.c
  158. fsck.h
  159. fsmonitor.c
  160. fsmonitor.h
  161. fuzz-commit-graph.c
  162. fuzz-pack-headers.c
  163. fuzz-pack-idx.c
  164. generate-cmdlist.sh
  165. generate-configlist.sh
  166. generate-hooklist.sh
  167. gettext.c
  168. gettext.h
  169. git-add--interactive.perl
  170. git-archimport.perl
  171. git-bisect.sh
  172. git-compat-util.h
  173. git-curl-compat.h
  174. git-cvsexportcommit.perl
  175. git-cvsimport.perl
  176. git-cvsserver.perl
  177. git-difftool--helper.sh
  178. git-filter-branch.sh
  179. git-instaweb.sh
  180. git-merge-octopus.sh
  181. git-merge-one-file.sh
  182. git-merge-resolve.sh
  183. git-mergetool--lib.sh
  184. git-mergetool.sh
  185. git-p4.py
  186. git-quiltimport.sh
  187. git-request-pull.sh
  188. git-send-email.perl
  189. git-sh-i18n.sh
  190. git-sh-setup.sh
  191. git-submodule.sh
  192. git-svn.perl
  193. GIT-VERSION-GEN
  194. git-web--browse.sh
  195. git.c
  196. git.rc
  197. gpg-interface.c
  198. gpg-interface.h
  199. graph.c
  200. graph.h
  201. grep.c
  202. grep.h
  203. hash-lookup.c
  204. hash-lookup.h
  205. hash.h
  206. hashmap.c
  207. hashmap.h
  208. help.c
  209. help.h
  210. hex.c
  211. hook.c
  212. hook.h
  213. http-backend.c
  214. http-fetch.c
  215. http-push.c
  216. http-walker.c
  217. http.c
  218. http.h
  219. ident.c
  220. imap-send.c
  221. INSTALL
  222. iterator.h
  223. json-writer.c
  224. json-writer.h
  225. khash.h
  226. kwset.c
  227. kwset.h
  228. levenshtein.c
  229. levenshtein.h
  230. LGPL-2.1
  231. line-log.c
  232. line-log.h
  233. line-range.c
  234. line-range.h
  235. linear-assignment.c
  236. linear-assignment.h
  237. list-objects-filter-options.c
  238. list-objects-filter-options.h
  239. list-objects-filter.c
  240. list-objects-filter.h
  241. list-objects.c
  242. list-objects.h
  243. list.h
  244. ll-merge.c
  245. ll-merge.h
  246. lockfile.c
  247. lockfile.h
  248. log-tree.c
  249. log-tree.h
  250. ls-refs.c
  251. ls-refs.h
  252. mailinfo.c
  253. mailinfo.h
  254. mailmap.c
  255. mailmap.h
  256. Makefile
  257. match-trees.c
  258. mem-pool.c
  259. mem-pool.h
  260. merge-blobs.c
  261. merge-blobs.h
  262. merge-ort-wrappers.c
  263. merge-ort-wrappers.h
  264. merge-ort.c
  265. merge-ort.h
  266. merge-recursive.c
  267. merge-recursive.h
  268. merge.c
  269. mergesort.c
  270. mergesort.h
  271. midx.c
  272. midx.h
  273. name-hash.c
  274. notes-cache.c
  275. notes-cache.h
  276. notes-merge.c
  277. notes-merge.h
  278. notes-utils.c
  279. notes-utils.h
  280. notes.c
  281. notes.h
  282. object-file.c
  283. object-name.c
  284. object-store.h
  285. object.c
  286. object.h
  287. oid-array.c
  288. oid-array.h
  289. oidmap.c
  290. oidmap.h
  291. oidset.c
  292. oidset.h
  293. oidtree.c
  294. oidtree.h
  295. pack-bitmap-write.c
  296. pack-bitmap.c
  297. pack-bitmap.h
  298. pack-check.c
  299. pack-objects.c
  300. pack-objects.h
  301. pack-revindex.c
  302. pack-revindex.h
  303. pack-write.c
  304. pack.h
  305. packfile.c
  306. packfile.h
  307. pager.c
  308. parallel-checkout.c
  309. parallel-checkout.h
  310. parse-options-cb.c
  311. parse-options.c
  312. parse-options.h
  313. patch-delta.c
  314. patch-ids.c
  315. patch-ids.h
  316. path.c
  317. path.h
  318. pathspec.c
  319. pathspec.h
  320. pkt-line.c
  321. pkt-line.h
  322. preload-index.c
  323. pretty.c
  324. pretty.h
  325. prio-queue.c
  326. prio-queue.h
  327. progress.c
  328. progress.h
  329. promisor-remote.c
  330. promisor-remote.h
  331. prompt.c
  332. prompt.h
  333. protocol-caps.c
  334. protocol-caps.h
  335. protocol.c
  336. protocol.h
  337. prune-packed.c
  338. prune-packed.h
  339. quote.c
  340. quote.h
  341. range-diff.c
  342. range-diff.h
  343. reachable.c
  344. reachable.h
  345. read-cache.c
  346. README.md
  347. rebase-interactive.c
  348. rebase-interactive.h
  349. rebase.c
  350. rebase.h
  351. ref-filter.c
  352. ref-filter.h
  353. reflog-walk.c
  354. reflog-walk.h
  355. refs.c
  356. refs.h
  357. refspec.c
  358. refspec.h
  359. remote-curl.c
  360. remote.c
  361. remote.h
  362. replace-object.c
  363. replace-object.h
  364. repo-settings.c
  365. repository.c
  366. repository.h
  367. rerere.c
  368. rerere.h
  369. reset.c
  370. reset.h
  371. resolve-undo.c
  372. resolve-undo.h
  373. revision.c
  374. revision.h
  375. run-command.c
  376. run-command.h
  377. SECURITY.md
  378. send-pack.c
  379. send-pack.h
  380. sequencer.c
  381. sequencer.h
  382. serve.c
  383. serve.h
  384. server-info.c
  385. setup.c
  386. sh-i18n--envsubst.c
  387. sha1dc_git.c
  388. sha1dc_git.h
  389. shallow.c
  390. shallow.h
  391. shell.c
  392. shortlog.h
  393. sideband.c
  394. sideband.h
  395. sigchain.c
  396. sigchain.h
  397. simple-ipc.h
  398. sparse-index.c
  399. sparse-index.h
  400. split-index.c
  401. split-index.h
  402. stable-qsort.c
  403. strbuf.c
  404. strbuf.h
  405. streaming.c
  406. streaming.h
  407. string-list.c
  408. string-list.h
  409. strmap.c
  410. strmap.h
  411. strvec.c
  412. strvec.h
  413. sub-process.c
  414. sub-process.h
  415. submodule-config.c
  416. submodule-config.h
  417. submodule.c
  418. submodule.h
  419. symlinks.c
  420. tag.c
  421. tag.h
  422. tar.h
  423. tempfile.c
  424. tempfile.h
  425. thread-utils.c
  426. thread-utils.h
  427. tmp-objdir.c
  428. tmp-objdir.h
  429. trace.c
  430. trace.h
  431. trace2.c
  432. trace2.h
  433. trailer.c
  434. trailer.h
  435. transport-helper.c
  436. transport-internal.h
  437. transport.c
  438. transport.h
  439. tree-diff.c
  440. tree-walk.c
  441. tree-walk.h
  442. tree.c
  443. tree.h
  444. unicode-width.h
  445. unimplemented.sh
  446. unix-socket.c
  447. unix-socket.h
  448. unix-stream-server.c
  449. unix-stream-server.h
  450. unpack-trees.c
  451. unpack-trees.h
  452. upload-pack.c
  453. upload-pack.h
  454. url.c
  455. url.h
  456. urlmatch.c
  457. urlmatch.h
  458. usage.c
  459. userdiff.c
  460. userdiff.h
  461. utf8.c
  462. utf8.h
  463. varint.c
  464. varint.h
  465. version.c
  466. version.h
  467. versioncmp.c
  468. walker.c
  469. walker.h
  470. wildmatch.c
  471. wildmatch.h
  472. worktree.c
  473. worktree.h
  474. wrap-for-bin.sh
  475. wrapper.c
  476. write-or-die.c
  477. ws.c
  478. wt-status.c
  479. wt-status.h
  480. xdiff-interface.c
  481. xdiff-interface.h
  482. zlib.c
README.md

Build status

Git - fast, scalable, distributed revision control system

Git is a fast, scalable, distributed revision control system with an unusually rich command set that provides both high-level operations and full access to internals.

Git is an Open Source project covered by the GNU General Public License version 2 (some parts of it are under different licenses, compatible with the GPLv2). It was originally written by Linus Torvalds with help of a group of hackers around the net.

Please read the file INSTALL for installation instructions.

Many Git online resources are accessible from https://git-scm.com/ including full documentation and Git related tools.

See Documentation/gittutorial.txt to get started, then see Documentation/giteveryday.txt for a useful minimum set of commands, and Documentation/git-<commandname>.txt for documentation of each command. If git has been correctly installed, then the tutorial can also be read with man gittutorial or git help tutorial, and the documentation of each command with man git-<commandname> or git help <commandname>.

CVS users may also want to read Documentation/gitcvs-migration.txt (man gitcvs-migration or git help cvs-migration if git is installed).

The user discussion and development of Git take place on the Git mailing list -- everyone is welcome to post bug reports, feature requests, comments and patches to git@vger.kernel.org (read Documentation/SubmittingPatches for instructions on patch submission and Documentation/CodingGuidelines).

Those wishing to help with error message, usage and informational message string translations (localization l10) should see po/README.md (a po file is a Portable Object file that holds the translations).

To subscribe to the list, send an email with just “subscribe git” in the body to majordomo@vger.kernel.org (not the Git list). The mailing list archives are available at https://lore.kernel.org/git/, http://marc.info/?l=git and other archival sites.

Issues which are security relevant should be disclosed privately to the Git Security mailing list git-security@googlegroups.com.

The maintainer frequently sends the “What's cooking” reports that list the current status of various development topics to the mailing list. The discussion following them give a good reference for project status, development direction and remaining tasks.

The name “git” was given by Linus Torvalds when he wrote the very first version. He described the tool as “the stupid content tracker” and the name as (depending on your mood):

  • random three-letter combination that is pronounceable, and not actually used by any common UNIX command. The fact that it is a mispronunciation of “get” may or may not be relevant.
  • stupid. contemptible and despicable. simple. Take your pick from the dictionary of slang.
  • “global information tracker”: you're in a good mood, and it actually works for you. Angels sing, and a light suddenly fills the room.
  • “goddamn idiotic truckload of sh*t”: when it breaks