4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * changes by Thomas Schoebel-Theuer
8 #include <linux/module.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/quotaops.h>
16 #include <linux/security.h>
18 /* Taken over from the old code... */
20 /* POSIX UID/GID verification for setting inode attributes. */
21 int inode_change_ok(const struct inode
*inode
, struct iattr
*attr
)
24 unsigned int ia_valid
= attr
->ia_valid
;
26 /* If force is set do it anyway. */
27 if (ia_valid
& ATTR_FORCE
)
30 /* Make sure a caller can chown. */
31 if ((ia_valid
& ATTR_UID
) &&
32 (current_fsuid() != inode
->i_uid
||
33 attr
->ia_uid
!= inode
->i_uid
) && !capable(CAP_CHOWN
))
36 /* Make sure caller can chgrp. */
37 if ((ia_valid
& ATTR_GID
) &&
38 (current_fsuid() != inode
->i_uid
||
39 (!in_group_p(attr
->ia_gid
) && attr
->ia_gid
!= inode
->i_gid
)) &&
43 /* Make sure a caller can chmod. */
44 if (ia_valid
& ATTR_MODE
) {
45 if (!is_owner_or_cap(inode
))
47 /* Also check the setgid bit! */
48 if (!in_group_p((ia_valid
& ATTR_GID
) ? attr
->ia_gid
:
49 inode
->i_gid
) && !capable(CAP_FSETID
))
50 attr
->ia_mode
&= ~S_ISGID
;
53 /* Check for setting the inode time. */
54 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
| ATTR_TIMES_SET
)) {
55 if (!is_owner_or_cap(inode
))
63 EXPORT_SYMBOL(inode_change_ok
);
66 * inode_newsize_ok - may this inode be truncated to a given size
67 * @inode: the inode to be truncated
68 * @offset: the new size to assign to the inode
69 * @Returns: 0 on success, -ve errno on failure
71 * inode_newsize_ok will check filesystem limits and ulimits to check that the
72 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
73 * when necessary. Caller must not proceed with inode size change if failure is
74 * returned. @inode must be a file (not directory), with appropriate
75 * permissions to allow truncate (inode_newsize_ok does NOT check these
78 * inode_newsize_ok must be called with i_mutex held.
80 int inode_newsize_ok(const struct inode
*inode
, loff_t offset
)
82 if (inode
->i_size
< offset
) {
85 limit
= current
->signal
->rlim
[RLIMIT_FSIZE
].rlim_cur
;
86 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
88 if (offset
> inode
->i_sb
->s_maxbytes
)
92 * truncation of in-use swapfiles is disallowed - it would
93 * cause subsequent swapout to scribble on the now-freed
96 if (IS_SWAPFILE(inode
))
102 send_sig(SIGXFSZ
, current
, 0);
106 EXPORT_SYMBOL(inode_newsize_ok
);
108 int inode_setattr(struct inode
* inode
, struct iattr
* attr
)
110 unsigned int ia_valid
= attr
->ia_valid
;
112 if (ia_valid
& ATTR_SIZE
&&
113 attr
->ia_size
!= i_size_read(inode
)) {
114 int error
= vmtruncate(inode
, attr
->ia_size
);
119 if (ia_valid
& ATTR_UID
)
120 inode
->i_uid
= attr
->ia_uid
;
121 if (ia_valid
& ATTR_GID
)
122 inode
->i_gid
= attr
->ia_gid
;
123 if (ia_valid
& ATTR_ATIME
)
124 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
125 inode
->i_sb
->s_time_gran
);
126 if (ia_valid
& ATTR_MTIME
)
127 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
128 inode
->i_sb
->s_time_gran
);
129 if (ia_valid
& ATTR_CTIME
)
130 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
131 inode
->i_sb
->s_time_gran
);
132 if (ia_valid
& ATTR_MODE
) {
133 umode_t mode
= attr
->ia_mode
;
135 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
137 inode
->i_mode
= mode
;
139 mark_inode_dirty(inode
);
143 EXPORT_SYMBOL(inode_setattr
);
145 int notify_change(struct dentry
* dentry
, struct iattr
* attr
)
147 struct inode
*inode
= dentry
->d_inode
;
148 mode_t mode
= inode
->i_mode
;
151 unsigned int ia_valid
= attr
->ia_valid
;
153 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
| ATTR_TIMES_SET
)) {
154 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
158 WARN_ON_ONCE(!mutex_is_locked(&inode
->i_mutex
));
160 now
= current_fs_time(inode
->i_sb
);
162 attr
->ia_ctime
= now
;
163 if (!(ia_valid
& ATTR_ATIME_SET
))
164 attr
->ia_atime
= now
;
165 if (!(ia_valid
& ATTR_MTIME_SET
))
166 attr
->ia_mtime
= now
;
167 if (ia_valid
& ATTR_KILL_PRIV
) {
168 attr
->ia_valid
&= ~ATTR_KILL_PRIV
;
169 ia_valid
&= ~ATTR_KILL_PRIV
;
170 error
= security_inode_need_killpriv(dentry
);
172 error
= security_inode_killpriv(dentry
);
178 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
179 * that the function has the ability to reinterpret a mode change
180 * that's due to these bits. This adds an implicit restriction that
181 * no function will ever call notify_change with both ATTR_MODE and
182 * ATTR_KILL_S*ID set.
184 if ((ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
)) &&
185 (ia_valid
& ATTR_MODE
))
188 if (ia_valid
& ATTR_KILL_SUID
) {
189 if (mode
& S_ISUID
) {
190 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
191 attr
->ia_mode
= (inode
->i_mode
& ~S_ISUID
);
194 if (ia_valid
& ATTR_KILL_SGID
) {
195 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
196 if (!(ia_valid
& ATTR_MODE
)) {
197 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
198 attr
->ia_mode
= inode
->i_mode
;
200 attr
->ia_mode
&= ~S_ISGID
;
203 if (!(attr
->ia_valid
& ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
)))
206 error
= security_inode_setattr(dentry
, attr
);
210 if (ia_valid
& ATTR_SIZE
)
211 down_write(&dentry
->d_inode
->i_alloc_sem
);
213 if (inode
->i_op
&& inode
->i_op
->setattr
) {
214 error
= inode
->i_op
->setattr(dentry
, attr
);
216 error
= inode_change_ok(inode
, attr
);
218 if ((ia_valid
& ATTR_UID
&& attr
->ia_uid
!= inode
->i_uid
) ||
219 (ia_valid
& ATTR_GID
&& attr
->ia_gid
!= inode
->i_gid
))
220 error
= vfs_dq_transfer(inode
, attr
) ?
223 error
= inode_setattr(inode
, attr
);
227 if (ia_valid
& ATTR_SIZE
)
228 up_write(&dentry
->d_inode
->i_alloc_sem
);
231 fsnotify_change(dentry
, ia_valid
);
236 EXPORT_SYMBOL(notify_change
);