blob: 49e204560655a8f1737a2a03a38b7a88604a5a4c [file] [log] [blame]
Miklos Szeredie9be9d52014-10-24 00:14:38 +02001/*
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
Al Viro0f7ff2d2015-12-06 12:31:07 -050015static int ovl_copy_up_truncate(struct dentry *dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +020016{
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
Al Viro0f7ff2d2015-12-06 12:31:07 -050032 stat.size = 0;
33 err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020034
35out_dput_parent:
36 dput(parent);
37 return err;
38}
39
40int ovl_setattr(struct dentry *dentry, struct iattr *attr)
41{
42 int err;
43 struct dentry *upperdentry;
44
Miklos Szeredicf9a6782015-12-11 16:30:49 +010045 /*
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
Miklos Szeredie9be9d52014-10-24 00:14:38 +020058 err = ovl_want_write(dentry);
59 if (err)
60 goto out;
61
Miklos Szerediacff81e2015-12-04 19:18:48 +010062 err = ovl_copy_up(dentry);
63 if (!err) {
64 upperdentry = ovl_dentry_upper(dentry);
65
Al Viro59551022016-01-22 15:40:57 -050066 inode_lock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020067 err = notify_change(upperdentry, attr, NULL);
Al Viro59551022016-01-22 15:40:57 -050068 inode_unlock(upperdentry->d_inode);
Miklos Szeredie9be9d52014-10-24 00:14:38 +020069 }
70 ovl_drop_write(dentry);
71out:
72 return err;
73}
74
75static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
76 struct kstat *stat)
77{
78 struct path realpath;
79
80 ovl_path_real(dentry, &realpath);
81 return vfs_getattr(&realpath, stat);
82}
83
84int ovl_permission(struct inode *inode, int mask)
85{
86 struct ovl_entry *oe;
87 struct dentry *alias = NULL;
88 struct inode *realinode;
89 struct dentry *realdentry;
90 bool is_upper;
91 int err;
92
93 if (S_ISDIR(inode->i_mode)) {
94 oe = inode->i_private;
95 } else if (mask & MAY_NOT_BLOCK) {
96 return -ECHILD;
97 } else {
98 /*
99 * For non-directories find an alias and get the info
100 * from there.
101 */
102 alias = d_find_any_alias(inode);
103 if (WARN_ON(!alias))
104 return -ENOENT;
105
106 oe = alias->d_fsdata;
107 }
108
109 realdentry = ovl_entry_real(oe, &is_upper);
110
Miklos Szeredi8d3095f2015-10-12 17:11:44 +0200111 if (ovl_is_default_permissions(inode)) {
112 struct kstat stat;
113 struct path realpath = { .dentry = realdentry };
114
115 if (mask & MAY_NOT_BLOCK)
116 return -ECHILD;
117
118 realpath.mnt = ovl_entry_mnt_real(oe, inode, is_upper);
119
120 err = vfs_getattr(&realpath, &stat);
121 if (err)
122 return err;
123
124 if ((stat.mode ^ inode->i_mode) & S_IFMT)
125 return -ESTALE;
126
127 inode->i_mode = stat.mode;
128 inode->i_uid = stat.uid;
129 inode->i_gid = stat.gid;
130
131 return generic_permission(inode, mask);
132 }
133
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134 /* Careful in RCU walk mode */
135 realinode = ACCESS_ONCE(realdentry->d_inode);
136 if (!realinode) {
137 WARN_ON(!(mask & MAY_NOT_BLOCK));
138 err = -ENOENT;
139 goto out_dput;
140 }
141
142 if (mask & MAY_WRITE) {
143 umode_t mode = realinode->i_mode;
144
145 /*
146 * Writes will always be redirected to upper layer, so
147 * ignore lower layer being read-only.
148 *
149 * If the overlay itself is read-only then proceed
150 * with the permission check, don't return EROFS.
151 * This will only happen if this is the lower layer of
152 * another overlayfs.
153 *
154 * If upper fs becomes read-only after the overlay was
155 * constructed return EROFS to prevent modification of
156 * upper layer.
157 */
158 err = -EROFS;
159 if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
160 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
161 goto out_dput;
162 }
163
164 err = __inode_permission(realinode, mask);
165out_dput:
166 dput(alias);
167 return err;
168}
169
Al Viro6b255392015-11-17 10:20:54 -0500170static const char *ovl_get_link(struct dentry *dentry,
Al Virofceef392015-12-29 15:58:39 -0500171 struct inode *inode,
172 struct delayed_call *done)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200173{
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200174 struct dentry *realdentry;
175 struct inode *realinode;
176
Al Viro6b255392015-11-17 10:20:54 -0500177 if (!dentry)
178 return ERR_PTR(-ECHILD);
179
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200180 realdentry = ovl_dentry_real(dentry);
181 realinode = realdentry->d_inode;
182
Al Viro6b255392015-11-17 10:20:54 -0500183 if (WARN_ON(!realinode->i_op->get_link))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200184 return ERR_PTR(-EPERM);
185
Al Virofceef392015-12-29 15:58:39 -0500186 return realinode->i_op->get_link(realdentry, realinode, done);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200187}
188
189static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
190{
191 struct path realpath;
192 struct inode *realinode;
193
194 ovl_path_real(dentry, &realpath);
195 realinode = realpath.dentry->d_inode;
196
197 if (!realinode->i_op->readlink)
198 return -EINVAL;
199
200 touch_atime(&realpath);
201
202 return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
203}
204
205
206static bool ovl_is_private_xattr(const char *name)
207{
hujianyangcead89b2014-11-24 18:25:21 +0800208 return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200209}
210
211int ovl_setxattr(struct dentry *dentry, const char *name,
212 const void *value, size_t size, int flags)
213{
214 int err;
215 struct dentry *upperdentry;
216
217 err = ovl_want_write(dentry);
218 if (err)
219 goto out;
220
221 err = -EPERM;
222 if (ovl_is_private_xattr(name))
223 goto out_drop_write;
224
225 err = ovl_copy_up(dentry);
226 if (err)
227 goto out_drop_write;
228
229 upperdentry = ovl_dentry_upper(dentry);
230 err = vfs_setxattr(upperdentry, name, value, size, flags);
231
232out_drop_write:
233 ovl_drop_write(dentry);
234out:
235 return err;
236}
237
Miklos Szeredi52148462014-11-20 16:40:00 +0100238static bool ovl_need_xattr_filter(struct dentry *dentry,
239 enum ovl_path_type type)
240{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100241 if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER)
242 return S_ISDIR(dentry->d_inode->i_mode);
243 else
244 return false;
Miklos Szeredi52148462014-11-20 16:40:00 +0100245}
246
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200247ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
248 void *value, size_t size)
249{
Miklos Szeredi52148462014-11-20 16:40:00 +0100250 struct path realpath;
251 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
252
253 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200254 return -ENODATA;
255
Miklos Szeredi52148462014-11-20 16:40:00 +0100256 return vfs_getxattr(realpath.dentry, name, value, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200257}
258
259ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
260{
Miklos Szeredi52148462014-11-20 16:40:00 +0100261 struct path realpath;
262 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200263 ssize_t res;
264 int off;
265
Miklos Szeredi52148462014-11-20 16:40:00 +0100266 res = vfs_listxattr(realpath.dentry, list, size);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200267 if (res <= 0 || size == 0)
268 return res;
269
Miklos Szeredi52148462014-11-20 16:40:00 +0100270 if (!ovl_need_xattr_filter(dentry, type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271 return res;
272
273 /* filter out private xattrs */
274 for (off = 0; off < res;) {
275 char *s = list + off;
276 size_t slen = strlen(s) + 1;
277
278 BUG_ON(off + slen > res);
279
280 if (ovl_is_private_xattr(s)) {
281 res -= slen;
282 memmove(s, s + slen, res - off);
283 } else {
284 off += slen;
285 }
286 }
287
288 return res;
289}
290
291int ovl_removexattr(struct dentry *dentry, const char *name)
292{
293 int err;
294 struct path realpath;
Miklos Szeredi52148462014-11-20 16:40:00 +0100295 enum ovl_path_type type = ovl_path_real(dentry, &realpath);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200296
297 err = ovl_want_write(dentry);
298 if (err)
299 goto out;
300
Miklos Szeredi52148462014-11-20 16:40:00 +0100301 err = -ENODATA;
302 if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303 goto out_drop_write;
304
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100305 if (!OVL_TYPE_UPPER(type)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200306 err = vfs_getxattr(realpath.dentry, name, NULL, 0);
307 if (err < 0)
308 goto out_drop_write;
309
310 err = ovl_copy_up(dentry);
311 if (err)
312 goto out_drop_write;
313
314 ovl_path_upper(dentry, &realpath);
315 }
316
317 err = vfs_removexattr(realpath.dentry, name);
318out_drop_write:
319 ovl_drop_write(dentry);
320out:
321 return err;
322}
323
324static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
325 struct dentry *realdentry)
326{
Miklos Szeredi1afaba12014-12-13 00:59:42 +0100327 if (OVL_TYPE_UPPER(type))
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200328 return false;
329
330 if (special_file(realdentry->d_inode->i_mode))
331 return false;
332
333 if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
334 return false;
335
336 return true;
337}
338
David Howells4bacc9c2015-06-18 14:32:31 +0100339struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200340{
341 int err;
342 struct path realpath;
343 enum ovl_path_type type;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200344
Al Viro9391dd02015-07-12 10:39:45 -0400345 if (d_is_dir(dentry))
346 return d_backing_inode(dentry);
347
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200348 type = ovl_path_real(dentry, &realpath);
David Howells4bacc9c2015-06-18 14:32:31 +0100349 if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) {
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200350 err = ovl_want_write(dentry);
351 if (err)
David Howells4bacc9c2015-06-18 14:32:31 +0100352 return ERR_PTR(err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200353
David Howells4bacc9c2015-06-18 14:32:31 +0100354 if (file_flags & O_TRUNC)
Al Viro0f7ff2d2015-12-06 12:31:07 -0500355 err = ovl_copy_up_truncate(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200356 else
357 err = ovl_copy_up(dentry);
David Howellsf25801e2015-06-18 14:32:23 +0100358 ovl_drop_write(dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200359 if (err)
David Howells4bacc9c2015-06-18 14:32:31 +0100360 return ERR_PTR(err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200361
362 ovl_path_upper(dentry, &realpath);
363 }
364
Miklos Szeredi1c8a47d2015-10-12 15:56:20 +0200365 if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE)
366 return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags);
367
David Howells4bacc9c2015-06-18 14:32:31 +0100368 return d_backing_inode(realpath.dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200369}
370
371static const struct inode_operations ovl_file_inode_operations = {
372 .setattr = ovl_setattr,
373 .permission = ovl_permission,
374 .getattr = ovl_getattr,
375 .setxattr = ovl_setxattr,
376 .getxattr = ovl_getxattr,
377 .listxattr = ovl_listxattr,
378 .removexattr = ovl_removexattr,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200379};
380
381static const struct inode_operations ovl_symlink_inode_operations = {
382 .setattr = ovl_setattr,
Al Viro6b255392015-11-17 10:20:54 -0500383 .get_link = ovl_get_link,
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200384 .readlink = ovl_readlink,
385 .getattr = ovl_getattr,
386 .setxattr = ovl_setxattr,
387 .getxattr = ovl_getxattr,
388 .listxattr = ovl_listxattr,
389 .removexattr = ovl_removexattr,
390};
391
392struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
393 struct ovl_entry *oe)
394{
395 struct inode *inode;
396
397 inode = new_inode(sb);
398 if (!inode)
399 return NULL;
400
401 mode &= S_IFMT;
402
403 inode->i_ino = get_next_ino();
404 inode->i_mode = mode;
405 inode->i_flags |= S_NOATIME | S_NOCMTIME;
406
407 switch (mode) {
408 case S_IFDIR:
409 inode->i_private = oe;
410 inode->i_op = &ovl_dir_inode_operations;
411 inode->i_fop = &ovl_dir_operations;
412 break;
413
414 case S_IFLNK:
415 inode->i_op = &ovl_symlink_inode_operations;
416 break;
417
418 case S_IFREG:
419 case S_IFSOCK:
420 case S_IFBLK:
421 case S_IFCHR:
422 case S_IFIFO:
423 inode->i_op = &ovl_file_inode_operations;
424 break;
425
426 default:
427 WARN(1, "illegal file type: %i\n", mode);
428 iput(inode);
429 inode = NULL;
430 }
431
432 return inode;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433}