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