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