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