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