Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2011 Novell 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> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/xattr.h> |
| 13 | #include "overlayfs.h" |
| 14 | |
| 15 | static int ovl_copy_up_last(struct dentry *dentry, struct iattr *attr, |
| 16 | bool no_data) |
| 17 | { |
| 18 | int err; |
| 19 | struct dentry *parent; |
| 20 | struct kstat stat; |
| 21 | struct path lowerpath; |
| 22 | |
| 23 | parent = dget_parent(dentry); |
| 24 | err = ovl_copy_up(parent); |
| 25 | if (err) |
| 26 | goto out_dput_parent; |
| 27 | |
| 28 | ovl_path_lower(dentry, &lowerpath); |
| 29 | err = vfs_getattr(&lowerpath, &stat); |
| 30 | if (err) |
| 31 | goto out_dput_parent; |
| 32 | |
| 33 | if (no_data) |
| 34 | stat.size = 0; |
| 35 | |
| 36 | err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat, attr); |
| 37 | |
| 38 | out_dput_parent: |
| 39 | dput(parent); |
| 40 | return err; |
| 41 | } |
| 42 | |
| 43 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 44 | { |
| 45 | int err; |
| 46 | struct dentry *upperdentry; |
| 47 | |
| 48 | err = ovl_want_write(dentry); |
| 49 | if (err) |
| 50 | goto out; |
| 51 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame^] | 52 | err = ovl_copy_up(dentry); |
| 53 | if (!err) { |
| 54 | upperdentry = ovl_dentry_upper(dentry); |
| 55 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 56 | mutex_lock(&upperdentry->d_inode->i_mutex); |
| 57 | err = notify_change(upperdentry, attr, NULL); |
| 58 | mutex_unlock(&upperdentry->d_inode->i_mutex); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 59 | } |
| 60 | ovl_drop_write(dentry); |
| 61 | out: |
| 62 | return err; |
| 63 | } |
| 64 | |
| 65 | static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 66 | struct kstat *stat) |
| 67 | { |
| 68 | struct path realpath; |
| 69 | |
| 70 | ovl_path_real(dentry, &realpath); |
| 71 | return vfs_getattr(&realpath, stat); |
| 72 | } |
| 73 | |
| 74 | int ovl_permission(struct inode *inode, int mask) |
| 75 | { |
| 76 | struct ovl_entry *oe; |
| 77 | struct dentry *alias = NULL; |
| 78 | struct inode *realinode; |
| 79 | struct dentry *realdentry; |
| 80 | bool is_upper; |
| 81 | int err; |
| 82 | |
| 83 | if (S_ISDIR(inode->i_mode)) { |
| 84 | oe = inode->i_private; |
| 85 | } else if (mask & MAY_NOT_BLOCK) { |
| 86 | return -ECHILD; |
| 87 | } else { |
| 88 | /* |
| 89 | * For non-directories find an alias and get the info |
| 90 | * from there. |
| 91 | */ |
| 92 | alias = d_find_any_alias(inode); |
| 93 | if (WARN_ON(!alias)) |
| 94 | return -ENOENT; |
| 95 | |
| 96 | oe = alias->d_fsdata; |
| 97 | } |
| 98 | |
| 99 | realdentry = ovl_entry_real(oe, &is_upper); |
| 100 | |
| 101 | /* Careful in RCU walk mode */ |
| 102 | realinode = ACCESS_ONCE(realdentry->d_inode); |
| 103 | if (!realinode) { |
| 104 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
| 105 | err = -ENOENT; |
| 106 | goto out_dput; |
| 107 | } |
| 108 | |
| 109 | if (mask & MAY_WRITE) { |
| 110 | umode_t mode = realinode->i_mode; |
| 111 | |
| 112 | /* |
| 113 | * Writes will always be redirected to upper layer, so |
| 114 | * ignore lower layer being read-only. |
| 115 | * |
| 116 | * If the overlay itself is read-only then proceed |
| 117 | * with the permission check, don't return EROFS. |
| 118 | * This will only happen if this is the lower layer of |
| 119 | * another overlayfs. |
| 120 | * |
| 121 | * If upper fs becomes read-only after the overlay was |
| 122 | * constructed return EROFS to prevent modification of |
| 123 | * upper layer. |
| 124 | */ |
| 125 | err = -EROFS; |
| 126 | if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) && |
| 127 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 128 | goto out_dput; |
| 129 | } |
| 130 | |
| 131 | err = __inode_permission(realinode, mask); |
| 132 | out_dput: |
| 133 | dput(alias); |
| 134 | return err; |
| 135 | } |
| 136 | |
| 137 | |
| 138 | struct ovl_link_data { |
| 139 | struct dentry *realdentry; |
| 140 | void *cookie; |
| 141 | }; |
| 142 | |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 143 | static const char *ovl_follow_link(struct dentry *dentry, void **cookie) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 144 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 145 | struct dentry *realdentry; |
| 146 | struct inode *realinode; |
NeilBrown | 3188b29 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 147 | struct ovl_link_data *data = NULL; |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 148 | const char *ret; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 149 | |
| 150 | realdentry = ovl_dentry_real(dentry); |
| 151 | realinode = realdentry->d_inode; |
| 152 | |
| 153 | if (WARN_ON(!realinode->i_op->follow_link)) |
| 154 | return ERR_PTR(-EPERM); |
| 155 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 156 | if (realinode->i_op->put_link) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 157 | data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL); |
NeilBrown | 3188b29 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 158 | if (!data) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 159 | return ERR_PTR(-ENOMEM); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 160 | data->realdentry = realdentry; |
NeilBrown | 3188b29 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 161 | } |
| 162 | |
Al Viro | 6e77137b | 2015-05-02 13:37:52 -0400 | [diff] [blame] | 163 | ret = realinode->i_op->follow_link(realdentry, cookie); |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 164 | if (IS_ERR_OR_NULL(ret)) { |
NeilBrown | 3188b29 | 2015-03-23 13:37:39 +1100 | [diff] [blame] | 165 | kfree(data); |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | if (data) |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 170 | data->cookie = *cookie; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 171 | |
Al Viro | 680baac | 2015-05-02 13:32:22 -0400 | [diff] [blame] | 172 | *cookie = data; |
| 173 | |
| 174 | return ret; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 175 | } |
| 176 | |
Al Viro | 5f2c417 | 2015-05-07 11:14:26 -0400 | [diff] [blame] | 177 | static void ovl_put_link(struct inode *unused, void *c) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 178 | { |
| 179 | struct inode *realinode; |
| 180 | struct ovl_link_data *data = c; |
| 181 | |
| 182 | if (!data) |
| 183 | return; |
| 184 | |
| 185 | realinode = data->realdentry->d_inode; |
Al Viro | 5f2c417 | 2015-05-07 11:14:26 -0400 | [diff] [blame] | 186 | realinode->i_op->put_link(realinode, data->cookie); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 187 | kfree(data); |
| 188 | } |
| 189 | |
| 190 | static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz) |
| 191 | { |
| 192 | struct path realpath; |
| 193 | struct inode *realinode; |
| 194 | |
| 195 | ovl_path_real(dentry, &realpath); |
| 196 | realinode = realpath.dentry->d_inode; |
| 197 | |
| 198 | if (!realinode->i_op->readlink) |
| 199 | return -EINVAL; |
| 200 | |
| 201 | touch_atime(&realpath); |
| 202 | |
| 203 | return realinode->i_op->readlink(realpath.dentry, buf, bufsiz); |
| 204 | } |
| 205 | |
| 206 | |
| 207 | static bool ovl_is_private_xattr(const char *name) |
| 208 | { |
hujianyang | cead89b | 2014-11-24 18:25:21 +0800 | [diff] [blame] | 209 | return strncmp(name, OVL_XATTR_PRE_NAME, OVL_XATTR_PRE_LEN) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | int ovl_setxattr(struct dentry *dentry, const char *name, |
| 213 | const void *value, size_t size, int flags) |
| 214 | { |
| 215 | int err; |
| 216 | struct dentry *upperdentry; |
| 217 | |
| 218 | err = ovl_want_write(dentry); |
| 219 | if (err) |
| 220 | goto out; |
| 221 | |
| 222 | err = -EPERM; |
| 223 | if (ovl_is_private_xattr(name)) |
| 224 | goto out_drop_write; |
| 225 | |
| 226 | err = ovl_copy_up(dentry); |
| 227 | if (err) |
| 228 | goto out_drop_write; |
| 229 | |
| 230 | upperdentry = ovl_dentry_upper(dentry); |
| 231 | err = vfs_setxattr(upperdentry, name, value, size, flags); |
| 232 | |
| 233 | out_drop_write: |
| 234 | ovl_drop_write(dentry); |
| 235 | out: |
| 236 | return err; |
| 237 | } |
| 238 | |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 239 | static bool ovl_need_xattr_filter(struct dentry *dentry, |
| 240 | enum ovl_path_type type) |
| 241 | { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 242 | if ((type & (__OVL_PATH_PURE | __OVL_PATH_UPPER)) == __OVL_PATH_UPPER) |
| 243 | return S_ISDIR(dentry->d_inode->i_mode); |
| 244 | else |
| 245 | return false; |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 246 | } |
| 247 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 248 | ssize_t ovl_getxattr(struct dentry *dentry, const char *name, |
| 249 | void *value, size_t size) |
| 250 | { |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 251 | struct path realpath; |
| 252 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); |
| 253 | |
| 254 | if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 255 | return -ENODATA; |
| 256 | |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 257 | return vfs_getxattr(realpath.dentry, name, value, size); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 261 | { |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 262 | struct path realpath; |
| 263 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 264 | ssize_t res; |
| 265 | int off; |
| 266 | |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 267 | res = vfs_listxattr(realpath.dentry, list, size); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 268 | if (res <= 0 || size == 0) |
| 269 | return res; |
| 270 | |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 271 | if (!ovl_need_xattr_filter(dentry, type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 272 | return res; |
| 273 | |
| 274 | /* filter out private xattrs */ |
| 275 | for (off = 0; off < res;) { |
| 276 | char *s = list + off; |
| 277 | size_t slen = strlen(s) + 1; |
| 278 | |
| 279 | BUG_ON(off + slen > res); |
| 280 | |
| 281 | if (ovl_is_private_xattr(s)) { |
| 282 | res -= slen; |
| 283 | memmove(s, s + slen, res - off); |
| 284 | } else { |
| 285 | off += slen; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | return res; |
| 290 | } |
| 291 | |
| 292 | int ovl_removexattr(struct dentry *dentry, const char *name) |
| 293 | { |
| 294 | int err; |
| 295 | struct path realpath; |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 296 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 297 | |
| 298 | err = ovl_want_write(dentry); |
| 299 | if (err) |
| 300 | goto out; |
| 301 | |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 302 | err = -ENODATA; |
| 303 | if (ovl_need_xattr_filter(dentry, type) && ovl_is_private_xattr(name)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 304 | goto out_drop_write; |
| 305 | |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 306 | if (!OVL_TYPE_UPPER(type)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 307 | err = vfs_getxattr(realpath.dentry, name, NULL, 0); |
| 308 | if (err < 0) |
| 309 | goto out_drop_write; |
| 310 | |
| 311 | err = ovl_copy_up(dentry); |
| 312 | if (err) |
| 313 | goto out_drop_write; |
| 314 | |
| 315 | ovl_path_upper(dentry, &realpath); |
| 316 | } |
| 317 | |
| 318 | err = vfs_removexattr(realpath.dentry, name); |
| 319 | out_drop_write: |
| 320 | ovl_drop_write(dentry); |
| 321 | out: |
| 322 | return err; |
| 323 | } |
| 324 | |
| 325 | static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, |
| 326 | struct dentry *realdentry) |
| 327 | { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 328 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 329 | return false; |
| 330 | |
| 331 | if (special_file(realdentry->d_inode->i_mode)) |
| 332 | return false; |
| 333 | |
| 334 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 335 | return false; |
| 336 | |
| 337 | return true; |
| 338 | } |
| 339 | |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 340 | struct inode *ovl_d_select_inode(struct dentry *dentry, unsigned file_flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 341 | { |
| 342 | int err; |
| 343 | struct path realpath; |
| 344 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 345 | |
Al Viro | 9391dd0 | 2015-07-12 10:39:45 -0400 | [diff] [blame] | 346 | if (d_is_dir(dentry)) |
| 347 | return d_backing_inode(dentry); |
| 348 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 349 | type = ovl_path_real(dentry, &realpath); |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 350 | if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 351 | err = ovl_want_write(dentry); |
| 352 | if (err) |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 353 | return ERR_PTR(err); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 354 | |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 355 | if (file_flags & O_TRUNC) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 356 | err = ovl_copy_up_last(dentry, NULL, true); |
| 357 | else |
| 358 | err = ovl_copy_up(dentry); |
David Howells | f25801e | 2015-06-18 14:32:23 +0100 | [diff] [blame] | 359 | ovl_drop_write(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 360 | if (err) |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 361 | return ERR_PTR(err); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 362 | |
| 363 | ovl_path_upper(dentry, &realpath); |
| 364 | } |
| 365 | |
Miklos Szeredi | 1c8a47d | 2015-10-12 15:56:20 +0200 | [diff] [blame] | 366 | if (realpath.dentry->d_flags & DCACHE_OP_SELECT_INODE) |
| 367 | return realpath.dentry->d_op->d_select_inode(realpath.dentry, file_flags); |
| 368 | |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 369 | return d_backing_inode(realpath.dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static const struct inode_operations ovl_file_inode_operations = { |
| 373 | .setattr = ovl_setattr, |
| 374 | .permission = ovl_permission, |
| 375 | .getattr = ovl_getattr, |
| 376 | .setxattr = ovl_setxattr, |
| 377 | .getxattr = ovl_getxattr, |
| 378 | .listxattr = ovl_listxattr, |
| 379 | .removexattr = ovl_removexattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 383 | .setattr = ovl_setattr, |
| 384 | .follow_link = ovl_follow_link, |
| 385 | .put_link = ovl_put_link, |
| 386 | .readlink = ovl_readlink, |
| 387 | .getattr = ovl_getattr, |
| 388 | .setxattr = ovl_setxattr, |
| 389 | .getxattr = ovl_getxattr, |
| 390 | .listxattr = ovl_listxattr, |
| 391 | .removexattr = ovl_removexattr, |
| 392 | }; |
| 393 | |
| 394 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, |
| 395 | struct ovl_entry *oe) |
| 396 | { |
| 397 | struct inode *inode; |
| 398 | |
| 399 | inode = new_inode(sb); |
| 400 | if (!inode) |
| 401 | return NULL; |
| 402 | |
| 403 | mode &= S_IFMT; |
| 404 | |
| 405 | inode->i_ino = get_next_ino(); |
| 406 | inode->i_mode = mode; |
| 407 | inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 408 | |
| 409 | switch (mode) { |
| 410 | case S_IFDIR: |
| 411 | inode->i_private = oe; |
| 412 | inode->i_op = &ovl_dir_inode_operations; |
| 413 | inode->i_fop = &ovl_dir_operations; |
| 414 | break; |
| 415 | |
| 416 | case S_IFLNK: |
| 417 | inode->i_op = &ovl_symlink_inode_operations; |
| 418 | break; |
| 419 | |
| 420 | case S_IFREG: |
| 421 | case S_IFSOCK: |
| 422 | case S_IFBLK: |
| 423 | case S_IFCHR: |
| 424 | case S_IFIFO: |
| 425 | inode->i_op = &ovl_file_inode_operations; |
| 426 | break; |
| 427 | |
| 428 | default: |
| 429 | WARN(1, "illegal file type: %i\n", mode); |
| 430 | iput(inode); |
| 431 | inode = NULL; |
| 432 | } |
| 433 | |
| 434 | return inode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 435 | } |