]>
git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/attr.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
8 #include <linux/export.h>
9 #include <linux/time.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>
19 static bool chown_ok(const struct inode
*inode
, kuid_t uid
)
21 if (uid_eq(current_fsuid(), inode
->i_uid
) &&
22 uid_eq(uid
, inode
->i_uid
))
24 if (capable_wrt_inode_uidgid(inode
, CAP_CHOWN
))
26 if (ns_capable(inode
->i_sb
->s_user_ns
, CAP_CHOWN
))
31 static bool chgrp_ok(const struct inode
*inode
, kgid_t gid
)
33 if (uid_eq(current_fsuid(), inode
->i_uid
) &&
34 (in_group_p(gid
) || gid_eq(gid
, inode
->i_gid
)))
36 if (capable_wrt_inode_uidgid(inode
, CAP_CHOWN
))
38 if (ns_capable(inode
->i_sb
->s_user_ns
, CAP_CHOWN
))
44 * setattr_prepare - check if attribute changes to a dentry are allowed
45 * @dentry: dentry to check
46 * @attr: attributes to change
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.
54 * Should be called as the first thing in ->setattr implementations,
55 * possibly after taking additional locks.
57 int setattr_prepare(struct dentry
*dentry
, struct iattr
*attr
)
59 struct inode
*inode
= d_inode(dentry
);
60 unsigned int ia_valid
= attr
->ia_valid
;
63 * First check size constraints. These can't be overriden using
66 if (ia_valid
& ATTR_SIZE
) {
67 int error
= inode_newsize_ok(inode
, attr
->ia_size
);
72 /* If force is set do it anyway. */
73 if (ia_valid
& ATTR_FORCE
)
76 /* Make sure a caller can chown. */
77 if ((ia_valid
& ATTR_UID
) && !chown_ok(inode
, attr
->ia_uid
))
80 /* Make sure caller can chgrp. */
81 if ((ia_valid
& ATTR_GID
) && !chgrp_ok(inode
, attr
->ia_gid
))
84 /* Make sure a caller can chmod. */
85 if (ia_valid
& ATTR_MODE
) {
86 if (!inode_owner_or_capable(inode
))
88 /* Also check the setgid bit! */
89 if (!in_group_p((ia_valid
& ATTR_GID
) ? attr
->ia_gid
:
91 !capable_wrt_inode_uidgid(inode
, CAP_FSETID
))
92 attr
->ia_mode
&= ~S_ISGID
;
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
))
102 /* User has permission for the change */
103 if (ia_valid
& ATTR_KILL_PRIV
) {
106 error
= security_inode_killpriv(dentry
);
113 EXPORT_SYMBOL(setattr_prepare
);
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
121 * inode_newsize_ok must be called with i_mutex held.
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
130 int inode_newsize_ok(const struct inode
*inode
, loff_t offset
)
132 if (inode
->i_size
< offset
) {
135 limit
= rlimit(RLIMIT_FSIZE
);
136 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
138 if (offset
> inode
->i_sb
->s_maxbytes
)
142 * truncation of in-use swapfiles is disallowed - it would
143 * cause subsequent swapout to scribble on the now-freed
146 if (IS_SWAPFILE(inode
))
152 send_sig(SIGXFSZ
, current
, 0);
156 EXPORT_SYMBOL(inode_newsize_ok
);
159 * setattr_copy - copy simple metadata updates into the generic inode
160 * @inode: the inode to be updated
161 * @attr: the new attributes
163 * setattr_copy must be called with i_mutex held.
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.
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.
173 void setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
175 unsigned int ia_valid
= attr
->ia_valid
;
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
;
193 if (!in_group_p(inode
->i_gid
) &&
194 !capable_wrt_inode_uidgid(inode
, CAP_FSETID
))
196 inode
->i_mode
= mode
;
199 EXPORT_SYMBOL(setattr_copy
);
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
207 * The caller must hold the i_mutex on the affected object.
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.
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
221 int notify_change(struct dentry
* dentry
, struct iattr
* attr
, struct inode
**delegated_inode
)
223 struct inode
*inode
= dentry
->d_inode
;
224 umode_t mode
= inode
->i_mode
;
227 unsigned int ia_valid
= attr
->ia_valid
;
229 WARN_ON_ONCE(!inode_is_locked(inode
));
231 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
| ATTR_TIMES_SET
)) {
232 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
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
240 if (ia_valid
& ATTR_TOUCH
) {
241 if (IS_IMMUTABLE(inode
))
244 if (!inode_owner_or_capable(inode
)) {
245 error
= inode_permission(inode
, MAY_WRITE
);
251 if ((ia_valid
& ATTR_MODE
)) {
252 umode_t amode
= attr
->ia_mode
;
253 /* Flag setting protected by i_mutex */
255 inode
->i_flags
&= ~S_NOSEC
;
258 now
= current_time(inode
);
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
);
270 ia_valid
= attr
->ia_valid
&= ~ATTR_KILL_PRIV
;
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.
280 if ((ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
)) &&
281 (ia_valid
& ATTR_MODE
))
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
);
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
;
296 attr
->ia_mode
&= ~S_ISGID
;
299 if (!(attr
->ia_valid
& ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
)))
303 * Verify that uid/gid changes are valid in the target
304 * namespace of the superblock.
306 if (ia_valid
& ATTR_UID
&&
307 !kuid_has_mapping(inode
->i_sb
->s_user_ns
, attr
->ia_uid
))
309 if (ia_valid
& ATTR_GID
&&
310 !kgid_has_mapping(inode
->i_sb
->s_user_ns
, attr
->ia_gid
))
313 /* Don't allow modifications of files with invalid uids or
314 * gids unless those uids & gids are being made valid.
316 if (!(ia_valid
& ATTR_UID
) && !uid_valid(inode
->i_uid
))
318 if (!(ia_valid
& ATTR_GID
) && !gid_valid(inode
->i_gid
))
321 error
= security_inode_setattr(dentry
, attr
);
324 error
= try_break_deleg(inode
, delegated_inode
);
328 if (inode
->i_op
->setattr
)
329 error
= inode
->i_op
->setattr(dentry
, attr
);
331 error
= simple_setattr(dentry
, attr
);
334 fsnotify_change(dentry
, ia_valid
);
335 ima_inode_post_setattr(dentry
);
336 evm_inode_post_setattr(dentry
, ia_valid
);
341 EXPORT_SYMBOL(notify_change
);