blob: 3566282a9199cb6e759cf21cf053ddaea3819464 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredibbb1e542016-12-16 11:02:56 +01002/*
3 * Copyright (C) 2011 Novell Inc.
4 * Copyright (C) 2016 Red Hat, Inc.
Miklos Szeredibbb1e542016-12-16 11:02:56 +01005 */
6
7#include <linux/fs.h>
Ingo Molnar5b825c32017-02-02 17:54:15 +01008#include <linux/cred.h>
Amir Goldstein9ee60ce2017-11-01 10:13:51 +02009#include <linux/ctype.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010010#include <linux/namei.h>
11#include <linux/xattr.h>
Miklos Szeredi02b69b22016-12-16 11:02:56 +010012#include <linux/ratelimit.h>
Amir Goldsteina9d01952017-04-30 14:46:31 +030013#include <linux/mount.h>
14#include <linux/exportfs.h>
Miklos Szeredibbb1e542016-12-16 11:02:56 +010015#include "overlayfs.h"
Miklos Szeredibbb1e542016-12-16 11:02:56 +010016
Miklos Szeredie28edc42016-12-16 11:02:56 +010017struct ovl_lookup_data {
Amir Goldstein146d62e2019-04-18 17:42:08 +030018 struct super_block *sb;
Miklos Szeredie28edc42016-12-16 11:02:56 +010019 struct qstr name;
20 bool is_dir;
21 bool opaque;
22 bool stop;
23 bool last;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010024 char *redirect;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -040025 bool metacopy;
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;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040032 char *buf;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010033
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040034 buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post));
35 if (IS_ERR_OR_NULL(buf))
36 return PTR_ERR(buf);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010037
Miklos Szeredi02b69b22016-12-16 11:02:56 +010038 if (buf[0] == '/') {
Amir Goldstein3ec9b3f2018-03-12 10:30:41 -040039 /*
40 * One of the ancestor path elements in an absolute path
41 * lookup in ovl_lookup_layer() could have been opaque and
42 * that will stop further lookup in lower layers (d->stop=true)
43 * But we have found an absolute redirect in decendant path
44 * element and that should force continue lookup in lower
45 * layers (reset d->stop).
46 */
47 d->stop = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010048 } else {
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040049 res = strlen(buf) + 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010050 memmove(buf + prelen, buf, res);
51 memcpy(buf, d->name.name, prelen);
52 }
53
54 strcat(buf, post);
55 kfree(d->redirect);
56 d->redirect = buf;
57 d->name.name = d->redirect;
58 d->name.len = strlen(d->redirect);
59
60 return 0;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010061}
62
Amir Goldsteina9d01952017-04-30 14:46:31 +030063static int ovl_acceptable(void *ctx, struct dentry *dentry)
64{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020065 /*
66 * A non-dir origin may be disconnected, which is fine, because
67 * we only need it for its unique inode number.
68 */
69 if (!d_is_dir(dentry))
70 return 1;
71
72 /* Don't decode a deleted empty directory */
73 if (d_unhashed(dentry))
74 return 0;
75
76 /* Check if directory belongs to the layer we are decoding from */
77 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +030078}
79
Amir Goldstein2e1a53282017-10-24 15:12:15 +030080/*
81 * Check validity of an overlay file handle buffer.
82 *
83 * Return 0 for a valid file handle.
84 * Return -ENODATA for "origin unknown".
85 * Return <0 for an invalid file handle.
86 */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020087int ovl_check_fb_len(struct ovl_fb *fb, int fb_len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030088{
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020089 if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030090 return -EINVAL;
91
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020092 if (fb->magic != OVL_FH_MAGIC)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030093 return -EINVAL;
94
95 /* Treat larger version and unknown flags as "origin unknown" */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020096 if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030097 return -ENODATA;
98
99 /* Treat endianness mismatch as "origin unknown" */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200100 if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
101 (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300102 return -ENODATA;
103
104 return 0;
105}
106
Amir Goldstein05122442018-01-11 08:25:32 +0200107static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300108{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300109 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300110 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300111
Amir Goldstein05122442018-01-11 08:25:32 +0200112 res = vfs_getxattr(dentry, name, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300113 if (res < 0) {
114 if (res == -ENODATA || res == -EOPNOTSUPP)
115 return NULL;
116 goto fail;
117 }
118 /* Zero size value means "copied up but origin unknown" */
119 if (res == 0)
120 return NULL;
121
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200122 fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300123 if (!fh)
124 return ERR_PTR(-ENOMEM);
125
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200126 res = vfs_getxattr(dentry, name, fh->buf, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300127 if (res < 0)
128 goto fail;
129
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200130 err = ovl_check_fb_len(&fh->fb, res);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300131 if (err < 0) {
132 if (err == -ENODATA)
133 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300134 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300135 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300136
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300137 return fh;
138
139out:
140 kfree(fh);
141 return NULL;
142
143fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800144 pr_warn_ratelimited("failed to get origin (%i)\n", res);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300145 goto out;
146invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800147 pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300148 goto out;
149}
150
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200151struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
152 bool connected)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300153{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200154 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300155 int bytes;
156
Amir Goldsteina9d01952017-04-30 14:46:31 +0300157 /*
158 * Make sure that the stored uuid matches the uuid of the lower
159 * layer where file handle will be decoded.
160 */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200161 if (!uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300162 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300163
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200164 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
165 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
166 bytes >> 2, (int)fh->fb.type,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200167 connected ? ovl_acceptable : NULL, mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200168 if (IS_ERR(real)) {
169 /*
170 * Treat stale file handle to lower file as "origin unknown".
171 * upper file handle could become stale when upper file is
172 * unlinked and this information is needed to handle stale
173 * index entries correctly.
174 */
175 if (real == ERR_PTR(-ESTALE) &&
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200176 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200177 real = NULL;
178 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300179 }
180
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200181 if (ovl_dentry_weird(real)) {
182 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300183 return NULL;
184 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300185
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200186 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300187}
188
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300189static bool ovl_is_opaquedir(struct dentry *dentry)
190{
191 return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE);
192}
193
Chengguang Xu1434a652020-05-26 09:35:57 +0800194static struct dentry *ovl_lookup_positive_unlocked(const char *name,
195 struct dentry *base, int len,
196 bool drop_negative)
197{
198 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
199
200 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
201 if (drop_negative && ret->d_lockref.count == 1) {
202 spin_lock(&ret->d_lock);
203 /* Recheck condition under lock */
204 if (d_is_negative(ret) && ret->d_lockref.count == 1)
205 __d_drop(ret);
206 spin_unlock(&ret->d_lock);
207 }
208 dput(ret);
209 ret = ERR_PTR(-ENOENT);
210 }
211 return ret;
212}
213
Miklos Szeredie28edc42016-12-16 11:02:56 +0100214static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
215 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100216 size_t prelen, const char *post,
Chengguang Xu1434a652020-05-26 09:35:57 +0800217 struct dentry **ret, bool drop_negative)
Miklos Szeredie28edc42016-12-16 11:02:56 +0100218{
219 struct dentry *this;
220 int err;
Vivek Goyal102b0d12018-03-09 15:44:43 -0500221 bool last_element = !post[0];
Miklos Szeredie28edc42016-12-16 11:02:56 +0100222
Chengguang Xu1434a652020-05-26 09:35:57 +0800223 this = ovl_lookup_positive_unlocked(name, base, namelen, drop_negative);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100224 if (IS_ERR(this)) {
225 err = PTR_ERR(this);
226 this = NULL;
227 if (err == -ENOENT || err == -ENAMETOOLONG)
228 goto out;
229 goto out_err;
230 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100231
232 if (ovl_dentry_weird(this)) {
233 /* Don't support traversing automounts and other weirdness */
234 err = -EREMOTE;
235 goto out_err;
236 }
237 if (ovl_is_whiteout(this)) {
238 d->stop = d->opaque = true;
239 goto put_and_out;
240 }
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400241 /*
242 * This dentry should be a regular file if previous layer lookup
243 * found a metacopy dentry.
244 */
245 if (last_element && d->metacopy && !d_is_reg(this)) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100246 d->stop = true;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400247 goto put_and_out;
248 }
249 if (!d_can_lookup(this)) {
250 if (d->is_dir || !last_element) {
251 d->stop = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100252 goto put_and_out;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400253 }
254 err = ovl_check_metacopy_xattr(this);
255 if (err < 0)
256 goto out_err;
Miklos Szeredi3a291772018-04-12 12:04:49 +0200257
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400258 d->metacopy = err;
259 d->stop = !d->metacopy;
Vivek Goyalb8a88242018-05-11 11:49:31 -0400260 if (!d->metacopy || d->last)
261 goto out;
Vivek Goyal0618a812018-05-11 11:49:31 -0400262 } else {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300263 if (ovl_lookup_trap_inode(d->sb, this)) {
264 /* Caught in a trap of overlapping layers */
265 err = -ELOOP;
266 goto out_err;
267 }
268
Vivek Goyal102b0d12018-03-09 15:44:43 -0500269 if (last_element)
Vivek Goyal0618a812018-05-11 11:49:31 -0400270 d->is_dir = true;
271 if (d->last)
272 goto out;
273
274 if (ovl_is_opaquedir(this)) {
275 d->stop = true;
276 if (last_element)
277 d->opaque = true;
278 goto out;
279 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100280 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100281 err = ovl_check_redirect(this, d, prelen, post);
282 if (err)
283 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100284out:
285 *ret = this;
286 return 0;
287
288put_and_out:
289 dput(this);
290 this = NULL;
291 goto out;
292
293out_err:
294 dput(this);
295 return err;
296}
297
298static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
Chengguang Xu1434a652020-05-26 09:35:57 +0800299 struct dentry **ret, bool drop_negative)
Miklos Szeredie28edc42016-12-16 11:02:56 +0100300{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100301 /* Counting down from the end, since the prefix can change */
302 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100303 struct dentry *dentry = NULL;
304 int err;
305
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100306 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100307 return ovl_lookup_single(base, d, d->name.name, d->name.len,
Chengguang Xu1434a652020-05-26 09:35:57 +0800308 0, "", ret, drop_negative);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100309
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100310 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
311 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100312 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100313 size_t thislen = next - s;
314 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100315
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100316 /* Verify we did not go off the rails */
317 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100318 return -EIO;
319
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100320 err = ovl_lookup_single(base, d, s, thislen,
Chengguang Xu1434a652020-05-26 09:35:57 +0800321 d->name.len - rem, next, &base,
322 drop_negative);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100323 dput(dentry);
324 if (err)
325 return err;
326 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100327 if (end)
328 break;
329
330 rem -= thislen + 1;
331
332 if (WARN_ON(rem >= d->name.len))
333 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100334 }
335 *ret = dentry;
336 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100337}
338
Amir Goldsteina9d01952017-04-30 14:46:31 +0300339
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200340int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200341 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300342{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300343 struct dentry *origin = NULL;
344 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300345
Amir Goldstein94375f92019-11-15 14:12:40 +0200346 for (i = 1; i < ofs->numlayer; i++) {
Amir Goldstein7e63c872019-11-14 22:28:41 +0200347 /*
348 * If lower fs uuid is not unique among lower fs we cannot match
349 * fh->uuid to layer.
350 */
Amir Goldstein94375f92019-11-15 14:12:40 +0200351 if (ofs->layers[i].fsid &&
352 ofs->layers[i].fs->bad_uuid)
Amir Goldstein7e63c872019-11-14 22:28:41 +0200353 continue;
354
Amir Goldstein94375f92019-11-15 14:12:40 +0200355 origin = ovl_decode_real_fh(fh, ofs->layers[i].mnt,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200356 connected);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300357 if (origin)
358 break;
359 }
360
361 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300362 return -ESTALE;
363 else if (IS_ERR(origin))
364 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300365
Amir Goldsteinf9418662018-01-19 21:33:44 +0200366 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300367 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
368 goto invalid;
369
Amir Goldstein415543d2017-06-21 15:28:42 +0300370 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500371 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300372 if (!*stackp) {
373 dput(origin);
374 return -ENOMEM;
375 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200376 **stackp = (struct ovl_path){
377 .dentry = origin,
Amir Goldstein94375f92019-11-15 14:12:40 +0200378 .layer = &ofs->layers[i]
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200379 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300380
381 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300382
383invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800384 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300385 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
386 d_inode(origin)->i_mode & S_IFMT);
387 dput(origin);
388 return -EIO;
389}
390
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200391static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300392 struct ovl_path **stackp, unsigned int *ctrp)
393{
Amir Goldstein05122442018-01-11 08:25:32 +0200394 struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300395 int err;
396
397 if (IS_ERR_OR_NULL(fh))
398 return PTR_ERR(fh);
399
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200400 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300401 kfree(fh);
402
403 if (err) {
404 if (err == -ESTALE)
405 return 0;
406 return err;
407 }
408
409 if (WARN_ON(*ctrp))
410 return -EIO;
411
412 *ctrp = 1;
413 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300414}
415
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100416/*
Amir Goldstein05122442018-01-11 08:25:32 +0200417 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300418 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
419 */
Amir Goldstein05122442018-01-11 08:25:32 +0200420static int ovl_verify_fh(struct dentry *dentry, const char *name,
421 const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300422{
Amir Goldstein05122442018-01-11 08:25:32 +0200423 struct ovl_fh *ofh = ovl_get_fh(dentry, name);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300424 int err = 0;
425
426 if (!ofh)
427 return -ENODATA;
428
429 if (IS_ERR(ofh))
430 return PTR_ERR(ofh);
431
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200432 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300433 err = -ESTALE;
434
435 kfree(ofh);
436 return err;
437}
438
439/*
Amir Goldstein05122442018-01-11 08:25:32 +0200440 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300441 *
Amir Goldstein05122442018-01-11 08:25:32 +0200442 * If @set is true and there is no stored file handle, encode @real and store
443 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300444 *
Amir Goldstein05122442018-01-11 08:25:32 +0200445 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300446 */
Amir Goldstein05122442018-01-11 08:25:32 +0200447int ovl_verify_set_fh(struct dentry *dentry, const char *name,
448 struct dentry *real, bool is_upper, bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300449{
450 struct inode *inode;
451 struct ovl_fh *fh;
452 int err;
453
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200454 fh = ovl_encode_real_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300455 err = PTR_ERR(fh);
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300456 if (IS_ERR(fh)) {
457 fh = NULL;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300458 goto fail;
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300459 }
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300460
Amir Goldstein05122442018-01-11 08:25:32 +0200461 err = ovl_verify_fh(dentry, name, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300462 if (set && err == -ENODATA)
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200463 err = ovl_do_setxattr(dentry, name, fh->buf, fh->fb.len, 0);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300464 if (err)
465 goto fail;
466
467out:
468 kfree(fh);
469 return err;
470
471fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200472 inode = d_inode(real);
lijiazi1bd0a3a2019-12-16 19:12:32 +0800473 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
Amir Goldstein05122442018-01-11 08:25:32 +0200474 is_upper ? "upper" : "origin", real,
475 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300476 goto out;
477}
478
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200479/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200480struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200481{
482 struct ovl_fh *fh;
483 struct dentry *upper;
484
485 if (!d_is_dir(index))
486 return dget(index);
487
488 fh = ovl_get_fh(index, OVL_XATTR_UPPER);
489 if (IS_ERR_OR_NULL(fh))
490 return ERR_CAST(fh);
491
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200492 upper = ovl_decode_real_fh(fh, ovl_upper_mnt(ofs), true);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200493 kfree(fh);
494
495 if (IS_ERR_OR_NULL(upper))
496 return upper ?: ERR_PTR(-ESTALE);
497
498 if (!d_is_dir(upper)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800499 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200500 index, upper);
501 dput(upper);
502 return ERR_PTR(-EIO);
503 }
504
505 return upper;
506}
507
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300508/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300509 * Verify that an index entry name matches the origin file handle stored in
510 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
511 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
512 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200513int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300514{
515 struct ovl_fh *fh = NULL;
516 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500517 struct ovl_path origin = { };
518 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200519 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300520 int err;
521
522 if (!d_inode(index))
523 return 0;
524
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300525 err = -EINVAL;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200526 if (index->d_name.len < sizeof(struct ovl_fb)*2)
Amir Goldstein415543d2017-06-21 15:28:42 +0300527 goto fail;
528
529 err = -ENOMEM;
530 len = index->d_name.len / 2;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200531 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300532 if (!fh)
533 goto fail;
534
535 err = -EINVAL;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200536 if (hex2bin(fh->buf, index->d_name.name, len))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300537 goto fail;
538
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200539 err = ovl_check_fb_len(&fh->fb, len);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300540 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300541 goto fail;
542
Amir Goldstein7db25d32018-01-11 11:03:13 +0200543 /*
544 * Whiteout index entries are used as an indication that an exported
545 * overlay file handle should be treated as stale (i.e. after unlink
546 * of the overlay inode). These entries contain no origin xattr.
547 */
548 if (ovl_is_whiteout(index))
549 goto out;
550
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200551 /*
552 * Verifying directory index entries are not stale is expensive, so
553 * only verify stale dir index if NFS export is enabled.
554 */
555 if (d_is_dir(index) && !ofs->config.nfs_export)
556 goto out;
557
558 /*
559 * Directory index entries should have 'upper' xattr pointing to the
560 * real upper dir. Non-dir index entries are hardlinks to the upper
561 * real inode. For non-dir index, we can read the copy up origin xattr
562 * directly from the index dentry, but for dir index we first need to
563 * decode the upper directory.
564 */
565 upper = ovl_index_upper(ofs, index);
566 if (IS_ERR_OR_NULL(upper)) {
567 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200568 /*
569 * Directory index entries with no 'upper' xattr need to be
570 * removed. When dir index entry has a stale 'upper' xattr,
571 * we assume that upper dir was removed and we treat the dir
572 * index as orphan entry that needs to be whited out.
573 */
574 if (err == -ESTALE)
575 goto orphan;
576 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200577 err = -ESTALE;
578 goto fail;
579 }
580
581 err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh);
582 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300583 if (err)
584 goto fail;
585
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200586 /* Check if non-dir index is orphan and don't warn before cleaning it */
587 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200588 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200589 if (err)
590 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300591
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200592 if (ovl_get_nlink(origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200593 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200594 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300595
Amir Goldstein415543d2017-06-21 15:28:42 +0300596out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200597 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300598 kfree(fh);
599 return err;
600
601fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800602 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
Amir Goldstein61b67472017-07-18 21:07:42 +0300603 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300604 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200605
606orphan:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800607 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
Amir Goldstein24f0b172018-01-11 15:33:51 +0200608 index, d_inode(index)->i_mode & S_IFMT,
609 d_inode(index)->i_nlink);
610 err = -ENOENT;
611 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300612}
613
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200614static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
615{
616 char *n, *s;
617
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200618 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200619 if (!n)
620 return -ENOMEM;
621
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200622 s = bin2hex(n, fh->buf, fh->fb.len);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200623 *name = (struct qstr) QSTR_INIT(n, s - n);
624
625 return 0;
626
627}
628
Amir Goldstein415543d2017-06-21 15:28:42 +0300629/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300630 * Lookup in indexdir for the index entry of a lower real inode or a copy up
631 * origin inode. The index entry name is the hex representation of the lower
632 * inode file handle.
633 *
634 * If the index dentry in negative, then either no lower aliases have been
635 * copied up yet, or aliases have been copied up in older kernels and are
636 * not indexed.
637 *
638 * If the index dentry for a copy up origin inode is positive, but points
639 * to an inode different than the upper inode, then either the upper inode
640 * has been copied up and not indexed or it was indexed, but since then
641 * index dir was cleared. Either way, that index cannot be used to indentify
642 * the overlay inode.
643 */
644int ovl_get_index_name(struct dentry *origin, struct qstr *name)
645{
Amir Goldstein359f3922017-06-21 15:28:41 +0300646 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200647 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300648
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200649 fh = ovl_encode_real_fh(origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300650 if (IS_ERR(fh))
651 return PTR_ERR(fh);
652
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200653 err = ovl_get_index_name_fh(fh, name);
654
Amir Goldstein359f3922017-06-21 15:28:41 +0300655 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300656 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200657}
Amir Goldstein359f3922017-06-21 15:28:41 +0300658
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200659/* Lookup index by file handle for NFS export */
660struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
661{
662 struct dentry *index;
663 struct qstr name;
664 int err;
665
666 err = ovl_get_index_name_fh(fh, &name);
667 if (err)
668 return ERR_PTR(err);
669
Al Viro6c2d47982019-10-31 01:21:58 -0400670 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200671 kfree(name.name);
672 if (IS_ERR(index)) {
673 if (PTR_ERR(index) == -ENOENT)
674 index = NULL;
675 return index;
676 }
677
Al Viro6c2d47982019-10-31 01:21:58 -0400678 if (ovl_is_whiteout(index))
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200679 err = -ESTALE;
680 else if (ovl_dentry_weird(index))
681 err = -EIO;
682 else
683 return index;
684
685 dput(index);
686 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300687}
688
Amir Goldstein06170152018-01-17 14:40:27 +0200689struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
690 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300691{
Amir Goldstein359f3922017-06-21 15:28:41 +0300692 struct dentry *index;
693 struct inode *inode;
694 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200695 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300696 int err;
697
698 err = ovl_get_index_name(origin, &name);
699 if (err)
700 return ERR_PTR(err);
701
Al Viro6c2d47982019-10-31 01:21:58 -0400702 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
Amir Goldstein359f3922017-06-21 15:28:41 +0300703 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300704 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300705 if (err == -ENOENT) {
706 index = NULL;
707 goto out;
708 }
lijiazi1bd0a3a2019-12-16 19:12:32 +0800709 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
Amir Goldstein359f3922017-06-21 15:28:41 +0300710 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
711 d_inode(origin)->i_ino, name.len, name.name,
712 err);
713 goto out;
714 }
715
Amir Goldstein0e082552017-07-18 21:07:43 +0300716 inode = d_inode(index);
Al Viro6c2d47982019-10-31 01:21:58 -0400717 if (ovl_is_whiteout(index) && !verify) {
Amir Goldstein06170152018-01-17 14:40:27 +0200718 /*
719 * When index lookup is called with !verify for decoding an
720 * overlay file handle, a whiteout index implies that decode
721 * should treat file handle as stale and no need to print a
722 * warning about it.
723 */
724 dput(index);
725 index = ERR_PTR(-ESTALE);
726 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300727 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
728 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
729 /*
730 * Index should always be of the same file type as origin
731 * except for the case of a whiteout index. A whiteout
732 * index should only exist if all lower aliases have been
733 * unlinked, which means that finding a lower origin on lookup
734 * whose index is a whiteout should be treated as an error.
735 */
lijiazi1bd0a3a2019-12-16 19:12:32 +0800736 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
Amir Goldstein0e082552017-07-18 21:07:43 +0300737 index, d_inode(index)->i_mode & S_IFMT,
738 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300739 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200740 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200741 if (!upper) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800742 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200743 origin, index);
744 goto fail;
745 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300746
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200747 /* Verify that dir index 'upper' xattr points to upper dir */
748 err = ovl_verify_upper(index, upper, false);
749 if (err) {
750 if (err == -ESTALE) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800751 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200752 upper, origin, index);
753 }
754 goto fail;
755 }
756 } else if (upper && d_inode(upper) != inode) {
757 goto out_dput;
758 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300759out:
760 kfree(name.name);
761 return index;
762
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300763out_dput:
764 dput(index);
765 index = NULL;
766 goto out;
767
Amir Goldstein359f3922017-06-21 15:28:41 +0300768fail:
769 dput(index);
770 index = ERR_PTR(-EIO);
771 goto out;
772}
773
774/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100775 * Returns next layer in stack starting from top.
776 * Returns -1 if this is the last layer.
777 */
778int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
779{
780 struct ovl_entry *oe = dentry->d_fsdata;
781
782 BUG_ON(idx < 0);
783 if (idx == 0) {
784 ovl_path_upper(dentry, path);
785 if (path->dentry)
786 return oe->numlower ? 1 : -1;
787 idx++;
788 }
789 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500790 path->dentry = oe->lowerstack[idx - 1].dentry;
791 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100792
793 return (idx < oe->numlower) ? idx + 1 : -1;
794}
795
Amir Goldstein9678e632018-01-03 19:34:45 +0200796/* Fix missing 'origin' xattr */
797static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower,
798 struct dentry *upper)
799{
800 int err;
801
802 if (ovl_check_origin_xattr(upper))
803 return 0;
804
805 err = ovl_want_write(dentry);
806 if (err)
807 return err;
808
809 err = ovl_set_origin(dentry, lower, upper);
810 if (!err)
811 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
812
813 ovl_drop_write(dentry);
814 return err;
815}
816
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100817struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
818 unsigned int flags)
819{
820 struct ovl_entry *oe;
821 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100822 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100823 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300824 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400825 struct ovl_path *stack = NULL, *origin_path = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100826 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200827 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300828 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100829 unsigned int ctr = 0;
830 struct inode *inode = NULL;
831 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100832 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100833 struct dentry *this;
834 unsigned int i;
835 int err;
Vivek Goyal6815f472020-06-01 11:56:51 -0400836 bool uppermetacopy = false;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100837 struct ovl_lookup_data d = {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300838 .sb = dentry->d_sb,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100839 .name = dentry->d_name,
840 .is_dir = false,
841 .opaque = false,
842 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500843 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100844 .redirect = NULL,
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400845 .metacopy = false,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100846 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100847
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100848 if (dentry->d_name.len > ofs->namelen)
849 return ERR_PTR(-ENAMETOOLONG);
850
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100851 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200852 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100853 if (upperdir) {
Chengguang Xu1434a652020-05-26 09:35:57 +0800854 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100855 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100856 goto out;
857
Miklos Szeredibccece12020-03-17 15:04:22 +0100858 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100859 dput(upperdentry);
860 err = -EREMOTE;
861 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100862 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300863 if (upperdentry && !d.is_dir) {
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400864 unsigned int origin_ctr = 0;
865
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300866 /*
867 * Lookup copy up origin by decoding origin file handle.
868 * We may get a disconnected dentry, which is fine,
869 * because we only need to hold the origin inode in
870 * cache and use its inode number. We may even get a
871 * connected dentry, that is not under any of the lower
872 * layers root. That is also fine for using it's inode
873 * number - it's the same as if we held a reference
874 * to a dentry in lower layer that was moved under us.
875 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400876 err = ovl_check_origin(ofs, upperdentry, &origin_path,
877 &origin_ctr);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300878 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400879 goto out_put_upper;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400880
881 if (d.metacopy)
Vivek Goyal6815f472020-06-01 11:56:51 -0400882 uppermetacopy = true;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300883 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100884
885 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300886 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100887 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
888 if (!upperredirect)
889 goto out_put_upper;
890 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300891 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100892 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100893 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100894 }
895
Miklos Szeredie28edc42016-12-16 11:02:56 +0100896 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100897 err = -ENOMEM;
Amir Goldstein94375f92019-11-15 14:12:40 +0200898 stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700899 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100900 if (!stack)
901 goto out_put_upper;
902 }
903
Miklos Szeredie28edc42016-12-16 11:02:56 +0100904 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500905 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100906
Vivek Goyal452061f2018-03-09 15:44:41 -0500907 if (!ofs->config.redirect_follow)
908 d.last = i == poe->numlower - 1;
909 else
910 d.last = lower.layer->idx == roe->numlower;
911
Chengguang Xu1434a652020-05-26 09:35:57 +0800912 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100913 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100914 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100915
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100916 if (!this)
917 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100918
Vivek Goyal6815f472020-06-01 11:56:51 -0400919 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
920 err = -EPERM;
921 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
922 goto out_put;
923 }
924
925 /*
Amir Goldstein9678e632018-01-03 19:34:45 +0200926 * If no origin fh is stored in upper of a merge dir, store fh
927 * of lower dir and set upper parent "impure".
928 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400929 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
Amir Goldstein9678e632018-01-03 19:34:45 +0200930 err = ovl_fix_origin(dentry, this, upperdentry);
931 if (err) {
932 dput(this);
933 goto out_put;
934 }
935 }
936
Amir Goldstein37b129162018-01-10 22:29:38 +0200937 /*
938 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200939 * lower dir that does not match a stored origin xattr. In any
940 * case, only verified origin is used for index lookup.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400941 *
942 * For non-dir dentry, if index=on, then ensure origin
943 * matches the dentry found using path based lookup,
944 * otherwise error out.
Amir Goldstein37b129162018-01-10 22:29:38 +0200945 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400946 if (upperdentry && !ctr &&
947 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
948 (!d.is_dir && ofs->config.index && origin_path))) {
Amir Goldstein37b129162018-01-10 22:29:38 +0200949 err = ovl_verify_origin(upperdentry, this, false);
950 if (err) {
951 dput(this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400952 if (d.is_dir)
953 break;
954 goto out_put;
Amir Goldstein37b129162018-01-10 22:29:38 +0200955 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200956 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200957 }
958
Vivek Goyal21d8d662020-06-02 11:23:38 -0400959 if (d.metacopy && ctr) {
960 /*
961 * Do not store intermediate metacopy dentries in
962 * lower chain, except top most lower metacopy dentry.
963 * Continue the loop so that if there is an absolute
964 * redirect on this dentry, poe can be reset to roe.
965 */
966 dput(this);
967 this = NULL;
968 } else {
969 stack[ctr].dentry = this;
970 stack[ctr].layer = lower.layer;
971 ctr++;
972 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100973
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100974 /*
975 * Following redirects can have security consequences: it's like
976 * a symlink into the lower layer without the permission checks.
977 * This is only a problem if the upper layer is untrusted (e.g
978 * comes from an USB drive). This can allow a non-readable file
979 * or directory to become readable.
980 *
981 * Only following redirects when redirects are enabled disables
982 * this attack vector when not necessary.
983 */
984 err = -EPERM;
985 if (d.redirect && !ofs->config.redirect_follow) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800986 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
Amir Goldsteinf8167812017-12-18 14:25:56 +0200987 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100988 goto out_put;
989 }
990
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500991 if (d.stop)
992 break;
993
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300994 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
995 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100996 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +0200997 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100998 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100999 }
1000
Vivek Goyal6815f472020-06-01 11:56:51 -04001001 /*
1002 * For regular non-metacopy upper dentries, there is no lower
1003 * path based lookup, hence ctr will be zero. If a dentry is found
1004 * using ORIGIN xattr on upper, install it in stack.
1005 *
1006 * For metacopy dentry, path based lookup will find lower dentries.
1007 * Just make sure a corresponding data dentry has been found.
1008 */
1009 if (d.metacopy || (uppermetacopy && !ctr)) {
1010 err = -EIO;
1011 goto out_put;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001012 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1013 if (WARN_ON(stack != NULL)) {
1014 err = -EIO;
1015 goto out_put;
1016 }
1017 stack = origin_path;
1018 ctr = 1;
Vivek Goyal59fb2012020-06-01 11:56:50 -04001019 origin = origin_path->dentry;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001020 origin_path = NULL;
1021 }
1022
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001023 /*
Vivek Goyal59fb2012020-06-01 11:56:50 -04001024 * Always lookup index if there is no-upperdentry.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001025 *
Vivek Goyal59fb2012020-06-01 11:56:50 -04001026 * For the case of upperdentry, we have set origin by now if it
1027 * needed to be set. There are basically three cases.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001028 *
Vivek Goyal59fb2012020-06-01 11:56:50 -04001029 * For directories, lookup index by lower inode and verify it matches
1030 * upper inode. We only trust dir index if we verified that lower dir
1031 * matches origin, otherwise dir index entries may be inconsistent
1032 * and we ignore them.
1033 *
1034 * For regular upper, we already set origin if upper had ORIGIN
1035 * xattr. There is no verification though as there is no path
1036 * based dentry lookup in lower in this case.
1037 *
1038 * For metacopy upper, we set a verified origin already if index
1039 * is enabled and if upper had an ORIGIN xattr.
1040 *
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001041 */
Vivek Goyal59fb2012020-06-01 11:56:50 -04001042 if (!upperdentry && ctr)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001043 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +03001044
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001045 if (origin && ovl_indexdir(dentry->d_sb) &&
1046 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +02001047 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +03001048 if (IS_ERR(index)) {
1049 err = PTR_ERR(index);
1050 index = NULL;
1051 goto out_put;
1052 }
1053 }
1054
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001055 oe = ovl_alloc_entry(ctr);
1056 err = -ENOMEM;
1057 if (!oe)
1058 goto out_put;
1059
Chandan Rajendrab9343632017-07-24 01:57:54 -05001060 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001061 dentry->d_fsdata = oe;
1062
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001063 if (upperopaque)
1064 ovl_dentry_set_opaque(dentry);
1065
Miklos Szeredi55acc662017-07-04 22:03:18 +02001066 if (upperdentry)
1067 ovl_dentry_set_upper_alias(dentry);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001068 else if (index) {
Amir Goldstein359f3922017-06-21 15:28:41 +03001069 upperdentry = dget(index);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001070 upperredirect = ovl_get_redirect_xattr(upperdentry, 0);
1071 if (IS_ERR(upperredirect)) {
1072 err = PTR_ERR(upperredirect);
1073 upperredirect = NULL;
1074 goto out_free_oe;
1075 }
1076 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001077
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001078 if (upperdentry || ctr) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001079 struct ovl_inode_params oip = {
1080 .upperdentry = upperdentry,
1081 .lowerpath = stack,
1082 .index = index,
1083 .numlower = ctr,
Vivek Goyal9cec54c2018-05-11 11:49:27 -04001084 .redirect = upperredirect,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001085 .lowerdata = (ctr > 1 && !d.is_dir) ?
1086 stack[ctr - 1].dentry : NULL,
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001087 };
1088
1089 inode = ovl_get_inode(dentry->d_sb, &oip);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +02001090 err = PTR_ERR(inode);
1091 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001092 goto out_free_oe;
Vivek Goyal28166ab2020-06-01 11:56:52 -04001093 if (upperdentry && !uppermetacopy)
1094 ovl_set_flag(OVL_UPPERDATA, inode);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001095 }
1096
Miklos Szeredif4288842020-03-17 15:04:22 +01001097 ovl_dentry_update_reval(dentry, upperdentry,
1098 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
1099
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001100 revert_creds(old_cred);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001101 if (origin_path) {
1102 dput(origin_path->dentry);
1103 kfree(origin_path);
1104 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001105 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001106 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001107 kfree(d.redirect);
Amir Goldstein829c28b2017-09-29 21:43:07 +03001108 return d_splice_alias(inode, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001109
1110out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001111 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001112 kfree(oe);
1113out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +03001114 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001115 for (i = 0; i < ctr; i++)
1116 dput(stack[i].dentry);
1117 kfree(stack);
1118out_put_upper:
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001119 if (origin_path) {
1120 dput(origin_path->dentry);
1121 kfree(origin_path);
1122 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001123 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001124 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001125out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001126 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001127 revert_creds(old_cred);
1128 return ERR_PTR(err);
1129}
1130
1131bool ovl_lower_positive(struct dentry *dentry)
1132{
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001133 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1134 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001135 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001136 unsigned int i;
1137 bool positive = false;
1138 bool done = false;
1139
1140 /*
1141 * If dentry is negative, then lower is positive iff this is a
1142 * whiteout.
1143 */
1144 if (!dentry->d_inode)
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001145 return ovl_dentry_is_opaque(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001146
1147 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001148 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001149 return true;
1150
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001151 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001152 /* Positive upper -> have to look up lower to see whether it exists */
1153 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1154 struct dentry *this;
1155 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1156
Al Viro6c2d47982019-10-31 01:21:58 -04001157 this = lookup_positive_unlocked(name->name, lowerdir,
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001158 name->len);
1159 if (IS_ERR(this)) {
1160 switch (PTR_ERR(this)) {
1161 case -ENOENT:
1162 case -ENAMETOOLONG:
1163 break;
1164
1165 default:
1166 /*
1167 * Assume something is there, we just couldn't
1168 * access it.
1169 */
1170 positive = true;
1171 break;
1172 }
1173 } else {
Al Viro6c2d47982019-10-31 01:21:58 -04001174 positive = !ovl_is_whiteout(this);
1175 done = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001176 dput(this);
1177 }
1178 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001179 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001180
1181 return positive;
1182}