blob: 0815e93bb487e0651619d942b6bd78d7fe6a2e89 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/security.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17/* Taken over from the old code... */
18
19/* POSIX UID/GID verification for setting inode attributes. */
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +100020int inode_change_ok(const struct inode *inode, struct iattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
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) &&
David Howellsda9592e2008-11-14 10:39:05 +110031 (current_fsuid() != inode->i_uid ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 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) &&
David Howellsda9592e2008-11-14 10:39:05 +110037 (current_fsuid() != inode->i_uid ||
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 (!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) {
Satyam Sharma3bd858a2007-07-17 15:00:08 +053044 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 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. */
Miklos Szeredi9767d742008-07-01 15:01:26 +020053 if (ia_valid & (ATTR_MTIME_SET | ATTR_ATIME_SET | ATTR_TIMES_SET)) {
Satyam Sharma3bd858a2007-07-17 15:00:08 +053054 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 goto error;
56 }
57fine:
58 retval = 0;
59error:
60 return retval;
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062EXPORT_SYMBOL(inode_change_ok);
63
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +100064/**
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 *
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
75 * conditions).
76 *
77 * inode_newsize_ok must be called with i_mutex held.
78 */
79int inode_newsize_ok(const struct inode *inode, loff_t offset)
80{
81 if (inode->i_size < offset) {
82 unsigned long limit;
83
Jiri Slabyd554ed892010-03-05 13:42:42 -080084 limit = rlimit(RLIMIT_FSIZE);
npiggin@suse.de25d9e2d2009-08-21 02:35:05 +100085 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107int inode_setattr(struct inode * inode, struct iattr * attr)
108{
109 unsigned int ia_valid = attr->ia_valid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
NeilBrown4a301312006-01-08 01:02:39 -0800111 if (ia_valid & ATTR_SIZE &&
112 attr->ia_size != i_size_read(inode)) {
113 int error = vmtruncate(inode, attr->ia_size);
114 if (error)
115 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 }
117
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;
133
134 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
135 mode &= ~S_ISGID;
136 inode->i_mode = mode;
137 }
138 mark_inode_dirty(inode);
NeilBrown4a301312006-01-08 01:02:39 -0800139
140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142EXPORT_SYMBOL(inode_setattr);
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144int notify_change(struct dentry * dentry, struct iattr * attr)
145{
146 struct inode *inode = dentry->d_inode;
Jeff Layton6de0ec02007-10-18 03:05:20 -0700147 mode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 int error;
149 struct timespec now;
150 unsigned int ia_valid = attr->ia_valid;
151
Miklos Szeredibeb29e02008-07-01 15:01:29 +0200152 if (ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_TIMES_SET)) {
153 if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
154 return -EPERM;
155 }
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 now = current_fs_time(inode->i_sb);
158
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;
Serge E. Hallynb5376772007-10-16 23:31:36 -0700164 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);
168 if (error > 0)
169 error = security_inode_killpriv(dentry);
170 if (error)
171 return error;
172 }
Jeff Layton6de0ec02007-10-18 03:05:20 -0700173
174 /*
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.
180 */
181 if ((ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) &&
182 (ia_valid & ATTR_MODE))
183 BUG();
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (ia_valid & ATTR_KILL_SUID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (mode & S_ISUID) {
Jeff Layton6de0ec02007-10-18 03:05:20 -0700187 ia_valid = attr->ia_valid |= ATTR_MODE;
188 attr->ia_mode = (inode->i_mode & ~S_ISUID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 }
191 if (ia_valid & ATTR_KILL_SGID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 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;
196 }
197 attr->ia_mode &= ~S_ISGID;
198 }
199 }
Jeff Layton6de0ec02007-10-18 03:05:20 -0700200 if (!(attr->ia_valid & ~(ATTR_KILL_SUID | ATTR_KILL_SGID)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return 0;
202
Miklos Szeredia77b72d2008-07-30 14:06:22 +0200203 error = security_inode_setattr(dentry, attr);
204 if (error)
205 return error;
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 if (ia_valid & ATTR_SIZE)
208 down_write(&dentry->d_inode->i_alloc_sem);
209
210 if (inode->i_op && inode->i_op->setattr) {
Miklos Szeredia77b72d2008-07-30 14:06:22 +0200211 error = inode->i_op->setattr(dentry, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 } else {
213 error = inode_change_ok(inode, attr);
Christoph Hellwig759bfee2010-03-03 09:05:02 -0500214 if (!error)
215 error = inode_setattr(inode, attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 }
217
218 if (ia_valid & ATTR_SIZE)
219 up_write(&dentry->d_inode->i_alloc_sem);
220
Robert Love0eeca282005-07-12 17:06:03 -0400221 if (!error)
222 fsnotify_change(dentry, ia_valid);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return error;
225}
226
227EXPORT_SYMBOL(notify_change);