]>
Commit | Line | Data |
---|---|---|
e9be9d5e MS |
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> | |
5201dc44 | 13 | #include <linux/posix_acl.h> |
e9be9d5e MS |
14 | #include "overlayfs.h" |
15 | ||
0f7ff2da | 16 | static int ovl_copy_up_truncate(struct dentry *dentry) |
e9be9d5e MS |
17 | { |
18 | int err; | |
19 | struct dentry *parent; | |
20 | struct kstat stat; | |
21 | struct path lowerpath; | |
8eac98b8 | 22 | const struct cred *old_cred; |
e9be9d5e MS |
23 | |
24 | parent = dget_parent(dentry); | |
25 | err = ovl_copy_up(parent); | |
26 | if (err) | |
27 | goto out_dput_parent; | |
28 | ||
29 | ovl_path_lower(dentry, &lowerpath); | |
e9be9d5e | 30 | |
8eac98b8 VG |
31 | old_cred = ovl_override_creds(dentry->d_sb); |
32 | err = vfs_getattr(&lowerpath, &stat); | |
33 | if (!err) { | |
34 | stat.size = 0; | |
35 | err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat); | |
36 | } | |
37 | revert_creds(old_cred); | |
e9be9d5e MS |
38 | |
39 | out_dput_parent: | |
40 | dput(parent); | |
41 | return err; | |
42 | } | |
43 | ||
44 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) | |
45 | { | |
46 | int err; | |
47 | struct dentry *upperdentry; | |
1175b6b8 | 48 | const struct cred *old_cred; |
e9be9d5e | 49 | |
cf9a6784 MS |
50 | /* |
51 | * Check for permissions before trying to copy-up. This is redundant | |
52 | * since it will be rechecked later by ->setattr() on upper dentry. But | |
53 | * without this, copy-up can be triggered by just about anybody. | |
54 | * | |
55 | * We don't initialize inode->size, which just means that | |
56 | * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not | |
57 | * check for a swapfile (which this won't be anyway). | |
58 | */ | |
59 | err = inode_change_ok(dentry->d_inode, attr); | |
60 | if (err) | |
61 | return err; | |
62 | ||
e9be9d5e MS |
63 | err = ovl_want_write(dentry); |
64 | if (err) | |
65 | goto out; | |
66 | ||
03bea604 MS |
67 | if (attr->ia_valid & ATTR_SIZE) { |
68 | struct inode *realinode = d_inode(ovl_dentry_real(dentry)); | |
69 | ||
70 | err = -ETXTBSY; | |
71 | if (atomic_read(&realinode->i_writecount) < 0) | |
72 | goto out_drop_write; | |
73 | } | |
74 | ||
acff81ec MS |
75 | err = ovl_copy_up(dentry); |
76 | if (!err) { | |
03bea604 MS |
77 | struct inode *winode = NULL; |
78 | ||
acff81ec MS |
79 | upperdentry = ovl_dentry_upper(dentry); |
80 | ||
03bea604 MS |
81 | if (attr->ia_valid & ATTR_SIZE) { |
82 | winode = d_inode(upperdentry); | |
83 | err = get_write_access(winode); | |
84 | if (err) | |
85 | goto out_drop_write; | |
86 | } | |
87 | ||
b99c2d91 MS |
88 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
89 | attr->ia_valid &= ~ATTR_MODE; | |
90 | ||
5955102c | 91 | inode_lock(upperdentry->d_inode); |
1175b6b8 | 92 | old_cred = ovl_override_creds(dentry->d_sb); |
e9be9d5e | 93 | err = notify_change(upperdentry, attr, NULL); |
1175b6b8 | 94 | revert_creds(old_cred); |
b81de061 KK |
95 | if (!err) |
96 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); | |
5955102c | 97 | inode_unlock(upperdentry->d_inode); |
03bea604 MS |
98 | |
99 | if (winode) | |
100 | put_write_access(winode); | |
e9be9d5e | 101 | } |
03bea604 | 102 | out_drop_write: |
e9be9d5e MS |
103 | ovl_drop_write(dentry); |
104 | out: | |
105 | return err; | |
106 | } | |
107 | ||
108 | static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry, | |
109 | struct kstat *stat) | |
110 | { | |
111 | struct path realpath; | |
1175b6b8 VG |
112 | const struct cred *old_cred; |
113 | int err; | |
e9be9d5e MS |
114 | |
115 | ovl_path_real(dentry, &realpath); | |
1175b6b8 VG |
116 | old_cred = ovl_override_creds(dentry->d_sb); |
117 | err = vfs_getattr(&realpath, stat); | |
118 | revert_creds(old_cred); | |
119 | return err; | |
e9be9d5e MS |
120 | } |
121 | ||
122 | int ovl_permission(struct inode *inode, int mask) | |
123 | { | |
e9be9d5e | 124 | bool is_upper; |
39b681f8 | 125 | struct inode *realinode = ovl_inode_real(inode, &is_upper); |
c0ca3d70 | 126 | const struct cred *old_cred; |
e9be9d5e MS |
127 | int err; |
128 | ||
e9be9d5e | 129 | /* Careful in RCU walk mode */ |
e9be9d5e MS |
130 | if (!realinode) { |
131 | WARN_ON(!(mask & MAY_NOT_BLOCK)); | |
a999d7e1 | 132 | return -ECHILD; |
e9be9d5e MS |
133 | } |
134 | ||
c0ca3d70 VG |
135 | /* |
136 | * Check overlay inode with the creds of task and underlying inode | |
137 | * with creds of mounter | |
138 | */ | |
139 | err = generic_permission(inode, mask); | |
140 | if (err) | |
141 | return err; | |
142 | ||
143 | old_cred = ovl_override_creds(inode->i_sb); | |
500cac3c | 144 | if (!is_upper && !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
754f8cb7 | 145 | mask &= ~(MAY_WRITE | MAY_APPEND); |
500cac3c VG |
146 | /* Make sure mounter can read file for copy up later */ |
147 | mask |= MAY_READ; | |
148 | } | |
9c630ebe | 149 | err = inode_permission(realinode, mask); |
c0ca3d70 VG |
150 | revert_creds(old_cred); |
151 | ||
152 | return err; | |
e9be9d5e MS |
153 | } |
154 | ||
6b255391 | 155 | static const char *ovl_get_link(struct dentry *dentry, |
fceef393 AV |
156 | struct inode *inode, |
157 | struct delayed_call *done) | |
e9be9d5e | 158 | { |
1175b6b8 VG |
159 | const struct cred *old_cred; |
160 | const char *p; | |
e9be9d5e | 161 | |
6b255391 AV |
162 | if (!dentry) |
163 | return ERR_PTR(-ECHILD); | |
164 | ||
1175b6b8 | 165 | old_cred = ovl_override_creds(dentry->d_sb); |
7764235b | 166 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
1175b6b8 VG |
167 | revert_creds(old_cred); |
168 | return p; | |
e9be9d5e MS |
169 | } |
170 | ||
0956254a | 171 | bool ovl_is_private_xattr(const char *name) |
e9be9d5e | 172 | { |
fe2b7595 AG |
173 | return strncmp(name, OVL_XATTR_PREFIX, |
174 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; | |
e9be9d5e MS |
175 | } |
176 | ||
0e585ccc AG |
177 | int ovl_xattr_set(struct dentry *dentry, const char *name, const void *value, |
178 | size_t size, int flags) | |
e9be9d5e MS |
179 | { |
180 | int err; | |
0e585ccc AG |
181 | struct path realpath; |
182 | enum ovl_path_type type = ovl_path_real(dentry, &realpath); | |
1175b6b8 | 183 | const struct cred *old_cred; |
e9be9d5e MS |
184 | |
185 | err = ovl_want_write(dentry); | |
186 | if (err) | |
187 | goto out; | |
188 | ||
0e585ccc AG |
189 | if (!value && !OVL_TYPE_UPPER(type)) { |
190 | err = vfs_getxattr(realpath.dentry, name, NULL, 0); | |
191 | if (err < 0) | |
192 | goto out_drop_write; | |
193 | } | |
194 | ||
e9be9d5e MS |
195 | err = ovl_copy_up(dentry); |
196 | if (err) | |
197 | goto out_drop_write; | |
198 | ||
0e585ccc AG |
199 | if (!OVL_TYPE_UPPER(type)) |
200 | ovl_path_upper(dentry, &realpath); | |
201 | ||
1175b6b8 | 202 | old_cred = ovl_override_creds(dentry->d_sb); |
0e585ccc AG |
203 | if (value) |
204 | err = vfs_setxattr(realpath.dentry, name, value, size, flags); | |
205 | else { | |
206 | WARN_ON(flags != XATTR_REPLACE); | |
207 | err = vfs_removexattr(realpath.dentry, name); | |
208 | } | |
1175b6b8 | 209 | revert_creds(old_cred); |
e9be9d5e MS |
210 | |
211 | out_drop_write: | |
212 | ovl_drop_write(dentry); | |
213 | out: | |
214 | return err; | |
215 | } | |
216 | ||
0eb45fc3 AG |
217 | int ovl_xattr_get(struct dentry *dentry, const char *name, |
218 | void *value, size_t size) | |
e9be9d5e | 219 | { |
b581755b | 220 | struct dentry *realdentry = ovl_dentry_real(dentry); |
1175b6b8 VG |
221 | ssize_t res; |
222 | const struct cred *old_cred; | |
52148463 | 223 | |
1175b6b8 VG |
224 | old_cred = ovl_override_creds(dentry->d_sb); |
225 | res = vfs_getxattr(realdentry, name, value, size); | |
226 | revert_creds(old_cred); | |
227 | return res; | |
e9be9d5e MS |
228 | } |
229 | ||
230 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) | |
231 | { | |
b581755b | 232 | struct dentry *realdentry = ovl_dentry_real(dentry); |
e9be9d5e | 233 | ssize_t res; |
7cb35119 MS |
234 | size_t len; |
235 | char *s; | |
1175b6b8 | 236 | const struct cred *old_cred; |
e9be9d5e | 237 | |
1175b6b8 | 238 | old_cred = ovl_override_creds(dentry->d_sb); |
b581755b | 239 | res = vfs_listxattr(realdentry, list, size); |
1175b6b8 | 240 | revert_creds(old_cred); |
e9be9d5e MS |
241 | if (res <= 0 || size == 0) |
242 | return res; | |
243 | ||
e9be9d5e | 244 | /* filter out private xattrs */ |
7cb35119 MS |
245 | for (s = list, len = res; len;) { |
246 | size_t slen = strnlen(s, len) + 1; | |
e9be9d5e | 247 | |
7cb35119 MS |
248 | /* underlying fs providing us with an broken xattr list? */ |
249 | if (WARN_ON(slen > len)) | |
250 | return -EIO; | |
e9be9d5e | 251 | |
7cb35119 | 252 | len -= slen; |
e9be9d5e MS |
253 | if (ovl_is_private_xattr(s)) { |
254 | res -= slen; | |
7cb35119 | 255 | memmove(s, s + slen, len); |
e9be9d5e | 256 | } else { |
7cb35119 | 257 | s += slen; |
e9be9d5e MS |
258 | } |
259 | } | |
260 | ||
261 | return res; | |
262 | } | |
263 | ||
39a25b2b VG |
264 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
265 | { | |
39b681f8 | 266 | struct inode *realinode = ovl_inode_real(inode, NULL); |
1175b6b8 VG |
267 | const struct cred *old_cred; |
268 | struct posix_acl *acl; | |
39a25b2b | 269 | |
5201dc44 | 270 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
39a25b2b VG |
271 | return NULL; |
272 | ||
273 | if (!realinode->i_op->get_acl) | |
274 | return NULL; | |
275 | ||
1175b6b8 | 276 | old_cred = ovl_override_creds(inode->i_sb); |
5201dc44 | 277 | acl = get_acl(realinode, type); |
1175b6b8 VG |
278 | revert_creds(old_cred); |
279 | ||
280 | return acl; | |
39a25b2b VG |
281 | } |
282 | ||
e9be9d5e MS |
283 | static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, |
284 | struct dentry *realdentry) | |
285 | { | |
1afaba1e | 286 | if (OVL_TYPE_UPPER(type)) |
e9be9d5e MS |
287 | return false; |
288 | ||
289 | if (special_file(realdentry->d_inode->i_mode)) | |
290 | return false; | |
291 | ||
292 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) | |
293 | return false; | |
294 | ||
295 | return true; | |
296 | } | |
297 | ||
2d902671 | 298 | int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags) |
e9be9d5e | 299 | { |
2d902671 | 300 | int err = 0; |
e9be9d5e MS |
301 | struct path realpath; |
302 | enum ovl_path_type type; | |
e9be9d5e MS |
303 | |
304 | type = ovl_path_real(dentry, &realpath); | |
4bacc9c9 | 305 | if (ovl_open_need_copy_up(file_flags, type, realpath.dentry)) { |
e9be9d5e | 306 | err = ovl_want_write(dentry); |
2d902671 MS |
307 | if (!err) { |
308 | if (file_flags & O_TRUNC) | |
309 | err = ovl_copy_up_truncate(dentry); | |
310 | else | |
311 | err = ovl_copy_up(dentry); | |
312 | ovl_drop_write(dentry); | |
313 | } | |
e9be9d5e MS |
314 | } |
315 | ||
2d902671 | 316 | return err; |
e9be9d5e MS |
317 | } |
318 | ||
d719e8f2 MS |
319 | int ovl_update_time(struct inode *inode, struct timespec *ts, int flags) |
320 | { | |
321 | struct dentry *alias; | |
322 | struct path upperpath; | |
323 | ||
324 | if (!(flags & S_ATIME)) | |
325 | return 0; | |
326 | ||
327 | alias = d_find_any_alias(inode); | |
328 | if (!alias) | |
329 | return 0; | |
330 | ||
331 | ovl_path_upper(alias, &upperpath); | |
332 | if (upperpath.dentry) { | |
333 | touch_atime(&upperpath); | |
334 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; | |
335 | } | |
336 | ||
337 | dput(alias); | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
e9be9d5e MS |
342 | static const struct inode_operations ovl_file_inode_operations = { |
343 | .setattr = ovl_setattr, | |
344 | .permission = ovl_permission, | |
345 | .getattr = ovl_getattr, | |
d837a49b | 346 | .setxattr = generic_setxattr, |
0eb45fc3 | 347 | .getxattr = generic_getxattr, |
e9be9d5e | 348 | .listxattr = ovl_listxattr, |
0e585ccc | 349 | .removexattr = generic_removexattr, |
39a25b2b | 350 | .get_acl = ovl_get_acl, |
d719e8f2 | 351 | .update_time = ovl_update_time, |
e9be9d5e MS |
352 | }; |
353 | ||
354 | static const struct inode_operations ovl_symlink_inode_operations = { | |
355 | .setattr = ovl_setattr, | |
6b255391 | 356 | .get_link = ovl_get_link, |
78a3fa4f | 357 | .readlink = generic_readlink, |
e9be9d5e | 358 | .getattr = ovl_getattr, |
d837a49b | 359 | .setxattr = generic_setxattr, |
0eb45fc3 | 360 | .getxattr = generic_getxattr, |
e9be9d5e | 361 | .listxattr = ovl_listxattr, |
0e585ccc | 362 | .removexattr = generic_removexattr, |
d719e8f2 | 363 | .update_time = ovl_update_time, |
e9be9d5e MS |
364 | }; |
365 | ||
51f7e52d | 366 | static void ovl_fill_inode(struct inode *inode, umode_t mode) |
e9be9d5e | 367 | { |
e9be9d5e MS |
368 | inode->i_ino = get_next_ino(); |
369 | inode->i_mode = mode; | |
d719e8f2 | 370 | inode->i_flags |= S_NOCMTIME; |
2a3a2a3f MS |
371 | #ifdef CONFIG_FS_POSIX_ACL |
372 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; | |
373 | #endif | |
e9be9d5e | 374 | |
07a2daab | 375 | mode &= S_IFMT; |
e9be9d5e MS |
376 | switch (mode) { |
377 | case S_IFDIR: | |
e9be9d5e MS |
378 | inode->i_op = &ovl_dir_inode_operations; |
379 | inode->i_fop = &ovl_dir_operations; | |
380 | break; | |
381 | ||
382 | case S_IFLNK: | |
383 | inode->i_op = &ovl_symlink_inode_operations; | |
384 | break; | |
385 | ||
51f7e52d MS |
386 | default: |
387 | WARN(1, "illegal file type: %i\n", mode); | |
388 | /* Fall through */ | |
389 | ||
e9be9d5e MS |
390 | case S_IFREG: |
391 | case S_IFSOCK: | |
392 | case S_IFBLK: | |
393 | case S_IFCHR: | |
394 | case S_IFIFO: | |
395 | inode->i_op = &ovl_file_inode_operations; | |
396 | break; | |
51f7e52d MS |
397 | } |
398 | } | |
e9be9d5e | 399 | |
51f7e52d MS |
400 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode) |
401 | { | |
402 | struct inode *inode; | |
403 | ||
404 | inode = new_inode(sb); | |
405 | if (inode) | |
406 | ovl_fill_inode(inode, mode); | |
407 | ||
408 | return inode; | |
409 | } | |
410 | ||
411 | static int ovl_inode_test(struct inode *inode, void *data) | |
412 | { | |
413 | return ovl_inode_real(inode, NULL) == data; | |
414 | } | |
415 | ||
416 | static int ovl_inode_set(struct inode *inode, void *data) | |
417 | { | |
418 | inode->i_private = (void *) (((unsigned long) data) | OVL_ISUPPER_MASK); | |
419 | return 0; | |
420 | } | |
421 | ||
422 | struct inode *ovl_get_inode(struct super_block *sb, struct inode *realinode) | |
423 | ||
424 | { | |
425 | struct inode *inode; | |
426 | ||
427 | inode = iget5_locked(sb, (unsigned long) realinode, | |
428 | ovl_inode_test, ovl_inode_set, realinode); | |
429 | if (inode && inode->i_state & I_NEW) { | |
430 | ovl_fill_inode(inode, realinode->i_mode); | |
431 | set_nlink(inode, realinode->i_nlink); | |
432 | unlock_new_inode(inode); | |
e9be9d5e MS |
433 | } |
434 | ||
435 | return inode; | |
e9be9d5e | 436 | } |