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 | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 33 | |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 34 | buf = ovl_get_redirect_xattr(dentry, prelen + strlen(post)); |
| 35 | if (IS_ERR_OR_NULL(buf)) |
| 36 | return PTR_ERR(buf); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 37 | |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 38 | if (buf[0] == '/') { |
Amir Goldstein | 3ec9b3f | 2018-03-12 10:30:41 -0400 | [diff] [blame] | 39 | /* |
| 40 | * One of the ancestor path elements in an absolute path |
| 41 | * lookup in ovl_lookup_layer() could have been opaque and |
| 42 | * that will stop further lookup in lower layers (d->stop=true) |
| 43 | * But we have found an absolute redirect in decendant path |
| 44 | * element and that should force continue lookup in lower |
| 45 | * layers (reset d->stop). |
| 46 | */ |
| 47 | d->stop = false; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 48 | } else { |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 49 | res = strlen(buf) + 1; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 50 | memmove(buf + prelen, buf, res); |
| 51 | memcpy(buf, d->name.name, prelen); |
| 52 | } |
| 53 | |
| 54 | strcat(buf, post); |
| 55 | kfree(d->redirect); |
| 56 | d->redirect = buf; |
| 57 | d->name.name = d->redirect; |
| 58 | d->name.len = strlen(d->redirect); |
| 59 | |
| 60 | return 0; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 63 | static int ovl_acceptable(void *ctx, struct dentry *dentry) |
| 64 | { |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 65 | /* |
| 66 | * A non-dir origin may be disconnected, which is fine, because |
| 67 | * we only need it for its unique inode number. |
| 68 | */ |
| 69 | if (!d_is_dir(dentry)) |
| 70 | return 1; |
| 71 | |
| 72 | /* Don't decode a deleted empty directory */ |
| 73 | if (d_unhashed(dentry)) |
| 74 | return 0; |
| 75 | |
| 76 | /* Check if directory belongs to the layer we are decoding from */ |
| 77 | return is_subdir(dentry, ((struct vfsmount *)ctx)->mnt_root); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 78 | } |
| 79 | |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 80 | /* |
| 81 | * Check validity of an overlay file handle buffer. |
| 82 | * |
| 83 | * Return 0 for a valid file handle. |
| 84 | * Return -ENODATA for "origin unknown". |
| 85 | * Return <0 for an invalid file handle. |
| 86 | */ |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 87 | int ovl_check_fb_len(struct ovl_fb *fb, int fb_len) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 88 | { |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 89 | if (fb_len < sizeof(struct ovl_fb) || fb_len < fb->len) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 90 | return -EINVAL; |
| 91 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 92 | if (fb->magic != OVL_FH_MAGIC) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 93 | return -EINVAL; |
| 94 | |
| 95 | /* Treat larger version and unknown flags as "origin unknown" */ |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 96 | if (fb->version > OVL_FH_VERSION || fb->flags & ~OVL_FH_FLAG_ALL) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 97 | return -ENODATA; |
| 98 | |
| 99 | /* Treat endianness mismatch as "origin unknown" */ |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 100 | if (!(fb->flags & OVL_FH_FLAG_ANY_ENDIAN) && |
| 101 | (fb->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 102 | return -ENODATA; |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 107 | static struct ovl_fh *ovl_get_fh(struct dentry *dentry, const char *name) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 108 | { |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 109 | int res, err; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 110 | struct ovl_fh *fh = NULL; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 111 | |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 112 | res = vfs_getxattr(dentry, name, NULL, 0); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 113 | if (res < 0) { |
| 114 | if (res == -ENODATA || res == -EOPNOTSUPP) |
| 115 | return NULL; |
| 116 | goto fail; |
| 117 | } |
| 118 | /* Zero size value means "copied up but origin unknown" */ |
| 119 | if (res == 0) |
| 120 | return NULL; |
| 121 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 122 | fh = kzalloc(res + OVL_FH_WIRE_OFFSET, GFP_KERNEL); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 123 | if (!fh) |
| 124 | return ERR_PTR(-ENOMEM); |
| 125 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 126 | res = vfs_getxattr(dentry, name, fh->buf, res); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 127 | if (res < 0) |
| 128 | goto fail; |
| 129 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 130 | err = ovl_check_fb_len(&fh->fb, res); |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 131 | if (err < 0) { |
| 132 | if (err == -ENODATA) |
| 133 | goto out; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 134 | goto invalid; |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 135 | } |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 136 | |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 137 | return fh; |
| 138 | |
| 139 | out: |
| 140 | kfree(fh); |
| 141 | return NULL; |
| 142 | |
| 143 | fail: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 144 | pr_warn_ratelimited("failed to get origin (%i)\n", res); |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 145 | goto out; |
| 146 | invalid: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 147 | pr_warn_ratelimited("invalid origin (%*phN)\n", res, fh); |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 148 | goto out; |
| 149 | } |
| 150 | |
Amir Goldstein | 8a22efa | 2018-03-09 15:51:02 +0200 | [diff] [blame] | 151 | struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt, |
| 152 | bool connected) |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 153 | { |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 154 | struct dentry *real; |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 155 | int bytes; |
| 156 | |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 157 | /* |
| 158 | * Make sure that the stored uuid matches the uuid of the lower |
| 159 | * layer where file handle will be decoded. |
| 160 | */ |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 161 | if (!uuid_equal(&fh->fb.uuid, &mnt->mnt_sb->s_uuid)) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 162 | return NULL; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 163 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 164 | bytes = (fh->fb.len - offsetof(struct ovl_fb, fid)); |
| 165 | real = exportfs_decode_fh(mnt, (struct fid *)fh->fb.fid, |
| 166 | bytes >> 2, (int)fh->fb.type, |
Amir Goldstein | 8a22efa | 2018-03-09 15:51:02 +0200 | [diff] [blame] | 167 | connected ? ovl_acceptable : NULL, mnt); |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 168 | if (IS_ERR(real)) { |
| 169 | /* |
| 170 | * Treat stale file handle to lower file as "origin unknown". |
| 171 | * upper file handle could become stale when upper file is |
| 172 | * unlinked and this information is needed to handle stale |
| 173 | * index entries correctly. |
| 174 | */ |
| 175 | if (real == ERR_PTR(-ESTALE) && |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 176 | !(fh->fb.flags & OVL_FH_FLAG_PATH_UPPER)) |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 177 | real = NULL; |
| 178 | return real; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 179 | } |
| 180 | |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 181 | if (ovl_dentry_weird(real)) { |
| 182 | dput(real); |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 183 | return NULL; |
| 184 | } |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 185 | |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 186 | return real; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 187 | } |
| 188 | |
Amir Goldstein | ee1d6d37 | 2017-05-11 16:42:26 +0300 | [diff] [blame] | 189 | static bool ovl_is_opaquedir(struct dentry *dentry) |
| 190 | { |
| 191 | return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE); |
| 192 | } |
| 193 | |
Chengguang Xu | 1434a65 | 2020-05-26 09:35:57 +0800 | [diff] [blame] | 194 | static struct dentry *ovl_lookup_positive_unlocked(const char *name, |
| 195 | struct dentry *base, int len, |
| 196 | bool drop_negative) |
| 197 | { |
| 198 | struct dentry *ret = lookup_one_len_unlocked(name, base, len); |
| 199 | |
| 200 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { |
| 201 | if (drop_negative && ret->d_lockref.count == 1) { |
| 202 | spin_lock(&ret->d_lock); |
| 203 | /* Recheck condition under lock */ |
| 204 | if (d_is_negative(ret) && ret->d_lockref.count == 1) |
| 205 | __d_drop(ret); |
| 206 | spin_unlock(&ret->d_lock); |
| 207 | } |
| 208 | dput(ret); |
| 209 | ret = ERR_PTR(-ENOENT); |
| 210 | } |
| 211 | return ret; |
| 212 | } |
| 213 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 214 | static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, |
| 215 | const char *name, unsigned int namelen, |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 216 | size_t prelen, const char *post, |
Chengguang Xu | 1434a65 | 2020-05-26 09:35:57 +0800 | [diff] [blame] | 217 | struct dentry **ret, bool drop_negative) |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 218 | { |
| 219 | struct dentry *this; |
| 220 | int err; |
Vivek Goyal | 102b0d1 | 2018-03-09 15:44:43 -0500 | [diff] [blame] | 221 | bool last_element = !post[0]; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 222 | |
Chengguang Xu | 1434a65 | 2020-05-26 09:35:57 +0800 | [diff] [blame] | 223 | this = ovl_lookup_positive_unlocked(name, base, namelen, drop_negative); |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 224 | if (IS_ERR(this)) { |
| 225 | err = PTR_ERR(this); |
| 226 | this = NULL; |
| 227 | if (err == -ENOENT || err == -ENAMETOOLONG) |
| 228 | goto out; |
| 229 | goto out_err; |
| 230 | } |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 231 | |
| 232 | if (ovl_dentry_weird(this)) { |
| 233 | /* Don't support traversing automounts and other weirdness */ |
| 234 | err = -EREMOTE; |
| 235 | goto out_err; |
| 236 | } |
| 237 | if (ovl_is_whiteout(this)) { |
| 238 | d->stop = d->opaque = true; |
| 239 | goto put_and_out; |
| 240 | } |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 241 | /* |
| 242 | * This dentry should be a regular file if previous layer lookup |
| 243 | * found a metacopy dentry. |
| 244 | */ |
| 245 | if (last_element && d->metacopy && !d_is_reg(this)) { |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 246 | d->stop = true; |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 247 | goto put_and_out; |
| 248 | } |
| 249 | if (!d_can_lookup(this)) { |
| 250 | if (d->is_dir || !last_element) { |
| 251 | d->stop = true; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 252 | goto put_and_out; |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 253 | } |
| 254 | err = ovl_check_metacopy_xattr(this); |
| 255 | if (err < 0) |
| 256 | goto out_err; |
Miklos Szeredi | 3a29177 | 2018-04-12 12:04:49 +0200 | [diff] [blame] | 257 | |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 258 | d->metacopy = err; |
| 259 | d->stop = !d->metacopy; |
Vivek Goyal | b8a8824 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 260 | if (!d->metacopy || d->last) |
| 261 | goto out; |
Vivek Goyal | 0618a81 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 262 | } else { |
Amir Goldstein | 146d62e | 2019-04-18 17:42:08 +0300 | [diff] [blame] | 263 | if (ovl_lookup_trap_inode(d->sb, this)) { |
| 264 | /* Caught in a trap of overlapping layers */ |
| 265 | err = -ELOOP; |
| 266 | goto out_err; |
| 267 | } |
| 268 | |
Vivek Goyal | 102b0d1 | 2018-03-09 15:44:43 -0500 | [diff] [blame] | 269 | if (last_element) |
Vivek Goyal | 0618a81 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 270 | d->is_dir = true; |
| 271 | if (d->last) |
| 272 | goto out; |
| 273 | |
| 274 | if (ovl_is_opaquedir(this)) { |
| 275 | d->stop = true; |
| 276 | if (last_element) |
| 277 | d->opaque = true; |
| 278 | goto out; |
| 279 | } |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 280 | } |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 281 | err = ovl_check_redirect(this, d, prelen, post); |
| 282 | if (err) |
| 283 | goto out_err; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 284 | out: |
| 285 | *ret = this; |
| 286 | return 0; |
| 287 | |
| 288 | put_and_out: |
| 289 | dput(this); |
| 290 | this = NULL; |
| 291 | goto out; |
| 292 | |
| 293 | out_err: |
| 294 | dput(this); |
| 295 | return err; |
| 296 | } |
| 297 | |
| 298 | 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] | 299 | struct dentry **ret, bool drop_negative) |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 300 | { |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 301 | /* Counting down from the end, since the prefix can change */ |
| 302 | size_t rem = d->name.len - 1; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 303 | struct dentry *dentry = NULL; |
| 304 | int err; |
| 305 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 306 | if (d->name.name[0] != '/') |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 307 | return ovl_lookup_single(base, d, d->name.name, d->name.len, |
Chengguang Xu | 1434a65 | 2020-05-26 09:35:57 +0800 | [diff] [blame] | 308 | 0, "", ret, drop_negative); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 309 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 310 | while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { |
| 311 | const char *s = d->name.name + d->name.len - rem; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 312 | const char *next = strchrnul(s, '/'); |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 313 | size_t thislen = next - s; |
| 314 | bool end = !next[0]; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 315 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 316 | /* Verify we did not go off the rails */ |
| 317 | if (WARN_ON(s[-1] != '/')) |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 318 | return -EIO; |
| 319 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 320 | err = ovl_lookup_single(base, d, s, thislen, |
Chengguang Xu | 1434a65 | 2020-05-26 09:35:57 +0800 | [diff] [blame] | 321 | d->name.len - rem, next, &base, |
| 322 | drop_negative); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 323 | dput(dentry); |
| 324 | if (err) |
| 325 | return err; |
| 326 | dentry = base; |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 327 | if (end) |
| 328 | break; |
| 329 | |
| 330 | rem -= thislen + 1; |
| 331 | |
| 332 | if (WARN_ON(rem >= d->name.len)) |
| 333 | return -EIO; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 334 | } |
| 335 | *ret = dentry; |
| 336 | return 0; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 337 | } |
| 338 | |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 339 | |
Amir Goldstein | 8a22efa | 2018-03-09 15:51:02 +0200 | [diff] [blame] | 340 | 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] | 341 | struct dentry *upperdentry, struct ovl_path **stackp) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 342 | { |
Amir Goldstein | f7d3dac | 2017-06-21 15:28:34 +0300 | [diff] [blame] | 343 | struct dentry *origin = NULL; |
| 344 | int i; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 345 | |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 346 | for (i = 1; i < ofs->numlayer; i++) { |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 347 | /* |
| 348 | * If lower fs uuid is not unique among lower fs we cannot match |
| 349 | * fh->uuid to layer. |
| 350 | */ |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 351 | if (ofs->layers[i].fsid && |
| 352 | ofs->layers[i].fs->bad_uuid) |
Amir Goldstein | 7e63c87 | 2019-11-14 22:28:41 +0200 | [diff] [blame] | 353 | continue; |
| 354 | |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 355 | origin = ovl_decode_real_fh(fh, ofs->layers[i].mnt, |
Amir Goldstein | 8a22efa | 2018-03-09 15:51:02 +0200 | [diff] [blame] | 356 | connected); |
Amir Goldstein | f7d3dac | 2017-06-21 15:28:34 +0300 | [diff] [blame] | 357 | if (origin) |
| 358 | break; |
| 359 | } |
| 360 | |
| 361 | if (!origin) |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 362 | return -ESTALE; |
| 363 | else if (IS_ERR(origin)) |
| 364 | return PTR_ERR(origin); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 365 | |
Amir Goldstein | f941866 | 2018-01-19 21:33:44 +0200 | [diff] [blame] | 366 | if (upperdentry && !ovl_is_whiteout(upperdentry) && |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 367 | ((d_inode(origin)->i_mode ^ d_inode(upperdentry)->i_mode) & S_IFMT)) |
| 368 | goto invalid; |
| 369 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 370 | if (!*stackp) |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 371 | *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 372 | if (!*stackp) { |
| 373 | dput(origin); |
| 374 | return -ENOMEM; |
| 375 | } |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame] | 376 | **stackp = (struct ovl_path){ |
| 377 | .dentry = origin, |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 378 | .layer = &ofs->layers[i] |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame] | 379 | }; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 380 | |
| 381 | return 0; |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 382 | |
| 383 | invalid: |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 384 | pr_warn_ratelimited("invalid origin (%pd2, ftype=%x, origin ftype=%x).\n", |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 385 | upperdentry, d_inode(upperdentry)->i_mode & S_IFMT, |
| 386 | d_inode(origin)->i_mode & S_IFMT); |
| 387 | dput(origin); |
| 388 | return -EIO; |
| 389 | } |
| 390 | |
Amir Goldstein | 1eff1a1 | 2017-12-12 22:40:46 +0200 | [diff] [blame] | 391 | static int ovl_check_origin(struct ovl_fs *ofs, struct dentry *upperdentry, |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 392 | struct ovl_path **stackp, unsigned int *ctrp) |
| 393 | { |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 394 | struct ovl_fh *fh = ovl_get_fh(upperdentry, OVL_XATTR_ORIGIN); |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 395 | int err; |
| 396 | |
| 397 | if (IS_ERR_OR_NULL(fh)) |
| 398 | return PTR_ERR(fh); |
| 399 | |
Amir Goldstein | 8a22efa | 2018-03-09 15:51:02 +0200 | [diff] [blame] | 400 | err = ovl_check_origin_fh(ofs, fh, false, upperdentry, stackp); |
Amir Goldstein | 2e1a5328 | 2017-10-24 15:12:15 +0300 | [diff] [blame] | 401 | kfree(fh); |
| 402 | |
| 403 | if (err) { |
| 404 | if (err == -ESTALE) |
| 405 | return 0; |
| 406 | return err; |
| 407 | } |
| 408 | |
| 409 | if (WARN_ON(*ctrp)) |
| 410 | return -EIO; |
| 411 | |
| 412 | *ctrp = 1; |
| 413 | return 0; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 414 | } |
| 415 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 416 | /* |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 417 | * Verify that @fh matches the file handle stored in xattr @name. |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 418 | * Return 0 on match, -ESTALE on mismatch, < 0 on error. |
| 419 | */ |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 420 | static int ovl_verify_fh(struct dentry *dentry, const char *name, |
| 421 | const struct ovl_fh *fh) |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 422 | { |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 423 | struct ovl_fh *ofh = ovl_get_fh(dentry, name); |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 424 | int err = 0; |
| 425 | |
| 426 | if (!ofh) |
| 427 | return -ENODATA; |
| 428 | |
| 429 | if (IS_ERR(ofh)) |
| 430 | return PTR_ERR(ofh); |
| 431 | |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 432 | 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] | 433 | err = -ESTALE; |
| 434 | |
| 435 | kfree(ofh); |
| 436 | return err; |
| 437 | } |
| 438 | |
| 439 | /* |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 440 | * Verify that @real dentry matches the file handle stored in xattr @name. |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 441 | * |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 442 | * If @set is true and there is no stored file handle, encode @real and store |
| 443 | * file handle in xattr @name. |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 444 | * |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 445 | * 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] | 446 | */ |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 447 | int ovl_verify_set_fh(struct dentry *dentry, const char *name, |
| 448 | struct dentry *real, bool is_upper, 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 | |
Amir Goldstein | 0512244 | 2018-01-11 08:25:32 +0200 | [diff] [blame] | 461 | err = ovl_verify_fh(dentry, name, fh); |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 462 | if (set && err == -ENODATA) |
Amir Goldstein | cbe7fba | 2019-11-15 13:33:03 +0200 | [diff] [blame] | 463 | err = ovl_do_setxattr(dentry, name, fh->buf, fh->fb.len, 0); |
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 | |
| 488 | fh = ovl_get_fh(index, OVL_XATTR_UPPER); |
| 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 | |
| 581 | err = ovl_verify_fh(upper, OVL_XATTR_ORIGIN, fh); |
| 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 | |
Amir Goldstein | e8f9e5b | 2018-01-11 11:33:24 +0200 | [diff] [blame] | 592 | if (ovl_get_nlink(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) || |
| 728 | ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) { |
| 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 */ |
| 748 | err = ovl_verify_upper(index, upper, false); |
| 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 */ |
| 797 | static int ovl_fix_origin(struct dentry *dentry, struct dentry *lower, |
| 798 | struct dentry *upper) |
| 799 | { |
| 800 | int err; |
| 801 | |
| 802 | if (ovl_check_origin_xattr(upper)) |
| 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) { |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 864 | unsigned int origin_ctr = 0; |
| 865 | |
Amir Goldstein | f7d3dac | 2017-06-21 15:28:34 +0300 | [diff] [blame] | 866 | /* |
| 867 | * Lookup copy up origin by decoding origin file handle. |
| 868 | * We may get a disconnected dentry, which is fine, |
| 869 | * because we only need to hold the origin inode in |
| 870 | * cache and use its inode number. We may even get a |
| 871 | * connected dentry, that is not under any of the lower |
| 872 | * layers root. That is also fine for using it's inode |
| 873 | * number - it's the same as if we held a reference |
| 874 | * to a dentry in lower layer that was moved under us. |
| 875 | */ |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 876 | err = ovl_check_origin(ofs, upperdentry, &origin_path, |
| 877 | &origin_ctr); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 878 | if (err) |
Vivek Goyal | 5455f92 | 2017-11-01 15:37:22 -0400 | [diff] [blame] | 879 | goto out_put_upper; |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 880 | |
| 881 | if (d.metacopy) |
Vivek Goyal | 6815f47 | 2020-06-01 11:56:51 -0400 | [diff] [blame] | 882 | uppermetacopy = true; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 883 | } |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 884 | |
| 885 | if (d.redirect) { |
Dan Carpenter | 0ce5cdc | 2017-09-22 23:45:18 +0300 | [diff] [blame] | 886 | err = -ENOMEM; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 887 | upperredirect = kstrdup(d.redirect, GFP_KERNEL); |
| 888 | if (!upperredirect) |
| 889 | goto out_put_upper; |
| 890 | if (d.redirect[0] == '/') |
Amir Goldstein | c22205d | 2017-04-26 23:40:52 +0300 | [diff] [blame] | 891 | poe = roe; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 892 | } |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 893 | upperopaque = d.opaque; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 894 | } |
| 895 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 896 | if (!d.stop && poe->numlower) { |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 897 | err = -ENOMEM; |
Amir Goldstein | 94375f9 | 2019-11-15 14:12:40 +0200 | [diff] [blame] | 898 | stack = kcalloc(ofs->numlayer - 1, sizeof(struct ovl_path), |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 899 | GFP_KERNEL); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 900 | if (!stack) |
| 901 | goto out_put_upper; |
| 902 | } |
| 903 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 904 | for (i = 0; !d.stop && i < poe->numlower; i++) { |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 905 | struct ovl_path lower = poe->lowerstack[i]; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 906 | |
Vivek Goyal | 452061f | 2018-03-09 15:44:41 -0500 | [diff] [blame] | 907 | if (!ofs->config.redirect_follow) |
| 908 | d.last = i == poe->numlower - 1; |
| 909 | else |
| 910 | d.last = lower.layer->idx == roe->numlower; |
| 911 | |
Chengguang Xu | 1434a65 | 2020-05-26 09:35:57 +0800 | [diff] [blame] | 912 | err = ovl_lookup_layer(lower.dentry, &d, &this, false); |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 913 | if (err) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 914 | goto out_put; |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 915 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 916 | if (!this) |
| 917 | continue; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 918 | |
Vivek Goyal | 6815f47 | 2020-06-01 11:56:51 -0400 | [diff] [blame] | 919 | if ((uppermetacopy || d.metacopy) && !ofs->config.metacopy) { |
| 920 | err = -EPERM; |
| 921 | pr_warn_ratelimited("refusing to follow metacopy origin for (%pd2)\n", dentry); |
| 922 | goto out_put; |
| 923 | } |
| 924 | |
| 925 | /* |
Amir Goldstein | 9678e63 | 2018-01-03 19:34:45 +0200 | [diff] [blame] | 926 | * If no origin fh is stored in upper of a merge dir, store fh |
| 927 | * of lower dir and set upper parent "impure". |
| 928 | */ |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 929 | if (upperdentry && !ctr && !ofs->noxattr && d.is_dir) { |
Amir Goldstein | 9678e63 | 2018-01-03 19:34:45 +0200 | [diff] [blame] | 930 | err = ovl_fix_origin(dentry, this, upperdentry); |
| 931 | if (err) { |
| 932 | dput(this); |
| 933 | goto out_put; |
| 934 | } |
| 935 | } |
| 936 | |
Amir Goldstein | 37b12916 | 2018-01-10 22:29:38 +0200 | [diff] [blame] | 937 | /* |
| 938 | * When "verify_lower" feature is enabled, do not merge with a |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 939 | * lower dir that does not match a stored origin xattr. In any |
| 940 | * case, only verified origin is used for index lookup. |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 941 | * |
| 942 | * For non-dir dentry, if index=on, then ensure origin |
| 943 | * matches the dentry found using path based lookup, |
| 944 | * otherwise error out. |
Amir Goldstein | 37b12916 | 2018-01-10 22:29:38 +0200 | [diff] [blame] | 945 | */ |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 946 | if (upperdentry && !ctr && |
| 947 | ((d.is_dir && ovl_verify_lower(dentry->d_sb)) || |
| 948 | (!d.is_dir && ofs->config.index && origin_path))) { |
Amir Goldstein | 37b12916 | 2018-01-10 22:29:38 +0200 | [diff] [blame] | 949 | err = ovl_verify_origin(upperdentry, this, false); |
| 950 | if (err) { |
| 951 | dput(this); |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 952 | if (d.is_dir) |
| 953 | break; |
| 954 | goto out_put; |
Amir Goldstein | 37b12916 | 2018-01-10 22:29:38 +0200 | [diff] [blame] | 955 | } |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 956 | origin = this; |
Amir Goldstein | 37b12916 | 2018-01-10 22:29:38 +0200 | [diff] [blame] | 957 | } |
| 958 | |
Vivek Goyal | 21d8d66 | 2020-06-02 11:23:38 -0400 | [diff] [blame] | 959 | if (d.metacopy && ctr) { |
| 960 | /* |
| 961 | * Do not store intermediate metacopy dentries in |
| 962 | * lower chain, except top most lower metacopy dentry. |
| 963 | * Continue the loop so that if there is an absolute |
| 964 | * redirect on this dentry, poe can be reset to roe. |
| 965 | */ |
| 966 | dput(this); |
| 967 | this = NULL; |
| 968 | } else { |
| 969 | stack[ctr].dentry = this; |
| 970 | stack[ctr].layer = lower.layer; |
| 971 | ctr++; |
| 972 | } |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 973 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 974 | /* |
| 975 | * Following redirects can have security consequences: it's like |
| 976 | * a symlink into the lower layer without the permission checks. |
| 977 | * This is only a problem if the upper layer is untrusted (e.g |
| 978 | * comes from an USB drive). This can allow a non-readable file |
| 979 | * or directory to become readable. |
| 980 | * |
| 981 | * Only following redirects when redirects are enabled disables |
| 982 | * this attack vector when not necessary. |
| 983 | */ |
| 984 | err = -EPERM; |
| 985 | if (d.redirect && !ofs->config.redirect_follow) { |
lijiazi | 1bd0a3a | 2019-12-16 19:12:32 +0800 | [diff] [blame] | 986 | pr_warn_ratelimited("refusing to follow redirect for (%pd2)\n", |
Amir Goldstein | f816781 | 2017-12-18 14:25:56 +0200 | [diff] [blame] | 987 | dentry); |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 988 | goto out_put; |
| 989 | } |
| 990 | |
Vivek Goyal | d1fe96c | 2018-02-02 10:23:24 -0500 | [diff] [blame] | 991 | if (d.stop) |
| 992 | break; |
| 993 | |
Amir Goldstein | c22205d | 2017-04-26 23:40:52 +0300 | [diff] [blame] | 994 | if (d.redirect && d.redirect[0] == '/' && poe != roe) { |
| 995 | poe = roe; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 996 | /* Find the current layer on the root dentry */ |
Amir Goldstein | d583ed7 | 2017-11-08 19:23:36 +0200 | [diff] [blame] | 997 | i = lower.layer->idx - 1; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 998 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 999 | } |
| 1000 | |
Vivek Goyal | 6815f47 | 2020-06-01 11:56:51 -0400 | [diff] [blame] | 1001 | /* |
| 1002 | * For regular non-metacopy upper dentries, there is no lower |
| 1003 | * path based lookup, hence ctr will be zero. If a dentry is found |
| 1004 | * using ORIGIN xattr on upper, install it in stack. |
| 1005 | * |
| 1006 | * For metacopy dentry, path based lookup will find lower dentries. |
| 1007 | * Just make sure a corresponding data dentry has been found. |
| 1008 | */ |
| 1009 | if (d.metacopy || (uppermetacopy && !ctr)) { |
| 1010 | err = -EIO; |
| 1011 | goto out_put; |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1012 | } else if (!d.is_dir && upperdentry && !ctr && origin_path) { |
| 1013 | if (WARN_ON(stack != NULL)) { |
| 1014 | err = -EIO; |
| 1015 | goto out_put; |
| 1016 | } |
| 1017 | stack = origin_path; |
| 1018 | ctr = 1; |
Vivek Goyal | 59fb201 | 2020-06-01 11:56:50 -0400 | [diff] [blame] | 1019 | origin = origin_path->dentry; |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1020 | origin_path = NULL; |
| 1021 | } |
| 1022 | |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1023 | /* |
Vivek Goyal | 59fb201 | 2020-06-01 11:56:50 -0400 | [diff] [blame] | 1024 | * Always lookup index if there is no-upperdentry. |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1025 | * |
Vivek Goyal | 59fb201 | 2020-06-01 11:56:50 -0400 | [diff] [blame] | 1026 | * For the case of upperdentry, we have set origin by now if it |
| 1027 | * needed to be set. There are basically three cases. |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1028 | * |
Vivek Goyal | 59fb201 | 2020-06-01 11:56:50 -0400 | [diff] [blame] | 1029 | * For directories, lookup index by lower inode and verify it matches |
| 1030 | * upper inode. We only trust dir index if we verified that lower dir |
| 1031 | * matches origin, otherwise dir index entries may be inconsistent |
| 1032 | * and we ignore them. |
| 1033 | * |
| 1034 | * For regular upper, we already set origin if upper had ORIGIN |
| 1035 | * xattr. There is no verification though as there is no path |
| 1036 | * based dentry lookup in lower in this case. |
| 1037 | * |
| 1038 | * For metacopy upper, we set a verified origin already if index |
| 1039 | * is enabled and if upper had an ORIGIN xattr. |
| 1040 | * |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1041 | */ |
Vivek Goyal | 59fb201 | 2020-06-01 11:56:50 -0400 | [diff] [blame] | 1042 | if (!upperdentry && ctr) |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1043 | origin = stack[0].dentry; |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 1044 | |
Amir Goldstein | ad1d615 | 2018-01-11 10:47:03 +0200 | [diff] [blame] | 1045 | if (origin && ovl_indexdir(dentry->d_sb) && |
| 1046 | (!d.is_dir || ovl_index_all(dentry->d_sb))) { |
Amir Goldstein | 0617015 | 2018-01-17 14:40:27 +0200 | [diff] [blame] | 1047 | index = ovl_lookup_index(ofs, upperdentry, origin, true); |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 1048 | if (IS_ERR(index)) { |
| 1049 | err = PTR_ERR(index); |
| 1050 | index = NULL; |
| 1051 | goto out_put; |
| 1052 | } |
| 1053 | } |
| 1054 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1055 | oe = ovl_alloc_entry(ctr); |
| 1056 | err = -ENOMEM; |
| 1057 | if (!oe) |
| 1058 | goto out_put; |
| 1059 | |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 1060 | memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1061 | dentry->d_fsdata = oe; |
| 1062 | |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 1063 | if (upperopaque) |
| 1064 | ovl_dentry_set_opaque(dentry); |
| 1065 | |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 1066 | if (upperdentry) |
| 1067 | ovl_dentry_set_upper_alias(dentry); |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 1068 | else if (index) { |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 1069 | upperdentry = dget(index); |
Vivek Goyal | 0a2d0d3 | 2018-05-11 11:49:32 -0400 | [diff] [blame] | 1070 | upperredirect = ovl_get_redirect_xattr(upperdentry, 0); |
| 1071 | if (IS_ERR(upperredirect)) { |
| 1072 | err = PTR_ERR(upperredirect); |
| 1073 | upperredirect = NULL; |
| 1074 | goto out_free_oe; |
| 1075 | } |
| 1076 | } |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 1077 | |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1078 | if (upperdentry || ctr) { |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 1079 | struct ovl_inode_params oip = { |
| 1080 | .upperdentry = upperdentry, |
| 1081 | .lowerpath = stack, |
| 1082 | .index = index, |
| 1083 | .numlower = ctr, |
Vivek Goyal | 9cec54c | 2018-05-11 11:49:27 -0400 | [diff] [blame] | 1084 | .redirect = upperredirect, |
Vivek Goyal | 2664bd0 | 2018-05-11 11:49:30 -0400 | [diff] [blame] | 1085 | .lowerdata = (ctr > 1 && !d.is_dir) ? |
| 1086 | stack[ctr - 1].dentry : NULL, |
Vivek Goyal | ac6a52e | 2018-05-08 09:27:21 -0400 | [diff] [blame] | 1087 | }; |
| 1088 | |
| 1089 | inode = ovl_get_inode(dentry->d_sb, &oip); |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 1090 | err = PTR_ERR(inode); |
| 1091 | if (IS_ERR(inode)) |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1092 | goto out_free_oe; |
Vivek Goyal | 28166ab | 2020-06-01 11:56:52 -0400 | [diff] [blame] | 1093 | if (upperdentry && !uppermetacopy) |
| 1094 | ovl_set_flag(OVL_UPPERDATA, inode); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
Miklos Szeredi | f428884 | 2020-03-17 15:04:22 +0100 | [diff] [blame] | 1097 | ovl_dentry_update_reval(dentry, upperdentry, |
| 1098 | DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE); |
| 1099 | |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1100 | revert_creds(old_cred); |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1101 | if (origin_path) { |
| 1102 | dput(origin_path->dentry); |
| 1103 | kfree(origin_path); |
| 1104 | } |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 1105 | dput(index); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1106 | kfree(stack); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1107 | kfree(d.redirect); |
Amir Goldstein | 829c28b | 2017-09-29 21:43:07 +0300 | [diff] [blame] | 1108 | return d_splice_alias(inode, dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1109 | |
| 1110 | out_free_oe: |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1111 | dentry->d_fsdata = NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1112 | kfree(oe); |
| 1113 | out_put: |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 1114 | dput(index); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1115 | for (i = 0; i < ctr; i++) |
| 1116 | dput(stack[i].dentry); |
| 1117 | kfree(stack); |
| 1118 | out_put_upper: |
Vivek Goyal | 9d3dfea | 2018-05-11 11:49:28 -0400 | [diff] [blame] | 1119 | if (origin_path) { |
| 1120 | dput(origin_path->dentry); |
| 1121 | kfree(origin_path); |
| 1122 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1123 | dput(upperdentry); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1124 | kfree(upperredirect); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1125 | out: |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1126 | kfree(d.redirect); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1127 | revert_creds(old_cred); |
| 1128 | return ERR_PTR(err); |
| 1129 | } |
| 1130 | |
| 1131 | bool ovl_lower_positive(struct dentry *dentry) |
| 1132 | { |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1133 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
| 1134 | const struct qstr *name = &dentry->d_name; |
Amir Goldstein | 6d0a8a9 | 2017-11-10 13:18:07 +0200 | [diff] [blame] | 1135 | const struct cred *old_cred; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1136 | unsigned int i; |
| 1137 | bool positive = false; |
| 1138 | bool done = false; |
| 1139 | |
| 1140 | /* |
| 1141 | * If dentry is negative, then lower is positive iff this is a |
| 1142 | * whiteout. |
| 1143 | */ |
| 1144 | if (!dentry->d_inode) |
Amir Goldstein | c62520a | 2018-01-14 19:25:31 +0200 | [diff] [blame] | 1145 | return ovl_dentry_is_opaque(dentry); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1146 | |
| 1147 | /* Negative upper -> positive lower */ |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 1148 | if (!ovl_dentry_upper(dentry)) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1149 | return true; |
| 1150 | |
Amir Goldstein | 6d0a8a9 | 2017-11-10 13:18:07 +0200 | [diff] [blame] | 1151 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1152 | /* Positive upper -> have to look up lower to see whether it exists */ |
| 1153 | for (i = 0; !done && !positive && i < poe->numlower; i++) { |
| 1154 | struct dentry *this; |
| 1155 | struct dentry *lowerdir = poe->lowerstack[i].dentry; |
| 1156 | |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 1157 | this = lookup_positive_unlocked(name->name, lowerdir, |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1158 | name->len); |
| 1159 | if (IS_ERR(this)) { |
| 1160 | switch (PTR_ERR(this)) { |
| 1161 | case -ENOENT: |
| 1162 | case -ENAMETOOLONG: |
| 1163 | break; |
| 1164 | |
| 1165 | default: |
| 1166 | /* |
| 1167 | * Assume something is there, we just couldn't |
| 1168 | * access it. |
| 1169 | */ |
| 1170 | positive = true; |
| 1171 | break; |
| 1172 | } |
| 1173 | } else { |
Al Viro | 6c2d4798 | 2019-10-31 01:21:58 -0400 | [diff] [blame] | 1174 | positive = !ovl_is_whiteout(this); |
| 1175 | done = true; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1176 | dput(this); |
| 1177 | } |
| 1178 | } |
Amir Goldstein | 6d0a8a9 | 2017-11-10 13:18:07 +0200 | [diff] [blame] | 1179 | revert_creds(old_cred); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 1180 | |
| 1181 | return positive; |
| 1182 | } |