Whamcloud - gitweb
LU-12639 tests: initialize variable sanity 317
[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
97 test_1() {
98         local devname=$(mdsdevname 1)
99         local filename=${DIR}/${tdir}/df1
100         local mds_path=${filename#$MOUNT}
101
102         mds_path=${mds_path#/}
103
104         $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir $tdir failed"
105         touch $filename || error "cannot touch $filename"
106
107         local xattrval=$(check_selinux_xattr "mds1" $mds_path)
108
109         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
110                 error "security.selinux xattr is not set"
111 }
112 run_test 1 "create file and check security.selinux xattr is set on MDT"
113
114 test_2a() {
115         local devname=$(mdsdevname 1)
116         local dirname=${DIR}/${tdir}/dir2a
117         local mds_path=${dirname#$MOUNT}
118
119         mds_path=${mds_path#/}
120
121         $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir failed"
122         mkdir $dirname || error "cannot mkdir $dirname"
123
124         local xattrval=$(check_selinux_xattr "mds1" $mds_path)
125
126         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
127                 error "security.selinux xattr is not set"
128 }
129 run_test 2a "create dir (mkdir) and check security.selinux xattr is set on MDT"
130
131 test_2b() {
132         local devname=$(mdsdevname 1)
133         local dirname1=${DIR}/$tdir/dir2b1
134         local dirname2=${DIR}/$tdir/dir2b2
135         local mds_path=${dirname1#$MOUNT}
136
137         mds_path=${mds_path#/}
138
139         $LFS setdirstripe -i0 -c1 ${DIR}/$tdir || error "create dir failed"
140         $LFS mkdir -c0 -i0 $dirname1 || error "cannot 'lfs mkdir' $dirname1"
141
142         local xattrval=$(check_selinux_xattr "mds1" $mds_path)
143
144         mds_path=${dirname2#$MOUNT}
145         mds_path=${mds_path#/}
146
147         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
148                 error "security.selinux xattr is not set"
149
150         $LFS setdirstripe -i0 $dirname2 ||
151             error "cannot 'lfs setdirstripe' $dirname2"
152
153         xattrval=$(check_selinux_xattr "mds1" $mds_path)
154
155         [ -n "$xattrval" -a "$xattrval" != '""' ] ||
156                 error "security.selinux xattr is not set"
157 }
158 run_test 2b "create dir with lfs and check security.selinux xattr is set on MDT"
159
160 test_3() {
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 \
164                         -l $level"
165
166         mkdir -p $DIR/$tdir
167         chmod 777 $DIR/$tdir
168
169         # "access" Lustre
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"
176
177         return 0
178 }
179 run_test 3 "access with unconfined user"
180
181 test_4() {
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"
185
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
190         fi
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
195         fi
196
197         mkdir -p $DIR/$tdir
198         chmod 777 $DIR/$tdir
199
200         # "access" Lustre
201         echo "As guest_u: touch $filename"
202         $RUNAS_CMD -u $RUNAS_ID runcon $guestctx touch $filename &&
203                 error "touch $filename should have failed"
204
205         # "access" Lustre
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"
212
213         return 0
214 }
215 run_test 4 "access with specific SELinux user"
216
217 test_5() {
218         local filename=$DIR/df5
219         local newsecctx="nfs_t"
220
221         # create file
222         touch $filename || error "cannot touch $filename"
223
224         # change sec context
225         chcon -t $newsecctx $filename
226         ls -lZ $filename
227
228         # purge client's cache
229         sync ; echo 3 > /proc/sys/vm/drop_caches
230
231         # get sec context
232         ls -lZ $filename
233         local secctxseen=$(ls -lZ $filename | awk '{print $4}' | cut -d: -f3)
234
235         [ "$newsecctx" == "$secctxseen" ] ||
236                 error "sec context seen from 1st mount point is not correct"
237
238         return 0
239 }
240 run_test 5 "security context retrieval from MDT xattr"
241
242 test_10() {
243         local filename1=$DIR/df10
244         local filename2=$DIR2/df10
245         local newsecctx="nfs_t"
246
247         # create file from 1st mount point
248         touch $filename1 || error "cannot touch $filename1"
249         ls -lZ $filename1
250
251         # change sec context from 2nd mount point
252         chcon -t $newsecctx $filename2
253         ls -lZ $filename2
254
255         # get sec context from 1st mount point
256         ls -lZ $filename1
257         local secctxseen=$(ls -lZ $filename1 | awk '{print $4}' | cut -d: -f3)
258
259         [ "$newsecctx" == "$secctxseen" ] ||
260                 error_ignore LU-6784 \
261                     "sec context seen from 1st mount point is not correct"
262
263         return 0
264 }
265 run_test 10 "[consistency] concurrent security context change"
266
267 test_20a() {
268         local filename1=$DIR/$tdir/df20a
269         local filename2=$DIR2/$tdir/df20a
270         local req_delay=20
271         local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t -l s0"
272
273         mkdir -p $DIR/$tdir
274         chmod 777 $DIR/$tdir
275
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"
279
280         # create file on first mount point
281         $RUNAS_CMD -u $RUNAS_ID runcon $unconctx touch $filename1 &
282         local touchpid=$!
283         sleep 5
284
285         if [[ -z "$(ps h -o comm -p $touchpid)" ]]; then
286                 error "touch failed to sleep, pid=$touchpid"
287         fi
288
289         # get sec info on second mount point
290         if [ -e "$filename2" ]; then
291                 secinfo2=$(ls -lZ $filename2 | awk '{print $4}')
292         fi
293
294         # get sec info on first mount point
295         wait $touchpid
296         secinfo1=$(ls -lZ $filename1 | awk '{print $4}')
297
298         # compare sec contexts
299         [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
300                 error "sec context seen from 2nd mount point is not correct"
301
302         return 0
303 }
304 run_test 20a "[atomicity] concurrent access from another client (file)"
305
306 test_20b() {
307         local dirname1=$DIR/$tdir/dd20b
308         local dirname2=$DIR2/$tdir/dd20b
309         local req_delay=20
310         local unconctx="-u unconfined_u -r unconfined_r -t unconfined_t -l s0"
311
312         mkdir -p $DIR/$tdir
313         chmod 777 $DIR/$tdir
314
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"
318
319         # create file on first mount point
320         $RUNAS_CMD -u $RUNAS_ID runcon $unconctx mkdir $dirname1 &
321         local mkdirpid=$!
322         sleep 5
323
324         if [[ -z "$(ps h -o comm -p $mkdirpid)" ]]; then
325                 error "mkdir failed to sleep, pid=$mkdirpid"
326         fi
327
328         # get sec info on second mount point
329         if [ -e "$dirname2" ]; then
330                 secinfo2=$(ls -ldZ $dirname2 | awk '{print $4}')
331         else
332                 secinfo2=""
333         fi
334
335         # get sec info on first mount point
336         wait $mkdirpid
337         secinfo1=$(ls -ldZ $dirname1 | awk '{print $4}')
338
339         # compare sec contexts
340         [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
341                 error "sec context seen from 2nd mount point is not correct"
342
343         return 0
344 }
345 run_test 20b "[atomicity] concurrent access from another client (dir)"
346
347 test_20c() {
348         local dirname1=$DIR/dd20c
349         local dirname2=$DIR2/dd20c
350         local req_delay=20
351
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"
355
356         # create file on first mount point
357         $LFS mkdir -c0 -i0 $dirname1 &
358         local mkdirpid=$!
359         sleep 5
360
361         if [[ -z "$(ps h -o comm -p $mkdirpid)" ]]; then
362                 error "lfs mkdir failed to sleep, pid=$mkdirpid"
363         fi
364
365         # get sec info on second mount point
366         if [ -e "$dirname2" ]; then
367                 secinfo2=$(ls -ldZ $dirname2 | awk '{print $4}')
368         else
369                 secinfo2=""
370         fi
371
372         # get sec info on first mount point
373         wait $mkdirpid
374         secinfo1=$(ls -ldZ $dirname1 | awk '{print $4}')
375
376         # compare sec contexts
377         [ -z "$secinfo2" -o "$secinfo1" == "$secinfo2" ] ||
378                 error "sec context seen from 2nd mount point is not correct"
379
380         return 0
381 }
382 run_test 20c "[atomicity] concurrent access from another client (dir via lfs)"
383
384 cleanup_20d() {
385         umount_client $MOUNT || error "umount $MOUNT failed"
386         mountcli
387 }
388
389 trace_cmd() {
390         local cmd="$@"
391         local xattr_prefix=$(grep -E \
392                 "#define[[:space:]]+XATTR_SECURITY_PREFIX[[:space:]]+" \
393                 /usr/include/linux/xattr.h 2>/dev/null |
394                 awk '{print $3}' | sed s+\"++g)
395         local xattr_suffix=$(grep -E \
396                 "#define[[:space:]]+XATTR_SELINUX_SUFFIX[[:space:]]+" \
397                 /usr/include/linux/xattr.h 2>/dev/null |
398                 awk '{print $3}' | sed s+\"++g)
399         local xattr_name=${xattr_prefix}${xattr_suffix}
400
401         [ -z "$xattr_name" ] && xattr_name="security.selinux"
402
403         # umount client
404         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
405                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
406         fi
407         if $(grep -q $MOUNT' ' /proc/mounts); then
408                 umount_client $MOUNT || error "umount $MOUNT failed"
409         fi
410         lustre_rmmod
411         # remount client
412         mount_client $MOUNT ${MOUNT_OPTS} || error "mount client failed"
413
414         $LCTL set_param debug=+info
415         $LCTL clear
416
417         echo $cmd
418         eval $cmd
419
420         $LCTL dk | grep "get xattr '${xattr_name}'"
421         [ $? -eq 0 ] && error "get xattr event was triggered" || true
422 }
423
424 test_20d() {
425         if [ $MDS1_VERSION -lt $(version_code 2.12.50) ] ||
426            [ $CLIENT_VERSION -lt $(version_code 2.12.50) ]; then
427                 skip "Need version >= 2.12.50"
428         fi
429         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
430
431         stack_trap cleanup_20d EXIT
432
433         local dirname=$DIR/$tdir/subdir
434
435         mkdir -p $DIR/$tdir
436         mkdir $dirname
437
438         trace_cmd stat $dirname
439         trace_cmd touch $dirname/f1
440         trace_cmd stat $dirname/f1
441         trace_cmd cat $dirname/f1
442         dd if=/dev/zero of=$dirname/f1 bs=1M count=10
443         trace_cmd /usr/bin/truncate -s 10240 $dirname/f1
444         trace_cmd lfs setstripe -E -1 -S 4M $dirname/f2
445         trace_cmd lfs migrate -E -1 -S 256K $dirname/f2
446         trace_cmd lfs setdirstripe -i 1 $dirname/d2
447         trace_cmd lfs migrate -m 0 $dirname/d2
448
449         lfs setdirstripe -i 1 -c 1 $dirname/d3
450         dirname=$dirname/d3/subdir
451         mkdir $dirname
452
453         trace_cmd stat $dirname
454         trace_cmd touch $dirname/f1
455         trace_cmd stat $dirname/f1
456         trace_cmd cat $dirname/f1
457         dd if=/dev/zero of=$dirname/f1 bs=1M count=10
458         trace_cmd /usr/bin/truncate -s 10240 $dirname/f1
459         trace_cmd lfs setstripe -E -1 -S 4M $dirname/f2
460         trace_cmd lfs migrate -E -1 -S 256K $dirname/f2
461 }
462 run_test 20d "[atomicity] avoid getxattr for security context"
463
464 check_nodemap() {
465         local nm=$1
466         local key=$2
467         local val=$3
468         local i
469
470         if [ "$nm" == "active" ]; then
471                 proc_param="active"
472         else
473                 proc_param="$nm.$key"
474         fi
475         is_sync=false
476         for i in $(seq 1 20); do
477                 out=$(do_facet mds1 $LCTL get_param -n \
478                                    nodemap.$proc_param 2>/dev/null)
479                 echo "On mds1, ${proc_param} = $out"
480                 [ "$val" == "$out" ] && is_sync=true && break
481                 sleep 1
482         done
483         if ! $is_sync; then
484                 error "$proc_param not updated on mds1 after 20 secs"
485         fi
486 }
487
488 create_nodemap() {
489         local nm=$1
490         local sepol
491         local client_ip=$(host_nids_address $HOSTNAME $NETTYPE)
492         local client_nid=$(h2nettype $client_ip)
493
494         do_facet mgs $LCTL nodemap_activate 1
495
496         do_facet mgs $LCTL nodemap_add $nm
497         do_facet mgs $LCTL nodemap_add_range \
498                         --name $nm --range $client_nid
499         do_facet mgs $LCTL nodemap_modify --name $nm \
500                         --property admin --value 1
501         do_facet mgs $LCTL nodemap_modify --name $nm \
502                         --property trusted --value 1
503
504         check_nodemap $nm admin_nodemap 1
505         check_nodemap $nm trusted_nodemap 1
506
507         sepol=$(l_getsepol | cut -d':' -f2- | xargs)
508         do_facet mgs $LCTL set_param nodemap.$nm.sepol="$sepol"
509         do_facet mgs $LCTL set_param -P nodemap.$nm.sepol="$sepol"
510
511         check_nodemap $nm sepol $sepol
512 }
513
514 remove_nodemap() {
515         local nm=$1
516
517         do_facet mgs $LCTL nodemap_del $nm
518
519         do_facet mgs $LCTL nodemap_activate 0
520
521         check_nodemap active x  0
522 }
523
524 test_21a() {
525         local sepol
526
527         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
528                 skip "Need MDS >= 2.11.56"
529
530         # umount client
531         if [ "$MOUNT_2" ] && $(grep -q $MOUNT2' ' /proc/mounts); then
532                 umount_client $MOUNT2 || error "umount $MOUNT2 failed"
533         fi
534         if $(grep -q $MOUNT' ' /proc/mounts); then
535                 umount_client $MOUNT || error "umount $MOUNT failed"
536         fi
537
538         # create nodemap entry with sepol
539         create_nodemap c0
540
541         if $GSS_SK; then
542                 # update mount option with skpath
543                 MOUNT_OPTS=$(add_sk_mntflag $MOUNT_OPTS)
544                 export SK_UNIQUE_NM=true
545
546                 # load specific key on servers
547                 do_nodes $(comma_list $(all_server_nodes)) "lgss_sk -t server \
548                                                     -l $SK_PATH/nodemap/c0.key"
549
550                 # set perms for per-nodemap keys else permission denied
551                 do_nodes $(comma_list $(all_server_nodes)) \
552                  "keyctl show | grep lustre | cut -c1-11 |
553                                 sed -e 's/ //g;' |
554                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
555
556         fi
557
558         # mount client without sending sepol
559         mount_client $MOUNT $MOUNT_OPTS &&
560                 error "client mount without sending sepol should be refused"
561
562         # mount client with sepol
563         echo -1 > /sys/module/ptlrpc/parameters/send_sepol
564         mount_client $MOUNT $MOUNT_OPTS ||
565                 error "client mount with sepol failed"
566
567         # umount client
568         umount_client $MOUNT || error "umount $MOUNT failed"
569
570         # store wrong sepol in nodemap
571         sepol="0:policy:0:0000000000000000000000000000000000000000000000000000000000000000"
572         do_facet mgs $LCTL set_param nodemap.c0.sepol="$sepol"
573         do_facet mgs $LCTL set_param -P nodemap.c0.sepol="$sepol"
574         check_nodemap c0 sepol $sepol
575
576         # mount client with sepol
577         mount_client $MOUNT $MOUNT_OPTS &&
578                 error "client mount without matching sepol should be refused"
579
580         # remove nodemap
581         remove_nodemap c0
582
583         if $GSS_SK; then
584                 export SK_UNIQUE_NM=false
585         fi
586
587         # remount client normally
588         echo 0 > /sys/module/ptlrpc/parameters/send_sepol
589         mountcli || error "normal client mount failed"
590 }
591 run_test 21a "Send sepol at connect"
592
593 test_21b() {
594         local sepol
595
596         [ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.11.56) ] &&
597                 skip "Need MDS >= 2.11.56"
598
599         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
600         echo test > $DIR/$tdir/toopen ||
601                 error "failed to write to $DIR/$tdir/toopen"
602         touch $DIR/$tdir/ftoremove ||
603                 error "failed to create $DIR/$tdir/ftoremove"
604         touch $DIR/$tdir/ftoremove2 ||
605                 error "failed to create $DIR/$tdir/ftoremove2"
606         touch $DIR/$tdir/ftoremove3 ||
607                 error "failed to create $DIR/$tdir/ftoremove3"
608         touch $DIR/$tdir/ftoremove4 ||
609                 error "failed to create $DIR/$tdir/ftoremove4"
610         mkdir $DIR/$tdir/dtoremove ||
611                 error "failed to create $DIR/$tdir/dtoremove"
612         mkdir $DIR/$tdir/dtoremove2 ||
613                 error "failed to create $DIR/$tdir/dtoremove2"
614         mkdir $DIR/$tdir/dtoremove3 ||
615                 error "failed to create $DIR/$tdir/dtoremove3"
616         mkdir $DIR/$tdir/dtoremove4 ||
617                 error "failed to create $DIR/$tdir/dtoremove4"
618         touch $DIR/$tdir/ftorename ||
619                 error "failed to create $DIR/$tdir/ftorename"
620         mkdir $DIR/$tdir/dtorename ||
621                 error "failed to create $DIR/$tdir/dtorename"
622         setfattr -n user.myattr -v myval $DIR/$tdir/toopen ||
623                 error "failed to set xattr on $DIR/$tdir/toopen"
624         echo 3 > /proc/sys/vm/drop_caches
625
626         # create nodemap entry with sepol
627         create_nodemap c0
628
629         if $GSS_SK; then
630                 export SK_UNIQUE_NM=true
631
632                 # load specific key on servers
633                 do_nodes $(comma_list $(all_server_nodes)) "lgss_sk -t server \
634                                                     -l $SK_PATH/nodemap/c0.key"
635
636                 # set perms for per-nodemap keys else permission denied
637                 do_nodes $(comma_list $(all_server_nodes)) \
638                  "keyctl show | grep lustre | cut -c1-11 |
639                                 sed -e 's/ //g;' |
640                                 xargs -IX keyctl setperm X 0x3f3f3f3f"
641
642         fi
643
644         # metadata ops without sending sepol
645         touch $DIR/$tdir/f0 && error "touch (1)"
646         lfs setstripe -c1 $DIR/$tdir/f1 && error "lfs setstripe (1)"
647         mkdir $DIR/$tdir/d0 && error "mkdir (1)"
648         lfs setdirstripe -i0 -c1 $DIR/$tdir/d1 && error "lfs setdirstripe (1)"
649         cat $DIR/$tdir/toopen && error "cat (1)"
650         rm -f $DIR/$tdir/ftoremove && error "rm (1)"
651         rmdir $DIR/$tdir/dtoremove && error "rmdir (1)"
652         mv $DIR/$tdir/ftorename $DIR/$tdir/ftorename2 && error "mv (1)"
653         mv $DIR/$tdir/dtorename $DIR/$tdir/dtorename2 && error "mv (2)"
654         getfattr -n user.myattr $DIR/$tdir/toopen && error "getfattr (1)"
655         setfattr -n user.myattr -v myval2 $DIR/$tdir/toopen &&
656                 error "setfattr (1)"
657         chattr +i $DIR/$tdir/toopen && error "chattr (1)"
658         lsattr $DIR/$tdir/toopen && error "lsattr (1)"
659         chattr -i $DIR/$tdir/toopen && error "chattr (1)"
660         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl1 && error "symlink (1)"
661         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl1 && error "hardlink (1)"
662
663         # metadata ops with sepol
664         echo -1 > /sys/module/ptlrpc/parameters/send_sepol
665         touch $DIR/$tdir/f2 || error "touch (2)"
666         lfs setstripe -c1 $DIR/$tdir/f3 || error "lfs setstripe (2)"
667         mkdir $DIR/$tdir/d2 || error "mkdir (2)"
668         lfs setdirstripe -i0 -c1 $DIR/$tdir/d3 || error "lfs setdirstripe (2)"
669         cat $DIR/$tdir/toopen || error "cat (2)"
670         rm -f $DIR/$tdir/ftoremove || error "rm (2)"
671         rmdir $DIR/$tdir/dtoremove || error "rmdir (2)"
672         mv $DIR/$tdir/ftorename $DIR/$tdir/ftorename2 || error "mv (3)"
673         mv $DIR/$tdir/dtorename $DIR/$tdir/dtorename2 || error "mv (4)"
674         getfattr -n user.myattr $DIR/$tdir/toopen || error "getfattr (2)"
675         setfattr -n user.myattr -v myval2 $DIR/$tdir/toopen ||
676                 error "setfattr (2)"
677         chattr +i $DIR/$tdir/toopen || error "chattr (2)"
678         lsattr $DIR/$tdir/toopen || error "lsattr (2)"
679         chattr -i $DIR/$tdir/toopen || error "chattr (2)"
680         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl2 || error "symlink (2)"
681         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl2 || error "hardlink (2)"
682         echo 3 > /proc/sys/vm/drop_caches
683
684         # store wrong sepol in nodemap
685         sepol="0:policy:0:0000000000000000000000000000000000000000000000000000000000000000"
686         do_facet mgs $LCTL set_param nodemap.c0.sepol="$sepol"
687         do_facet mgs $LCTL set_param -P nodemap.c0.sepol="$sepol"
688         check_nodemap c0 sepol $sepol
689
690         # metadata ops with sepol
691         touch $DIR/$tdir/f4 && error "touch (3)"
692         lfs setstripe -c1 $DIR/$tdir/f5 && error "lfs setstripe (3)"
693         mkdir $DIR/$tdir/d4 && error "mkdir (3)"
694         lfs setdirstripe -i0 -c1 $DIR/$tdir/d5 && error "lfs setdirstripe (3)"
695         cat $DIR/$tdir/toopen && error "cat (3)"
696         rm -f $DIR/$tdir/ftoremove2 && error "rm (3)"
697         rmdir $DIR/$tdir/dtoremove2 && error "rmdir (3)"
698         mv $DIR/$tdir/ftorename2 $DIR/$tdir/ftorename && error "mv (5)"
699         mv $DIR/$tdir/dtorename2 $DIR/$tdir/dtorename && error "mv (6)"
700         getfattr -n user.myattr $DIR/$tdir/toopen && error "getfattr (3)"
701         setfattr -n user.myattr -v myval3 $DIR/$tdir/toopen &&
702                 error "setfattr (3)"
703         chattr +i $DIR/$tdir/toopen && error "chattr (3)"
704         lsattr $DIR/$tdir/toopen && error "lsattr (3)"
705         chattr -i $DIR/$tdir/toopen && error "chattr (3)"
706         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl3 && error "symlink (3)"
707         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl3 && error "hardlink (3)"
708
709         # reset correct sepol
710         sepol=$(l_getsepol | cut -d':' -f2- | xargs)
711         do_facet mgs $LCTL set_param nodemap.c0.sepol="$sepol"
712         do_facet mgs $LCTL set_param -P nodemap.c0.sepol="$sepol"
713         check_nodemap c0 sepol $sepol
714
715         # metadata ops with sepol every 10 seconds only
716         echo 10 > /sys/module/ptlrpc/parameters/send_sepol
717         touch $DIR/$tdir/f6 || error "touch (4)"
718         lfs setstripe -c1 $DIR/$tdir/f7 || error "lfs setstripe (4)"
719         mkdir $DIR/$tdir/d6 || error "mkdir (4)"
720         lfs setdirstripe -i0 -c1 $DIR/$tdir/d7 || error "lfs setdirstripe (4)"
721         cat $DIR/$tdir/toopen || error "cat (4)"
722         rm -f $DIR/$tdir/ftoremove2 || error "rm (4)"
723         rmdir $DIR/$tdir/dtoremove2 || error "rmdir (4)"
724         mv $DIR/$tdir/ftorename2 $DIR/$tdir/ftorename || error "mv (7)"
725         mv $DIR/$tdir/dtorename2 $DIR/$tdir/dtorename || error "mv (8)"
726         getfattr -n user.myattr $DIR/$tdir/toopen || error "getfattr (4)"
727         setfattr -n user.myattr -v myval3 $DIR/$tdir/toopen ||
728                 error "setfattr (4)"
729         chattr +i $DIR/$tdir/toopen || error "chattr (4)"
730         lsattr $DIR/$tdir/toopen || error "lsattr (4)"
731         chattr -i $DIR/$tdir/toopen || error "chattr (4)"
732         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl4 || error "symlink (4)"
733         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl4 || error "hardlink (4)"
734         echo 3 > /proc/sys/vm/drop_caches
735
736         # change one SELinux boolean value
737         sebool=$(getsebool deny_ptrace | awk '{print $3}')
738         if [ "$sebool" == "off" ]; then
739                 setsebool -P deny_ptrace on
740         else
741                 setsebool -P deny_ptrace off
742         fi
743
744         # sepol should not be checked yet, so metadata ops without matching
745         # sepol should succeed
746         touch $DIR/$tdir/f8 || error "touch (5)"
747         lfs setstripe -c1 $DIR/$tdir/f9 || error "lfs setstripe (5)"
748         mkdir $DIR/$tdir/d8 || error "mkdir (5)"
749         lfs setdirstripe -i0 -c1 $DIR/$tdir/d9 || error "lfs setdirstripe (5)"
750         cat $DIR/$tdir/toopen || error "cat (5)"
751         rm -f $DIR/$tdir/ftoremove3 || error "rm (5)"
752         rmdir $DIR/$tdir/dtoremove3 || error "rmdir (5)"
753         mv $DIR/$tdir/ftorename $DIR/$tdir/ftorename2 || error "mv (9)"
754         mv $DIR/$tdir/dtorename $DIR/$tdir/dtorename2 || error "mv (10)"
755         getfattr -n user.myattr $DIR/$tdir/toopen || error "getfattr (5)"
756         setfattr -n user.myattr -v myval4 $DIR/$tdir/toopen ||
757                 error "setfattr (5)"
758         chattr +i $DIR/$tdir/toopen || error "chattr (5)"
759         lsattr $DIR/$tdir/toopen || error "lsattr (5)"
760         chattr -i $DIR/$tdir/toopen || error "chattr (5)"
761         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl5 || error "symlink (5)"
762         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl5 || error "hardlink (5)"
763         echo 3 > /proc/sys/vm/drop_caches
764
765         sleep 10
766         # metadata ops without matching sepol: should fail now
767         touch $DIR/$tdir/f10 && error "touch (6)"
768         lfs setstripe -c1 $DIR/$tdir/f11 && error "lfs setstripe (6)"
769         mkdir $DIR/$tdir/d10 && error "mkdir (6)"
770         lfs setdirstripe -i0 -c1 $DIR/$tdir/d11 && error "lfs setdirstripe (6)"
771         cat $DIR/$tdir/toopen && error "cat (6)"
772         rm -f $DIR/$tdir/ftoremove4 && error "rm (6)"
773         rmdir $DIR/$tdir/dtoremove4 && error "rmdir (6)"
774         mv $DIR/$tdir/ftorename2 $DIR/$tdir/ftorename && error "mv (11)"
775         mv $DIR/$tdir/dtorename2 $DIR/$tdir/dtorename && error "mv (12)"
776         getfattr -n user.myattr $DIR/$tdir/toopen && error "getfattr (6)"
777         setfattr -n user.myattr -v myval5 $DIR/$tdir/toopen &&
778                 error "setfattr (6)"
779         chattr +i $DIR/$tdir/toopen && error "chattr (6)"
780         lsattr $DIR/$tdir/toopen && error "lsattr (6)"
781         chattr -i $DIR/$tdir/toopen && error "chattr (6)"
782         ln -s $DIR/$tdir/toopen $DIR/$tdir/toopen_sl6 && error "symlink (6)"
783         ln $DIR/$tdir/toopen $DIR/$tdir/toopen_hl6 && error "hardlink (6)"
784
785         # restore SELinux boolean value
786         if [ "$sebool" == "off" ]; then
787                 setsebool -P deny_ptrace off
788         else
789                 setsebool -P deny_ptrace on
790         fi
791
792         # remove nodemap
793         remove_nodemap c0
794         echo 0 > /sys/module/ptlrpc/parameters/send_sepol
795
796         if $GSS_SK; then
797                 export SK_UNIQUE_NM=false
798         fi
799 }
800 run_test 21b "Send sepol for metadata ops"
801
802 complete $SECONDS
803 check_and_cleanup_lustre
804 exit_status