blob: 1a9b515fc45d468bcd435931a99444aed9eca634 [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 Szeredi610afc02020-09-02 10:58:49 +020033 struct ovl_fs *ofs = OVL_FS(d->sb);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010034
Miklos Szeredi610afc02020-09-02 10:58:49 +020035 buf = ovl_get_redirect_xattr(ofs, dentry, prelen + strlen(post));
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040036 if (IS_ERR_OR_NULL(buf))
37 return PTR_ERR(buf);
Miklos Szeredi02b69b22016-12-16 11:02:56 +010038
Miklos Szeredi02b69b22016-12-16 11:02:56 +010039 if (buf[0] == '/') {
Amir Goldstein3ec9b3f2018-03-12 10:30:41 -040040 /*
41 * One of the ancestor path elements in an absolute path
42 * lookup in ovl_lookup_layer() could have been opaque and
43 * that will stop further lookup in lower layers (d->stop=true)
44 * But we have found an absolute redirect in decendant path
45 * element and that should force continue lookup in lower
46 * layers (reset d->stop).
47 */
48 d->stop = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010049 } else {
Vivek Goyal0a2d0d32018-05-11 11:49:32 -040050 res = strlen(buf) + 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010051 memmove(buf + prelen, buf, res);
52 memcpy(buf, d->name.name, prelen);
53 }
54
55 strcat(buf, post);
56 kfree(d->redirect);
57 d->redirect = buf;
58 d->name.name = d->redirect;
59 d->name.len = strlen(d->redirect);
60
61 return 0;
Miklos Szeredi02b69b22016-12-16 11:02:56 +010062}
63
Amir Goldsteina9d01952017-04-30 14:46:31 +030064static int ovl_acceptable(void *ctx, struct dentry *dentry)
65{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +020066 /*
67 * A non-dir origin may be disconnected, which is fine, because
68 * we only need it for its unique inode number.
69 */
70 if (!d_is_dir(dentry))
71 return 1;
72
73 /* Don't decode a deleted empty directory */
74 if (d_unhashed(dentry))
75 return 0;
76
77 /* Check if directory belongs to the layer we are decoding from */
78 return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root);
Amir Goldsteina9d01952017-04-30 14:46:31 +030079}
80
Amir Goldstein2e1a53282017-10-24 15:12:15 +030081/*
82 * Check validity of an overlay file handle buffer.
83 *
84 * Return 0 for a valid file handle.
85 * Return -ENODATA for "origin unknown".
86 * Return <0 for an invalid file handle.
87 */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020088int ovl_check_fb_len(struct ovl_fb *fb, int fb_len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030089{
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020090 if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030091 return -EINVAL;
92
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020093 if (fb->magic != OVL_FH_MAGIC)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030094 return -EINVAL;
95
96 /* Treat larger version and unknown flags as "origin unknown" */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020097 if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL)
Amir Goldstein2e1a53282017-10-24 15:12:15 +030098 return -ENODATA;
99
100 /* Treat endianness mismatch as "origin unknown" */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200101 if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) &&
102 (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300103 return -ENODATA;
104
105 return 0;
106}
107
Miklos Szeredi610afc02020-09-02 10:58:49 +0200108static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200109 enum ovl_xattr ox)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300110{
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300111 int res, err;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300112 struct ovl_fh *fh = NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300113
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200114 res = ovl_do_getxattr(ofs, dentry, ox, NULL, 0);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300115 if (res < 0) {
116 if (res == -ENODATA || res == -EOPNOTSUPP)
117 return NULL;
118 goto fail;
119 }
120 /* Zero size value means "copied up but origin unknown" */
121 if (res == 0)
122 return NULL;
123
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200124 fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300125 if (!fh)
126 return ERR_PTR(-ENOMEM);
127
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200128 res = ovl_do_getxattr(ofs, dentry, ox, fh->buf, res);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300129 if (res < 0)
130 goto fail;
131
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200132 err = ovl_check_fb_len(&fh->fb, res);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300133 if (err < 0) {
134 if (err == -ENODATA)
135 goto out;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300136 goto invalid;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300137 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300138
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300139 return fh;
140
141out:
142 kfree(fh);
143 return NULL;
144
145fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800146 pr_warn_ratelimited("failed to get origin (%i)\n", res);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300147 goto out;
148invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800149 pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300150 goto out;
151}
152
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300153struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
154 struct vfsmount *mnt, bool connected)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300155{
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200156 struct dentry *real;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300157 int bytes;
158
Miklos Szeredic846af02020-12-14 15:26:14 +0100159 if (!capable(CAP_DAC_READ_SEARCH))
160 return NULL;
161
Amir Goldsteina9d01952017-04-30 14:46:31 +0300162 /*
163 * Make sure that the stored uuid matches the uuid of the lower
164 * layer where file handle will be decoded.
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300165 * In case of uuid=off option just make sure that stored uuid is null.
Amir Goldsteina9d01952017-04-30 14:46:31 +0300166 */
Pavel Tikhomirov5830fb62020-10-13 17:59:54 +0300167 if (ofs->config.uuid ? !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) :
168 !uuid_is_null(&fh->fb.uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300169 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300170
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200171 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
172 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
173 bytes >> 2, (int)fh->fb.type,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200174 connected ? ovl_acceptable : NULL, mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200175 if (IS_ERR(real)) {
176 /*
177 * Treat stale file handle to lower file as "origin unknown".
178 * upper file handle could become stale when upper file is
179 * unlinked and this information is needed to handle stale
180 * index entries correctly.
181 */
182 if (real == ERR_PTR(-ESTALE) &&
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200183 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200184 real = NULL;
185 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300186 }
187
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200188 if (ovl_dentry_weird(real)) {
189 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300190 return NULL;
191 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300192
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200193 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300194}
195
Miklos Szeredi610afc02020-09-02 10:58:49 +0200196static bool ovl_is_opaquedir(struct super_block *sb, struct dentry *dentry)
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300197{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200198 return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_OPAQUE);
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300199}
200
Chengguang Xu1434a652020-05-26 09:35:57 +0800201static struct dentry *ovl_lookup_positive_unlocked(const char *name,
202 struct dentry *base, int len,
203 bool drop_negative)
204{
205 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
206
207 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
208 if (drop_negative && ret->d_lockref.count == 1) {
209 spin_lock(&ret->d_lock);
210 /* Recheck condition under lock */
211 if (d_is_negative(ret) && ret->d_lockref.count == 1)
212 __d_drop(ret);
213 spin_unlock(&ret->d_lock);
214 }
215 dput(ret);
216 ret = ERR_PTR(-ENOENT);
217 }
218 return ret;
219}
220
Miklos Szeredie28edc42016-12-16 11:02:56 +0100221static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
222 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100223 size_t prelen, const char *post,
Chengguang Xu1434a652020-05-26 09:35:57 +0800224 struct dentry **ret, bool drop_negative)
Miklos Szeredie28edc42016-12-16 11:02:56 +0100225{
226 struct dentry *this;
227 int err;
Vivek Goyal102b0d12018-03-09 15:44:43 -0500228 bool last_element = !post[0];
Miklos Szeredie28edc42016-12-16 11:02:56 +0100229
Chengguang Xu1434a652020-05-26 09:35:57 +0800230 this = ovl_lookup_positive_unlocked(name, base, namelen, drop_negative);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100231 if (IS_ERR(this)) {
232 err = PTR_ERR(this);
233 this = NULL;
234 if (err == -ENOENT || err == -ENAMETOOLONG)
235 goto out;
236 goto out_err;
237 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100238
239 if (ovl_dentry_weird(this)) {
240 /* Don't support traversing automounts and other weirdness */
241 err = -EREMOTE;
242 goto out_err;
243 }
244 if (ovl_is_whiteout(this)) {
245 d->stop = d->opaque = true;
246 goto put_and_out;
247 }
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400248 /*
249 * This dentry should be a regular file if previous layer lookup
250 * found a metacopy dentry.
251 */
252 if (last_element && d->metacopy && !d_is_reg(this)) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100253 d->stop = true;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400254 goto put_and_out;
255 }
256 if (!d_can_lookup(this)) {
257 if (d->is_dir || !last_element) {
258 d->stop = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100259 goto put_and_out;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400260 }
Miklos Szeredi610afc02020-09-02 10:58:49 +0200261 err = ovl_check_metacopy_xattr(OVL_FS(d->sb), this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400262 if (err < 0)
263 goto out_err;
Miklos Szeredi3a291772018-04-12 12:04:49 +0200264
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400265 d->metacopy = err;
266 d->stop = !d->metacopy;
Vivek Goyalb8a88242018-05-11 11:49:31 -0400267 if (!d->metacopy || d->last)
268 goto out;
Vivek Goyal0618a812018-05-11 11:49:31 -0400269 } else {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300270 if (ovl_lookup_trap_inode(d->sb, this)) {
271 /* Caught in a trap of overlapping layers */
272 err = -ELOOP;
273 goto out_err;
274 }
275
Vivek Goyal102b0d12018-03-09 15:44:43 -0500276 if (last_element)
Vivek Goyal0618a812018-05-11 11:49:31 -0400277 d->is_dir = true;
278 if (d->last)
279 goto out;
280
Miklos Szeredi610afc02020-09-02 10:58:49 +0200281 if (ovl_is_opaquedir(d->sb, this)) {
Vivek Goyal0618a812018-05-11 11:49:31 -0400282 d->stop = true;
283 if (last_element)
284 d->opaque = true;
285 goto out;
286 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100287 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100288 err = ovl_check_redirect(this, d, prelen, post);
289 if (err)
290 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100291out:
292 *ret = this;
293 return 0;
294
295put_and_out:
296 dput(this);
297 this = NULL;
298 goto out;
299
300out_err:
301 dput(this);
302 return err;
303}
304
305static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
Chengguang Xu1434a652020-05-26 09:35:57 +0800306 struct dentry **ret, bool drop_negative)
Miklos Szeredie28edc42016-12-16 11:02:56 +0100307{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100308 /* Counting down from the end, since the prefix can change */
309 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100310 struct dentry *dentry = NULL;
311 int err;
312
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100313 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100314 return ovl_lookup_single(base, d, d->name.name, d->name.len,
Chengguang Xu1434a652020-05-26 09:35:57 +0800315 0, "", ret, drop_negative);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100316
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100317 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
318 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100319 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100320 size_t thislen = next - s;
321 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100322
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100323 /* Verify we did not go off the rails */
324 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100325 return -EIO;
326
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100327 err = ovl_lookup_single(base, d, s, thislen,
Chengguang Xu1434a652020-05-26 09:35:57 +0800328 d->name.len - rem, next, &base,
329 drop_negative);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100330 dput(dentry);
331 if (err)
332 return err;
333 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100334 if (end)
335 break;
336
337 rem -= thislen + 1;
338
339 if (WARN_ON(rem >= d->name.len))
340 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100341 }
342 *ret = dentry;
343 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100344}
345
Amir Goldsteina9d01952017-04-30 14:46:31 +0300346
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200347int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200348 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300349{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300350 struct dentry *origin = NULL;
351 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300352
Amir Goldstein94375f92019-11-15 14:12:40 +0200353 for (i = 1; i < ofs->numlayer; i++) {
Amir Goldstein7e63c872019-11-14 22:28:41 +0200354 /*
355 * If lower fs uuid is not unique among lower fs we cannot match
356 * fh->uuid to layer.
357 */
Amir Goldstein94375f92019-11-15 14:12:40 +0200358 if (ofs->layers[i].fsid &&
359 ofs->layers[i].fs->bad_uuid)
Amir Goldstein7e63c872019-11-14 22:28:41 +0200360 continue;
361
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300362 origin = ovl_decode_real_fh(ofs, fh, ofs->layers[i].mnt,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200363 connected);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300364 if (origin)
365 break;
366 }
367
368 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300369 return -ESTALE;
370 else if (IS_ERR(origin))
371 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300372
Amir Goldsteinf9418662018-01-19 21:33:44 +0200373 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Al Viro6e3e2c42021-03-01 20:37:10 -0500374 inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300375 goto invalid;
376
Amir Goldstein415543d2017-06-21 15:28:42 +0300377 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500378 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300379 if (!*stackp) {
380 dput(origin);
381 return -ENOMEM;
382 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200383 **stackp = (struct ovl_path){
384 .dentry = origin,
Amir Goldstein94375f92019-11-15 14:12:40 +0200385 .layer = &ofs->layers[i]
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200386 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300387
388 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300389
390invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800391 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300392 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
393 d_inode(origin)->i_mode & S_IFMT);
394 dput(origin);
Amir Goldsteinffb24e32021-04-27 13:28:26 +0300395 return -ESTALE;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300396}
397
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200398static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
youngjund78a0dc2020-06-21 05:50:01 -0700399 struct ovl_path **stackp)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300400{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200401 struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300402 int err;
403
404 if (IS_ERR_OR_NULL(fh))
405 return PTR_ERR(fh);
406
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200407 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300408 kfree(fh);
409
410 if (err) {
411 if (err == -ESTALE)
412 return 0;
413 return err;
414 }
415
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300416 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300417}
418
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100419/*
Amir Goldstein05122442018-01-11 08:25:32 +0200420 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300421 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
422 */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200423static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200424 enum ovl_xattr ox, const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300425{
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200426 struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300427 int err = 0;
428
429 if (!ofh)
430 return -ENODATA;
431
432 if (IS_ERR(ofh))
433 return PTR_ERR(ofh);
434
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200435 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300436 err = -ESTALE;
437
438 kfree(ofh);
439 return err;
440}
441
442/*
Amir Goldstein05122442018-01-11 08:25:32 +0200443 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300444 *
Amir Goldstein05122442018-01-11 08:25:32 +0200445 * If @set is true and there is no stored file handle, encode @real and store
446 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300447 *
Amir Goldstein05122442018-01-11 08:25:32 +0200448 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300449 */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200450int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200451 enum ovl_xattr ox, struct dentry *real, bool is_upper,
Miklos Szeredi610afc02020-09-02 10:58:49 +0200452 bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300453{
454 struct inode *inode;
455 struct ovl_fh *fh;
456 int err;
457
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300458 fh = ovl_encode_real_fh(ofs, real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300459 err = PTR_ERR(fh);
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300460 if (IS_ERR(fh)) {
461 fh = NULL;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300462 goto fail;
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300463 }
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300464
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200465 err = ovl_verify_fh(ofs, dentry, ox, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300466 if (set && err == -ENODATA)
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200467 err = ovl_do_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300468 if (err)
469 goto fail;
470
471out:
472 kfree(fh);
473 return err;
474
475fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200476 inode = d_inode(real);
lijiazi1bd0a3a2019-12-16 19:12:32 +0800477 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
Amir Goldstein05122442018-01-11 08:25:32 +0200478 is_upper ? "upper" : "origin", real,
479 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300480 goto out;
481}
482
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200483/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200484struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200485{
486 struct ovl_fh *fh;
487 struct dentry *upper;
488
489 if (!d_is_dir(index))
490 return dget(index);
491
Miklos Szeredi610afc02020-09-02 10:58:49 +0200492 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200493 if (IS_ERR_OR_NULL(fh))
494 return ERR_CAST(fh);
495
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300496 upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), true);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200497 kfree(fh);
498
499 if (IS_ERR_OR_NULL(upper))
500 return upper ?: ERR_PTR(-ESTALE);
501
502 if (!d_is_dir(upper)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800503 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200504 index, upper);
505 dput(upper);
506 return ERR_PTR(-EIO);
507 }
508
509 return upper;
510}
511
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300512/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300513 * Verify that an index entry name matches the origin file handle stored in
514 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
515 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
516 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200517int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300518{
519 struct ovl_fh *fh = NULL;
520 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500521 struct ovl_path origin = { };
522 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200523 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300524 int err;
525
526 if (!d_inode(index))
527 return 0;
528
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300529 err = -EINVAL;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200530 if (index->d_name.len < sizeof(struct ovl_fb)*2)
Amir Goldstein415543d2017-06-21 15:28:42 +0300531 goto fail;
532
533 err = -ENOMEM;
534 len = index->d_name.len / 2;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200535 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300536 if (!fh)
537 goto fail;
538
539 err = -EINVAL;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200540 if (hex2bin(fh->buf, index->d_name.name, len))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300541 goto fail;
542
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200543 err = ovl_check_fb_len(&fh->fb, len);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300544 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300545 goto fail;
546
Amir Goldstein7db25d32018-01-11 11:03:13 +0200547 /*
548 * Whiteout index entries are used as an indication that an exported
549 * overlay file handle should be treated as stale (i.e. after unlink
550 * of the overlay inode). These entries contain no origin xattr.
551 */
552 if (ovl_is_whiteout(index))
553 goto out;
554
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200555 /*
556 * Verifying directory index entries are not stale is expensive, so
557 * only verify stale dir index if NFS export is enabled.
558 */
559 if (d_is_dir(index) && !ofs->config.nfs_export)
560 goto out;
561
562 /*
563 * Directory index entries should have 'upper' xattr pointing to the
564 * real upper dir. Non-dir index entries are hardlinks to the upper
565 * real inode. For non-dir index, we can read the copy up origin xattr
566 * directly from the index dentry, but for dir index we first need to
567 * decode the upper directory.
568 */
569 upper = ovl_index_upper(ofs, index);
570 if (IS_ERR_OR_NULL(upper)) {
571 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200572 /*
573 * Directory index entries with no 'upper' xattr need to be
574 * removed. When dir index entry has a stale 'upper' xattr,
575 * we assume that upper dir was removed and we treat the dir
576 * index as orphan entry that needs to be whited out.
577 */
578 if (err == -ESTALE)
579 goto orphan;
580 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200581 err = -ESTALE;
582 goto fail;
583 }
584
Miklos Szeredi610afc02020-09-02 10:58:49 +0200585 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200586 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300587 if (err)
588 goto fail;
589
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200590 /* Check if non-dir index is orphan and don't warn before cleaning it */
591 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200592 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200593 if (err)
594 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300595
Miklos Szeredi610afc02020-09-02 10:58:49 +0200596 if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200597 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200598 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300599
Amir Goldstein415543d2017-06-21 15:28:42 +0300600out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200601 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300602 kfree(fh);
603 return err;
604
605fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800606 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
Amir Goldstein61b67472017-07-18 21:07:42 +0300607 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300608 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200609
610orphan:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800611 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
Amir Goldstein24f0b172018-01-11 15:33:51 +0200612 index, d_inode(index)->i_mode & S_IFMT,
613 d_inode(index)->i_nlink);
614 err = -ENOENT;
615 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300616}
617
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200618static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
619{
620 char *n, *s;
621
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200622 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200623 if (!n)
624 return -ENOMEM;
625
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200626 s = bin2hex(n, fh->buf, fh->fb.len);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200627 *name = (struct qstr) QSTR_INIT(n, s - n);
628
629 return 0;
630
631}
632
Amir Goldstein415543d2017-06-21 15:28:42 +0300633/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300634 * Lookup in indexdir for the index entry of a lower real inode or a copy up
635 * origin inode. The index entry name is the hex representation of the lower
636 * inode file handle.
637 *
638 * If the index dentry in negative, then either no lower aliases have been
639 * copied up yet, or aliases have been copied up in older kernels and are
640 * not indexed.
641 *
642 * If the index dentry for a copy up origin inode is positive, but points
643 * to an inode different than the upper inode, then either the upper inode
644 * has been copied up and not indexed or it was indexed, but since then
645 * index dir was cleared. Either way, that index cannot be used to indentify
646 * the overlay inode.
647 */
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300648int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
649 struct qstr *name)
Amir Goldstein359f3922017-06-21 15:28:41 +0300650{
Amir Goldstein359f3922017-06-21 15:28:41 +0300651 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200652 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300653
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300654 fh = ovl_encode_real_fh(ofs, origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300655 if (IS_ERR(fh))
656 return PTR_ERR(fh);
657
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200658 err = ovl_get_index_name_fh(fh, name);
659
Amir Goldstein359f3922017-06-21 15:28:41 +0300660 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300661 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200662}
Amir Goldstein359f3922017-06-21 15:28:41 +0300663
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200664/* Lookup index by file handle for NFS export */
665struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
666{
667 struct dentry *index;
668 struct qstr name;
669 int err;
670
671 err = ovl_get_index_name_fh(fh, &name);
672 if (err)
673 return ERR_PTR(err);
674
Al Viro6c2d47982019-10-31 01:21:58 -0400675 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200676 kfree(name.name);
677 if (IS_ERR(index)) {
678 if (PTR_ERR(index) == -ENOENT)
679 index = NULL;
680 return index;
681 }
682
Al Viro6c2d47982019-10-31 01:21:58 -0400683 if (ovl_is_whiteout(index))
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200684 err = -ESTALE;
685 else if (ovl_dentry_weird(index))
686 err = -EIO;
687 else
688 return index;
689
690 dput(index);
691 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300692}
693
Amir Goldstein06170152018-01-17 14:40:27 +0200694struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
695 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300696{
Amir Goldstein359f3922017-06-21 15:28:41 +0300697 struct dentry *index;
698 struct inode *inode;
699 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200700 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300701 int err;
702
Pavel Tikhomirov1cdb0cb2020-10-13 17:59:53 +0300703 err = ovl_get_index_name(ofs, origin, &name);
Amir Goldstein359f3922017-06-21 15:28:41 +0300704 if (err)
705 return ERR_PTR(err);
706
Al Viro6c2d47982019-10-31 01:21:58 -0400707 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
Amir Goldstein359f3922017-06-21 15:28:41 +0300708 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300709 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300710 if (err == -ENOENT) {
711 index = NULL;
712 goto out;
713 }
lijiazi1bd0a3a2019-12-16 19:12:32 +0800714 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
Amir Goldstein359f3922017-06-21 15:28:41 +0300715 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
716 d_inode(origin)->i_ino, name.len, name.name,
717 err);
718 goto out;
719 }
720
Amir Goldstein0e082552017-07-18 21:07:43 +0300721 inode = d_inode(index);
Al Viro6c2d47982019-10-31 01:21:58 -0400722 if (ovl_is_whiteout(index) && !verify) {
Amir Goldstein06170152018-01-17 14:40:27 +0200723 /*
724 * When index lookup is called with !verify for decoding an
725 * overlay file handle, a whiteout index implies that decode
726 * should treat file handle as stale and no need to print a
727 * warning about it.
728 */
729 dput(index);
730 index = ERR_PTR(-ESTALE);
731 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300732 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
Al Viro6e3e2c42021-03-01 20:37:10 -0500733 inode_wrong_type(inode, d_inode(origin)->i_mode)) {
Amir Goldstein0e082552017-07-18 21:07:43 +0300734 /*
735 * Index should always be of the same file type as origin
736 * except for the case of a whiteout index. A whiteout
737 * index should only exist if all lower aliases have been
738 * unlinked, which means that finding a lower origin on lookup
739 * whose index is a whiteout should be treated as an error.
740 */
lijiazi1bd0a3a2019-12-16 19:12:32 +0800741 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
Amir Goldstein0e082552017-07-18 21:07:43 +0300742 index, d_inode(index)->i_mode & S_IFMT,
743 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300744 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200745 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200746 if (!upper) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800747 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200748 origin, index);
749 goto fail;
750 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300751
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200752 /* Verify that dir index 'upper' xattr points to upper dir */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200753 err = ovl_verify_upper(ofs, index, upper, false);
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200754 if (err) {
755 if (err == -ESTALE) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800756 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200757 upper, origin, index);
758 }
759 goto fail;
760 }
761 } else if (upper && d_inode(upper) != inode) {
762 goto out_dput;
763 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300764out:
765 kfree(name.name);
766 return index;
767
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300768out_dput:
769 dput(index);
770 index = NULL;
771 goto out;
772
Amir Goldstein359f3922017-06-21 15:28:41 +0300773fail:
774 dput(index);
775 index = ERR_PTR(-EIO);
776 goto out;
777}
778
779/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100780 * Returns next layer in stack starting from top.
781 * Returns -1 if this is the last layer.
782 */
783int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
784{
785 struct ovl_entry *oe = dentry->d_fsdata;
786
787 BUG_ON(idx < 0);
788 if (idx == 0) {
789 ovl_path_upper(dentry, path);
790 if (path->dentry)
791 return oe->numlower ? 1 : -1;
792 idx++;
793 }
794 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500795 path->dentry = oe->lowerstack[idx - 1].dentry;
796 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100797
798 return (idx < oe->numlower) ? idx + 1 : -1;
799}
800
Amir Goldstein9678e632018-01-03 19:34:45 +0200801/* Fix missing 'origin' xattr */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200802static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
803 struct dentry *lower, struct dentry *upper)
Amir Goldstein9678e632018-01-03 19:34:45 +0200804{
805 int err;
806
Miklos Szeredi610afc02020-09-02 10:58:49 +0200807 if (ovl_check_origin_xattr(ofs, upper))
Amir Goldstein9678e632018-01-03 19:34:45 +0200808 return 0;
809
810 err = ovl_want_write(dentry);
811 if (err)
812 return err;
813
Amir Goldsteina0c236b2021-06-19 12:26:17 +0300814 err = ovl_set_origin(ofs, lower, upper);
Amir Goldstein9678e632018-01-03 19:34:45 +0200815 if (!err)
816 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
817
818 ovl_drop_write(dentry);
819 return err;
820}
821
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100822struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
823 unsigned int flags)
824{
825 struct ovl_entry *oe;
826 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100827 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100828 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300829 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400830 struct ovl_path *stack = NULL, *origin_path = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100831 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200832 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300833 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100834 unsigned int ctr = 0;
835 struct inode *inode = NULL;
836 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100837 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100838 struct dentry *this;
839 unsigned int i;
840 int err;
Vivek Goyal6815f472020-06-01 11:56:51 -0400841 bool uppermetacopy = false;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100842 struct ovl_lookup_data d = {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300843 .sb = dentry->d_sb,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100844 .name = dentry->d_name,
845 .is_dir = false,
846 .opaque = false,
847 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500848 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100849 .redirect = NULL,
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400850 .metacopy = false,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100851 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100852
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100853 if (dentry->d_name.len > ofs->namelen)
854 return ERR_PTR(-ENAMETOOLONG);
855
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100856 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200857 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100858 if (upperdir) {
Chengguang Xu1434a652020-05-26 09:35:57 +0800859 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100860 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100861 goto out;
862
Miklos Szeredibccece12020-03-17 15:04:22 +0100863 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100864 dput(upperdentry);
865 err = -EREMOTE;
866 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100867 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300868 if (upperdentry && !d.is_dir) {
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300869 /*
870 * Lookup copy up origin by decoding origin file handle.
871 * We may get a disconnected dentry, which is fine,
872 * because we only need to hold the origin inode in
873 * cache and use its inode number. We may even get a
874 * connected dentry, that is not under any of the lower
875 * layers root. That is also fine for using it's inode
876 * number - it's the same as if we held a reference
877 * to a dentry in lower layer that was moved under us.
878 */
youngjund78a0dc2020-06-21 05:50:01 -0700879 err = ovl_check_origin(ofs, upperdentry, &origin_path);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300880 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400881 goto out_put_upper;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400882
883 if (d.metacopy)
Vivek Goyal6815f472020-06-01 11:56:51 -0400884 uppermetacopy = true;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300885 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100886
887 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300888 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100889 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
890 if (!upperredirect)
891 goto out_put_upper;
892 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300893 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100894 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100895 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100896 }
897
Miklos Szeredie28edc42016-12-16 11:02:56 +0100898 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100899 err = -ENOMEM;
Amir Goldstein94375f92019-11-15 14:12:40 +0200900 stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700901 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100902 if (!stack)
903 goto out_put_upper;
904 }
905
Miklos Szeredie28edc42016-12-16 11:02:56 +0100906 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500907 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100908
Vivek Goyal452061f2018-03-09 15:44:41 -0500909 if (!ofs->config.redirect_follow)
910 d.last = i == poe->numlower - 1;
911 else
912 d.last = lower.layer->idx == roe->numlower;
913
Chengguang Xu1434a652020-05-26 09:35:57 +0800914 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100915 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100916 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100917
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100918 if (!this)
919 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100920
Vivek Goyal6815f472020-06-01 11:56:51 -0400921 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
Mickaël Salaüneaab1d42021-03-29 18:49:07 +0200922 dput(this);
Vivek Goyal6815f472020-06-01 11:56:51 -0400923 err = -EPERM;
924 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
925 goto out_put;
926 }
927
928 /*
Amir Goldstein9678e632018-01-03 19:34:45 +0200929 * If no origin fh is stored in upper of a merge dir, store fh
930 * of lower dir and set upper parent "impure".
931 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400932 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
Miklos Szeredi610afc02020-09-02 10:58:49 +0200933 err = ovl_fix_origin(ofs, dentry, this, upperdentry);
Amir Goldstein9678e632018-01-03 19:34:45 +0200934 if (err) {
935 dput(this);
936 goto out_put;
937 }
938 }
939
Amir Goldstein37b129162018-01-10 22:29:38 +0200940 /*
941 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200942 * lower dir that does not match a stored origin xattr. In any
943 * case, only verified origin is used for index lookup.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400944 *
945 * For non-dir dentry, if index=on, then ensure origin
946 * matches the dentry found using path based lookup,
947 * otherwise error out.
Amir Goldstein37b129162018-01-10 22:29:38 +0200948 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400949 if (upperdentry && !ctr &&
950 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
951 (!d.is_dir && ofs->config.index && origin_path))) {
Miklos Szeredi610afc02020-09-02 10:58:49 +0200952 err = ovl_verify_origin(ofs, upperdentry, this, false);
Amir Goldstein37b129162018-01-10 22:29:38 +0200953 if (err) {
954 dput(this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400955 if (d.is_dir)
956 break;
957 goto out_put;
Amir Goldstein37b129162018-01-10 22:29:38 +0200958 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200959 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200960 }
961
Vivek Goyal21d8d662020-06-02 11:23:38 -0400962 if (d.metacopy && ctr) {
963 /*
964 * Do not store intermediate metacopy dentries in
965 * lower chain, except top most lower metacopy dentry.
966 * Continue the loop so that if there is an absolute
967 * redirect on this dentry, poe can be reset to roe.
968 */
969 dput(this);
970 this = NULL;
971 } else {
972 stack[ctr].dentry = this;
973 stack[ctr].layer = lower.layer;
974 ctr++;
975 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100976
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100977 /*
978 * Following redirects can have security consequences: it's like
979 * a symlink into the lower layer without the permission checks.
980 * This is only a problem if the upper layer is untrusted (e.g
981 * comes from an USB drive). This can allow a non-readable file
982 * or directory to become readable.
983 *
984 * Only following redirects when redirects are enabled disables
985 * this attack vector when not necessary.
986 */
987 err = -EPERM;
988 if (d.redirect && !ofs->config.redirect_follow) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800989 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
Amir Goldsteinf8167812017-12-18 14:25:56 +0200990 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100991 goto out_put;
992 }
993
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500994 if (d.stop)
995 break;
996
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300997 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
998 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100999 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +02001000 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001001 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001002 }
1003
Vivek Goyal6815f472020-06-01 11:56:51 -04001004 /*
1005 * For regular non-metacopy upper dentries, there is no lower
1006 * path based lookup, hence ctr will be zero. If a dentry is found
1007 * using ORIGIN xattr on upper, install it in stack.
1008 *
1009 * For metacopy dentry, path based lookup will find lower dentries.
1010 * Just make sure a corresponding data dentry has been found.
1011 */
1012 if (d.metacopy || (uppermetacopy && !ctr)) {
Kevin Locke0a8d0b62020-08-23 08:38:17 -06001013 pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n",
1014 dentry);
Vivek Goyal6815f472020-06-01 11:56:51 -04001015 err = -EIO;
1016 goto out_put;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001017 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1018 if (WARN_ON(stack != NULL)) {
1019 err = -EIO;
1020 goto out_put;
1021 }
1022 stack = origin_path;
1023 ctr = 1;
Vivek Goyal59fb2012020-06-01 11:56:50 -04001024 origin = origin_path->dentry;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001025 origin_path = NULL;
1026 }
1027
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001028 /*
Vivek Goyal59fb2012020-06-01 11:56:50 -04001029 * Always lookup index if there is no-upperdentry.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001030 *
Vivek Goyal59fb2012020-06-01 11:56:50 -04001031 * For the case of upperdentry, we have set origin by now if it
1032 * needed to be set. There are basically three cases.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001033 *
Vivek Goyal59fb2012020-06-01 11:56:50 -04001034 * For directories, lookup index by lower inode and verify it matches
1035 * upper inode. We only trust dir index if we verified that lower dir
1036 * matches origin, otherwise dir index entries may be inconsistent
1037 * and we ignore them.
1038 *
1039 * For regular upper, we already set origin if upper had ORIGIN
1040 * xattr. There is no verification though as there is no path
1041 * based dentry lookup in lower in this case.
1042 *
1043 * For metacopy upper, we set a verified origin already if index
1044 * is enabled and if upper had an ORIGIN xattr.
1045 *
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001046 */
Vivek Goyal59fb2012020-06-01 11:56:50 -04001047 if (!upperdentry && ctr)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001048 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +03001049
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001050 if (origin && ovl_indexdir(dentry->d_sb) &&
1051 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +02001052 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +03001053 if (IS_ERR(index)) {
1054 err = PTR_ERR(index);
1055 index = NULL;
1056 goto out_put;
1057 }
1058 }
1059
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001060 oe = ovl_alloc_entry(ctr);
1061 err = -ENOMEM;
1062 if (!oe)
1063 goto out_put;
1064
Chandan Rajendrab9343632017-07-24 01:57:54 -05001065 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001066 dentry->d_fsdata = oe;
1067
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001068 if (upperopaque)
1069 ovl_dentry_set_opaque(dentry);
1070
Miklos Szeredi55acc662017-07-04 22:03:18 +02001071 if (upperdentry)
1072 ovl_dentry_set_upper_alias(dentry);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001073 else if (index) {
Amir Goldstein359f3922017-06-21 15:28:41 +03001074 upperdentry = dget(index);
Miklos Szeredi610afc02020-09-02 10:58:49 +02001075 upperredirect = ovl_get_redirect_xattr(ofs, upperdentry, 0);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001076 if (IS_ERR(upperredirect)) {
1077 err = PTR_ERR(upperredirect);
1078 upperredirect = NULL;
1079 goto out_free_oe;
1080 }
Miklos Szeredi610afc02020-09-02 10:58:49 +02001081 err = ovl_check_metacopy_xattr(ofs, upperdentry);
Amir Goldstein4518dfc2020-07-15 16:38:08 +03001082 if (err < 0)
1083 goto out_free_oe;
1084 uppermetacopy = err;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001085 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001086
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001087 if (upperdentry || ctr) {
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001088 struct ovl_inode_params oip = {
1089 .upperdentry = upperdentry,
1090 .lowerpath = stack,
1091 .index = index,
1092 .numlower = ctr,
Vivek Goyal9cec54c2018-05-11 11:49:27 -04001093 .redirect = upperredirect,
Vivek Goyal2664bd02018-05-11 11:49:30 -04001094 .lowerdata = (ctr > 1 && !d.is_dir) ?
1095 stack[ctr - 1].dentry : NULL,
Vivek Goyalac6a52e2018-05-08 09:27:21 -04001096 };
1097
1098 inode = ovl_get_inode(dentry->d_sb, &oip);
Miklos Szeredib9ac5c272017-07-04 22:03:17 +02001099 err = PTR_ERR(inode);
1100 if (IS_ERR(inode))
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001101 goto out_free_oe;
Vivek Goyal28166ab2020-06-01 11:56:52 -04001102 if (upperdentry && !uppermetacopy)
1103 ovl_set_flag(OVL_UPPERDATA, inode);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001104 }
1105
Miklos Szeredif4288842020-03-17 15:04:22 +01001106 ovl_dentry_update_reval(dentry, upperdentry,
1107 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
1108
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001109 revert_creds(old_cred);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001110 if (origin_path) {
1111 dput(origin_path->dentry);
1112 kfree(origin_path);
1113 }
Amir Goldstein359f3922017-06-21 15:28:41 +03001114 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001115 kfree(stack);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001116 kfree(d.redirect);
Amir Goldstein829c28b2017-09-29 21:43:07 +03001117 return d_splice_alias(inode, dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001118
1119out_free_oe:
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001120 dentry->d_fsdata = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001121 kfree(oe);
1122out_put:
Amir Goldstein359f3922017-06-21 15:28:41 +03001123 dput(index);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001124 for (i = 0; i < ctr; i++)
1125 dput(stack[i].dentry);
1126 kfree(stack);
1127out_put_upper:
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001128 if (origin_path) {
1129 dput(origin_path->dentry);
1130 kfree(origin_path);
1131 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001132 dput(upperdentry);
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001133 kfree(upperredirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001134out:
Miklos Szeredi02b69b22016-12-16 11:02:56 +01001135 kfree(d.redirect);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001136 revert_creds(old_cred);
1137 return ERR_PTR(err);
1138}
1139
1140bool ovl_lower_positive(struct dentry *dentry)
1141{
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001142 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
1143 const struct qstr *name = &dentry->d_name;
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001144 const struct cred *old_cred;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001145 unsigned int i;
1146 bool positive = false;
1147 bool done = false;
1148
1149 /*
1150 * If dentry is negative, then lower is positive iff this is a
1151 * whiteout.
1152 */
1153 if (!dentry->d_inode)
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001154 return ovl_dentry_is_opaque(dentry);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001155
1156 /* Negative upper -> positive lower */
Miklos Szeredi09d8b582017-07-04 22:03:16 +02001157 if (!ovl_dentry_upper(dentry))
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001158 return true;
1159
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001160 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001161 /* Positive upper -> have to look up lower to see whether it exists */
1162 for (i = 0; !done && !positive && i < poe->numlower; i++) {
1163 struct dentry *this;
1164 struct dentry *lowerdir = poe->lowerstack[i].dentry;
1165
Al Viro6c2d47982019-10-31 01:21:58 -04001166 this = lookup_positive_unlocked(name->name, lowerdir,
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001167 name->len);
1168 if (IS_ERR(this)) {
1169 switch (PTR_ERR(this)) {
1170 case -ENOENT:
1171 case -ENAMETOOLONG:
1172 break;
1173
1174 default:
1175 /*
1176 * Assume something is there, we just couldn't
1177 * access it.
1178 */
1179 positive = true;
1180 break;
1181 }
1182 } else {
Al Viro6c2d47982019-10-31 01:21:58 -04001183 positive = !ovl_is_whiteout(this);
1184 done = true;
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001185 dput(this);
1186 }
1187 }
Amir Goldstein6d0a8a92017-11-10 13:18:07 +02001188 revert_creds(old_cred);
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001189
1190 return positive;
1191}