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

[PATCH v4 3/3] t0034: add negative tests and allow git init to mostly work under sudo

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
May 10, 2022, 17:46 UTC
Message-ID
<20220510174616.18629-4-carenas@gmail.com>
In-Reply-To
<20220510174616.18629-1-carenas@gmail.com>

Add a support library that provides one function that can be used to run a "scriplet" of commands through sudo and that helps invoking sudo in the slightly awkward way that is required to ensure it doesn't block the call (if shell was allowed as tested in the prerequisite) and it doesn't run the command through a different shell than the one we intended.

Add additional negative tests as suggested by Junio and that use a new workspace that is owned by root.

Note that the specific test that documents that after the previous changes, it is no longer possible for root (if obtained through sudo) to NOT add an exception or NOT need a "workaround" to be able to run git commands in a repository owned by thyself, is marked as a regression and is expected to be fixed with a future change, which hasn't been provided yet and that is not part of this series.

As described in the comments from the test itself, at least one of the documented workarounds could fail if sudo doesn't allow root to call sudo or if root is not in sudoers, but that is very unlikely, and more importantly actually not what is provided by the currently supported sudo configuration this test already relies on and therefore adding a way to validate it works in the prerequisite has been punted for now.

Helped-by: Junio C Hamano <gitster@pobox.com>
Helped-by: Phillip Wood <phillip.wood123@gmail.com>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 t/lib-sudo.sh                  | 12 +++++++
 t/t0034-root-safe-directory.sh | 58 ++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 t/lib-sudo.sh
diff --git a/t/lib-sudo.sh b/t/lib-sudo.sh
new file mode 100644
index 0000000000..d8a88fb9db
--- /dev/null
+++ b/t/lib-sudo.sh
@@ -0,0 +1,12 @@
+# Helpers for running git commands under sudo.
+
+# Runs a scriplet passed through stdin under sudo.
+run_with_sudo () {
+	local ret
+	local RUN="$TEST_DIRECTORY/$$.sh"
+	write_script "$RUN" "$TEST_SHELL_PATH"
+	sudo "$TEST_SHELL_PATH" -c "\"$RUN\""
+	ret=$?
+	rm -f "$RUN"
+	return $ret
+}
diff --git a/t/t0034-root-safe-directory.sh b/t/t0034-root-safe-directory.sh
index ecd9dca6b3..54f687d787 100755
--- a/t/t0034-root-safe-directory.sh
+++ b/t/t0034-root-safe-directory.sh
@@ -3,6 +3,7 @@
 test_description='verify safe.directory checks while running as root'
 
 . ./test-lib.sh
+. "$TEST_DIRECTORY"/lib-sudo.sh
 
 if [ "$GIT_TEST_ALLOW_SUDO" != "YES" ]
 then
@@ -10,6 +11,12 @@ then
 	test_done
 fi
 
+if ! test_have_prereq NOT_ROOT
+then
+	skip_all="These tests do not support running as root"
+	test_done
+fi
+
 test_lazy_prereq SUDO '
 	sudo -n id -u >u &&
 	id -u root >r &&
@@ -19,6 +26,12 @@ test_lazy_prereq SUDO '
 	test_cmp u r
 '
 
+if ! test_have_prereq SUDO
+then
+	skip_all="Your sudo/system configuration is either too strict or unsupported"
+	test_done
+fi
+
 test_expect_success SUDO 'setup' '
 	sudo rm -rf root &&
 	mkdir -p root/r &&
@@ -37,6 +50,51 @@ test_expect_success SUDO 'sudo git status as original owner' '
 	)
 '
 
+test_expect_success SUDO 'setup root owned repository' '
+	sudo mkdir -p root/p &&
+	sudo git init root/p
+'
+
+test_expect_success 'cannot access if owned by root' '
+	(
+		cd root/p &&
+		test_must_fail git status
+	)
+'
+
+test_expect_failure SUDO 'can access with sudo if root' '
+	(
+		cd root/p &&
+		sudo git status
+	)
+'
+
+test_expect_success SUDO 'can access with sudo using a workaround' '
+	# run sudo twice; would fail if root is not in sudoers
+	(
+		cd root/p &&
+		sudo sudo git status
+	) &&
+	# provide explicit GIT_DIR
+	(
+		cd root/p &&
+		run_with_sudo <<-END
+			GIT_DIR=.git &&
+			GIT_WORK_TREE=. &&
+			export GIT_DIR GIT_WORK_TREE &&
+			git status
+		END
+	) &&
+	# discard SUDO_UID
+	(
+		cd root/p &&
+		run_with_sudo <<-END
+			unset SUDO_UID &&
+			git status
+		END
+	)
+'
+
 # this MUST be always the last test
 test_expect_success SUDO 'cleanup' '
 	sudo rm -rf root
