| 1 | #!/bin/bashscip-shell filesystem . . `/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 | cleanupscip-shell shell shell-project 0.0.0 cleanup().() { |
| 13 | cd /scip-shell filesystem . . `/`. |
| 14 | lxc-destroy -n c1 -f || truescip-shell system . . true. |
| 15 | if [scip-shell system . . `[`. $DONE -eq 0 ]; then |
| 16 | echoscip-shell system . . echo. "FAIL" |
| 17 | exit 1 |
| 18 | fi |
| 19 | echoscip-shell system . . echo. "PASS" |
| 20 | } |
| 21 | |
| 22 | trap cleanup EXIT SIGHUP SIGINT SIGTERM |
| 23 | |
| 24 | if [scip-shell system . . `[`. ! -d /etc/lxcscip-shell filesystem . . `/etc/lxc`. ]; then |
| 25 | mkdirscip-shell system . . mkdir. -p /etc/lxc/scip-shell filesystem . . `/etc/lxc/`. |
| 26 | catscip-shell system . . cat. > /etc/lxc/default.confscip-shell filesystem . . `/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 | echoscip-shell system . . echo. "lxc.no_new_privs = 1" >> /var/lib/lxc/c1/configscip-shell filesystem . . `/var/lib/lxc/c1/config`. |
| 34 | |
| 35 | lxc-start -n c1 |
| 36 | p1=$(lxc-info -n c1 -p -H) |
| 37 | [scip-shell system . . `[`. "$p1" != "-1" ] || { echoscip-shell system . . echo. "Failed to start container c1"; falsescip-shell system . . false.; } |
| 38 | |
| 39 | lxc-attach -n c1 --clear-env -- mkdir -p /home/ubuntuscip-shell filesystem . . `/home/ubuntu`. |
| 40 | lxc-attach -n c1 --clear-env -- /bin/shscip-shell filesystem . . `/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 || { echoscip-shell system . . echo. "Managed to ping localhost"; falsescip-shell system . . 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 | sedscip-shell system . . sed. -i 's/lxc.no_new_privs = 1/lxc.no_new_privs = 0/' /var/lib/lxc/c1/configscip-shell filesystem . . `/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 || { echoscip-shell system . . echo. "Managed to ping localhost"; falsescip-shell system . . false.; } |
| 52 | lxc-stop -n c1 -k |
| 53 | |
| 54 | DONE=1 |