~ubuntu-branches/ubuntu/wily/apparmor/wily

« back to all changes in this revision

Viewing changes to tests/regression/apparmor/setattr.sh

  • Committer: Bazaar Package Importer
  • Author(s): Kees Cook
  • Date: 2011-04-27 10:38:07 UTC
  • mfrom: (5.1.118 natty)
  • Revision ID: james.westby@ubuntu.com-20110427103807-ym3rhwys6o84ith0
Tags: 2.6.1-2
debian/copyright: clarify for some full organization names.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /bin/bash
 
2
#       Copyright (C) 2002-2005 Novell/SUSE
 
3
#
 
4
#       This program is free software; you can redistribute it and/or
 
5
#       modify it under the terms of the GNU General Public License as
 
6
#       published by the Free Software Foundation, version 2 of the
 
7
#       License.
 
8
 
 
9
#=NAME setattr
 
10
#=DESCRIPTION
 
11
# Write permission is required in a confined processes profile in order to
 
12
# change the mode (chmod, chgrp, chown) of a file.  This test verifies these
 
13
# system calls for unconfined and confined processes. It also includes
 
14
# the fxxx version of the tests.
 
15
#=END
 
16
 
 
17
checkfile()
 
18
{
 
19
        _file=$1
 
20
        _str=$2
 
21
        _newfileperm=$3
 
22
        _newuser=$4
 
23
        _newgroup=$5
 
24
 
 
25
        set -- `ls -ld $_file`
 
26
 
 
27
        if [ $1 != "$_newfileperm" -o $3 != $_newuser -o $4 != $_newgroup ]
 
28
        then
 
29
                echo "Error: ($_str)"
 
30
                echo "Error: ls -l $file output does not look correct"
 
31
                echo "Error: saw: $1/$3/$4   expected: $_newfileperm/$_newuser/$_newgroup"
 
32
        fi
 
33
}
 
34
 
 
35
resettest()
 
36
{
 
37
        rm -f $file
 
38
        touch $file
 
39
        chmod $origfileperm $file
 
40
        rm -rf $dir
 
41
        mkdir $dir
 
42
        chmod $origfileperm $dir
 
43
}
 
44
 
 
45
pwd=`dirname $0`
 
46
pwd=`cd $pwd ; /bin/pwd`
 
47
 
 
48
bin=$pwd
 
49
 
 
50
. $bin/prologue.inc
 
51
 
 
52
file=$tmpdir/file
 
53
dir="$tmpdir/dir/"
 
54
 
 
55
okperm=rw
 
56
badperm=r
 
57
 
 
58
pwfiles="/etc/passwd:r /etc/group:r"
 
59
 
 
60
origfileperm=644
 
61
origfilepermstr="-rw-r--r--"
 
62
newfileperm=400
 
63
newfilepermstr="-r--------"
 
64
origdirperm=644
 
65
origdirpermstr="drw-r--r--"
 
66
newdirperm=400
 
67
newdirpermstr="dr--------"
 
68
origuser=`id -un`
 
69
newuser=nobody
 
70
newuid=$(awk -F: "/^${newuser}:/ {print \$3}" /etc/passwd)
 
71
origgroup=`id -gn`
 
72
# Sigh, debian uses group nogroup instead of nobody
 
73
# XXX - not sure what to do if neither exist.
 
74
if [ $(grep -c nobody /etc/group) -gt 0 ] ; then
 
75
        newgroup=nobody
 
76
elif [ $(grep -c nogroup /etc/group) -gt 0 ] ; then
 
77
        newgroup=nogroup
 
78
else 
 
79
        newgroup=bin
 
80
fi
 
81
newgid=$(awk -F: "/^${newgroup}:/ {print \$3}" /etc/group)
 
82
#echo newuser=${newuser} newuid=${newuid}
 
83
#echo newgroup=${newgroup} newgid=${newgid}
 
84
 
 
85
 
 
86
# NOTE on the ordering of tests: XFS requires the FOWNER capability
 
87
# to chgrp a file that you are not the owner of; linux's vfs layer will
 
88
# allow you to do it if you are in the group of the file without FOWNER.
 
89
# Therefore, we should do the chgrp test BEFORE changing the owner of
 
90
# the file.
 
91
 
 
92
# PASS TEST (UNCONFINED)
 
93
resettest
 
94
 
 
95
settest chmod
 
96
runchecktest "CHMOD (unconfined)" pass $file $newfileperm
 
97
 
 
98
settest chgrp
 
99
runchecktest "CHGRP (unconfined)" pass $file $newgid
 
100
runchecktest "CHGRP dir (unconfined)" pass $dir $newgid
 
101
 
 
102
settest chown
 
103
runchecktest "CHOWN (unconfined)" pass $file $newuid
 
104
 
 
105
checkfile $file "unconfined" $newfilepermstr $newuser $newgroup
 
106
 
 
107
 
 
108
settest chmod
 
