blob: 96d394bdaddfa29e52b74d815dd5842e87cb6820 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080012#include <linux/capability.h>
Robert Love0eeca282005-07-12 17:06:03 -040013#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/fcntl.h>
15#include <linux/quotaops.h>
16#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18/* Taken over from the old code... */
19
20/* POSIX UID/GID verification for setting inode attributes. */
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +100021int inode_change_ok(const struct inode *inode, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022{
23 int retval = -EPERM;
24 unsigned int ia_valid = attr->ia_valid;
25
26 /* If force is set do it anyway. */
27 if (ia_valid & ATTR_FORCE)
28 goto fine;
29
30 /* Make sure a caller can chown. */
31 if ((ia_valid & ATTR_UID) &&
David Howellsda9592e2008-11-14 10:39:05 +110032 (current_fsuid() != inode->i_uid ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 attr->ia_uid != inode->i_uid) && !capable(CAP_CHOWN))
34 goto error;
35
36 /* Make sure caller can chgrp. */
37 if ((ia_valid & ATTR_GID) &&
David Howellsda9592e2008-11-14 10:39:05 +110038 (current_fsuid() != inode->i_uid ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 (!in_group_p(attr->ia_gid) && attr->ia_gid != inode->i_gid)) &&
40 !capable(CAP_CHOWN))
41 goto error;
42
43 /* Make sure a caller can chmod. */
44 if (ia_valid & ATTR_MODE) {
Satyam Sharma3bd858a2007-07-17 15:00:08 +053045 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 goto error;
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;
51 }
52
53 /* Check for setting the inode time. */
Miklos Szeredi9767d742008-07-01 15:01:26 +020054 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
Satyam Sharma3bd858a2007-07-17 15:00:08 +053055 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 goto error;
57 }
58fine:
59 retval = 0;
60error:
61 return retval;
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063EXPORT_SYMBOL(inode_change_ok);
64
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +100065/**
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
70 *
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
76 * conditions).
77 *
78 * inode_newsize_ok must be called with i_mutex held.
79 */
80int inode_newsize_ok(const struct inode *inode, loff_t offset)
81{
82 if (inode->i_size < offset) {
83 unsigned long limit;
84
85 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
86 if (limit != RLIM_INFINITY && offset > limit)
87 goto out_sig;
88 if (offset > inode->i_sb->s_maxbytes)
89 goto out_big;
90 } else {
91 /*
92 * truncation of in-use swapfiles is disallowed - it would
93 * cause subsequent swapout to scribble on the now-freed
94 * blocks.
95 */
96 if (IS_SWAPFILE(inode))
97 return -ETXTBSY;
98 }
99
100 return 0;
101out_sig:
102 send_sig(SIGXFSZ, current, 0);
103out_big:
104 return -EFBIG;
105}
106EXPORT_SYMBOL(inode_newsize_ok);
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108int inode_setattr(struct inode * inode, struct iattr * attr)
109{
110 unsigned int ia_valid = attr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
NeilBrown4a301312006-01-08 01:02:39 -0800112 if (ia_valid & ATTR_SIZE &&
113 attr->ia_size != i_size_read(inode)) {
114 int error = vmtruncate(inode, attr->ia_size);
115 if (error)
116 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
118
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;
134
135 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
136 mode &= ~S_ISGID;
137 inode->i_mode = mode;
138 }
139 mark_inode_dirty(inode);
NeilBrown4a301312006-01-08 01:02:39 -0800140
141 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143EXPORT_SYMBOL(inode_setattr);
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145int notify_change(struct dentry * dentry, struct iattr * attr)
146{
147 struct inode *inode = dentry->d_inode;
Jeff Layton6de0ec02007-10-18 03:05:20 -0700148 mode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 int error;
150 struct timespec now;
151 unsigned int ia_valid = attr->ia_valid;
152
Miklos Szeredibeb29e02008-07-01 15:01:29 +0200153 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
154 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
155 return -EPERM;
156 }
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 now = current_fs_time(inode->i_sb);
159
160 attr->ia_ctime = now;
161 if (!(ia_valid & ATTR_ATIME_SET))
162 attr->ia_atime = now;
163 if (!(ia_valid & ATTR_MTIME_SET))
164 attr->ia_mtime = now;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700165 if (ia_valid & ATTR_KILL_PRIV) {
166 attr->ia_valid &= ~ATTR_KILL_PRIV;
167 ia_valid &= ~ATTR_KILL_PRIV;
168 error = security_inode_need_killpriv(dentry);
169 if (error > 0)
170 error = security_inode_killpriv(dentry);
171 if (error)
172 return error;
173 }
Jeff Layton6de0ec02007-10-18 03:05:20 -0700174
175 /*
176 * We now pass ATTR_KILL_S*ID to the lower level setattr function so
177 * that the function has the ability to reinterpret a mode change
178 * that's due to these bits. This adds an implicit restriction that
179 * no function will ever call notify_change with both ATTR_MODE and
180 * ATTR_KILL_S*ID set.
181 */
182 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
183 (ia_valid & ATTR_MODE))
184 BUG();
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (ia_valid & ATTR_KILL_SUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (mode & S_ISUID) {
Jeff Layton6de0ec02007-10-18 03:05:20 -0700188 ia_valid = attr->ia_valid |= ATTR_MODE;
189 attr->ia_mode = (inode->i_mode & ~S_ISUID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191 }
192 if (ia_valid & ATTR_KILL_SGID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
194 if (!(ia_valid & ATTR_MODE)) {
195 ia_valid = attr->ia_valid |= ATTR_MODE;
196 attr->ia_mode = inode->i_mode;
197 }
198 attr->ia_mode &= ~S_ISGID;
199 }
200 }
Jeff Layton6de0ec02007-10-18 03:05:20 -0700201 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return 0;
203
Miklos Szeredia77b72d2008-07-30 14:06:22 +0200204 error = security_inode_setattr(dentry, attr);
205 if (error)
206 return error;
207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (ia_valid & ATTR_SIZE)
209 down_write(&dentry->d_inode->i_alloc_sem);
210
211 if (inode->i_op && inode->i_op->setattr) {
Miklos Szeredia77b72d2008-07-30 14:06:22 +0200212 error = inode->i_op->setattr(dentry, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 } else {
214 error = inode_change_ok(inode, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!error) {
216 if ((ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) ||
217 (ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid))
Jan Kara9e3509e2009-01-26 16:45:12 +0100218 error = vfs_dq_transfer(inode, attr) ?
219 -EDQUOT : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!error)
221 error = inode_setattr(inode, attr);
222 }
223 }
224
225 if (ia_valid & ATTR_SIZE)
226 up_write(&dentry->d_inode->i_alloc_sem);
227
Robert Love0eeca282005-07-12 17:06:03 -0400228 if (!error)
229 fsnotify_change(dentry, ia_valid);
230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return error;
232}
233
234EXPORT_SYMBOL(notify_change);