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