]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/attr.c
UBUNTU: Start new release
[mirror_ubuntu-zesty-kernel.git] / fs / attr.c
1 /*
2 * linux/fs/attr.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
6 */
7
8 #include <linux/export.h>
9 #include <linux/time.h>
10 #include <linux/mm.h>
11 #include <linux/string.h>
12 #include <linux/capability.h>
13 #include <linux/fsnotify.h>
14 #include <linux/fcntl.h>
15 #include <linux/security.h>
16 #include <linux/evm.h>
17 #include <linux/ima.h>
18
19 static bool chown_ok(const struct inode *inode, kuid_t uid)
20 {
21 if (uid_eq(current_fsuid(), inode->i_uid) &&
22 uid_eq(uid, inode->i_uid))
23 return true;
24 if (capable_wrt_inode_uidgid(inode, CAP_CHOWN))
25 return true;
26 if (ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
27 return true;
28 return false;
29 }
30
31 static bool chgrp_ok(const struct inode *inode, kgid_t gid)
32 {
33 if (uid_eq(current_fsuid(), inode->i_uid) &&
34 (in_group_p(gid) || gid_eq(gid, inode->i_gid)))
35 return true;
36 if (capable_wrt_inode_uidgid(inode, CAP_CHOWN))
37 return true;
38 if (ns_capable(inode->i_sb->s_user_ns, CAP_CHOWN))
39 return true;
40 return false;
41 }
42
43 /**
44 * setattr_prepare - check if attribute changes to a dentry are allowed
45 * @dentry: dentry to check
46 * @attr: attributes to change
47 *
48 * Check if we are allowed to change the attributes contained in @attr
49 * in the given dentry. This includes the normal unix access permission
50 * checks, as well as checks for rlimits and others. The function also clears
51 * SGID bit from mode if user is not allowed to set it. Also file capabilities
52 * and IMA extended attributes are cleared if ATTR_KILL_PRIV is set.
53 *
54 * Should be called as the first thing in ->setattr implementations,
55 * possibly after taking additional locks.
56 */
57 int setattr_prepare(struct dentry *dentry, struct iattr *attr)
58 {
59 struct inode *inode = d_inode(dentry);
60 unsigned int ia_valid = attr->ia_valid;
61
62 /*
63 * First check size constraints. These can't be overriden using
64 * ATTR_FORCE.
65 */
66 if (ia_valid & ATTR_SIZE) {
67 int error = inode_newsize_ok(inode, attr->ia_size);
68 if (error)
69 return error;
70 }
71
72 /* If force is set do it anyway. */
73 if (ia_valid & ATTR_FORCE)
74 goto kill_priv;
75
76 /* Make sure a caller can chown. */
77 if ((ia_valid & ATTR_UID) && !chown_ok(inode, attr->ia_uid))
78 return -EPERM;
79
80 /* Make sure caller can chgrp. */
81 if ((ia_valid & ATTR_GID) && !chgrp_ok(inode, attr->ia_gid))
82 return -EPERM;
83
84 /* Make sure a caller can chmod. */
85 if (ia_valid & ATTR_MODE) {
86 if (!inode_owner_or_capable(inode))
87 return -EPERM;
88 /* Also check the setgid bit! */
89 if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
90 inode->i_gid) &&
91 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
92 attr->ia_mode &= ~S_ISGID;
93 }
94
95 /* Check for setting the inode time. */
96 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
97 if (!inode_owner_or_capable(inode))
98 return -EPERM;
99 }
100
101 kill_priv:
102 /* User has permission for the change */
103 if (ia_valid & ATTR_KILL_PRIV) {
104 int error;
105
106 error = security_inode_killpriv(dentry);
107 if (error)
108 return error;
109 }
110
111 return 0;
112 }
113 EXPORT_SYMBOL(setattr_prepare);
114
115 /**
116 * inode_newsize_ok - may this inode be truncated to a given size
117 * @inode: the inode to be truncated
118 * @offset: the new size to assign to the inode
119 * @Returns: 0 on success, -ve errno on failure
120 *
121 * inode_newsize_ok must be called with i_mutex held.
122 *
123 * inode_newsize_ok will check filesystem limits and ulimits to check that the
124 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
125 * when necessary. Caller must not proceed with inode size change if failure is
126 * returned. @inode must be a file (not directory), with appropriate
127 * permissions to allow truncate (inode_newsize_ok does NOT check these
128 * conditions).
129 */
130 int inode_newsize_ok(const struct inode *inode, loff_t offset)
131 {
132 if (inode->i_size < offset) {
133 unsigned long limit;
134
135 limit = rlimit(RLIMIT_FSIZE);
136 if (limit != RLIM_INFINITY && offset > limit)
137 goto out_sig;
138 if (offset > inode->i_sb->s_maxbytes)
139 goto out_big;
140 } else {
141 /*
142 * truncation of in-use swapfiles is disallowed - it would
143 * cause subsequent swapout to scribble on the now-freed
144 * blocks.
145 */
146 if (IS_SWAPFILE(inode))
147 return -ETXTBSY;
148 }
149
150 return 0;
151 out_sig:
152 send_sig(SIGXFSZ, current, 0);
153 out_big:
154 return -EFBIG;
155 }
156 EXPORT_SYMBOL(inode_newsize_ok);
157
158 /**
159 * setattr_copy - copy simple metadata updates into the generic inode
160 * @inode: the inode to be updated
161 * @attr: the new attributes
162 *
163 * setattr_copy must be called with i_mutex held.
164 *
165 * setattr_copy updates the inode's metadata with that specified
166 * in attr. Noticeably missing is inode size update, which is more complex
167 * as it requires pagecache updates.
168 *
169 * The inode is not marked as dirty after this operation. The rationale is
170 * that for "simple" filesystems, the struct inode is the inode storage.
171 * The caller is free to mark the inode dirty afterwards if needed.
172 */
173 void setattr_copy(struct inode *inode, const struct iattr *attr)
174 {
175 unsigned int ia_valid = attr->ia_valid;
176
177 if (ia_valid & ATTR_UID)
178 inode->i_uid = attr->ia_uid;
179 if (ia_valid & ATTR_GID)
180 inode->i_gid = attr->ia_gid;
181 if (ia_valid & ATTR_ATIME)
182 inode->i_atime = timespec_trunc(attr->ia_atime,
183 inode->i_sb->s_time_gran);
184 if (ia_valid & ATTR_MTIME)
185 inode->i_mtime = timespec_trunc(attr->ia_mtime,
186 inode->i_sb->s_time_gran);
187 if (ia_valid & ATTR_CTIME)
188 inode->i_ctime = timespec_trunc(attr->ia_ctime,
189 inode->i_sb->s_time_gran);
190 if (ia_valid & ATTR_MODE) {
191 umode_t mode = attr->ia_mode;
192
193 if (!in_group_p(inode->i_gid) &&
194 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
195 mode &= ~S_ISGID;
196 inode->i_mode = mode;
197 }
198 }
199 EXPORT_SYMBOL(setattr_copy);
200
201 /**
202 * notify_change - modify attributes of a filesytem object
203 * @dentry: object affected
204 * @iattr: new attributes
205 * @delegated_inode: returns inode, if the inode is delegated
206 *
207 * The caller must hold the i_mutex on the affected object.
208 *
209 * If notify_change discovers a delegation in need of breaking,
210 * it will return -EWOULDBLOCK and return a reference to the inode in
211 * delegated_inode. The caller should then break the delegation and
212 * retry. Because breaking a delegation may take a long time, the
213 * caller should drop the i_mutex before doing so.
214 *
215 * Alternatively, a caller may pass NULL for delegated_inode. This may
216 * be appropriate for callers that expect the underlying filesystem not
217 * to be NFS exported. Also, passing NULL is fine for callers holding
218 * the file open for write, as there can be no conflicting delegation in
219 * that case.
220 */
221 int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
222 {
223 struct inode *inode = dentry->d_inode;
224 umode_t mode = inode->i_mode;
225 int error;
226 struct timespec now;
227 unsigned int ia_valid = attr->ia_valid;
228
229 WARN_ON_ONCE(!inode_is_locked(inode));
230
231 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
232 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
233 return -EPERM;
234 }
235
236 /*
237 * If utimes(2) and friends are called with times == NULL (or both
238 * times are UTIME_NOW), then we need to check for write permission
239 */
240 if (ia_valid & ATTR_TOUCH) {
241 if (IS_IMMUTABLE(inode))
242 return -EPERM;
243
244 if (!inode_owner_or_capable(inode)) {
245 error = inode_permission(inode, MAY_WRITE);
246 if (error)
247 return error;
248 }
249 }
250
251 if ((ia_valid & ATTR_MODE)) {
252 umode_t amode = attr->ia_mode;
253 /* Flag setting protected by i_mutex */
254 if (is_sxid(amode))
255 inode->i_flags &= ~S_NOSEC;
256 }
257
258 now = current_time(inode);
259
260 attr->ia_ctime = now;
261 if (!(ia_valid & ATTR_ATIME_SET))
262 attr->ia_atime = now;
263 if (!(ia_valid & ATTR_MTIME_SET))
264 attr->ia_mtime = now;
265 if (ia_valid & ATTR_KILL_PRIV) {
266 error = security_inode_need_killpriv(dentry);
267 if (error < 0)
268 return error;
269 if (error == 0)
270 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
271 }
272
273 /*
274 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
275 * that the function has the ability to reinterpret a mode change
276 * that's due to these bits. This adds an implicit restriction that
277 * no function will ever call notify_change with both ATTR_MODE and
278 * ATTR_KILL_S*ID set.
279 */
280 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
281 (ia_valid & ATTR_MODE))
282 BUG();
283
284 if (ia_valid & ATTR_KILL_SUID) {
285 if (mode & S_ISUID) {
286 ia_valid = attr->ia_valid |= ATTR_MODE;
287 attr->ia_mode = (inode->i_mode & ~S_ISUID);
288 }
289 }
290 if (ia_valid & ATTR_KILL_SGID) {
291 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
292 if (!(ia_valid & ATTR_MODE)) {
293 ia_valid = attr->ia_valid |= ATTR_MODE;
294 attr->ia_mode = inode->i_mode;
295 }
296 attr->ia_mode &= ~S_ISGID;
297 }
298 }
299 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
300 return 0;
301
302 /*
303 * Verify that uid/gid changes are valid in the target
304 * namespace of the superblock.
305 */
306 if (ia_valid & ATTR_UID &&
307 !kuid_has_mapping(inode->i_sb->s_user_ns, attr->ia_uid))
308 return -EOVERFLOW;
309 if (ia_valid & ATTR_GID &&
310 !kgid_has_mapping(inode->i_sb->s_user_ns, attr->ia_gid))
311 return -EOVERFLOW;
312
313 /* Don't allow modifications of files with invalid uids or
314 * gids unless those uids & gids are being made valid.
315 */
316 if (!(ia_valid & ATTR_UID) && !uid_valid(inode->i_uid))
317 return -EOVERFLOW;
318 if (!(ia_valid & ATTR_GID) && !gid_valid(inode->i_gid))
319 return -EOVERFLOW;
320
321 error = security_inode_setattr(dentry, attr);
322 if (error)
323 return error;
324 error = try_break_deleg(inode, delegated_inode);
325 if (error)
326 return error;
327
328 if (inode->i_op->setattr)
329 error = inode->i_op->setattr(dentry, attr);
330 else
331 error = simple_setattr(dentry, attr);
332
333 if (!error) {
334 fsnotify_change(dentry, ia_valid);
335 ima_inode_post_setattr(dentry);
336 evm_inode_post_setattr(dentry, ia_valid);
337 }
338
339 return error;
340 }
341 EXPORT_SYMBOL(notify_change);