]> git.proxmox.com Git - mirror_lxc.git/blame - src/tests/lxc-test-no-new-privs
spelling: timeout
[mirror_lxc.git] / src / tests / lxc-test-no-new-privs
CommitLineData
bca94305
CB
1#!/bin/bash
2
3# lxc: linux Container library
4
5# Authors:
6# Christian Brauner <christian.brauner@mailbox.org>
7#
8# This is a test script for PR_SET_NO_NEW_PRIVS
9
10# This library is free software; you can redistribute it and/or
11# modify it under the terms of the GNU Lesser General Public
12# License as published by the Free Software Foundation; either
13# version 2.1 of the License, or (at your option) any later version.
14
15# This library is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18# Lesser General Public License for more details.
19
20# You should have received a copy of the GNU Lesser General Public
21# License along with this library; if not, write to the Free Software
22# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
24set -eux
25
26DONE=0
27cleanup() {
28 cd /
29 lxc-destroy -n c1 -f || true
30 if [ $DONE -eq 0 ]; then
31 echo "FAIL"
32 exit 1
33 fi
34 echo "PASS"
35}
36
37trap cleanup EXIT SIGHUP SIGINT SIGTERM
38
39mkdir -p /etc/lxc/
40cat > /etc/lxc/default.conf << EOF
7fa3f2e9 41lxc.net.0.type = veth
42lxc.net.0.link = lxcbr0
bca94305
CB
43EOF
44
45ARCH=i386
46if type dpkg >/dev/null 2>&1; then
47 ARCH=$(dpkg --print-architecture)
48fi
49
50lxc-create -t download -n c1 -- -d ubuntu -r xenial -a $ARCH
51echo "lxc.no_new_privs = 1" >> /var/lib/lxc/c1/config
52
53lxc-start -n c1
54p1=$(lxc-info -n c1 -p -H)
55[ "$p1" != "-1" ] || { echo "Failed to start container c1 (run $count)"; false; }
56sleep 5s
57lxc-attach -n c1 --clear-env -- apt update -y
58lxc-attach -n c1 --clear-env -- apt install -y gcc make
59
60# Here documents don't seem to like sudo -i.
61lxc-attach -n c1 --clear-env -- /bin/bash -c "cat <<EOF > /nnptest.c
62#include <stdio.h>
63#include <unistd.h>
64#include <sys/types.h>
65
66int main(int argc, char *argv[])
67{
68 printf(\"%d\n\", geteuid());
69}
70EOF"
71lxc-attach -n c1 --clear-env -- cat /nnptest.c
72lxc-attach -n c1 --clear-env -- make -C / nnptest
73lxc-attach -n c1 --clear-env -- chmod u+s /nnptest
74
75# Check that lxc-attach obeys PR_SET_NO_NEW_PRIVS when it is set.
76NNP_EUID=$(lxc-attach -n c1 --clear-env -- sudo -u ubuntu /nnptest)
77if [ "$NNP_EUID" -ne 1000 ]; then
78 exit 1
79fi
80lxc-stop -n c1 -k
81
82# Check that lxc-attach obeys PR_SET_NO_NEW_PRIVS when it is not set.
83sed -i 's/lxc.no_new_privs = 1/lxc.no_new_privs = 0/' /var/lib/lxc/c1/config
84lxc-start -n c1
85NNP_EUID=$(lxc-attach -n c1 --clear-env -- sudo -u ubuntu /nnptest)
86if [ "$NNP_EUID" -ne 0 ]; then
87 exit 1
88fi
89lxc-stop -n c1 -k
90
91# Check that lxc-execute and lxc-start obey PR_SET_NO_NEW_PRIVS when it is set.
92NNP_EUID=$(lxc-execute -n c1 -- sudo -u ubuntu /nnptest)
93if [ "$NNP_EUID" -ne 0 ]; then
94 exit 1
95fi
96
97# Check that lxc-execute and lxc-start obey PR_SET_NO_NEW_PRIVS when it is not set.
98sed -i 's/lxc.no_new_privs = 0/lxc.no_new_privs = 1/' /var/lib/lxc/c1/config
99NNP_EUID=$(lxc-execute -n c1 -- sudo -u ubuntu /nnptest)
100if [ "$NNP_EUID" -ne 1000 ]; then
101 exit 1
102fi
103
104DONE=1