Skip to content

Commit e838237

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 e838237

File tree

3 files changed

+29
-9
lines changed

3 files changed

+29
-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: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,26 @@ 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+
set -e
19+
$*
20+
}
21+
export -f fake-sudo
22+
# 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.
23+
# 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.
24+
alias sudo='fake-sudo'
25+
alias sudo_w_env='fake-sudo'
26+
fi
27+
828
info() {
929
echo "info: $*" 1>&2
1030
}
@@ -113,9 +133,9 @@ preflight_kvm() {
113133
if ! has_privileges; then
114134
fatal "running unprivileged, and /dev/kvm not writable"
115135
else
116-
sudo rm -f /dev/kvm
117-
sudo mknod /dev/kvm c 10 232
118-
sudo setfacl -m u:"$USER":rw /dev/kvm
136+
${SUDO} rm -f /dev/kvm
137+
${SUDO} mknod /dev/kvm c 10 232
138+
${SUDO} setfacl -m u:"$USER":rw /dev/kvm
119139
fi
120140
fi
121141
fi
@@ -567,10 +587,10 @@ runcompose_tree() {
567587
set - "$@" --repo "${repo}" --write-composejson-to "${composejson}"
568588
# we hardcode a umask of 0022 here to make sure that composes are run
569589
# with a consistent value, regardless of the environment
570-
(umask 0022 && sudo -E "$@")
571-
sudo chown -R -h "${USER}":"${USER}" "${tmprepo}"
590+
(umask 0022 && ${SUDO_W_ENV} -E "$@")
591+
${SUDO} chown -R -h "${USER}":"${USER}" "${tmprepo}"
572592
if [ -f "${composejson}" ]; then
573-
sudo chown "${USER}":"${USER}" "${composejson}"
593+
${SUDO} chown "${USER}":"${USER}" "${composejson}"
574594
fi
575595
else
576596
runvm_with_cache -- "$@" --repo "${repo}" --write-composejson-to "${composejson}"

0 commit comments

Comments
 (0)