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

Re: [PATCH v2 3/3] t: add tests for safe.directory when running with sudo

From
Carlo Marcelo Arenas Belón <carenas@gmail.com>
Date
Apr 29, 2022, 20:05 UTC
Message-ID
<20220429200544.p7ggvccdnksmhvxt@carlos-mbp.lan>
In-Reply-To
<xmqqee1fbu67.fsf@gitster.g>
On Fri, Apr 29, 2022 at 11:50:40AM -0700, Junio C Hamano wrote:
Show 8 quoted lines
> Carlo Marcelo Arenas Belón <carenas@gmail.com> writes:
> 
> > If we do :
> >
> >   [0] login as regular user || sudo to root || login as root
> 
> Among these three, the last one is equivalent to "sudo and then
> unset the environment", right?

Correct, the differences from the most likely sequence of commands of a potential tester on their workstation (which as you point, most of the time doesn't allow login in as root) is :

  $ sudo -s -H
  # unset SUDO_UID
  # IKNOWWHATIAMDOING=yes ./t0034-root-safe-directory.sh
the unset only relevant for the third option, of course.
Show 6 quoted lines
> As many installations no longer allow direct "login" as "root",
> clarifying how users can get the behaviour for the third column
> would help our documentation, and that is the reason behind the
> question.  IOW, this is merely a simple yes-or-no question to
> sanity-check our mutual understanding, and no need to get overly
> defensive about being asked.

Apologies if sounding otherwise, but I can assure I am not offended by any of this questions, and looking forward to improve in the documentation with the help you had provided on reviewing it.

I would also like to thank you personally, since I am well aware that as a not native English speaker, and because Spanish is my mother's tongue I usually write things in a way that is not natural or expected and also overly verbose and without expected pauses. So having a second set of eyes on it is specially appreciated.

Show 19 quoted lines
> >   [1] % mkdir -p root/r
> >   [2] % git init root/r
> >   [3] % cd root/r && git status
> >
> >   step \ type | regular user | sudo to root | root
> > --------------------------------------------------
> >             1 |    work      |     work     | work
> >   before    2 |    work      |     work     | work
> >             3 |    work      |     work     | work
> > ---------------------------------------------------
> >             1 |    work      |     work     | work
> >    after    2 |    work      |     work     | work
> >             3 |    work      |     fail     | work
> 
> So the only difference is that in a repository created by a user who
> did "sudo mkdir; sudo git init".  It used to be that the same user
> can read the repository with "sudo git status" (because we did not
> care about how we become 'root', we only saw the owner of the repo
> and the current euid).  Now, such an access is no longer allowed.

Correct, but more importantly we now allow the sequence documented in the test case which is also IMHO more comnon and useful, where the last step might need to run through sudo (unlike the previous ones)

Show 13 quoted lines
> But I do not see what relevance the above has to the argument you
> were making, against "if you start these tests as 'root' (instead of
> starting as an ordinary user), some tests may succeed but for a
> wrong reason, and some tests may fail because they are not prepared
> for it; it is wrong to mark only the latter with prerequisite and
> not the former".  The change in the behaviour we see above is for
> those who start as an ordinary user and uses "sudo" without dropping
> SUDO_UID.  How is it relevant to allow those who start the test as
> 'root' (not an ordinary user) to try that?  Tests done under such
> condition will see 'root' in euid, SUDO_UID, and st.st_uid, so there
> is no way for them to detect any mismatch and behave differently,
> so the transition from "it used to work" to "now it is forbidden"
> is not even tested.

Correct again; guess my intention wasn't clear. My concern was that by blocking root, we don't allow for such a test (if it can be written, since as you pointed out is challenging) to be added and we also lose the currently failing run which can be used as an "explanation" of sorts to the question of "is this supported and expected to work?" which I think the tests help answer and regardless of what is in the documentation.

