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