]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/overlayfs/inode.c
ovl: do not require mounter to have MAY_WRITE on lower
[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 "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         const struct cred *old_cred;
45
46         /*
47          * Check for permissions before trying to copy-up.  This is redundant
48          * since it will be rechecked later by ->setattr() on upper dentry.  But
49          * without this, copy-up can be triggered by just about anybody.
50          *
51          * We don't initialize inode->size, which just means that
52          * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not
53          * check for a swapfile (which this won't be anyway).
54          */
55         err = inode_change_ok(dentry->d_inode, attr);
56         if (err)
57                 return err;
58
59         err = ovl_want_write(dentry);
60         if (err)
61                 goto out;
62
63         if (attr->ia_valid & ATTR_SIZE) {
64                 struct inode *realinode = d_inode(ovl_dentry_real(dentry));
65
66                 err = -ETXTBSY;
67                 if (atomic_read(&realinode->i_writecount) < 0)
68                         goto out_drop_write;
69         }
70
71         err = ovl_copy_up(dentry);
72         if (!err) {
73                 struct inode *winode = NULL;
74
75                 upperdentry = ovl_dentry_upper(dentry);
76
77                 if (attr->ia_valid & ATTR_SIZE) {
78                         winode = d_inode(upperdentry);
79                         err = get_write_access(winode);
80                         if (err)
81                                 goto out_drop_write;
82                 }
83
84                 if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
85                         attr->ia_valid &= ~ATTR_MODE;
86
87                 inode_lock(upperdentry->d_inode);
88                 old_cred = ovl_override_creds(dentry->d_sb);
89                 err = notify_change(upperdentry, attr, NULL);
90                 revert_creds(old_cred);
91                 if (!err)
92                         ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
93                 inode_unlock(upperdentry->d_inode);
94
95                 if (winode)
96                         put_write_access(winode);
97         }
98 out_drop_write:
99         ovl_drop_write(dentry);
100 out:
101         return err;
102 }
103
104 static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
105                          struct kstat *stat)
106 {
107         struct path realpath;
108         const struct cred *old_cred;
109         int err;
110
111         ovl_path_real(dentry, &realpath);
112         old_cred = ovl_override_creds(dentry->d_sb);
113         err = vfs_getattr(&realpath, stat);
114         revert_creds(old_cred);
115         return err;
116 }
117
118 int ovl_permission(struct inode *inode, int mask)
119 {
120         struct ovl_entry *oe = inode->i_private;
121         bool is_upper;
122         struct dentry *realdentry = ovl_entry_real(oe, &is_upper);
123         struct inode *realinode;
124         const struct cred *old_cred;
125         int err;
126
127         if (ovl_is_default_permissions(inode)) {
128                 struct kstat stat;
129                 struct path realpath = { .dentry = realdentry };
130
131                 if (mask & MAY_NOT_BLOCK)
132                         return -ECHILD;
133
134                 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
135
136                 err = vfs_getattr(&realpath, &stat);
137                 if (err)
138                         return err;
139
140                 if ((stat.mode ^ inode->i_mode) & S_IFMT)
141                         return -ESTALE;
142
143                 inode->i_mode = stat.mode;
144                 inode->i_uid = stat.uid;
145                 inode->i_gid = stat.gid;
146
147                 return generic_permission(inode, mask);
148         }
149
150         /* Careful in RCU walk mode */
151         realinode = d_inode_rcu(realdentry);
152         if (!realinode) {
153                 WARN_ON(!(mask & MAY_NOT_BLOCK));
154                 return -ENOENT;
155         }
156
157         if (mask & MAY_WRITE) {
158                 umode_t mode = realinode->i_mode;
159
160                 /*
161                  * Writes will always be redirected to upper layer, so
162                  * ignore lower layer being read-only.
163                  *
164                  * If the overlay itself is read-only then proceed
165                  * with the permission check, don't return EROFS.
166                  * This will only happen if this is the lower layer of
167                  * another overlayfs.
168                  *
169                  * If upper fs becomes read-only after the overlay was
170                  * constructed return EROFS to prevent modification of
171                  * upper layer.
172                  */
173                 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
174                     (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
175                         return -EROFS;
176         }
177
178         /*
179          * Check overlay inode with the creds of task and underlying inode
180          * with creds of mounter
181          */
182         err = generic_permission(inode, mask);
183         if (err)
184                 return err;
185
186         old_cred = ovl_override_creds(inode->i_sb);
187         if (!is_upper)
188                 mask &= ~(MAY_WRITE | MAY_APPEND);
189         err = __inode_permission(realinode, mask);
190         revert_creds(old_cred);
191
192         return err;
193 }
194
195 static const char *ovl_get_link(struct dentry *dentry,
196                                 struct inode *inode,
197                                 struct delayed_call *done)
198 {
199         struct dentry *realdentry;
200         struct inode *realinode;
201         const struct cred *old_cred;
202         const char *p;
203
204         if (!dentry)
205                 return ERR_PTR(-ECHILD);
206
207         realdentry = ovl_dentry_real(dentry);
208         realinode = realdentry->d_inode;
209
210         if (WARN_ON(!realinode->i_op->get_link))
211                 return ERR_PTR(-EPERM);
212
213         old_cred = ovl_override_creds(dentry->d_sb);
214         p = realinode->i_op->get_link(realdentry, realinode, done);
215         revert_creds(old_cred);
216         return p;
217 }
218
219 static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
220 {
221         struct path realpath;
222         struct inode *realinode;
223         const struct cred *old_cred;
224         int err;
225
226         ovl_path_real(dentry, &realpath);
227         realinode = realpath.dentry->d_inode;
228
229         if (!realinode->i_op->readlink)
230                 return -EINVAL;
231
232         touch_atime(&realpath);
233
234         old_cred = ovl_override_creds(dentry->d_sb);
235         err = realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
236         revert_creds(old_cred);
237         return err;
238 }
239
240 static bool ovl_is_private_xattr(const char *name)
241 {
242         return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
243 }
244
245 int ovl_setxattr(struct dentry *dentry, struct inode *inode,
246                  const char *name, const void *value,
247                  size_t size, int flags)
248 {
249         int err;
250         struct dentry *upperdentry;
251         const struct cred *old_cred;
252
253         err = ovl_want_write(dentry);
254         if (err)
255                 goto out;
256
257         err = -EPERM;
258         if (ovl_is_private_xattr(name))
259                 goto out_drop_write;
260
261         err = ovl_copy_up(dentry);
262         if (err)
263                 goto out_drop_write;
264
265         upperdentry = ovl_dentry_upper(dentry);
266         old_cred = ovl_override_creds(dentry->d_sb);
267         err = vfs_setxattr(upperdentry, name, value, size, flags);
268         revert_creds(old_cred);
269
270 out_drop_write:
271         ovl_drop_write(dentry);
272 out:
273         return err;
274 }
275
276 ssize_t ovl_getxattr(struct dentry *dentry, struct inode *inode,
277                      const char *name, void *value, size_t size)
278 {
279         struct dentry *realdentry = ovl_dentry_real(dentry);
280         ssize_t res;
281         const struct cred *old_cred;
282
283         if (ovl_is_private_xattr(name))
284                 return -ENODATA;
285
286         old_cred = ovl_override_creds(dentry->d_sb);
287         res = vfs_getxattr(realdentry, name, value, size);
288         revert_creds(old_cred);
289         return res;
290 }
291
292 ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
293 {
294         struct dentry *realdentry = ovl_dentry_real(dentry);
295         ssize_t res;
296         int off;
297         const struct cred *old_cred;
298
299         old_cred = ovl_override_creds(dentry->d_sb);
300         res = vfs_listxattr(realdentry, list, size);
301         revert_creds(old_cred);
302         if (res <= 0 || size == 0)
303                 return res;
304
305         /* filter out private xattrs */
306         for (off = 0; off < res;) {
307                 char *s = list + off;
308                 size_t slen = strlen(s) + 1;
309
310                 BUG_ON(off + slen > res);
311
312                 if (ovl_is_private_xattr(s)) {
313                         res -= slen;
314                         memmove(s, s + slen, res - off);
315                 } else {
316                         off += slen;
317                 }
318         }
319
320         return res;
321 }
322
323 int ovl_removexattr(struct dentry *dentry, const char *name)
324 {
325         int err;
326         struct path realpath;
327         enum ovl_path_type type = ovl_path_real(dentry, &realpath);
328         const struct cred *old_cred;
329
330         err = ovl_want_write(dentry);
331         if (err)
332                 goto out;
333
334         err = -ENODATA;
335         if (ovl_is_private_xattr(name))
336                 goto out_drop_write;
337
338         if (!OVL_TYPE_UPPER(type)) {
339                 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
340                 if (err < 0)
341                         goto out_drop_write;
342
343                 err = ovl_copy_up(dentry);
344                 if (err)
345                         goto out_drop_write;
346
347                 ovl_path_upper(dentry, &realpath);
348         }
349
350         old_cred = ovl_override_creds(dentry->d_sb);
351         err = vfs_removexattr(realpath.dentry, name);
352         revert_creds(old_cred);
353 out_drop_write:
354         ovl_drop_write(dentry);
355 out:
356         return err;
357 }
358
359 struct posix_acl *ovl_get_acl(struct inode *inode, int type)
360 {
361         struct inode *realinode = ovl_inode_real(inode);
362         const struct cred *old_cred;
363         struct posix_acl *acl;
364
365         if (!IS_POSIXACL(realinode))
366                 return NULL;
367
368         if (!realinode->i_op->get_acl)
369                 return NULL;
370
371         old_cred = ovl_override_creds(inode->i_sb);
372         acl = realinode->i_op->get_acl(realinode, type);
373         revert_creds(old_cred);
374
375         return acl;
376 }
377
378 static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
379                                   struct dentry *realdentry)
380 {
381         if (OVL_TYPE_UPPER(type))
382                 return false;
383
384         if (special_file(realdentry->d_inode->i_mode))
385                 return false;
386
387         if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
388                 return false;
389
390         return true;
391 }
392
393 int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags)
394 {
395         int err = 0;
396         struct path realpath;
397         enum ovl_path_type type;
398
399         type = ovl_path_real(dentry, &realpath);
400         if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
401                 err = ovl_want_write(dentry);
402                 if (!err) {
403                         if (file_flags & O_TRUNC)
404                                 err = ovl_copy_up_truncate(dentry);
405                         else
406                                 err = ovl_copy_up(dentry);
407                         ovl_drop_write(dentry);
408                 }
409         }
410
411         return err;
412 }
413
414 static const struct inode_operations ovl_file_inode_operations = {
415         .setattr        = ovl_setattr,
416         .permission     = ovl_permission,
417         .getattr        = ovl_getattr,
418         .setxattr       = ovl_setxattr,
419         .getxattr       = ovl_getxattr,
420         .listxattr      = ovl_listxattr,
421         .removexattr    = ovl_removexattr,
422         .get_acl        = ovl_get_acl,
423 };
424
425 static const struct inode_operations ovl_symlink_inode_operations = {
426         .setattr        = ovl_setattr,
427         .get_link       = ovl_get_link,
428         .readlink       = ovl_readlink,
429         .getattr        = ovl_getattr,
430         .setxattr       = ovl_setxattr,
431         .getxattr       = ovl_getxattr,
432         .listxattr      = ovl_listxattr,
433         .removexattr    = ovl_removexattr,
434 };
435
436 struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
437                             struct ovl_entry *oe)
438 {
439         struct inode *inode;
440
441         inode = new_inode(sb);
442         if (!inode)
443                 return NULL;
444
445         inode->i_ino = get_next_ino();
446         inode->i_mode = mode;
447         inode->i_flags |= S_NOATIME | S_NOCMTIME;
448         inode->i_private = oe;
449
450         mode &= S_IFMT;
451         switch (mode) {
452         case S_IFDIR:
453                 inode->i_op = &ovl_dir_inode_operations;
454                 inode->i_fop = &ovl_dir_operations;
455                 break;
456
457         case S_IFLNK:
458                 inode->i_op = &ovl_symlink_inode_operations;
459                 break;
460
461         case S_IFREG:
462         case S_IFSOCK:
463         case S_IFBLK:
464         case S_IFCHR:
465         case S_IFIFO:
466                 inode->i_op = &ovl_file_inode_operations;
467                 break;
468
469         default:
470                 WARN(1, "illegal file type: %i\n", mode);
471                 iput(inode);
472                 inode = NULL;
473         }
474
475         return inode;
476 }