Debian Codegraph @

debian/tests/executables

raw
1 #!/bin/sh
2 # Test getting and setting of capabilities using getcap(8) and setcap(8).
3 # a simple test program. Assumes AUTOPKGTEST_TMP is set, see
4 # /usr/share/doc/autopkgtest/README.package-tests.gz
5 set -e
6
7 echo "@@@ Running test for getcap/setcap... @@@"
8
9
10 #########
11 # Preconditions
12 #########
13 if [ `id -u` -ne 0 ]
14 then
15 echo "This test must be run as root" >&2
16 exit 1
17 elif [ -z "$AUTOPKGTEST_TMP" ]
18 then
19 echo "This test requires that \$AUTOPKGTEST_TMP be set" >&2
20 exit 1
21 fi
22
23 # We will mount a tmpfs within AUTOPKGTEST_TMP. This is the only way to be sure that we
24 # are on a filesystem mounted without the nosuid option
25 test_tmpfs="$AUTOPKGTEST_TMP/tmpfs_executables"
26
27 # Make sure that we undo all our changes to the system
28 cleanup_testenv() {
29 cd /
30 if mountpoint -q "$test_tmpfs"
31 then
32 umount "$test_tmpfs"
33 fi
34 rmdir "$test_tmpfs"
35 }
36 trap cleanup_testenv EXIT HUP INT QUIT TERM
37
38
39 #########
40 # Set some names. We are modifying capabilities, so paths should be absolute.
41 #########
42 testuser=nobody
43 testfile="$test_tmpfs/testfile"
44 testprog="$test_tmpfs/chown_testfile"
45 testprog_src="${testprog}.c"
46
47
48 #########
49 # Step 1: Mount out tmpfs, and cd to it
50 #########
51
52 mkdir -p "$test_tmpfs"
53 mount -t tmpfs -o size=10m tmpfs "$test_tmpfs"
54 cd "$tmpfs"
55
56
57 #########
58 # Step 2: Prepare a trivial program that changes permissions of file $testfile
59 #########
60
61 cat > "$testprog_src" <<EOF
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <sys/types.h>
65 #include <unistd.h>
66
67 int main(int argc, char **argv)
68 {
69 /* See if we can chown the given file */
70 if (chown("$testfile", getuid(), getgid()) != 0) {
71 perror("chown");
72 exit(EXIT_FAILURE);
73 }
74
75 printf("OK\n");
76 exit(EXIT_SUCCESS);
77 }
78 EOF
79
80 gcc -Wall -o "$testprog" "$testprog_src"
81 chmod 755 "$testprog"
82
83
84 #########
85 # Step 3: Ceate a file with root ownership
86 #########
87
88 touch "$testfile"
89
90
91 #########
92 # Step 4: Verify that our test program fails to chown the file due to
93 # insufficient permissions
94 #########
95
96 echo "Attempt to chown *without* CAP_CHOWN should fail:"
97 if su -c "$testprog" -s /bin/sh nobody 2>&1
98 then
99 echo "Test file chown was successful although it shouldn't have been" >&2
100 exit 1
101 else
102 echo "OK, failed"
103 fi
104
105
106 #########
107 # Step 5: Set CAP_CHOWN and ensure that our test program succeeds this time
108 #########
109
110 echo "Set CAP_CHOWN on test file:"
111 if ! setcap cap_chown+ep "$testprog" 2>&1
112 then
113 echo "Failed to set CAP_CHOWN on $testprog" >&2
114 exit 1
115 elif ! setcap -v cap_chown+ep "$testprog" 2>&1
116 then
117 echo "Setting CAP_CHOWN on $testprog succeeded but verification failed" >&2
118 exit 1
119 else
120 echo "OK, set"
121 fi
122
123 echo "Attempt to chown *with* CAP_CHOWN should succeed:"
124 if ! su -c "$testprog" -s /bin/sh nobody 2>&1
125 then
126 echo "Test file chown failed although it shouldn't have" >&2
127 exit 1
128 else
129 echo "OK, chown succeeded"
130 fi
131
132
133 #########
134 # Step 6:
135 # Final verification
136 #########
137
138 if [ `stat -c '%u:%g' "$testfile"` != "`id -u $testuser`:`id -g $testuser`" ]
139 then
140 echo "chown succeeded but resulting ownership doesn't match expecation" >&2
141 exit 1
142 fi
143
144 # End of test
145 echo "@@@ Test: OK @@@"