109
runchecktest "CHMOD dir (unconfined)" pass $dir $newfileperm
 
110
 
 
111
settest chgrp
 
112
runchecktest "CHGRP dir (unconfined)" pass $dir $newgid
 
113
 
 
114
settest chown
 
115
runchecktest "CHOWN dir (unconfined)" pass $dir $newuid
 
116
 
 
117
checkfile $dir "dir unconfined" $newdirpermstr $newuser $newgroup
 
118
 
 
119
 
 
120
# PASS TEST (UNCONFINED w/FOPS)
 
121
resettest
 
122
 
 
123
settest fchmod
 
124
runchecktest "FCHMOD (unconfined)" pass $file $newfileperm
 
125
 
 
126
settest fchgrp
 
127
runchecktest "FCHGRP (unconfined)" pass $file $newgid
 
128
 
 
129
settest fchown
 
130
runchecktest "FCHOWN (unconfined)" pass $file $newuid
 
131
 
 
132
checkfile $file "unconfined" $newfilepermstr $newuser $newgroup
 
133
 
 
134
 
 
135
settest fchmod
 
136
runchecktest "FCHMOD dir (unconfined)" pass $dir $newfileperm
 
137
 
 
138
settest fchgrp
 
139
runchecktest "FCHGRP dir (unconfined)" pass $dir $newgid
 
140
 
 
141
settest fchown
 
142
runchecktest "FCHOWN dir (unconfined)" pass $dir $newuid
 
143
 
 
144
checkfile $dir "dir unconfined" $newdirpermstr $newuser $newgroup
 
145
 
 
146
# PASS TEST (CONFINED)
 
147
resettest
 
148
 
 
149
settest chmod
 
150
genprofile $file:$okperm 
 
151
runchecktest "CHMOD (confined $okperm)" pass $file $newfileperm
 
152
 
 
153
settest chgrp
 
154
genprofile $file:$okperm $pwfiles cap:chown
 
155
runchecktest "CHGRP (confined $okperm)" pass $file $newgid
 
156
 
 
157
settest chown
 
158
genprofile $file:$okperm $pwfiles cap:chown
 
159
runchecktest "CHOWN (confined $okperm)" pass $file $newuid
 
160
 
 
161
checkfile $file "confined $okperm" $newfilepermstr $newuser $newgroup
 
162
 
 
163
 
 
164
settest chmod
 
165
genprofile $dir:$okperm 
 
166
runchecktest "CHMOD dir (confined $okperm)" pass $dir $newfileperm
 
167
 
 
168
settest chgrp
 
169
genprofile $dir:$okperm $pwfiles cap:chown
 
170
runchecktest "CHGRP dir (confined $okperm)" pass $dir $newgid
 
171
 
 
172
settest chown
 
173
genprofile $dir:$okperm $pwfiles cap:chown
 
174
runchecktest "CHOWN dir (confined $okperm)" pass $dir $newuid
 
175
 
 
176
checkfile $dir "confined dir $okperm" $newdirpermstr $newuser $newgroup
 
177
 
 
178
# PASS TEST (CONFINED w/FOPS)
 
179
resettest
 
180
 
 
181
settest fchmod
 
182
genprofile $file:$okperm 
 
183
runchecktest "FCHMOD (confined $okperm)" pass $file $newfileperm
 
184
 
 
185
settest fchgrp
 
186
genprofile $file:$okperm $pwfiles cap:chown
 
187
runchecktest "FCHGRP (confined $okperm)" pass $file $newgid
 
188
 
 
189
settest fchown
 
190
genprofile $file:$okperm $pwfiles cap:chown
 
191
runchecktest "FCHOWN (confined $okperm)" pass $file $newuid
 
192
 
 
193
checkfile $file "confined $okperm" $newfilepermstr $newuser $newgroup
 
194
 
 
195
 
 
196
settest fchmod
 
197
genprofile $dir:$okperm 
 
198
runchecktest "FCHMOD dir (confined $okperm)" pass $dir $newfileperm
 
199
 
 
200
settest fchgrp
 
201
genprofile $dir:$okperm $pwfiles cap:chown
 
202
runchecktest "FCHGRP dir (confined $okperm)" pass $dir $newgid
 
203
 
 
204
settest fchown
 
205
genprofile $dir:$okperm $pwfiles cap:chown
 
206
runchecktest "FCHOWN dir (confined $okperm)" pass $dir $newuid
 
207
 
 
208
checkfile $dir "confined dir $okperm" $newdirpermstr $newuser $newgroup
 
209
 
 
210
# FAIL TEST (CONFINED)
 
211
resettest
 
212
 
 
213
settest chmod
 
214
genprofile $file:$badperm $pwfiles
 
215
runchecktest "CHMOD (confined $badperm)" fail $file $newfileperm
 
216
 
 
217
settest chgrp
 
218
genprofile $file:$badperm $pwfiles cap:chown
 
