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