Linux-libre 4.1.48-gnu
[librecmc/linux-libre.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_last(struct dentry *dentry, struct iattr *attr,
16                             bool no_data)
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         if (no_data)
34                 stat.size = 0;
35
36         err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat, attr);
37
38 out_dput_parent:
39         dput(parent);
40         return err;
41 }
42
43 int ovl_setattr(struct dentry *dentry, struct iattr *attr)
44 {
45         int err;
46         struct dentry *upperdentry;
47
48         /*
49          * Check for permissions before trying to copy-up.  This is redundant
50          * since it will be rechecked later by ->setattr() on upper dentry.  But
51          * without this, copy-up can be triggered by just about anybody.
52          *
53          * We don't initialize inode->size, which just means that
54          * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
55          * check for a swapfile (which this won't be anyway).
56          */
57         err = setattr_prepare(dentry, attr);
58         if (err)
59                 return err;
60
61         err = ovl_want_write(dentry);
62         if (err)
63                 goto out;
64
65         err = ovl_copy_up(dentry);
66         if (!err) {
67                 upperdentry = ovl_dentry_upper(dentry);
68
69                 mutex_lock(&upperdentry->d_inode->i_mutex);
70
71                 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
72                         attr->ia_valid &= ~ATTR_MODE;
73
74                 err = notify_change(upperdentry, attr, NULL);
75                 if (!err)
76                         ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
77                 mutex_unlock(&upperdentry->d_inode->i_mutex);
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
89         ovl_path_real(dentry, &realpath);
90         return vfs_getattr(&realpath, stat);
91 }
92
93 int ovl_permission(struct inode *inode, int mask)
94 {
95         struct ovl_entry *oe;
96         struct dentry *alias = NULL;
97         struct inode *realinode;
98         struct dentry *realdentry;
99         bool is_upper;
100         int err;
101
102         if (S_ISDIR(inode->i_mode)) {
103                 oe = inode->i_private;
104         } else if (mask & MAY_NOT_BLOCK) {
105                 return -ECHILD;
106         } else {
107                 /*
108                  * For non-directories find an alias and get the info
109                  * from there.
110                  */
111                 alias = d_find_any_alias(inode);
112                 if (WARN_ON(!alias))
113                         return -ENOENT;
114
115                 oe = alias->d_fsdata;
116         }
117
118         realdentry = ovl_entry_real(oe, &is_upper);
119
120         /* Careful in RCU walk mode */
121         realinode = ACCESS_ONCE(realdentry->d_inode);
122         if (!realinode) {
123                 WARN_ON(!(mask & MAY_NOT_BLOCK));
124                 err = -ENOENT;
125                 goto out_dput;
126         }
127
128         if (mask & MAY_WRITE) {
129                 umode_t mode = realinode->i_mode;
130
131                 /*
132                  * Writes will always be redirected to upper layer, so
133                  * ignore lower layer being read-only.
134                  *
135                  * If the overlay itself is read-only then proceed
136                  * with the permission check, don't return EROFS.
137                  * This will only happen if this is the lower layer of
138                  * another overlayfs.
139                  *
140                  * If upper fs becomes read-only after the overlay was
141                  * constructed return EROFS to prevent modification of
142                  * upper layer.
143                  */
144                 err = -EROFS;
145                 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
146                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
147                         goto out_dput;
148         }
149
150         err = __inode_permission(realinode, mask);
151 out_dput:
152         dput(alias);
153         return err;
154 }
155
156
157 struct ovl_link_data {
158         struct dentry *realdentry;
159         void *cookie;
160 };
161
162 static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
163 {
164         void *ret;
165         struct dentry *realdentry;
166         struct inode *realinode;
167
168         realdentry = ovl_dentry_real(dentry);
169         realinode = realdentry->d_inode;
170
171         if (WARN_ON(!realinode->i_op->follow_link))
172                 return ERR_PTR(-EPERM);
173
174         ret = realinode->i_op->follow_link(realdentry, nd);
175         if (IS_ERR(ret))
176                 return ret;
177
178         if (realinode->i_op->put_link) {
179                 struct ovl_link_data *data;
180
181                 data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
182                 if (!data) {
183                         realinode->i_op->put_link(realdentry, nd, ret);
184                         return ERR_PTR(-ENOMEM);
185                 }
186                 data->realdentry = realdentry;
187                 data->cookie = ret;
188
189                 return data;
190         } else {
191                 return NULL;
192         }
193 }
194
195 static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
196 {
197         struct inode *realinode;
198         struct ovl_link_data *data = c;
199
200         if (!data)
201                 return;
202
203         realinode = data->realdentry->d_inode;
204         realinode->i_op->put_link(data->realdentry, nd, data->cookie);
205         kfree(data);
206 }
207
208 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
209 {
210         struct path realpath;
211         struct inode *realinode;
212
213         ovl_path_real(dentry, &realpath);
214         realinode = realpath.dentry->d_inode;
215
216         if (!realinode->i_op->readlink)
217                 return -EINVAL;
218
219         touch_atime(&realpath);
220
221         return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
222 }
223
224 bool ovl_is_private_xattr(const char *name)
225 {
226         return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
227 }
228
229 int ovl_setxattr(struct dentry *dentry, const char *name,
230                  const void *value, size_t size, int flags)
231 {
232         int err;
233         struct dentry *upperdentry;
234
235         err = ovl_want_write(dentry);
236         if (err)
237                 goto out;
238
239         err = -EPERM;
240         if (ovl_is_private_xattr(name))
241                 goto out_drop_write;
242
243         err = ovl_copy_up(dentry);
244         if (err)
245                 goto out_drop_write;
246
247         upperdentry = ovl_dentry_upper(dentry);
248         err = vfs_setxattr(upperdentry, name, value, size, flags);
249
250 out_drop_write:
251         ovl_drop_write(dentry);
252 out:
253         return err;
254 }
255
256 static bool ovl_need_xattr_filter(struct dentry *dentry,
257                                   enum ovl_path_type type)
258 {
259         if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
260                 return S_ISDIR(dentry->d_inode->i_mode);
261         else
262                 return false;
263 }
264
265 ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
266                      void *value, size_t size)
267 {
268         struct path realpath;
269         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
270
271         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
272                 return -ENODATA;
273
274         return vfs_getxattr(realpath.dentry, name, value, size);
275 }
276
277 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
278 {
279         struct path realpath;
280         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
281         ssize_t res;
282         size_t len;
283         char *s;
284
285         res = vfs_listxattr(realpath.dentry, list, size);
286         if (res <= 0 || size == 0)
287                 return res;
288
289         if (!ovl_need_xattr_filter(dentry, type))
290                 return res;
291
292         /* filter out private xattrs */
293         for (s = list, len = res; len;) {
294                 size_t slen = strnlen(s, len) + 1;
295
296                 /* underlying fs providing us with an broken xattr list? */
297                 if (WARN_ON(slen > len))
298                         return -EIO;
299
300                 len -= slen;
301                 if (ovl_is_private_xattr(s)) {
302                         res -= slen;
303                         memmove(s, s + slen, len);
304                 } else {
305                         s += slen;
306                 }
307         }
308
309         return res;
310 }
311
312 int ovl_removexattr(struct dentry *dentry, const char *name)
313 {
314         int err;
315         struct path realpath;
316         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
317
318         err = ovl_want_write(dentry);
319         if (err)
320                 goto out;
321
322         err = -ENODATA;
323         if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
324                 goto out_drop_write;
325
326         if (!OVL_TYPE_UPPER(type)) {
327                 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
328                 if (err < 0)
329                         goto out_drop_write;
330
331                 err = ovl_copy_up(dentry);
332                 if (err)
333                         goto out_drop_write;
334
335                 ovl_path_upper(dentry, &realpath);
336         }
337
338         err = vfs_removexattr(realpath.dentry, name);
339 out_drop_write:
340         ovl_drop_write(dentry);
341 out:
342         return err;
343 }
344
345 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
346                                   struct dentry *realdentry)
347 {
348         if (OVL_TYPE_UPPER(type))
349                 return false;
350
351         if (special_file(realdentry->d_inode->i_mode))
352                 return false;
353
354         if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
355                 return false;
356
357         return true;
358 }
359
360 struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
361 {
362         int err;
363         struct path realpath;
364         enum ovl_path_type type;
365
366         if (d_is_dir(dentry))
367                 return d_backing_inode(dentry);
368
369         type = ovl_path_real(dentry, &realpath);
370         if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
371                 err = ovl_want_write(dentry);
372                 if (err)
373                         return ERR_PTR(err);
374
375                 if (file_flags & O_TRUNC)
376                         err = ovl_copy_up_last(dentry, NULL, true);
377                 else
378                         err = ovl_copy_up(dentry);
379                 ovl_drop_write(dentry);
380                 if (err)
381                         return ERR_PTR(err);
382
383                 ovl_path_upper(dentry, &realpath);
384         }
385
386         return d_backing_inode(realpath.dentry);
387 }
388
389 static const struct inode_operations ovl_file_inode_operations = {
390         .setattr        = ovl_setattr,
391         .permission     = ovl_permission,
392         .getattr        = ovl_getattr,
393         .setxattr       = ovl_setxattr,
394         .getxattr       = ovl_getxattr,
395         .listxattr      = ovl_listxattr,
396         .removexattr    = ovl_removexattr,
397 };
398
399 static const struct inode_operations ovl_symlink_inode_operations = {
400         .setattr        = ovl_setattr,
401         .follow_link    = ovl_follow_link,
402         .put_link       = ovl_put_link,
403         .readlink       = ovl_readlink,
404         .getattr        = ovl_getattr,
405         .setxattr       = ovl_setxattr,
406         .getxattr       = ovl_getxattr,
407         .listxattr      = ovl_listxattr,
408         .removexattr    = ovl_removexattr,
409 };
410
411 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
412                             struct ovl_entry *oe)
413 {
414         struct inode *inode;
415
416         inode = new_inode(sb);
417         if (!inode)
418                 return NULL;
419
420         inode->i_ino = get_next_ino();
421         inode->i_mode = mode;
422         inode->i_flags |= S_NOATIME | S_NOCMTIME;
423
424         mode &= S_IFMT;
425         switch (mode) {
426         case S_IFDIR:
427                 inode->i_private = oe;
428                 inode->i_op = &ovl_dir_inode_operations;
429                 inode->i_fop = &ovl_dir_operations;
430                 break;
431
432         case S_IFLNK:
433                 inode->i_op = &ovl_symlink_inode_operations;
434                 break;
435
436         case S_IFREG:
437         case S_IFSOCK:
438         case S_IFBLK:
439         case S_IFCHR:
440         case S_IFIFO:
441                 inode->i_op = &ovl_file_inode_operations;
442                 break;
443
444         default:
445                 WARN(1, "illegal file type: %i\n", mode);
446                 iput(inode);
447                 inode = NULL;
448         }
449
450         return inode;
451 }