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> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 12 | #include <linux/namei.h> |
| 13 | #include <linux/xattr.h> |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 14 | #include <linux/ratelimit.h> |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 15 | #include <linux/mount.h> |
| 16 | #include <linux/exportfs.h> |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 17 | #include "overlayfs.h" |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 18 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 19 | struct ovl_lookup_data { |
| 20 | struct qstr name; |
| 21 | bool is_dir; |
| 22 | bool opaque; |
| 23 | bool stop; |
| 24 | bool last; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 25 | char *redirect; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 26 | }; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 27 | |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 28 | static int ovl_check_redirect(struct dentry *dentry, struct ovl_lookup_data *d, |
| 29 | size_t prelen, const char *post) |
| 30 | { |
| 31 | int res; |
| 32 | char *s, *next, *buf = NULL; |
| 33 | |
| 34 | res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, NULL, 0); |
| 35 | if (res < 0) { |
| 36 | if (res == -ENODATA || res == -EOPNOTSUPP) |
| 37 | return 0; |
| 38 | goto fail; |
| 39 | } |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 40 | buf = kzalloc(prelen + res + strlen(post) + 1, GFP_KERNEL); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 41 | if (!buf) |
| 42 | return -ENOMEM; |
| 43 | |
| 44 | if (res == 0) |
| 45 | goto invalid; |
| 46 | |
| 47 | res = vfs_getxattr(dentry, OVL_XATTR_REDIRECT, buf, res); |
| 48 | if (res < 0) |
| 49 | goto fail; |
| 50 | if (res == 0) |
| 51 | goto invalid; |
| 52 | if (buf[0] == '/') { |
| 53 | for (s = buf; *s++ == '/'; s = next) { |
| 54 | next = strchrnul(s, '/'); |
| 55 | if (s == next) |
| 56 | goto invalid; |
| 57 | } |
| 58 | } else { |
| 59 | if (strchr(buf, '/') != NULL) |
| 60 | goto invalid; |
| 61 | |
| 62 | memmove(buf + prelen, buf, res); |
| 63 | memcpy(buf, d->name.name, prelen); |
| 64 | } |
| 65 | |
| 66 | strcat(buf, post); |
| 67 | kfree(d->redirect); |
| 68 | d->redirect = buf; |
| 69 | d->name.name = d->redirect; |
| 70 | d->name.len = strlen(d->redirect); |
| 71 | |
| 72 | return 0; |
| 73 | |
| 74 | err_free: |
| 75 | kfree(buf); |
| 76 | return 0; |
| 77 | fail: |
| 78 | pr_warn_ratelimited("overlayfs: failed to get redirect (%i)\n", res); |
| 79 | goto err_free; |
| 80 | invalid: |
| 81 | pr_warn_ratelimited("overlayfs: invalid redirect (%s)\n", buf); |
| 82 | goto err_free; |
| 83 | } |
| 84 | |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 85 | static int ovl_acceptable(void *ctx, struct dentry *dentry) |
| 86 | { |
| 87 | return 1; |
| 88 | } |
| 89 | |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 90 | static struct ovl_fh *ovl_get_origin_fh(struct dentry *dentry) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 91 | { |
| 92 | int res; |
| 93 | struct ovl_fh *fh = NULL; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 94 | |
| 95 | res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, NULL, 0); |
| 96 | if (res < 0) { |
| 97 | if (res == -ENODATA || res == -EOPNOTSUPP) |
| 98 | return NULL; |
| 99 | goto fail; |
| 100 | } |
| 101 | /* Zero size value means "copied up but origin unknown" */ |
| 102 | if (res == 0) |
| 103 | return NULL; |
| 104 | |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 105 | fh = kzalloc(res, GFP_KERNEL); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 106 | if (!fh) |
| 107 | return ERR_PTR(-ENOMEM); |
| 108 | |
| 109 | res = vfs_getxattr(dentry, OVL_XATTR_ORIGIN, fh, res); |
| 110 | if (res < 0) |
| 111 | goto fail; |
| 112 | |
| 113 | if (res < sizeof(struct ovl_fh) || res < fh->len) |
| 114 | goto invalid; |
| 115 | |
| 116 | if (fh->magic != OVL_FH_MAGIC) |
| 117 | goto invalid; |
| 118 | |
| 119 | /* Treat larger version and unknown flags as "origin unknown" */ |
| 120 | if (fh->version > OVL_FH_VERSION || fh->flags & ~OVL_FH_FLAG_ALL) |
| 121 | goto out; |
| 122 | |
| 123 | /* Treat endianness mismatch as "origin unknown" */ |
| 124 | if (!(fh->flags & OVL_FH_FLAG_ANY_ENDIAN) && |
| 125 | (fh->flags & OVL_FH_FLAG_BIG_ENDIAN) != OVL_FH_FLAG_CPU_ENDIAN) |
| 126 | goto out; |
| 127 | |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 128 | return fh; |
| 129 | |
| 130 | out: |
| 131 | kfree(fh); |
| 132 | return NULL; |
| 133 | |
| 134 | fail: |
| 135 | pr_warn_ratelimited("overlayfs: failed to get origin (%i)\n", res); |
| 136 | goto out; |
| 137 | invalid: |
| 138 | pr_warn_ratelimited("overlayfs: invalid origin (%*phN)\n", res, fh); |
| 139 | goto out; |
| 140 | } |
| 141 | |
| 142 | static struct dentry *ovl_get_origin(struct dentry *dentry, |
| 143 | struct vfsmount *mnt) |
| 144 | { |
| 145 | struct dentry *origin = NULL; |
| 146 | struct ovl_fh *fh = ovl_get_origin_fh(dentry); |
| 147 | int bytes; |
| 148 | |
| 149 | if (IS_ERR_OR_NULL(fh)) |
| 150 | return (struct dentry *)fh; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 151 | |
| 152 | /* |
| 153 | * Make sure that the stored uuid matches the uuid of the lower |
| 154 | * layer where file handle will be decoded. |
| 155 | */ |
Christoph Hellwig | 8578709 | 2017-05-10 15:06:33 +0200 | [diff] [blame] | 156 | if (!uuid_equal(&fh->uuid, &mnt->mnt_sb->s_uuid)) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 157 | goto out; |
| 158 | |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 159 | bytes = (fh->len - offsetof(struct ovl_fh, fid)); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 160 | origin = exportfs_decode_fh(mnt, (struct fid *)fh->fid, |
| 161 | bytes >> 2, (int)fh->type, |
| 162 | ovl_acceptable, NULL); |
| 163 | if (IS_ERR(origin)) { |
| 164 | /* Treat stale file handle as "origin unknown" */ |
| 165 | if (origin == ERR_PTR(-ESTALE)) |
| 166 | origin = NULL; |
| 167 | goto out; |
| 168 | } |
| 169 | |
| 170 | if (ovl_dentry_weird(origin) || |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 171 | ((d_inode(origin)->i_mode ^ d_inode(dentry)->i_mode) & S_IFMT)) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 172 | goto invalid; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 173 | |
| 174 | out: |
| 175 | kfree(fh); |
| 176 | return origin; |
| 177 | |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 178 | invalid: |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 179 | pr_warn_ratelimited("overlayfs: invalid origin (%pd2)\n", origin); |
| 180 | dput(origin); |
| 181 | origin = NULL; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 182 | goto out; |
| 183 | } |
| 184 | |
Amir Goldstein | ee1d6d37 | 2017-05-11 16:42:26 +0300 | [diff] [blame] | 185 | static bool ovl_is_opaquedir(struct dentry *dentry) |
| 186 | { |
| 187 | return ovl_check_dir_xattr(dentry, OVL_XATTR_OPAQUE); |
| 188 | } |
| 189 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 190 | static int ovl_lookup_single(struct dentry *base, struct ovl_lookup_data *d, |
| 191 | const char *name, unsigned int namelen, |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 192 | size_t prelen, const char *post, |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 193 | struct dentry **ret) |
| 194 | { |
| 195 | struct dentry *this; |
| 196 | int err; |
| 197 | |
| 198 | this = lookup_one_len_unlocked(name, base, namelen); |
| 199 | if (IS_ERR(this)) { |
| 200 | err = PTR_ERR(this); |
| 201 | this = NULL; |
| 202 | if (err == -ENOENT || err == -ENAMETOOLONG) |
| 203 | goto out; |
| 204 | goto out_err; |
| 205 | } |
| 206 | if (!this->d_inode) |
| 207 | goto put_and_out; |
| 208 | |
| 209 | if (ovl_dentry_weird(this)) { |
| 210 | /* Don't support traversing automounts and other weirdness */ |
| 211 | err = -EREMOTE; |
| 212 | goto out_err; |
| 213 | } |
| 214 | if (ovl_is_whiteout(this)) { |
| 215 | d->stop = d->opaque = true; |
| 216 | goto put_and_out; |
| 217 | } |
| 218 | if (!d_can_lookup(this)) { |
| 219 | d->stop = true; |
| 220 | if (d->is_dir) |
| 221 | goto put_and_out; |
| 222 | goto out; |
| 223 | } |
| 224 | d->is_dir = true; |
| 225 | if (!d->last && ovl_is_opaquedir(this)) { |
| 226 | d->stop = d->opaque = true; |
| 227 | goto out; |
| 228 | } |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 229 | err = ovl_check_redirect(this, d, prelen, post); |
| 230 | if (err) |
| 231 | goto out_err; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 232 | out: |
| 233 | *ret = this; |
| 234 | return 0; |
| 235 | |
| 236 | put_and_out: |
| 237 | dput(this); |
| 238 | this = NULL; |
| 239 | goto out; |
| 240 | |
| 241 | out_err: |
| 242 | dput(this); |
| 243 | return err; |
| 244 | } |
| 245 | |
| 246 | static int ovl_lookup_layer(struct dentry *base, struct ovl_lookup_data *d, |
| 247 | struct dentry **ret) |
| 248 | { |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 249 | /* Counting down from the end, since the prefix can change */ |
| 250 | size_t rem = d->name.len - 1; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 251 | struct dentry *dentry = NULL; |
| 252 | int err; |
| 253 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 254 | if (d->name.name[0] != '/') |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 255 | return ovl_lookup_single(base, d, d->name.name, d->name.len, |
| 256 | 0, "", ret); |
| 257 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 258 | while (!IS_ERR_OR_NULL(base) && d_can_lookup(base)) { |
| 259 | const char *s = d->name.name + d->name.len - rem; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 260 | const char *next = strchrnul(s, '/'); |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 261 | size_t thislen = next - s; |
| 262 | bool end = !next[0]; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 263 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 264 | /* Verify we did not go off the rails */ |
| 265 | if (WARN_ON(s[-1] != '/')) |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 266 | return -EIO; |
| 267 | |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 268 | err = ovl_lookup_single(base, d, s, thislen, |
| 269 | d->name.len - rem, next, &base); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 270 | dput(dentry); |
| 271 | if (err) |
| 272 | return err; |
| 273 | dentry = base; |
Amir Goldstein | 4c7d0c9 | 2017-01-18 15:19:54 +0100 | [diff] [blame] | 274 | if (end) |
| 275 | break; |
| 276 | |
| 277 | rem -= thislen + 1; |
| 278 | |
| 279 | if (WARN_ON(rem >= d->name.len)) |
| 280 | return -EIO; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 281 | } |
| 282 | *ret = dentry; |
| 283 | return 0; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 286 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 287 | static int ovl_check_origin(struct dentry *upperdentry, |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 288 | struct ovl_path *lower, unsigned int numlower, |
| 289 | struct ovl_path **stackp, unsigned int *ctrp) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 290 | { |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 291 | struct vfsmount *mnt; |
Amir Goldstein | f7d3dac | 2017-06-21 15:28:34 +0300 | [diff] [blame] | 292 | struct dentry *origin = NULL; |
| 293 | int i; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 294 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 295 | for (i = 0; i < numlower; i++) { |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 296 | mnt = lower[i].layer->mnt; |
Amir Goldstein | f7d3dac | 2017-06-21 15:28:34 +0300 | [diff] [blame] | 297 | origin = ovl_get_origin(upperdentry, mnt); |
| 298 | if (IS_ERR(origin)) |
| 299 | return PTR_ERR(origin); |
| 300 | |
| 301 | if (origin) |
| 302 | break; |
| 303 | } |
| 304 | |
| 305 | if (!origin) |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 306 | return 0; |
| 307 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 308 | BUG_ON(*ctrp); |
| 309 | if (!*stackp) |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 310 | *stackp = kmalloc(sizeof(struct ovl_path), GFP_KERNEL); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 311 | if (!*stackp) { |
| 312 | dput(origin); |
| 313 | return -ENOMEM; |
| 314 | } |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 315 | **stackp = (struct ovl_path){.dentry = origin, .layer = lower[i].layer}; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 316 | *ctrp = 1; |
| 317 | |
| 318 | return 0; |
| 319 | } |
| 320 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 321 | /* |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 322 | * Verify that @fh matches the origin file handle stored in OVL_XATTR_ORIGIN. |
| 323 | * Return 0 on match, -ESTALE on mismatch, < 0 on error. |
| 324 | */ |
| 325 | static int ovl_verify_origin_fh(struct dentry *dentry, const struct ovl_fh *fh) |
| 326 | { |
| 327 | struct ovl_fh *ofh = ovl_get_origin_fh(dentry); |
| 328 | int err = 0; |
| 329 | |
| 330 | if (!ofh) |
| 331 | return -ENODATA; |
| 332 | |
| 333 | if (IS_ERR(ofh)) |
| 334 | return PTR_ERR(ofh); |
| 335 | |
| 336 | if (fh->len != ofh->len || memcmp(fh, ofh, fh->len)) |
| 337 | err = -ESTALE; |
| 338 | |
| 339 | kfree(ofh); |
| 340 | return err; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Verify that an inode matches the origin file handle stored in upper inode. |
| 345 | * |
| 346 | * If @set is true and there is no stored file handle, encode and store origin |
| 347 | * file handle in OVL_XATTR_ORIGIN. |
| 348 | * |
| 349 | * Return 0 on match, -ESTALE on mismatch, < 0 on error. |
| 350 | */ |
Amir Goldstein | d976807 | 2017-09-24 13:00:19 +0300 | [diff] [blame] | 351 | int ovl_verify_origin(struct dentry *dentry, struct dentry *origin, |
| 352 | bool is_upper, bool set) |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 353 | { |
| 354 | struct inode *inode; |
| 355 | struct ovl_fh *fh; |
| 356 | int err; |
| 357 | |
Amir Goldstein | 54fb347 | 2017-06-21 15:28:38 +0300 | [diff] [blame] | 358 | fh = ovl_encode_fh(origin, is_upper); |
Amir Goldstein | 8b88a2e | 2017-06-21 15:28:37 +0300 | [diff] [blame] | 359 | err = PTR_ERR(fh); |
| 360 | if (IS_ERR(fh)) |
| 361 | goto fail; |
| 362 | |
| 363 | err = ovl_verify_origin_fh(dentry, fh); |
| 364 | if (set && err == -ENODATA) |
| 365 | err = ovl_do_setxattr(dentry, OVL_XATTR_ORIGIN, fh, fh->len, 0); |
| 366 | if (err) |
| 367 | goto fail; |
| 368 | |
| 369 | out: |
| 370 | kfree(fh); |
| 371 | return err; |
| 372 | |
| 373 | fail: |
| 374 | inode = d_inode(origin); |
| 375 | pr_warn_ratelimited("overlayfs: failed to verify origin (%pd2, ino=%lu, err=%i)\n", |
| 376 | origin, inode ? inode->i_ino : 0, err); |
| 377 | goto out; |
| 378 | } |
| 379 | |
| 380 | /* |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 381 | * Verify that an index entry name matches the origin file handle stored in |
| 382 | * OVL_XATTR_ORIGIN and that origin file handle can be decoded to lower path. |
| 383 | * Return 0 on match, -ESTALE on mismatch or stale origin, < 0 on error. |
| 384 | */ |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 385 | int ovl_verify_index(struct dentry *index, struct ovl_path *lower, |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 386 | unsigned int numlower) |
| 387 | { |
| 388 | struct ovl_fh *fh = NULL; |
| 389 | size_t len; |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 390 | struct ovl_path origin = { }; |
| 391 | struct ovl_path *stack = &origin; |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 392 | unsigned int ctr = 0; |
| 393 | int err; |
| 394 | |
| 395 | if (!d_inode(index)) |
| 396 | return 0; |
| 397 | |
Amir Goldstein | 61b6747 | 2017-07-18 21:07:42 +0300 | [diff] [blame] | 398 | /* |
| 399 | * Directory index entries are going to be used for looking up |
| 400 | * redirected upper dirs by lower dir fh when decoding an overlay |
| 401 | * file handle of a merge dir. Whiteout index entries are going to be |
| 402 | * used as an indication that an exported overlay file handle should |
| 403 | * be treated as stale (i.e. after unlink of the overlay inode). |
| 404 | * We don't know the verification rules for directory and whiteout |
| 405 | * index entries, because they have not been implemented yet, so return |
Amir Goldstein | fa0096e | 2017-10-24 12:24:11 +0300 | [diff] [blame] | 406 | * EINVAL if those entries are found to abort the mount to avoid |
| 407 | * corrupting an index that was created by a newer kernel. |
Amir Goldstein | 61b6747 | 2017-07-18 21:07:42 +0300 | [diff] [blame] | 408 | */ |
Amir Goldstein | fa0096e | 2017-10-24 12:24:11 +0300 | [diff] [blame] | 409 | err = -EINVAL; |
Amir Goldstein | 61b6747 | 2017-07-18 21:07:42 +0300 | [diff] [blame] | 410 | if (d_is_dir(index) || ovl_is_whiteout(index)) |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 411 | goto fail; |
| 412 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 413 | if (index->d_name.len < sizeof(struct ovl_fh)*2) |
| 414 | goto fail; |
| 415 | |
| 416 | err = -ENOMEM; |
| 417 | len = index->d_name.len / 2; |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 418 | fh = kzalloc(len, GFP_KERNEL); |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 419 | if (!fh) |
| 420 | goto fail; |
| 421 | |
| 422 | err = -EINVAL; |
| 423 | if (hex2bin((u8 *)fh, index->d_name.name, len) || len != fh->len) |
| 424 | goto fail; |
| 425 | |
| 426 | err = ovl_verify_origin_fh(index, fh); |
| 427 | if (err) |
| 428 | goto fail; |
| 429 | |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 430 | err = ovl_check_origin(index, lower, numlower, &stack, &ctr); |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 431 | if (!err && !ctr) |
| 432 | err = -ESTALE; |
| 433 | if (err) |
| 434 | goto fail; |
| 435 | |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 436 | /* Check if index is orphan and don't warn before cleaning it */ |
| 437 | if (d_inode(index)->i_nlink == 1 && |
Vivek Goyal | 08d8f8a | 2017-11-27 10:12:44 -0500 | [diff] [blame] | 438 | ovl_get_nlink(origin.dentry, index, 0) == 0) |
Amir Goldstein | caf70cb | 2017-06-21 13:46:12 +0300 | [diff] [blame] | 439 | err = -ENOENT; |
| 440 | |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 441 | dput(origin.dentry); |
| 442 | out: |
| 443 | kfree(fh); |
| 444 | return err; |
| 445 | |
| 446 | fail: |
Amir Goldstein | 61b6747 | 2017-07-18 21:07:42 +0300 | [diff] [blame] | 447 | pr_warn_ratelimited("overlayfs: failed to verify index (%pd2, ftype=%x, err=%i)\n", |
| 448 | index, d_inode(index)->i_mode & S_IFMT, err); |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 449 | goto out; |
| 450 | } |
| 451 | |
| 452 | /* |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 453 | * Lookup in indexdir for the index entry of a lower real inode or a copy up |
| 454 | * origin inode. The index entry name is the hex representation of the lower |
| 455 | * inode file handle. |
| 456 | * |
| 457 | * If the index dentry in negative, then either no lower aliases have been |
| 458 | * copied up yet, or aliases have been copied up in older kernels and are |
| 459 | * not indexed. |
| 460 | * |
| 461 | * If the index dentry for a copy up origin inode is positive, but points |
| 462 | * to an inode different than the upper inode, then either the upper inode |
| 463 | * has been copied up and not indexed or it was indexed, but since then |
| 464 | * index dir was cleared. Either way, that index cannot be used to indentify |
| 465 | * the overlay inode. |
| 466 | */ |
| 467 | int ovl_get_index_name(struct dentry *origin, struct qstr *name) |
| 468 | { |
| 469 | int err; |
| 470 | struct ovl_fh *fh; |
| 471 | char *n, *s; |
| 472 | |
| 473 | fh = ovl_encode_fh(origin, false); |
| 474 | if (IS_ERR(fh)) |
| 475 | return PTR_ERR(fh); |
| 476 | |
| 477 | err = -ENOMEM; |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 478 | n = kzalloc(fh->len * 2, GFP_KERNEL); |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 479 | if (n) { |
| 480 | s = bin2hex(n, fh, fh->len); |
| 481 | *name = (struct qstr) QSTR_INIT(n, s - n); |
| 482 | err = 0; |
| 483 | } |
| 484 | kfree(fh); |
| 485 | |
| 486 | return err; |
| 487 | |
| 488 | } |
| 489 | |
| 490 | static struct dentry *ovl_lookup_index(struct dentry *dentry, |
| 491 | struct dentry *upper, |
| 492 | struct dentry *origin) |
| 493 | { |
| 494 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 495 | struct dentry *index; |
| 496 | struct inode *inode; |
| 497 | struct qstr name; |
| 498 | int err; |
| 499 | |
| 500 | err = ovl_get_index_name(origin, &name); |
| 501 | if (err) |
| 502 | return ERR_PTR(err); |
| 503 | |
| 504 | index = lookup_one_len_unlocked(name.name, ofs->indexdir, name.len); |
| 505 | if (IS_ERR(index)) { |
Amir Goldstein | e0082a0 | 2017-09-24 13:01:35 +0300 | [diff] [blame] | 506 | err = PTR_ERR(index); |
Amir Goldstein | 7937a56 | 2017-10-20 17:19:06 +0300 | [diff] [blame] | 507 | if (err == -ENOENT) { |
| 508 | index = NULL; |
| 509 | goto out; |
| 510 | } |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 511 | pr_warn_ratelimited("overlayfs: failed inode index lookup (ino=%lu, key=%*s, err=%i);\n" |
| 512 | "overlayfs: mount with '-o index=off' to disable inodes index.\n", |
| 513 | d_inode(origin)->i_ino, name.len, name.name, |
| 514 | err); |
| 515 | goto out; |
| 516 | } |
| 517 | |
Amir Goldstein | 0e08255 | 2017-07-18 21:07:43 +0300 | [diff] [blame] | 518 | inode = d_inode(index); |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 519 | if (d_is_negative(index)) { |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 520 | goto out_dput; |
Amir Goldstein | 0e08255 | 2017-07-18 21:07:43 +0300 | [diff] [blame] | 521 | } else if (upper && d_inode(upper) != inode) { |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 522 | goto out_dput; |
Amir Goldstein | 0e08255 | 2017-07-18 21:07:43 +0300 | [diff] [blame] | 523 | } else if (ovl_dentry_weird(index) || ovl_is_whiteout(index) || |
| 524 | ((inode->i_mode ^ d_inode(origin)->i_mode) & S_IFMT)) { |
| 525 | /* |
| 526 | * Index should always be of the same file type as origin |
| 527 | * except for the case of a whiteout index. A whiteout |
| 528 | * index should only exist if all lower aliases have been |
| 529 | * unlinked, which means that finding a lower origin on lookup |
| 530 | * whose index is a whiteout should be treated as an error. |
| 531 | */ |
| 532 | pr_warn_ratelimited("overlayfs: bad index found (index=%pd2, ftype=%x, origin ftype=%x).\n", |
| 533 | index, d_inode(index)->i_mode & S_IFMT, |
| 534 | d_inode(origin)->i_mode & S_IFMT); |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 535 | goto fail; |
| 536 | } |
| 537 | |
| 538 | out: |
| 539 | kfree(name.name); |
| 540 | return index; |
| 541 | |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 542 | out_dput: |
| 543 | dput(index); |
| 544 | index = NULL; |
| 545 | goto out; |
| 546 | |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 547 | fail: |
| 548 | dput(index); |
| 549 | index = ERR_PTR(-EIO); |
| 550 | goto out; |
| 551 | } |
| 552 | |
| 553 | /* |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 554 | * Returns next layer in stack starting from top. |
| 555 | * Returns -1 if this is the last layer. |
| 556 | */ |
| 557 | int ovl_path_next(int idx, struct dentry *dentry, struct path *path) |
| 558 | { |
| 559 | struct ovl_entry *oe = dentry->d_fsdata; |
| 560 | |
| 561 | BUG_ON(idx < 0); |
| 562 | if (idx == 0) { |
| 563 | ovl_path_upper(dentry, path); |
| 564 | if (path->dentry) |
| 565 | return oe->numlower ? 1 : -1; |
| 566 | idx++; |
| 567 | } |
| 568 | BUG_ON(idx > oe->numlower); |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 569 | path->dentry = oe->lowerstack[idx - 1].dentry; |
| 570 | path->mnt = oe->lowerstack[idx - 1].layer->mnt; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 571 | |
| 572 | return (idx < oe->numlower) ? idx + 1 : -1; |
| 573 | } |
| 574 | |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 575 | static int ovl_find_layer(struct ovl_fs *ofs, struct ovl_path *path) |
| 576 | { |
| 577 | int i; |
| 578 | |
| 579 | for (i = 0; i < ofs->numlower; i++) { |
| 580 | if (ofs->lower_layers[i].mnt == path->layer->mnt) |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | return i; |
| 585 | } |
| 586 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 587 | struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 588 | unsigned int flags) |
| 589 | { |
| 590 | struct ovl_entry *oe; |
| 591 | const struct cred *old_cred; |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 592 | struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 593 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
Amir Goldstein | c22205d | 2017-04-26 23:40:52 +0300 | [diff] [blame] | 594 | struct ovl_entry *roe = dentry->d_sb->s_root->d_fsdata; |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 595 | struct ovl_path *stack = NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 596 | struct dentry *upperdir, *upperdentry = NULL; |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 597 | struct dentry *index = NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 598 | unsigned int ctr = 0; |
| 599 | struct inode *inode = NULL; |
| 600 | bool upperopaque = false; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 601 | char *upperredirect = NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 602 | struct dentry *this; |
| 603 | unsigned int i; |
| 604 | int err; |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 605 | struct ovl_lookup_data d = { |
| 606 | .name = dentry->d_name, |
| 607 | .is_dir = false, |
| 608 | .opaque = false, |
| 609 | .stop = false, |
| 610 | .last = !poe->numlower, |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 611 | .redirect = NULL, |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 612 | }; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 613 | |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 614 | if (dentry->d_name.len > ofs->namelen) |
| 615 | return ERR_PTR(-ENAMETOOLONG); |
| 616 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 617 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 618 | upperdir = ovl_dentry_upper(dentry->d_parent); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 619 | if (upperdir) { |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 620 | err = ovl_lookup_layer(upperdir, &d, &upperdentry); |
| 621 | if (err) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 622 | goto out; |
| 623 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 624 | if (upperdentry && unlikely(ovl_dentry_remote(upperdentry))) { |
| 625 | dput(upperdentry); |
| 626 | err = -EREMOTE; |
| 627 | goto out; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 628 | } |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 629 | if (upperdentry && !d.is_dir) { |
| 630 | BUG_ON(!d.stop || d.redirect); |
Amir Goldstein | f7d3dac | 2017-06-21 15:28:34 +0300 | [diff] [blame] | 631 | /* |
| 632 | * Lookup copy up origin by decoding origin file handle. |
| 633 | * We may get a disconnected dentry, which is fine, |
| 634 | * because we only need to hold the origin inode in |
| 635 | * cache and use its inode number. We may even get a |
| 636 | * connected dentry, that is not under any of the lower |
| 637 | * layers root. That is also fine for using it's inode |
| 638 | * number - it's the same as if we held a reference |
| 639 | * to a dentry in lower layer that was moved under us. |
| 640 | */ |
Amir Goldstein | 415543d | 2017-06-21 15:28:42 +0300 | [diff] [blame] | 641 | err = ovl_check_origin(upperdentry, roe->lowerstack, |
| 642 | roe->numlower, &stack, &ctr); |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 643 | if (err) |
Vivek Goyal | 5455f92 | 2017-11-01 15:37:22 -0400 | [diff] [blame] | 644 | goto out_put_upper; |
Amir Goldstein | a9d0195 | 2017-04-30 14:46:31 +0300 | [diff] [blame] | 645 | } |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 646 | |
| 647 | if (d.redirect) { |
Dan Carpenter | 0ce5cdc | 2017-09-22 23:45:18 +0300 | [diff] [blame] | 648 | err = -ENOMEM; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 649 | upperredirect = kstrdup(d.redirect, GFP_KERNEL); |
| 650 | if (!upperredirect) |
| 651 | goto out_put_upper; |
| 652 | if (d.redirect[0] == '/') |
Amir Goldstein | c22205d | 2017-04-26 23:40:52 +0300 | [diff] [blame] | 653 | poe = roe; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 654 | } |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 655 | upperopaque = d.opaque; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 656 | } |
| 657 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 658 | if (!d.stop && poe->numlower) { |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 659 | err = -ENOMEM; |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 660 | stack = kcalloc(ofs->numlower, sizeof(struct ovl_path), |
Michal Hocko | 0ee931c | 2017-09-13 16:28:29 -0700 | [diff] [blame] | 661 | GFP_KERNEL); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 662 | if (!stack) |
| 663 | goto out_put_upper; |
| 664 | } |
| 665 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 666 | for (i = 0; !d.stop && i < poe->numlower; i++) { |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 667 | struct ovl_path lower = poe->lowerstack[i]; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 668 | |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 669 | d.last = i == poe->numlower - 1; |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 670 | err = ovl_lookup_layer(lower.dentry, &d, &this); |
Miklos Szeredi | e28edc4 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 671 | if (err) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 672 | goto out_put; |
Miklos Szeredi | 6b2d5fe | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 673 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 674 | if (!this) |
| 675 | continue; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 676 | |
| 677 | stack[ctr].dentry = this; |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 678 | stack[ctr].layer = lower.layer; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 679 | ctr++; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 680 | |
| 681 | if (d.stop) |
| 682 | break; |
| 683 | |
Miklos Szeredi | 438c84c | 2017-12-11 11:28:10 +0100 | [diff] [blame] | 684 | /* |
| 685 | * Following redirects can have security consequences: it's like |
| 686 | * a symlink into the lower layer without the permission checks. |
| 687 | * This is only a problem if the upper layer is untrusted (e.g |
| 688 | * comes from an USB drive). This can allow a non-readable file |
| 689 | * or directory to become readable. |
| 690 | * |
| 691 | * Only following redirects when redirects are enabled disables |
| 692 | * this attack vector when not necessary. |
| 693 | */ |
| 694 | err = -EPERM; |
| 695 | if (d.redirect && !ofs->config.redirect_follow) { |
| 696 | pr_warn_ratelimited("overlay: refusing to follow redirect for (%pd2)\n", dentry); |
| 697 | goto out_put; |
| 698 | } |
| 699 | |
Amir Goldstein | c22205d | 2017-04-26 23:40:52 +0300 | [diff] [blame] | 700 | if (d.redirect && d.redirect[0] == '/' && poe != roe) { |
| 701 | poe = roe; |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 702 | |
| 703 | /* Find the current layer on the root dentry */ |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 704 | i = ovl_find_layer(ofs, &lower); |
| 705 | if (WARN_ON(i == ofs->numlower)) |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 706 | break; |
| 707 | } |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 708 | } |
| 709 | |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 710 | /* Lookup index by lower inode and verify it matches upper inode */ |
| 711 | if (ctr && !d.is_dir && ovl_indexdir(dentry->d_sb)) { |
| 712 | struct dentry *origin = stack[0].dentry; |
| 713 | |
| 714 | index = ovl_lookup_index(dentry, upperdentry, origin); |
| 715 | if (IS_ERR(index)) { |
| 716 | err = PTR_ERR(index); |
| 717 | index = NULL; |
| 718 | goto out_put; |
| 719 | } |
| 720 | } |
| 721 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 722 | oe = ovl_alloc_entry(ctr); |
| 723 | err = -ENOMEM; |
| 724 | if (!oe) |
| 725 | goto out_put; |
| 726 | |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 727 | oe->opaque = upperopaque; |
Chandan Rajendra | b934363 | 2017-07-24 01:57:54 -0500 | [diff] [blame] | 728 | memcpy(oe->lowerstack, stack, sizeof(struct ovl_path) * ctr); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 729 | dentry->d_fsdata = oe; |
| 730 | |
Miklos Szeredi | 55acc66 | 2017-07-04 22:03:18 +0200 | [diff] [blame] | 731 | if (upperdentry) |
| 732 | ovl_dentry_set_upper_alias(dentry); |
| 733 | else if (index) |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 734 | upperdentry = dget(index); |
| 735 | |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 736 | if (upperdentry || ctr) { |
Amir Goldstein | 6eaf011 | 2017-10-12 19:03:04 +0300 | [diff] [blame] | 737 | inode = ovl_get_inode(dentry, upperdentry, index); |
Miklos Szeredi | b9ac5c27 | 2017-07-04 22:03:17 +0200 | [diff] [blame] | 738 | err = PTR_ERR(inode); |
| 739 | if (IS_ERR(inode)) |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 740 | goto out_free_oe; |
Miklos Szeredi | cf31c46 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 741 | |
| 742 | OVL_I(inode)->redirect = upperredirect; |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 743 | if (index) |
| 744 | ovl_set_flag(OVL_INDEX, inode); |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | revert_creds(old_cred); |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 748 | dput(index); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 749 | kfree(stack); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 750 | kfree(d.redirect); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 751 | d_add(dentry, inode); |
| 752 | |
| 753 | return NULL; |
| 754 | |
| 755 | out_free_oe: |
Miklos Szeredi | e6d2ebd | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 756 | dentry->d_fsdata = NULL; |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 757 | kfree(oe); |
| 758 | out_put: |
Amir Goldstein | 359f392 | 2017-06-21 15:28:41 +0300 | [diff] [blame] | 759 | dput(index); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 760 | for (i = 0; i < ctr; i++) |
| 761 | dput(stack[i].dentry); |
| 762 | kfree(stack); |
| 763 | out_put_upper: |
| 764 | dput(upperdentry); |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 765 | kfree(upperredirect); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 766 | out: |
Miklos Szeredi | 02b69b2 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 767 | kfree(d.redirect); |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 768 | revert_creds(old_cred); |
| 769 | return ERR_PTR(err); |
| 770 | } |
| 771 | |
| 772 | bool ovl_lower_positive(struct dentry *dentry) |
| 773 | { |
| 774 | struct ovl_entry *oe = dentry->d_fsdata; |
| 775 | struct ovl_entry *poe = dentry->d_parent->d_fsdata; |
| 776 | const struct qstr *name = &dentry->d_name; |
| 777 | unsigned int i; |
| 778 | bool positive = false; |
| 779 | bool done = false; |
| 780 | |
| 781 | /* |
| 782 | * If dentry is negative, then lower is positive iff this is a |
| 783 | * whiteout. |
| 784 | */ |
| 785 | if (!dentry->d_inode) |
| 786 | return oe->opaque; |
| 787 | |
| 788 | /* Negative upper -> positive lower */ |
Miklos Szeredi | 09d8b58 | 2017-07-04 22:03:16 +0200 | [diff] [blame] | 789 | if (!ovl_dentry_upper(dentry)) |
Miklos Szeredi | bbb1e54 | 2016-12-16 11:02:56 +0100 | [diff] [blame] | 790 | return true; |
| 791 | |
| 792 | /* Positive upper -> have to look up lower to see whether it exists */ |
| 793 | for (i = 0; !done && !positive && i < poe->numlower; i++) { |
| 794 | struct dentry *this; |
| 795 | struct dentry *lowerdir = poe->lowerstack[i].dentry; |
| 796 | |
| 797 | this = lookup_one_len_unlocked(name->name, lowerdir, |
| 798 | name->len); |
| 799 | if (IS_ERR(this)) { |
| 800 | switch (PTR_ERR(this)) { |
| 801 | case -ENOENT: |
| 802 | case -ENAMETOOLONG: |
| 803 | break; |
| 804 | |
| 805 | default: |
| 806 | /* |
| 807 | * Assume something is there, we just couldn't |
| 808 | * access it. |
| 809 | */ |
| 810 | positive = true; |
| 811 | break; |
| 812 | } |
| 813 | } else { |
| 814 | if (this->d_inode) { |
| 815 | positive = !ovl_is_whiteout(this); |
| 816 | done = true; |
| 817 | } |
| 818 | dput(this); |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | return positive; |
| 823 | } |