]>
git.proxmox.com Git - mirror_ubuntu-artful-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/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>
20 static bool chown_ok(const struct inode
*inode
, kuid_t uid
)
22 if (uid_eq(current_fsuid(), inode
->i_uid
) &&
23 uid_eq(uid
, inode
->i_uid
))
25 if (capable_wrt_inode_uidgid(inode
, CAP_CHOWN
))
27 if (ns_capable(inode
->i_sb
->s_user_ns
, CAP_CHOWN
))
32 static bool chgrp_ok(const struct inode
*inode
, kgid_t gid
)
34 if (uid_eq(current_fsuid(), inode
->i_uid
) &&
35 (in_group_p(gid
) || gid_eq(gid
, inode
->i_gid
)))
37 if (capable_wrt_inode_uidgid(inode
, CAP_CHOWN
))
39 if (ns_capable(inode
->i_sb
->s_user_ns
, CAP_CHOWN
))
45 * setattr_prepare - check if attribute changes to a dentry are allowed
46 * @dentry: dentry to check
47 * @attr: attributes to change
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.
55 * Should be called as the first thing in ->setattr implementations,
56 * possibly after taking additional locks.
58 int setattr_prepare(struct dentry
*dentry
, struct iattr
*attr
)
60 struct inode
*inode
= d_inode(dentry
);
61 unsigned int ia_valid
= attr
->ia_valid
;
64 * First check size constraints. These can't be overriden using
67 if (ia_valid
& ATTR_SIZE
) {
68 int error
= inode_newsize_ok(inode
, attr
->ia_size
);
73 /* If force is set do it anyway. */
74 if (ia_valid
& ATTR_FORCE
)
77 /* Make sure a caller can chown. */
78 if ((ia_valid
& ATTR_UID
) && !chown_ok(inode
, attr
->ia_uid
))
81 /* Make sure caller can chgrp. */
82 if ((ia_valid
& ATTR_GID
) && !chgrp_ok(inode
, attr
->ia_gid
))
85 /* Make sure a caller can chmod. */
86 if (ia_valid
& ATTR_MODE
) {
87 if (!inode_owner_or_capable(inode
))
89 /* Also check the setgid bit! */
90 if (!in_group_p((ia_valid
& ATTR_GID
) ? attr
->ia_gid
:
92 !capable_wrt_inode_uidgid(inode
, CAP_FSETID
))
93 attr
->ia_mode
&= ~S_ISGID
;
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
))
103 /* User has permission for the change */
104 if (ia_valid
& ATTR_KILL_PRIV
) {
107 error
= security_inode_killpriv(dentry
);
114 EXPORT_SYMBOL(setattr_prepare
);
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
122 * inode_newsize_ok must be called with i_mutex held.
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
131 int inode_newsize_ok(const struct inode
*inode
, loff_t offset
)
133 if (inode
->i_size
< offset
) {
136 limit
= rlimit(RLIMIT_FSIZE
);
137 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
139 if (offset
> inode
->i_sb
->s_maxbytes
)
143 * truncation of in-use swapfiles is disallowed - it would
144 * cause subsequent swapout to scribble on the now-freed
147 if (IS_SWAPFILE(inode
))
153 send_sig(SIGXFSZ
, current
, 0);
157 EXPORT_SYMBOL(inode_newsize_ok
);
160 * setattr_copy - copy simple metadata updates into the generic inode
161 * @inode: the inode to be updated
162 * @attr: the new attributes
164 * setattr_copy must be called with i_mutex held.
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.
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.
174 void setattr_copy(struct inode
*inode
, const struct iattr
*attr
)
176 unsigned int ia_valid
= attr
->ia_valid
;
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
;
194 if (!in_group_p(inode
->i_gid
) &&
195 !capable_wrt_inode_uidgid(inode
, CAP_FSETID
))
197 inode
->i_mode
= mode
;
200 EXPORT_SYMBOL(setattr_copy
);
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
208 * The caller must hold the i_mutex on the affected object.
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.
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
222 int notify_change(struct dentry
* dentry
, struct iattr
* attr
, struct inode
**delegated_inode
)
224 struct inode
*inode
= dentry
->d_inode
;
225 umode_t mode
= inode
->i_mode
;
228 unsigned int ia_valid
= attr
->ia_valid
;
230 WARN_ON_ONCE(!inode_is_locked(inode
));
232 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
| ATTR_TIMES_SET
)) {
233 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
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
241 if (ia_valid
& ATTR_TOUCH
) {
242 if (IS_IMMUTABLE(inode
))
245 if (!inode_owner_or_capable(inode
)) {
246 error
= inode_permission(inode
, MAY_WRITE
);
252 if ((ia_valid
& ATTR_MODE
)) {
253 umode_t amode
= attr
->ia_mode
;
254 /* Flag setting protected by i_mutex */
256 inode
->i_flags
&= ~S_NOSEC
;
259 now
= current_time(inode
);
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
);
271 ia_valid
= attr
->ia_valid
&= ~ATTR_KILL_PRIV
;
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.
281 if ((ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
)) &&
282 (ia_valid
& ATTR_MODE
))
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
);
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
;
297 attr
->ia_mode
&= ~S_ISGID
;
300 if (!(attr
->ia_valid
& ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
)))
304 * Verify that uid/gid changes are valid in the target
305 * namespace of the superblock.
307 if (ia_valid
& ATTR_UID
&&
308 !kuid_has_mapping(inode
->i_sb
->s_user_ns
, attr
->ia_uid
))
310 if (ia_valid
& ATTR_GID
&&
311 !kgid_has_mapping(inode
->i_sb
->s_user_ns
, attr
->ia_gid
))
314 /* Don't allow modifications of files with invalid uids or
315 * gids unless those uids & gids are being made valid.
317 if (!(ia_valid
& ATTR_UID
) && !uid_valid(inode
->i_uid
))
319 if (!(ia_valid
& ATTR_GID
) && !gid_valid(inode
->i_gid
))
322 error
= security_inode_setattr(dentry
, attr
);
325 error
= try_break_deleg(inode
, delegated_inode
);
329 if (inode
->i_op
->setattr
)
330 error
= inode
->i_op
->setattr(dentry
, attr
);
332 error
= simple_setattr(dentry
, attr
);
335 fsnotify_change(dentry
, ia_valid
);
336 ima_inode_post_setattr(dentry
);
337 evm_inode_post_setattr(dentry
, ia_valid
);
342 EXPORT_SYMBOL(notify_change
);