219
runchecktest "CHGRP (confined $badperm)" fail $file $newgid
 
220
 
 
221
settest chown
 
222
genprofile $file:$badperm $pwfiles cap:chown
 
223
runchecktest "CHOWN (confined $badperm)" fail $file $newuid
 
224
 
 
225
checkfile $file "confined $badperm" $origfilepermstr $origuser $origgroup
 
226
 
 
227
 
 
228
settest chmod
 
229
genprofile $dir:$badperm $pwfiles
 
230
runchecktest "CHMOD dir (confined $badperm)" fail $dir $newfileperm
 
231
 
 
232
settest chgrp
 
233
genprofile $dir:$badperm $pwfiles cap:chown
 
234
runchecktest "CHGRP dir (confined $badperm)" fail $dir $newgid
 
235
 
 
236
settest chown
 
237
genprofile $dir:$badperm $pwfiles cap:chown
 
238
runchecktest "CHOWN dir (confined $badperm)" fail $dir $newuid
 
239
 
 
240
checkfile $dir "confined dir $badperm" $origdirpermstr $origuser $origgroup
 
241
 
 
242
# FAIL TEST (CONFINED/LACKING CAPS)
 
243
resettest
 
244
 
 
245
settest chgrp
 
246
genprofile $file:$okperm $pwfiles
 
247
runchecktest "CHGRP (confined $okperm/no capabilities)" fail $file $newgid
 
248
 
 
249
settest chown
 
250
genprofile $file:$okperm $pwfiles
 
251
runchecktest "CHOWN (confined $okperm/no capabilities)" fail $file $newuid
 
252
 
 
253
checkfile $file "confined $badperm" $origfilepermstr $origuser $origgroup
 
254
 
 
255
 
 
256
settest chgrp
 
257
genprofile $dir:$okperm $pwfiles
 
258
runchecktest "CHGRP dir (confined $okperm/no capabilities)" fail $dir $newgid
 
259
 
 
260
settest chown
 
261
genprofile $dir:$okperm $pwfiles
 
262
runchecktest "CHOWN dir (confined $okperm/no capabilities)" fail $dir $newuid
 
263
 
 
264
checkfile $dir "confined dir $badperm" $origdirpermstr $origuser $origgroup
 
265
 
 
266
# FAIL TEST (CONFINED w/FOPS)
 
267
resettest
 
268
 
 
269
settest fchmod
 
270
genprofile $file:$badperm $pwfiles
 
271
runchecktest "FCHMOD (confined $badperm)" fail $file $newfileperm
 
272
 
 
273
settest fchgrp
 
274
genprofile $file:$badperm $pwfiles cap:chown
 
275
runchecktest "FCHGRP (confined $badperm)" fail $file $newgid
 
276
 
 
277
settest fchown
 
278
genprofile $file:$badperm $pwfiles cap:chown
 
279
runchecktest "FCHOWN (confined $badperm)" fail $file $newuid
 
280
 
 
281
checkfile $file "confined $badperm" $origfilepermstr $origuser $origgroup
 
282
 
 
283
 
 
284
settest fchmod
 
285
genprofile $dir:$badperm $pwfiles
 
286
runchecktest "FCHMOD dir (confined $badperm)" fail $dir $newfileperm
 
287
 
 
288
settest fchgrp
 
289
genprofile $dir:$badperm $pwfiles cap:chown
 
290
runchecktest "FCHGRP dir (confined $badperm)" fail $dir $newgid
 
291
 
 
292
settest fchown
 
293
genprofile $dir:$badperm $pwfiles cap:chown
 
294
runchecktest "FCHOWN dir (confined $badperm)" fail $dir $newuid
 
295
 
 
296
checkfile $dir "confined dir $badperm" $origdirpermstr $origuser $origgroup
 
297
 
 
298
# FAIL TEST (CONFINED w/FOPS/LACKING CAPS)
 
299
resettest
 
300
 
 
301
settest fchgrp
 
302
genprofile $file:$okperm $pwfiles
 
303
runchecktest "FCHGRP (confined $okperm/no capabilities)" fail $file $newgid
 
304
 
 
305
settest fchown
 
306
genprofile $file:$okperm $pwfiles
 
307
runchecktest "FCHOWN (confined $okperm/no capabilities)" fail $file $newuid
 
308
 
 
309
checkfile $file "confined $badperm" $origfilepermstr $origuser $origgroup
 
310
 
 
311
 
 
312
settest fchgrp
 
313
genprofile $dir:$okperm $pwfiles
 
314
runchecktest "FCHGRP dir (confined $okperm/no capabilities)" fail $dir $newgid
 
315
 
 
316
settest fchown
 
317
genprofile $dir:$okperm $pwfiles
 
318
runchecktest "FCHOWN dir (confined $okperm/no capabilities)" fail $dir $newuid
 
319
 
 
320
checkfile $dir "confined dir $badperm" $origdirpermstr $origuser $origgroup