Show 12 quoted lines
> > and rejects the repository because it is NOT owned by that id (it was created
> > by root anyway, even if there is no way for git to know that it was done
> > at a different time and with a different session, and therefore the SUDO_UID
> > variable it is honoring could be considered irrelevant in the current context.
> >
> > in the documentation patch (which I think would be better to squash with the
> > fix) I explain what to do as a workaround, and I expect this use case to be
> > less common than the currently broken one (so a net positive)
> 
> Both of these two paragraphs speak truth, but there is no relevance
> to, and it does not justify, your "I disagree, and think that the
> fact ... proves my point".

I ended up doing a bigger refactoring with v3 that split the tests in two and which I think will help also in the long run, but kept the Documentation patch independent since it already has your SO and don't want to waste your time further by having to re-review it.

Assuming that there are no more improvements (or even if they are) to be made to the documentation would be OK if I follow my own advice and squash it together with the code change that introduces the change?

Apologies for not doing it earlier, but in my defense I would say that I sent the currently applied version originally as an RFC ;)

Show 37 quoted lines
> For example, this is the 'setup' step.
> 
> > +test_expect_success SUDO 'setup' '
> > +	sudo rm -rf root &&
> > +	mkdir -p root/r &&
> > +	sudo chown root root &&
> > +	(
> > +		cd root/r &&
> > +		git init
> > +	)
> > +'
> 
> If the test was started by an ordinary user, root/r is owned by the
> user who is not 'root'.  If the test was started by 'root',
> everything is owned by 'root'.  Either way, 'root' is owned by
> 'root'.  In such a repository, we see this test:
> 
> +test_expect_success SUDO 'sudo git status as original owner' '
> +	(
> +		cd root/r &&
> +		git status &&
> +		sudo git status
> +	)
> +'
> 
> The behaviour we are trying to ensure is that, even though root/r is
> owned by non-root, accessing it with "git status" as the original
> user and "git status" as root work, as long as if you used "sudo" in
> the second "git status", so that "git status" can take SUDO_UID into
> account.  The test is making sure that our "pay attention to
> SUDO_UID" mechanism has not been broken by future changes.
> 
> If we start this test as 'root', we cannot test for that.  The setup
> step made everything owned by 'root', and we go there as 'root' and
> run "git status", which should succceed, but with "sudo git status",
> even we broke SUDO_UID mechanism and compared euid with st.st_uid,
> we'll allow an access.

Depending of the way you become 'root' the test could fail as well, but IMHO the failing should be expected (and as shown in the table above) was introduced with this change.

The gist is that if we started the whole test with sudo, there is no way for git (with the current implementation) to differenciate if that SUDO_UID is relevant only to its current run or to the environment and more importantly what the intentions are from the user that is running on that environment.

Alternatively we could ammend the code to allow for such logic but I don't think that would be easier to implement and I don't see much of a benefit.

My thinking is that if we are going to let sudo tells us who the user is then we must trust that SUDO_UID indicates also the intention of the user behind that environment and therefore it is correct in this case to fail.

If the user really meant to not trust its SUDO_UID and instead use the id we are running as, then he should have removed it from the environment before invoking git as documented.

Long term, as you proposed, if we are running as root we might ALSO consider that any root owned repositories should be fine to trust but that change is orthogonal to fixing the 'sudo git <command> in my own repository doesn't work after the last maint release'

It should also be important to note that starting the whole test with sudo is not trivial, neither expected, and will also block because of sudo most likely removing the IKNOWWHATIAMDOING environment variable, so whichever way we decide we shouldn't expect someone accidentally failing this test.

> So the test may succeed but it succeeds for a wrong reason even
> after we break the mechanism added by this series.

the test as root without a SUDO_UID would succeed for the wrong reasons so I agree (and again apologize for suggesting it) that using a prerequisite to squash that run (as done in the other tests) was wrong.

what I am still not sure about is if it is worth complicating the test by adding logic that differenciate a root user with and without SUDO_UID that might run it, and I have to admit that the current draft I have of v3 does follow your advice of blocking root from even being able to run it as suggested (and which is also what I am leaning for).

Carlo

PS. I apologize for not trimming on your responses more aggresively to save some bytes as I would normally do, but wanted to make sure you understand that by trimming those parts I wasn't implying they weren't read or relevant, but just not strictly neccesary and implictly agreed upon for further discussion

Previous: Junio C HamanoNext: Carlo Marcelo Arenas Belón
Message 78 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.