Skip to content

Commit 4ed4d90

Browse files
carenasgitster
authored andcommitted
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. Suggested-by: Junio C Hamano <[email protected]> Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent b9063af commit 4ed4d90

File tree

3 files changed

+14
-21
lines changed

3 files changed

+14
-21
lines changed

Documentation/config/safe.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ As explained, Git only allows you to access repositories owned by
3131
yourself, i.e. the user who is running Git, by default. When Git
3232
is running as 'root' in a non Windows platform that provides sudo,
3333
however, git checks the SUDO_UID environment variable that sudo creates
34-
and will allow access to the uid recorded as its value instead.
34+
and will allow access to the uid recorded as its value in addition to
35+
the id from 'root'.
3536
This is to make it easy to perform a common sequence during installation
3637
"make && sudo make install". A git process running under 'sudo' runs as
3738
'root' but the 'sudo' command exports the environment variable to record
3839
which id the original user has.
3940
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
4142
the `SUDO_UID` variable from root's environment before invoking git.

git-compat-util.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,9 +420,10 @@ static inline int git_offset_1st_component(const char *path)
420420
* maybe provide you with a patch that would prevent this issue again
421421
* in the future.
422422
*/
423-
static inline void extract_id_from_env(const char *env, uid_t *id)
423+
static inline int id_from_env_matches(const char *env, uid_t id)
424424
{
425425
const char *real_uid = getenv(env);
426+
int matches = 0;
426427

427428
/* discard anything empty to avoid a more complex check below */
428429
if (real_uid && *real_uid) {
@@ -432,9 +433,10 @@ static inline void extract_id_from_env(const char *env, uid_t *id)
432433
errno = 0;
433434
/* silent overflow errors could trigger a bug here */
434435
env_id = strtoul(real_uid, &endptr, 10);
435-
if (!*endptr && !errno)
436-
*id = env_id;
436+
if (!*endptr && !errno && (uid_t)env_id == id)
437+
matches = 1;
437438
}
439+
return matches;
438440
}
439441

440442
static inline int is_path_owned_by_current_uid(const char *path)
@@ -446,10 +448,13 @@ static inline int is_path_owned_by_current_uid(const char *path)
446448
return 0;
447449

448450
euid = geteuid();
451+
if (st.st_uid == euid)
452+
return 1;
453+
449454
if (euid == ROOT_UID)
450-
extract_id_from_env("SUDO_UID", &euid);
455+
return id_from_env_matches("SUDO_UID", st.st_uid);
451456

452-
return st.st_uid == euid;
457+
return 0;
453458
}
454459

455460
#define is_path_owned_by_current_user is_path_owned_by_current_uid

t/t0034-root-safe-directory.sh

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ test_expect_success 'can access if addressed explicitly' '
6868
)
6969
'
7070

71-
test_expect_failure SUDO 'can access with sudo if root' '
71+
test_expect_success SUDO 'can access with sudo if root' '
7272
(
7373
cd root/p &&
7474
sudo git status
@@ -85,19 +85,6 @@ test_expect_success SUDO 'can access with sudo if root by removing SUDO_UID' '
8585
)
8686
'
8787

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-
10188
# this MUST be always the last test
10289
test_expect_success SUDO 'cleanup' '
10390
sudo rm -rf root

0 commit comments

Comments
 (0)