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