4 # Test permission checks in ->setattr
6 #-----------------------------------------------------------------------
7 # Copyright (c) 2008 Christoph Hellwig.
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.
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.
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 #-----------------------------------------------------------------------
25 seqres=$RESULT_DIR/$seq
26 echo "QA output created by $seq"
30 status=1 # failure is the default!
31 trap "_cleanup_files; exit \$status" 0 1 2 3 15
32 tag="added by qa $seq"
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
42 # Create two files, one owned by root, one by the qa_user
48 chown ${qa_user}:${qa_user} $test_user
52 # Remove our files again
62 sed -e "s,$test_root,test.root,g" -e "s,$test_user,test.user,g"
65 # get standard environment, filters and checks
69 # real QA test starts here
77 test_root=$TEST_DIR/$seq.$$.root
78 test_user=$TEST_DIR/$seq.$$.user
81 # make sure we have a normal umask set
86 # Test the ATTR_UID case
89 echo "testing ATTR_UID"
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
97 echo "user: chown root owned file to root (should fail)"
98 su ${qa_user} -c "chown root $test_root" 2>&1 | _filter_files
100 echo "user: chown qa_user owned file to qa_user (should succeed)"
101 su ${qa_user} -c "chown ${qa_user} $test_user"
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
110 # Test the ATTR_GID case
113 echo "testing ATTR_GID"
118 echo "user: chgrp root owned file to root (should fail)"
119 su ${qa_user} -c "chgrp root $test_root" 2>&1 | _filter_files
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
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
127 echo "user: chgrp qa_user owned file to qa_user (should succeed)"
128 su ${qa_user} -c "chgrp ${qa_user} $test_user"
130 #echo "user: chgrp qa_user owned file to secondary group (should succeed)"
131 #su ${qa_user} -c "chgrp ${group2} $test_user"
136 # Test the ATTR_MODE case
139 echo "testing ATTR_MODE"
144 echo "user: chmod a+r on qa_user owned file (should succeed)"
145 su ${qa_user} -c "chmod a+r $test_user"
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
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
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()."
162 # reg file + file's gid not in process' group set + no approp. privileges -> clear sgid
164 echo "check that the sgid bit is cleared"
165 chown ${qa_user}:root $test_user
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
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.
177 echo "check that suid bit is not cleared"
180 stat -c '%A' $test_user
185 # Now test out the clear of suid/sgid for chown
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."
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
205 echo "check that suid/sgid bits are cleared after successful chown..."
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
213 echo "with user exec perm"
214 chmod ug+s $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
220 echo "with group exec perm"
221 chmod ug+s $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
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
238 # Now test out the clear of suid/sgid for truncate
240 echo "check that suid/sgid bits are cleared after successful truncate..."
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
249 echo "with user exec perm"
250 echo frobnozzle >> $test_user
251 chmod ug+s $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
257 echo "with group exec perm"
258 echo frobnozzle >> $test_user
259 chmod ug+s $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
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
275 # Test ATTR_*TIMES_SET
278 echo "testing ATTR_*TIMES_SET"
283 echo "user: touch qa_user file (should succeed)"
284 su ${qa_user} -c "touch $test_user"
286 echo "user: touch root file (should fail)"
287 su ${qa_user} -c "touch $test_root" 2>&1 | _filter_files