blob: 69f4f19659fc53ef6d73399e37aa4fa272720271 [file] [log] [blame]
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001/*
2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, 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>
Ingo Molnar5b825c32017-02-02 17:54:15 +010011#include <linux/cred.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010012#include <linux/namei.h>
13#include <linux/xattr.h>
Miklos Szeredi02b69b22016-12-16 11:02:56 +010014#include <linux/ratelimit.h>
Amir Goldsteina9d01952017-04-30 14:46:31 +030015#include <linux/mount.h>
16#include <linux/exportfs.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010017#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010018
Miklos Szeredie28edc42016-12-16 11:02:56 +010019struct ovl_lookup_data {
20 struct qstr name;
21 bool is_dir;
22 bool opaque;
23 bool stop;
24 bool last;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010025 char *redirect;
Miklos Szeredie28edc42016-12-16 11:02:56 +010026};
Miklos Szeredibbb1e542016-12-16 11:02:56 +010027
Miklos Szeredi02b69b22016-12-16 11:02:56 +010028static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d,
29 size_t prelen, const char *post)
30{
31 int res;
32 char *s, *next, *buf = NULL;
33
34 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0);
35 if (res < 0) {
36 if (res == -ENODATA || res == -EOPNOTSUPP)
37 return 0;
38 goto fail;
39 }
Michal Hocko0ee931c2017-09-13 16:28:29 -070040 buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010041 if (!buf)
42 return -ENOMEM;
43
44 if (res == 0)
45 goto invalid;
46
47 res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res);
48 if (res < 0)
49 goto fail;
50 if (res == 0)
51 goto invalid;
52 if (buf[0] == '/') {
53 for (s = buf; *s++ == '/'; s = next) {
54 next = strchrnul(s, '/');
55 if (s == next)
56 goto invalid;
57 }
58 } else {
59 if (strchr(buf, '/') != NULL)
60 goto invalid;
61
62 memmove(buf + prelen, buf, res);
63 memcpy(buf, d->name.name, prelen);
64 }
65
66 strcat(buf, post);
67 kfree(d->redirect);
68 d->redirect = buf;
69 d->name.name = d->redirect;
70 d->name.len = strlen(d->redirect);
71
72 return 0;
73
74err_free:
75 kfree(buf);
76 return 0;
77fail:
78 pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res);
79 goto err_free;
80invalid:
81 pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf);
82 goto err_free;
83}
84
Amir Goldsteina9d01952017-04-30 14:46:31 +030085static int ovl_acceptable(void *ctx, struct dentry *dentry)
86{
87 return 1;
88}
89
Amir Goldstein8b88a2e2017-06-21 15:28:37 +030090static struct ovl_fh *ovl_get_origin_fh(struct dentry *dentry)
Amir Goldsteina9d01952017-04-30 14:46:31 +030091{
92 int res;
93 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +030094
95 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0);
96 if (res < 0) {
97 if (res == -ENODATA || res == -EOPNOTSUPP)
98 return NULL;
99 goto fail;
100 }
101 /* Zero size value means "copied up but origin unknown" */
102 if (res == 0)
103 return NULL;
104
Michal Hocko0ee931c2017-09-13 16:28:29 -0700105 fh = kzalloc(res, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300106 if (!fh)
107 return ERR_PTR(-ENOMEM);
108
109 res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res);
110 if (res < 0)
111 goto fail;
112
113 if (res < sizeof(struct ovl_fh) || res < fh->len)
114 goto invalid;
115
116 if (fh->magic != OVL_FH_MAGIC)
117 goto invalid;
118
119 /* Treat larger version and unknown flags as "origin unknown" */
120 if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL)
121 goto out;
122
123 /* Treat endianness mismatch as "origin unknown" */
124 if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
125 (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
126 goto out;
127
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300128 return fh;
129
130out:
131 kfree(fh);
132 return NULL;
133
134fail:
135 pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res);
136 goto out;
137invalid:
138 pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh);
139 goto out;
140}
141
142static struct dentry *ovl_get_origin(struct dentry *dentry,
143 struct vfsmount *mnt)
144{
145 struct dentry *origin = NULL;
146 struct ovl_fh *fh = ovl_get_origin_fh(dentry);
147 int bytes;
148
149 if (IS_ERR_OR_NULL(fh))
150 return (struct dentry *)fh;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300151
152 /*
153 * Make sure that the stored uuid matches the uuid of the lower
154 * layer where file handle will be decoded.
155 */
Christoph Hellwig85787092017-05-10 15:06:33 +0200156 if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid))
Amir Goldsteina9d01952017-04-30 14:46:31 +0300157 goto out;
158
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300159 bytes = (fh->len - offsetof(struct ovl_fh, fid));
Amir Goldsteina9d01952017-04-30 14:46:31 +0300160 origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid,
161 bytes >> 2, (int)fh->type,
162 ovl_acceptable, NULL);
163 if (IS_ERR(origin)) {
164 /* Treat stale file handle as "origin unknown" */
165 if (origin == ERR_PTR(-ESTALE))
166 origin = NULL;
167 goto out;
168 }
169
170 if (ovl_dentry_weird(origin) ||
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300171 ((d_inode(origin)->i_mode ^ d_inode(dentry)->i_mode) & S_IFMT))
Amir Goldsteina9d01952017-04-30 14:46:31 +0300172 goto invalid;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300173
174out:
175 kfree(fh);
176 return origin;
177
Amir Goldsteina9d01952017-04-30 14:46:31 +0300178invalid:
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300179 pr_warn_ratelimited("overlayfs: invalid origin (%pd2)\n", origin);
180 dput(origin);
181 origin = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300182 goto out;
183}
184
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300185static bool ovl_is_opaquedir(struct dentry *dentry)
186{
187 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
188}
189
Miklos Szeredie28edc42016-12-16 11:02:56 +0100190static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
191 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100192 size_t prelen, const char *post,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100193 struct dentry **ret)
194{
195 struct dentry *this;
196 int err;
197
198 this = lookup_one_len_unlocked(name, base, namelen);
199 if (IS_ERR(this)) {
200 err = PTR_ERR(this);
201 this = NULL;
202 if (err == -ENOENT || err == -ENAMETOOLONG)
203 goto out;
204 goto out_err;
205 }
206 if (!this->d_inode)
207 goto put_and_out;
208
209 if (ovl_dentry_weird(this)) {
210 /* Don't support traversing automounts and other weirdness */
211 err = -EREMOTE;
212 goto out_err;
213 }
214 if (ovl_is_whiteout(this)) {
215 d->stop = d->opaque = true;
216 goto put_and_out;
217 }
218 if (!d_can_lookup(this)) {
219 d->stop = true;
220 if (d->is_dir)
221 goto put_and_out;
222 goto out;
223 }
224 d->is_dir = true;
225 if (!d->last && ovl_is_opaquedir(this)) {
226 d->stop = d->opaque = true;
227 goto out;
228 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100229 err = ovl_check_redirect(this, d, prelen, post);
230 if (err)
231 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100232out:
233 *ret = this;
234 return 0;
235
236put_and_out:
237 dput(this);
238 this = NULL;
239 goto out;
240
241out_err:
242 dput(this);
243 return err;
244}
245
246static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
247 struct dentry **ret)
248{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100249 /* Counting down from the end, since the prefix can change */
250 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100251 struct dentry *dentry = NULL;
252 int err;
253
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100254 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100255 return ovl_lookup_single(base, d, d->name.name, d->name.len,
256 0, "", ret);
257
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100258 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
259 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100260 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100261 size_t thislen = next - s;
262 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100263
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100264 /* Verify we did not go off the rails */
265 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100266 return -EIO;
267
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100268 err = ovl_lookup_single(base, d, s, thislen,
269 d->name.len - rem, next, &base);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100270 dput(dentry);
271 if (err)
272 return err;
273 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100274 if (end)
275 break;
276
277 rem -= thislen + 1;
278
279 if (WARN_ON(rem >= d->name.len))
280 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100281 }
282 *ret = dentry;
283 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100284}
285
Amir Goldsteina9d01952017-04-30 14:46:31 +0300286
Amir Goldstein415543d2017-06-21 15:28:42 +0300287static int ovl_check_origin(struct dentry *upperdentry,
Chandan Rajendrab9343632017-07-24 01:57:54 -0500288 struct ovl_path *lower, unsigned int numlower,
289 struct ovl_path **stackp, unsigned int *ctrp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300290{
Amir Goldsteina9d01952017-04-30 14:46:31 +0300291 struct vfsmount *mnt;
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300292 struct dentry *origin = NULL;
293 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300294
Amir Goldstein415543d2017-06-21 15:28:42 +0300295 for (i = 0; i < numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500296 mnt = lower[i].layer->mnt;
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300297 origin = ovl_get_origin(upperdentry, mnt);
298 if (IS_ERR(origin))
299 return PTR_ERR(origin);
300
301 if (origin)
302 break;
303 }
304
305 if (!origin)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300306 return 0;
307
Amir Goldstein415543d2017-06-21 15:28:42 +0300308 BUG_ON(*ctrp);
309 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500310 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300311 if (!*stackp) {
312 dput(origin);
313 return -ENOMEM;
314 }
Chandan Rajendrab9343632017-07-24 01:57:54 -0500315 **stackp = (struct ovl_path){.dentry = origin, .layer = lower[i].layer};
Amir Goldsteina9d01952017-04-30 14:46:31 +0300316 *ctrp = 1;
317
318 return 0;
319}
320
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100321/*
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300322 * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN.
323 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
324 */
325static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh)
326{
327 struct ovl_fh *ofh = ovl_get_origin_fh(dentry);
328 int err = 0;
329
330 if (!ofh)
331 return -ENODATA;
332
333 if (IS_ERR(ofh))
334 return PTR_ERR(ofh);
335
336 if (fh->len != ofh->len || memcmp(fh, ofh, fh->len))
337 err = -ESTALE;
338
339 kfree(ofh);
340 return err;
341}
342
343/*
344 * Verify that an inode matches the origin file handle stored in upper inode.
345 *
346 * If @set is true and there is no stored file handle, encode and store origin
347 * file handle in OVL_XATTR_ORIGIN.
348 *
349 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
350 */
Amir Goldsteind9768072017-09-24 13:00:19 +0300351int ovl_verify_origin(struct dentry *dentry, struct dentry *origin,
352 bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300353{
354 struct inode *inode;
355 struct ovl_fh *fh;
356 int err;
357
Amir Goldstein54fb3472017-06-21 15:28:38 +0300358 fh = ovl_encode_fh(origin, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300359 err = PTR_ERR(fh);
360 if (IS_ERR(fh))
361 goto fail;
362
363 err = ovl_verify_origin_fh(dentry, fh);
364 if (set && err == -ENODATA)
365 err = ovl_do_setxattr(dentry, OVL_XATTR_ORIGIN, fh, fh->len, 0);
366 if (err)
367 goto fail;
368
369out:
370 kfree(fh);
371 return err;
372
373fail:
374 inode = d_inode(origin);
375 pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n",
376 origin, inode ? inode->i_ino : 0, err);
377 goto out;
378}
379
380/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300381 * Verify that an index entry name matches the origin file handle stored in
382 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
383 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
384 */
Chandan Rajendrab9343632017-07-24 01:57:54 -0500385int ovl_verify_index(struct dentry *index, struct ovl_path *lower,
Amir Goldstein415543d2017-06-21 15:28:42 +0300386 unsigned int numlower)
387{
388 struct ovl_fh *fh = NULL;
389 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500390 struct ovl_path origin = { };
391 struct ovl_path *stack = &origin;
Amir Goldstein415543d2017-06-21 15:28:42 +0300392 unsigned int ctr = 0;
393 int err;
394
395 if (!d_inode(index))
396 return 0;
397
Amir Goldstein61b67472017-07-18 21:07:42 +0300398 /*
399 * Directory index entries are going to be used for looking up
400 * redirected upper dirs by lower dir fh when decoding an overlay
401 * file handle of a merge dir. Whiteout index entries are going to be
402 * used as an indication that an exported overlay file handle should
403 * be treated as stale (i.e. after unlink of the overlay inode).
404 * We don't know the verification rules for directory and whiteout
405 * index entries, because they have not been implemented yet, so return
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300406 * EINVAL if those entries are found to abort the mount to avoid
407 * corrupting an index that was created by a newer kernel.
Amir Goldstein61b67472017-07-18 21:07:42 +0300408 */
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300409 err = -EINVAL;
Amir Goldstein61b67472017-07-18 21:07:42 +0300410 if (d_is_dir(index) || ovl_is_whiteout(index))
Amir Goldstein415543d2017-06-21 15:28:42 +0300411 goto fail;
412
Amir Goldstein415543d2017-06-21 15:28:42 +0300413 if (index->d_name.len < sizeof(struct ovl_fh)*2)
414 goto fail;
415
416 err = -ENOMEM;
417 len = index->d_name.len / 2;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700418 fh = kzalloc(len, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300419 if (!fh)
420 goto fail;
421
422 err = -EINVAL;
423 if (hex2bin((u8 *)fh, index->d_name.name, len) || len != fh->len)
424 goto fail;
425
426 err = ovl_verify_origin_fh(index, fh);
427 if (err)
428 goto fail;
429
Chandan Rajendrab9343632017-07-24 01:57:54 -0500430 err = ovl_check_origin(index, lower, numlower, &stack, &ctr);
Amir Goldstein415543d2017-06-21 15:28:42 +0300431 if (!err && !ctr)
432 err = -ESTALE;
433 if (err)
434 goto fail;
435
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300436 /* Check if index is orphan and don't warn before cleaning it */
437 if (d_inode(index)->i_nlink == 1 &&
Vivek Goyal08d8f8a2017-11-27 10:12:44 -0500438 ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300439 err = -ENOENT;
440
Amir Goldstein415543d2017-06-21 15:28:42 +0300441 dput(origin.dentry);
442out:
443 kfree(fh);
444 return err;
445
446fail:
Amir Goldstein61b67472017-07-18 21:07:42 +0300447 pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n",
448 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300449 goto out;
450}
451
452/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300453 * Lookup in indexdir for the index entry of a lower real inode or a copy up
454 * origin inode. The index entry name is the hex representation of the lower
455 * inode file handle.
456 *
457 * If the index dentry in negative, then either no lower aliases have been
458 * copied up yet, or aliases have been copied up in older kernels and are
459 * not indexed.
460 *
461 * If the index dentry for a copy up origin inode is positive, but points
462 * to an inode different than the upper inode, then either the upper inode
463 * has been copied up and not indexed or it was indexed, but since then
464 * index dir was cleared. Either way, that index cannot be used to indentify
465 * the overlay inode.
466 */
467int ovl_get_index_name(struct dentry *origin, struct qstr *name)
468{
469 int err;
470 struct ovl_fh *fh;
471 char *n, *s;
472
473 fh = ovl_encode_fh(origin, false);
474 if (IS_ERR(fh))
475 return PTR_ERR(fh);
476
477 err = -ENOMEM;
Michal Hocko0ee931c2017-09-13 16:28:29 -0700478 n = kzalloc(fh->len * 2, GFP_KERNEL);
Amir Goldstein359f3922017-06-21 15:28:41 +0300479 if (n) {
480 s = bin2hex(n, fh, fh->len);
481 *name = (struct qstr) QSTR_INIT(n, s - n);
482 err = 0;
483 }
484 kfree(fh);
485
486 return err;
487
488}
489
490static struct dentry *ovl_lookup_index(struct dentry *dentry,
491 struct dentry *upper,
492 struct dentry *origin)
493{
494 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
495 struct dentry *index;
496 struct inode *inode;
497 struct qstr name;
498 int err;
499
500 err = ovl_get_index_name(origin, &name);
501 if (err)
502 return ERR_PTR(err);
503
504 index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len);
505 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300506 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300507 if (err == -ENOENT) {
508 index = NULL;
509 goto out;
510 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300511 pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n"
512 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
513 d_inode(origin)->i_ino, name.len, name.name,
514 err);
515 goto out;
516 }
517
Amir Goldstein0e082552017-07-18 21:07:43 +0300518 inode = d_inode(index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300519 if (d_is_negative(index)) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300520 goto out_dput;
Amir Goldstein0e082552017-07-18 21:07:43 +0300521 } else if (upper && d_inode(upper) != inode) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300522 goto out_dput;
Amir Goldstein0e082552017-07-18 21:07:43 +0300523 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
524 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
525 /*
526 * Index should always be of the same file type as origin
527 * except for the case of a whiteout index. A whiteout
528 * index should only exist if all lower aliases have been
529 * unlinked, which means that finding a lower origin on lookup
530 * whose index is a whiteout should be treated as an error.
531 */
532 pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
533 index, d_inode(index)->i_mode & S_IFMT,
534 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300535 goto fail;
536 }
537
538out:
539 kfree(name.name);
540 return index;
541
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300542out_dput:
543 dput(index);
544 index = NULL;
545 goto out;
546
Amir Goldstein359f3922017-06-21 15:28:41 +0300547fail:
548 dput(index);
549 index = ERR_PTR(-EIO);
550 goto out;
551}
552
553/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100554 * Returns next layer in stack starting from top.
555 * Returns -1 if this is the last layer.
556 */
557int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
558{
559 struct ovl_entry *oe = dentry->d_fsdata;
560
561 BUG_ON(idx < 0);
562 if (idx == 0) {
563 ovl_path_upper(dentry, path);
564 if (path->dentry)
565 return oe->numlower ? 1 : -1;
566 idx++;
567 }
568 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500569 path->dentry = oe->lowerstack[idx - 1].dentry;
570 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100571
572 return (idx < oe->numlower) ? idx + 1 : -1;
573}
574
Chandan Rajendrab9343632017-07-24 01:57:54 -0500575static int ovl_find_layer(struct ovl_fs *ofs, struct ovl_path *path)
576{
577 int i;
578
579 for (i = 0; i < ofs->numlower; i++) {
580 if (ofs->lower_layers[i].mnt == path->layer->mnt)
581 break;
582 }
583
584 return i;
585}
586
Amir Goldstein9678e632018-01-03 19:34:45 +0200587/* Fix missing 'origin' xattr */
588static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
589 struct dentry *upper)
590{
591 int err;
592
593 if (ovl_check_origin_xattr(upper))
594 return 0;
595
596 err = ovl_want_write(dentry);
597 if (err)
598 return err;
599
600 err = ovl_set_origin(dentry, lower, upper);
601 if (!err)
602 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
603
604 ovl_drop_write(dentry);
605 return err;
606}
607
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100608struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
609 unsigned int flags)
610{
611 struct ovl_entry *oe;
612 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100613 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100614 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300615 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500616 struct ovl_path *stack = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100617 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300618 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100619 unsigned int ctr = 0;
620 struct inode *inode = NULL;
621 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100622 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100623 struct dentry *this;
624 unsigned int i;
625 int err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100626 struct ovl_lookup_data d = {
627 .name = dentry->d_name,
628 .is_dir = false,
629 .opaque = false,
630 .stop = false,
631 .last = !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100632 .redirect = NULL,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100633 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100634
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100635 if (dentry->d_name.len > ofs->namelen)
636 return ERR_PTR(-ENAMETOOLONG);
637
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100638 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200639 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100640 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100641 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
642 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100643 goto out;
644
Miklos Szeredie28edc42016-12-16 11:02:56 +0100645 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
646 dput(upperdentry);
647 err = -EREMOTE;
648 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100649 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300650 if (upperdentry && !d.is_dir) {
651 BUG_ON(!d.stop || d.redirect);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300652 /*
653 * Lookup copy up origin by decoding origin file handle.
654 * We may get a disconnected dentry, which is fine,
655 * because we only need to hold the origin inode in
656 * cache and use its inode number. We may even get a
657 * connected dentry, that is not under any of the lower
658 * layers root. That is also fine for using it's inode
659 * number - it's the same as if we held a reference
660 * to a dentry in lower layer that was moved under us.
661 */
Amir Goldstein415543d2017-06-21 15:28:42 +0300662 err = ovl_check_origin(upperdentry, roe->lowerstack,
663 roe->numlower, &stack, &ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300664 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400665 goto out_put_upper;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300666 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100667
668 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300669 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100670 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
671 if (!upperredirect)
672 goto out_put_upper;
673 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300674 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100675 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100676 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100677 }
678
Miklos Szeredie28edc42016-12-16 11:02:56 +0100679 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100680 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500681 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700682 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100683 if (!stack)
684 goto out_put_upper;
685 }
686
Miklos Szeredie28edc42016-12-16 11:02:56 +0100687 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500688 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100689
Miklos Szeredie28edc42016-12-16 11:02:56 +0100690 d.last = i == poe->numlower - 1;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500691 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100692 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100693 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100694
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100695 if (!this)
696 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100697
Amir Goldstein9678e632018-01-03 19:34:45 +0200698 /*
699 * If no origin fh is stored in upper of a merge dir, store fh
700 * of lower dir and set upper parent "impure".
701 */
702 if (upperdentry && !ctr && !ofs->noxattr) {
703 err = ovl_fix_origin(dentry, this, upperdentry);
704 if (err) {
705 dput(this);
706 goto out_put;
707 }
708 }
709
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100710 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500711 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100712 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100713
714 if (d.stop)
715 break;
716
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100717 /*
718 * Following redirects can have security consequences: it's like
719 * a symlink into the lower layer without the permission checks.
720 * This is only a problem if the upper layer is untrusted (e.g
721 * comes from an USB drive). This can allow a non-readable file
722 * or directory to become readable.
723 *
724 * Only following redirects when redirects are enabled disables
725 * this attack vector when not necessary.
726 */
727 err = -EPERM;
728 if (d.redirect && !ofs->config.redirect_follow) {
Amir Goldsteinf8167812017-12-18 14:25:56 +0200729 pr_warn_ratelimited("overlayfs: refusing to follow redirect for (%pd2)\n",
730 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100731 goto out_put;
732 }
733
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300734 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
735 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100736
737 /* Find the current layer on the root dentry */
Chandan Rajendrab9343632017-07-24 01:57:54 -0500738 i = ovl_find_layer(ofs, &lower);
739 if (WARN_ON(i == ofs->numlower))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100740 break;
741 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100742 }
743
Amir Goldstein359f3922017-06-21 15:28:41 +0300744 /* Lookup index by lower inode and verify it matches upper inode */
745 if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) {
746 struct dentry *origin = stack[0].dentry;
747
748 index = ovl_lookup_index(dentry, upperdentry, origin);
749 if (IS_ERR(index)) {
750 err = PTR_ERR(index);
751 index = NULL;
752 goto out_put;
753 }
754 }
755
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100756 oe = ovl_alloc_entry(ctr);
757 err = -ENOMEM;
758 if (!oe)
759 goto out_put;
760
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100761 oe->opaque = upperopaque;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500762 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200763 dentry->d_fsdata = oe;
764
Miklos Szeredi55acc662017-07-04 22:03:18 +0200765 if (upperdentry)
766 ovl_dentry_set_upper_alias(dentry);
767 else if (index)
Amir Goldstein359f3922017-06-21 15:28:41 +0300768 upperdentry = dget(index);
769
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200770 if (upperdentry || ctr) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300771 inode = ovl_get_inode(dentry, upperdentry, index);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200772 err = PTR_ERR(inode);
773 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200774 goto out_free_oe;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200775
776 OVL_I(inode)->redirect = upperredirect;
Amir Goldstein359f3922017-06-21 15:28:41 +0300777 if (index)
778 ovl_set_flag(OVL_INDEX, inode);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200779 }
780
781 revert_creds(old_cred);
Amir Goldstein359f3922017-06-21 15:28:41 +0300782 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100783 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100784 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100785 d_add(dentry, inode);
786
787 return NULL;
788
789out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200790 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100791 kfree(oe);
792out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +0300793 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100794 for (i = 0; i < ctr; i++)
795 dput(stack[i].dentry);
796 kfree(stack);
797out_put_upper:
798 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100799 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100800out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100801 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100802 revert_creds(old_cred);
803 return ERR_PTR(err);
804}
805
806bool ovl_lower_positive(struct dentry *dentry)
807{
808 struct ovl_entry *oe = dentry->d_fsdata;
809 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
810 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200811 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100812 unsigned int i;
813 bool positive = false;
814 bool done = false;
815
816 /*
817 * If dentry is negative, then lower is positive iff this is a
818 * whiteout.
819 */
820 if (!dentry->d_inode)
821 return oe->opaque;
822
823 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200824 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100825 return true;
826
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200827 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100828 /* Positive upper -> have to look up lower to see whether it exists */
829 for (i = 0; !done && !positive && i < poe->numlower; i++) {
830 struct dentry *this;
831 struct dentry *lowerdir = poe->lowerstack[i].dentry;
832
833 this = lookup_one_len_unlocked(name->name, lowerdir,
834 name->len);
835 if (IS_ERR(this)) {
836 switch (PTR_ERR(this)) {
837 case -ENOENT:
838 case -ENAMETOOLONG:
839 break;
840
841 default:
842 /*
843 * Assume something is there, we just couldn't
844 * access it.
845 */
846 positive = true;
847 break;
848 }
849 } else {
850 if (this->d_inode) {
851 positive = !ovl_is_whiteout(this);
852 done = true;
853 }
854 dput(this);
855 }
856 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +0200857 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100858
859 return positive;
860}