HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / overlayfs / inode.c
blob013d27dc6f58473e882aa9ec805b04ee678e4a38
1 /*
3 * Copyright (C) 2011 Novell Inc.
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 */
10 #include <linux/fs.h>
11 #include <linux/slab.h>
12 #include <linux/xattr.h>
13 #include "overlayfs.h"
15 static int ovl_copy_up_truncate(struct dentry *dentry)
17 int err;
18 struct dentry *parent;
19 struct kstat stat;
20 struct path lowerpath;
22 parent = dget_parent(dentry);
23 err = ovl_copy_up(parent);
24 if (err)
25 goto out_dput_parent;
27 ovl_path_lower(dentry, &lowerpath);
28 err = vfs_getattr(&lowerpath, &stat);
29 if (err)
30 goto out_dput_parent;
32 stat.size = 0;
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
35 out_dput_parent:
36 dput(parent);
37 return err;
40 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
42 int err;
43 struct dentry *upperdentry;
46 * Check for permissions before trying to copy-up. This is redundant
47 * since it will be rechecked later by ->setattr() on upper dentry. But
48 * without this, copy-up can be triggered by just about anybody.
50 * We don't initialize inode->size, which just means that
51 * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
52 * check for a swapfile (which this won't be anyway).
54 err = inode_change_ok(dentry->d_inode, attr);
55 if (err)
56 return err;
58 err = ovl_want_write(dentry);
59 if (err)
60 goto out;
62 err = ovl_copy_up(dentry);
63 if (!err) {
64 upperdentry = ovl_dentry_upper(dentry);
66 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
67 attr->ia_valid &= ~ATTR_MODE;
69 mutex_lock(&upperdentry->d_inode->i_mutex);
70 err = notify_change(upperdentry, attr, NULL);
71 if (!err)
72 ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
73 mutex_unlock(&upperdentry->d_inode->i_mutex);
75 ovl_drop_write(dentry);
76 out:
77 return err;
80 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
81 struct kstat *stat)
83 struct path realpath;
85 ovl_path_real(dentry, &realpath);
86 return vfs_getattr(&realpath, stat);
89 int ovl_permission(struct inode *inode, int mask)
91 struct ovl_entry *oe;
92 struct dentry *alias = NULL;
93 struct inode *realinode;
94 struct dentry *realdentry;
95 bool is_upper;
96 int err;
98 if (S_ISDIR(inode->i_mode)) {
99 oe = inode->i_private;
100 } else if (mask & MAY_NOT_BLOCK) {
101 return -ECHILD;
102 } else {
104 * For non-directories find an alias and get the info
105 * from there.
107 alias = d_find_any_alias(inode);
108 if (WARN_ON(!alias))
109 return -ENOENT;
111 oe = alias->d_fsdata;
114 realdentry = ovl_entry_real(oe, &is_upper);
116 /* Careful in RCU walk mode */
117 realinode = ACCESS_ONCE(realdentry->d_inode);
118 if (!realinode) {
119 WARN_ON(!(mask & MAY_NOT_BLOCK));
120 err = -ENOENT;
121 goto out_dput;
124 if (mask & MAY_WRITE) {
125 umode_t mode = realinode->i_mode;
128 * Writes will always be redirected to upper layer, so
129 * ignore lower layer being read-only.
131 * If the overlay itself is read-only then proceed
132 * with the permission check, don't return EROFS.
133 * This will only happen if this is the lower layer of
134 * another overlayfs.
136 * If upper fs becomes read-only after the overlay was
137 * constructed return EROFS to prevent modification of
138 * upper layer.
140 err = -EROFS;
141 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
142 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
143 goto out_dput;
146 err = __inode_permission(realinode, mask);
147 out_dput:
148 dput(alias);
149 return err;
153 struct ovl_link_data {
154 struct dentry *realdentry;
155 void *cookie;
158 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
160 struct dentry *realdentry;
161 struct inode *realinode;
162 struct ovl_link_data *data = NULL;
163 const char *ret;
165 realdentry = ovl_dentry_real(dentry);
166 realinode = realdentry->d_inode;
168 if (WARN_ON(!realinode->i_op->follow_link))
169 return ERR_PTR(-EPERM);
171 if (realinode->i_op->put_link) {
172 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
173 if (!data)
174 return ERR_PTR(-ENOMEM);
175 data->realdentry = realdentry;
178 ret = realinode->i_op->follow_link(realdentry, cookie);
179 if (IS_ERR_OR_NULL(ret)) {
180 kfree(data);
181 return ret;
184 if (data)
185 data->cookie = *cookie;
187 *cookie = data;
189 return ret;
192 static void ovl_put_link(struct inode *unused, void *c)
194 struct inode *realinode;
195 struct ovl_link_data *data = c;
197 if (!data)
198 return;
200 realinode = data->realdentry->d_inode;
201 realinode->i_op->put_link(realinode, data->cookie);
202 kfree(data);
205 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
207 struct path realpath;
208 struct inode *realinode;
210 ovl_path_real(dentry, &realpath);
211 realinode = realpath.dentry->d_inode;
213 if (!realinode->i_op->readlink)
214 return -EINVAL;
216 touch_atime(&realpath);
218 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
222 bool ovl_is_private_xattr(const char *name)
224 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
227 int ovl_setxattr(struct dentry *dentry, const char *name,
228 const void *value, size_t size, int flags)
230 int err;
231 struct dentry *upperdentry;
233 err = ovl_want_write(dentry);
234 if (err)
235 goto out;
237 err = -EPERM;
238 if (ovl_is_private_xattr(name))
239 goto out_drop_write;
241 err = ovl_copy_up(dentry);
242 if (err)
243 goto out_drop_write;
245 upperdentry = ovl_dentry_upper(dentry);
246 err = vfs_setxattr(upperdentry, name, value, size, flags);
248 out_drop_write:
249 ovl_drop_write(dentry);
250 out:
251 return err;
254 static bool ovl_need_xattr_filter(struct dentry *dentry,
255 enum ovl_path_type type)
257 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
258 return S_ISDIR(dentry->d_inode->i_mode);
259 else
260 return false;
263 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
264 void *value, size_t size)
266 struct path realpath;
267 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
269 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
270 return -ENODATA;
272 return vfs_getxattr(realpath.dentry, name, value, size);
275 static bool ovl_can_list(const char *s)
277 /* List all non-trusted xatts */
278 if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
279 return true;
281 /* Never list trusted.overlay, list other trusted for superuser only */
282 return !ovl_is_private_xattr(s) &&
283 ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN);
286 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
288 struct path realpath;
289 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
290 ssize_t res;
291 size_t len;
292 char *s;
294 res = vfs_listxattr(realpath.dentry, list, size);
295 if (res <= 0 || size == 0)
296 return res;
298 if (!ovl_need_xattr_filter(dentry, type))
299 return res;
301 /* filter out private xattrs */
302 for (s = list, len = res; len;) {
303 size_t slen = strnlen(s, len) + 1;
305 /* underlying fs providing us with an broken xattr list? */
306 if (WARN_ON(slen > len))
307 return -EIO;
309 len -= slen;
310 if (!ovl_can_list(s)) {
311 res -= slen;
312 memmove(s, s + slen, len);
313 } else {
314 s += slen;
318 return res;
321 int ovl_removexattr(struct dentry *dentry, const char *name)
323 int err;
324 struct path realpath;
325 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
327 err = ovl_want_write(dentry);
328 if (err)
329 goto out;
331 err = -ENODATA;
332 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
333 goto out_drop_write;
335 if (!OVL_TYPE_UPPER(type)) {
336 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
337 if (err < 0)
338 goto out_drop_write;
340 err = ovl_copy_up(dentry);
341 if (err)
342 goto out_drop_write;
344 ovl_path_upper(dentry, &realpath);
347 err = vfs_removexattr(realpath.dentry, name);
348 out_drop_write:
349 ovl_drop_write(dentry);
350 out:
351 return err;
354 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
355 struct dentry *realdentry)
357 if (OVL_TYPE_UPPER(type))
358 return false;
360 if (special_file(realdentry->d_inode->i_mode))
361 return false;
363 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
364 return false;
366 return true;
369 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
371 int err;
372 struct path realpath;
373 enum ovl_path_type type;
375 if (d_is_dir(dentry))
376 return d_backing_inode(dentry);
378 type = ovl_path_real(dentry, &realpath);
379 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
380 err = ovl_want_write(dentry);
381 if (err)
382 return ERR_PTR(err);
384 if (file_flags & O_TRUNC)
385 err = ovl_copy_up_truncate(dentry);
386 else
387 err = ovl_copy_up(dentry);
388 ovl_drop_write(dentry);
389 if (err)
390 return ERR_PTR(err);
392 ovl_path_upper(dentry, &realpath);
395 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
396 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
398 return d_backing_inode(realpath.dentry);
401 static const struct inode_operations ovl_file_inode_operations = {
402 .setattr = ovl_setattr,
403 .permission = ovl_permission,
404 .getattr = ovl_getattr,
405 .setxattr = ovl_setxattr,
406 .getxattr = ovl_getxattr,
407 .listxattr = ovl_listxattr,
408 .removexattr = ovl_removexattr,
411 static const struct inode_operations ovl_symlink_inode_operations = {
412 .setattr = ovl_setattr,
413 .follow_link = ovl_follow_link,
414 .put_link = ovl_put_link,
415 .readlink = ovl_readlink,
416 .getattr = ovl_getattr,
417 .setxattr = ovl_setxattr,
418 .getxattr = ovl_getxattr,
419 .listxattr = ovl_listxattr,
420 .removexattr = ovl_removexattr,
423 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
424 struct ovl_entry *oe)
426 struct inode *inode;
428 inode = new_inode(sb);
429 if (!inode)
430 return NULL;
432 inode->i_ino = get_next_ino();
433 inode->i_mode = mode;
434 inode->i_flags |= S_NOATIME | S_NOCMTIME;
436 mode &= S_IFMT;
437 switch (mode) {
438 case S_IFDIR:
439 inode->i_private = oe;
440 inode->i_op = &ovl_dir_inode_operations;
441 inode->i_fop = &ovl_dir_operations;
442 break;
444 case S_IFLNK:
445 inode->i_op = &ovl_symlink_inode_operations;
446 break;
448 case S_IFREG:
449 case S_IFSOCK:
450 case S_IFBLK:
451 case S_IFCHR:
452 case S_IFIFO:
453 inode->i_op = &ovl_file_inode_operations;
454 break;
456 default:
457 WARN(1, "illegal file type: %i\n", mode);
458 iput(inode);
459 inode = NULL;
462 return inode;