generic: test for non-zero used blocks while writing into a file
[xfstests-dev.git] / tests / generic / 598
1 #! /bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3 # Copyright (c) 2020 Red Hat, Inc.  All Rights Reserved.
4 #
5 # FS QA Test No. 598
6 #
7 # Test protected_regular and protected_fifos sysctls
8 #
9 seq=`basename $0`
10 seqres=$RESULT_DIR/$seq
11 echo "QA output created by $seq"
12
13 here=`pwd`
14 tmp=/tmp/$$
15 status=1        # failure is the default!
16 trap "_cleanup; exit \$status" 0 1 2 3 15
17
18 _cleanup()
19 {
20         rm -rf $TEST_DIR/$seq
21         [ ! -z "$REGULAR_PROTECTION" ] \
22                 && sysctl -qw fs.protected_regular=$REGULAR_PROTECTION
23         [ ! -z "$FIFO_PROTECTION" ] \
24                 && sysctl -qw fs.protected_fifos=$FIFO_PROTECTION
25         cd /
26         rm -f $tmp.*
27 }
28
29 # get standard environment, filters and checks
30 . ./common/rc
31 . ./common/filter
32
33 # remove previous $seqres.full before test
34 rm -f $seqres.full
35
36 # real QA test starts here
37
38 # Modify as appropriate.
39 _supported_fs generic
40 _require_test
41 _require_sysctl_variable fs.protected_regular
42 _require_sysctl_variable fs.protected_fifos
43 _require_user fsgqa2
44 # Do this SECOND so that qa_user is fsgqa, and _user_do uses that account
45 _require_user fsgqa
46
47 USER1=fsgqa2
48 USER2=fsgqa
49
50 # Save current system state to reset when done
51 REGULAR_PROTECTION=`sysctl -n fs.protected_regular`
52 FIFO_PROTECTION=`sysctl -n fs.protected_fifos`
53
54 test_access()
55 {
56         FILENAME=$1
57
58         # sticky dir is world & group writable:
59         echo "= group & world writable dir"
60         chmod og+w $TEST_DIR/$seq/sticky_dir
61         # "open -f" opens O_CREAT
62         _user_do "$XFS_IO_PROG -c \"open -f $TEST_DIR/$seq/sticky_dir/$FILENAME\""
63         # sticky dir is only group writable:
64         echo "= only group writable dir"
65         chmod o-w $TEST_DIR/$seq/sticky_dir
66         _user_do "$XFS_IO_PROG -c \"open -f $TEST_DIR/$seq/sticky_dir/$FILENAME\""
67 }
68
69 setup_tree()
70 {
71         # Create sticky dir owned by $USER2
72         mkdir -p $TEST_DIR/$seq
73         mkdir -p $TEST_DIR/$seq/sticky_dir
74         chmod 1777 $TEST_DIR/$seq/sticky_dir
75         chown $USER2.$USER2 $TEST_DIR/$seq/sticky_dir
76
77         # Create file & fifo in that dir owned by $USER1, and open
78         # normal read/write privs for world & group
79         $XFS_IO_PROG -c "open -f $TEST_DIR/$seq/sticky_dir/file"
80         chown $USER1.$USER1 $TEST_DIR/$seq/sticky_dir/file
81         chmod o+rw $TEST_DIR/$seq/sticky_dir/file
82
83         mkfifo $TEST_DIR/$seq/sticky_dir/fifo
84         chown $USER1.$USER1 $TEST_DIR/$seq/sticky_dir/fifo
85         chmod o+rw $TEST_DIR/$seq/sticky_dir/fifo
86 }
87
88 setup_tree
89
90 # First test fs.protected_regular
91 # With protection set to 1, O_CREAT opens in a world-writable sticky
92 # directory should fail if the file exists, is owned by another, and
93 # file owner != dir owner
94 #
95 # With protection set to 2, the same goes for group-writable
96 # sticky directories
97
98 echo "== Test file open when owned by another and file owner != dir owner"
99 sysctl -w fs.protected_regular=0
100 test_access file
101 sysctl -w fs.protected_regular=1
102 test_access file
103 sysctl -w fs.protected_regular=2
104 test_access file
105
106 echo
107
108 # Now test fs.protected_fifos
109 # With protection set to 1, O_CREAT opens in a world-writable sticky
110 # directory should fail if the fifo exists, is owned by another, and
111 # file owner != dir owner
112 #
113 # With protection set to 2, the same goes for group-writable
114 # sticky directories
115 echo "== Test fifo open when owned by another and fifo owner != dir owner"
116 sysctl -w fs.protected_fifos=0
117 test_access fifo
118 sysctl -w fs.protected_fifos=1
119 test_access fifo
120 sysctl -w fs.protected_fifos=2
121 test_access fifo
122
123 # success, all done
124 status=0
125 exit