Skip to content

Commit e215345

Browse files
Fix sudo usage.
Sudo is being used directly all over the place, which doesn't work when the user is already root. Add a SUDO and SUDO_W_ENV variable that evaluates to the equivalent sudo command, but is blanked when running as root. Also add a sudo and sudo_w_env alias that map to a fake-root function that just runs the command passed without any sudo call, so any attempted use of sudo by python scripts triggered from the cmd-* scripts won't actually use sudo when running as root.
1 parent a13cf77 commit e215345

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

src/cmd-fetch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ dn=$(dirname "$0")
88
FILE=cache/pkgcache-repo
99
if [ -d "${FILE}" ]
1010
then
11-
pkgcachesize=$(sudo du --bytes --max-depth 0 "${FILE}" \
11+
pkgcachesize=$(${SUDO} du --bytes --max-depth 0 "${FILE}" \
1212
| awk '{print $1; exit}')
1313
pkglimit=$((1024 * 1024 * 1024 * 5))
1414
if [[ "${pkgcachesize}" -gt "${pkglimit}" ]]
1515
then
16-
sudo cosa prune --pkgcache
16+
${SUDO} cosa prune --pkgcache
1717
fi
1818
fi
1919

src/cmd-init

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ source=$1; shift
147147
preflight
148148

149149
if has_privileges; then
150-
sudo chown "$USER:" .
150+
${SUDO} chown "$USER:" .
151151
elif [ ! -w . ]; then
152152
fatal "init: running unprivileged, and current directory not writable"
153153
fi

src/cmdlib.sh

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,27 @@ set -euo pipefail
55
DIR=$(dirname "$(realpath "${BASH_SOURCE[0]}")")
66
RFC3339="%Y-%m-%dT%H:%M:%SZ"
77

8+
# Fix 'sudo' in case we're running as root
9+
if [ "$(id -u)" != "0" ]; then
10+
export SUDO=sudo
11+
export SUDO_W_ENV='sudo -E'
12+
# always provide the sudo_w_env alias so python scripts can be sure it always exists
13+
alias sudo_w_env='sudo -E'
14+
else
15+
export SUDO=
16+
export SUDO_W_ENV=
17+
fake-sudo() {
18+
# pass thru the exit code implicitly
19+
set -e
20+
"$@"
21+
}
22+
export -f fake-sudo
23+
# Spoof 'sudo' in the environment to go to our wrapper that does nothing instead, including a sudo_w_env that drops the -E options as well.
24+
# Python code doesn't use the ${SUDO} or ${SUDO_W_ENV} variables, so this forces them to the right thing when hardcoding the subprocess commands.
25+
alias sudo='fake-sudo'
26+
alias sudo_w_env='fake-sudo'
27+
fi
28+
829
info() {
930
echo "info: $*" 1>&2
1031
}
@@ -113,9 +134,9 @@ preflight_kvm() {
113134
if ! has_privileges; then
114135
fatal "running unprivileged, and /dev/kvm not writable"
115136
else
116-
sudo rm -f /dev/kvm
117-
sudo mknod /dev/kvm c 10 232
118-
sudo setfacl -m u:"$USER":rw /dev/kvm
137+
${SUDO} rm -f /dev/kvm
138+
${SUDO} mknod /dev/kvm c 10 232
139+
${SUDO} setfacl -m u:"$USER":rw /dev/kvm
119140
fi
120141
fi
121142
fi
@@ -567,10 +588,10 @@ runcompose_tree() {
567588
set - "$@" --repo "${repo}" --write-composejson-to "${composejson}"
568589
# we hardcode a umask of 0022 here to make sure that composes are run
569590
# with a consistent value, regardless of the environment
570-
(umask 0022 && sudo -E "$@")
571-
sudo chown -R -h "${USER}":"${USER}" "${tmprepo}"
591+
(umask 0022 && ${SUDO_W_ENV} -E "$@")
592+
${SUDO} chown -R -h "${USER}":"${USER}" "${tmprepo}"
572593
if [ -f "${composejson}" ]; then
573-
sudo chown "${USER}":"${USER}" "${composejson}"
594+
${SUDO} chown "${USER}":"${USER}" "${composejson}"
574595
fi
575596
else
576597
runvm_with_cache -- "$@" --repo "${repo}" --write-composejson-to "${composejson}"

0 commit comments

Comments
 (0)