Merge remote-tracking branch 'crypto/master'
[deliverable/linux.git] / fs / overlayfs / inode.c
1 /*
2 *
3 * Copyright (C) 2011 Novell Inc.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 */
9
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/xattr.h>
13 #include <linux/posix_acl.h>
14 #include "overlayfs.h"
15
16 static int ovl_copy_up_truncate(struct dentry *dentry)
17 {
18 int err;
19 struct dentry *parent;
20 struct kstat stat;
21 struct path lowerpath;
22
23 parent = dget_parent(dentry);
24 err = ovl_copy_up(parent);
25 if (err)
26 goto out_dput_parent;
27
28 ovl_path_lower(dentry, &lowerpath);
29 err = vfs_getattr(&lowerpath, &stat);
30 if (err)
31 goto out_dput_parent;
32
33 stat.size = 0;
34 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
35
36 out_dput_parent:
37 dput(parent);
38 return err;
39 }
40
41 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
42 {
43 int err;
44 struct dentry *upperdentry;
45 const struct cred *old_cred;
46
47 /*
48 * Check for permissions before trying to copy-up. This is redundant
49 * since it will be rechecked later by ->setattr() on upper dentry. But
50 * without this, copy-up can be triggered by just about anybody.
51 *
52 * We don't initialize inode->size, which just means that
53 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
54 * check for a swapfile (which this won't be anyway).
55 */
56 err = inode_change_ok(dentry->d_inode, attr);
57 if (err)
58 return err;
59
60 err = ovl_want_write(dentry);
61 if (err)
62 goto out;
63
64 err = ovl_copy_up(dentry);
65 if (!err) {
66 upperdentry = ovl_dentry_upper(dentry);
67
68 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
69 attr->ia_valid &= ~ATTR_MODE;
70
71 inode_lock(upperdentry->d_inode);
72 old_cred = ovl_override_creds(dentry->d_sb);
73 err = notify_change(upperdentry, attr, NULL);
74 revert_creds(old_cred);
75 if (!err)
76 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
77 inode_unlock(upperdentry->d_inode);
78 }
79 ovl_drop_write(dentry);
80 out:
81 return err;
82 }
83
84 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
85 struct kstat *stat)
86 {
87 struct path realpath;
88 const struct cred *old_cred;
89 int err;
90
91 ovl_path_real(dentry, &realpath);
92 old_cred = ovl_override_creds(dentry->d_sb);
93 err = vfs_getattr(&realpath, stat);
94 revert_creds(old_cred);
95 return err;
96 }
97
98 int ovl_permission(struct inode *inode, int mask)
99 {
100 bool is_upper;
101 struct inode *realinode = ovl_inode_real(inode, &is_upper);
102 const struct cred *old_cred;
103 int err;
104
105 /* Careful in RCU walk mode */
106 if (!realinode) {
107 WARN_ON(!(mask & MAY_NOT_BLOCK));
108 return -ECHILD;
109 }
110
111 /*
112 * Check overlay inode with the creds of task and underlying inode
113 * with creds of mounter
114 */
115 err = generic_permission(inode, mask);
116 if (err)
117 return err;
118
119 old_cred = ovl_override_creds(inode->i_sb);
120 if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) {
121 mask &= ~(MAY_WRITE | MAY_APPEND);
122 /* Make sure mounter can read file for copy up later */
123 mask |= MAY_READ;
124 }
125 err = inode_permission(realinode, mask);
126 revert_creds(old_cred);
127
128 return err;
129 }
130
131 static const char *ovl_get_link(struct dentry *dentry,
132 struct inode *inode,
133 struct delayed_call *done)
134 {
135 struct dentry *realdentry;
136 struct inode *realinode;
137 const struct cred *old_cred;
138 const char *p;
139
140 if (!dentry)
141 return ERR_PTR(-ECHILD);
142
143 realdentry = ovl_dentry_real(dentry);
144 realinode = realdentry->d_inode;
145
146 if (WARN_ON(!realinode->i_op->get_link))
147 return ERR_PTR(-EPERM);
148
149 old_cred = ovl_override_creds(dentry->d_sb);
150 p = realinode->i_op->get_link(realdentry, realinode, done);
151 revert_creds(old_cred);
152 return p;
153 }
154
155 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
156 {
157 struct path realpath;
158 struct inode *realinode;
159 const struct cred *old_cred;
160 int err;
161
162 ovl_path_real(dentry, &realpath);
163 realinode = realpath.dentry->d_inode;
164
165 if (!realinode->i_op->readlink)
166 return -EINVAL;
167
168 old_cred = ovl_override_creds(dentry->d_sb);
169 err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
170 revert_creds(old_cred);
171 return err;
172 }
173
174 bool ovl_is_private_xattr(const char *name)
175 {
176 return strncmp(name, OVL_XATTR_PREFIX,
177 sizeof(OVL_XATTR_PREFIX) - 1) == 0;
178 }
179
180 int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value,
181 size_t size, int flags)
182 {
183 int err;
184 struct path realpath;
185 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
186 const struct cred *old_cred;
187
188 err = ovl_want_write(dentry);
189 if (err)
190 goto out;
191
192 if (!value && !OVL_TYPE_UPPER(type)) {
193 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
194 if (err < 0)
195 goto out_drop_write;
196 }
197
198 err = ovl_copy_up(dentry);
199 if (err)
200 goto out_drop_write;
201
202 if (!OVL_TYPE_UPPER(type))
203 ovl_path_upper(dentry, &realpath);
204
205 old_cred = ovl_override_creds(dentry->d_sb);
206 if (value)
207 err = vfs_setxattr(realpath.dentry, name, value, size, flags);
208 else {
209 WARN_ON(flags != XATTR_REPLACE);
210 err = vfs_removexattr(realpath.dentry, name);
211 }
212 revert_creds(old_cred);
213
214 out_drop_write:
215 ovl_drop_write(dentry);
216 out:
217 return err;
218 }
219
220 int ovl_xattr_get(struct dentry *dentry, const char *name,
221 void *value, size_t size)
222 {
223 struct dentry *realdentry = ovl_dentry_real(dentry);
224 ssize_t res;
225 const struct cred *old_cred;
226
227 old_cred = ovl_override_creds(dentry->d_sb);
228 res = vfs_getxattr(realdentry, name, value, size);
229 revert_creds(old_cred);
230 return res;
231 }
232
233 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
234 {
235 struct dentry *realdentry = ovl_dentry_real(dentry);
236 ssize_t res;
237 size_t len;
238 char *s;
239 const struct cred *old_cred;
240
241 old_cred = ovl_override_creds(dentry->d_sb);
242 res = vfs_listxattr(realdentry, list, size);
243 revert_creds(old_cred);
244 if (res <= 0 || size == 0)
245 return res;
246
247 /* filter out private xattrs */
248 for (s = list, len = res; len;) {
249 size_t slen = strnlen(s, len) + 1;
250
251 /* underlying fs providing us with an broken xattr list? */
252 if (WARN_ON(slen > len))
253 return -EIO;
254
255 len -= slen;
256 if (ovl_is_private_xattr(s)) {
257 res -= slen;
258 memmove(s, s + slen, len);
259 } else {
260 s += slen;
261 }
262 }
263
264 return res;
265 }
266
267 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
268 {
269 struct inode *realinode = ovl_inode_real(inode, NULL);
270 const struct cred *old_cred;
271 struct posix_acl *acl;
272
273 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode))
274 return NULL;
275
276 if (!realinode->i_op->get_acl)
277 return NULL;
278
279 old_cred = ovl_override_creds(inode->i_sb);
280 acl = get_acl(realinode, type);
281 revert_creds(old_cred);
282
283 return acl;
284 }
285
286 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
287 struct dentry *realdentry)
288 {
289 if (OVL_TYPE_UPPER(type))
290 return false;
291
292 if (special_file(realdentry->d_inode->i_mode))
293 return false;
294
295 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
296 return false;
297
298 return true;
299 }
300
301 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
302 {
303 int err = 0;
304 struct path realpath;
305 enum ovl_path_type type;
306
307 type = ovl_path_real(dentry, &realpath);
308 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
309 err = ovl_want_write(dentry);
310 if (!err) {
311 if (file_flags & O_TRUNC)
312 err = ovl_copy_up_truncate(dentry);
313 else
314 err = ovl_copy_up(dentry);
315 ovl_drop_write(dentry);
316 }
317 }
318
319 return err;
320 }
321
322 int ovl_update_time(struct inode *inode, struct timespec *ts, int flags)
323 {
324 struct dentry *alias;
325 struct path upperpath;
326
327 if (!(flags & S_ATIME))
328 return 0;
329
330 alias = d_find_any_alias(inode);
331 if (!alias)
332 return 0;
333
334 ovl_path_upper(alias, &upperpath);
335 if (upperpath.dentry) {
336 touch_atime(&upperpath);
337 inode->i_atime = d_inode(upperpath.dentry)->i_atime;
338 }
339
340 dput(alias);
341
342 return 0;
343 }
344
345 static const struct inode_operations ovl_file_inode_operations = {
346 .setattr = ovl_setattr,
347 .permission = ovl_permission,
348 .getattr = ovl_getattr,
349 .setxattr = generic_setxattr,
350 .getxattr = generic_getxattr,
351 .listxattr = ovl_listxattr,
352 .removexattr = generic_removexattr,
353 .get_acl = ovl_get_acl,
354 .update_time = ovl_update_time,
355 };
356
357 static const struct inode_operations ovl_symlink_inode_operations = {
358 .setattr = ovl_setattr,
359 .get_link = ovl_get_link,
360 .readlink = ovl_readlink,
361 .getattr = ovl_getattr,
362 .setxattr = generic_setxattr,
363 .getxattr = generic_getxattr,
364 .listxattr = ovl_listxattr,
365 .removexattr = generic_removexattr,
366 .update_time = ovl_update_time,
367 };
368
369 static void ovl_fill_inode(struct inode *inode, umode_t mode)
370 {
371 inode->i_ino = get_next_ino();
372 inode->i_mode = mode;
373 inode->i_flags |= S_NOCMTIME;
374 #ifdef CONFIG_FS_POSIX_ACL
375 inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
376 #endif
377
378 mode &= S_IFMT;
379 switch (mode) {
380 case S_IFDIR:
381 inode->i_op = &ovl_dir_inode_operations;
382 inode->i_fop = &ovl_dir_operations;
383 break;
384
385 case S_IFLNK:
386 inode->i_op = &ovl_symlink_inode_operations;
387 break;
388
389 default:
390 WARN(1, "illegal file type: %i\n", mode);
391 /* Fall through */
392
393 case S_IFREG:
394 case S_IFSOCK:
395 case S_IFBLK:
396 case S_IFCHR:
397 case S_IFIFO:
398 inode->i_op = &ovl_file_inode_operations;
399 break;
400 }
401 }
402
403 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode)
404 {
405 struct inode *inode;
406
407 inode = new_inode(sb);
408 if (inode)
409 ovl_fill_inode(inode, mode);
410
411 return inode;
412 }
413
414 static int ovl_inode_test(struct inode *inode, void *data)
415 {
416 return ovl_inode_real(inode, NULL) == data;
417 }
418
419 static int ovl_inode_set(struct inode *inode, void *data)
420 {
421 inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK);
422 return 0;
423 }
424
425 struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode)
426
427 {
428 struct inode *inode;
429
430 inode = iget5_locked(sb, (unsigned long) realinode,
431 ovl_inode_test, ovl_inode_set, realinode);
432 if (inode && inode->i_state & I_NEW) {
433 ovl_fill_inode(inode, realinode->i_mode);
434 set_nlink(inode, realinode->i_nlink);
435 unlock_new_inode(inode);
436 }
437
438 return inode;
439 }
This page took 0.042353 seconds and 5 git commands to generate.