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