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

[PATCH v3 2/3] git-compat-util: avoid failing dir ownership checks if running privileged

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
May 3, 2022, 06:54 UTC
Message-ID
<20220503065442.95699-3-carenas@gmail.com>
In-Reply-To
<20220503065442.95699-1-carenas@gmail.com>

bdc77d1d685 (Add a function to determine whether a path is owned by the current user, 2022-03-02) checks for the effective uid of the running process using geteuid() but didn't account for cases where that user was root (because git was invoked through sudo or a compatible tool) and the original uid that repository trusted for its config was no longer known, therefore failing the following otherwise safe call:

  guy@renard ~/Software/uncrustify $ sudo git describe --always --dirty
  [sudo] password for guy:
  fatal: unsafe repository ('/home/guy/Software/uncrustify' is owned by someone else)

Attempt to detect those cases by using the environment variables that those tools create to keep track of the original user id, and do the ownership check using that instead.

This assumes the environment the user is running on after going privileged can't be tampered with, and also adds code to restrict that the new behavior only applies if running as root, therefore keeping the most common case, which runs unprivileged, from changing, but because of that, it will miss cases where sudo (or an equivalent) was used to change to another unprivileged user or where the equivalent tool used to raise privileges didn't track the original id in a sudo compatible way.

Reported-by: Guy Maurel <guy.j@maurel.de>
Helped-by: SZEDER Gábor <szeder.dev@gmail.com>
Helped-by: Randall Becker <rsbecker@nexbridge.com>
Helped-by: Phillip Wood <phillip.wood123@gmail.com>
Suggested-by: Johannes Schindelin <Johannes.Schindelin@gmx.de>
Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com>
---
 Documentation/config/safe.txt  |  9 ++++++++
 git-compat-util.h              | 40 +++++++++++++++++++++++++++++++++-
 t/t0034-root-safe-directory.sh |  2 +-
 3 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/Documentation/config/safe.txt b/Documentation/config/safe.txt
index 6d764fe0ccf..ee558ced8c7 100644
--- a/Documentation/config/safe.txt
+++ b/Documentation/config/safe.txt
@@ -26,3 +26,12 @@ directory was listed in the `safe.directory` list. If `safe.directory=*`
 is set in system config and you want to re-enable this protection, then
 initialize your list with an empty value before listing the repositories
 that you deem safe.
++
+When git tries to check for ownership of git repositories, it will
+obviously do so with the uid of the user that is running git itself,
+but if git is running as root, it will check first if it might have
+been started through `sudo`, and if that is the case, will instead
+use the uid of the user that did so.
+If that is not what you would prefer and want git to only trust
+repositories that are owned by root instead, then you should remove
+the `SUDO_UID` variable from root's environment.
diff --git a/git-compat-util.h b/git-compat-util.h
index 63ba89dd31d..dfdd3e4f81a 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -393,12 +393,50 @@ static inline int git_offset_1st_component(const char *path)
 #endif
 
 #ifndef is_path_owned_by_current_user
+
+#ifdef __TANDEM
+#define ROOT_UID 65535
+#else
+#define ROOT_UID 0
+#endif
+
+/*
+ * this helper function overrides a ROOT_UID with the one provided by
+ * an environment variable, do not use unless the original user is
+ * root
+ */
+static inline void extract_id_from_env(const char *env, uid_t *id)
+{
+	const char *real_uid = getenv(env);
+
+	/* discard any empty values */
+	if (real_uid && *real_uid) {
+		char *endptr;
+		unsigned long env_id;
+		int saved_errno = errno;
+
+		errno = 0;
+		env_id = strtoul(real_uid, &endptr, 10);
+		if (!errno && !*endptr && env_id <= (uid_t)-1)
+			*id = env_id;
+
+		errno = saved_errno;
+	}
+}
+
 static inline int is_path_owned_by_current_uid(const char *path)
 {
 	struct stat st;
+	uid_t euid;
+
 	if (lstat(path, &st))
 		return 0;
-	return st.st_uid == geteuid();
+
+	euid = geteuid();
+	if (euid == ROOT_UID)
+		extract_id_from_env("SUDO_UID", &euid);
+
+	return st.st_uid == euid;
 }
 
 #define is_path_owned_by_current_user is_path_owned_by_current_uid
diff --git a/t/t0034-root-safe-directory.sh b/t/t0034-root-safe-directory.sh
index 6dac7a05cfd..dd659aed4e1 100755
--- a/t/t0034-root-safe-directory.sh
+++ b/t/t0034-root-safe-directory.sh
@@ -32,7 +32,7 @@ test_expect_success SUDO 'setup' '
 	)
 '
 
-test_expect_failure SUDO 'sudo git status as original owner' '
+test_expect_success SUDO 'sudo git status as original owner' '
 	(
 		cd root/r &&
 		git status &&
-- 
2.36.0.352.g0cd7feaf86f
Previous: Carlo Marcelo Arenas BelónNext: Johannes Schindelin
Message 88 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.