blob: beb945e1963c0aac86fbce312ca9814821a1c33d [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
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100587struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
588 unsigned int flags)
589{
590 struct ovl_entry *oe;
591 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100592 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100593 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300594 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500595 struct ovl_path *stack = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100596 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300597 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100598 unsigned int ctr = 0;
599 struct inode *inode = NULL;
600 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100601 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100602 struct dentry *this;
603 unsigned int i;
604 int err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100605 struct ovl_lookup_data d = {
606 .name = dentry->d_name,
607 .is_dir = false,
608 .opaque = false,
609 .stop = false,
610 .last = !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100611 .redirect = NULL,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100612 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100613
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100614 if (dentry->d_name.len > ofs->namelen)
615 return ERR_PTR(-ENAMETOOLONG);
616
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100617 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200618 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100619 if (upperdir) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100620 err = ovl_lookup_layer(upperdir, &d, &upperdentry);
621 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100622 goto out;
623
Miklos Szeredie28edc42016-12-16 11:02:56 +0100624 if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) {
625 dput(upperdentry);
626 err = -EREMOTE;
627 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100628 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300629 if (upperdentry && !d.is_dir) {
630 BUG_ON(!d.stop || d.redirect);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300631 /*
632 * Lookup copy up origin by decoding origin file handle.
633 * We may get a disconnected dentry, which is fine,
634 * because we only need to hold the origin inode in
635 * cache and use its inode number. We may even get a
636 * connected dentry, that is not under any of the lower
637 * layers root. That is also fine for using it's inode
638 * number - it's the same as if we held a reference
639 * to a dentry in lower layer that was moved under us.
640 */
Amir Goldstein415543d2017-06-21 15:28:42 +0300641 err = ovl_check_origin(upperdentry, roe->lowerstack,
642 roe->numlower, &stack, &ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300643 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400644 goto out_put_upper;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300645 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100646
647 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300648 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100649 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
650 if (!upperredirect)
651 goto out_put_upper;
652 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300653 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100654 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100655 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100656 }
657
Miklos Szeredie28edc42016-12-16 11:02:56 +0100658 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100659 err = -ENOMEM;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500660 stack = kcalloc(ofs->numlower, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700661 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100662 if (!stack)
663 goto out_put_upper;
664 }
665
Miklos Szeredie28edc42016-12-16 11:02:56 +0100666 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500667 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100668
Miklos Szeredie28edc42016-12-16 11:02:56 +0100669 d.last = i == poe->numlower - 1;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500670 err = ovl_lookup_layer(lower.dentry, &d, &this);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100671 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100672 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100673
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100674 if (!this)
675 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100676
677 stack[ctr].dentry = this;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500678 stack[ctr].layer = lower.layer;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100679 ctr++;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100680
681 if (d.stop)
682 break;
683
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100684 /*
685 * Following redirects can have security consequences: it's like
686 * a symlink into the lower layer without the permission checks.
687 * This is only a problem if the upper layer is untrusted (e.g
688 * comes from an USB drive). This can allow a non-readable file
689 * or directory to become readable.
690 *
691 * Only following redirects when redirects are enabled disables
692 * this attack vector when not necessary.
693 */
694 err = -EPERM;
695 if (d.redirect && !ofs->config.redirect_follow) {
696 pr_warn_ratelimited("overlay: refusing to follow redirect for (%pd2)\n", dentry);
697 goto out_put;
698 }
699
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300700 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
701 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100702
703 /* Find the current layer on the root dentry */
Chandan Rajendrab9343632017-07-24 01:57:54 -0500704 i = ovl_find_layer(ofs, &lower);
705 if (WARN_ON(i == ofs->numlower))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100706 break;
707 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100708 }
709
Amir Goldstein359f3922017-06-21 15:28:41 +0300710 /* Lookup index by lower inode and verify it matches upper inode */
711 if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) {
712 struct dentry *origin = stack[0].dentry;
713
714 index = ovl_lookup_index(dentry, upperdentry, origin);
715 if (IS_ERR(index)) {
716 err = PTR_ERR(index);
717 index = NULL;
718 goto out_put;
719 }
720 }
721
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100722 oe = ovl_alloc_entry(ctr);
723 err = -ENOMEM;
724 if (!oe)
725 goto out_put;
726
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100727 oe->opaque = upperopaque;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500728 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200729 dentry->d_fsdata = oe;
730
Miklos Szeredi55acc662017-07-04 22:03:18 +0200731 if (upperdentry)
732 ovl_dentry_set_upper_alias(dentry);
733 else if (index)
Amir Goldstein359f3922017-06-21 15:28:41 +0300734 upperdentry = dget(index);
735
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200736 if (upperdentry || ctr) {
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300737 inode = ovl_get_inode(dentry, upperdentry, index);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +0200738 err = PTR_ERR(inode);
739 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200740 goto out_free_oe;
Miklos Szeredicf31c462017-07-04 22:03:16 +0200741
742 OVL_I(inode)->redirect = upperredirect;
Amir Goldstein359f3922017-06-21 15:28:41 +0300743 if (index)
744 ovl_set_flag(OVL_INDEX, inode);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200745 }
746
747 revert_creds(old_cred);
Amir Goldstein359f3922017-06-21 15:28:41 +0300748 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100749 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100750 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100751 d_add(dentry, inode);
752
753 return NULL;
754
755out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +0200756 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100757 kfree(oe);
758out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +0300759 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100760 for (i = 0; i < ctr; i++)
761 dput(stack[i].dentry);
762 kfree(stack);
763out_put_upper:
764 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100765 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100766out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100767 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100768 revert_creds(old_cred);
769 return ERR_PTR(err);
770}
771
772bool ovl_lower_positive(struct dentry *dentry)
773{
774 struct ovl_entry *oe = dentry->d_fsdata;
775 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
776 const struct qstr *name = &dentry->d_name;
777 unsigned int i;
778 bool positive = false;
779 bool done = false;
780
781 /*
782 * If dentry is negative, then lower is positive iff this is a
783 * whiteout.
784 */
785 if (!dentry->d_inode)
786 return oe->opaque;
787
788 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200789 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100790 return true;
791
792 /* Positive upper -> have to look up lower to see whether it exists */
793 for (i = 0; !done && !positive && i < poe->numlower; i++) {
794 struct dentry *this;
795 struct dentry *lowerdir = poe->lowerstack[i].dentry;
796
797 this = lookup_one_len_unlocked(name->name, lowerdir,
798 name->len);
799 if (IS_ERR(this)) {
800 switch (PTR_ERR(this)) {
801 case -ENOENT:
802 case -ENAMETOOLONG:
803 break;
804
805 default:
806 /*
807 * Assume something is there, we just couldn't
808 * access it.
809 */
810 positive = true;
811 break;
812 }
813 } else {
814 if (this->d_inode) {
815 positive = !ovl_is_whiteout(this);
816 done = true;
817 }
818 dput(this);
819 }
820 }
821
822 return positive;
823}