git-compat-util: allow root to access both SUDO_UID and root owned

Previous changes introduced a regression which will prevent root for accessing repositories owned by thyself if using sudo because SUDO_UID takes precedence. Loosen that restriction by allowing root to access repositories owned by both uid by default and without having to add a safe.directory exception. A previous workaround that was documented in the tests is no longer needed so it has been removed together with its specially crafted prerequisite. Helped-by: Johanness Schindelin <Johannes.Schindelin@gmx.de> Signed-off-by: Carlo Marcelo Arenas Belón <carenas@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Carlo Marcelo Arenas Belón committed Jun 17, 2022 at 13:23 UTC 6b11e3d52e919cce91011f4f9025e6f4b61375f2
3 files changed +11 -18
Documentation/config/safe.txt
+4 -3
@@ -30,12 +30,13 @@ that you deem safe.
30 As explained, Git only allows you to access repositories owned by
31 yourself, i.e. the user who is running Git, by default. When Git
32 is running as 'root' in a non Windows platform that provides sudo,
33 - however, git checks the SUDO_UID environment variable that sudo creates
34 -and will allow access to the uid recorded as its value instead.
33 +however, git checks the SUDO_UID environment variable that sudo creates
34 +and will allow access to the uid recorded as its value in addition to
35 +the id from 'root'.
36 This is to make it easy to perform a common sequence during installation
37 "make && sudo make install". A git process running under 'sudo' runs as
38 'root' but the 'sudo' command exports the environment variable to record
39 which id the original user has.
40 If that is not what you would prefer and want git to only trust
40 -repositories that are owned by root instead, then you must remove
41 +repositories that are owned by root instead, then you can remove
42 the `SUDO_UID` variable from root's environment before invoking git.
git-compat-util.h
+6 -1
@@ -447,7 +447,12 @@ static inline int is_path_owned_by_current_uid(const char *path)
447
448 euid = geteuid();
449 if (euid == ROOT_UID)
450 - extract_id_from_env("SUDO_UID", &euid);
450 + {
451 + if (st.st_uid == ROOT_UID)
452 + return 1;
453 + else
454 + extract_id_from_env("SUDO_UID", &euid);
455 + }
456
457 return st.st_uid == euid;
458 }
t/t0034-root-safe-directory.sh
+1 -14
@@ -68,7 +68,7 @@ test_expect_success 'can access if addressed explicitly' '
68 )
69 '
70
71 -test_expect_failure SUDO 'can access with sudo if root' '
71 +test_expect_success SUDO 'can access with sudo if root' '
72 (
73 cd root/p &&
74 sudo git status
@@ -85,19 +85,6 @@ test_expect_success SUDO 'can access with sudo if root by removing SUDO_UID' '
85 )
86 '
87
88 -test_lazy_prereq SUDO_SUDO '
89 - sudo sudo id -u >u &&
90 - id -u root >r &&
91 - test_cmp u r
92 -'
93 -
94 -test_expect_success SUDO_SUDO 'can access with sudo abusing SUDO_UID' '
95 - (
96 - cd root/p &&
97 - sudo sudo git status
98 - )
99 -'
100 -
88 # this MUST be always the last test
89 test_expect_success SUDO 'cleanup' '
90 sudo rm -rf root