| 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 PR_SET_NO_NEW_PRIVS |
| 8 | |
| 9 | set -eux |
| 10 | |
| 11 | DONE=0 |
| 12 | cleanup() { |
| 13 | cd / |
| 14 | lxc-destroy -n c1 -f || true |
| 15 | if [ $DONE -eq 0 ]; then |
| 16 | echo "FAIL" |
| 17 | exit 1 |
| 18 | fi |
| 19 | echo "PASS" |
| 20 | } |
| 21 | |
| 22 | trap cleanup EXIT SIGHUP SIGINT SIGTERM |
| 23 | |
| 24 | if [ ! -d /etc/lxc ]; then |
| 25 | mkdir -p /etc/lxc/ |
| 26 | cat > /etc/lxc/default.conf << EOF |
| 27 | lxc.net.0.type = veth |
| 28 | lxc.net.0.link = lxcbr0 |
| 29 | EOF |
| 30 | fi |
| 31 | |
| 32 | lxc-create -t busybox -n c1 |
| 33 | echo "lxc.no_new_privs = 1" >> /var/lib/lxc/c1/config |
| 34 | |
| 35 | lxc-start -n c1 |
| 36 | p1=$(lxc-info -n c1 -p -H) |
| 37 | [ "$p1" != "-1" ] || { echo "Failed to start container c1"; false; } |
| 38 | |
| 39 | lxc-attach -n c1 --clear-env -- mkdir -p /home/ubuntu |
| 40 | lxc-attach -n c1 --clear-env -- /bin/sh -c "cat <<EOF >> /etc/passwd |
| 41 | ubuntu:x:1000:1000:ubuntu:/home/ubuntu:/bin/sh |
| 42 | EOF" |
| 43 | |
| 44 | # Check that lxc-attach obeys PR_SET_NO_NEW_PRIVS when it is set. |
| 45 | ! lxc-attach -n c1 --clear-env --uid 1000 --gid 1000 -- ping -c 1 127.0.0.1 || { echo "Managed to ping localhost"; false; } |
| 46 | lxc-stop -n c1 -k |
| 47 | |
| 48 | # Check that lxc-attach obeys PR_SET_NO_NEW_PRIVS when it is not set. |
| 49 | sed -i 's/lxc.no_new_privs = 1/lxc.no_new_privs = 0/' /var/lib/lxc/c1/config |
| 50 | lxc-start -n c1 |
| 51 | lxc-attach -n c1 --clear-env --uid 1000 --gid 1000 -- ping -c 1 127.0.0.1 || { echo "Managed to ping localhost"; false; } |
| 52 | lxc-stop -n c1 -k |
| 53 | |
| 54 | DONE=1 |