4 # In order to be able to do the runcon commands in test_4,
5 # the SELinux policy must allow transitions from unconfined_t
6 # to user_t and guest_t:
7 # #============= unconfined_r ==============
8 # allow unconfined_r guest_r;
9 # allow unconfined_r user_r;
11 # Run select tests by setting ONLY, or as arguments to the script.
12 # Skip specific tests by setting EXCEPT.
14 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
18 # bug number for skipped test:
19 ALWAYS_EXCEPT=${ALWAYS_EXCEPT:-"$SANITY_SELINUX_EXCEPT"}
20 # UPDATE THE COMMENT ABOVE WITH BUG NUMBERS WHEN CHANGING ALWAYS_EXCEPT!
25 LUSTRE=${LUSTRE:-$(dirname $0)/..}
26 . $LUSTRE/tests/test-framework.sh
28 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
31 require_dsh_mds || exit 0
33 [ "$SLOW" = "no" ] && EXCEPT_SLOW="xxx"
35 RUNAS_CMD=${RUNAS_CMD:-runas}
36 # $RUNAS_ID may get set incorrectly somewhere else
37 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
38 error "RUNAS_ID set to 0, but UID is also 0!"
41 # global variables of this sanity
45 echo -n "Checking SELinux environment... "
46 local selinux_status=$(getenforce)
47 if [ "$selinux_status" != "Enforcing" ]; then
48 skip "SELinux is currently in $selinux_status mode," \
49 "but it must be enforced to run sanity-selinux" && exit 0
51 local selinux_policy=$(sestatus |
52 awk -F':' '$1 == "Loaded policy name" {print $2}' | xargs)
53 if [ -z "$selinux_policy" ]; then
54 selinux_policy=$(sestatus |
55 awk -F':' '$1 == "Policy from config file" {print $2}' | xargs)
57 [ "$selinux_policy" == "targeted" ] ||
58 error "Accepting only targeted policy"
59 echo "$selinux_status, $selinux_policy"
64 # we want double mount
65 MOUNT_2=${MOUNT_2:-"yes"}
66 check_and_setup_lustre
68 rm -rf $DIR/[df][0-9]*
70 check_runas_id $RUNAS_ID $RUNAS_ID $RUNAS
76 check_selinux_xattr() {
79 local mds_dev=$(facet_device $mds)
80 local mntpt="/tmp/mdt_"
83 do_facet $mds mkdir -p $mntpt || error "mkdir $mntpt failed"
84 mount_fstype $mds $mntpt || error "mount $mds failed"
86 local xattrval=$(do_facet $mds getfattr -n security.selinux \
87 ${mntpt}/ROOT/$mds_path |
88 awk -F"=" '$1=="security.selinux" {print $2}')
90 unmount_fstype $mds $mntpt || error "umount $mds failed"
91 do_facet $mds rmdir $mntpt || error "rmdir $mntpt failed"
98 local devname=$(mdsdevname 1)
99 local filename=${DIR}/${tdir}/df1
100 local mds_path=${filename#$MOUNT}
102 mds_path=${mds_path#/}
104 $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir $tdir failed"
105 touch $filename || error "cannot touch $filename"
107 local xattrval=$(check_selinux_xattr "mds1" $mds_path)
109 [ -n "$xattrval" -a "$xattrval" != '""' ] ||
110 error "security.selinux xattr is not set"
112 run_test 1 "create file and check security.selinux xattr is set on MDT"
115 local devname=$(mdsdevname 1)
116 local dirname=${DIR}/${tdir}/dir2a
117 local mds_path=${dirname#$MOUNT}
119 mds_path=${mds_path#/}
121 $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir failed"
122 mkdir $dirname || error "cannot mkdir $dirname"
124 local xattrval=$(check_selinux_xattr "mds1" $mds_path)
126 [ -n "$xattrval" -a "$xattrval" != '""' ] ||
127 error "security.selinux xattr is not set"
129 run_test 2a "create dir (mkdir) and check security.selinux xattr is set on MDT"
132 local devname=$(mdsdevname 1)
133 local dirname1=${DIR}/$tdir/dir2b1
134 local dirname2=${DIR}/$tdir/dir2b2
135 local mds_path=${dirname1#$MOUNT}
137 mds_path=${mds_path#/}
139 $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir failed"
140 $LFS mkdir -c0 $dirname1 || error "cannot 'lfs mkdir' $dirname1"
142 local xattrval=$(check_selinux_xattr "mds1" $mds_path)
144 mds_path=${dirname2#$MOUNT}
145 mds_path=${mds_path#/}
147 [ -n "$xattrval" -a "$xattrval" != '""' ] ||
148 error "security.selinux xattr is not set"
150 $LFS setdirstripe -i0 $dirname2 ||
151 error "cannot 'lfs setdirstripe' $dirname2"
153 xattrval=$(check_selinux_xattr "mds1" $mds_path)
155 [ -n "$xattrval" -a "$xattrval" != '""' ] ||
156 error "security.selinux xattr is not set"
158 run_test 2b "create dir with lfs and check security.selinux xattr is set on MDT"
161 local filename=$DIR/$tdir/df3
162 local level=$(id -Z | cut -d':' -f4-)
163 local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t \
170 echo "As unconfined_u: touch $filename"
171 $RUNAS_CMD -u $RUNAS_ID runcon $unconctx touch $filename ||
172 error "can't touch $filename"
173 echo "As unconfined_u: rm -f $filename"
174 $RUNAS_CMD -u $RUNAS_ID runcon $unconctx rm -f $filename ||
175 error "can't remove $filename"
179 run_test 3 "access with unconfined user"
182 local filename=$DIR/$tdir/df4
183 local guestctx="-u guest_u -r guest_r -t guest_t -l s0"
184 local usrctx="-u user_u -r user_r -t user_t -l s0"
186 sesearch --role_allow | grep -q "allow unconfined_r user_r"
187 if [ $? -ne 0 ]; then
188 skip "SELinux policy module must allow transition from \
189 unconfined_r to user_r for this test." && exit 0
191 sesearch --role_allow | grep -q "allow unconfined_r guest_r"
192 if [ $? -ne 0 ]; then
193 skip "SELinux policy module must allow transition from \
194 unconfined_r to guest_r for this test." && exit 0
201 echo "As guest_u: touch $filename"
202 $RUNAS_CMD -u $RUNAS_ID runcon $guestctx touch $filename &&
203 error "touch $filename should have failed"
206 echo "As user_u: touch $filename"
207 $RUNAS_CMD -u $RUNAS_ID runcon $usrctx touch $filename ||
208 error "can't touch $filename"
209 echo "As user_u: rm -f $filename"
210 $RUNAS_CMD -u $RUNAS_ID runcon $usrctx rm -f $filename ||
211 error "can't remove $filename"
215 run_test 4 "access with specific SELinux user"
218 local filename=$DIR/df5
219 local newsecctx="nfs_t"
222 touch $filename || error "cannot touch $filename"
225 chcon -t $newsecctx $filename
228 # purge client's cache
229 sync ; echo 3 > /proc/sys/vm/drop_caches
233 local secctxseen=$(ls -lZ $filename | awk '{print $4}' | cut -d: -f3)
235 [ "$newsecctx" == "$secctxseen" ] ||
236 error "sec context seen from 1st mount point is not correct"
240 run_test 5 "security context retrieval from MDT xattr"
243 local filename1=$DIR/df10
244 local filename2=$DIR2/df10
245 local newsecctx="nfs_t"
247 # create file from 1st mount point
248 touch $filename1 || error "cannot touch $filename1"
251 # change sec context from 2nd mount point
252 chcon -t $newsecctx $filename2
255 # get sec context from 1st mount point
257 local secctxseen=$(ls -lZ $filename1 | awk '{print $4}' | cut -d: -f3)
259 [ "$newsecctx" == "$secctxseen" ] ||
260 error_ignore LU-6784 \
261 "sec context seen from 1st mount point is not correct"
265 run_test 10 "[consistency] concurrent security context change"
268 local filename1=$DIR/$tdir/df20a
269 local filename2=$DIR2/$tdir/df20a
271 local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t -l s0"
276 # sleep some time in ll_create_nd()
277 #define OBD_FAIL_LLITE_CREATE_FILE_PAUSE 0x1409
278 do_facet client "$LCTL set_param fail_val=$req_delay fail_loc=0x1409"
280 # create file on first mount point
281 $RUNAS_CMD -u $RUNAS_ID runcon $unconctx touch $filename1 &
285 if [[ -z "$(ps h -o comm -p $touchpid)" ]]; then
286 error "touch failed to sleep, pid=$touchpid"
289 # get sec info on second mount point
290 if [ -e "$filename2" ]; then
291 secinfo2=$(ls -lZ $filename2 | awk '{print $4}')
294 # get sec info on first mount point
296 secinfo1=$(ls -lZ $filename1 | awk '{print $4}')
298 # compare sec contexts
299 [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
300 error "sec context seen from 2nd mount point is not correct"
304 run_test 20a "[atomicity] concurrent access from another client (file)"
307 local dirname1=$DIR/$tdir/dd20b
308 local dirname2=$DIR2/$tdir/dd20b
310 local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t -l s0"
315 # sleep some time in ll_create_nd()
316 #define OBD_FAIL_LLITE_NEWNODE_PAUSE 0x140a
317 do_facet client "$LCTL set_param fail_val=$req_delay fail_loc=0x140a"
319 # create file on first mount point
320 $RUNAS_CMD -u $RUNAS_ID runcon $unconctx mkdir $dirname1 &
324 if [[ -z "$(ps h -o comm -p $mkdirpid)" ]]; then
325 error "mkdir failed to sleep, pid=$mkdirpid"
328 # get sec info on second mount point
329 if [ -e "$dirname2" ]; then
330 secinfo2=$(ls -ldZ $dirname2 | awk '{print $4}')
335 # get sec info on first mount point
337 secinfo1=$(ls -ldZ $dirname1 | awk '{print $4}')
339 # compare sec contexts
340 [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
341 error "sec context seen from 2nd mount point is not correct"
345 run_test 20b "[atomicity] concurrent access from another client (dir)"
348 local dirname1=$DIR/dd20c
349 local dirname2=$DIR2/dd20c
352 # sleep some time in ll_create_nd()
353 #define OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE 0x140b
354 do_facet client "$LCTL set_param fail_val=$req_delay fail_loc=0x140b"
356 # create file on first mount point
357 lfs mkdir -c0 $dirname1 &
361 if [[ -z "$(ps h -o comm -p $mkdirpid)" ]]; then
362 error "lfs mkdir failed to sleep, pid=$mkdirpid"
365 # get sec info on second mount point
366 if [ -e "$dirname2" ]; then
367 secinfo2=$(ls -ldZ $dirname2 | awk '{print $4}')
372 # get sec info on first mount point
374 secinfo1=$(ls -ldZ $dirname1 | awk '{print $4}')
376 # compare sec contexts
377 [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
378 error "sec context seen from 2nd mount point is not correct"
382 run_test 20c "[atomicity] concurrent access from another client (dir via lfs)"
386 check_and_cleanup_lustre