]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/attr.c
always call inode_change_ok early in ->setattr
[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
8#include <linux/module.h>
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>
1da177e4
LT
16
17/* Taken over from the old code... */
18
19/* POSIX UID/GID verification for setting inode attributes. */
25d9e2d1 20int inode_change_ok(const struct inode *inode, struct iattr *attr)
1da177e4
LT
21{
22 int retval = -EPERM;
23 unsigned int ia_valid = attr->ia_valid;
24
25 /* If force is set do it anyway. */
26 if (ia_valid & ATTR_FORCE)
27 goto fine;
28
29 /* Make sure a caller can chown. */
30 if ((ia_valid & ATTR_UID) &&
da9592ed 31 (current_fsuid() != inode->i_uid ||
1da177e4
LT
32 attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
33 goto error;
34
35 /* Make sure caller can chgrp. */
36 if ((ia_valid & ATTR_GID) &&
da9592ed 37 (current_fsuid() != inode->i_uid ||
1da177e4
LT
38 (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
39 !capable(CAP_CHOWN))
40 goto error;
41
42 /* Make sure a caller can chmod. */
43 if (ia_valid & ATTR_MODE) {
3bd858ab 44 if (!is_owner_or_cap(inode))
1da177e4
LT
45 goto error;
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;
50 }
51
52 /* Check for setting the inode time. */
9767d749 53 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
3bd858ab 54 if (!is_owner_or_cap(inode))
1da177e4
LT
55 goto error;
56 }
57fine:
58 retval = 0;
59error:
60 return retval;
61}
1da177e4
LT
62EXPORT_SYMBOL(inode_change_ok);
63
25d9e2d1 64/**
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
69 *
7bb46a67 70 * inode_newsize_ok must be called with i_mutex held.
71 *
25d9e2d1 72 * inode_newsize_ok will check filesystem limits and ulimits to check that the
73 * new inode size is within limits. inode_newsize_ok will also send SIGXFSZ
74 * when necessary. Caller must not proceed with inode size change if failure is
75 * returned. @inode must be a file (not directory), with appropriate
76 * permissions to allow truncate (inode_newsize_ok does NOT check these
77 * conditions).
25d9e2d1 78 */
79int inode_newsize_ok(const struct inode *inode, loff_t offset)
80{
81 if (inode->i_size < offset) {
82 unsigned long limit;
83
d554ed89 84 limit = rlimit(RLIMIT_FSIZE);
25d9e2d1 85 if (limit != RLIM_INFINITY && offset > limit)
86 goto out_sig;
87 if (offset > inode->i_sb->s_maxbytes)
88 goto out_big;
89 } else {
90 /*
91 * truncation of in-use swapfiles is disallowed - it would
92 * cause subsequent swapout to scribble on the now-freed
93 * blocks.
94 */
95 if (IS_SWAPFILE(inode))
96 return -ETXTBSY;
97 }
98
99 return 0;
100out_sig:
101 send_sig(SIGXFSZ, current, 0);
102out_big:
103 return -EFBIG;
104}
105EXPORT_SYMBOL(inode_newsize_ok);
106
7bb46a67 107/**
6a1a90ad 108 * setattr_copy - copy simple metadata updates into the generic inode
7bb46a67 109 * @inode: the inode to be updated
110 * @attr: the new attributes
111 *
6a1a90ad 112 * setattr_copy must be called with i_mutex held.
7bb46a67 113 *
6a1a90ad 114 * setattr_copy updates the inode's metadata with that specified
7bb46a67 115 * in attr. Noticably missing is inode size update, which is more complex
116 * as it requires pagecache updates. See simple_setsize.
117 *
118 * The inode is not marked as dirty after this operation. The rationale is
119 * that for "simple" filesystems, the struct inode is the inode storage.
120 * The caller is free to mark the inode dirty afterwards if needed.
121 */
6a1a90ad 122void setattr_copy(struct inode *inode, const struct iattr *attr)
1da177e4
LT
123{
124 unsigned int ia_valid = attr->ia_valid;
4a30131e 125
1da177e4
LT
126 if (ia_valid & ATTR_UID)
127 inode->i_uid = attr->ia_uid;
128 if (ia_valid & ATTR_GID)
129 inode->i_gid = attr->ia_gid;
130 if (ia_valid & ATTR_ATIME)
131 inode->i_atime = timespec_trunc(attr->ia_atime,
132 inode->i_sb->s_time_gran);
133 if (ia_valid & ATTR_MTIME)
134 inode->i_mtime = timespec_trunc(attr->ia_mtime,
135 inode->i_sb->s_time_gran);
136 if (ia_valid & ATTR_CTIME)
137 inode->i_ctime = timespec_trunc(attr->ia_ctime,
138 inode->i_sb->s_time_gran);
139 if (ia_valid & ATTR_MODE) {
140 umode_t mode = attr->ia_mode;
141
142 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
143 mode &= ~S_ISGID;
144 inode->i_mode = mode;
145 }
7bb46a67 146}
6a1a90ad 147EXPORT_SYMBOL(setattr_copy);
7bb46a67 148
1da177e4
LT
149int notify_change(struct dentry * dentry, struct iattr * attr)
150{
151 struct inode *inode = dentry->d_inode;
6de0ec00 152 mode_t mode = inode->i_mode;
1da177e4
LT
153 int error;
154 struct timespec now;
155 unsigned int ia_valid = attr->ia_valid;
156
beb29e05
MS
157 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
158 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
159 return -EPERM;
160 }
161
1da177e4
LT
162 now = current_fs_time(inode->i_sb);
163
164 attr->ia_ctime = now;
165 if (!(ia_valid & ATTR_ATIME_SET))
166 attr->ia_atime = now;
167 if (!(ia_valid & ATTR_MTIME_SET))
168 attr->ia_mtime = now;
b5376771
SH
169 if (ia_valid & ATTR_KILL_PRIV) {
170 attr->ia_valid &= ~ATTR_KILL_PRIV;
171 ia_valid &= ~ATTR_KILL_PRIV;
172 error = security_inode_need_killpriv(dentry);
173 if (error > 0)
174 error = security_inode_killpriv(dentry);
175 if (error)
176 return error;
177 }
6de0ec00
JL
178
179 /*
180 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
181 * that the function has the ability to reinterpret a mode change
182 * that's due to these bits. This adds an implicit restriction that
183 * no function will ever call notify_change with both ATTR_MODE and
184 * ATTR_KILL_S*ID set.
185 */
186 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
187 (ia_valid & ATTR_MODE))
188 BUG();
189
1da177e4 190 if (ia_valid & ATTR_KILL_SUID) {
1da177e4 191 if (mode & S_ISUID) {
6de0ec00
JL
192 ia_valid = attr->ia_valid |= ATTR_MODE;
193 attr->ia_mode = (inode->i_mode & ~S_ISUID);
1da177e4
LT
194 }
195 }
196 if (ia_valid & ATTR_KILL_SGID) {
1da177e4
LT
197 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
198 if (!(ia_valid & ATTR_MODE)) {
199 ia_valid = attr->ia_valid |= ATTR_MODE;
200 attr->ia_mode = inode->i_mode;
201 }
202 attr->ia_mode &= ~S_ISGID;
203 }
204 }
6de0ec00 205 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
1da177e4
LT
206 return 0;
207
a77b72da
MS
208 error = security_inode_setattr(dentry, attr);
209 if (error)
210 return error;
211
1da177e4
LT
212 if (ia_valid & ATTR_SIZE)
213 down_write(&dentry->d_inode->i_alloc_sem);
214
eef2380c 215 if (inode->i_op->setattr)
a77b72da 216 error = inode->i_op->setattr(dentry, attr);
eef2380c
CH
217 else
218 error = simple_setattr(dentry, attr);
1da177e4
LT
219
220 if (ia_valid & ATTR_SIZE)
221 up_write(&dentry->d_inode->i_alloc_sem);
222
0eeca283
RL
223 if (!error)
224 fsnotify_change(dentry, ia_valid);
225
1da177e4
LT
226 return error;
227}
228
229EXPORT_SYMBOL(notify_change);