File: executables

package info (click to toggle)
libcap2 1%3A2.75-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,068 kB
  • sloc: ansic: 9,181; sh: 1,138; makefile: 812; cpp: 45; asm: 16
file content (145 lines) | stat: -rwxr-xr-x 3,046 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/sh
# Test getting and setting of capabilities using getcap(8) and setcap(8).
# a simple test program. Assumes AUTOPKGTEST_TMP is set, see
# /usr/share/doc/autopkgtest/README.package-tests.gz
set -e

echo "@@@ Running test for getcap/setcap... @@@"


#########
# Preconditions
#########
if [ `id -u` -ne 0 ]
then
	echo "This test must be run as root" >&2
	exit 1
elif [ -z "$AUTOPKGTEST_TMP" ]
then
	echo "This test requires that \$AUTOPKGTEST_TMP be set" >&2
	exit 1
fi

# We will mount a tmpfs within AUTOPKGTEST_TMP. This is the only way to be sure that we
# are on a filesystem mounted without the nosuid option
test_tmpfs="$AUTOPKGTEST_TMP/tmpfs_executables"

# Make sure that we undo all our changes to the system
cleanup_testenv() {
	cd /
	if mountpoint -q "$test_tmpfs"
	then
		umount "$test_tmpfs"
	fi
	rmdir "$test_tmpfs"
}
trap cleanup_testenv EXIT HUP INT QUIT TERM


#########
# Set some names. We are modifying capabilities, so paths should be absolute.
#########
testuser=nobody
testfile="$test_tmpfs/testfile"
testprog="$test_tmpfs/chown_testfile"
testprog_src="${testprog}.c"


#########
# Step 1: Mount out tmpfs, and cd to it
#########

mkdir -p "$test_tmpfs"
mount -t tmpfs -o size=10m tmpfs "$test_tmpfs"
cd "$tmpfs"


#########
# Step 2: Prepare a trivial program that changes permissions of file $testfile
#########

cat > "$testprog_src" <<EOF
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(int argc, char **argv)
{
	/* See if we can chown the given file */
	if (chown("$testfile", getuid(), getgid()) != 0) {
		perror("chown");
		exit(EXIT_FAILURE);
	}

	printf("OK\n");
	exit(EXIT_SUCCESS);
}
EOF

gcc -Wall -o "$testprog" "$testprog_src"
chmod 755 "$testprog"


#########
# Step 3: Ceate a file with root ownership
#########

touch "$testfile"


#########
# Step 4: Verify that our test program fails to chown the file due to
#         insufficient permissions
#########

echo "Attempt to chown *without* CAP_CHOWN should fail:"
if su -c "$testprog" -s /bin/sh nobody 2>&1
then
	echo "Test file chown was successful although it shouldn't have been" >&2
	exit 1
else
	echo "OK, failed"
fi


#########
# Step 5: Set CAP_CHOWN and ensure that our test program succeeds this time
#########

echo "Set CAP_CHOWN on test file:"
if ! setcap cap_chown+ep "$testprog" 2>&1
then
	echo "Failed to set CAP_CHOWN on $testprog" >&2
	exit 1
elif ! setcap -v cap_chown+ep "$testprog" 2>&1
then
	echo "Setting CAP_CHOWN on $testprog succeeded but verification failed" >&2
	exit 1
else
	echo "OK, set"
fi

echo "Attempt to chown *with* CAP_CHOWN should succeed:"
if ! su -c "$testprog" -s /bin/sh nobody 2>&1
then
	echo "Test file chown failed although it shouldn't have" >&2
	exit 1
else
	echo "OK, chown succeeded"
fi


#########
# Step 6:
#   Final verification
#########

if [ `stat -c '%u:%g' "$testfile"` != "`id -u $testuser`:`id -g $testuser`" ]
then
	echo "chown succeeded but resulting ownership doesn't match expecation" >&2
	exit 1
fi

# End of test
echo "@@@ Test: OK @@@"