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

Re: [PATCH] git-compat-util: avoid failing dir ownership checks if running privileged

From
Carlo Arenas <carenas@gmail.com>
Date
Apr 27, 2022, 20:59 UTC
Message-ID
<CAPUEsph9tUkm+JsvU6z0Wt7mtRcTCH1vbvYTEdWjNyXzdoU=vw@mail.gmail.com>
In-Reply-To
<xmqqzgk6l5ly.fsf@gitster.g>
On Wed, Apr 27, 2022 at 11:54 AM Junio C Hamano <gitster@pobox.com> wrote:
> The "sudo sh to get a shell, chdir to /var/tmp/foo to do something"
> use case does care---it needs to make sure that whereever it goes is
> not part of a hostile repository.  So "if SUDO_UID is there, anything
> goes" is not a good protection for such a use case.

FWIW that was never part of the proposal, indeed making git aware of SUDO_ID when it is running as only as root was part of the design to avoid other users probably allowing repositories they don't control by having an evil SUDO_ID.

as per the root user, I was expecting that he could be trusted more and that wouldn't accidentally get an evil SUDO_ID on their session because it is something that gets generated from their original account and they should be aware of it and even might need to tweak it (ex: by un setting it if it gets in the way).

To make the `sudo make install` use case possible I think it make sense to trust SUDO_ID by default, but I would be open to add a configuration setting that would be required to enable it (it indeed might become useful for root users that might want to disable it as an alternative to unset SUDO_ID).

Show 6 quoted lines
> Possible?  Yes.  Practical?  I dunno.  Especially if we do not
> deliberately truncate by reading into int16_t but use "int" (32-bit
> almost everywhere we care about) or "long".  Now how likely is uid_t
> narrower than long?  If it is not likely, then we probably are OK to
> use long and not worry about the truncation at all.  Or use strtoll()
> to make it even less likely.

strtoll (or my suggested alternative of strtoimax) suffer from portability issues which are handled later in the git-compat-util.h file, so while I think it is worth doing, I would prefer doing so later as part of moving this code into a proper compat module.

for this iteration, the suggestion by Phillip to use strtoul makes more sense (specially after confirmation by Randall that it works in NON-STOP), it is more portable and doesn't require later in the file git-compat magic to work, it allows for the full range of 32bit that might be needed in 32bit *NIX, it is should fit find even in 32bit environments with a 32bit unsigned uid_t (which is fairly common), it doesn't sign extend if applied into a wider uid_t and would be likely able to be assigned even without a cast and without the risk of truncation for most uid_t types. Lastly, since now this code is focused only in sudo compatibility, and sudo casts their uid to an unsigned int and prints it into that variable with "%u" it is the right "API".

For comparison, I would also think it is better to let the compiler warn if needed so at least the users would know their uid_t type is narrower than what we expect and sudo sets, and might work as a weather balloon to find those systems.

>
> So, in short, I think it is reasonable if we did:
>
>  - do the extra "if SUDO_UID exists, then..." only when we are root.

already in since the RFC, see discussion above for an option to tighten it further if really needed, but even in that case I would rather do it in the next iteration.

>  - use strtol to ensure SUDO_UID is in good shape (all digits) and
>    read into long; use errno=0 trick to notice overflows.  err on
>    the safe side and do not allow if the value cannot be read
>    correctly.

already implemented as part of v2, except it is using strtoul/unsigned long. also in the line of "err on the safe side" I was also discarding any values that would overflow an uid_t (which is assumed to be unsigned), which affect the next bullet point.

>  - do the comparison between st.st_uid and strtol() result by
>    casting both to (long).  Without this, st.st_uid has a value that
>    needs wider than long, we cannot allow the owner of such a
>    repository in (and I somehow feel that is unlikely to happen).

do you still want to cast both sides even if discarding values that would overflow an uid_t?

FWIW, as Phillip reported, using long (or another signed type) would restrict the valid ids in 32-bit *NIX to half of what the expected uint32_t range most of them should have.

>  - or omit the third one --- tough luck for those whose UID is
>    beyond what a long can represent (and I somehow feel that this is
>    probably OK).

That was what I proposed in the current RFC (at least in 32bit *NIX) but I have to admit that i'd seen a lot more issues with uid larger than INT_MAX that I expected when doing google for it, so that is what I thought it would be safer to use the full 32-bit and an unsigned type instead.

> Another thing I'd suggest doing is to have a helper function to do
> the "we do a bit more than geteuid() to come up with the allowed
> owner of repositories we encounter", and call that function instead
> of geteuid().

I did this originally, but discarded it quickly because it was a little odd and easy to reuse, for something that I was hoping wouldn't stay in git-compat for too long.

The use of the current helper function covers that use case but is by design harder to reuse while keeping the overall logic of the 2 combined functions more visible.

Once we move this code into a proper compat module, I would be happy to add a geteuid_or_sudo() function but that would be probably after we also fix doas.

Carlo
Previous: Junio C HamanoNext: rsbecker@nexbridge.com
Message 17 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.