cdf04c2c0b984d10ce211aa22203691acb0d74a2
[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.${qa_user}
48         chown ${qa_user}:${qa_user} test.${qa_user}
49 }
50
51 #
52 # Remove our files again
53 #
54 _cleanup_files()
55 {
56         rm -f test.${qa_user}
57         rm -f test.root
58 }
59
60 # get standard environment, filters and checks
61 . ./common/rc
62 . ./common/filter
63
64 # real QA test starts here
65 _supported_fs generic
66 _supported_os Linux
67
68 _require_user
69 _need_to_be_root
70
71 #
72 # make sure we have a normal umask set
73 #
74 umask 022
75
76 #
77 # Test the ATTR_UID case
78 #
79 echo
80 echo "testing ATTR_UID"
81 echo
82
83 _create_files
84
85 echo "user: chown root owned file to qa_user (should fail)"
86 su ${qa_user} -c "chown ${qa_user} test.root"
87
88 echo "user: chown root owned file to root (should fail)"
89 su ${qa_user} -c "chown root test.root"
90
91 echo "user: chown qa_user owned file to qa_user (should succeed)"
92 su ${qa_user} -c "chown ${qa_user} test.${qa_user}"
93
94 # this would work without _POSIX_CHOWN_RESTRICTED
95 echo "user: chown qa_user owned file to root (should fail)"
96 su ${qa_user} -c "chown root test.${qa_user}"
97
98 _cleanup_files
99
100 #
101 # Test the ATTR_GID case
102 #
103 echo
104 echo "testing ATTR_GID"
105 echo
106
107 _create_files
108
109 echo "user: chgrp root owned file to root (should fail)"
110 su ${qa_user} -c "chgrp root test.root"
111
112 echo "user: chgrp qa_user owned file to root (should fail)"
113 su ${qa_user} -c "chgrp root test.${qa_user}"
114
115 echo "user: chgrp root owned file to qa_user (should fail)"
116 su ${qa_user} -c "chgrp ${qa_user} test.root"
117
118 echo "user: chgrp qa_user owned file to qa_user (should succeed)"
119 su ${qa_user} -c "chgrp ${qa_user} test.${qa_user}"
120
121 #echo "user: chgrp qa_user owned file to secondary group (should succeed)"
122 #su ${qa_user} -c "chgrp ${group2} test.${qa_user}"
123
124 _cleanup_files
125
126 #
127 # Test the ATTR_MODE case
128 #
129 echo
130 echo "testing ATTR_MODE"
131 echo
132
133 _create_files
134
135 echo "user: chmod a+r on qa_user owned file (should succeed)"
136 su ${qa_user} -c "chmod a+r test.${qa_user}"
137
138 echo "user: chmod a+r on root owned file (should fail)"
139 su ${qa_user} -c "chmod a+r test.root"
140
141 #
142 # Setup a file owned by the qa_user, but with a group ID that
143 # is not present in the qa_users group list (use root to make it easier for it)
144 # and mark it with set sgid bit
145 #
146 # From Posix (www.opengroup.org) for chmod:
147 #       "If the calling process does not have appropriate privileges, and
148 #       if the group ID of the file does not match the effective group ID
149 #       or one of the supplementary group IDs and if the file is a regular
150 #       file, bit S_ISGID (set-group-ID on execution) in the file's mode
151 #       shall be cleared upon successful return from chmod()."
152 # i.e.
153 # reg file + file's gid not in process' group set + no approp. privileges -> clear sgid
154 #
155 echo "check that the sgid bit is cleared"
156 chown ${qa_user}:root test.${qa_user}
157 chmod g+s test.${qa_user}
158
159 # and let the qa_user change permission bits
160 su ${qa_user} -c "chmod a+w test.${qa_user}"
161 stat -c '%A' test.${qa_user}
162
163 #
164 # Setup a file owned by the qa_user and with the suid bit set.
165 # A chmod by root should not clear the suid bit.
166 # There is nothing in Posix that says it should but just checking.
167 #
168 echo "check that suid bit is not cleared"
169 chmod u+s test.${qa_user}
170 chmod a+w test.${qa_user}
171 stat -c '%A' test.${qa_user}
172
173 _cleanup_files
174
175 _create_files
176 # Now test out the clear of suid/sgid for chown
177 #
178 # From Posix (www.opengroup.org) for chown:
179 #       "If the specified file is a regular file, one or more of the S_IXUSR,
180 #       S_IXGRP, or S_IXOTH bits of the file mode are set, and the process
181 #       does not have appropriate privileges, the set-user-ID (S_ISUID) and
182 #       set-group-ID (S_ISGID) bits of the file mode shall be cleared upon
183 #       successful return from chown(). If the specified file is a regular
184 #       file, one or more of the S_IXUSR, S_IXGRP, or S_IXOTH bits of the
185 #       file mode are set, and the process has appropriate privileges, it
186 #       is implementation-defined whether the set-user-ID and set-group-ID
187 #       bits are altered. If the chown() function is successfully invoked
188 #       on a file that is not a regular file and one or more of the S_IXUSR,
189 #       S_IXGRP, or S_IXOTH bits of the file mode are set, the set-user-ID
190 #       and set-group-ID bits may be cleared."
191 # i.e.
192 # reg file + exec-mode-bits set + no appropriate privileges -> clear suid,sgid
193 # reg file + exec-mode-bits set + appropriate privileges -> maybe clear suid,sgid
194 # non reg file + exec-mode-bits set + chown success on file (??) -> maybe clear suid/sgid
195 #
196 echo "check that suid/sgid bits are cleared after successful chown..."
197
198 echo "with no exec perm"
199 chmod ug+s test.${qa_user}
200 echo -n "before: "; stat -c '%A' test.${qa_user}
201 chown root test.${qa_user}
202 echo -n "after:  "; stat -c '%A' test.${qa_user}
203
204 echo "with user exec perm"
205 chmod ug+s test.${qa_user}
206 chmod u+x test.${qa_user}
207 echo -n "before: "; stat -c '%A' test.${qa_user}
208 chown root test.${qa_user}
209 echo -n "after:  "; stat -c '%A' test.${qa_user}
210
211 echo "with group exec perm"
212 chmod ug+s test.${qa_user}
213 chmod g+x test.${qa_user}
214 chmod u-x test.${qa_user}
215 echo -n "before: "; stat -c '%A' test.${qa_user}
216 chown root test.${qa_user}
217 echo -n "after:  "; stat -c '%A' test.${qa_user}
218
219 echo "with user+group exec perm"
220 chmod ug+s test.${qa_user}
221 chmod ug+x test.${qa_user}
222 echo -n "before: "; stat -c '%A' test.${qa_user}
223 chown root test.${qa_user}
224 echo -n "after:  "; stat -c '%A' test.${qa_user}
225
226 _cleanup_files
227
228 #
229 # Test ATTR_*TIMES_SET
230 #
231 echo
232 echo "testing ATTR_*TIMES_SET"
233 echo
234
235 _create_files
236
237 echo "user: touch qa_user file (should succeed)"
238 su ${qa_user} -c "touch test.${qa_user}"
239
240 echo "user: touch root file (should fail)"
241 su ${qa_user} -c "touch test.root"
242
243 _cleanup_files
244
245 # success, all done
246 echo "*** done"
247 rm -f $seqres.full
248 status=0