-- 
2.36.1.371.g0fb0ef0c8d
Previous: Junio C HamanoNext: Junio C Hamano
Message 145 of 170 in “git-compat-util: avoid failing dir ownership checks if running priviledged”
  1. git-compat-util: avoid failing dir ownership checks if running priviledgedCarlo Marcelo Arenas Belón, Apr 26, 2022
  2. Derrick StoleeApr 26, 2022
  3. Junio C HamanoApr 26, 2022
  4. rsbecker@nexbridge.comApr 26, 2022
  5. Carlo ArenasApr 26, 2022
  6. Junio C HamanoApr 26, 2022
  7. Carlo ArenasApr 26, 2022
  8. Carlo ArenasApr 26, 2022
  9. Derrick StoleeApr 29, 2022
  10. git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, Apr 27, 2022
  11. Phillip WoodApr 27, 2022
  12. Phillip WoodApr 27, 2022
  13. rsbecker@nexbridge.comApr 27, 2022
  14. Carlo ArenasApr 27, 2022
  15. Phillip WoodApr 27, 2022
  16. Junio C HamanoApr 27, 2022
  17. Carlo ArenasApr 27, 2022
  18. rsbecker@nexbridge.comApr 27, 2022
  19. Junio C HamanoApr 27, 2022
  20. Phillip WoodApr 28, 2022
  21. Carlo ArenasApr 27, 2022
  22. rsbecker@nexbridge.comApr 27, 2022
  23. Junio C HamanoApr 27, 2022
  24. Carlo ArenasApr 27, 2022
  25. Phillip WoodApr 27, 2022
  26. rsbecker@nexbridge.comApr 27, 2022
  27. Carlo ArenasApr 27, 2022
  28. rsbecker@nexbridge.comApr 27, 2022
  29. Carlo ArenasApr 27, 2022
  30. rsbecker@nexbridge.comApr 27, 2022
  31. Phillip WoodApr 27, 2022
  32. Carlo ArenasApr 27, 2022
  33. Phillip WoodApr 27, 2022
  34. Carlo ArenasApr 27, 2022
  35. git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, Apr 27, 2022
  36. Junio C HamanoApr 27, 2022
  37. 0/2 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, Apr 28, 2022
  38. 1/2 Documentation: explain how safe.directory works when running under sudoCarlo Marcelo Arenas Belón, Apr 28, 2022
  39. Junio C HamanoApr 28, 2022
  40. Carlo ArenasApr 28, 2022
  41. Junio C HamanoApr 28, 2022
  42. 2/2 t: add tests for safe.directory when running with sudoCarlo Marcelo Arenas Belón, Apr 28, 2022
  43. Junio C HamanoApr 28, 2022
  44. Junio C HamanoApr 28, 2022
  45. 0/3 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, Apr 28, 2022
  46. 1/3 git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, Apr 28, 2022
  47. Phillip WoodApr 28, 2022
  48. Carlo ArenasApr 28, 2022
  49. 2/3 Documentation: explain how safe.directory works when running under sudoCarlo Marcelo Arenas Belón, Apr 28, 2022
  50. Bagas SanjayaApr 30, 2022
  51. Junio C HamanoApr 30, 2022
  52. Carlo Marcelo Arenas BelónApr 30, 2022
  53. 3/3 t: add tests for safe.directory when running with sudoCarlo Marcelo Arenas Belón, Apr 28, 2022
  54. Junio C HamanoApr 28, 2022
  55. Phillip WoodApr 28, 2022
  56. Junio C HamanoApr 28, 2022
  57. Carlo ArenasMay 6, 2022
  58. Junio C HamanoMay 6, 2022
  59. Carlo ArenasMay 6, 2022
  60. Junio C HamanoMay 6, 2022
  61. Carlo Marcelo Arenas BelónMay 7, 2022
  62. rsbecker@nexbridge.comApr 28, 2022
  63. Carlo ArenasApr 28, 2022
  64. rsbecker@nexbridge.comApr 28, 2022
  65. Junio C HamanoApr 28, 2022
  66. Carlo ArenasApr 28, 2022
  67. rsbecker@nexbridge.comApr 28, 2022
  68. Junio C HamanoApr 28, 2022
  69. rsbecker@nexbridge.comApr 28, 2022
  70. Junio C HamanoApr 28, 2022
  71. Junio C HamanoApr 28, 2022
  72. rsbecker@nexbridge.comApr 28, 2022
  73. Carlo ArenasApr 28, 2022
  74. Carlo ArenasApr 28, 2022
  75. Junio C HamanoApr 28, 2022
  76. Carlo Marcelo Arenas BelónApr 29, 2022
  77. Junio C HamanoApr 29, 2022
  78. Carlo Marcelo Arenas BelónApr 29, 2022
  79. 0/3 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, May 2, 2022
  80. 1/3 t: document regression git safe.directory when using sudoCarlo Marcelo Arenas Belón, May 2, 2022
  81. Junio C HamanoMay 2, 2022
  82. Carlo ArenasMay 2, 2022
  83. 2/3 git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, May 2, 2022
  84. 3/3 t0034: enhance framework to allow testing more commands under sudoCarlo Marcelo Arenas Belón, May 2, 2022
  85. Junio C HamanoMay 2, 2022
  86. Carlo ArenasMay 3, 2022
  87. 0/3 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, May 3, 2022
  88. 2/3 git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, May 3, 2022
  89. Johannes SchindelinMay 5, 2022
  90. Phillip WoodMay 5, 2022
  91. Carlo ArenasMay 6, 2022
  92. Junio C HamanoMay 6, 2022
  93. Carlo ArenasMay 6, 2022
  94. Junio C HamanoMay 6, 2022
  95. Carlo ArenasMay 6, 2022
  96. rsbecker@nexbridge.comMay 6, 2022
  97. Junio C HamanoMay 5, 2022
  98. Carlo ArenasMay 6, 2022
  99. 1/3 t: document regression git safe.directory when using sudoCarlo Marcelo Arenas Belón, May 3, 2022
  100. Phillip WoodMay 3, 2022
  101. Carlo Marcelo Arenas BelónMay 3, 2022
  102. Phillip WoodMay 4, 2022
  103. Carlo ArenasMay 4, 2022
  104. Phillip WoodMay 4, 2022
  105. Johannes SchindelinMay 5, 2022
  106. Phillip WoodMay 5, 2022
  107. Junio C HamanoMay 5, 2022
  108. Junio C HamanoMay 5, 2022
  109. Junio C HamanoMay 5, 2022
  110. Carlo ArenasMay 6, 2022
  111. Phillip WoodMay 9, 2022
  112. Carlo ArenasMay 9, 2022
  113. Phillip WoodMay 9, 2022
  114. Junio C HamanoMay 9, 2022
  115. Carlo ArenasMay 9, 2022
  116. Carlo ArenasMay 6, 2022
  117. 3/3 t0034: enhance framework to allow testing more commands under sudoCarlo Marcelo Arenas Belón, May 3, 2022
  118. Phillip WoodMay 3, 2022
  119. Junio C HamanoMay 3, 2022
  120. Carlo ArenasMay 6, 2022
  121. 0/3 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, May 7, 2022
  122. 1/3 t: regression git needs safe.directory when using sudoCarlo Marcelo Arenas Belón, May 7, 2022
  123. 2/3 git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, May 7, 2022
  124. Junio C HamanoMay 7, 2022
  125. Carlo Marcelo Arenas BelónMay 7, 2022
  126. Junio C HamanoMay 9, 2022
  127. rsbecker@nexbridge.comMay 9, 2022
  128. Carlo ArenasMay 9, 2022
  129. rsbecker@nexbridge.comMay 9, 2022
  130. Junio C HamanoMay 9, 2022
  131. 3/3 t0034: add negative tests and allow git init to mostly work under sudoCarlo Marcelo Arenas Belón, May 7, 2022
  132. Phillip WoodMay 10, 2022
  133. Carlo ArenasMay 10, 2022
  134. 0/3 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, May 10, 2022
  135. 1/3 t: regression git needs safe.directory when using sudoCarlo Marcelo Arenas Belón, May 10, 2022
  136. Junio C HamanoMay 10, 2022
  137. Carlo ArenasMay 10, 2022
  138. Junio C HamanoMay 10, 2022
  139. Carlo ArenasMay 11, 2022
  140. Junio C HamanoMay 11, 2022
  141. 2/3 git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, May 10, 2022
  142. Junio C HamanoMay 10, 2022
  143. Carlo ArenasMay 11, 2022
  144. Junio C HamanoMay 11, 2022
  145. 3/3 t0034: add negative tests and allow git init to mostly work under sudoCarlo Marcelo Arenas Belón, May 10, 2022
  146. Junio C HamanoMay 10, 2022
  147. Junio C HamanoMay 10, 2022
  148. Carlo ArenasMay 11, 2022
  149. Junio C HamanoMay 11, 2022
  150. 0/4 fix `sudo make install` regression in maintCarlo Marcelo Arenas Belón, May 13, 2022
  151. 1/4 t: regression git needs safe.directory when using sudoCarlo Marcelo Arenas Belón, May 13, 2022
  152. SZEDER GáborJun 3, 2022
  153. 2/4 git-compat-util: avoid failing dir ownership checks if running privilegedCarlo Marcelo Arenas Belón, May 13, 2022
  154. SZEDER GáborJun 3, 2022
  155. Junio C HamanoJun 3, 2022
  156. SZEDER GáborJun 3, 2022
  157. 3/4 t0034: add negative tests and allow git init to mostly work under sudoCarlo Marcelo Arenas Belón, May 13, 2022
  158. Junio C HamanoMay 13, 2022
  159. Carlo ArenasMay 14, 2022
  160. Junio C HamanoMay 15, 2022
  161. Carlo ArenasMay 15, 2022
  162. Junio C HamanoMay 16, 2022
  163. Carlo Marcelo Arenas BelónMay 16, 2022
  164. Junio C HamanoMay 16, 2022
  165. 4/4 git-compat-util: allow root to access both SUDO_UID and root ownedCarlo Marcelo Arenas Belón, May 13, 2022
  166. Johannes SchindelinJun 15, 2022
  167. Carlo ArenasJun 17, 2022
  168. Junio C HamanoJun 17, 2022
  169. git-compat-util: allow root to access both SUDO_UID and root ownedCarlo Marcelo Arenas Belón, Jun 17, 2022
  170. Junio C HamanoJun 17, 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.