Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
bbb1e54d MS |
2 | /* |
3 | * Copyright (C) 2011 Novell Inc. | |
4 | * Copyright (C) 2016 Red Hat, Inc. | |
bbb1e54d MS |
5 | */ |
6 | ||
7 | #include <linux/fs.h> | |
5b825c3a | 8 | #include <linux/cred.h> |
9ee60ce2 | 9 | #include <linux/ctype.h> |
bbb1e54d MS |
10 | #include <linux/namei.h> |
11 | #include <linux/xattr.h> | |
02b69b28 | 12 | #include <linux/ratelimit.h> |
a9d01957 AG |
13 | #include <linux/mount.h> |
14 | #include <linux/exportfs.h> | |
bbb1e54d | 15 | #include "overlayfs.h" |
bbb1e54d | 16 | |
e28edc46 | 17 | struct ovl_lookup_data { |
146d62e5 | 18 | struct super_block *sb; |
dad7017a | 19 | struct vfsmount *mnt; |
e28edc46 MS |
20 | struct qstr name; |
21 | bool is_dir; | |
22 | bool opaque; | |
23 | bool stop; | |
24 | bool last; | |
02b69b28 | 25 | char *redirect; |
9d3dfea3 | 26 | bool metacopy; |
e28edc46 | 27 | }; |
bbb1e54d | 28 | |
2d343087 | 29 | static int ovl_check_redirect(const struct path *path, struct ovl_lookup_data *d, |
02b69b28 MS |
30 | size_t prelen, const char *post) |
31 | { | |
32 | int res; | |
0a2d0d3f | 33 | char *buf; |
610afc0b | 34 | struct ovl_fs *ofs = OVL_FS(d->sb); |
02b69b28 | 35 | |
dad7017a | 36 | buf = ovl_get_redirect_xattr(ofs, path, prelen + strlen(post)); |
0a2d0d3f VG |
37 | if (IS_ERR_OR_NULL(buf)) |
38 | return PTR_ERR(buf); | |
02b69b28 | 39 | |
02b69b28 | 40 | if (buf[0] == '/') { |
3ec9b3fa AG |
41 | /* |
42 | * One of the ancestor path elements in an absolute path | |
43 | * lookup in ovl_lookup_layer() could have been opaque and | |
44 | * that will stop further lookup in lower layers (d->stop=true) | |
4f119628 | 45 | * But we have found an absolute redirect in descendant path |
3ec9b3fa AG |
46 | * element and that should force continue lookup in lower |
47 | * layers (reset d->stop). | |
48 | */ | |
49 | d->stop = false; | |
02b69b28 | 50 | } else { |
0a2d0d3f | 51 | res = strlen(buf) + 1; |
02b69b28 MS |
52 | memmove(buf + prelen, buf, res); |
53 | memcpy(buf, d->name.name, prelen); | |
54 | } | |
55 | ||
56 | strcat(buf, post); | |
57 | kfree(d->redirect); | |
58 | d->redirect = buf; | |
59 | d->name.name = d->redirect; | |
60 | d->name.len = strlen(d->redirect); | |
61 | ||
62 | return 0; | |
02b69b28 MS |
63 | } |
64 | ||
a9d01957 AG |
65 | static int ovl_acceptable(void *ctx, struct dentry *dentry) |
66 | { | |
e8f9e5b7 AG |
67 | /* |
68 | * A non-dir origin may be disconnected, which is fine, because | |
69 | * we only need it for its unique inode number. | |
70 | */ | |
71 | if (!d_is_dir(dentry)) | |
72 | return 1; | |
73 | ||
74 | /* Don't decode a deleted empty directory */ | |
75 | if (d_unhashed(dentry)) | |
76 | return 0; | |
77 | ||
78 | /* Check if directory belongs to the layer we are decoding from */ | |
79 | return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root); | |
a9d01957 AG |
80 | } |
81 | ||
2e1a5328 AG |
82 | /* |
83 | * Check validity of an overlay file handle buffer. | |
84 | * | |
85 | * Return 0 for a valid file handle. | |
86 | * Return -ENODATA for "origin unknown". | |
87 | * Return <0 for an invalid file handle. | |
88 | */ | |
cbe7fba8 | 89 | int ovl_check_fb_len(struct ovl_fb *fb, int fb_len) |
2e1a5328 | 90 | { |
cbe7fba8 | 91 | if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len) |
2e1a5328 AG |
92 | return -EINVAL; |
93 | ||
cbe7fba8 | 94 | if (fb->magic != OVL_FH_MAGIC) |
2e1a5328 AG |
95 | return -EINVAL; |
96 | ||
97 | /* Treat larger version and unknown flags as "origin unknown" */ | |
cbe7fba8 | 98 | if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL) |
2e1a5328 AG |
99 | return -ENODATA; |
100 | ||
101 | /* Treat endianness mismatch as "origin unknown" */ | |
cbe7fba8 AG |
102 | if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) && |
103 | (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN) | |
2e1a5328 AG |
104 | return -ENODATA; |
105 | ||
106 | return 0; | |
107 | } | |
108 | ||
dad7017a | 109 | static struct ovl_fh *ovl_get_fh(struct ovl_fs *ofs, struct dentry *upperdentry, |
43d193f8 | 110 | enum ovl_xattr ox) |
a9d01957 | 111 | { |
2e1a5328 | 112 | int res, err; |
a9d01957 | 113 | struct ovl_fh *fh = NULL; |
a9d01957 | 114 | |
dad7017a | 115 | res = ovl_getxattr_upper(ofs, upperdentry, ox, NULL, 0); |
a9d01957 AG |
116 | if (res < 0) { |
117 | if (res == -ENODATA || res == -EOPNOTSUPP) | |
118 | return NULL; | |
119 | goto fail; | |
120 | } | |
121 | /* Zero size value means "copied up but origin unknown" */ | |
122 | if (res == 0) | |
123 | return NULL; | |
124 | ||
cbe7fba8 | 125 | fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL); |
a9d01957 AG |
126 | if (!fh) |
127 | return ERR_PTR(-ENOMEM); | |
128 | ||
dad7017a | 129 | res = ovl_getxattr_upper(ofs, upperdentry, ox, fh->buf, res); |
a9d01957 AG |
130 | if (res < 0) |
131 | goto fail; | |
132 | ||
cbe7fba8 | 133 | err = ovl_check_fb_len(&fh->fb, res); |
2e1a5328 AG |
134 | if (err < 0) { |
135 | if (err == -ENODATA) | |
136 | goto out; | |
a9d01957 | 137 | goto invalid; |
2e1a5328 | 138 | } |
a9d01957 | 139 | |
8b88a2e6 AG |
140 | return fh; |
141 | ||
142 | out: | |
143 | kfree(fh); | |
144 | return NULL; | |
145 | ||
146 | fail: | |
1bd0a3ae | 147 | pr_warn_ratelimited("failed to get origin (%i)\n", res); |
8b88a2e6 AG |
148 | goto out; |
149 | invalid: | |
1bd0a3ae | 150 | pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh); |
8b88a2e6 AG |
151 | goto out; |
152 | } | |
153 | ||
1cdb0cb6 PT |
154 | struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh, |
155 | struct vfsmount *mnt, bool connected) | |
8b88a2e6 | 156 | { |
e8f9e5b7 | 157 | struct dentry *real; |
8b88a2e6 AG |
158 | int bytes; |
159 | ||
c846af05 MS |
160 | if (!capable(CAP_DAC_READ_SEARCH)) |
161 | return NULL; | |
162 | ||
a9d01957 AG |
163 | /* |
164 | * Make sure that the stored uuid matches the uuid of the lower | |
165 | * layer where file handle will be decoded. | |
5830fb6b | 166 | * In case of uuid=off option just make sure that stored uuid is null. |
a9d01957 | 167 | */ |
5830fb6b PT |
168 | if (ofs->config.uuid ? !uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid) : |
169 | !uuid_is_null(&fh->fb.uuid)) | |
2e1a5328 | 170 | return NULL; |
a9d01957 | 171 | |
cbe7fba8 AG |
172 | bytes = (fh->fb.len - offsetof(struct ovl_fb, fid)); |
173 | real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid, | |
174 | bytes >> 2, (int)fh->fb.type, | |
8a22efa1 | 175 | connected ? ovl_acceptable : NULL, mnt); |
e8f9e5b7 AG |
176 | if (IS_ERR(real)) { |
177 | /* | |
178 | * Treat stale file handle to lower file as "origin unknown". | |
179 | * upper file handle could become stale when upper file is | |
180 | * unlinked and this information is needed to handle stale | |
181 | * index entries correctly. | |
182 | */ | |
183 | if (real == ERR_PTR(-ESTALE) && | |
cbe7fba8 | 184 | !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER)) |
e8f9e5b7 AG |
185 | real = NULL; |
186 | return real; | |
a9d01957 AG |
187 | } |
188 | ||
e8f9e5b7 AG |
189 | if (ovl_dentry_weird(real)) { |
190 | dput(real); | |
2e1a5328 AG |
191 | return NULL; |
192 | } | |
a9d01957 | 193 | |
e8f9e5b7 | 194 | return real; |
a9d01957 AG |
195 | } |
196 | ||
2d343087 | 197 | static bool ovl_is_opaquedir(struct ovl_fs *ofs, const struct path *path) |
ee1d6d37 | 198 | { |
dad7017a | 199 | return ovl_path_check_dir_xattr(ofs, path, OVL_XATTR_OPAQUE); |
ee1d6d37 AG |
200 | } |
201 | ||
ba9ea771 CB |
202 | static struct dentry *ovl_lookup_positive_unlocked(struct ovl_lookup_data *d, |
203 | const char *name, | |
1434a65e CX |
204 | struct dentry *base, int len, |
205 | bool drop_negative) | |
206 | { | |
4609e1f1 | 207 | struct dentry *ret = lookup_one_unlocked(mnt_idmap(d->mnt), name, base, len); |
1434a65e CX |
208 | |
209 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { | |
210 | if (drop_negative && ret->d_lockref.count == 1) { | |
211 | spin_lock(&ret->d_lock); | |
212 | /* Recheck condition under lock */ | |
213 | if (d_is_negative(ret) && ret->d_lockref.count == 1) | |
214 | __d_drop(ret); | |
215 | spin_unlock(&ret->d_lock); | |
216 | } | |
217 | dput(ret); | |
218 | ret = ERR_PTR(-ENOENT); | |
219 | } | |
220 | return ret; | |
221 | } | |
222 | ||
e28edc46 MS |
223 | static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, |
224 | const char *name, unsigned int namelen, | |
02b69b28 | 225 | size_t prelen, const char *post, |
1434a65e | 226 | struct dentry **ret, bool drop_negative) |
e28edc46 MS |
227 | { |
228 | struct dentry *this; | |
dad7017a | 229 | struct path path; |
e28edc46 | 230 | int err; |
102b0d11 | 231 | bool last_element = !post[0]; |
e28edc46 | 232 | |
ba9ea771 | 233 | this = ovl_lookup_positive_unlocked(d, name, base, namelen, drop_negative); |
e28edc46 MS |
234 | if (IS_ERR(this)) { |
235 | err = PTR_ERR(this); | |
236 | this = NULL; | |
237 | if (err == -ENOENT || err == -ENAMETOOLONG) | |
238 | goto out; | |
239 | goto out_err; | |
240 | } | |
e28edc46 MS |
241 | |
242 | if (ovl_dentry_weird(this)) { | |
243 | /* Don't support traversing automounts and other weirdness */ | |
244 | err = -EREMOTE; | |
245 | goto out_err; | |
246 | } | |
247 | if (ovl_is_whiteout(this)) { | |
248 | d->stop = d->opaque = true; | |
249 | goto put_and_out; | |
250 | } | |
9d3dfea3 VG |
251 | /* |
252 | * This dentry should be a regular file if previous layer lookup | |
253 | * found a metacopy dentry. | |
254 | */ | |
255 | if (last_element && d->metacopy && !d_is_reg(this)) { | |
e28edc46 | 256 | d->stop = true; |
9d3dfea3 VG |
257 | goto put_and_out; |
258 | } | |
dad7017a CB |
259 | |
260 | path.dentry = this; | |
261 | path.mnt = d->mnt; | |
9d3dfea3 VG |
262 | if (!d_can_lookup(this)) { |
263 | if (d->is_dir || !last_element) { | |
264 | d->stop = true; | |
e28edc46 | 265 | goto put_and_out; |
9d3dfea3 | 266 | } |
dad7017a | 267 | err = ovl_check_metacopy_xattr(OVL_FS(d->sb), &path); |
9d3dfea3 VG |
268 | if (err < 0) |
269 | goto out_err; | |
3a291774 | 270 | |
9d3dfea3 VG |
271 | d->metacopy = err; |
272 | d->stop = !d->metacopy; | |
b8a8824c VG |
273 | if (!d->metacopy || d->last) |
274 | goto out; | |
0618a816 | 275 | } else { |
146d62e5 AG |
276 | if (ovl_lookup_trap_inode(d->sb, this)) { |
277 | /* Caught in a trap of overlapping layers */ | |
278 | err = -ELOOP; | |
279 | goto out_err; | |
280 | } | |
281 | ||
102b0d11 | 282 | if (last_element) |
0618a816 VG |
283 | d->is_dir = true; |
284 | if (d->last) | |
285 | goto out; | |
286 | ||
dad7017a | 287 | if (ovl_is_opaquedir(OVL_FS(d->sb), &path)) { |
0618a816 VG |
288 | d->stop = true; |
289 | if (last_element) | |
290 | d->opaque = true; | |
291 | goto out; | |
292 | } | |
e28edc46 | 293 | } |
dad7017a | 294 | err = ovl_check_redirect(&path, d, prelen, post); |
02b69b28 MS |
295 | if (err) |
296 | goto out_err; | |
e28edc46 MS |
297 | out: |
298 | *ret = this; | |
299 | return 0; | |
300 | ||
301 | put_and_out: | |
302 | dput(this); | |
303 | this = NULL; | |
304 | goto out; | |
305 | ||
306 | out_err: | |
307 | dput(this); | |
308 | return err; | |
309 | } | |
310 | ||
311 | static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, | |
1434a65e | 312 | struct dentry **ret, bool drop_negative) |
e28edc46 | 313 | { |
4c7d0c9c AG |
314 | /* Counting down from the end, since the prefix can change */ |
315 | size_t rem = d->name.len - 1; | |
02b69b28 MS |
316 | struct dentry *dentry = NULL; |
317 | int err; | |
318 | ||
4c7d0c9c | 319 | if (d->name.name[0] != '/') |
02b69b28 | 320 | return ovl_lookup_single(base, d, d->name.name, d->name.len, |
1434a65e | 321 | 0, "", ret, drop_negative); |
02b69b28 | 322 | |
4c7d0c9c AG |
323 | while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { |
324 | const char *s = d->name.name + d->name.len - rem; | |
02b69b28 | 325 | const char *next = strchrnul(s, '/'); |
4c7d0c9c AG |
326 | size_t thislen = next - s; |
327 | bool end = !next[0]; | |
02b69b28 | 328 | |
4c7d0c9c AG |
329 | /* Verify we did not go off the rails */ |
330 | if (WARN_ON(s[-1] != '/')) | |
02b69b28 MS |
331 | return -EIO; |
332 | ||
4c7d0c9c | 333 | err = ovl_lookup_single(base, d, s, thislen, |
1434a65e CX |
334 | d->name.len - rem, next, &base, |
335 | drop_negative); | |
02b69b28 MS |
336 | dput(dentry); |
337 | if (err) | |
338 | return err; | |
339 | dentry = base; | |
4c7d0c9c AG |
340 | if (end) |
341 | break; | |
342 | ||
343 | rem -= thislen + 1; | |
344 | ||
345 | if (WARN_ON(rem >= d->name.len)) | |
346 | return -EIO; | |
02b69b28 MS |
347 | } |
348 | *ret = dentry; | |
349 | return 0; | |
e28edc46 MS |
350 | } |
351 | ||
a9d01957 | 352 | |
8a22efa1 | 353 | int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected, |
f941866f | 354 | struct dentry *upperdentry, struct ovl_path **stackp) |
a9d01957 | 355 | { |
f7d3daca AG |
356 | struct dentry *origin = NULL; |
357 | int i; | |
a9d01957 | 358 | |
94375f9d | 359 | for (i = 1; i < ofs->numlayer; i++) { |
7e63c87f AG |
360 | /* |
361 | * If lower fs uuid is not unique among lower fs we cannot match | |
362 | * fh->uuid to layer. | |
363 | */ | |
94375f9d AG |
364 | if (ofs->layers[i].fsid && |
365 | ofs->layers[i].fs->bad_uuid) | |
7e63c87f AG |
366 | continue; |
367 | ||
1cdb0cb6 | 368 | origin = ovl_decode_real_fh(ofs, fh, ofs->layers[i].mnt, |
8a22efa1 | 369 | connected); |
f7d3daca AG |
370 | if (origin) |
371 | break; | |
372 | } | |
373 | ||
374 | if (!origin) | |
2e1a5328 AG |
375 | return -ESTALE; |
376 | else if (IS_ERR(origin)) | |
377 | return PTR_ERR(origin); | |
378 | ||
f941866f | 379 | if (upperdentry && !ovl_is_whiteout(upperdentry) && |
6e3e2c43 | 380 | inode_wrong_type(d_inode(upperdentry), d_inode(origin)->i_mode)) |
2e1a5328 | 381 | goto invalid; |
a9d01957 | 382 | |
415543d5 | 383 | if (!*stackp) |
b9343632 | 384 | *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); |
a9d01957 AG |
385 | if (!*stackp) { |
386 | dput(origin); | |
387 | return -ENOMEM; | |
388 | } | |
1eff1a1d AG |
389 | **stackp = (struct ovl_path){ |
390 | .dentry = origin, | |
94375f9d | 391 | .layer = &ofs->layers[i] |
1eff1a1d | 392 | }; |
a9d01957 AG |
393 | |
394 | return 0; | |
2e1a5328 AG |
395 | |
396 | invalid: | |
1bd0a3ae | 397 | pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n", |
2e1a5328 AG |
398 | upperdentry, d_inode(upperdentry)->i_mode & S_IFMT, |
399 | d_inode(origin)->i_mode & S_IFMT); | |
400 | dput(origin); | |
ffb24e3c | 401 | return -ESTALE; |
2e1a5328 AG |
402 | } |
403 | ||
1eff1a1d | 404 | static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry, |
d78a0dcf | 405 | struct ovl_path **stackp) |
2e1a5328 | 406 | { |
610afc0b | 407 | struct ovl_fh *fh = ovl_get_fh(ofs, upperdentry, OVL_XATTR_ORIGIN); |
2e1a5328 AG |
408 | int err; |
409 | ||
410 | if (IS_ERR_OR_NULL(fh)) | |
411 | return PTR_ERR(fh); | |
412 | ||
8a22efa1 | 413 | err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp); |
2e1a5328 AG |
414 | kfree(fh); |
415 | ||
416 | if (err) { | |
417 | if (err == -ESTALE) | |
418 | return 0; | |
419 | return err; | |
420 | } | |
421 | ||
2e1a5328 | 422 | return 0; |
a9d01957 AG |
423 | } |
424 | ||
8b88a2e6 | 425 | /* |
05122443 | 426 | * Verify that @fh matches the file handle stored in xattr @name. |
8b88a2e6 AG |
427 | * Return 0 on match, -ESTALE on mismatch, < 0 on error. |
428 | */ | |
610afc0b | 429 | static int ovl_verify_fh(struct ovl_fs *ofs, struct dentry *dentry, |
43d193f8 | 430 | enum ovl_xattr ox, const struct ovl_fh *fh) |
8b88a2e6 | 431 | { |
43d193f8 | 432 | struct ovl_fh *ofh = ovl_get_fh(ofs, dentry, ox); |
8b88a2e6 AG |
433 | int err = 0; |
434 | ||
435 | if (!ofh) | |
436 | return -ENODATA; | |
437 | ||
438 | if (IS_ERR(ofh)) | |
439 | return PTR_ERR(ofh); | |
440 | ||
cbe7fba8 | 441 | if (fh->fb.len != ofh->fb.len || memcmp(&fh->fb, &ofh->fb, fh->fb.len)) |
8b88a2e6 AG |
442 | err = -ESTALE; |
443 | ||
444 | kfree(ofh); | |
445 | return err; | |
446 | } | |
447 | ||
448 | /* | |
05122443 | 449 | * Verify that @real dentry matches the file handle stored in xattr @name. |
8b88a2e6 | 450 | * |
05122443 AG |
451 | * If @set is true and there is no stored file handle, encode @real and store |
452 | * file handle in xattr @name. | |
8b88a2e6 | 453 | * |
05122443 | 454 | * Return 0 on match, -ESTALE on mismatch, -ENODATA on no xattr, < 0 on error. |
8b88a2e6 | 455 | */ |
610afc0b | 456 | int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry, |
43d193f8 | 457 | enum ovl_xattr ox, struct dentry *real, bool is_upper, |
610afc0b | 458 | bool set) |
8b88a2e6 AG |
459 | { |
460 | struct inode *inode; | |
461 | struct ovl_fh *fh; | |
462 | int err; | |
463 | ||
1cdb0cb6 | 464 | fh = ovl_encode_real_fh(ofs, real, is_upper); |
8b88a2e6 | 465 | err = PTR_ERR(fh); |
babf4770 AG |
466 | if (IS_ERR(fh)) { |
467 | fh = NULL; | |
8b88a2e6 | 468 | goto fail; |
babf4770 | 469 | } |
8b88a2e6 | 470 | |
43d193f8 | 471 | err = ovl_verify_fh(ofs, dentry, ox, fh); |
8b88a2e6 | 472 | if (set && err == -ENODATA) |
c914c0e2 | 473 | err = ovl_setxattr(ofs, dentry, ox, fh->buf, fh->fb.len); |
8b88a2e6 AG |
474 | if (err) |
475 | goto fail; | |
476 | ||
477 | out: | |
478 | kfree(fh); | |
479 | return err; | |
480 | ||
481 | fail: | |
05122443 | 482 | inode = d_inode(real); |
1bd0a3ae | 483 | pr_warn_ratelimited("failed to verify %s (%pd2, ino=%lu, err=%i)\n", |
05122443 AG |
484 | is_upper ? "upper" : "origin", real, |
485 | inode ? inode->i_ino : 0, err); | |
8b88a2e6 AG |
486 | goto out; |
487 | } | |
488 | ||
e8f9e5b7 | 489 | /* Get upper dentry from index */ |
8ea28765 AG |
490 | struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index, |
491 | bool connected) | |
e8f9e5b7 AG |
492 | { |
493 | struct ovl_fh *fh; | |
494 | struct dentry *upper; | |
495 | ||
496 | if (!d_is_dir(index)) | |
497 | return dget(index); | |
498 | ||
610afc0b | 499 | fh = ovl_get_fh(ofs, index, OVL_XATTR_UPPER); |
e8f9e5b7 AG |
500 | if (IS_ERR_OR_NULL(fh)) |
501 | return ERR_CAST(fh); | |
502 | ||
8ea28765 | 503 | upper = ovl_decode_real_fh(ofs, fh, ovl_upper_mnt(ofs), connected); |
e8f9e5b7 AG |
504 | kfree(fh); |
505 | ||
506 | if (IS_ERR_OR_NULL(upper)) | |
507 | return upper ?: ERR_PTR(-ESTALE); | |
508 | ||
509 | if (!d_is_dir(upper)) { | |
1bd0a3ae | 510 | pr_warn_ratelimited("invalid index upper (%pd2, upper=%pd2).\n", |
e8f9e5b7 AG |
511 | index, upper); |
512 | dput(upper); | |
513 | return ERR_PTR(-EIO); | |
514 | } | |
515 | ||
516 | return upper; | |
517 | } | |
518 | ||
415543d5 AG |
519 | /* |
520 | * Verify that an index entry name matches the origin file handle stored in | |
521 | * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. | |
522 | * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. | |
523 | */ | |
1eff1a1d | 524 | int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index) |
415543d5 AG |
525 | { |
526 | struct ovl_fh *fh = NULL; | |
527 | size_t len; | |
b9343632 CR |
528 | struct ovl_path origin = { }; |
529 | struct ovl_path *stack = &origin; | |
e8f9e5b7 | 530 | struct dentry *upper = NULL; |
415543d5 AG |
531 | int err; |
532 | ||
533 | if (!d_inode(index)) | |
534 | return 0; | |
535 | ||
fa0096e3 | 536 | err = -EINVAL; |
cbe7fba8 | 537 | if (index->d_name.len < sizeof(struct ovl_fb)*2) |
415543d5 AG |
538 | goto fail; |
539 | ||
540 | err = -ENOMEM; | |
541 | len = index->d_name.len / 2; | |
cbe7fba8 | 542 | fh = kzalloc(len + OVL_FH_WIRE_OFFSET, GFP_KERNEL); |
415543d5 AG |
543 | if (!fh) |
544 | goto fail; | |
545 | ||
546 | err = -EINVAL; | |
cbe7fba8 | 547 | if (hex2bin(fh->buf, index->d_name.name, len)) |
2e1a5328 AG |
548 | goto fail; |
549 | ||
cbe7fba8 | 550 | err = ovl_check_fb_len(&fh->fb, len); |
2e1a5328 | 551 | if (err) |
415543d5 AG |
552 | goto fail; |
553 | ||
7db25d36 AG |
554 | /* |
555 | * Whiteout index entries are used as an indication that an exported | |
556 | * overlay file handle should be treated as stale (i.e. after unlink | |
557 | * of the overlay inode). These entries contain no origin xattr. | |
558 | */ | |
559 | if (ovl_is_whiteout(index)) | |
560 | goto out; | |
561 | ||
e8f9e5b7 AG |
562 | /* |
563 | * Verifying directory index entries are not stale is expensive, so | |
564 | * only verify stale dir index if NFS export is enabled. | |
565 | */ | |
566 | if (d_is_dir(index) && !ofs->config.nfs_export) | |
567 | goto out; | |
568 | ||
569 | /* | |
570 | * Directory index entries should have 'upper' xattr pointing to the | |
571 | * real upper dir. Non-dir index entries are hardlinks to the upper | |
572 | * real inode. For non-dir index, we can read the copy up origin xattr | |
573 | * directly from the index dentry, but for dir index we first need to | |
574 | * decode the upper directory. | |
575 | */ | |
8ea28765 | 576 | upper = ovl_index_upper(ofs, index, false); |
e8f9e5b7 AG |
577 | if (IS_ERR_OR_NULL(upper)) { |
578 | err = PTR_ERR(upper); | |
24f0b172 AG |
579 | /* |
580 | * Directory index entries with no 'upper' xattr need to be | |
581 | * removed. When dir index entry has a stale 'upper' xattr, | |
582 | * we assume that upper dir was removed and we treat the dir | |
583 | * index as orphan entry that needs to be whited out. | |
584 | */ | |
585 | if (err == -ESTALE) | |
586 | goto orphan; | |
587 | else if (!err) | |
e8f9e5b7 | 588 | err = -ESTALE; |
415543d5 | 589 | goto fail; |
e8f9e5b7 | 590 | } |
415543d5 | 591 | |
610afc0b | 592 | err = ovl_verify_fh(ofs, upper, OVL_XATTR_ORIGIN, fh); |
e8f9e5b7 | 593 | dput(upper); |
415543d5 AG |
594 | if (err) |
595 | goto fail; | |
596 | ||
e8f9e5b7 AG |
597 | /* Check if non-dir index is orphan and don't warn before cleaning it */ |
598 | if (!d_is_dir(index) && d_inode(index)->i_nlink == 1) { | |
8a22efa1 | 599 | err = ovl_check_origin_fh(ofs, fh, false, index, &stack); |
e8f9e5b7 AG |
600 | if (err) |
601 | goto fail; | |
602 | ||
610afc0b | 603 | if (ovl_get_nlink(ofs, origin.dentry, index, 0) == 0) |
24f0b172 | 604 | goto orphan; |
e8f9e5b7 | 605 | } |
caf70cb2 | 606 | |
415543d5 | 607 | out: |
e8f9e5b7 | 608 | dput(origin.dentry); |
415543d5 AG |
609 | kfree(fh); |
610 | return err; | |
611 | ||
612 | fail: | |
1bd0a3ae | 613 | pr_warn_ratelimited("failed to verify index (%pd2, ftype=%x, err=%i)\n", |
61b67471 | 614 | index, d_inode(index)->i_mode & S_IFMT, err); |
415543d5 | 615 | goto out; |
24f0b172 AG |
616 | |
617 | orphan: | |
1bd0a3ae | 618 | pr_warn_ratelimited("orphan index entry (%pd2, ftype=%x, nlink=%u)\n", |
24f0b172 AG |
619 | index, d_inode(index)->i_mode & S_IFMT, |
620 | d_inode(index)->i_nlink); | |
621 | err = -ENOENT; | |
622 | goto out; | |
415543d5 AG |
623 | } |
624 | ||
91ffe7be AG |
625 | static int ovl_get_index_name_fh(struct ovl_fh *fh, struct qstr *name) |
626 | { | |
627 | char *n, *s; | |
628 | ||
cbe7fba8 | 629 | n = kcalloc(fh->fb.len, 2, GFP_KERNEL); |
91ffe7be AG |
630 | if (!n) |
631 | return -ENOMEM; | |
632 | ||
cbe7fba8 | 633 | s = bin2hex(n, fh->buf, fh->fb.len); |
91ffe7be AG |
634 | *name = (struct qstr) QSTR_INIT(n, s - n); |
635 | ||
636 | return 0; | |
637 | ||
638 | } | |
639 | ||
359f392c AG |
640 | /* |
641 | * Lookup in indexdir for the index entry of a lower real inode or a copy up | |
642 | * origin inode. The index entry name is the hex representation of the lower | |
643 | * inode file handle. | |
644 | * | |
645 | * If the index dentry in negative, then either no lower aliases have been | |
646 | * copied up yet, or aliases have been copied up in older kernels and are | |
647 | * not indexed. | |
648 | * | |
649 | * If the index dentry for a copy up origin inode is positive, but points | |
650 | * to an inode different than the upper inode, then either the upper inode | |
651 | * has been copied up and not indexed or it was indexed, but since then | |
4f119628 | 652 | * index dir was cleared. Either way, that index cannot be used to identify |
359f392c AG |
653 | * the overlay inode. |
654 | */ | |
1cdb0cb6 PT |
655 | int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin, |
656 | struct qstr *name) | |
359f392c | 657 | { |
359f392c | 658 | struct ovl_fh *fh; |
91ffe7be | 659 | int err; |
359f392c | 660 | |
1cdb0cb6 | 661 | fh = ovl_encode_real_fh(ofs, origin, false); |
359f392c AG |
662 | if (IS_ERR(fh)) |
663 | return PTR_ERR(fh); | |
664 | ||
91ffe7be | 665 | err = ovl_get_index_name_fh(fh, name); |
359f392c | 666 | |
91ffe7be | 667 | kfree(fh); |
359f392c | 668 | return err; |
91ffe7be AG |
669 | } |
670 | ||
671 | /* Lookup index by file handle for NFS export */ | |
672 | struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh) | |
673 | { | |
674 | struct dentry *index; | |
675 | struct qstr name; | |
676 | int err; | |
677 | ||
678 | err = ovl_get_index_name_fh(fh, &name); | |
679 | if (err) | |
680 | return ERR_PTR(err); | |
681 | ||
6c2d4798 | 682 | index = lookup_positive_unlocked(name.name, ofs->indexdir, name.len); |
91ffe7be AG |
683 | kfree(name.name); |
684 | if (IS_ERR(index)) { | |
685 | if (PTR_ERR(index) == -ENOENT) | |
686 | index = NULL; | |
687 | return index; | |
688 | } | |
689 | ||
6c2d4798 | 690 | if (ovl_is_whiteout(index)) |
91ffe7be AG |
691 | err = -ESTALE; |
692 | else if (ovl_dentry_weird(index)) | |
693 | err = -EIO; | |
694 | else | |
695 | return index; | |
359f392c | 696 | |
91ffe7be AG |
697 | dput(index); |
698 | return ERR_PTR(err); | |
359f392c AG |
699 | } |
700 | ||
06170154 AG |
701 | struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper, |
702 | struct dentry *origin, bool verify) | |
359f392c | 703 | { |
359f392c AG |
704 | struct dentry *index; |
705 | struct inode *inode; | |
706 | struct qstr name; | |
ad1d615c | 707 | bool is_dir = d_is_dir(origin); |
359f392c AG |
708 | int err; |
709 | ||
1cdb0cb6 | 710 | err = ovl_get_index_name(ofs, origin, &name); |
359f392c AG |
711 | if (err) |
712 | return ERR_PTR(err); | |
713 | ||
4609e1f1 | 714 | index = lookup_one_positive_unlocked(ovl_upper_mnt_idmap(ofs), name.name, |
ba9ea771 | 715 | ofs->indexdir, name.len); |
359f392c | 716 | if (IS_ERR(index)) { |
e0082a0f | 717 | err = PTR_ERR(index); |
7937a56f AG |
718 | if (err == -ENOENT) { |
719 | index = NULL; | |
720 | goto out; | |
721 | } | |
1bd0a3ae | 722 | pr_warn_ratelimited("failed inode index lookup (ino=%lu, key=%.*s, err=%i);\n" |
359f392c AG |
723 | "overlayfs: mount with '-o index=off' to disable inodes index.\n", |
724 | d_inode(origin)->i_ino, name.len, name.name, | |
725 | err); | |
726 | goto out; | |
727 | } | |
728 | ||
0e082555 | 729 | inode = d_inode(index); |
6c2d4798 | 730 | if (ovl_is_whiteout(index) && !verify) { |
06170154 AG |
731 | /* |
732 | * When index lookup is called with !verify for decoding an | |
733 | * overlay file handle, a whiteout index implies that decode | |
734 | * should treat file handle as stale and no need to print a | |
735 | * warning about it. | |
736 | */ | |
737 | dput(index); | |
738 | index = ERR_PTR(-ESTALE); | |
739 | goto out; | |
0e082555 | 740 | } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) || |
6e3e2c43 | 741 | inode_wrong_type(inode, d_inode(origin)->i_mode)) { |
0e082555 AG |
742 | /* |
743 | * Index should always be of the same file type as origin | |
744 | * except for the case of a whiteout index. A whiteout | |
745 | * index should only exist if all lower aliases have been | |
746 | * unlinked, which means that finding a lower origin on lookup | |
747 | * whose index is a whiteout should be treated as an error. | |
748 | */ | |
1bd0a3ae | 749 | pr_warn_ratelimited("bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n", |
0e082555 AG |
750 | index, d_inode(index)->i_mode & S_IFMT, |
751 | d_inode(origin)->i_mode & S_IFMT); | |
359f392c | 752 | goto fail; |
06170154 | 753 | } else if (is_dir && verify) { |
ad1d615c | 754 | if (!upper) { |
1bd0a3ae | 755 | pr_warn_ratelimited("suspected uncovered redirected dir found (origin=%pd2, index=%pd2).\n", |
ad1d615c AG |
756 | origin, index); |
757 | goto fail; | |
758 | } | |
359f392c | 759 | |
ad1d615c | 760 | /* Verify that dir index 'upper' xattr points to upper dir */ |
610afc0b | 761 | err = ovl_verify_upper(ofs, index, upper, false); |
ad1d615c AG |
762 | if (err) { |
763 | if (err == -ESTALE) { | |
1bd0a3ae | 764 | pr_warn_ratelimited("suspected multiply redirected dir found (upper=%pd2, origin=%pd2, index=%pd2).\n", |
ad1d615c AG |
765 | upper, origin, index); |
766 | } | |
767 | goto fail; | |
768 | } | |
769 | } else if (upper && d_inode(upper) != inode) { | |
770 | goto out_dput; | |
771 | } | |
359f392c AG |
772 | out: |
773 | kfree(name.name); | |
774 | return index; | |
775 | ||
6eaf0111 AG |
776 | out_dput: |
777 | dput(index); | |
778 | index = NULL; | |
779 | goto out; | |
780 | ||
359f392c AG |
781 | fail: |
782 | dput(index); | |
783 | index = ERR_PTR(-EIO); | |
784 | goto out; | |
785 | } | |
786 | ||
bbb1e54d MS |
787 | /* |
788 | * Returns next layer in stack starting from top. | |
789 | * Returns -1 if this is the last layer. | |
790 | */ | |
791 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) | |
792 | { | |
793 | struct ovl_entry *oe = dentry->d_fsdata; | |
794 | ||
795 | BUG_ON(idx < 0); | |
796 | if (idx == 0) { | |
797 | ovl_path_upper(dentry, path); | |
798 | if (path->dentry) | |
799 | return oe->numlower ? 1 : -1; | |
800 | idx++; | |
801 | } | |
802 | BUG_ON(idx > oe->numlower); | |
b9343632 CR |
803 | path->dentry = oe->lowerstack[idx - 1].dentry; |
804 | path->mnt = oe->lowerstack[idx - 1].layer->mnt; | |
bbb1e54d MS |
805 | |
806 | return (idx < oe->numlower) ? idx + 1 : -1; | |
807 | } | |
808 | ||
9678e630 | 809 | /* Fix missing 'origin' xattr */ |
610afc0b MS |
810 | static int ovl_fix_origin(struct ovl_fs *ofs, struct dentry *dentry, |
811 | struct dentry *lower, struct dentry *upper) | |
9678e630 AG |
812 | { |
813 | int err; | |
814 | ||
610afc0b | 815 | if (ovl_check_origin_xattr(ofs, upper)) |
9678e630 AG |
816 | return 0; |
817 | ||
818 | err = ovl_want_write(dentry); | |
819 | if (err) | |
820 | return err; | |
821 | ||
a0c236b1 | 822 | err = ovl_set_origin(ofs, lower, upper); |
9678e630 AG |
823 | if (!err) |
824 | err = ovl_set_impure(dentry->d_parent, upper->d_parent); | |
825 | ||
826 | ovl_drop_write(dentry); | |
827 | return err; | |
828 | } | |
829 | ||
bbb1e54d MS |
830 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
831 | unsigned int flags) | |
832 | { | |
833 | struct ovl_entry *oe; | |
834 | const struct cred *old_cred; | |
6b2d5fe4 | 835 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
bbb1e54d | 836 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
c22205d0 | 837 | struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; |
9d3dfea3 | 838 | struct ovl_path *stack = NULL, *origin_path = NULL; |
bbb1e54d | 839 | struct dentry *upperdir, *upperdentry = NULL; |
ad1d615c | 840 | struct dentry *origin = NULL; |
359f392c | 841 | struct dentry *index = NULL; |
bbb1e54d MS |
842 | unsigned int ctr = 0; |
843 | struct inode *inode = NULL; | |
844 | bool upperopaque = false; | |
02b69b28 | 845 | char *upperredirect = NULL; |
bbb1e54d MS |
846 | struct dentry *this; |
847 | unsigned int i; | |
848 | int err; | |
6815f479 | 849 | bool uppermetacopy = false; |
e28edc46 | 850 | struct ovl_lookup_data d = { |
146d62e5 | 851 | .sb = dentry->d_sb, |
e28edc46 MS |
852 | .name = dentry->d_name, |
853 | .is_dir = false, | |
854 | .opaque = false, | |
855 | .stop = false, | |
452061fd | 856 | .last = ofs->config.redirect_follow ? false : !poe->numlower, |
02b69b28 | 857 | .redirect = NULL, |
9d3dfea3 | 858 | .metacopy = false, |
e28edc46 | 859 | }; |
bbb1e54d | 860 | |
6b2d5fe4 MS |
861 | if (dentry->d_name.len > ofs->namelen) |
862 | return ERR_PTR(-ENAMETOOLONG); | |
863 | ||
bbb1e54d | 864 | old_cred = ovl_override_creds(dentry->d_sb); |
09d8b586 | 865 | upperdir = ovl_dentry_upper(dentry->d_parent); |
bbb1e54d | 866 | if (upperdir) { |
dad7017a | 867 | d.mnt = ovl_upper_mnt(ofs); |
1434a65e | 868 | err = ovl_lookup_layer(upperdir, &d, &upperdentry, true); |
e28edc46 | 869 | if (err) |
bbb1e54d MS |
870 | goto out; |
871 | ||
bccece1e | 872 | if (upperdentry && upperdentry->d_flags & DCACHE_OP_REAL) { |
e28edc46 MS |
873 | dput(upperdentry); |
874 | err = -EREMOTE; | |
875 | goto out; | |
bbb1e54d | 876 | } |
a9d01957 | 877 | if (upperdentry && !d.is_dir) { |
f7d3daca AG |
878 | /* |
879 | * Lookup copy up origin by decoding origin file handle. | |
880 | * We may get a disconnected dentry, which is fine, | |
881 | * because we only need to hold the origin inode in | |
882 | * cache and use its inode number. We may even get a | |
883 | * connected dentry, that is not under any of the lower | |
884 | * layers root. That is also fine for using it's inode | |
885 | * number - it's the same as if we held a reference | |
886 | * to a dentry in lower layer that was moved under us. | |
887 | */ | |
d78a0dcf | 888 | err = ovl_check_origin(ofs, upperdentry, &origin_path); |
a9d01957 | 889 | if (err) |
5455f92b | 890 | goto out_put_upper; |
9d3dfea3 VG |
891 | |
892 | if (d.metacopy) | |
6815f479 | 893 | uppermetacopy = true; |
a9d01957 | 894 | } |
02b69b28 MS |
895 | |
896 | if (d.redirect) { | |
0ce5cdc9 | 897 | err = -ENOMEM; |
02b69b28 MS |
898 | upperredirect = kstrdup(d.redirect, GFP_KERNEL); |
899 | if (!upperredirect) | |
900 | goto out_put_upper; | |
901 | if (d.redirect[0] == '/') | |
c22205d0 | 902 | poe = roe; |
02b69b28 | 903 | } |
e28edc46 | 904 | upperopaque = d.opaque; |
bbb1e54d MS |
905 | } |
906 | ||
e28edc46 | 907 | if (!d.stop && poe->numlower) { |
bbb1e54d | 908 | err = -ENOMEM; |
94375f9d | 909 | stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path), |
0ee931c4 | 910 | GFP_KERNEL); |
bbb1e54d MS |
911 | if (!stack) |
912 | goto out_put_upper; | |
913 | } | |
914 | ||
e28edc46 | 915 | for (i = 0; !d.stop && i < poe->numlower; i++) { |
b9343632 | 916 | struct ovl_path lower = poe->lowerstack[i]; |
bbb1e54d | 917 | |
452061fd VG |
918 | if (!ofs->config.redirect_follow) |
919 | d.last = i == poe->numlower - 1; | |
920 | else | |
921 | d.last = lower.layer->idx == roe->numlower; | |
922 | ||
dad7017a | 923 | d.mnt = lower.layer->mnt; |
1434a65e | 924 | err = ovl_lookup_layer(lower.dentry, &d, &this, false); |
e28edc46 | 925 | if (err) |
bbb1e54d | 926 | goto out_put; |
6b2d5fe4 | 927 | |
bbb1e54d MS |
928 | if (!this) |
929 | continue; | |
bbb1e54d | 930 | |
6815f479 | 931 | if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) { |
eaab1d45 | 932 | dput(this); |
6815f479 VG |
933 | err = -EPERM; |
934 | pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry); | |
935 | goto out_put; | |
936 | } | |
937 | ||
9678e630 AG |
938 | /* |
939 | * If no origin fh is stored in upper of a merge dir, store fh | |
940 | * of lower dir and set upper parent "impure". | |
941 | */ | |
9d3dfea3 | 942 | if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) { |
610afc0b | 943 | err = ovl_fix_origin(ofs, dentry, this, upperdentry); |
9678e630 AG |
944 | if (err) { |
945 | dput(this); | |
946 | goto out_put; | |
947 | } | |
948 | } | |
949 | ||
37b12916 AG |
950 | /* |
951 | * When "verify_lower" feature is enabled, do not merge with a | |
ad1d615c AG |
952 | * lower dir that does not match a stored origin xattr. In any |
953 | * case, only verified origin is used for index lookup. | |
9d3dfea3 VG |
954 | * |
955 | * For non-dir dentry, if index=on, then ensure origin | |
956 | * matches the dentry found using path based lookup, | |
957 | * otherwise error out. | |
37b12916 | 958 | */ |
9d3dfea3 VG |
959 | if (upperdentry && !ctr && |
960 | ((d.is_dir && ovl_verify_lower(dentry->d_sb)) || | |
961 | (!d.is_dir && ofs->config.index && origin_path))) { | |
610afc0b | 962 | err = ovl_verify_origin(ofs, upperdentry, this, false); |
37b12916 AG |
963 | if (err) { |
964 | dput(this); | |
9d3dfea3 VG |
965 | if (d.is_dir) |
966 | break; | |
967 | goto out_put; | |
37b12916 | 968 | } |
ad1d615c | 969 | origin = this; |
37b12916 AG |
970 | } |
971 | ||
21d8d66a VG |
972 | if (d.metacopy && ctr) { |
973 | /* | |
974 | * Do not store intermediate metacopy dentries in | |
975 | * lower chain, except top most lower metacopy dentry. | |
976 | * Continue the loop so that if there is an absolute | |
977 | * redirect on this dentry, poe can be reset to roe. | |
978 | */ | |
979 | dput(this); | |
980 | this = NULL; | |
981 | } else { | |
982 | stack[ctr].dentry = this; | |
983 | stack[ctr].layer = lower.layer; | |
984 | ctr++; | |
985 | } | |
02b69b28 | 986 | |
438c84c2 MS |
987 | /* |
988 | * Following redirects can have security consequences: it's like | |
989 | * a symlink into the lower layer without the permission checks. | |
990 | * This is only a problem if the upper layer is untrusted (e.g | |
991 | * comes from an USB drive). This can allow a non-readable file | |
992 | * or directory to become readable. | |
993 | * | |
994 | * Only following redirects when redirects are enabled disables | |
995 | * this attack vector when not necessary. | |
996 | */ | |
997 | err = -EPERM; | |
998 | if (d.redirect && !ofs->config.redirect_follow) { | |
1bd0a3ae | 999 | pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n", |
f8167817 | 1000 | dentry); |
438c84c2 MS |
1001 | goto out_put; |
1002 | } | |
1003 | ||
d1fe96c0 VG |
1004 | if (d.stop) |
1005 | break; | |
1006 | ||
c22205d0 AG |
1007 | if (d.redirect && d.redirect[0] == '/' && poe != roe) { |
1008 | poe = roe; | |
02b69b28 | 1009 | /* Find the current layer on the root dentry */ |
d583ed7d | 1010 | i = lower.layer->idx - 1; |
02b69b28 | 1011 | } |
bbb1e54d MS |
1012 | } |
1013 | ||
6815f479 VG |
1014 | /* |
1015 | * For regular non-metacopy upper dentries, there is no lower | |
1016 | * path based lookup, hence ctr will be zero. If a dentry is found | |
1017 | * using ORIGIN xattr on upper, install it in stack. | |
1018 | * | |
1019 | * For metacopy dentry, path based lookup will find lower dentries. | |
1020 | * Just make sure a corresponding data dentry has been found. | |
1021 | */ | |
1022 | if (d.metacopy || (uppermetacopy && !ctr)) { | |
0a8d0b64 KL |
1023 | pr_warn_ratelimited("metacopy with no lower data found - abort lookup (%pd2)\n", |
1024 | dentry); | |
6815f479 VG |
1025 | err = -EIO; |
1026 | goto out_put; | |
9d3dfea3 VG |
1027 | } else if (!d.is_dir && upperdentry && !ctr && origin_path) { |
1028 | if (WARN_ON(stack != NULL)) { | |
1029 | err = -EIO; | |
1030 | goto out_put; | |
1031 | } | |
1032 | stack = origin_path; | |
1033 | ctr = 1; | |
59fb2013 | 1034 | origin = origin_path->dentry; |
9d3dfea3 VG |
1035 | origin_path = NULL; |
1036 | } | |
1037 | ||
ad1d615c | 1038 | /* |
59fb2013 | 1039 | * Always lookup index if there is no-upperdentry. |
9d3dfea3 | 1040 | * |
59fb2013 VG |
1041 | * For the case of upperdentry, we have set origin by now if it |
1042 | * needed to be set. There are basically three cases. | |
1043 | * | |
1044 | * For directories, lookup index by lower inode and verify it matches | |
1045 | * upper inode. We only trust dir index if we verified that lower dir | |
1046 | * matches origin, otherwise dir index entries may be inconsistent | |
1047 | * and we ignore them. | |
1048 | * | |
1049 | * For regular upper, we already set origin if upper had ORIGIN | |
1050 | * xattr. There is no verification though as there is no path | |
1051 | * based dentry lookup in lower in this case. | |
1052 | * | |
1053 | * For metacopy upper, we set a verified origin already if index | |
1054 | * is enabled and if upper had an ORIGIN xattr. | |
9d3dfea3 | 1055 | * |
ad1d615c | 1056 | */ |
59fb2013 | 1057 | if (!upperdentry && ctr) |
ad1d615c | 1058 | origin = stack[0].dentry; |
359f392c | 1059 | |
ad1d615c AG |
1060 | if (origin && ovl_indexdir(dentry->d_sb) && |
1061 | (!d.is_dir || ovl_index_all(dentry->d_sb))) { | |
06170154 | 1062 | index = ovl_lookup_index(ofs, upperdentry, origin, true); |
359f392c AG |
1063 | if (IS_ERR(index)) { |
1064 | err = PTR_ERR(index); | |
1065 | index = NULL; | |
1066 | goto out_put; | |
1067 | } | |
1068 | } | |
1069 | ||
bbb1e54d MS |
1070 | oe = ovl_alloc_entry(ctr); |
1071 | err = -ENOMEM; | |
1072 | if (!oe) | |
1073 | goto out_put; | |
1074 | ||
b9343632 | 1075 | memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); |
e6d2ebdd | 1076 | dentry->d_fsdata = oe; |
bbb1e54d | 1077 | |
c62520a8 AG |
1078 | if (upperopaque) |
1079 | ovl_dentry_set_opaque(dentry); | |
1080 | ||
55acc661 MS |
1081 | if (upperdentry) |
1082 | ovl_dentry_set_upper_alias(dentry); | |
0a2d0d3f | 1083 | else if (index) { |
dad7017a CB |
1084 | struct path upperpath = { |
1085 | .dentry = upperdentry = dget(index), | |
1086 | .mnt = ovl_upper_mnt(ofs), | |
1087 | }; | |
1088 | ||
cf4ef780 SG |
1089 | /* |
1090 | * It's safe to assign upperredirect here: the previous | |
1091 | * assignment of happens only if upperdentry is non-NULL, and | |
1092 | * this one only if upperdentry is NULL. | |
1093 | */ | |
dad7017a | 1094 | upperredirect = ovl_get_redirect_xattr(ofs, &upperpath, 0); |
0a2d0d3f VG |
1095 | if (IS_ERR(upperredirect)) { |
1096 | err = PTR_ERR(upperredirect); | |
1097 | upperredirect = NULL; | |
1098 | goto out_free_oe; | |
1099 | } | |
dad7017a | 1100 | err = ovl_check_metacopy_xattr(ofs, &upperpath); |
4518dfcf AG |
1101 | if (err < 0) |
1102 | goto out_free_oe; | |
1103 | uppermetacopy = err; | |
0a2d0d3f | 1104 | } |
359f392c | 1105 | |
e6d2ebdd | 1106 | if (upperdentry || ctr) { |
ac6a52eb VG |
1107 | struct ovl_inode_params oip = { |
1108 | .upperdentry = upperdentry, | |
1109 | .lowerpath = stack, | |
1110 | .index = index, | |
1111 | .numlower = ctr, | |
9cec54c8 | 1112 | .redirect = upperredirect, |
2664bd08 VG |
1113 | .lowerdata = (ctr > 1 && !d.is_dir) ? |
1114 | stack[ctr - 1].dentry : NULL, | |
ac6a52eb VG |
1115 | }; |
1116 | ||
1117 | inode = ovl_get_inode(dentry->d_sb, &oip); | |
b9ac5c27 MS |
1118 | err = PTR_ERR(inode); |
1119 | if (IS_ERR(inode)) | |
bbb1e54d | 1120 | goto out_free_oe; |
28166ab3 VG |
1121 | if (upperdentry && !uppermetacopy) |
1122 | ovl_set_flag(OVL_UPPERDATA, inode); | |
bbb1e54d MS |
1123 | } |
1124 | ||
f4288844 MS |
1125 | ovl_dentry_update_reval(dentry, upperdentry, |
1126 | DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); | |
1127 | ||
bbb1e54d | 1128 | revert_creds(old_cred); |
9d3dfea3 VG |
1129 | if (origin_path) { |
1130 | dput(origin_path->dentry); | |
1131 | kfree(origin_path); | |
1132 | } | |
359f392c | 1133 | dput(index); |
bbb1e54d | 1134 | kfree(stack); |
02b69b28 | 1135 | kfree(d.redirect); |
829c28be | 1136 | return d_splice_alias(inode, dentry); |
bbb1e54d MS |
1137 | |
1138 | out_free_oe: | |
e6d2ebdd | 1139 | dentry->d_fsdata = NULL; |
bbb1e54d MS |
1140 | kfree(oe); |
1141 | out_put: | |
359f392c | 1142 | dput(index); |
bbb1e54d MS |
1143 | for (i = 0; i < ctr; i++) |
1144 | dput(stack[i].dentry); | |
1145 | kfree(stack); | |
1146 | out_put_upper: | |
9d3dfea3 VG |
1147 | if (origin_path) { |
1148 | dput(origin_path->dentry); | |
1149 | kfree(origin_path); | |
1150 | } | |
bbb1e54d | 1151 | dput(upperdentry); |
02b69b28 | 1152 | kfree(upperredirect); |
bbb1e54d | 1153 | out: |
02b69b28 | 1154 | kfree(d.redirect); |
bbb1e54d MS |
1155 | revert_creds(old_cred); |
1156 | return ERR_PTR(err); | |
1157 | } | |
1158 | ||
1159 | bool ovl_lower_positive(struct dentry *dentry) | |
1160 | { | |
bbb1e54d MS |
1161 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
1162 | const struct qstr *name = &dentry->d_name; | |
6d0a8a90 | 1163 | const struct cred *old_cred; |
bbb1e54d MS |
1164 | unsigned int i; |
1165 | bool positive = false; | |
1166 | bool done = false; | |
1167 | ||
1168 | /* | |
1169 | * If dentry is negative, then lower is positive iff this is a | |
1170 | * whiteout. | |
1171 | */ | |
1172 | if (!dentry->d_inode) | |
c62520a8 | 1173 | return ovl_dentry_is_opaque(dentry); |
bbb1e54d MS |
1174 | |
1175 | /* Negative upper -> positive lower */ | |
09d8b586 | 1176 | if (!ovl_dentry_upper(dentry)) |
bbb1e54d MS |
1177 | return true; |
1178 | ||
6d0a8a90 | 1179 | old_cred = ovl_override_creds(dentry->d_sb); |
bbb1e54d MS |
1180 | /* Positive upper -> have to look up lower to see whether it exists */ |
1181 | for (i = 0; !done && !positive && i < poe->numlower; i++) { | |
1182 | struct dentry *this; | |
1183 | struct dentry *lowerdir = poe->lowerstack[i].dentry; | |
1184 | ||
4609e1f1 | 1185 | this = lookup_one_positive_unlocked(mnt_idmap(poe->lowerstack[i].layer->mnt), |
ba9ea771 | 1186 | name->name, lowerdir, name->len); |
bbb1e54d MS |
1187 | if (IS_ERR(this)) { |
1188 | switch (PTR_ERR(this)) { | |
1189 | case -ENOENT: | |
1190 | case -ENAMETOOLONG: | |
1191 | break; | |
1192 | ||
1193 | default: | |
1194 | /* | |
1195 | * Assume something is there, we just couldn't | |
1196 | * access it. | |
1197 | */ | |
1198 | positive = true; | |
1199 | break; | |
1200 | } | |
1201 | } else { | |
6c2d4798 AV |
1202 | positive = !ovl_is_whiteout(this); |
1203 | done = true; | |
bbb1e54d MS |
1204 | dput(this); |
1205 | } | |
1206 | } | |
6d0a8a90 | 1207 | revert_creds(old_cred); |
bbb1e54d MS |
1208 | |
1209 | return positive; | |
1210 | } |