| 1 |
# Check privilige drop: UID/GID changes, chdir, environment, and file |
| 2 |
# descriptors |
| 3 |
|
| 4 |
# ensure failure to drop privileges doesn't retain root privileges |
| 5 |
sed -ri 's/^#(user|group)\s*=\s*$/\1 = nonexistent-\1/' /etc/lacme/lacme.conf |
| 6 |
! lacme account 2>"$STDERR" || fail |
| 7 |
grepstderr -Fxq "getgrnam(nonexistent-group)" |
| 8 |
grepstderr -Fxq "Error: Invalid client version" |
| 9 |
|
| 10 |
sed -ri 's/^group\s*=\s*nonexistent.*/#&/' /etc/lacme/lacme.conf |
| 11 |
! lacme account 2>"$STDERR" || fail |
| 12 |
grepstderr -Fxq "getpwnam(nonexistent-user)" |
| 13 |
grepstderr -Fxq "Error: Invalid client version" |
| 14 |
|
| 15 |
# create wrapper to inspect processes |
| 16 |
STATUSDIR="/dev/shm/lacme-wrap" |
| 17 |
install -oroot -groot -m0755 /dev/stdin /run/lacme-wrap <<-EOF |
| 18 |
#!/bin/sh |
| 19 |
set -ue |
| 20 |
PATH="/usr/bin:/bin" |
| 21 |
export PATH |
| 22 |
|
| 23 |
prefix="$STATUSDIR/\${1##*[/-]}" |
| 24 |
cat </proc/\$\$/status >"\$prefix/status" |
| 25 |
pwd >"\$prefix/cwd" |
| 26 |
stat -c "%U:%G %#a" . >"\$prefix/cwd-mod" |
| 27 |
sort -z </proc/\$\$/environ | tr "\\0" "\\n" >"\$prefix/environ" |
| 28 |
( find -P /proc/\$\$/fd -mindepth 1 \! -lname "\$0" -printf "%P %#m %U:%G %l\\n" >"\$prefix/fd" ) |
| 29 |
|
| 30 |
exec "\$@" |
| 31 |
EOF |
| 32 |
|
| 33 |
# also check privilege drop for the spawned accountd |
| 34 |
adduser --system --group \ |
| 35 |
--home /nonexistent --no-create-home \ |
| 36 |
--gecos "lacme account user" \ |
| 37 |
--quiet lacme-account |
| 38 |
sed -ri 's/^#?(user|group)\s*=\s*nonexistent.*/\1 = lacme-account/' /etc/lacme/lacme.conf |
| 39 |
chown lacme-account: /etc/lacme/account.key |
| 40 |
|
| 41 |
install -oroot -groot -dm0755 -- "$STATUSDIR" |
| 42 |
install -olacme-account -groot -dm0700 -- "$STATUSDIR/accountd" |
| 43 |
install -o_lacme-client -groot -dm0700 -- "$STATUSDIR/client" |
| 44 |
install -o_lacme-www -groot -dm0700 -- "$STATUSDIR/webserver" |
| 45 |
|
| 46 |
# test with a group that's not the primary group (nogroup) of _lacme-www etc |
| 47 |
addgroup --system nogroup2 |
| 48 |
sed -ri 's|^#?group\s*=\s*nogroup$|group = nogroup2|' /etc/lacme/lacme.conf |
| 49 |
sed -ri 's|^#?command\s*=.*/lacme-accountd$|command = /run/lacme-wrap /usr/bin/lacme-accountd|' /etc/lacme/lacme.conf |
| 50 |
sed -ri 's|^#?command\s*=.*/lacme/client$|command = /run/lacme-wrap /usr/libexec/lacme/client|' /etc/lacme/lacme.conf |
| 51 |
sed -ri 's|^#?command\s*=.*/lacme/webserver$|command = /run/lacme-wrap /usr/libexec/lacme/webserver|' /etc/lacme/lacme.conf |
| 52 |
sed -ri 's|^#?config\s*=\s*$|config = /etc/lacme/lacme-accountd.conf|' /etc/lacme/lacme.conf |
| 53 |
|
| 54 |
check_status() { |
| 55 |
local path="$STATUSDIR/$1/status" user="$2" group="$3" |
| 56 |
UID="$(getent passwd "$user" | cut -sd: -f3)" |
| 57 |
GID="$(getent group "$group" | cut -sd: -f3)" |
| 58 |
[ -n "$UID" -a -n "$GID" ] || return 1 |
| 59 |
grep -Ex "Uid:\\s+$UID\\s+$UID\\s+$UID\\s+$UID" "$path" || return 1 |
| 60 |
grep -Ex "Gid:\\s+$GID\\s+$GID\\s+$GID\\s+$GID" "$path" || return 1 |
| 61 |
grep -Ex "Groups:\s+$GID\s*" "$path" || return 1 |
| 62 |
} |
| 63 |
check_cwd() { |
| 64 |
local path="$STATUSDIR/$1/cwd" dir="$2" cwd |
| 65 |
cwd="$(cat <"$path")" || return 1 |
| 66 |
[ "$cwd" = "$dir" ] || return 1 |
| 67 |
} |
| 68 |
|
| 69 |
check_accountd() { |
| 70 |
local socket_ino stderr prefix="$STATUSDIR/accountd" |
| 71 |
check_status accountd lacme-account lacme-account || return 1 |
| 72 |
check_cwd accountd / || return 1 |
| 73 |
|
| 74 |
diff --label="a/accountd/environ" --label="b/accountd/environ" \ |
| 75 |
--color=auto --unified "$prefix/environ" - <<-EOF |
| 76 |
HOME=/nonexistent |
| 77 |
LOGNAME=lacme-account |
| 78 |
PATH=/usr/bin:/bin |
| 79 |
SHELL=/usr/sbin/nologin |
| 80 |
TERM=$TERM |
| 81 |
USER=lacme-account |
| 82 |
EOF |
| 83 |
|
| 84 |
stderr="$(readlink -e "/proc/$$/fd/2")" |
| 85 |
socket_ino="$(sed -rn '/^0 .* socket:\[([0-9]+)\]$/ {s//\1/p;q}' "$prefix/fd")" |
| 86 |
[ -n "$socket_ino" ] || return 1 |
| 87 |
grep -Fxq "0 0700 $UID:$GID socket:[$socket_ino]" "$prefix/fd" || return 1 |
| 88 |
grep -Fxq "1 0700 $UID:$GID socket:[$socket_ino]" "$prefix/fd" || return 1 |
| 89 |
grep -Fxq "2 0700 $UID:$GID $stderr" "$prefix/fd" || return 1 |
| 90 |
sed -ri '\#^[012] #d' "$prefix/fd" |
| 91 |
! test -s "$prefix/fd" || return 1 |
| 92 |
} |
| 93 |
check_client() { |
| 94 |
local command="$1" cwd="$2" UID GID stdout stderr prefix="$STATUSDIR/client" |
| 95 |
check_status client _lacme-client nogroup2 |
| 96 |
check_cwd client "$cwd" |
| 97 |
|
| 98 |
diff --label="a/client/environ" --label="b/client/environ" \ |
| 99 |
--color=auto --unified "$prefix/environ" - <<-EOF |
| 100 |
DEBUG=0 |
| 101 |
HOME=/nonexistent |
| 102 |
LOGNAME=_lacme-client |
| 103 |
PATH=/usr/bin:/bin |
| 104 |
SHELL=/usr/sbin/nologin |
| 105 |
TERM=$TERM |
| 106 |
USER=_lacme-client |
| 107 |
EOF |
| 108 |
|
| 109 |
stdout="$(readlink -e "/proc/$$/fd/1")" |
| 110 |
stderr="$(readlink -e "/proc/$$/fd/2")" |
| 111 |
if [ "$command" = "account" ]; then # no pipe |
| 112 |
grep -Fxq "0 0500 $UID:$GID /dev/null" "$prefix/fd" || return 1 |
| 113 |
grep -Fxq "1 0700 $UID:$GID $stdout" "$prefix/fd" || return 1 |
| 114 |
elif [ "$command" = "order" ]; then |
| 115 |
grep -Exq "0 0500 $UID:$GID pipe:\[[0-9]+\]" "$prefix/fd" || return 1 |
| 116 |
grep -Exq "1 0300 $UID:$GID pipe:\[[0-9]+\]" "$prefix/fd" || return 1 |
| 117 |
else |
| 118 |
exit 1 |
| 119 |
fi |
| 120 |
grep -Fxq "2 0700 $UID:$GID $stderr" "$prefix/fd" || return 1 |
| 121 |
sed -ri '\#^[012] #d' "$prefix/fd" |
| 122 |
|
| 123 |
grep -Exq "[0-9]+ 0700 $UID:$GID socket:\[[0-9]+\]" "$prefix/fd" || return 1 |
| 124 |
sed -ri '0,\#^[0-9]+ .* socket:\[[0-9]+\]$# {//d}' "$prefix/fd" |
| 125 |
|
| 126 |
grep -Eq "^[0-9]+ 0500 $UID:$GID /tmp/lacme-client.conf\.json-" "$prefix/fd" || return 1 |
| 127 |
sed -ri '0,\#^[0-9]+ .* /tmp/lacme-client.conf\.json-# {//d}' "$prefix/fd" |
| 128 |
! test -s "$prefix/fd" || return 1 |
| 129 |
} |
| 130 |
check_webserver() { |
| 131 |
local cwd="$1" UID GID stdout stderr prefix="$STATUSDIR/webserver" |
| 132 |
check_status webserver _lacme-www nogroup2 |
| 133 |
check_cwd webserver "$cwd" |
| 134 |
|
| 135 |
diff --label="a/webserver/environ" --label="b/webserver/environ" \ |
| 136 |
--color=auto --unified "$prefix/environ" - <<-EOF |
| 137 |
DEBUG=0 |
| 138 |
HOME=/nonexistent |
| 139 |
LOGNAME=_lacme-www |
| 140 |
PATH=/usr/bin:/bin |
| 141 |
SHELL=/usr/sbin/nologin |
| 142 |
TERM=$TERM |
| 143 |
USER=_lacme-www |
| 144 |
EOF |
| 145 |
|
| 146 |
stdout="$(readlink -e "/proc/$$/fd/1")" |
| 147 |
stderr="$(readlink -e "/proc/$$/fd/2")" |
| 148 |
grep -Fxq "0 0500 $UID:$GID /dev/null" "$prefix/fd" || return 1 |
| 149 |
grep -Fxq "1 0700 $UID:$GID $stdout" "$prefix/fd" || return 1 |
| 150 |
grep -Fxq "2 0700 $UID:$GID $stderr" "$prefix/fd" || return 1 |
| 151 |
sed -ri '\#^[012] #d' "$prefix/fd" |
| 152 |
|
| 153 |
grep -Exq "[0-9]+ 0700 $UID:$GID socket:\[[0-9]+\]" "$prefix/fd" || return 1 |
| 154 |
sed -ri '0,\#^[0-9]+ .* socket:\[[0-9]+\]$# {//d}' "$prefix/fd" |
| 155 |
! test -s "$prefix/fd" || return 1 |
| 156 |
} |
| 157 |
|
| 158 |
lacme account |
| 159 |
check_accountd |
| 160 |
check_client account / |
| 161 |
! test -e "$STATUSDIR/webserver/status" # account 'command' doesn't start the webserver |
| 162 |
|
| 163 |
lacme newOrder |
| 164 |
check_accountd |
| 165 |
challenge_dir="$(cat "$STATUSDIR/webserver/cwd")" |
| 166 |
[ "${challenge_dir#"/tmp/acme-challenge."}" != "$challenge_dir" ] || exit 1 |
| 167 |
check_client order "$challenge_dir" |
| 168 |
check_webserver "$challenge_dir" |
| 169 |
|
| 170 |
# the temporary challenge directory is created with permissive mode |
| 171 |
diff --label="a/webserver/cwd" --label="b/webserver/cwd" \ |
| 172 |
--color=auto --unified "$STATUSDIR/webserver/cwd-mod" - <<-EOF |
| 173 |
_lacme-client:root 0755 |
| 174 |
EOF |
| 175 |
|
| 176 |
# vim: set filetype=sh : |