Whamcloud - gitweb
LU-12662 tests: Add new pjdfstest into tests
[fs/lustre-release.git] / lustre / tests / sanity-selinux.sh
1 #!/bin/bash
2 #
3 # NOTE
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;
10 #
11 # Run select tests by setting ONLY, or as arguments to the script.
12 # Skip specific tests by setting EXCEPT.
13 #
14 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
15 set -e
16
17 ONLY=${ONLY:-"$*"}
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!
21
22 SRCDIR=$(dirname $0)
23 SAVE_PWD=$PWD
24
25 LUSTRE=${LUSTRE:-$(dirname $0)/..}
26 . $LUSTRE/tests/test-framework.sh
27 init_test_env $@
28 . ${CONFIG:=$LUSTRE/tests/cfg/$NAME.sh}
29 init_logging
30
31 require_dsh_mds || exit 0
32
33 [ "$SLOW" = "no" ] && EXCEPT_SLOW="xxx"
34
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!"
39
40 #
41 # global variables of this  sanity
42 #
43
44 check_selinux() {
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
50         fi
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)
56         fi
57         [ "$selinux_policy" == "targeted" ] ||
58                 error "Accepting only targeted policy"
59         echo "$selinux_status, $selinux_policy"
60 }
61
62 check_selinux
63
64 # we want double mount
65 MOUNT_2=${MOUNT_2:-"yes"}
66 check_and_setup_lustre
67
68 rm -rf $DIR/[df][0-9]*
69
70 check_runas_id $RUNAS_ID $RUNAS_ID $RUNAS
71
72 build_test_filter
73
74 umask 077
75
76 check_selinux_xattr() {
77         local mds=$1
78         local mds_path=$2
79         local mds_dev=$(facet_device $mds)
80         local mntpt="/tmp/mdt_"
81         local opts
82
83         do_facet $mds mkdir -p $mntpt  || error "mkdir $mntpt failed"
84         mount_fstype $mds $mntpt  || error "mount $mds failed"
85
86         local xattrval=$(do_facet $mds getfattr -n security.selinux \
87                                 ${mntpt}/ROOT/$mds_path |
88                          awk -F"=" '$1=="security.selinux" {print $2}')
89
90         unmount_fstype $mds $mntpt || error "umount $mds failed"
91         do_facet $mds rmdir $mntpt || error "rmdir $mntpt failed"
92
93         echo $xattrval
94 }
95
96 get_sel_ctx() {
97         local file=$1
98
99         [ -n "$file" ] || return;
100         [ -f $file ] || return;
101         stat $file | awk '$1 == "Context:" {print $2}'
102 }
103
104 test_1() {
105         local devname=$(mdsdevname 1)
106         local filename=${DIR}/${tdir}/df1
107         local mds_path=${filename#$MOUNT}
108
109         mds_path=${mds_path#/}
110
111         $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir $tdir failed"
112         touch $filename || error "cannot touch $filename"
113
114         local xattrval=$(check_selinux_xattr "mds1" $mds_path)
115
116         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
117                 error "security.selinux xattr is not set"
118 }
119 run_test 1 "create file and check security.selinux xattr is set on MDT"
120
121 test_2a() {
122         local devname=$(mdsdevname 1)
123         local dirname=${DIR}/${tdir}/dir2a
124         local mds_path=${dirname#$MOUNT}
125
126         mds_path=${mds_path#/}
127
128         $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir failed"
129         mkdir $dirname || error "cannot mkdir $dirname"
130
131         local xattrval=$(check_selinux_xattr "mds1" $mds_path)
132
133         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
134                 error "security.selinux xattr is not set"
135 }
136 run_test 2a "create dir (mkdir) and check security.selinux xattr is set on MDT"
137
138 test_2b() {
139         local devname=$(mdsdevname 1)
140         local dirname1=${DIR}/$tdir/dir2b1
141         local dirname2=${DIR}/$tdir/dir2b2
142         local mds_path=${dirname1#$MOUNT}
143
144         mds_path=${mds_path#/}
145
146         $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir failed"
147         $LFS mkdir -c0 -i0 $dirname1 || error "cannot 'lfs mkdir' $dirname1"
148
149         local xattrval=$(check_selinux_xattr "mds1" $mds_path)
150
151         mds_path=${dirname2#$MOUNT}
152         mds_path=${mds_path#/}
153
154         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
155                 error "security.selinux xattr is not set"
156
157         $LFS setdirstripe -i0 $dirname2 ||
158             error "cannot 'lfs setdirstripe' $dirname2"
159
160         xattrval=$(check_selinux_xattr "mds1" $mds_path)
161
162         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
163                 error "security.selinux xattr is not set"
164 }
165 run_test 2b "create dir with lfs and check security.selinux xattr is set on MDT"
166
167 test_3() {
168         local filename=$DIR/$tdir/df3
169         local level=$(id -Z | cut -d':' -f4-)
170         local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t \
171                         -l $level"
172
173         mkdir -p $DIR/$tdir
174         chmod 777 $DIR/$tdir
175
176         # "access" Lustre
177         echo "As unconfined_u: touch $filename"
178         $RUNAS_CMD -u $RUNAS_ID runcon $unconctx touch $filename ||
179                 error "can't touch $filename"
180         echo "As unconfined_u: rm -f $filename"
181         $RUNAS_CMD -u $RUNAS_ID runcon $unconctx rm -f $filename ||
182                 error "can't remove $filename"
183
184         return 0
185 }
186 run_test 3 "access with unconfined user"
187
188 test_4() {
189         local filename=$DIR/$tdir/df4
190         local guestctx="-u guest_u -r guest_r -t guest_t -l s0"
191         local usrctx="-u user_u -r user_r -t user_t -l s0"
192
193         sesearch --role_allow | grep -q "allow unconfined_r user_r"
194         if [ $? -ne 0 ]; then
195             skip "SELinux policy module must allow transition from \
196                    unconfined_r to user_r for this test." && exit 0
197         fi
198         sesearch --role_allow | grep -q "allow unconfined_r guest_r"
199         if [ $? -ne 0 ]; then
200             skip "SELinux policy module must allow transition from \
201                    unconfined_r to guest_r for this test." && exit 0
202         fi
203
204         mkdir -p $DIR/$tdir
205         chmod 777 $DIR/$tdir
206
207         # "access" Lustre
208         echo "As guest_u: touch $filename"
209         $RUNAS_CMD -u $RUNAS_ID runcon $guestctx touch $filename &&
210                 error "touch $filename should have failed"
211
212         # "access" Lustre
213         echo "As user_u: touch $filename"
214         $RUNAS_CMD -u $RUNAS_ID runcon $usrctx touch $filename ||
215                 error "can't touch $filename"
216         echo "As user_u: rm -f $filename"
217         $RUNAS_CMD -u $RUNAS_ID runcon $usrctx rm -f $filename ||
218                 error "can't remove $filename"
219
220         return 0
221 }
222 run_test 4 "access with specific SELinux user"
223
224 test_5() {
225         local filename=$DIR/df5
226         local newsecctx="nfs_t"
227
228         # create file
229         touch $filename || error "cannot touch $filename"
230
231         # change sec context
232         chcon -t $newsecctx $filename
233         ls -lZ $filename
234
235         # purge client's cache
236         sync ; echo 3 > /proc/sys/vm/drop_caches
237
238         # get sec context
239         ls -lZ $filename
240         local secctxseen=$(get_sel_ctx $filename | cut -d: -f3)
241
242         [ "$newsecctx" == "$secctxseen" ] ||
243                 error "sec context seen from 1st mount point is not correct"
244
245         return 0
246 }
247 run_test 5 "security context retrieval from MDT xattr"
248
249 test_10() {
250         local filename1=$DIR/df10
251         local filename2=$DIR2/df10
252         local newsecctx="nfs_t"
253
254         # create file from 1st mount point
255         touch $filename1 || error "cannot touch $filename1"
256         ls -lZ $filename1
257
258         # change sec context from 2nd mount point
259         chcon -t $newsecctx $filename2
260         ls -lZ $filename2
261
262         # get sec context from 1st mount point
263         ls -lZ $filename1
264         local secctxseen=$(get_sel_ctx $filename1 | cut -d: -f3)
265
266         [ "$newsecctx" == "$secctxseen" ] ||
267                 error_ignore LU-6784 \
268                     "sec context seen from 1st mount point is not correct"
269
270         return 0
271 }
272 run_test 10 "[consistency] concurrent security context change"
273
274 test_20a() {
275         local filename1=$DIR/$tdir/df20a
276         local filename2=$DIR2/$tdir/df20a
277         local req_delay=20
278         local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t -l s0"
279
280         mkdir -p $DIR/$tdir
281         chmod 777 $DIR/$tdir
282
283         # sleep some time in ll_create_nd()
284         #define OBD_FAIL_LLITE_CREATE_FILE_PAUSE   0x1409
285         do_facet client "$LCTL set_param fail_val=$req_delay fail_loc=0x1409"
286
287         # create file on first mount point
288         $RUNAS_CMD -u $RUNAS_ID runcon $unconctx touch $filename1 &
289         local touchpid=$!
290         sleep 5
291
292         if [[ -z "$(ps h -o comm -p $touchpid)" ]]; then
293                 error "touch failed to sleep, pid=$touchpid"
294         fi
295
296         # get sec info on second mount point
297         if [ -e "$filename2" ]; then
298                 secinfo2=$(get_sel_ctx $filename2)
299         fi
300
301         # get sec info on first mount point
302         wait $touchpid
303         secinfo1=$(get_sel_ctx $filename1)
304
305         # compare sec contexts
306         [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
307                 error "sec context seen from 2nd mount point is not correct"
308
309         return 0
310 }
311 run_test 20a "[atomicity] concurrent access from another client (file)"
312
313 test_20b() {
314         local dirname1=$DIR/$tdir/dd20b
315         local dirname2=$DIR2/$tdir/dd20b
316         local req_delay=20
317         local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t -l s0"
318
319         mkdir -p $DIR/$tdir
320         chmod 777 $DIR/$tdir
321
322         # sleep some time in ll_create_nd()
323         #define OBD_FAIL_LLITE_NEWNODE_PAUSE     0x140a
324         do_facet client "$LCTL set_param fail_val=$req_delay fail_loc=0x140a"
325
326         # create file on first mount point
327         $RUNAS_CMD -u $RUNAS_ID runcon $unconctx mkdir $dirname1 &
328         local mkdirpid=$!
329         sleep 5
330
331         if [[ -z "$(ps h -o comm -p $mkdirpid)" ]]; then
332                 error "mkdir failed to sleep, pid=$mkdirpid"
333         fi
334
335         # get sec info on second mount point
336         if [ -e "$dirname2" ]; then
337                 secinfo2=$(ls -ldZ $dirname2 | awk '{print $4}')
338         else
339                 secinfo2=""
340         fi
341
342         # get sec info on first mount point
343         wait $mkdirpid
344         secinfo1=$(ls -ldZ $dirname1 | awk '{print $4}')
345
346         # compare sec contexts
347         [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
348                 error "sec context seen from 2nd mount point is not correct"
349
350         return 0
351 }
352 run_test 20b "[atomicity] concurrent access from another client (dir)"
353
354 test_20c() {
355         local dirname1=$DIR/dd20c
356         local dirname2=$DIR2/dd20c
357         local req_delay=20
358
359         # sleep some time in ll_create_nd()
360         #define OBD_FAIL_LLITE_SETDIRSTRIPE_PAUSE     0x140b
361         do_facet client "$LCTL set_param fail_val=$req_delay fail_loc=0x140b"
362
363         # create file on first mount point
364         $LFS mkdir -c0 -i0 $dirname1 &
365         local mkdirpid=$!
366         sleep 5
367
368         if [[ -z "$(ps h -o comm -p $mkdirpid)" ]]; then
369                 error "lfs mkdir failed to sleep, pid=$mkdirpid"
370         fi
371
372         # get sec info on second mount point
373         if [ -e "$dirname2" ]; then
374                 secinfo2=$(ls -ldZ $dirname2 | awk '{print $4}')
375         else
376                 secinfo2=""
377         fi
378
379         # get sec info on first mount point
380         wait $mkdirpid
381         secinfo1=$(ls -ldZ $dirname1 | awk '{print $4}')
382
383         # compare sec contexts
384         [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
385                 error "sec context seen from 2nd mount point is not correct"
386
387         return 0
388 }
389 run_test 20c "[atomicity] concurrent access from another client (dir via lfs)"
390
391 check_nodemap() {
392         local nm=$1
393         local key=$2
394         local val=$3
395         local i
396
397         if [ "$nm" == "active" ]; then
398                 proc_param="active"
399         else
400                 proc_param="$nm.$key"
401         fi
402         is_sync=false
403         for i in $(seq 1 20); do
404                 out=$(do_facet mds1 $LCTL get_param -n \
405                                    nodemap.$proc_param 2>/dev/null)
406                 echo "On mds1, ${proc_param} = $out"
407                 [ "$val" == "$out" ] && is_sync=true && break
408                 sleep 1
409         done
410         if ! $is_sync; then
411                 error "$proc_param not updated on mds1 after 20 secs"
412         fi
413 }
414
415 create_nodemap() {
416         local nm=$1
417         local sepol
418         local client_ip=$(host_nids_address $HOSTNAME $NETTYPE)
419         local client_nid=$(h2nettype $client_ip)
420
421         do_facet mgs $LCTL nodemap_activate 1
422
423         do_facet mgs $LCTL nodemap_add $nm
424         do_facet mgs $LCTL nodemap_add_range \
425                         --name $nm --range $client_nid
426         do_facet mgs $LCTL nodemap_modify --name $nm \
427                         --property admin --value 1
428         do_facet mgs $LCTL nodemap_modify --name $nm \
429                         --property trusted --value 1
430
431         check_nodemap $nm admin_nodemap 1
432         check_nodemap $nm trusted_nodemap 1
433
434         sepol=$(l_getsepol | cut -d':' -f2- | xargs)
435         do_facet mgs $LCTL set_param nodemap.$nm.sepol="$sepol"
436         do_facet mgs $LCTL set_param -P nodemap.$nm.sepol="$sepol"
437
438         check_nodemap $nm sepol $sepol
439 }
440
441 remove_nodemap() {
442         local nm=$1
443
444         do_facet mgs $LCTL nodemap_del $nm
445
446         do_facet mgs $LCTL nodemap_activate 0
447
448         check_nodemap active x  0
449 }
450
451 test_21a() {
452         local sepol
453
454         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
455                 skip "Need MDS >= 2.11.56"
456
457         # umount client
458         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
459                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
460         fi
461         if $(grep -q $MOUNT' ' /proc/mounts); then
462                 umount_client $MOUNT || error "umount $MOUNT failed"
463         fi
464
465         # create nodemap entry with sepol
466         create_nodemap c0
467
468         if $GSS_SK; then
469                 # update mount option with skpath
470                 MOUNT_OPTS=$(add_sk_mntflag $MOUNT_OPTS)
471                 export SK_UNIQUE_NM=true
472
473                 # load specific key on servers
474                 do_nodes $(comma_list $(all_server_nodes)) "lgss_sk -t server \
475                                                     -l $SK_PATH/nodemap/c0.key"
476
477                 # set perms for per-nodemap keys else permission denied
478                 do_nodes $(comma_list $(all_server_nodes)) \
479                  "keyctl show | grep lustre | cut -c1-11 |
480                                 sed -e 's/ //g;' |
481                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
482
483         fi
484
485         # mount client without sending sepol
486         mount_client $MOUNT $MOUNT_OPTS &&
487                 error "client mount without sending sepol should be refused"
488
489         # mount client with sepol
490         echo -1 > /sys/module/ptlrpc/parameters/send_sepol
491         mount_client $MOUNT $MOUNT_OPTS ||
492                 error "client mount with sepol failed"
493
494         # umount client
495         umount_client $MOUNT || error "umount $MOUNT failed"
496
497         # store wrong sepol in nodemap
498         sepol="0:policy:0:0000000000000000000000000000000000000000000000000000000000000000"
499         do_facet mgs $LCTL set_param nodemap.c0.sepol="$sepol"
500         do_facet mgs $LCTL set_param -P nodemap.c0.sepol="$sepol"
501         check_nodemap c0 sepol $sepol
502
503         # mount client with sepol
504         mount_client $MOUNT $MOUNT_OPTS &&
505                 error "client mount without matching sepol should be refused"
506
507         # remove nodemap
508         remove_nodemap c0
509
510         if $GSS_SK; then
511                 export SK_UNIQUE_NM=false
512         fi
513
514         # remount client normally
515         echo 0 > /sys/module/ptlrpc/parameters/send_sepol
516         mountcli || error "normal client mount failed"
517 }
518 run_test 21a "Send sepol at connect"
519
520 test_21b() {
521         local sepol
522
523         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
524                 skip "Need MDS >= 2.11.56"
525
526         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
527         echo test > $DIR/$tdir/toopen ||
528                 error "failed to write to $DIR/$tdir/toopen"
529         touch $DIR/$tdir/ftoremove ||
530                 error "failed to create $DIR/$tdir/ftoremove"
531         touch $DIR/$tdir/ftoremove2 ||
532                 error "failed to create $DIR/$tdir/ftoremove2"
533         touch $DIR/$tdir/ftoremove3 ||
534                 error "failed to create $DIR/$tdir/ftoremove3"
535         touch $DIR/$tdir/ftoremove4 ||
536                 error "failed to create $DIR/$tdir/ftoremove4"
537         mkdir $DIR/$tdir/dtoremove ||
538                 error "failed to create $DIR/$tdir/dtoremove"
539         mkdir $DIR/$tdir/dtoremove2 ||
540                 error "failed to create $DIR/$tdir/dtoremove2"
541         mkdir $DIR/$tdir/dtoremove3 ||
542                 error "failed to create $DIR/$tdir/dtoremove3"
543         mkdir $DIR/$tdir/dtoremove4 ||
544                 error "failed to create $DIR/$tdir/dtoremove4"
545         touch $DIR/$tdir/ftorename ||
546                 error "failed to create $DIR/$tdir/ftorename"
547         mkdir $DIR/$tdir/dtorename ||
548                 error "failed to create $DIR/$tdir/dtorename"
549         setfattr -n user.myattr -v myval $DIR/$tdir/toopen ||
550                 error "failed to set xattr on $DIR/$tdir/toopen"
551         echo 3 > /proc/sys/vm/drop_caches
552
553         # create nodemap entry with sepol
554         create_nodemap c0
555
556         if $GSS_SK; then
557                 export SK_UNIQUE_NM=true
558
559                 # load specific key on servers
560                 do_nodes $(comma_list $(all_server_nodes)) "lgss_sk -t server \
561                                                     -l $SK_PATH/nodemap/c0.key"
562
563                 # set perms for per-nodemap keys else permission denied
564                 do_nodes $(comma_list $(all_server_nodes)) \
565                  "keyctl show | grep lustre | cut -c1-11 |
566                                 sed -e 's/ //g;' |
567                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
568
569         fi
570
571         # metadata ops without sending sepol
572         touch $DIR/$tdir/f0 && error "touch (1)"
573         lfs setstripe -c1 $DIR/$tdir/f1 && error "lfs setstripe (1)"
574         mkdir $DIR/$tdir/d0 && error "mkdir (1)"
575         lfs setdirstripe -i0 -c1 $DIR/$tdir/d1 && error "lfs setdirstripe (1)"
576         cat $DIR/$tdir/toopen && error "cat (1)"
577         rm -f $DIR/$tdir/ftoremove && error "rm (1)"
578         rmdir $DIR/$tdir/dtoremove && error "rmdir (1)"
579         mv $DIR/$tdir/ftorename $DIR/$tdir/ftorename2 && error "mv (1)"
580         mv $DIR/$tdir/dtorename $DIR/$tdir/dtorename2 && error "mv (2)"
581         getfattr -n user.myattr $DIR/$tdir/toopen && error "getfattr (1)"
582         setfattr -n user.myattr -v myval2 $DIR/$tdir/toopen &&
583                 error "setfattr (1)"
584         chattr +i $DIR/$tdir/toopen && error "chattr (1)"
585         lsattr $DIR/$tdir/toopen && error "lsattr (1)"
586         chattr -i $DIR/$tdir/toopen && error "chattr (1)"
587         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl1 && error "symlink (1)"
588         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl1 && error "hardlink (1)"
589
590         # metadata ops with sepol
591         echo -1 > /sys/module/ptlrpc/parameters/send_sepol
592         touch $DIR/$tdir/f2 || error "touch (2)"
593         lfs setstripe -c1 $DIR/$tdir/f3 || error "lfs setstripe (2)"
594         mkdir $DIR/$tdir/d2 || error "mkdir (2)"
595         lfs setdirstripe -i0 -c1 $DIR/$tdir/d3 || error "lfs setdirstripe (2)"
596         cat $DIR/$tdir/toopen || error "cat (2)"
597         rm -f $DIR/$tdir/ftoremove || error "rm (2)"
598         rmdir $DIR/$tdir/dtoremove || error "rmdir (2)"
599         mv $DIR/$tdir/ftorename $DIR/$tdir/ftorename2 || error "mv (3)"
600         mv $DIR/$tdir/dtorename $DIR/$tdir/dtorename2 || error "mv (4)"
601         getfattr -n user.myattr $DIR/$tdir/toopen || error "getfattr (2)"
602         setfattr -n user.myattr -v myval2 $DIR/$tdir/toopen ||
603                 error "setfattr (2)"
604         chattr +i $DIR/$tdir/toopen || error "chattr (2)"
605         lsattr $DIR/$tdir/toopen || error "lsattr (2)"
606         chattr -i $DIR/$tdir/toopen || error "chattr (2)"
607         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl2 || error "symlink (2)"
608         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl2 || error "hardlink (2)"
609         echo 3 > /proc/sys/vm/drop_caches
610
611         # store wrong sepol in nodemap
612         sepol="0:policy:0:0000000000000000000000000000000000000000000000000000000000000000"
613         do_facet mgs $LCTL set_param nodemap.c0.sepol="$sepol"
614         do_facet mgs $LCTL set_param -P nodemap.c0.sepol="$sepol"
615         check_nodemap c0 sepol $sepol
616
617         # metadata ops with sepol
618         touch $DIR/$tdir/f4 && error "touch (3)"
619         lfs setstripe -c1 $DIR/$tdir/f5 && error "lfs setstripe (3)"
620         mkdir $DIR/$tdir/d4 && error "mkdir (3)"
621         lfs setdirstripe -i0 -c1 $DIR/$tdir/d5 && error "lfs setdirstripe (3)"
622         cat $DIR/$tdir/toopen && error "cat (3)"
623         rm -f $DIR/$tdir/ftoremove2 && error "rm (3)"
624         rmdir $DIR/$tdir/dtoremove2 && error "rmdir (3)"
625         mv $DIR/$tdir/ftorename2 $DIR/$tdir/ftorename && error "mv (5)"
626         mv $DIR/$tdir/dtorename2 $DIR/$tdir/dtorename && error "mv (6)"
627         getfattr -n user.myattr $DIR/$tdir/toopen && error "getfattr (3)"
628         setfattr -n user.myattr -v myval3 $DIR/$tdir/toopen &&
629                 error "setfattr (3)"
630         chattr +i $DIR/$tdir/toopen && error "chattr (3)"
631         lsattr $DIR/$tdir/toopen && error "lsattr (3)"
632         chattr -i $DIR/$tdir/toopen && error "chattr (3)"
633         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl3 && error "symlink (3)"
634         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl3 && error "hardlink (3)"
635
636         # reset correct sepol
637         sepol=$(l_getsepol | cut -d':' -f2- | xargs)
638         do_facet mgs $LCTL set_param nodemap.c0.sepol="$sepol"
639         do_facet mgs $LCTL set_param -P nodemap.c0.sepol="$sepol"
640         check_nodemap c0 sepol $sepol
641
642         # metadata ops with sepol every 10 seconds only
643         echo 10 > /sys/module/ptlrpc/parameters/send_sepol
644         touch $DIR/$tdir/f6 || error "touch (4)"
645         lfs setstripe -c1 $DIR/$tdir/f7 || error "lfs setstripe (4)"
646         mkdir $DIR/$tdir/d6 || error "mkdir (4)"
647         lfs setdirstripe -i0 -c1 $DIR/$tdir/d7 || error "lfs setdirstripe (4)"
648         cat $DIR/$tdir/toopen || error "cat (4)"
649         rm -f $DIR/$tdir/ftoremove2 || error "rm (4)"
650         rmdir $DIR/$tdir/dtoremove2 || error "rmdir (4)"
651         mv $DIR/$tdir/ftorename2 $DIR/$tdir/ftorename || error "mv (7)"
652         mv $DIR/$tdir/dtorename2 $DIR/$tdir/dtorename || error "mv (8)"
653         getfattr -n user.myattr $DIR/$tdir/toopen || error "getfattr (4)"
654         setfattr -n user.myattr -v myval3 $DIR/$tdir/toopen ||
655                 error "setfattr (4)"
656         chattr +i $DIR/$tdir/toopen || error "chattr (4)"
657         lsattr $DIR/$tdir/toopen || error "lsattr (4)"
658         chattr -i $DIR/$tdir/toopen || error "chattr (4)"
659         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl4 || error "symlink (4)"
660         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl4 || error "hardlink (4)"
661         echo 3 > /proc/sys/vm/drop_caches
662
663         # change one SELinux boolean value
664         sebool=$(getsebool deny_ptrace | awk '{print $3}')
665         if [ "$sebool" == "off" ]; then
666                 setsebool -P deny_ptrace on
667         else
668                 setsebool -P deny_ptrace off
669         fi
670
671         # sepol should not be checked yet, so metadata ops without matching
672         # sepol should succeed
673         touch $DIR/$tdir/f8 || error "touch (5)"
674         lfs setstripe -c1 $DIR/$tdir/f9 || error "lfs setstripe (5)"
675         mkdir $DIR/$tdir/d8 || error "mkdir (5)"
676         lfs setdirstripe -i0 -c1 $DIR/$tdir/d9 || error "lfs setdirstripe (5)"
677         cat $DIR/$tdir/toopen || error "cat (5)"
678         rm -f $DIR/$tdir/ftoremove3 || error "rm (5)"
679         rmdir $DIR/$tdir/dtoremove3 || error "rmdir (5)"
680         mv $DIR/$tdir/ftorename $DIR/$tdir/ftorename2 || error "mv (9)"
681         mv $DIR/$tdir/dtorename $DIR/$tdir/dtorename2 || error "mv (10)"
682         getfattr -n user.myattr $DIR/$tdir/toopen || error "getfattr (5)"
683         setfattr -n user.myattr -v myval4 $DIR/$tdir/toopen ||
684                 error "setfattr (5)"
685         chattr +i $DIR/$tdir/toopen || error "chattr (5)"
686         lsattr $DIR/$tdir/toopen || error "lsattr (5)"
687         chattr -i $DIR/$tdir/toopen || error "chattr (5)"
688         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl5 || error "symlink (5)"
689         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl5 || error "hardlink (5)"
690         echo 3 > /proc/sys/vm/drop_caches
691
692         sleep 10
693         # metadata ops without matching sepol: should fail now
694         touch $DIR/$tdir/f10 && error "touch (6)"
695         lfs setstripe -c1 $DIR/$tdir/f11 && error "lfs setstripe (6)"
696         mkdir $DIR/$tdir/d10 && error "mkdir (6)"
697         lfs setdirstripe -i0 -c1 $DIR/$tdir/d11 && error "lfs setdirstripe (6)"
698         cat $DIR/$tdir/toopen && error "cat (6)"
699         rm -f $DIR/$tdir/ftoremove4 && error "rm (6)"
700         rmdir $DIR/$tdir/dtoremove4 && error "rmdir (6)"
701         mv $DIR/$tdir/ftorename2 $DIR/$tdir/ftorename && error "mv (11)"
702         mv $DIR/$tdir/dtorename2 $DIR/$tdir/dtorename && error "mv (12)"
703         getfattr -n user.myattr $DIR/$tdir/toopen && error "getfattr (6)"
704         setfattr -n user.myattr -v myval5 $DIR/$tdir/toopen &&
705                 error "setfattr (6)"
706         chattr +i $DIR/$tdir/toopen && error "chattr (6)"
707         lsattr $DIR/$tdir/toopen && error "lsattr (6)"
708         chattr -i $DIR/$tdir/toopen && error "chattr (6)"
709         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl6 && error "symlink (6)"
710         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl6 && error "hardlink (6)"
711
712         # restore SELinux boolean value
713         if [ "$sebool" == "off" ]; then
714                 setsebool -P deny_ptrace off
715         else
716                 setsebool -P deny_ptrace on
717         fi
718
719         # remove nodemap
720         remove_nodemap c0
721         echo 0 > /sys/module/ptlrpc/parameters/send_sepol
722
723         if $GSS_SK; then
724                 export SK_UNIQUE_NM=false
725         fi
726 }
727 run_test 21b "Send sepol for metadata ops"
728
729 complete $SECONDS
730 check_and_cleanup_lustre
731 exit_status