OSDN Git Service

013d27dc6f58473e882aa9ec805b04ee678e4a38
[sagit-ice-cold/kernel_xiaomi_msm8998.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 "overlayfs.h"
14
15 static int ovl_copy_up_truncate(struct dentry *dentry)
16 {
17         int err;
18         struct dentry *parent;
19         struct kstat stat;
20         struct path lowerpath;
21
22         parent = dget_parent(dentry);
23         err = ovl_copy_up(parent);
24         if (err)
25                 goto out_dput_parent;
26
27         ovl_path_lower(dentry, &lowerpath);
28         err = vfs_getattr(&lowerpath, &stat);
29         if (err)
30                 goto out_dput_parent;
31
32         stat.size = 0;
33         err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
34
35 out_dput_parent:
36         dput(parent);
37         return err;
38 }
39
40 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41 {
42         int err;
43         struct dentry *upperdentry;
44
45         /*
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.
49          *
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).
53          */
54         err = inode_change_ok(dentry->d_inode, attr);
55         if (err)
56                 return err;
57
58         err = ovl_want_write(dentry);
59         if (err)
60                 goto out;
61
62         err = ovl_copy_up(dentry);
63         if (!err) {
64                 upperdentry = ovl_dentry_upper(dentry);
65
66                 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
67                         attr->ia_valid &= ~ATTR_MODE;
68
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);
74         }
75         ovl_drop_write(dentry);
76 out:
77         return err;
78 }
79
80 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
81                          struct kstat *stat)
82 {
83         struct path realpath;
84
85         ovl_path_real(dentry, &realpath);
86         return vfs_getattr(&realpath, stat);
87 }
88
89 int ovl_permission(struct inode *inode, int mask)
90 {
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;
97
98         if (S_ISDIR(inode->i_mode)) {
99                 oe = inode->i_private;
100         } else if (mask & MAY_NOT_BLOCK) {
101                 return -ECHILD;
102         } else {
103                 /*
104                  * For non-directories find an alias and get the info
105                  * from there.
106                  */
107                 alias = d_find_any_alias(inode);
108                 if (WARN_ON(!alias))
109                         return -ENOENT;
110
111                 oe = alias->d_fsdata;
112         }
113
114         realdentry = ovl_entry_real(oe, &is_upper);
115
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;
122         }
123
124         if (mask & MAY_WRITE) {
125                 umode_t mode = realinode->i_mode;
126
127                 /*
128                  * Writes will always be redirected to upper layer, so
129                  * ignore lower layer being read-only.
130                  *
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.
135                  *
136                  * If upper fs becomes read-only after the overlay was
137                  * constructed return EROFS to prevent modification of
138                  * upper layer.
139                  */
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;
144         }
145
146         err = __inode_permission(realinode, mask);
147 out_dput:
148         dput(alias);
149         return err;
150 }
151
152
153 struct ovl_link_data {
154         struct dentry *realdentry;
155         void *cookie;
156 };
157
158 static const char *ovl_follow_link(struct dentry *dentry, void **cookie)
159 {
160         struct dentry *realdentry;
161         struct inode *realinode;
162         struct ovl_link_data *data = NULL;
163         const char *ret;
164
165         realdentry = ovl_dentry_real(dentry);
166         realinode = realdentry->d_inode;
167
168         if (WARN_ON(!realinode->i_op->follow_link))
169                 return ERR_PTR(-EPERM);
170
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;
176         }
177
178         ret = realinode->i_op->follow_link(realdentry, cookie);
179         if (IS_ERR_OR_NULL(ret)) {
180                 kfree(data);
181                 return ret;
182         }
183
184         if (data)
185                 data->cookie = *cookie;
186
187         *cookie = data;
188
189         return ret;
190 }
191
192 static void ovl_put_link(struct inode *unused, void *c)
193 {
194         struct inode *realinode;
195         struct ovl_link_data *data = c;
196
197         if (!data)
198                 return;
199
200         realinode = data->realdentry->d_inode;
201         realinode->i_op->put_link(realinode, data->cookie);
202         kfree(data);
203 }
204
205 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
206 {
207         struct path realpath;
208         struct inode *realinode;
209
210         ovl_path_real(dentry, &realpath);
211         realinode = realpath.dentry->d_inode;
212
213         if (!realinode->i_op->readlink)
214                 return -EINVAL;
215
216         touch_atime(&realpath);
217
218         return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
219 }
220
221
222 bool ovl_is_private_xattr(const char *name)
223 {
224         return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
225 }
226
227 int ovl_setxattr(struct dentry *dentry, const char *name,
228                  const void *value, size_t size, int flags)
229 {
230         int err;
231         struct dentry *upperdentry;
232
233         err = ovl_want_write(dentry);
234         if (err)
235                 goto out;
236
237         err = -EPERM;
238         if (ovl_is_private_xattr(name))
239                 goto out_drop_write;
240
241         err = ovl_copy_up(dentry);
242         if (err)
243                 goto out_drop_write;
244
245         upperdentry = ovl_dentry_upper(dentry);
246         err = vfs_setxattr(upperdentry, name, value, size, flags);
247
248 out_drop_write:
249         ovl_drop_write(dentry);
250 out:
251         return err;
252 }
253
254 static bool ovl_need_xattr_filter(struct dentry *dentry,
255                                   enum ovl_path_type type)
256 {
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;
261 }
262
263 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
264                      void *value, size_t size)
265 {
266         struct path realpath;
267         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
268
269         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
270                 return -ENODATA;
271
272         return vfs_getxattr(realpath.dentry, name, value, size);
273 }
274
275 static bool ovl_can_list(const char *s)
276 {
277         /* List all non-trusted xatts */
278         if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0)
279                 return true;
280
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);
284 }
285
286 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
287 {
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;
293
294         res = vfs_listxattr(realpath.dentry, list, size);
295         if (res <= 0 || size == 0)
296                 return res;
297
298         if (!ovl_need_xattr_filter(dentry, type))
299                 return res;
300
301         /* filter out private xattrs */
302         for (s = list, len = res; len;) {
303                 size_t slen = strnlen(s, len) + 1;
304
305                 /* underlying fs providing us with an broken xattr list? */
306                 if (WARN_ON(slen > len))
307                         return -EIO;
308
309                 len -= slen;
310                 if (!ovl_can_list(s)) {
311                         res -= slen;
312                         memmove(s, s + slen, len);
313                 } else {
314                         s += slen;
315                 }
316         }
317
318         return res;
319 }
320
321 int ovl_removexattr(struct dentry *dentry, const char *name)
322 {
323         int err;
324         struct path realpath;
325         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
326
327         err = ovl_want_write(dentry);
328         if (err)
329                 goto out;
330
331         err = -ENODATA;
332         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
333                 goto out_drop_write;
334
335         if (!OVL_TYPE_UPPER(type)) {
336                 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
337                 if (err < 0)
338                         goto out_drop_write;
339
340                 err = ovl_copy_up(dentry);
341                 if (err)
342                         goto out_drop_write;
343
344                 ovl_path_upper(dentry, &realpath);
345         }
346
347         err = vfs_removexattr(realpath.dentry, name);
348 out_drop_write:
349         ovl_drop_write(dentry);
350 out:
351         return err;
352 }
353
354 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
355                                   struct dentry *realdentry)
356 {
357         if (OVL_TYPE_UPPER(type))
358                 return false;
359
360         if (special_file(realdentry->d_inode->i_mode))
361                 return false;
362
363         if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
364                 return false;
365
366         return true;
367 }
368
369 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
370 {
371         int err;
372         struct path realpath;
373         enum ovl_path_type type;
374
375         if (d_is_dir(dentry))
376                 return d_backing_inode(dentry);
377
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);
383
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);
391
392                 ovl_path_upper(dentry, &realpath);
393         }
394
395         if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
396                 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
397
398         return d_backing_inode(realpath.dentry);
399 }
400
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,
409 };
410
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,
421 };
422
423 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
424                             struct ovl_entry *oe)
425 {
426         struct inode *inode;
427
428         inode = new_inode(sb);
429         if (!inode)
430                 return NULL;
431
432         inode->i_ino = get_next_ino();
433         inode->i_mode = mode;
434         inode->i_flags |= S_NOATIME | S_NOCMTIME;
435
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;
443
444         case S_IFLNK:
445                 inode->i_op = &ovl_symlink_inode_operations;
446                 break;
447
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;
455
456         default:
457                 WARN(1, "illegal file type: %i\n", mode);
458                 iput(inode);
459                 inode = NULL;
460         }
461
462         return inode;
463 }