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