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/security.h>
17 /* Taken over from the old code... */
19 /* POSIX UID/GID verification for setting inode attributes. */
20 int inode_change_ok(const struct inode
*inode
, struct iattr
*attr
)
23 unsigned int ia_valid
= attr
->ia_valid
;
25 /* If force is set do it anyway. */
26 if (ia_valid
& ATTR_FORCE
)
29 /* Make sure a caller can chown. */
30 if ((ia_valid
& ATTR_UID
) &&
31 (current_fsuid() != inode
->i_uid
||
32 attr
->ia_uid
!= inode
->i_uid
) && !capable(CAP_CHOWN
))
35 /* Make sure caller can chgrp. */
36 if ((ia_valid
& ATTR_GID
) &&
37 (current_fsuid() != inode
->i_uid
||
38 (!in_group_p(attr
->ia_gid
) && attr
->ia_gid
!= inode
->i_gid
)) &&
42 /* Make sure a caller can chmod. */
43 if (ia_valid
& ATTR_MODE
) {
44 if (!is_owner_or_cap(inode
))
46 /* Also check the setgid bit! */
47 if (!in_group_p((ia_valid
& ATTR_GID
) ? attr
->ia_gid
:
48 inode
->i_gid
) && !capable(CAP_FSETID
))
49 attr
->ia_mode
&= ~S_ISGID
;
52 /* Check for setting the inode time. */
53 if (ia_valid
& (ATTR_MTIME_SET
| ATTR_ATIME_SET
| ATTR_TIMES_SET
)) {
54 if (!is_owner_or_cap(inode
))
62 EXPORT_SYMBOL(inode_change_ok
);
65 * inode_newsize_ok - may this inode be truncated to a given size
66 * @inode: the inode to be truncated
67 * @offset: the new size to assign to the inode
68 * @Returns: 0 on success, -ve errno on failure
70 * inode_newsize_ok will check filesystem limits and ulimits to check that the
71 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
72 * when necessary. Caller must not proceed with inode size change if failure is
73 * returned. @inode must be a file (not directory), with appropriate
74 * permissions to allow truncate (inode_newsize_ok does NOT check these
77 * inode_newsize_ok must be called with i_mutex held.
79 int inode_newsize_ok(const struct inode
*inode
, loff_t offset
)
81 if (inode
->i_size
< offset
) {
84 limit
= rlimit(RLIMIT_FSIZE
);
85 if (limit
!= RLIM_INFINITY
&& offset
> limit
)
87 if (offset
> inode
->i_sb
->s_maxbytes
)
91 * truncation of in-use swapfiles is disallowed - it would
92 * cause subsequent swapout to scribble on the now-freed
95 if (IS_SWAPFILE(inode
))
101 send_sig(SIGXFSZ
, current
, 0);
105 EXPORT_SYMBOL(inode_newsize_ok
);
107 int inode_setattr(struct inode
* inode
, struct iattr
* attr
)
109 unsigned int ia_valid
= attr
->ia_valid
;
111 if (ia_valid
& ATTR_SIZE
&&
112 attr
->ia_size
!= i_size_read(inode
)) {
113 int error
= vmtruncate(inode
, attr
->ia_size
);
118 if (ia_valid
& ATTR_UID
)
119 inode
->i_uid
= attr
->ia_uid
;
120 if (ia_valid
& ATTR_GID
)
121 inode
->i_gid
= attr
->ia_gid
;
122 if (ia_valid
& ATTR_ATIME
)
123 inode
->i_atime
= timespec_trunc(attr
->ia_atime
,
124 inode
->i_sb
->s_time_gran
);
125 if (ia_valid
& ATTR_MTIME
)
126 inode
->i_mtime
= timespec_trunc(attr
->ia_mtime
,
127 inode
->i_sb
->s_time_gran
);
128 if (ia_valid
& ATTR_CTIME
)
129 inode
->i_ctime
= timespec_trunc(attr
->ia_ctime
,
130 inode
->i_sb
->s_time_gran
);
131 if (ia_valid
& ATTR_MODE
) {
132 umode_t mode
= attr
->ia_mode
;
134 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
136 inode
->i_mode
= mode
;
138 mark_inode_dirty(inode
);
142 EXPORT_SYMBOL(inode_setattr
);
144 int notify_change(struct dentry
* dentry
, struct iattr
* attr
)
146 struct inode
*inode
= dentry
->d_inode
;
147 mode_t mode
= inode
->i_mode
;
150 unsigned int ia_valid
= attr
->ia_valid
;
152 if (ia_valid
& (ATTR_MODE
| ATTR_UID
| ATTR_GID
| ATTR_TIMES_SET
)) {
153 if (IS_IMMUTABLE(inode
) || IS_APPEND(inode
))
157 now
= current_fs_time(inode
->i_sb
);
159 attr
->ia_ctime
= now
;
160 if (!(ia_valid
& ATTR_ATIME_SET
))
161 attr
->ia_atime
= now
;
162 if (!(ia_valid
& ATTR_MTIME_SET
))
163 attr
->ia_mtime
= now
;
164 if (ia_valid
& ATTR_KILL_PRIV
) {
165 attr
->ia_valid
&= ~ATTR_KILL_PRIV
;
166 ia_valid
&= ~ATTR_KILL_PRIV
;
167 error
= security_inode_need_killpriv(dentry
);
169 error
= security_inode_killpriv(dentry
);
175 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
176 * that the function has the ability to reinterpret a mode change
177 * that's due to these bits. This adds an implicit restriction that
178 * no function will ever call notify_change with both ATTR_MODE and
179 * ATTR_KILL_S*ID set.
181 if ((ia_valid
& (ATTR_KILL_SUID
|ATTR_KILL_SGID
)) &&
182 (ia_valid
& ATTR_MODE
))
185 if (ia_valid
& ATTR_KILL_SUID
) {
186 if (mode
& S_ISUID
) {
187 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
188 attr
->ia_mode
= (inode
->i_mode
& ~S_ISUID
);
191 if (ia_valid
& ATTR_KILL_SGID
) {
192 if ((mode
& (S_ISGID
| S_IXGRP
)) == (S_ISGID
| S_IXGRP
)) {
193 if (!(ia_valid
& ATTR_MODE
)) {
194 ia_valid
= attr
->ia_valid
|= ATTR_MODE
;
195 attr
->ia_mode
= inode
->i_mode
;
197 attr
->ia_mode
&= ~S_ISGID
;
200 if (!(attr
->ia_valid
& ~(ATTR_KILL_SUID
| ATTR_KILL_SGID
)))
203 error
= security_inode_setattr(dentry
, attr
);
207 if (ia_valid
& ATTR_SIZE
)
208 down_write(&dentry
->d_inode
->i_alloc_sem
);
210 if (inode
->i_op
&& inode
->i_op
->setattr
) {
211 error
= inode
->i_op
->setattr(dentry
, attr
);
213 error
= inode_change_ok(inode
, attr
);
215 error
= inode_setattr(inode
, attr
);
218 if (ia_valid
& ATTR_SIZE
)
219 up_write(&dentry
->d_inode
->i_alloc_sem
);
222 fsnotify_change(dentry
, ia_valid
);
227 EXPORT_SYMBOL(notify_change
);