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