cd3c0b0e6565cfad82a04163d80de98cadda0d0d
[xfstests-dev.git] / tests / generic / 193
1 #! /bin/bash
2 # FS QA Test No. 193
3 #
4 # Test permission checks in ->setattr
5 #
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2008 Christoph Hellwig.
8 #
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation.
12 #
13 # This program is distributed in the hope that it would be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write the Free Software Foundation,
20 # Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21 #-----------------------------------------------------------------------
22 #
23
24 seq=`basename $0`
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
27
28 here=`pwd`
29 tmp=/tmp/$$
30 status=1        # failure is the default!
31 trap "_cleanup_files; exit \$status" 0 1 2 3 15
32 tag="added by qa $seq"
33
34 #
35 # For some tests we need a secondary group for the qa_user.  Currently
36 # that's not available in the framework, so the tests using it are
37 # commented out.
38 #
39 #group2=foo
40
41 #
42 # Create two files, one owned by root, one by the qa_user
43 #
44 _create_files()
45 {
46         touch $test_root
47         touch $test_user
48         chown ${qa_user}:${qa_user} $test_user
49 }
50
51 #
52 # Remove our files again
53 #
54 _cleanup_files()
55 {
56         rm -f $test_user
57         rm -f $test_root
58 }
59
60 _filter_files()
61 {
62         sed -e "s,$test_root,test.root,g" -e "s,$test_user,test.user,g"
63 }
64
65 # get standard environment, filters and checks
66 . ./common/rc
67 . ./common/filter
68
69 # real QA test starts here
70 _supported_fs generic
71 _supported_os Linux
72
73 _require_test
74 _require_user
75 _need_to_be_root
76
77 test_root=$TEST_DIR/$seq.$$.root
78 test_user=$TEST_DIR/$seq.$$.user
79
80 #
81 # make sure we have a normal umask set
82 #
83 umask 022
84
85 #
86 # Test the ATTR_UID case
87 #
88 echo
89 echo "testing ATTR_UID"
90 echo
91
92 _create_files
93
94 echo "user: chown root owned file to qa_user (should fail)"
95 su ${qa_user} -c "chown ${qa_user} $test_root" 2>&1 | _filter_files
96
97 echo "user: chown root owned file to root (should fail)"
98 su ${qa_user} -c "chown root $test_root" 2>&1 | _filter_files
99
100 echo "user: chown qa_user owned file to qa_user (should succeed)"
101 su ${qa_user} -c "chown ${qa_user} $test_user"
102
103 # this would work without _POSIX_CHOWN_RESTRICTED
104 echo "user: chown qa_user owned file to root (should fail)"
105 su ${qa_user} -c "chown root $test_user" 2>&1 | _filter_files
106
107 _cleanup_files
108
109 #
110 # Test the ATTR_GID case
111 #
112 echo
113 echo "testing ATTR_GID"
114 echo
115
116 _create_files
117
118 echo "user: chgrp root owned file to root (should fail)"
119 su ${qa_user} -c "chgrp root $test_root" 2>&1 | _filter_files
120
121 echo "user: chgrp qa_user owned file to root (should fail)"
122 su ${qa_user} -c "chgrp root $test_user" 2>&1 | _filter_files
123
124 echo "user: chgrp root owned file to qa_user (should fail)"
125 su ${qa_user} -c "chgrp ${qa_user} $test_root" 2>&1 | _filter_files
126
127 echo "user: chgrp qa_user owned file to qa_user (should succeed)"
128 su ${qa_user} -c "chgrp ${qa_user} $test_user"
129
130 #echo "user: chgrp qa_user owned file to secondary group (should succeed)"
131 #su ${qa_user} -c "chgrp ${group2} $test_user"
132
133 _cleanup_files
134
135 #
136 # Test the ATTR_MODE case
137 #
138 echo
139 echo "testing ATTR_MODE"
140 echo
141
142 _create_files
143
144 echo "user: chmod a+r on qa_user owned file (should succeed)"
145 su ${qa_user} -c "chmod a+r $test_user"
146
147 echo "user: chmod a+r on root owned file (should fail)"
148 su ${qa_user} -c "chmod a+r $test_root" 2>&1 | _filter_files
149
150 #
151 # Setup a file owned by the qa_user, but with a group ID that
152 # is not present in the qa_users group list (use root to make it easier for it)
153 # and mark it with set sgid bit
154 #
155 # From Posix (www.opengroup.org) for chmod:
156 #       "If the calling process does not have appropriate privileges, and
157 #       if the group ID of the file does not match the effective group ID
158 #       or one of the supplementary group IDs and if the file is a regular
159 #       file, bit S_ISGID (set-group-ID on execution) in the file's mode
160 #       shall be cleared upon successful return from chmod()."
161 # i.e.
162 # reg file + file's gid not in process' group set + no approp. privileges -> clear sgid
163 #
164 echo "check that the sgid bit is cleared"
165 chown ${qa_user}:root $test_user
166 chmod g+s $test_user
167
168 # and let the qa_user change permission bits
169 su ${qa_user} -c "chmod a+w $test_user"
170 stat -c '%A' $test_user
171
172 #
173 # Setup a file owned by the qa_user and with the suid bit set.
174 # A chmod by root should not clear the suid bit.
175 # There is nothing in Posix that says it should but just checking.
176 #
177 echo "check that suid bit is not cleared"
178 chmod u+s $test_user
179 chmod a+w $test_user
180 stat -c '%A' $test_user
181
182 _cleanup_files
183
184 _create_files
185 # Now test out the clear of suid/sgid for chown
186 #
187 # From Posix (www.opengroup.org) for chown:
188 #       "If the specified file is a regular file, one or more of the S_IXUSR,
189 #       S_IXGRP, or S_IXOTH bits of the file mode are set, and the process
190 #       does not have appropriate privileges, the set-user-ID (S_ISUID) and
191 #       set-group-ID (S_ISGID) bits of the file mode shall be cleared upon
192 #       successful return from chown(). If the specified file is a regular
193 #       file, one or more of the S_IXUSR, S_IXGRP, or S_IXOTH bits of the
194 #       file mode are set, and the process has appropriate privileges, it
195 #       is implementation-defined whether the set-user-ID and set-group-ID
196 #       bits are altered. If the chown() function is successfully invoked
197 #       on a file that is not a regular file and one or more of the S_IXUSR,
198 #       S_IXGRP, or S_IXOTH bits of the file mode are set, the set-user-ID
199 #       and set-group-ID bits may be cleared."
200 # i.e.
201 # reg file + exec-mode-bits set + no appropriate privileges -> clear suid,sgid
202 # reg file + exec-mode-bits set + appropriate privileges -> maybe clear suid,sgid
203 # non reg file + exec-mode-bits set + chown success on file (??) -> maybe clear suid/sgid
204 #
205 echo "check that suid/sgid bits are cleared after successful chown..."
206
207 echo "with no exec perm"
208 chmod ug+s $test_user
209 echo -n "before: "; stat -c '%A' $test_user
210 chown root $test_user
211 echo -n "after:  "; stat -c '%A' $test_user
212
213 echo "with user exec perm"
214 chmod ug+s $test_user
215 chmod u+x $test_user
216 echo -n "before: "; stat -c '%A' $test_user
217 chown root $test_user
218 echo -n "after:  "; stat -c '%A' $test_user
219
220 echo "with group exec perm"
221 chmod ug+s $test_user
222 chmod g+x $test_user
223 chmod u-x $test_user
224 echo -n "before: "; stat -c '%A' $test_user
225 chown root $test_user
226 echo -n "after:  "; stat -c '%A' $test_user
227
228 echo "with user+group exec perm"
229 chmod ug+s $test_user
230 chmod ug+x $test_user
231 echo -n "before: "; stat -c '%A' $test_user
232 chown root $test_user
233 echo -n "after:  "; stat -c '%A' $test_user
234
235 _cleanup_files
236
237 _create_files
238 # Now test out the clear of suid/sgid for truncate
239 #
240 echo "check that suid/sgid bits are cleared after successful truncate..."
241
242 echo "with no exec perm"
243 echo frobnozzle >> $test_user
244 chmod ug+s $test_user
245 echo -n "before: "; stat -c '%A' $test_user
246 su ${qa_user} -c "echo > $test_user"
247 echo -n "after:  "; stat -c '%A' $test_user
248
249 echo "with user exec perm"
250 echo frobnozzle >> $test_user
251 chmod ug+s $test_user
252 chmod u+x $test_user
253 echo -n "before: "; stat -c '%A' $test_user
254 su ${qa_user} -c "echo > $test_user"
255 echo -n "after:  "; stat -c '%A' $test_user
256
257 echo "with group exec perm"
258 echo frobnozzle >> $test_user
259 chmod ug+s $test_user
260 chmod g+x $test_user
261 chmod u-x $test_user
262 echo -n "before: "; stat -c '%A' $test_user
263 su ${qa_user} -c "echo > $test_user"
264 echo -n "after:  "; stat -c '%A' $test_user
265
266 echo "with user+group exec perm"
267 echo frobnozzle >> $test_user
268 chmod ug+s $test_user
269 chmod ug+x $test_user
270 echo -n "before: "; stat -c '%A' $test_user
271 su ${qa_user} -c "echo > $test_user"
272 echo -n "after:  "; stat -c '%A' $test_user
273
274 #
275 # Test ATTR_*TIMES_SET
276 #
277 echo
278 echo "testing ATTR_*TIMES_SET"
279 echo
280
281 _create_files
282
283 echo "user: touch qa_user file (should succeed)"
284 su ${qa_user} -c "touch $test_user"
285
286 echo "user: touch root file (should fail)"
287 su ${qa_user} -c "touch $test_root" 2>&1 | _filter_files
288
289 _cleanup_files
290
291 # success, all done
292 echo "*** done"
293 rm -f $seqres.full
294 status=0