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