xfstests: remove _need_to_be_root
[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
76 test_root=$TEST_DIR/$seq.$$.root
77 test_user=$TEST_DIR/$seq.$$.user
78
79 #
80 # make sure we have a normal umask set
81 #
82 umask 022
83
84 #
85 # Test the ATTR_UID case
86 #
87 echo
88 echo "testing ATTR_UID"
89 echo
90
91 _create_files
92
93 echo "user: chown root owned file to qa_user (should fail)"
94 su ${qa_user} -c "chown ${qa_user} $test_root" 2>&1 | _filter_files
95
96 echo "user: chown root owned file to root (should fail)"
97 su ${qa_user} -c "chown root $test_root" 2>&1 | _filter_files
98
99 echo "user: chown qa_user owned file to qa_user (should succeed)"
100 su ${qa_user} -c "chown ${qa_user} $test_user"
101
102 # this would work without _POSIX_CHOWN_RESTRICTED
103 echo "user: chown qa_user owned file to root (should fail)"
104 su ${qa_user} -c "chown root $test_user" 2>&1 | _filter_files
105
106 _cleanup_files
107
108 #
109 # Test the ATTR_GID case
110 #
111 echo
112 echo "testing ATTR_GID"
113 echo
114
115 _create_files
116
117 echo "user: chgrp root owned file to root (should fail)"
118 su ${qa_user} -c "chgrp root $test_root" 2>&1 | _filter_files
119
120 echo "user: chgrp qa_user owned file to root (should fail)"
121 su ${qa_user} -c "chgrp root $test_user" 2>&1 | _filter_files
122
123 echo "user: chgrp root owned file to qa_user (should fail)"
124 su ${qa_user} -c "chgrp ${qa_user} $test_root" 2>&1 | _filter_files
125
126 echo "user: chgrp qa_user owned file to qa_user (should succeed)"
127 su ${qa_user} -c "chgrp ${qa_user} $test_user"
128
129 #echo "user: chgrp qa_user owned file to secondary group (should succeed)"
130 #su ${qa_user} -c "chgrp ${group2} $test_user"
131
132 _cleanup_files
133
134 #
135 # Test the ATTR_MODE case
136 #
137 echo
138 echo "testing ATTR_MODE"
139 echo
140
141 _create_files
142
143 echo "user: chmod a+r on qa_user owned file (should succeed)"
144 su ${qa_user} -c "chmod a+r $test_user"
145
146 echo "user: chmod a+r on root owned file (should fail)"
147 su ${qa_user} -c "chmod a+r $test_root" 2>&1 | _filter_files
148
149 #
150 # Setup a file owned by the qa_user, but with a group ID that
151 # is not present in the qa_users group list (use root to make it easier for it)
152 # and mark it with set sgid bit
153 #
154 # From Posix (www.opengroup.org) for chmod:
155 #       "If the calling process does not have appropriate privileges, and
156 #       if the group ID of the file does not match the effective group ID
157 #       or one of the supplementary group IDs and if the file is a regular
158 #       file, bit S_ISGID (set-group-ID on execution) in the file's mode
159 #       shall be cleared upon successful return from chmod()."
160 # i.e.
161 # reg file + file's gid not in process' group set + no approp. privileges -> clear sgid
162 #
163 echo "check that the sgid bit is cleared"
164 chown ${qa_user}:root $test_user
165 chmod g+s $test_user
166
167 # and let the qa_user change permission bits
168 su ${qa_user} -c "chmod a+w $test_user"
169 stat -c '%A' $test_user
170
171 #
172 # Setup a file owned by the qa_user and with the suid bit set.
173 # A chmod by root should not clear the suid bit.
174 # There is nothing in Posix that says it should but just checking.
175 #
176 echo "check that suid bit is not cleared"
177 chmod u+s $test_user
178 chmod a+w $test_user
179 stat -c '%A' $test_user
180
181 _cleanup_files
182
183 _create_files
184 # Now test out the clear of suid/sgid for chown
185 #
186 # From Posix (www.opengroup.org) for chown:
187 #       "If the specified file is a regular file, one or more of the S_IXUSR,
188 #       S_IXGRP, or S_IXOTH bits of the file mode are set, and the process
189 #       does not have appropriate privileges, the set-user-ID (S_ISUID) and
190 #       set-group-ID (S_ISGID) bits of the file mode shall be cleared upon
191 #       successful return from chown(). If the specified file is a regular
192 #       file, one or more of the S_IXUSR, S_IXGRP, or S_IXOTH bits of the
193 #       file mode are set, and the process has appropriate privileges, it
194 #       is implementation-defined whether the set-user-ID and set-group-ID
195 #       bits are altered. If the chown() function is successfully invoked
196 #       on a file that is not a regular file and one or more of the S_IXUSR,
197 #       S_IXGRP, or S_IXOTH bits of the file mode are set, the set-user-ID
198 #       and set-group-ID bits may be cleared."
199 # i.e.
200 # reg file + exec-mode-bits set + no appropriate privileges -> clear suid,sgid
201 # reg file + exec-mode-bits set + appropriate privileges -> maybe clear suid,sgid
202 # non reg file + exec-mode-bits set + chown success on file (??) -> maybe clear suid/sgid
203 #
204 echo "check that suid/sgid bits are cleared after successful chown..."
205
206 echo "with no exec perm"
207 chmod ug+s $test_user
208 echo -n "before: "; stat -c '%A' $test_user
209 chown root $test_user
210 echo -n "after:  "; stat -c '%A' $test_user
211
212 echo "with user exec perm"
213 chmod ug+s $test_user
214 chmod u+x $test_user
215 echo -n "before: "; stat -c '%A' $test_user
216 chown root $test_user
217 echo -n "after:  "; stat -c '%A' $test_user
218
219 echo "with group exec perm"
220 chmod ug+s $test_user
221 chmod g+x $test_user
222 chmod u-x $test_user
223 echo -n "before: "; stat -c '%A' $test_user
224 chown root $test_user
225 echo -n "after:  "; stat -c '%A' $test_user
226
227 echo "with user+group exec perm"
228 chmod ug+s $test_user
229 chmod ug+x $test_user
230 echo -n "before: "; stat -c '%A' $test_user
231 chown root $test_user
232 echo -n "after:  "; stat -c '%A' $test_user
233
234 _cleanup_files
235
236 _create_files
237 # Now test out the clear of suid/sgid for truncate
238 #
239 echo "check that suid/sgid bits are cleared after successful truncate..."
240
241 echo "with no exec perm"
242 echo frobnozzle >> $test_user
243 chmod ug+s $test_user
244 echo -n "before: "; stat -c '%A' $test_user
245 su ${qa_user} -c "echo > $test_user"
246 echo -n "after:  "; stat -c '%A' $test_user
247
248 echo "with user exec perm"
249 echo frobnozzle >> $test_user
250 chmod ug+s $test_user
251 chmod u+x $test_user
252 echo -n "before: "; stat -c '%A' $test_user
253 su ${qa_user} -c "echo > $test_user"
254 echo -n "after:  "; stat -c '%A' $test_user
255
256 echo "with group exec perm"
257 echo frobnozzle >> $test_user
258 chmod ug+s $test_user
259 chmod g+x $test_user
260 chmod u-x $test_user
261 echo -n "before: "; stat -c '%A' $test_user
262 su ${qa_user} -c "echo > $test_user"
263 echo -n "after:  "; stat -c '%A' $test_user
264
265 echo "with user+group exec perm"
266 echo frobnozzle >> $test_user
267 chmod ug+s $test_user
268 chmod ug+x $test_user
269 echo -n "before: "; stat -c '%A' $test_user
270 su ${qa_user} -c "echo > $test_user"
271 echo -n "after:  "; stat -c '%A' $test_user
272
273 #
274 # Test ATTR_*TIMES_SET
275 #
276 echo
277 echo "testing ATTR_*TIMES_SET"
278 echo
279
280 _create_files
281
282 echo "user: touch qa_user file (should succeed)"
283 su ${qa_user} -c "touch $test_user"
284
285 echo "user: touch root file (should fail)"
286 su ${qa_user} -c "touch $test_root" 2>&1 | _filter_files
287
288 _cleanup_files
289
290 # success, all done
291 echo "*** done"
292 rm -f $seqres.full
293 status=0