blob: a6162c4076db73a88f61c91bf4497d97a2e50a6d [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
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200153struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
154 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
Amir Goldsteina9d01952017-04-30 14:46:31 +0300159 /*
160 * Make sure that the stored uuid matches the uuid of the lower
161 * layer where file handle will be decoded.
162 */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200163 if (!uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300164 return NULL;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300165
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200166 bytes = (fh->fb.len - offsetof(struct ovl_fb, fid));
167 real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid,
168 bytes >> 2, (int)fh->fb.type,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200169 connected ? ovl_acceptable : NULL, mnt);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200170 if (IS_ERR(real)) {
171 /*
172 * Treat stale file handle to lower file as "origin unknown".
173 * upper file handle could become stale when upper file is
174 * unlinked and this information is needed to handle stale
175 * index entries correctly.
176 */
177 if (real == ERR_PTR(-ESTALE) &&
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200178 !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER))
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200179 real = NULL;
180 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300181 }
182
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200183 if (ovl_dentry_weird(real)) {
184 dput(real);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300185 return NULL;
186 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300187
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200188 return real;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300189}
190
Miklos Szeredi610afc02020-09-02 10:58:49 +0200191static bool ovl_is_opaquedir(struct super_block *sb, struct dentry *dentry)
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300192{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200193 return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_OPAQUE);
Amir Goldsteinee1d6d372017-05-11 16:42:26 +0300194}
195
Chengguang Xu1434a652020-05-26 09:35:57 +0800196static struct dentry *ovl_lookup_positive_unlocked(const char *name,
197 struct dentry *base, int len,
198 bool drop_negative)
199{
200 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
201
202 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
203 if (drop_negative && ret->d_lockref.count == 1) {
204 spin_lock(&ret->d_lock);
205 /* Recheck condition under lock */
206 if (d_is_negative(ret) && ret->d_lockref.count == 1)
207 __d_drop(ret);
208 spin_unlock(&ret->d_lock);
209 }
210 dput(ret);
211 ret = ERR_PTR(-ENOENT);
212 }
213 return ret;
214}
215
Miklos Szeredie28edc42016-12-16 11:02:56 +0100216static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d,
217 const char *name, unsigned int namelen,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100218 size_t prelen, const char *post,
Chengguang Xu1434a652020-05-26 09:35:57 +0800219 struct dentry **ret, bool drop_negative)
Miklos Szeredie28edc42016-12-16 11:02:56 +0100220{
221 struct dentry *this;
222 int err;
Vivek Goyal102b0d12018-03-09 15:44:43 -0500223 bool last_element = !post[0];
Miklos Szeredie28edc42016-12-16 11:02:56 +0100224
Chengguang Xu1434a652020-05-26 09:35:57 +0800225 this = ovl_lookup_positive_unlocked(name, base, namelen, drop_negative);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100226 if (IS_ERR(this)) {
227 err = PTR_ERR(this);
228 this = NULL;
229 if (err == -ENOENT || err == -ENAMETOOLONG)
230 goto out;
231 goto out_err;
232 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100233
234 if (ovl_dentry_weird(this)) {
235 /* Don't support traversing automounts and other weirdness */
236 err = -EREMOTE;
237 goto out_err;
238 }
239 if (ovl_is_whiteout(this)) {
240 d->stop = d->opaque = true;
241 goto put_and_out;
242 }
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400243 /*
244 * This dentry should be a regular file if previous layer lookup
245 * found a metacopy dentry.
246 */
247 if (last_element && d->metacopy && !d_is_reg(this)) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100248 d->stop = true;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400249 goto put_and_out;
250 }
251 if (!d_can_lookup(this)) {
252 if (d->is_dir || !last_element) {
253 d->stop = true;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100254 goto put_and_out;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400255 }
Miklos Szeredi610afc02020-09-02 10:58:49 +0200256 err = ovl_check_metacopy_xattr(OVL_FS(d->sb), this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400257 if (err < 0)
258 goto out_err;
Miklos Szeredi3a291772018-04-12 12:04:49 +0200259
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400260 d->metacopy = err;
261 d->stop = !d->metacopy;
Vivek Goyalb8a88242018-05-11 11:49:31 -0400262 if (!d->metacopy || d->last)
263 goto out;
Vivek Goyal0618a812018-05-11 11:49:31 -0400264 } else {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300265 if (ovl_lookup_trap_inode(d->sb, this)) {
266 /* Caught in a trap of overlapping layers */
267 err = -ELOOP;
268 goto out_err;
269 }
270
Vivek Goyal102b0d12018-03-09 15:44:43 -0500271 if (last_element)
Vivek Goyal0618a812018-05-11 11:49:31 -0400272 d->is_dir = true;
273 if (d->last)
274 goto out;
275
Miklos Szeredi610afc02020-09-02 10:58:49 +0200276 if (ovl_is_opaquedir(d->sb, this)) {
Vivek Goyal0618a812018-05-11 11:49:31 -0400277 d->stop = true;
278 if (last_element)
279 d->opaque = true;
280 goto out;
281 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100282 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100283 err = ovl_check_redirect(this, d, prelen, post);
284 if (err)
285 goto out_err;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100286out:
287 *ret = this;
288 return 0;
289
290put_and_out:
291 dput(this);
292 this = NULL;
293 goto out;
294
295out_err:
296 dput(this);
297 return err;
298}
299
300static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d,
Chengguang Xu1434a652020-05-26 09:35:57 +0800301 struct dentry **ret, bool drop_negative)
Miklos Szeredie28edc42016-12-16 11:02:56 +0100302{
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100303 /* Counting down from the end, since the prefix can change */
304 size_t rem = d->name.len - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100305 struct dentry *dentry = NULL;
306 int err;
307
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100308 if (d->name.name[0] != '/')
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100309 return ovl_lookup_single(base, d, d->name.name, d->name.len,
Chengguang Xu1434a652020-05-26 09:35:57 +0800310 0, "", ret, drop_negative);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100311
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100312 while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) {
313 const char *s = d->name.name + d->name.len - rem;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100314 const char *next = strchrnul(s, '/');
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100315 size_t thislen = next - s;
316 bool end = !next[0];
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100317
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100318 /* Verify we did not go off the rails */
319 if (WARN_ON(s[-1] != '/'))
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100320 return -EIO;
321
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100322 err = ovl_lookup_single(base, d, s, thislen,
Chengguang Xu1434a652020-05-26 09:35:57 +0800323 d->name.len - rem, next, &base,
324 drop_negative);
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100325 dput(dentry);
326 if (err)
327 return err;
328 dentry = base;
Amir Goldstein4c7d0c92017-01-18 15:19:54 +0100329 if (end)
330 break;
331
332 rem -= thislen + 1;
333
334 if (WARN_ON(rem >= d->name.len))
335 return -EIO;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100336 }
337 *ret = dentry;
338 return 0;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100339}
340
Amir Goldsteina9d01952017-04-30 14:46:31 +0300341
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200342int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200343 struct dentry *upperdentry, struct ovl_path **stackp)
Amir Goldsteina9d01952017-04-30 14:46:31 +0300344{
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300345 struct dentry *origin = NULL;
346 int i;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300347
Amir Goldstein94375f92019-11-15 14:12:40 +0200348 for (i = 1; i < ofs->numlayer; i++) {
Amir Goldstein7e63c872019-11-14 22:28:41 +0200349 /*
350 * If lower fs uuid is not unique among lower fs we cannot match
351 * fh->uuid to layer.
352 */
Amir Goldstein94375f92019-11-15 14:12:40 +0200353 if (ofs->layers[i].fsid &&
354 ofs->layers[i].fs->bad_uuid)
Amir Goldstein7e63c872019-11-14 22:28:41 +0200355 continue;
356
Amir Goldstein94375f92019-11-15 14:12:40 +0200357 origin = ovl_decode_real_fh(fh, ofs->layers[i].mnt,
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200358 connected);
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300359 if (origin)
360 break;
361 }
362
363 if (!origin)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300364 return -ESTALE;
365 else if (IS_ERR(origin))
366 return PTR_ERR(origin);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300367
Amir Goldsteinf9418662018-01-19 21:33:44 +0200368 if (upperdentry && !ovl_is_whiteout(upperdentry) &&
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300369 ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT))
370 goto invalid;
371
Amir Goldstein415543d2017-06-21 15:28:42 +0300372 if (!*stackp)
Chandan Rajendrab9343632017-07-24 01:57:54 -0500373 *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300374 if (!*stackp) {
375 dput(origin);
376 return -ENOMEM;
377 }
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200378 **stackp = (struct ovl_path){
379 .dentry = origin,
Amir Goldstein94375f92019-11-15 14:12:40 +0200380 .layer = &ofs->layers[i]
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200381 };
Amir Goldsteina9d01952017-04-30 14:46:31 +0300382
383 return 0;
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300384
385invalid:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800386 pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n",
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300387 upperdentry, d_inode(upperdentry)->i_mode & S_IFMT,
388 d_inode(origin)->i_mode & S_IFMT);
389 dput(origin);
390 return -EIO;
391}
392
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200393static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry,
youngjund78a0dc2020-06-21 05:50:01 -0700394 struct ovl_path **stackp)
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300395{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200396 struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300397 int err;
398
399 if (IS_ERR_OR_NULL(fh))
400 return PTR_ERR(fh);
401
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200402 err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300403 kfree(fh);
404
405 if (err) {
406 if (err == -ESTALE)
407 return 0;
408 return err;
409 }
410
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300411 return 0;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300412}
413
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100414/*
Amir Goldstein05122442018-01-11 08:25:32 +0200415 * Verify that @fh matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300416 * Return 0 on match, -ESTALE on mismatch, < 0 on error.
417 */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200418static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200419 enum ovl_xattr ox, const struct ovl_fh *fh)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300420{
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200421 struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300422 int err = 0;
423
424 if (!ofh)
425 return -ENODATA;
426
427 if (IS_ERR(ofh))
428 return PTR_ERR(ofh);
429
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200430 if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len))
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300431 err = -ESTALE;
432
433 kfree(ofh);
434 return err;
435}
436
437/*
Amir Goldstein05122442018-01-11 08:25:32 +0200438 * Verify that @real dentry matches the file handle stored in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300439 *
Amir Goldstein05122442018-01-11 08:25:32 +0200440 * If @set is true and there is no stored file handle, encode @real and store
441 * file handle in xattr @name.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300442 *
Amir Goldstein05122442018-01-11 08:25:32 +0200443 * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error.
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300444 */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200445int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200446 enum ovl_xattr ox, struct dentry *real, bool is_upper,
Miklos Szeredi610afc02020-09-02 10:58:49 +0200447 bool set)
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300448{
449 struct inode *inode;
450 struct ovl_fh *fh;
451 int err;
452
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200453 fh = ovl_encode_real_fh(real, is_upper);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300454 err = PTR_ERR(fh);
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300455 if (IS_ERR(fh)) {
456 fh = NULL;
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300457 goto fail;
Amir Goldsteinbabf4772018-10-10 19:10:06 +0300458 }
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300459
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200460 err = ovl_verify_fh(ofs, dentry, ox, fh);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300461 if (set && err == -ENODATA)
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200462 err = ovl_do_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300463 if (err)
464 goto fail;
465
466out:
467 kfree(fh);
468 return err;
469
470fail:
Amir Goldstein05122442018-01-11 08:25:32 +0200471 inode = d_inode(real);
lijiazi1bd0a3a2019-12-16 19:12:32 +0800472 pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n",
Amir Goldstein05122442018-01-11 08:25:32 +0200473 is_upper ? "upper" : "origin", real,
474 inode ? inode->i_ino : 0, err);
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300475 goto out;
476}
477
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200478/* Get upper dentry from index */
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200479struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200480{
481 struct ovl_fh *fh;
482 struct dentry *upper;
483
484 if (!d_is_dir(index))
485 return dget(index);
486
Miklos Szeredi610afc02020-09-02 10:58:49 +0200487 fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200488 if (IS_ERR_OR_NULL(fh))
489 return ERR_CAST(fh);
490
Miklos Szeredi08f4c7c2020-06-04 10:48:19 +0200491 upper = ovl_decode_real_fh(fh, ovl_upper_mnt(ofs), true);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200492 kfree(fh);
493
494 if (IS_ERR_OR_NULL(upper))
495 return upper ?: ERR_PTR(-ESTALE);
496
497 if (!d_is_dir(upper)) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800498 pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n",
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200499 index, upper);
500 dput(upper);
501 return ERR_PTR(-EIO);
502 }
503
504 return upper;
505}
506
Amir Goldstein8b88a2e2017-06-21 15:28:37 +0300507/*
Amir Goldstein415543d2017-06-21 15:28:42 +0300508 * Verify that an index entry name matches the origin file handle stored in
509 * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path.
510 * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error.
511 */
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200512int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index)
Amir Goldstein415543d2017-06-21 15:28:42 +0300513{
514 struct ovl_fh *fh = NULL;
515 size_t len;
Chandan Rajendrab9343632017-07-24 01:57:54 -0500516 struct ovl_path origin = { };
517 struct ovl_path *stack = &origin;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200518 struct dentry *upper = NULL;
Amir Goldstein415543d2017-06-21 15:28:42 +0300519 int err;
520
521 if (!d_inode(index))
522 return 0;
523
Amir Goldsteinfa0096e2017-10-24 12:24:11 +0300524 err = -EINVAL;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200525 if (index->d_name.len < sizeof(struct ovl_fb)*2)
Amir Goldstein415543d2017-06-21 15:28:42 +0300526 goto fail;
527
528 err = -ENOMEM;
529 len = index->d_name.len / 2;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200530 fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL);
Amir Goldstein415543d2017-06-21 15:28:42 +0300531 if (!fh)
532 goto fail;
533
534 err = -EINVAL;
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200535 if (hex2bin(fh->buf, index->d_name.name, len))
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300536 goto fail;
537
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200538 err = ovl_check_fb_len(&fh->fb, len);
Amir Goldstein2e1a53282017-10-24 15:12:15 +0300539 if (err)
Amir Goldstein415543d2017-06-21 15:28:42 +0300540 goto fail;
541
Amir Goldstein7db25d32018-01-11 11:03:13 +0200542 /*
543 * Whiteout index entries are used as an indication that an exported
544 * overlay file handle should be treated as stale (i.e. after unlink
545 * of the overlay inode). These entries contain no origin xattr.
546 */
547 if (ovl_is_whiteout(index))
548 goto out;
549
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200550 /*
551 * Verifying directory index entries are not stale is expensive, so
552 * only verify stale dir index if NFS export is enabled.
553 */
554 if (d_is_dir(index) && !ofs->config.nfs_export)
555 goto out;
556
557 /*
558 * Directory index entries should have 'upper' xattr pointing to the
559 * real upper dir. Non-dir index entries are hardlinks to the upper
560 * real inode. For non-dir index, we can read the copy up origin xattr
561 * directly from the index dentry, but for dir index we first need to
562 * decode the upper directory.
563 */
564 upper = ovl_index_upper(ofs, index);
565 if (IS_ERR_OR_NULL(upper)) {
566 err = PTR_ERR(upper);
Amir Goldstein24f0b172018-01-11 15:33:51 +0200567 /*
568 * Directory index entries with no 'upper' xattr need to be
569 * removed. When dir index entry has a stale 'upper' xattr,
570 * we assume that upper dir was removed and we treat the dir
571 * index as orphan entry that needs to be whited out.
572 */
573 if (err == -ESTALE)
574 goto orphan;
575 else if (!err)
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200576 err = -ESTALE;
577 goto fail;
578 }
579
Miklos Szeredi610afc02020-09-02 10:58:49 +0200580 err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200581 dput(upper);
Amir Goldstein415543d2017-06-21 15:28:42 +0300582 if (err)
583 goto fail;
584
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200585 /* Check if non-dir index is orphan and don't warn before cleaning it */
586 if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) {
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200587 err = ovl_check_origin_fh(ofs, fh, false, index, &stack);
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200588 if (err)
589 goto fail;
Amir Goldstein415543d2017-06-21 15:28:42 +0300590
Miklos Szeredi610afc02020-09-02 10:58:49 +0200591 if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0)
Amir Goldstein24f0b172018-01-11 15:33:51 +0200592 goto orphan;
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200593 }
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300594
Amir Goldstein415543d2017-06-21 15:28:42 +0300595out:
Amir Goldsteine8f9e5b2018-01-11 11:33:24 +0200596 dput(origin.dentry);
Amir Goldstein415543d2017-06-21 15:28:42 +0300597 kfree(fh);
598 return err;
599
600fail:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800601 pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n",
Amir Goldstein61b67472017-07-18 21:07:42 +0300602 index, d_inode(index)->i_mode & S_IFMT, err);
Amir Goldstein415543d2017-06-21 15:28:42 +0300603 goto out;
Amir Goldstein24f0b172018-01-11 15:33:51 +0200604
605orphan:
lijiazi1bd0a3a2019-12-16 19:12:32 +0800606 pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n",
Amir Goldstein24f0b172018-01-11 15:33:51 +0200607 index, d_inode(index)->i_mode & S_IFMT,
608 d_inode(index)->i_nlink);
609 err = -ENOENT;
610 goto out;
Amir Goldstein415543d2017-06-21 15:28:42 +0300611}
612
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200613static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name)
614{
615 char *n, *s;
616
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200617 n = kcalloc(fh->fb.len, 2, GFP_KERNEL);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200618 if (!n)
619 return -ENOMEM;
620
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200621 s = bin2hex(n, fh->buf, fh->fb.len);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200622 *name = (struct qstr) QSTR_INIT(n, s - n);
623
624 return 0;
625
626}
627
Amir Goldstein415543d2017-06-21 15:28:42 +0300628/*
Amir Goldstein359f3922017-06-21 15:28:41 +0300629 * Lookup in indexdir for the index entry of a lower real inode or a copy up
630 * origin inode. The index entry name is the hex representation of the lower
631 * inode file handle.
632 *
633 * If the index dentry in negative, then either no lower aliases have been
634 * copied up yet, or aliases have been copied up in older kernels and are
635 * not indexed.
636 *
637 * If the index dentry for a copy up origin inode is positive, but points
638 * to an inode different than the upper inode, then either the upper inode
639 * has been copied up and not indexed or it was indexed, but since then
640 * index dir was cleared. Either way, that index cannot be used to indentify
641 * the overlay inode.
642 */
643int ovl_get_index_name(struct dentry *origin, struct qstr *name)
644{
Amir Goldstein359f3922017-06-21 15:28:41 +0300645 struct ovl_fh *fh;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200646 int err;
Amir Goldstein359f3922017-06-21 15:28:41 +0300647
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200648 fh = ovl_encode_real_fh(origin, false);
Amir Goldstein359f3922017-06-21 15:28:41 +0300649 if (IS_ERR(fh))
650 return PTR_ERR(fh);
651
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200652 err = ovl_get_index_name_fh(fh, name);
653
Amir Goldstein359f3922017-06-21 15:28:41 +0300654 kfree(fh);
Amir Goldstein359f3922017-06-21 15:28:41 +0300655 return err;
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200656}
Amir Goldstein359f3922017-06-21 15:28:41 +0300657
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200658/* Lookup index by file handle for NFS export */
659struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh)
660{
661 struct dentry *index;
662 struct qstr name;
663 int err;
664
665 err = ovl_get_index_name_fh(fh, &name);
666 if (err)
667 return ERR_PTR(err);
668
Al Viro6c2d47982019-10-31 01:21:58 -0400669 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200670 kfree(name.name);
671 if (IS_ERR(index)) {
672 if (PTR_ERR(index) == -ENOENT)
673 index = NULL;
674 return index;
675 }
676
Al Viro6c2d47982019-10-31 01:21:58 -0400677 if (ovl_is_whiteout(index))
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200678 err = -ESTALE;
679 else if (ovl_dentry_weird(index))
680 err = -EIO;
681 else
682 return index;
683
684 dput(index);
685 return ERR_PTR(err);
Amir Goldstein359f3922017-06-21 15:28:41 +0300686}
687
Amir Goldstein06170152018-01-17 14:40:27 +0200688struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
689 struct dentry *origin, bool verify)
Amir Goldstein359f3922017-06-21 15:28:41 +0300690{
Amir Goldstein359f3922017-06-21 15:28:41 +0300691 struct dentry *index;
692 struct inode *inode;
693 struct qstr name;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200694 bool is_dir = d_is_dir(origin);
Amir Goldstein359f3922017-06-21 15:28:41 +0300695 int err;
696
697 err = ovl_get_index_name(origin, &name);
698 if (err)
699 return ERR_PTR(err);
700
Al Viro6c2d47982019-10-31 01:21:58 -0400701 index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len);
Amir Goldstein359f3922017-06-21 15:28:41 +0300702 if (IS_ERR(index)) {
Amir Goldsteine0082a02017-09-24 13:01:35 +0300703 err = PTR_ERR(index);
Amir Goldstein7937a562017-10-20 17:19:06 +0300704 if (err == -ENOENT) {
705 index = NULL;
706 goto out;
707 }
lijiazi1bd0a3a2019-12-16 19:12:32 +0800708 pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n"
Amir Goldstein359f3922017-06-21 15:28:41 +0300709 "overlayfs: mount with '-o index=off' to disable inodes index.\n",
710 d_inode(origin)->i_ino, name.len, name.name,
711 err);
712 goto out;
713 }
714
Amir Goldstein0e082552017-07-18 21:07:43 +0300715 inode = d_inode(index);
Al Viro6c2d47982019-10-31 01:21:58 -0400716 if (ovl_is_whiteout(index) && !verify) {
Amir Goldstein06170152018-01-17 14:40:27 +0200717 /*
718 * When index lookup is called with !verify for decoding an
719 * overlay file handle, a whiteout index implies that decode
720 * should treat file handle as stale and no need to print a
721 * warning about it.
722 */
723 dput(index);
724 index = ERR_PTR(-ESTALE);
725 goto out;
Amir Goldstein0e082552017-07-18 21:07:43 +0300726 } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) ||
727 ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) {
728 /*
729 * Index should always be of the same file type as origin
730 * except for the case of a whiteout index. A whiteout
731 * index should only exist if all lower aliases have been
732 * unlinked, which means that finding a lower origin on lookup
733 * whose index is a whiteout should be treated as an error.
734 */
lijiazi1bd0a3a2019-12-16 19:12:32 +0800735 pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n",
Amir Goldstein0e082552017-07-18 21:07:43 +0300736 index, d_inode(index)->i_mode & S_IFMT,
737 d_inode(origin)->i_mode & S_IFMT);
Amir Goldstein359f3922017-06-21 15:28:41 +0300738 goto fail;
Amir Goldstein06170152018-01-17 14:40:27 +0200739 } else if (is_dir && verify) {
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200740 if (!upper) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800741 pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n",
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200742 origin, index);
743 goto fail;
744 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300745
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200746 /* Verify that dir index 'upper' xattr points to upper dir */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200747 err = ovl_verify_upper(ofs, index, upper, false);
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200748 if (err) {
749 if (err == -ESTALE) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800750 pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n",
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200751 upper, origin, index);
752 }
753 goto fail;
754 }
755 } else if (upper && d_inode(upper) != inode) {
756 goto out_dput;
757 }
Amir Goldstein359f3922017-06-21 15:28:41 +0300758out:
759 kfree(name.name);
760 return index;
761
Amir Goldstein6eaf0112017-10-12 19:03:04 +0300762out_dput:
763 dput(index);
764 index = NULL;
765 goto out;
766
Amir Goldstein359f3922017-06-21 15:28:41 +0300767fail:
768 dput(index);
769 index = ERR_PTR(-EIO);
770 goto out;
771}
772
773/*
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100774 * Returns next layer in stack starting from top.
775 * Returns -1 if this is the last layer.
776 */
777int ovl_path_next(int idx, struct dentry *dentry, struct path *path)
778{
779 struct ovl_entry *oe = dentry->d_fsdata;
780
781 BUG_ON(idx < 0);
782 if (idx == 0) {
783 ovl_path_upper(dentry, path);
784 if (path->dentry)
785 return oe->numlower ? 1 : -1;
786 idx++;
787 }
788 BUG_ON(idx > oe->numlower);
Chandan Rajendrab9343632017-07-24 01:57:54 -0500789 path->dentry = oe->lowerstack[idx - 1].dentry;
790 path->mnt = oe->lowerstack[idx - 1].layer->mnt;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100791
792 return (idx < oe->numlower) ? idx + 1 : -1;
793}
794
Amir Goldstein9678e632018-01-03 19:34:45 +0200795/* Fix missing 'origin' xattr */
Miklos Szeredi610afc02020-09-02 10:58:49 +0200796static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry,
797 struct dentry *lower, struct dentry *upper)
Amir Goldstein9678e632018-01-03 19:34:45 +0200798{
799 int err;
800
Miklos Szeredi610afc02020-09-02 10:58:49 +0200801 if (ovl_check_origin_xattr(ofs, upper))
Amir Goldstein9678e632018-01-03 19:34:45 +0200802 return 0;
803
804 err = ovl_want_write(dentry);
805 if (err)
806 return err;
807
808 err = ovl_set_origin(dentry, lower, upper);
809 if (!err)
810 err = ovl_set_impure(dentry->d_parent, upper->d_parent);
811
812 ovl_drop_write(dentry);
813 return err;
814}
815
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100816struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
817 unsigned int flags)
818{
819 struct ovl_entry *oe;
820 const struct cred *old_cred;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100821 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100822 struct ovl_entry *poe = dentry->d_parent->d_fsdata;
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300823 struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400824 struct ovl_path *stack = NULL, *origin_path = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100825 struct dentry *upperdir, *upperdentry = NULL;
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200826 struct dentry *origin = NULL;
Amir Goldstein359f3922017-06-21 15:28:41 +0300827 struct dentry *index = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100828 unsigned int ctr = 0;
829 struct inode *inode = NULL;
830 bool upperopaque = false;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100831 char *upperredirect = NULL;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100832 struct dentry *this;
833 unsigned int i;
834 int err;
Vivek Goyal6815f472020-06-01 11:56:51 -0400835 bool uppermetacopy = false;
Miklos Szeredie28edc42016-12-16 11:02:56 +0100836 struct ovl_lookup_data d = {
Amir Goldstein146d62e2019-04-18 17:42:08 +0300837 .sb = dentry->d_sb,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100838 .name = dentry->d_name,
839 .is_dir = false,
840 .opaque = false,
841 .stop = false,
Vivek Goyal452061f2018-03-09 15:44:41 -0500842 .last = ofs->config.redirect_follow ? false : !poe->numlower,
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100843 .redirect = NULL,
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400844 .metacopy = false,
Miklos Szeredie28edc42016-12-16 11:02:56 +0100845 };
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100846
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100847 if (dentry->d_name.len > ofs->namelen)
848 return ERR_PTR(-ENAMETOOLONG);
849
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100850 old_cred = ovl_override_creds(dentry->d_sb);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200851 upperdir = ovl_dentry_upper(dentry->d_parent);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100852 if (upperdir) {
Chengguang Xu1434a652020-05-26 09:35:57 +0800853 err = ovl_lookup_layer(upperdir, &d, &upperdentry, true);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100854 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100855 goto out;
856
Miklos Szeredibccece12020-03-17 15:04:22 +0100857 if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) {
Miklos Szeredie28edc42016-12-16 11:02:56 +0100858 dput(upperdentry);
859 err = -EREMOTE;
860 goto out;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100861 }
Amir Goldsteina9d01952017-04-30 14:46:31 +0300862 if (upperdentry && !d.is_dir) {
Amir Goldsteinf7d3dac2017-06-21 15:28:34 +0300863 /*
864 * Lookup copy up origin by decoding origin file handle.
865 * We may get a disconnected dentry, which is fine,
866 * because we only need to hold the origin inode in
867 * cache and use its inode number. We may even get a
868 * connected dentry, that is not under any of the lower
869 * layers root. That is also fine for using it's inode
870 * number - it's the same as if we held a reference
871 * to a dentry in lower layer that was moved under us.
872 */
youngjund78a0dc2020-06-21 05:50:01 -0700873 err = ovl_check_origin(ofs, upperdentry, &origin_path);
Amir Goldsteina9d01952017-04-30 14:46:31 +0300874 if (err)
Vivek Goyal5455f922017-11-01 15:37:22 -0400875 goto out_put_upper;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400876
877 if (d.metacopy)
Vivek Goyal6815f472020-06-01 11:56:51 -0400878 uppermetacopy = true;
Amir Goldsteina9d01952017-04-30 14:46:31 +0300879 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100880
881 if (d.redirect) {
Dan Carpenter0ce5cdc2017-09-22 23:45:18 +0300882 err = -ENOMEM;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100883 upperredirect = kstrdup(d.redirect, GFP_KERNEL);
884 if (!upperredirect)
885 goto out_put_upper;
886 if (d.redirect[0] == '/')
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300887 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100888 }
Miklos Szeredie28edc42016-12-16 11:02:56 +0100889 upperopaque = d.opaque;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100890 }
891
Miklos Szeredie28edc42016-12-16 11:02:56 +0100892 if (!d.stop && poe->numlower) {
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100893 err = -ENOMEM;
Amir Goldstein94375f92019-11-15 14:12:40 +0200894 stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path),
Michal Hocko0ee931c2017-09-13 16:28:29 -0700895 GFP_KERNEL);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100896 if (!stack)
897 goto out_put_upper;
898 }
899
Miklos Szeredie28edc42016-12-16 11:02:56 +0100900 for (i = 0; !d.stop && i < poe->numlower; i++) {
Chandan Rajendrab9343632017-07-24 01:57:54 -0500901 struct ovl_path lower = poe->lowerstack[i];
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100902
Vivek Goyal452061f2018-03-09 15:44:41 -0500903 if (!ofs->config.redirect_follow)
904 d.last = i == poe->numlower - 1;
905 else
906 d.last = lower.layer->idx == roe->numlower;
907
Chengguang Xu1434a652020-05-26 09:35:57 +0800908 err = ovl_lookup_layer(lower.dentry, &d, &this, false);
Miklos Szeredie28edc42016-12-16 11:02:56 +0100909 if (err)
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100910 goto out_put;
Miklos Szeredi6b2d5fe2016-12-16 11:02:56 +0100911
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100912 if (!this)
913 continue;
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100914
Vivek Goyal6815f472020-06-01 11:56:51 -0400915 if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) {
916 err = -EPERM;
917 pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry);
918 goto out_put;
919 }
920
921 /*
Amir Goldstein9678e632018-01-03 19:34:45 +0200922 * If no origin fh is stored in upper of a merge dir, store fh
923 * of lower dir and set upper parent "impure".
924 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400925 if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) {
Miklos Szeredi610afc02020-09-02 10:58:49 +0200926 err = ovl_fix_origin(ofs, dentry, this, upperdentry);
Amir Goldstein9678e632018-01-03 19:34:45 +0200927 if (err) {
928 dput(this);
929 goto out_put;
930 }
931 }
932
Amir Goldstein37b129162018-01-10 22:29:38 +0200933 /*
934 * When "verify_lower" feature is enabled, do not merge with a
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200935 * lower dir that does not match a stored origin xattr. In any
936 * case, only verified origin is used for index lookup.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400937 *
938 * For non-dir dentry, if index=on, then ensure origin
939 * matches the dentry found using path based lookup,
940 * otherwise error out.
Amir Goldstein37b129162018-01-10 22:29:38 +0200941 */
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400942 if (upperdentry && !ctr &&
943 ((d.is_dir && ovl_verify_lower(dentry->d_sb)) ||
944 (!d.is_dir && ofs->config.index && origin_path))) {
Miklos Szeredi610afc02020-09-02 10:58:49 +0200945 err = ovl_verify_origin(ofs, upperdentry, this, false);
Amir Goldstein37b129162018-01-10 22:29:38 +0200946 if (err) {
947 dput(this);
Vivek Goyal9d3dfea2018-05-11 11:49:28 -0400948 if (d.is_dir)
949 break;
950 goto out_put;
Amir Goldstein37b129162018-01-10 22:29:38 +0200951 }
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200952 origin = this;
Amir Goldstein37b129162018-01-10 22:29:38 +0200953 }
954
Vivek Goyal21d8d662020-06-02 11:23:38 -0400955 if (d.metacopy && ctr) {
956 /*
957 * Do not store intermediate metacopy dentries in
958 * lower chain, except top most lower metacopy dentry.
959 * Continue the loop so that if there is an absolute
960 * redirect on this dentry, poe can be reset to roe.
961 */
962 dput(this);
963 this = NULL;
964 } else {
965 stack[ctr].dentry = this;
966 stack[ctr].layer = lower.layer;
967 ctr++;
968 }
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100969
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100970 /*
971 * Following redirects can have security consequences: it's like
972 * a symlink into the lower layer without the permission checks.
973 * This is only a problem if the upper layer is untrusted (e.g
974 * comes from an USB drive). This can allow a non-readable file
975 * or directory to become readable.
976 *
977 * Only following redirects when redirects are enabled disables
978 * this attack vector when not necessary.
979 */
980 err = -EPERM;
981 if (d.redirect && !ofs->config.redirect_follow) {
lijiazi1bd0a3a2019-12-16 19:12:32 +0800982 pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n",
Amir Goldsteinf8167812017-12-18 14:25:56 +0200983 dentry);
Miklos Szeredi438c84c2017-12-11 11:28:10 +0100984 goto out_put;
985 }
986
Vivek Goyald1fe96c2018-02-02 10:23:24 -0500987 if (d.stop)
988 break;
989
Amir Goldsteinc22205d2017-04-26 23:40:52 +0300990 if (d.redirect && d.redirect[0] == '/' && poe != roe) {
991 poe = roe;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100992 /* Find the current layer on the root dentry */
Amir Goldsteind583ed72017-11-08 19:23:36 +0200993 i = lower.layer->idx - 1;
Miklos Szeredi02b69b22016-12-16 11:02:56 +0100994 }
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100995 }
996
Vivek Goyal6815f472020-06-01 11:56:51 -0400997 /*
998 * For regular non-metacopy upper dentries, there is no lower
999 * path based lookup, hence ctr will be zero. If a dentry is found
1000 * using ORIGIN xattr on upper, install it in stack.
1001 *
1002 * For metacopy dentry, path based lookup will find lower dentries.
1003 * Just make sure a corresponding data dentry has been found.
1004 */
1005 if (d.metacopy || (uppermetacopy && !ctr)) {
1006 err = -EIO;
1007 goto out_put;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001008 } else if (!d.is_dir && upperdentry && !ctr && origin_path) {
1009 if (WARN_ON(stack != NULL)) {
1010 err = -EIO;
1011 goto out_put;
1012 }
1013 stack = origin_path;
1014 ctr = 1;
Vivek Goyal59fb2012020-06-01 11:56:50 -04001015 origin = origin_path->dentry;
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001016 origin_path = NULL;
1017 }
1018
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001019 /*
Vivek Goyal59fb2012020-06-01 11:56:50 -04001020 * Always lookup index if there is no-upperdentry.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001021 *
Vivek Goyal59fb2012020-06-01 11:56:50 -04001022 * For the case of upperdentry, we have set origin by now if it
1023 * needed to be set. There are basically three cases.
Vivek Goyal9d3dfea2018-05-11 11:49:28 -04001024 *
Vivek Goyal59fb2012020-06-01 11:56:50 -04001025 * For directories, lookup index by lower inode and verify it matches
1026 * upper inode. We only trust dir index if we verified that lower dir
1027 * matches origin, otherwise dir index entries may be inconsistent
1028 * and we ignore them.
1029 *
1030 * For regular upper, we already set origin if upper had ORIGIN
1031 * xattr. There is no verification though as there is no path
1032 * based dentry lookup in lower in this case.
1033 *
1034 * For metacopy upper, we set a verified origin already if index
1035 * is enabled and if upper had an ORIGIN xattr.
1036 *
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001037 */
Vivek Goyal59fb2012020-06-01 11:56:50 -04001038 if (!upperdentry && ctr)
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001039 origin = stack[0].dentry;
Amir Goldstein359f3922017-06-21 15:28:41 +03001040
Amir Goldsteinad1d6152018-01-11 10:47:03 +02001041 if (origin && ovl_indexdir(dentry->d_sb) &&
1042 (!d.is_dir || ovl_index_all(dentry->d_sb))) {
Amir Goldstein06170152018-01-17 14:40:27 +02001043 index = ovl_lookup_index(ofs, upperdentry, origin, true);
Amir Goldstein359f3922017-06-21 15:28:41 +03001044 if (IS_ERR(index)) {
1045 err = PTR_ERR(index);
1046 index = NULL;
1047 goto out_put;
1048 }
1049 }
1050
Miklos Szeredibbb1e542016-12-16 11:02:56 +01001051 oe = ovl_alloc_entry(ctr);
1052 err = -ENOMEM;
1053 if (!oe)
1054 goto out_put;
1055
Chandan Rajendrab9343632017-07-24 01:57:54 -05001056 memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr);
Miklos Szeredie6d2ebd2017-07-04 22:03:16 +02001057 dentry->d_fsdata = oe;
1058
Amir Goldsteinc62520a2018-01-14 19:25:31 +02001059 if (upperopaque)
1060 ovl_dentry_set_opaque(dentry);
1061
Miklos Szeredi55acc662017-07-04 22:03:18 +02001062 if (upperdentry)
1063 ovl_dentry_set_upper_alias(dentry);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001064 else if (index) {
Amir Goldstein359f3922017-06-21 15:28:41 +03001065 upperdentry = dget(index);
Miklos Szeredi610afc02020-09-02 10:58:49 +02001066 upperredirect = ovl_get_redirect_xattr(ofs, upperdentry, 0);
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001067 if (IS_ERR(upperredirect)) {
1068 err = PTR_ERR(upperredirect);
1069 upperredirect = NULL;
1070 goto out_free_oe;
1071 }
Miklos Szeredi610afc02020-09-02 10:58:49 +02001072 err = ovl_check_metacopy_xattr(ofs, upperdentry);
Amir Goldstein4518dfc2020-07-15 16:38:08 +03001073 if (err < 0)
1074 goto out_free_oe;
1075 uppermetacopy = err;
Vivek Goyal0a2d0d32018-05-11 11:49:32 -04001076 }
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}