Debian Codegraph @

src/tests/lxc-test-unpriv

raw
1 #!/bin/bash
2
3 # SPDX-License-Identifier: LGPL-2.1+
4
5 # lxc: linux Container library
6 #
7 # This is a test script for unprivileged containers
8 #
9 # This test assumes an Ubuntu host
10
11 if [ $(id -u) -ne 0 ]; then
12 echo "ERROR: Must run as root."
13 exit 1
14 fi
15
16 # Test if we're using an overlayfs module that handles symlinks correctly. If
17 # not, we skip these tests since overlay clones will not work correctly.
18 if modprobe -q overlayfs; then
19 TMPDIR=$(mktemp -d)
20
21 MOUNTDIR="${TMPDIR}/ovl_symlink_test"
22
23 mkdir ${MOUNTDIR}
24
25 mount -t tmpfs none ${MOUNTDIR}
26
27 mkdir ${MOUNTDIR}/{lowerdir,upperdir,workdir,overlayfs}
28 mount -t overlayfs -o lowerdir="${MOUNTDIR}/lowerdir",upperdir="${MOUNTDIR}/upperdir",workdir="${MOUNTDIR}/workdir" none "${MOUNTDIR}/overlayfs"
29
30 CORRECT_LINK_TARGET="${MOUNTDIR}/overlayfs/placeholder_file"
31 exec 9> "${CORRECT_LINK_TARGET}"
32
33 DETECTED_LINK_TARGET=$(readlink -q /proc/$$/fd/9)
34
35 # cleanup
36 exec 9>&-
37
38 umount "${MOUNTDIR}/overlayfs"
39 umount ${MOUNTDIR}
40
41 rmdir ${MOUNTDIR}
42
43 # This overlay module does not correctly handle symlinks, so skip the
44 # tests.
45 if [ "${DETECTED_LINK_TARGET}" != "${CORRECT_LINK_TARGET}" ]; then
46 exit 0
47 fi
48 fi
49
50 command -v newuidmap >/dev/null 2>&1 || { echo "'newuidmap' command is missing" >&2; exit 1; }
51
52 DONE=0
53 UNPRIV_LOG=$(mktemp --dry-run)
54 cleanup() {
55 cd /
56
57 if [ $DONE -eq 0 ]; then
58 cat "${UNPRIV_LOG}"
59 fi
60 rm -f "${UNPRIV_LOG}" || true
61
62 run_cmd lxc-stop -n c2 -k -l trace -o "${UNPRIV_LOG}" || true
63 run_cmd lxc-stop -n c1 -k -l trace -o "${UNPRIV_LOG}" || true
64 pkill -u $(id -u $TUSER) -9 || true
65
66 sed -i '/lxcunpriv/d' /run/lxc/nics /etc/lxc/lxc-usernet
67 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
68
69 rm -Rf $HDIR /run/user/$(id -u $TUSER) || true
70
71 deluser $TUSER
72
73 if [ $DONE -eq 0 ]; then
74 echo "FAIL"
75 exit 1
76 fi
77
78 echo "PASS"
79 }
80
81 run_cmd() {
82 sudo -i -u $TUSER \
83 env http_proxy=${http_proxy:-} https_proxy=${https_proxy:-} \
84 XDG_RUNTIME_DIR=/run/user/$(id -u $TUSER) ASAN_OPTIONS=${ASAN_OPTIONS:-} \
85 UBSAN_OPTIONS=${UBSAN_OPTIONS:-} $*
86 }
87
88 # create a test user
89 TUSER=lxcunpriv
90 HDIR=/home/$TUSER
91
92 trap cleanup EXIT SIGHUP SIGINT SIGTERM
93 set -eux
94
95 id $TUSER &> /dev/null && deluser -q --remove-home $TUSER
96 useradd $TUSER
97
98 mkdir -p $HDIR
99 echo "$TUSER veth lxcbr0 2" >> /etc/lxc/lxc-usernet
100 sed -i '/^lxcunpriv:/d' /etc/subuid /etc/subgid
101
102 usermod -v 910000-919999 -w 910000-919999 $TUSER
103
104 mkdir -p $HDIR/.config/lxc/
105 cat > $HDIR/.config/lxc/default.conf << EOF
106 lxc.net.0.type = veth
107 lxc.net.0.link = lxcbr0
108 lxc.idmap = u 0 910000 9999
109 lxc.idmap = g 0 910000 9999
110 EOF
111 chown -R $TUSER: $HDIR
112
113 mkdir -p /run/user/$(id -u $TUSER)
114 chown -R $TUSER: /run/user/$(id -u $TUSER)
115
116 cd $HDIR
117
118 run_cmd lxc-create -t busybox -n c1 -l trace -o "${UNPRIV_LOG}"
119
120 # Make sure we can start it - twice
121
122 for count in $(seq 1 2); do
123 run_cmd lxc-start -n c1 -d -l trace -o "${UNPRIV_LOG}"
124
125 p1=$(run_cmd lxc-info -n c1 -p -H -l trace -o "${UNPRIV_LOG}")
126 [ "$p1" != "-1" ] || { echo "Failed to start container c1 (run $count)"; false; }
127
128 run_cmd lxc-info -n c1 -l trace -o "${UNPRIV_LOG}"
129 run_cmd lxc-attach -n c1 -l trace -o "${UNPRIV_LOG}" -- /bin/true
130
131 run_cmd lxc-stop -n c1 -k -l trace -o "${UNPRIV_LOG}"
132 done
133
134 run_cmd lxc-copy -s -n c1 -N c2 -l trace -o "${UNPRIV_LOG}"
135 run_cmd lxc-start -n c2 -d -l trace -o "${UNPRIV_LOG}"
136 p1=$(run_cmd lxc-info -n c2 -p -H -l trace -o "${UNPRIV_LOG}")
137 [ "$p1" != "-1" ] || { echo "Failed to start container c2"; false; }
138
139 run_cmd lxc-stop -n c2 -k -l trace -o "${UNPRIV_LOG}"
140
141 if command -v cgm >/dev/null 2>&1; then
142 echo "Testing containers under different cgroups per subsystem"
143 run_cmd cgm create freezer x1/x2
144 cgm movepid freezer x1 $$
145 run_cmd lxc-start -n c1 -d -l trace -o "${UNPRIV_LOG}"
146 p1=$(run_cmd lxc-info -n c1 -p -H -l trace -o "${UNPRIV_LOG}")
147 [ "$p1" != "-1" ] || { echo "Failed to start container c1"; false; }
148 run_cmd lxc-info -n c1 -l trace -o "${UNPRIV_LOG}"
149 run_cmd lxc-attach -n c1 -l trace -o "${UNPRIV_LOG}" -- /bin/true
150 run_cmd lxc-cgroup -n c1 freezer.state -l trace -o "${UNPRIV_LOG}"
151
152 echo "Testing lxc-attach and lxc-cgroup from different cgroup"
153 cgm movepid freezer x2 $$
154 run_cmd lxc-attach -n c1 -l trace -o "${UNPRIV_LOG}" -- /bin/true
155 run_cmd lxc-cgroup -n c1 -l trace -o "${UNPRIV_LOG}" freezer.state
156 run_cmd lxc-cgroup -n c1 -l trace -o "${UNPRIV_LOG}" memory.limit_in_bytes
157 fi
158
159 DONE=1