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> |
Ingo Molnar | 5b825c3 | 2017-02-02 17:54:15 +0100 | [diff] [blame] | 12 | #include <linux/cred.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 13 | #include <linux/xattr.h> |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 14 | #include <linux/posix_acl.h> |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 15 | #include "overlayfs.h" |
| 16 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 17 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 18 | { |
| 19 | int err; |
| 20 | struct dentry *upperdentry; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 21 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 22 | |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 23 | /* |
| 24 | * Check for permissions before trying to copy-up. This is redundant |
| 25 | * since it will be rechecked later by ->setattr() on upper dentry. But |
| 26 | * without this, copy-up can be triggered by just about anybody. |
| 27 | * |
| 28 | * We don't initialize inode->size, which just means that |
| 29 | * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not |
| 30 | * check for a swapfile (which this won't be anyway). |
| 31 | */ |
Jan Kara | 31051c8 | 2016-05-26 16:55:18 +0200 | [diff] [blame] | 32 | err = setattr_prepare(dentry, attr); |
Miklos Szeredi | cf9a678 | 2015-12-11 16:30:49 +0100 | [diff] [blame] | 33 | if (err) |
| 34 | return err; |
| 35 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 36 | err = ovl_want_write(dentry); |
| 37 | if (err) |
| 38 | goto out; |
| 39 | |
Miklos Szeredi | acff81e | 2015-12-04 19:18:48 +0100 | [diff] [blame] | 40 | err = ovl_copy_up(dentry); |
| 41 | if (!err) { |
| 42 | upperdentry = ovl_dentry_upper(dentry); |
| 43 | |
Miklos Szeredi | b99c2d9 | 2016-07-04 16:49:48 +0200 | [diff] [blame] | 44 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 45 | attr->ia_valid &= ~ATTR_MODE; |
| 46 | |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 47 | inode_lock(upperdentry->d_inode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 48 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 49 | err = notify_change(upperdentry, attr, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 50 | revert_creds(old_cred); |
Konstantin Khlebnikov | b81de06 | 2016-01-31 16:21:29 +0300 | [diff] [blame] | 51 | if (!err) |
| 52 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); |
Al Viro | 5955102 | 2016-01-22 15:40:57 -0500 | [diff] [blame] | 53 | inode_unlock(upperdentry->d_inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 54 | } |
| 55 | ovl_drop_write(dentry); |
| 56 | out: |
| 57 | return err; |
| 58 | } |
| 59 | |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 60 | int ovl_getattr(const struct path *path, struct kstat *stat, |
| 61 | u32 request_mask, unsigned int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 62 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 63 | struct dentry *dentry = path->dentry; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 64 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 65 | struct path realpath; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 66 | const struct cred *old_cred; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 67 | bool is_dir = S_ISDIR(dentry->d_inode->i_mode); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 68 | int err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 69 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 70 | type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 71 | old_cred = ovl_override_creds(dentry->d_sb); |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 72 | err = vfs_getattr(&realpath, stat, request_mask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 73 | if (err) |
| 74 | goto out; |
| 75 | |
| 76 | /* |
| 77 | * When all layers are on the same fs, all real inode number are |
| 78 | * unique, so we use the overlay st_dev, which is friendly to du -x. |
| 79 | * |
| 80 | * We also use st_ino of the copy up origin, if we know it. |
| 81 | * This guaranties constant st_dev/st_ino across copy up. |
| 82 | * |
| 83 | * If filesystem supports NFS export ops, this also guaranties |
| 84 | * persistent st_ino across mount cycle. |
| 85 | */ |
| 86 | if (ovl_same_sb(dentry->d_sb)) { |
| 87 | if (OVL_TYPE_ORIGIN(type)) { |
| 88 | struct kstat lowerstat; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 89 | u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 90 | |
| 91 | ovl_path_lower(dentry, &realpath); |
| 92 | err = vfs_getattr(&realpath, &lowerstat, |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 93 | lowermask, flags); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 94 | if (err) |
| 95 | goto out; |
| 96 | |
| 97 | WARN_ON_ONCE(stat->dev != lowerstat.dev); |
| 98 | /* |
| 99 | * Lower hardlinks are broken on copy up to different |
| 100 | * upper files, so we cannot use the lower origin st_ino |
| 101 | * for those different files, even for the same fs case. |
| 102 | */ |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 103 | if (is_dir || lowerstat.nlink == 1) |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 104 | stat->ino = lowerstat.ino; |
| 105 | } |
| 106 | stat->dev = dentry->d_sb->s_dev; |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 107 | } else if (is_dir) { |
| 108 | /* |
| 109 | * If not all layers are on the same fs the pair {real st_ino; |
| 110 | * overlay st_dev} is not unique, so use the non persistent |
| 111 | * overlay st_ino. |
| 112 | * |
| 113 | * Always use the overlay st_dev for directories, so 'find |
| 114 | * -xdev' will scan the entire overlay mount and won't cross the |
| 115 | * overlay mount boundaries. |
| 116 | */ |
| 117 | stat->dev = dentry->d_sb->s_dev; |
| 118 | stat->ino = dentry->d_inode->i_ino; |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 119 | } |
Miklos Szeredi | 5b71209 | 2017-05-05 11:38:58 +0200 | [diff] [blame^] | 120 | |
| 121 | /* |
| 122 | * It's probably not worth it to count subdirs to get the |
| 123 | * correct link count. nlink=1 seems to pacify 'find' and |
| 124 | * other utilities. |
| 125 | */ |
| 126 | if (is_dir && OVL_TYPE_MERGE(type)) |
| 127 | stat->nlink = 1; |
| 128 | |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 129 | out: |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 130 | revert_creds(old_cred); |
Amir Goldstein | 72b608f | 2017-04-24 00:25:49 +0300 | [diff] [blame] | 131 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 132 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | int ovl_permission(struct inode *inode, int mask) |
| 136 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 137 | bool is_upper; |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 138 | struct inode *realinode = ovl_inode_real(inode, &is_upper); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 139 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 140 | int err; |
| 141 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 142 | /* Careful in RCU walk mode */ |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 143 | if (!realinode) { |
| 144 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
Miklos Szeredi | a999d7e | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 145 | return -ECHILD; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 148 | /* |
| 149 | * Check overlay inode with the creds of task and underlying inode |
| 150 | * with creds of mounter |
| 151 | */ |
| 152 | err = generic_permission(inode, mask); |
| 153 | if (err) |
| 154 | return err; |
| 155 | |
| 156 | old_cred = ovl_override_creds(inode->i_sb); |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 157 | if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
Vivek Goyal | 754f8cb | 2016-07-01 16:34:29 -0400 | [diff] [blame] | 158 | mask &= ~(MAY_WRITE | MAY_APPEND); |
Vivek Goyal | 500cac3 | 2016-07-13 11:00:14 -0400 | [diff] [blame] | 159 | /* Make sure mounter can read file for copy up later */ |
| 160 | mask |= MAY_READ; |
| 161 | } |
Miklos Szeredi | 9c630eb | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 162 | err = inode_permission(realinode, mask); |
Vivek Goyal | c0ca3d7 | 2016-07-01 16:34:27 -0400 | [diff] [blame] | 163 | revert_creds(old_cred); |
| 164 | |
| 165 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 168 | static const char *ovl_get_link(struct dentry *dentry, |
Al Viro | fceef39 | 2015-12-29 15:58:39 -0500 | [diff] [blame] | 169 | struct inode *inode, |
| 170 | struct delayed_call *done) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 171 | { |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 172 | const struct cred *old_cred; |
| 173 | const char *p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 174 | |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 175 | if (!dentry) |
| 176 | return ERR_PTR(-ECHILD); |
| 177 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 178 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | 7764235b | 2016-10-04 14:40:45 +0200 | [diff] [blame] | 179 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 180 | revert_creds(old_cred); |
| 181 | return p; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 182 | } |
| 183 | |
Miklos Szeredi | 0956254 | 2016-08-08 15:08:49 +0200 | [diff] [blame] | 184 | bool ovl_is_private_xattr(const char *name) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 185 | { |
Andreas Gruenbacher | fe2b759 | 2016-08-22 17:59:22 +0200 | [diff] [blame] | 186 | return strncmp(name, OVL_XATTR_PREFIX, |
| 187 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 188 | } |
| 189 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 190 | int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value, |
| 191 | size_t size, int flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 192 | { |
| 193 | int err; |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 194 | struct path realpath; |
| 195 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 196 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 197 | |
| 198 | err = ovl_want_write(dentry); |
| 199 | if (err) |
| 200 | goto out; |
| 201 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 202 | if (!value && !OVL_TYPE_UPPER(type)) { |
| 203 | err = vfs_getxattr(realpath.dentry, name, NULL, 0); |
| 204 | if (err < 0) |
| 205 | goto out_drop_write; |
| 206 | } |
| 207 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 208 | err = ovl_copy_up(dentry); |
| 209 | if (err) |
| 210 | goto out_drop_write; |
| 211 | |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 212 | if (!OVL_TYPE_UPPER(type)) |
| 213 | ovl_path_upper(dentry, &realpath); |
| 214 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 215 | old_cred = ovl_override_creds(dentry->d_sb); |
Andreas Gruenbacher | 0e585cc | 2016-08-22 17:22:11 +0200 | [diff] [blame] | 216 | if (value) |
| 217 | err = vfs_setxattr(realpath.dentry, name, value, size, flags); |
| 218 | else { |
| 219 | WARN_ON(flags != XATTR_REPLACE); |
| 220 | err = vfs_removexattr(realpath.dentry, name); |
| 221 | } |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 222 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 223 | |
| 224 | out_drop_write: |
| 225 | ovl_drop_write(dentry); |
| 226 | out: |
| 227 | return err; |
| 228 | } |
| 229 | |
Andreas Gruenbacher | 0eb45fc | 2016-08-22 17:52:55 +0200 | [diff] [blame] | 230 | int ovl_xattr_get(struct dentry *dentry, const char *name, |
| 231 | void *value, size_t size) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 232 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 233 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 234 | ssize_t res; |
| 235 | const struct cred *old_cred; |
Miklos Szeredi | 5214846 | 2014-11-20 16:40:00 +0100 | [diff] [blame] | 236 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 237 | old_cred = ovl_override_creds(dentry->d_sb); |
| 238 | res = vfs_getxattr(realdentry, name, value, size); |
| 239 | revert_creds(old_cred); |
| 240 | return res; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 244 | { |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 245 | struct dentry *realdentry = ovl_dentry_real(dentry); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 246 | ssize_t res; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 247 | size_t len; |
| 248 | char *s; |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 249 | const struct cred *old_cred; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 250 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 251 | old_cred = ovl_override_creds(dentry->d_sb); |
Miklos Szeredi | b581755 | 2016-06-06 16:21:37 +0200 | [diff] [blame] | 252 | res = vfs_listxattr(realdentry, list, size); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 253 | revert_creds(old_cred); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 254 | if (res <= 0 || size == 0) |
| 255 | return res; |
| 256 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 257 | /* filter out private xattrs */ |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 258 | for (s = list, len = res; len;) { |
| 259 | size_t slen = strnlen(s, len) + 1; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 260 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 261 | /* underlying fs providing us with an broken xattr list? */ |
| 262 | if (WARN_ON(slen > len)) |
| 263 | return -EIO; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 264 | |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 265 | len -= slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 266 | if (ovl_is_private_xattr(s)) { |
| 267 | res -= slen; |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 268 | memmove(s, s + slen, len); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 269 | } else { |
Miklos Szeredi | 7cb3511 | 2016-09-01 11:12:00 +0200 | [diff] [blame] | 270 | s += slen; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | return res; |
| 275 | } |
| 276 | |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 277 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 278 | { |
Miklos Szeredi | 39b681f | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 279 | struct inode *realinode = ovl_inode_real(inode, NULL); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 280 | const struct cred *old_cred; |
| 281 | struct posix_acl *acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 282 | |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 283 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 284 | return NULL; |
| 285 | |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 286 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 5201dc4 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 287 | acl = get_acl(realinode, type); |
Vivek Goyal | 1175b6b | 2016-07-01 16:34:28 -0400 | [diff] [blame] | 288 | revert_creds(old_cred); |
| 289 | |
| 290 | return acl; |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 291 | } |
| 292 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 293 | static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, |
| 294 | struct dentry *realdentry) |
| 295 | { |
Miklos Szeredi | 1afaba1 | 2014-12-13 00:59:42 +0100 | [diff] [blame] | 296 | if (OVL_TYPE_UPPER(type)) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 297 | return false; |
| 298 | |
| 299 | if (special_file(realdentry->d_inode->i_mode)) |
| 300 | return false; |
| 301 | |
| 302 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 303 | return false; |
| 304 | |
| 305 | return true; |
| 306 | } |
| 307 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 308 | int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 309 | { |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 310 | int err = 0; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 311 | struct path realpath; |
| 312 | enum ovl_path_type type; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 313 | |
| 314 | type = ovl_path_real(dentry, &realpath); |
David Howells | 4bacc9c | 2015-06-18 14:32:31 +0100 | [diff] [blame] | 315 | if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 316 | err = ovl_want_write(dentry); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 317 | if (!err) { |
Amir Goldstein | 9aba652 | 2016-11-12 21:36:03 +0200 | [diff] [blame] | 318 | err = ovl_copy_up_flags(dentry, file_flags); |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 319 | ovl_drop_write(dentry); |
| 320 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 321 | } |
| 322 | |
Miklos Szeredi | 2d90267 | 2016-06-30 08:53:27 +0200 | [diff] [blame] | 323 | return err; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 324 | } |
| 325 | |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 326 | int ovl_update_time(struct inode *inode, struct timespec *ts, int flags) |
| 327 | { |
| 328 | struct dentry *alias; |
| 329 | struct path upperpath; |
| 330 | |
| 331 | if (!(flags & S_ATIME)) |
| 332 | return 0; |
| 333 | |
| 334 | alias = d_find_any_alias(inode); |
| 335 | if (!alias) |
| 336 | return 0; |
| 337 | |
| 338 | ovl_path_upper(alias, &upperpath); |
| 339 | if (upperpath.dentry) { |
| 340 | touch_atime(&upperpath); |
| 341 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; |
| 342 | } |
| 343 | |
| 344 | dput(alias); |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 349 | static const struct inode_operations ovl_file_inode_operations = { |
| 350 | .setattr = ovl_setattr, |
| 351 | .permission = ovl_permission, |
| 352 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 353 | .listxattr = ovl_listxattr, |
Vivek Goyal | 39a25b2 | 2016-07-01 16:34:26 -0400 | [diff] [blame] | 354 | .get_acl = ovl_get_acl, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 355 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 356 | }; |
| 357 | |
| 358 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 359 | .setattr = ovl_setattr, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 360 | .get_link = ovl_get_link, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 361 | .getattr = ovl_getattr, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 362 | .listxattr = ovl_listxattr, |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 363 | .update_time = ovl_update_time, |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 364 | }; |
| 365 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 366 | /* |
| 367 | * It is possible to stack overlayfs instance on top of another |
| 368 | * overlayfs instance as lower layer. We need to annonate the |
| 369 | * stackable i_mutex locks according to stack level of the super |
| 370 | * block instance. An overlayfs instance can never be in stack |
| 371 | * depth 0 (there is always a real fs below it). An overlayfs |
| 372 | * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. |
| 373 | * |
| 374 | * For example, here is a snip from /proc/lockdep_chains after |
| 375 | * dir_iterate of nested overlayfs: |
| 376 | * |
| 377 | * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) |
| 378 | * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) |
| 379 | * [...] &type->i_mutex_dir_key (stack_depth=0) |
| 380 | */ |
| 381 | #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH |
| 382 | |
| 383 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) |
| 384 | { |
| 385 | #ifdef CONFIG_LOCKDEP |
| 386 | static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; |
| 387 | static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; |
| 388 | |
| 389 | int depth = inode->i_sb->s_stack_depth - 1; |
| 390 | |
| 391 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) |
| 392 | depth = 0; |
| 393 | |
| 394 | if (S_ISDIR(inode->i_mode)) |
| 395 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); |
| 396 | else |
| 397 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); |
| 398 | #endif |
| 399 | } |
| 400 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 401 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 402 | { |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 403 | inode->i_ino = get_next_ino(); |
| 404 | inode->i_mode = mode; |
Miklos Szeredi | d719e8f | 2016-07-29 12:05:23 +0200 | [diff] [blame] | 405 | inode->i_flags |= S_NOCMTIME; |
Miklos Szeredi | 2a3a2a3 | 2016-09-01 11:11:59 +0200 | [diff] [blame] | 406 | #ifdef CONFIG_FS_POSIX_ACL |
| 407 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; |
| 408 | #endif |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 409 | |
Amir Goldstein | b1eaa95 | 2017-01-11 12:07:46 +0200 | [diff] [blame] | 410 | ovl_lockdep_annotate_inode_mutex_key(inode); |
| 411 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 412 | switch (mode & S_IFMT) { |
| 413 | case S_IFREG: |
| 414 | inode->i_op = &ovl_file_inode_operations; |
| 415 | break; |
| 416 | |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 417 | case S_IFDIR: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 418 | inode->i_op = &ovl_dir_inode_operations; |
| 419 | inode->i_fop = &ovl_dir_operations; |
| 420 | break; |
| 421 | |
| 422 | case S_IFLNK: |
| 423 | inode->i_op = &ovl_symlink_inode_operations; |
| 424 | break; |
| 425 | |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 426 | default: |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 427 | inode->i_op = &ovl_file_inode_operations; |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 428 | init_special_inode(inode, mode, rdev); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 429 | break; |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 430 | } |
| 431 | } |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 432 | |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 433 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 434 | { |
| 435 | struct inode *inode; |
| 436 | |
| 437 | inode = new_inode(sb); |
| 438 | if (inode) |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 439 | ovl_fill_inode(inode, mode, rdev); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 440 | |
| 441 | return inode; |
| 442 | } |
| 443 | |
| 444 | static int ovl_inode_test(struct inode *inode, void *data) |
| 445 | { |
| 446 | return ovl_inode_real(inode, NULL) == data; |
| 447 | } |
| 448 | |
| 449 | static int ovl_inode_set(struct inode *inode, void *data) |
| 450 | { |
| 451 | inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK); |
| 452 | return 0; |
| 453 | } |
| 454 | |
| 455 | struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode) |
| 456 | |
| 457 | { |
| 458 | struct inode *inode; |
| 459 | |
| 460 | inode = iget5_locked(sb, (unsigned long) realinode, |
| 461 | ovl_inode_test, ovl_inode_set, realinode); |
| 462 | if (inode && inode->i_state & I_NEW) { |
Miklos Szeredi | ca4c8a3 | 2016-12-16 11:02:55 +0100 | [diff] [blame] | 463 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); |
Miklos Szeredi | 51f7e52 | 2016-07-29 12:05:24 +0200 | [diff] [blame] | 464 | set_nlink(inode, realinode->i_nlink); |
| 465 | unlock_new_inode(inode); |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | return inode; |
Miklos Szeredi | e9be9d5 | 2014-10-24 00:14:38 +0200 | [diff] [blame] | 469 | } |