| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# SPDX-License-Identifier: LGPL-2.1+ |
| 4 |
|
| 5 |
# |
| 6 |
# This is a bash test case to test lxc-usernsexec. |
| 7 |
# It basically supports usring lxc-usernsexec to execute itself |
| 8 |
# and then create files and check that their ownership is as expected. |
| 9 |
# |
| 10 |
# It requires that the current user has at least 1 value in subuid and /etc/subgid |
| 11 |
TEMP_D="" |
| 12 |
VERBOSITY=0 |
| 13 |
set -f |
| 14 |
|
| 15 |
fail() { echo "$@" 1>&2; exit 1; } |
| 16 |
error() { echo "$@" 1>&2; } |
| 17 |
skip() { |
| 18 |
error "SKIP:" "$@" |
| 19 |
exit 0 |
| 20 |
} |
| 21 |
debug() { |
| 22 |
local level=${1}; shift; |
| 23 |
[ "${level}" -gt "${VERBOSITY}" ] && return |
| 24 |
error "${@}" |
| 25 |
} |
| 26 |
|
| 27 |
collect_owners() { |
| 28 |
# collect_owners([--dir=dir], file1, file2 ...) |
| 29 |
# set _RET to a space delimited array of |
| 30 |
# <file1>:owner:group <file2>:owner:group ... |
| 31 |
local out="" ret="" dir="" |
| 32 |
if [ "${1#--dir=}" != "$1" ]; then |
| 33 |
dir="${1#--dir=}" |
| 34 |
shift |
| 35 |
fi |
| 36 |
for arg in "$@"; do |
| 37 |
# drop the :* so that input can be same as touch_files. |
| 38 |
out=$(stat --format "%n:%u:%g" "${dir}${arg}") || { |
| 39 |
error "failed to stat ${arg}" |
| 40 |
return 1; |
| 41 |
} |
| 42 |
ret="$ret ${out##*/}" |
| 43 |
done |
| 44 |
_RET="${ret# }" |
| 45 |
} |
| 46 |
|
| 47 |
cleanup() { |
| 48 |
if [ -d "$TEMP_D" ]; then |
| 49 |
rm -Rf "$TEMP_D" |
| 50 |
fi |
| 51 |
} |
| 52 |
|
| 53 |
touch_files() { |
| 54 |
# touch_files tok [tok ...] |
| 55 |
# tok is filename:chown_id:chown_gid |
| 56 |
# if chown_id or chown_gid is empty, then chown will do the right thing |
| 57 |
# and only change the provided value. |
| 58 |
local args="" tok="" fname="" uidgid="" |
| 59 |
args=( "$@" ) |
| 60 |
for tok in "$@"; do |
| 61 |
fname=${tok%%:*} |
| 62 |
uidgid=${tok#$fname} |
| 63 |
uidgid=${uidgid#:} |
| 64 |
: > "$fname" || { error "failed to create $fname"; return 1; } |
| 65 |
[ -z "$uidgid" ] && continue |
| 66 |
chown $uidgid "$fname" || { error "failed to chmod '$uidgid' $fname ($?)"; return 1; } |
| 67 |
done |
| 68 |
} |
| 69 |
|
| 70 |
inside_cleanup() { |
| 71 |
local f="" |
| 72 |
rm -f "${FILES[@]}" |
| 73 |
echo "$STATUS" >&5 |
| 74 |
echo "$STATUS" >&6 |
| 75 |
} |
| 76 |
|
| 77 |
set_files() { |
| 78 |
local x="" |
| 79 |
FILES=( ) |
| 80 |
for x in "$@"; do |
| 81 |
FILES[${#FILES[@]}]="${x%%:*}" |
| 82 |
done |
| 83 |
} |
| 84 |
|
| 85 |
inside() { |
| 86 |
# this what gets run inside the usernsexec environment. |
| 87 |
# basically expects arguments of <filename>:uid:gid |
| 88 |
# it will create the file, and then chmod it to the provided uid:gid |
| 89 |
# it writes to file descriptor 5 a single line with space delimited |
| 90 |
# exit_value uid gid [<filename>:<owner>:<group> ... ] |
| 91 |
STATUS=127 |
| 92 |
trap inside_cleanup EXIT |
| 93 |
local uid="" gid="" x="" |
| 94 |
|
| 95 |
uid=$(id -u) || fail "failed execution of id -u" |
| 96 |
gid=$(id -g) || fail "failed execution of id -g" |
| 97 |
|
| 98 |
set_files "$@" |
| 99 |
|
| 100 |
touch_files "$@" || fail "failed to create files" |
| 101 |
|
| 102 |
collect_owners "${FILES[@]}" || fail "failed to collect owners" |
| 103 |
result="$_RET" |
| 104 |
|
| 105 |
# tell caller we are done. |
| 106 |
echo "0" "$uid" "$gid" "$result" >&5 |
| 107 |
STATUS=0 |
| 108 |
|
| 109 |
# let the caller do things while the files are around. |
| 110 |
read -t 30 x <&6 |
| 111 |
|
| 112 |
exit |
| 113 |
} |
| 114 |
|
| 115 |
runtest() { |
| 116 |
# runtest(mydir, nsexec_args, [inside [...]]) |
| 117 |
# - use 'mydir' as a working dir. |
| 118 |
# - execute lxc-usernsexec $nsexec_args -- <self> inside <inside args> |
| 119 |
# |
| 120 |
# write to stdout |
| 121 |
# exit_value inside_exit_value inside_uid:inside_gid <results> |
| 122 |
# |
| 123 |
# where results are a list of space separated |
| 124 |
# filename:uid:gid |
| 125 |
# for each file passed in inside_args |
| 126 |
[ $# -ge 3 ] || { error "runtest expects 2 args"; return 1; } |
| 127 |
local mydir="$1" nsexec_args="$2" |
| 128 |
shift 2 |
| 129 |
local ret inside_owners t="" |
| 130 |
KIDPID="" |
| 131 |
|
| 132 |
mkfifo "${mydir}/5" && exec 5<>"${mydir}/5" || return |
| 133 |
mkfifo "${mydir}/6" && exec 6<>"${mydir}/6" || return |
| 134 |
mkdir --mode=777 "${mydir}/work" || return |
| 135 |
cd "${mydir}/work" |
| 136 |
|
| 137 |
set_files "$@" |
| 138 |
|
| 139 |
local results="" oresults="" iresults="" iuid="" igid="" n=0 |
| 140 |
|
| 141 |
error "$" $USERNSEXEC ${nsexec_args} -- "$MYPATH" inside "$*" |
| 142 |
${USERNSEXEC} ${nsexec_args} -- "$MYPATH" inside "$@" & |
| 143 |
KIDPID=$! |
| 144 |
|
| 145 |
[ -d "/proc/$KIDPID" ] || { |
| 146 |
wait $KIDPID |
| 147 |
fail "kid $KIDPID died quickly $?" |
| 148 |
} |
| 149 |
|
| 150 |
# if lxc-usernsexec fails to execute MYPATH inside, then |
| 151 |
# the read below would timeout. To avoid a long timeout, |
| 152 |
# we do a short timeout and check the pid is alive. |
| 153 |
while ! read -t 1 ret iuid igid inside_owners <&5; do |
| 154 |
n=$((n+1)) |
| 155 |
if [ ! -d "/proc/$KIDPID" ]; then |
| 156 |
wait $KIDPID |
| 157 |
fail "kid $KIDPID is gone $?" |
| 158 |
fi |
| 159 |
[ $n -ge 30 ] && fail "child never wrote to pipe" |
| 160 |
done |
| 161 |
iresults=( $inside_owners ) |
| 162 |
|
| 163 |
collect_owners "--dir=${mydir}/work/" "${FILES[@]}" || return |
| 164 |
oresults=( $_RET ) |
| 165 |
|
| 166 |
echo 0 >&6 |
| 167 |
wait |
| 168 |
|
| 169 |
ret=$? |
| 170 |
|
| 171 |
results=( ) |
| 172 |
for((i=0;i<${#iresults[@]};i++)); do |
| 173 |
results[$i]="${oresults[$i]}:${iresults[$i]#*:}" |
| 174 |
done |
| 175 |
|
| 176 |
echo 0 $ret "$iuid:$igid" "${results[@]}" |
| 177 |
} |
| 178 |
|
| 179 |
runcheck() { |
| 180 |
local name="$1" expected="$2" nsexec_args="$3" found="" |
| 181 |
shift 3 |
| 182 |
mkdir "${TEMP_D}/$name" || fail "failed mkdir <TEMP_D>/$name.d" |
| 183 |
local err="${TEMP_D}/$name.err" |
| 184 |
out=$("$MYPATH" runtest "${TEMP_D}/$name" "$nsexec_args" "$@" 2>"$err") || { |
| 185 |
error "$name: FAIL - runtest failed $?" |
| 186 |
[ -n "$out" ] && error " $out" |
| 187 |
sed 's,^, ,' "$err" 1>&2 |
| 188 |
ERRORS="${ERRORS} $name" |
| 189 |
return 1 |
| 190 |
} |
| 191 |
set -- $out |
| 192 |
local parentrc=$1 kidrc=$2 iuidgid="$3" found="" |
| 193 |
shift 3 |
| 194 |
found="$*" |
| 195 |
[ "$parentrc" = "0" -a "$kidrc" = "0" ] || { |
| 196 |
error "$name: FAIL - parentrc=$parentrc kidrc=$kidrc found=$found" |
| 197 |
ERRORS="${ERRORS} $name" |
| 198 |
return 1 |
| 199 |
} |
| 200 |
[ "$expected" = "$found" ] && { |
| 201 |
error "$name: PASS" |
| 202 |
PASS="${PASSES} $name" |
| 203 |
return 0 |
| 204 |
} |
| 205 |
echo "$name: FAIL expected '$expected' != found '$found'" |
| 206 |
FAILS="${FAILS} $name" |
| 207 |
return 1 |
| 208 |
} |
| 209 |
|
| 210 |
setup_Usage() { |
| 211 |
cat <<EOF |
| 212 |
${0} setup_and_run [-- run-args] |
| 213 |
|
| 214 |
setup the system by creating a user (default is '${asuser:-test-userns}') |
| 215 |
and then run test as that user. Must be root. |
| 216 |
|
| 217 |
If user exists, then do not create the user. |
| 218 |
|
| 219 |
-v | --verbose - be more verbose |
| 220 |
--create-subuid=UID:RANGE |
| 221 |
--create-subgid=UID:RANGE if adding subuid/subgid use this START:RANGE |
| 222 |
example (default) 3000000000:5 |
| 223 |
EOF |
| 224 |
} |
| 225 |
|
| 226 |
setup_and_run() { |
| 227 |
local short_opts="hv" |
| 228 |
local long_opts="help,user:,create-subuid:,create-subgid:,verbose" |
| 229 |
local getopt_out="" |
| 230 |
getopt_out=$(getopt --name "${0##*/}" \ |
| 231 |
--options "${short_opts}" --long "${long_opts}" -- "$@") && |
| 232 |
eval set -- "${getopt_out}" || |
| 233 |
{ bad_Usage; return; } |
| 234 |
|
| 235 |
local cur="" next="" asuser="test-userns" |
| 236 |
local create_subuid="3000000000:5" create_subgid="3000000000:5" |
| 237 |
while [ $# -ne 0 ]; do |
| 238 |
cur="$1"; next="$2"; |
| 239 |
case "$cur" in |
| 240 |
-h|--help) setup_Usage ; exit 0;; |
| 241 |
--user) asuser="$next"; shift;; |
| 242 |
--create-subuid) create_subuid=$next; shift;; |
| 243 |
--create-subgid) create_subgid=$next; shift;; |
| 244 |
-v|--verbose) VERBOSITY=$((${VERBOSITY}+1));; |
| 245 |
--) shift; break;; |
| 246 |
esac |
| 247 |
shift; |
| 248 |
done |
| 249 |
|
| 250 |
local pt_args="" |
| 251 |
pt_args=( "$@" ) |
| 252 |
|
| 253 |
if [ "$(id -u)" != "0" ]; then |
| 254 |
error "Sorry, setup_and_run has to be done as root, not uid=$(id -u)" |
| 255 |
return 1 |
| 256 |
fi |
| 257 |
|
| 258 |
local home="/home/$asuser" |
| 259 |
if [ ! -d "$home" ]; then |
| 260 |
debug 1 "creating user $asuser" |
| 261 |
useradd "$asuser" --create-home "--home-dir=$home" || { |
| 262 |
error "failed to create $asuser" |
| 263 |
return 1 |
| 264 |
} |
| 265 |
else |
| 266 |
debug 1 "$asuser existed" |
| 267 |
fi |
| 268 |
|
| 269 |
local subuid="" subgid="" |
| 270 |
subuid=$(awk -F: '$1 == n { print $2; exit(0); }' "n=$asuser" /etc/subuid) || { |
| 271 |
error "failed to read /etc/subuid for $asuser" |
| 272 |
return 1 |
| 273 |
} |
| 274 |
|
| 275 |
if [ -n "$subuid" ]; then |
| 276 |
debug 1 "$asuser already had subuid=$subuid" |
| 277 |
else |
| 278 |
debug 1 "adding $asuser:$create_subuid to /etc/subuid" |
| 279 |
echo "$asuser:$create_subuid" >> /etc/subuid || { |
| 280 |
error "failed to add $asuser to /etc/subuid" |
| 281 |
} |
| 282 |
fi |
| 283 |
|
| 284 |
subgid=$(awk -F: '$1 == n { print $2; exit(0); }' "n=$asuser" /etc/subgid) || { |
| 285 |
error "failed to read /etc/subgid for $asuser" |
| 286 |
return 1 |
| 287 |
} |
| 288 |
if [ -n "$subgid" ]; then |
| 289 |
debug 1 "$asuser already had subgid=$subgid" |
| 290 |
else |
| 291 |
debug 1 "adding $asuser:$create_subgid to /etc/subgid" |
| 292 |
echo "$asuser:$create_subgid" >> /etc/subgid || { |
| 293 |
error "failed to add $asuser to /etc/subgid" |
| 294 |
} |
| 295 |
fi |
| 296 |
|
| 297 |
debug 0 "as $asuser executing ${MYPATH} ${pt_args[*]}" |
| 298 |
sudo -Hu "$asuser" ASAN_OPTIONS=${ASAN_OPTIONS:-} UBSAN_OPTIONS=${UBSAN_OPTIONS:-} "${MYPATH}" "${pt_args[@]}" |
| 299 |
} |
| 300 |
|
| 301 |
USERNSEXEC=${USERNSEXEC:-lxc-usernsexec} |
| 302 |
MYPATH=$(readlink -f "$0") || { echo "failed to get full path to self: $0"; exit 1; } |
| 303 |
export MYPATH |
| 304 |
|
| 305 |
if [ "$1" = "inside" ]; then |
| 306 |
shift |
| 307 |
inside "$@" |
| 308 |
exit |
| 309 |
elif [ "$1" = "runtest" ]; then |
| 310 |
shift |
| 311 |
runtest "$@" |
| 312 |
exit |
| 313 |
elif [ "$1" = "setup_and_run" ]; then |
| 314 |
shift |
| 315 |
setup_and_run "$@" |
| 316 |
exit |
| 317 |
fi |
| 318 |
|
| 319 |
name=$(id --user --name) || fail "failed to get username" |
| 320 |
if [ "$name" = "root" ]; then |
| 321 |
setup_and_run "$@" |
| 322 |
exit |
| 323 |
fi |
| 324 |
|
| 325 |
subuid=$(awk -F: '$1 == n { print $2; exit(0); }' "n=$name" /etc/subuid) && |
| 326 |
[ -n "$subuid" ] || fail "did not find $name in /etc/subuid" |
| 327 |
|
| 328 |
subgid=$(awk -F: '$1 == n { print $2; exit(0); }' "n=$name" /etc/subgid) && |
| 329 |
[ -n "$subgid" ] || fail "did not find $name in /etc/subgid" |
| 330 |
|
| 331 |
|
| 332 |
uid=$(id --user) || fail "failed to get uid" |
| 333 |
gid=$(id --group) || fail "failed to get gid" |
| 334 |
|
| 335 |
mapuid="u:0:$uid:1" |
| 336 |
mapgid="g:0:$gid:1" |
| 337 |
|
| 338 |
ver=$(dpkg-query --show lxc-utils | awk '{print $2}') |
| 339 |
error "uid=$uid gid=$gid name=$name subuid=$subuid subgid=$subgid ver=$ver" |
| 340 |
error "lxc-utils=$ver kver=$(uname -r)" |
| 341 |
error "USERNSEXEC=$USERNSEXEC" |
| 342 |
|
| 343 |
TEMP_D=$(mktemp -d) |
| 344 |
trap cleanup EXIT |
| 345 |
|
| 346 |
PASSES=""; FAILS=""; ERRORS="" |
| 347 |
runcheck nouidgid "f0:$subuid:$subgid:0:0" "" f0 |
| 348 |
|
| 349 |
runcheck myuidgid "f0:$uid:$gid:0:0" \ |
| 350 |
"-m$mapuid -m$mapgid" f0 |
| 351 |
|
| 352 |
runcheck subuidgid \ |
| 353 |
"f0:$subuid:$subgid:0:0" \ |
| 354 |
"-mu:0:$subuid:1 -mg:0:$subgid:1" f0:0:0 |
| 355 |
|
| 356 |
runcheck bothsets "f0:$uid:$gid:0:0 f1:$subuid:$subgid:1:1 f2:$uid:$subgid:0:1" \ |
| 357 |
"-m$mapuid -m$mapgid -mu:1:$subuid:1 -mg:1:$subgid:1" \ |
| 358 |
f0 f1:1:1 f2::1 |
| 359 |
|
| 360 |
runcheck mismatch "f0:$uid:$subgid:0:0 f1:$subuid:$gid:15:31" \ |
| 361 |
"-mu:0:$uid:1 -mg:0:$subgid:1 -mu:15:$subuid:1 -mg:31:$gid:1" \ |
| 362 |
f0 f1:15:31 |
| 363 |
|
| 364 |
FAILS=${FAILS# } |
| 365 |
ERRORS=${ERRORS# } |
| 366 |
PASSES=${PASSES# } |
| 367 |
|
| 368 |
[ -z "${FAILS}" ] || error "FAILS: ${FAILS}" |
| 369 |
[ -z "${ERRORS}" ] || error "ERRORS: ${ERRORS}" |
| 370 |
[ -z "${FAILS}" -a -z "${ERRORS}" ] || exit 1 |
| 371 |
exit 0 |