]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - fs/attr.c
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / fs / attr.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/attr.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
6 */
7
630d9c47 8#include <linux/export.h>
1da177e4
LT
9#include <linux/time.h>
10#include <linux/mm.h>
11#include <linux/string.h>
3f07c014 12#include <linux/sched/signal.h>
16f7e0fe 13#include <linux/capability.h>
0eeca283 14#include <linux/fsnotify.h>
1da177e4 15#include <linux/fcntl.h>
1da177e4 16#include <linux/security.h>
975d2943 17#include <linux/evm.h>
9957a504 18#include <linux/ima.h>
1da177e4 19
7eb18a7f
EB
20static 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
32static 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
2c27c65e 44/**
31051c85
JK
45 * setattr_prepare - check if attribute changes to a dentry are allowed
46 * @dentry: dentry to check
2c27c65e
CH
47 * @attr: attributes to change
48 *
49 * Check if we are allowed to change the attributes contained in @attr
31051c85
JK
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.
2c27c65e
CH
54 *
55 * Should be called as the first thing in ->setattr implementations,
56 * possibly after taking additional locks.
57 */
31051c85 58int setattr_prepare(struct dentry *dentry, struct iattr *attr)
1da177e4 59{
31051c85 60 struct inode *inode = d_inode(dentry);
1da177e4
LT
61 unsigned int ia_valid = attr->ia_valid;
62
2c27c65e
CH
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
1da177e4
LT
73 /* If force is set do it anyway. */
74 if (ia_valid & ATTR_FORCE)
030b533c 75 goto kill_priv;
1da177e4
LT
76
77 /* Make sure a caller can chown. */
7eb18a7f 78 if ((ia_valid & ATTR_UID) && !chown_ok(inode, attr->ia_uid))
2c27c65e 79 return -EPERM;
1da177e4
LT
80
81 /* Make sure caller can chgrp. */
7eb18a7f 82 if ((ia_valid & ATTR_GID) && !chgrp_ok(inode, attr->ia_gid))
2c27c65e 83 return -EPERM;
1da177e4
LT
84
85 /* Make sure a caller can chmod. */
86 if (ia_valid & ATTR_MODE) {
2e149670 87 if (!inode_owner_or_capable(inode))
2c27c65e 88 return -EPERM;
1da177e4
LT
89 /* Also check the setgid bit! */
90 if (!in_group_p((ia_valid & ATTR_GID) ? attr->ia_gid :
7fa294c8 91 inode->i_gid) &&
23adbe12 92 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
1da177e4
LT
93 attr->ia_mode &= ~S_ISGID;
94 }
95
96 /* Check for setting the inode time. */
9767d749 97 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
2e149670 98 if (!inode_owner_or_capable(inode))
2c27c65e 99 return -EPERM;
1da177e4 100 }
2c27c65e 101
030b533c
JK
102kill_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
2c27c65e 112 return 0;
1da177e4 113}
31051c85 114EXPORT_SYMBOL(setattr_prepare);
1da177e4 115
25d9e2d1 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 *
7bb46a67 122 * inode_newsize_ok must be called with i_mutex held.
123 *
25d9e2d1 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).
25d9e2d1 130 */
131int inode_newsize_ok(const struct inode *inode, loff_t offset)
132{
133 if (inode->i_size < offset) {
134 unsigned long limit;
135
d554ed89 136 limit = rlimit(RLIMIT_FSIZE);
25d9e2d1 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;
152out_sig:
153 send_sig(SIGXFSZ, current, 0);
154out_big:
155 return -EFBIG;
156}
157EXPORT_SYMBOL(inode_newsize_ok);
158
7bb46a67 159/**
6a1a90ad 160 * setattr_copy - copy simple metadata updates into the generic inode
7bb46a67 161 * @inode: the inode to be updated
162 * @attr: the new attributes
163 *
6a1a90ad 164 * setattr_copy must be called with i_mutex held.
7bb46a67 165 *
6a1a90ad 166 * setattr_copy updates the inode's metadata with that specified
25985edc 167 * in attr. Noticeably missing is inode size update, which is more complex
2c27c65e 168 * as it requires pagecache updates.
7bb46a67 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 */
6a1a90ad 174void setattr_copy(struct inode *inode, const struct iattr *attr)
1da177e4
LT
175{
176 unsigned int ia_valid = attr->ia_valid;
4a30131e 177
1da177e4
LT
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
7fa294c8 194 if (!in_group_p(inode->i_gid) &&
23adbe12 195 !capable_wrt_inode_uidgid(inode, CAP_FSETID))
1da177e4
LT
196 mode &= ~S_ISGID;
197 inode->i_mode = mode;
198 }
7bb46a67 199}
6a1a90ad 200EXPORT_SYMBOL(setattr_copy);
7bb46a67 201
27ac0ffe
BF
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 */
222int notify_change(struct dentry * dentry, struct iattr * attr, struct inode **delegated_inode)
1da177e4
LT
223{
224 struct inode *inode = dentry->d_inode;
8d334acd 225 umode_t mode = inode->i_mode;
1da177e4
LT
226 int error;
227 struct timespec now;
228 unsigned int ia_valid = attr->ia_valid;
229
5955102c 230 WARN_ON_ONCE(!inode_is_locked(inode));
c4107b30 231
beb29e05
MS
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
f2b20f6e
MS
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
69b45732 252 if ((ia_valid & ATTR_MODE)) {
8d334acd 253 umode_t amode = attr->ia_mode;
69b45732
AK
254 /* Flag setting protected by i_mutex */
255 if (is_sxid(amode))
256 inode->i_flags &= ~S_NOSEC;
257 }
258
c2050a45 259 now = current_time(inode);
1da177e4
LT
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;
b5376771 266 if (ia_valid & ATTR_KILL_PRIV) {
b5376771 267 error = security_inode_need_killpriv(dentry);
030b533c 268 if (error < 0)
b5376771 269 return error;
030b533c
JK
270 if (error == 0)
271 ia_valid = attr->ia_valid &= ~ATTR_KILL_PRIV;
b5376771 272 }
6de0ec00
JL
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
1da177e4 285 if (ia_valid & ATTR_KILL_SUID) {
1da177e4 286 if (mode & S_ISUID) {
6de0ec00
JL
287 ia_valid = attr->ia_valid |= ATTR_MODE;
288 attr->ia_mode = (inode->i_mode & ~S_ISUID);
1da177e4
LT
289 }
290 }
291 if (ia_valid & ATTR_KILL_SGID) {
1da177e4
LT
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 }
6de0ec00 300 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
1da177e4
LT
301 return 0;
302
a475acf0
SF
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
0bd23d09
EB
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
a77b72da 322 error = security_inode_setattr(dentry, attr);
27ac0ffe
BF
323 if (error)
324 return error;
325 error = try_break_deleg(inode, delegated_inode);
a77b72da
MS
326 if (error)
327 return error;
328
eef2380c 329 if (inode->i_op->setattr)
a77b72da 330 error = inode->i_op->setattr(dentry, attr);
eef2380c
CH
331 else
332 error = simple_setattr(dentry, attr);
1da177e4 333
975d2943 334 if (!error) {
0eeca283 335 fsnotify_change(dentry, ia_valid);
9957a504 336 ima_inode_post_setattr(dentry);
975d2943
MZ
337 evm_inode_post_setattr(dentry, ia_valid);
338 }
0eeca283 339
1da177e4
LT
340 return error;
341}
1da177e4 342EXPORT_SYMBOL(notify_change);