Skip to content

Commit b9063af

Browse files
carenasgitster
authored andcommitted
t0034: add negative tests and allow git init to mostly work under sudo
Add a support library that provides one function that can be used to run a "scriplet" of commands through sudo and that helps invoking sudo in the slightly awkward way that is required to ensure it doesn't block the call (if shell was allowed as tested in the prerequisite) and it doesn't run the command through a different shell than the one we intended. Add additional negative tests as suggested by Junio and that use a new workspace that is owned by root. Document a regression that was introduced by previous commits where root won't be able anymore to access directories they own unless SUDO_UID is removed from their environment. The tests document additional ways that this new restriction could be worked around and the documentation explains why it might be instead considered a feature, but a "fix" is planned for a future change. Helped-by: Junio C Hamano <[email protected]> Helped-by: Phillip Wood <[email protected]> Signed-off-by: Carlo Marcelo Arenas Belón <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent ae9abbb commit b9063af

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

t/lib-sudo.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Helpers for running git commands under sudo.
2+
3+
# Runs a scriplet passed through stdin under sudo.
4+
run_with_sudo () {
5+
local ret
6+
local RUN="$TEST_DIRECTORY/$$.sh"
7+
write_script "$RUN" "$TEST_SHELL_PATH"
8+
# avoid calling "$RUN" directly so sudo doesn't get a chance to
9+
# override the shell, add aditional restrictions or even reject
10+
# running the script because its security policy deem it unsafe
11+
sudo "$TEST_SHELL_PATH" -c "\"$RUN\""
12+
ret=$?
13+
rm -f "$RUN"
14+
return $ret
15+
}

t/t0034-root-safe-directory.sh

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@
33
test_description='verify safe.directory checks while running as root'
44

55
. ./test-lib.sh
6+
. "$TEST_DIRECTORY"/lib-sudo.sh
67

78
if [ "$GIT_TEST_ALLOW_SUDO" != "YES" ]
89
then
910
skip_all="You must set env var GIT_TEST_ALLOW_SUDO=YES in order to run this test"
1011
test_done
1112
fi
1213

14+
if ! test_have_prereq NOT_ROOT
15+
then
16+
skip_all="These tests do not support running as root"
17+
test_done
18+
fi
19+
1320
test_lazy_prereq SUDO '
1421
sudo -n id -u >u &&
1522
id -u root >r &&
@@ -19,6 +26,12 @@ test_lazy_prereq SUDO '
1926
test_cmp u r
2027
'
2128

29+
if ! test_have_prereq SUDO
30+
then
31+
skip_all="Your sudo/system configuration is either too strict or unsupported"
32+
test_done
33+
fi
34+
2235
test_expect_success SUDO 'setup' '
2336
sudo rm -rf root &&
2437
mkdir -p root/r &&
@@ -36,6 +49,55 @@ test_expect_success SUDO 'sudo git status as original owner' '
3649
)
3750
'
3851

52+
test_expect_success SUDO 'setup root owned repository' '
53+
sudo mkdir -p root/p &&
54+
sudo git init root/p
55+
'
56+
57+
test_expect_success 'cannot access if owned by root' '
58+
(
59+
cd root/p &&
60+
test_must_fail git status
61+
)
62+
'
63+
64+
test_expect_success 'can access if addressed explicitly' '
65+
(
66+
cd root/p &&
67+
GIT_DIR=.git GIT_WORK_TREE=. git status
68+
)
69+
'
70+
71+
test_expect_failure SUDO 'can access with sudo if root' '
72+
(
73+
cd root/p &&
74+
sudo git status
75+
)
76+
'
77+
78+
test_expect_success SUDO 'can access with sudo if root by removing SUDO_UID' '
79+
(
80+
cd root/p &&
81+
run_with_sudo <<-END
82+
unset SUDO_UID &&
83+
git status
84+
END
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+
39101
# this MUST be always the last test
40102
test_expect_success SUDO 'cleanup' '
41103
sudo rm -rf root

0 commit comments

Comments
 (0)