1 /*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7 /*
8 * Some corrections by tytso.
9 */
10
11 /* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14 /* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17 #include <linux/init.h>
18 #include <linux/export.h>
19 #include <linux/kernel.h>
20 #include <linux/slab.h>
21 #include <linux/fs.h>
22 #include <linux/namei.h>
23 #include <linux/pagemap.h>
24 #include <linux/fsnotify.h>
25 #include <linux/personality.h>
26 #include <linux/security.h>
27 #include <linux/ima.h>
28 #include <linux/syscalls.h>
29 #include <linux/mount.h>
30 #include <linux/audit.h>
31 #include <linux/capability.h>
32 #include <linux/file.h>
33 #include <linux/fcntl.h>
34 #include <linux/device_cgroup.h>
35 #include <linux/fs_struct.h>
36 #include <linux/posix_acl.h>
37 #include <linux/hash.h>
38 #include <linux/bitops.h>
39 #include <linux/init_task.h>
40 #include <asm/uaccess.h>
41
42 #include "internal.h"
43 #include "mount.h"
44
45 /* [Feb-1997 T. Schoebel-Theuer]
46 * Fundamental changes in the pathname lookup mechanisms (namei)
47 * were necessary because of omirr. The reason is that omirr needs
48 * to know the _real_ pathname, not the user-supplied one, in case
49 * of symlinks (and also when transname replacements occur).
50 *
51 * The new code replaces the old recursive symlink resolution with
52 * an iterative one (in case of non-nested symlink chains). It does
53 * this with calls to <fs>_follow_link().
54 * As a side effect, dir_namei(), _namei() and follow_link() are now
55 * replaced with a single function lookup_dentry() that can handle all
56 * the special cases of the former code.
57 *
58 * With the new dcache, the pathname is stored at each inode, at least as
59 * long as the refcount of the inode is positive. As a side effect, the
60 * size of the dcache depends on the inode cache and thus is dynamic.
61 *
62 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
63 * resolution to correspond with current state of the code.
64 *
65 * Note that the symlink resolution is not *completely* iterative.
66 * There is still a significant amount of tail- and mid- recursion in
67 * the algorithm. Also, note that <fs>_readlink() is not used in
68 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
69 * may return different results than <fs>_follow_link(). Many virtual
70 * filesystems (including /proc) exhibit this behavior.
71 */
72
73 /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
74 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
75 * and the name already exists in form of a symlink, try to create the new
76 * name indicated by the symlink. The old code always complained that the
77 * name already exists, due to not following the symlink even if its target
78 * is nonexistent. The new semantics affects also mknod() and link() when
79 * the name is a symlink pointing to a non-existent name.
80 *
81 * I don't know which semantics is the right one, since I have no access
82 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
83 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
84 * "old" one. Personally, I think the new semantics is much more logical.
85 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
86 * file does succeed in both HP-UX and SunOs, but not in Solaris
87 * and in the old Linux semantics.
88 */
89
90 /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
91 * semantics. See the comments in "open_namei" and "do_link" below.
92 *
93 * [10-Sep-98 Alan Modra] Another symlink change.
94 */
95
96 /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
97 * inside the path - always follow.
98 * in the last component in creation/removal/renaming - never follow.
99 * if LOOKUP_FOLLOW passed - follow.
100 * if the pathname has trailing slashes - follow.
101 * otherwise - don't follow.
102 * (applied in that order).
103 *
104 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
105 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
106 * During the 2.4 we need to fix the userland stuff depending on it -
107 * hopefully we will be able to get rid of that wart in 2.5. So far only
108 * XEmacs seems to be relying on it...
109 */
110 /*
111 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
112 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
113 * any extra contention...
114 */
115
116 /* In order to reduce some races, while at the same time doing additional
117 * checking and hopefully speeding things up, we copy filenames to the
118 * kernel data space before using them..
119 *
120 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
121 * PATH_MAX includes the nul terminator --RR.
122 */
123
124 #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
125
126 struct filename *
getname_flags(const char __user * filename,int flags,int * empty)127 getname_flags(const char __user *filename, int flags, int *empty)
128 {
129 struct filename *result;
130 char *kname;
131 int len;
132
133 result = audit_reusename(filename);
134 if (result)
135 return result;
136
137 result = __getname();
138 if (unlikely(!result))
139 return ERR_PTR(-ENOMEM);
140
141 /*
142 * First, try to embed the struct filename inside the names_cache
143 * allocation
144 */
145 kname = (char *)result->iname;
146 result->name = kname;
147
148 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
149 if (unlikely(len < 0)) {
150 __putname(result);
151 return ERR_PTR(len);
152 }
153
154 /*
155 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
156 * separate struct filename so we can dedicate the entire
157 * names_cache allocation for the pathname, and re-do the copy from
158 * userland.
159 */
160 if (unlikely(len == EMBEDDED_NAME_MAX)) {
161 const size_t size = offsetof(struct filename, iname[1]);
162 kname = (char *)result;
163
164 /*
165 * size is chosen that way we to guarantee that
166 * result->iname[0] is within the same object and that
167 * kname can't be equal to result->iname, no matter what.
168 */
169 result = kzalloc(size, GFP_KERNEL);
170 if (unlikely(!result)) {
171 __putname(kname);
172 return ERR_PTR(-ENOMEM);
173 }
174 result->name = kname;
175 len = strncpy_from_user(kname, filename, PATH_MAX);
176 if (unlikely(len < 0)) {
177 __putname(kname);
178 kfree(result);
179 return ERR_PTR(len);
180 }
181 if (unlikely(len == PATH_MAX)) {
182 __putname(kname);
183 kfree(result);
184 return ERR_PTR(-ENAMETOOLONG);
185 }
186 }
187
188 result->refcnt = 1;
189 /* The empty path is special. */
190 if (unlikely(!len)) {
191 if (empty)
192 *empty = 1;
193 if (!(flags & LOOKUP_EMPTY)) {
194 putname(result);
195 return ERR_PTR(-ENOENT);
196 }
197 }
198
199 result->uptr = filename;
200 result->aname = NULL;
201 audit_getname(result);
202 return result;
203 }
204
205 struct filename *
getname(const char __user * filename)206 getname(const char __user * filename)
207 {
208 return getname_flags(filename, 0, NULL);
209 }
210
211 struct filename *
getname_kernel(const char * filename)212 getname_kernel(const char * filename)
213 {
214 struct filename *result;
215 int len = strlen(filename) + 1;
216
217 result = __getname();
218 if (unlikely(!result))
219 return ERR_PTR(-ENOMEM);
220
221 if (len <= EMBEDDED_NAME_MAX) {
222 result->name = (char *)result->iname;
223 } else if (len <= PATH_MAX) {
224 const size_t size = offsetof(struct filename, iname[1]);
225 struct filename *tmp;
226
227 tmp = kmalloc(size, GFP_KERNEL);
228 if (unlikely(!tmp)) {
229 __putname(result);
230 return ERR_PTR(-ENOMEM);
231 }
232 tmp->name = (char *)result;
233 result = tmp;
234 } else {
235 __putname(result);
236 return ERR_PTR(-ENAMETOOLONG);
237 }
238 memcpy((char *)result->name, filename, len);
239 result->uptr = NULL;
240 result->aname = NULL;
241 result->refcnt = 1;
242 audit_getname(result);
243
244 return result;
245 }
246
putname(struct filename * name)247 void putname(struct filename *name)
248 {
249 BUG_ON(name->refcnt <= 0);
250
251 if (--name->refcnt > 0)
252 return;
253
254 if (name->name != name->iname) {
255 __putname(name->name);
256 kfree(name);
257 } else
258 __putname(name);
259 }
260
check_acl(struct inode * inode,int mask)261 static int check_acl(struct inode *inode, int mask)
262 {
263 #ifdef CONFIG_FS_POSIX_ACL
264 struct posix_acl *acl;
265
266 if (mask & MAY_NOT_BLOCK) {
267 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
268 if (!acl)
269 return -EAGAIN;
270 /* no ->get_acl() calls in RCU mode... */
271 if (is_uncached_acl(acl))
272 return -ECHILD;
273 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
274 }
275
276 acl = get_acl(inode, ACL_TYPE_ACCESS);
277 if (IS_ERR(acl))
278 return PTR_ERR(acl);
279 if (acl) {
280 int error = posix_acl_permission(inode, acl, mask);
281 posix_acl_release(acl);
282 return error;
283 }
284 #endif
285
286 return -EAGAIN;
287 }
288
289 /*
290 * This does the basic permission checking
291 */
acl_permission_check(struct inode * inode,int mask)292 static int acl_permission_check(struct inode *inode, int mask)
293 {
294 unsigned int mode = inode->i_mode;
295
296 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
297 mode >>= 6;
298 else {
299 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
300 int error = check_acl(inode, mask);
301 if (error != -EAGAIN)
302 return error;
303 }
304
305 if (in_group_p(inode->i_gid))
306 mode >>= 3;
307 }
308
309 /*
310 * If the DACs are ok we don't need any capability check.
311 */
312 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
313 return 0;
314 return -EACCES;
315 }
316
317 /**
318 * generic_permission - check for access rights on a Posix-like filesystem
319 * @inode: inode to check access rights for
320 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
321 *
322 * Used to check for read/write/execute permissions on a file.
323 * We use "fsuid" for this, letting us set arbitrary permissions
324 * for filesystem access without changing the "normal" uids which
325 * are used for other things.
326 *
327 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
328 * request cannot be satisfied (eg. requires blocking or too much complexity).
329 * It would then be called again in ref-walk mode.
330 */
generic_permission(struct inode * inode,int mask)331 int generic_permission(struct inode *inode, int mask)
332 {
333 int ret;
334
335 /*
336 * Do the basic permission checks.
337 */
338 ret = acl_permission_check(inode, mask);
339 if (ret != -EACCES)
340 return ret;
341
342 if (S_ISDIR(inode->i_mode)) {
343 /* DACs are overridable for directories */
344 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
345 return 0;
346 if (!(mask & MAY_WRITE))
347 if (capable_wrt_inode_uidgid(inode,
348 CAP_DAC_READ_SEARCH))
349 return 0;
350 return -EACCES;
351 }
352 /*
353 * Read/write DACs are always overridable.
354 * Executable DACs are overridable when there is
355 * at least one exec bit set.
356 */
357 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
358 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
359 return 0;
360
361 /*
362 * Searching includes executable on directories, else just read.
363 */
364 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
365 if (mask == MAY_READ)
366 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
367 return 0;
368
369 return -EACCES;
370 }
371 EXPORT_SYMBOL(generic_permission);
372
373 /*
374 * We _really_ want to just do "generic_permission()" without
375 * even looking at the inode->i_op values. So we keep a cache
376 * flag in inode->i_opflags, that says "this has not special
377 * permission function, use the fast case".
378 */
do_inode_permission(struct vfsmount * mnt,struct inode * inode,int mask)379 static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
380 {
381 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
382 if (likely(mnt && inode->i_op->permission2))
383 return inode->i_op->permission2(mnt, inode, mask);
384 if (likely(inode->i_op->permission))
385 return inode->i_op->permission(inode, mask);
386
387 /* This gets set once for the inode lifetime */
388 spin_lock(&inode->i_lock);
389 inode->i_opflags |= IOP_FASTPERM;
390 spin_unlock(&inode->i_lock);
391 }
392 return generic_permission(inode, mask);
393 }
394
395 /**
396 * __inode_permission - Check for access rights to a given inode
397 * @inode: Inode to check permission on
398 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
399 *
400 * Check for read/write/execute permissions on an inode.
401 *
402 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
403 *
404 * This does not check for a read-only file system. You probably want
405 * inode_permission().
406 */
__inode_permission2(struct vfsmount * mnt,struct inode * inode,int mask)407 int __inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
408 {
409 int retval;
410
411 if (unlikely(mask & MAY_WRITE)) {
412 /*
413 * Nobody gets write access to an immutable file.
414 */
415 if (IS_IMMUTABLE(inode))
416 return -EPERM;
417
418 /*
419 * Updating mtime will likely cause i_uid and i_gid to be
420 * written back improperly if their true value is unknown
421 * to the vfs.
422 */
423 if (HAS_UNMAPPED_ID(inode))
424 return -EACCES;
425 }
426
427 retval = do_inode_permission(mnt, inode, mask);
428 if (retval)
429 return retval;
430
431 retval = devcgroup_inode_permission(inode, mask);
432 if (retval)
433 return retval;
434
435 retval = security_inode_permission(inode, mask);
436 return retval;
437 }
438 EXPORT_SYMBOL(__inode_permission2);
439
__inode_permission(struct inode * inode,int mask)440 int __inode_permission(struct inode *inode, int mask)
441 {
442 return __inode_permission2(NULL, inode, mask);
443 }
444 EXPORT_SYMBOL(__inode_permission);
445
446 /**
447 * sb_permission - Check superblock-level permissions
448 * @sb: Superblock of inode to check permission on
449 * @inode: Inode to check permission on
450 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
451 *
452 * Separate out file-system wide checks from inode-specific permission checks.
453 */
sb_permission(struct super_block * sb,struct inode * inode,int mask)454 static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
455 {
456 if (unlikely(mask & MAY_WRITE)) {
457 umode_t mode = inode->i_mode;
458
459 /* Nobody gets write access to a read-only fs. */
460 if ((sb->s_flags & MS_RDONLY) &&
461 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
462 return -EROFS;
463 }
464 return 0;
465 }
466
467 /**
468 * inode_permission - Check for access rights to a given inode
469 * @inode: Inode to check permission on
470 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
471 *
472 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
473 * this, letting us set arbitrary permissions for filesystem access without
474 * changing the "normal" UIDs which are used for other things.
475 *
476 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
477 */
inode_permission2(struct vfsmount * mnt,struct inode * inode,int mask)478 int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
479 {
480 int retval;
481
482 retval = sb_permission(inode->i_sb, inode, mask);
483 if (retval)
484 return retval;
485 return __inode_permission2(mnt, inode, mask);
486 }
487 EXPORT_SYMBOL(inode_permission2);
488
inode_permission(struct inode * inode,int mask)489 int inode_permission(struct inode *inode, int mask)
490 {
491 return inode_permission2(NULL, inode, mask);
492 }
493 EXPORT_SYMBOL(inode_permission);
494
495 /**
496 * path_get - get a reference to a path
497 * @path: path to get the reference to
498 *
499 * Given a path increment the reference count to the dentry and the vfsmount.
500 */
path_get(const struct path * path)501 void path_get(const struct path *path)
502 {
503 mntget(path->mnt);
504 dget(path->dentry);
505 }
506 EXPORT_SYMBOL(path_get);
507
508 /**
509 * path_put - put a reference to a path
510 * @path: path to put the reference to
511 *
512 * Given a path decrement the reference count to the dentry and the vfsmount.
513 */
path_put(const struct path * path)514 void path_put(const struct path *path)
515 {
516 dput(path->dentry);
517 mntput(path->mnt);
518 }
519 EXPORT_SYMBOL(path_put);
520
521 #define EMBEDDED_LEVELS 2
522 struct nameidata {
523 struct path path;
524 struct qstr last;
525 struct path root;
526 struct inode *inode; /* path.dentry.d_inode */
527 unsigned int flags;
528 unsigned seq, m_seq;
529 int last_type;
530 unsigned depth;
531 int total_link_count;
532 struct saved {
533 struct path link;
534 struct delayed_call done;
535 const char *name;
536 unsigned seq;
537 } *stack, internal[EMBEDDED_LEVELS];
538 struct filename *name;
539 struct nameidata *saved;
540 struct inode *link_inode;
541 unsigned root_seq;
542 int dfd;
543 };
544
set_nameidata(struct nameidata * p,int dfd,struct filename * name)545 static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
546 {
547 struct nameidata *old = current->nameidata;
548 p->stack = p->internal;
549 p->dfd = dfd;
550 p->name = name;
551 p->total_link_count = old ? old->total_link_count : 0;
552 p->saved = old;
553 current->nameidata = p;
554 }
555
restore_nameidata(void)556 static void restore_nameidata(void)
557 {
558 struct nameidata *now = current->nameidata, *old = now->saved;
559
560 current->nameidata = old;
561 if (old)
562 old->total_link_count = now->total_link_count;
563 if (now->stack != now->internal)
564 kfree(now->stack);
565 }
566
__nd_alloc_stack(struct nameidata * nd)567 static int __nd_alloc_stack(struct nameidata *nd)
568 {
569 struct saved *p;
570
571 if (nd->flags & LOOKUP_RCU) {
572 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
573 GFP_ATOMIC);
574 if (unlikely(!p))
575 return -ECHILD;
576 } else {
577 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
578 GFP_KERNEL);
579 if (unlikely(!p))
580 return -ENOMEM;
581 }
582 memcpy(p, nd->internal, sizeof(nd->internal));
583 nd->stack = p;
584 return 0;
585 }
586
587 /**
588 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
589 * @path: nameidate to verify
590 *
591 * Rename can sometimes move a file or directory outside of a bind
592 * mount, path_connected allows those cases to be detected.
593 */
path_connected(const struct path * path)594 static bool path_connected(const struct path *path)
595 {
596 struct vfsmount *mnt = path->mnt;
597 struct super_block *sb = mnt->mnt_sb;
598
599 /* Bind mounts and multi-root filesystems can have disconnected paths */
600 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
601 return true;
602
603 return is_subdir(path->dentry, mnt->mnt_root);
604 }
605
nd_alloc_stack(struct nameidata * nd)606 static inline int nd_alloc_stack(struct nameidata *nd)
607 {
608 if (likely(nd->depth != EMBEDDED_LEVELS))
609 return 0;
610 if (likely(nd->stack != nd->internal))
611 return 0;
612 return __nd_alloc_stack(nd);
613 }
614
drop_links(struct nameidata * nd)615 static void drop_links(struct nameidata *nd)
616 {
617 int i = nd->depth;
618 while (i--) {
619 struct saved *last = nd->stack + i;
620 do_delayed_call(&last->done);
621 clear_delayed_call(&last->done);
622 }
623 }
624
terminate_walk(struct nameidata * nd)625 static void terminate_walk(struct nameidata *nd)
626 {
627 drop_links(nd);
628 if (!(nd->flags & LOOKUP_RCU)) {
629 int i;
630 path_put(&nd->path);
631 for (i = 0; i < nd->depth; i++)
632 path_put(&nd->stack[i].link);
633 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
634 path_put(&nd->root);
635 nd->root.mnt = NULL;
636 }
637 } else {
638 nd->flags &= ~LOOKUP_RCU;
639 if (!(nd->flags & LOOKUP_ROOT))
640 nd->root.mnt = NULL;
641 rcu_read_unlock();
642 }
643 nd->depth = 0;
644 }
645
646 /* path_put is needed afterwards regardless of success or failure */
legitimize_path(struct nameidata * nd,struct path * path,unsigned seq)647 static bool legitimize_path(struct nameidata *nd,
648 struct path *path, unsigned seq)
649 {
650 int res = __legitimize_mnt(path->mnt, nd->m_seq);
651 if (unlikely(res)) {
652 if (res > 0)
653 path->mnt = NULL;
654 path->dentry = NULL;
655 return false;
656 }
657 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
658 path->dentry = NULL;
659 return false;
660 }
661 return !read_seqcount_retry(&path->dentry->d_seq, seq);
662 }
663
legitimize_links(struct nameidata * nd)664 static bool legitimize_links(struct nameidata *nd)
665 {
666 int i;
667 for (i = 0; i < nd->depth; i++) {
668 struct saved *last = nd->stack + i;
669 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
670 drop_links(nd);
671 nd->depth = i + 1;
672 return false;
673 }
674 }
675 return true;
676 }
677
678 /*
679 * Path walking has 2 modes, rcu-walk and ref-walk (see
680 * Documentation/filesystems/path-lookup.txt). In situations when we can't
681 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
682 * normal reference counts on dentries and vfsmounts to transition to ref-walk
683 * mode. Refcounts are grabbed at the last known good point before rcu-walk
684 * got stuck, so ref-walk may continue from there. If this is not successful
685 * (eg. a seqcount has changed), then failure is returned and it's up to caller
686 * to restart the path walk from the beginning in ref-walk mode.
687 */
688
689 /**
690 * unlazy_walk - try to switch to ref-walk mode.
691 * @nd: nameidata pathwalk data
692 * @dentry: child of nd->path.dentry or NULL
693 * @seq: seq number to check dentry against
694 * Returns: 0 on success, -ECHILD on failure
695 *
696 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
697 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
698 * @nd or NULL. Must be called from rcu-walk context.
699 * Nothing should touch nameidata between unlazy_walk() failure and
700 * terminate_walk().
701 */
unlazy_walk(struct nameidata * nd,struct dentry * dentry,unsigned seq)702 static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
703 {
704 struct dentry *parent = nd->path.dentry;
705
706 BUG_ON(!(nd->flags & LOOKUP_RCU));
707
708 nd->flags &= ~LOOKUP_RCU;
709 if (unlikely(!legitimize_links(nd)))
710 goto out2;
711 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
712 goto out2;
713 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
714 goto out1;
715
716 /*
717 * For a negative lookup, the lookup sequence point is the parents
718 * sequence point, and it only needs to revalidate the parent dentry.
719 *
720 * For a positive lookup, we need to move both the parent and the
721 * dentry from the RCU domain to be properly refcounted. And the
722 * sequence number in the dentry validates *both* dentry counters,
723 * since we checked the sequence number of the parent after we got
724 * the child sequence number. So we know the parent must still
725 * be valid if the child sequence number is still valid.
726 */
727 if (!dentry) {
728 if (read_seqcount_retry(&parent->d_seq, nd->seq))
729 goto out;
730 BUG_ON(nd->inode != parent->d_inode);
731 } else {
732 if (!lockref_get_not_dead(&dentry->d_lockref))
733 goto out;
734 if (read_seqcount_retry(&dentry->d_seq, seq))
735 goto drop_dentry;
736 }
737
738 /*
739 * Sequence counts matched. Now make sure that the root is
740 * still valid and get it if required.
741 */
742 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
743 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
744 rcu_read_unlock();
745 dput(dentry);
746 return -ECHILD;
747 }
748 }
749
750 rcu_read_unlock();
751 return 0;
752
753 drop_dentry:
754 rcu_read_unlock();
755 dput(dentry);
756 goto drop_root_mnt;
757 out2:
758 nd->path.mnt = NULL;
759 out1:
760 nd->path.dentry = NULL;
761 out:
762 rcu_read_unlock();
763 drop_root_mnt:
764 if (!(nd->flags & LOOKUP_ROOT))
765 nd->root.mnt = NULL;
766 return -ECHILD;
767 }
768
unlazy_link(struct nameidata * nd,struct path * link,unsigned seq)769 static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
770 {
771 if (unlikely(!legitimize_path(nd, link, seq))) {
772 drop_links(nd);
773 nd->depth = 0;
774 nd->flags &= ~LOOKUP_RCU;
775 nd->path.mnt = NULL;
776 nd->path.dentry = NULL;
777 if (!(nd->flags & LOOKUP_ROOT))
778 nd->root.mnt = NULL;
779 rcu_read_unlock();
780 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
781 return 0;
782 }
783 path_put(link);
784 return -ECHILD;
785 }
786
d_revalidate(struct dentry * dentry,unsigned int flags)787 static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
788 {
789 return dentry->d_op->d_revalidate(dentry, flags);
790 }
791
792 /**
793 * complete_walk - successful completion of path walk
794 * @nd: pointer nameidata
795 *
796 * If we had been in RCU mode, drop out of it and legitimize nd->path.
797 * Revalidate the final result, unless we'd already done that during
798 * the path walk or the filesystem doesn't ask for it. Return 0 on
799 * success, -error on failure. In case of failure caller does not
800 * need to drop nd->path.
801 */
complete_walk(struct nameidata * nd)802 static int complete_walk(struct nameidata *nd)
803 {
804 struct dentry *dentry = nd->path.dentry;
805 int status;
806
807 if (nd->flags & LOOKUP_RCU) {
808 if (!(nd->flags & LOOKUP_ROOT))
809 nd->root.mnt = NULL;
810 if (unlikely(unlazy_walk(nd, NULL, 0)))
811 return -ECHILD;
812 }
813
814 if (likely(!(nd->flags & LOOKUP_JUMPED)))
815 return 0;
816
817 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
818 return 0;
819
820 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
821 if (status > 0)
822 return 0;
823
824 if (!status)
825 status = -ESTALE;
826
827 return status;
828 }
829
set_root(struct nameidata * nd)830 static void set_root(struct nameidata *nd)
831 {
832 struct fs_struct *fs = current->fs;
833
834 if (nd->flags & LOOKUP_RCU) {
835 unsigned seq;
836
837 do {
838 seq = read_seqcount_begin(&fs->seq);
839 nd->root = fs->root;
840 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
841 } while (read_seqcount_retry(&fs->seq, seq));
842 } else {
843 get_fs_root(fs, &nd->root);
844 }
845 }
846
path_put_conditional(struct path * path,struct nameidata * nd)847 static void path_put_conditional(struct path *path, struct nameidata *nd)
848 {
849 dput(path->dentry);
850 if (path->mnt != nd->path.mnt)
851 mntput(path->mnt);
852 }
853
path_to_nameidata(const struct path * path,struct nameidata * nd)854 static inline void path_to_nameidata(const struct path *path,
855 struct nameidata *nd)
856 {
857 if (!(nd->flags & LOOKUP_RCU)) {
858 dput(nd->path.dentry);
859 if (nd->path.mnt != path->mnt)
860 mntput(nd->path.mnt);
861 }
862 nd->path.mnt = path->mnt;
863 nd->path.dentry = path->dentry;
864 }
865
nd_jump_root(struct nameidata * nd)866 static int nd_jump_root(struct nameidata *nd)
867 {
868 if (nd->flags & LOOKUP_RCU) {
869 struct dentry *d;
870 nd->path = nd->root;
871 d = nd->path.dentry;
872 nd->inode = d->d_inode;
873 nd->seq = nd->root_seq;
874 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
875 return -ECHILD;
876 } else {
877 path_put(&nd->path);
878 nd->path = nd->root;
879 path_get(&nd->path);
880 nd->inode = nd->path.dentry->d_inode;
881 }
882 nd->flags |= LOOKUP_JUMPED;
883 return 0;
884 }
885
886 /*
887 * Helper to directly jump to a known parsed path from ->get_link,
888 * caller must have taken a reference to path beforehand.
889 */
nd_jump_link(struct path * path)890 void nd_jump_link(struct path *path)
891 {
892 struct nameidata *nd = current->nameidata;
893 path_put(&nd->path);
894
895 nd->path = *path;
896 nd->inode = nd->path.dentry->d_inode;
897 nd->flags |= LOOKUP_JUMPED;
898 }
899
put_link(struct nameidata * nd)900 static inline void put_link(struct nameidata *nd)
901 {
902 struct saved *last = nd->stack + --nd->depth;
903 do_delayed_call(&last->done);
904 if (!(nd->flags & LOOKUP_RCU))
905 path_put(&last->link);
906 }
907
908 int sysctl_protected_symlinks __read_mostly = 0;
909 int sysctl_protected_hardlinks __read_mostly = 0;
910
911 /**
912 * may_follow_link - Check symlink following for unsafe situations
913 * @nd: nameidata pathwalk data
914 *
915 * In the case of the sysctl_protected_symlinks sysctl being enabled,
916 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
917 * in a sticky world-writable directory. This is to protect privileged
918 * processes from failing races against path names that may change out
919 * from under them by way of other users creating malicious symlinks.
920 * It will permit symlinks to be followed only when outside a sticky
921 * world-writable directory, or when the uid of the symlink and follower
922 * match, or when the directory owner matches the symlink's owner.
923 *
924 * Returns 0 if following the symlink is allowed, -ve on error.
925 */
may_follow_link(struct nameidata * nd)926 static inline int may_follow_link(struct nameidata *nd)
927 {
928 const struct inode *inode;
929 const struct inode *parent;
930 kuid_t puid;
931
932 if (!sysctl_protected_symlinks)
933 return 0;
934
935 /* Allowed if owner and follower match. */
936 inode = nd->link_inode;
937 if (uid_eq(current_cred()->fsuid, inode->i_uid))
938 return 0;
939
940 /* Allowed if parent directory not sticky and world-writable. */
941 parent = nd->inode;
942 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
943 return 0;
944
945 /* Allowed if parent directory and link owner match. */
946 puid = parent->i_uid;
947 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
948 return 0;
949
950 if (nd->flags & LOOKUP_RCU)
951 return -ECHILD;
952
953 audit_log_link_denied("follow_link", &nd->stack[0].link);
954 return -EACCES;
955 }
956
957 /**
958 * safe_hardlink_source - Check for safe hardlink conditions
959 * @inode: the source inode to hardlink from
960 *
961 * Return false if at least one of the following conditions:
962 * - inode is not a regular file
963 * - inode is setuid
964 * - inode is setgid and group-exec
965 * - access failure for read and write
966 *
967 * Otherwise returns true.
968 */
safe_hardlink_source(struct inode * inode)969 static bool safe_hardlink_source(struct inode *inode)
970 {
971 umode_t mode = inode->i_mode;
972
973 /* Special files should not get pinned to the filesystem. */
974 if (!S_ISREG(mode))
975 return false;
976
977 /* Setuid files should not get pinned to the filesystem. */
978 if (mode & S_ISUID)
979 return false;
980
981 /* Executable setgid files should not get pinned to the filesystem. */
982 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
983 return false;
984
985 /* Hardlinking to unreadable or unwritable sources is dangerous. */
986 if (inode_permission(inode, MAY_READ | MAY_WRITE))
987 return false;
988
989 return true;
990 }
991
992 /**
993 * may_linkat - Check permissions for creating a hardlink
994 * @link: the source to hardlink from
995 *
996 * Block hardlink when all of:
997 * - sysctl_protected_hardlinks enabled
998 * - fsuid does not match inode
999 * - hardlink source is unsafe (see safe_hardlink_source() above)
1000 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
1001 *
1002 * Returns 0 if successful, -ve on error.
1003 */
may_linkat(struct path * link)1004 static int may_linkat(struct path *link)
1005 {
1006 struct inode *inode;
1007
1008 if (!sysctl_protected_hardlinks)
1009 return 0;
1010
1011 inode = link->dentry->d_inode;
1012
1013 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1014 * otherwise, it must be a safe source.
1015 */
1016 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
1017 return 0;
1018
1019 audit_log_link_denied("linkat", link);
1020 return -EPERM;
1021 }
1022
1023 static __always_inline
get_link(struct nameidata * nd)1024 const char *get_link(struct nameidata *nd)
1025 {
1026 struct saved *last = nd->stack + nd->depth - 1;
1027 struct dentry *dentry = last->link.dentry;
1028 struct inode *inode = nd->link_inode;
1029 int error;
1030 const char *res;
1031
1032 if (!(nd->flags & LOOKUP_RCU)) {
1033 touch_atime(&last->link);
1034 cond_resched();
1035 } else if (atime_needs_update_rcu(&last->link, inode)) {
1036 if (unlikely(unlazy_walk(nd, NULL, 0)))
1037 return ERR_PTR(-ECHILD);
1038 touch_atime(&last->link);
1039 }
1040
1041 error = security_inode_follow_link(dentry, inode,
1042 nd->flags & LOOKUP_RCU);
1043 if (unlikely(error))
1044 return ERR_PTR(error);
1045
1046 nd->last_type = LAST_BIND;
1047 res = inode->i_link;
1048 if (!res) {
1049 const char * (*get)(struct dentry *, struct inode *,
1050 struct delayed_call *);
1051 get = inode->i_op->get_link;
1052 if (nd->flags & LOOKUP_RCU) {
1053 res = get(NULL, inode, &last->done);
1054 if (res == ERR_PTR(-ECHILD)) {
1055 if (unlikely(unlazy_walk(nd, NULL, 0)))
1056 return ERR_PTR(-ECHILD);
1057 res = get(dentry, inode, &last->done);
1058 }
1059 } else {
1060 res = get(dentry, inode, &last->done);
1061 }
1062 if (IS_ERR_OR_NULL(res))
1063 return res;
1064 }
1065 if (*res == '/') {
1066 if (!nd->root.mnt)
1067 set_root(nd);
1068 if (unlikely(nd_jump_root(nd)))
1069 return ERR_PTR(-ECHILD);
1070 while (unlikely(*++res == '/'))
1071 ;
1072 }
1073 if (!*res)
1074 res = NULL;
1075 return res;
1076 }
1077
1078 /*
1079 * follow_up - Find the mountpoint of path's vfsmount
1080 *
1081 * Given a path, find the mountpoint of its source file system.
1082 * Replace @path with the path of the mountpoint in the parent mount.
1083 * Up is towards /.
1084 *
1085 * Return 1 if we went up a level and 0 if we were already at the
1086 * root.
1087 */
follow_up(struct path * path)1088 int follow_up(struct path *path)
1089 {
1090 struct mount *mnt = real_mount(path->mnt);
1091 struct mount *parent;
1092 struct dentry *mountpoint;
1093
1094 read_seqlock_excl(&mount_lock);
1095 parent = mnt->mnt_parent;
1096 if (parent == mnt) {
1097 read_sequnlock_excl(&mount_lock);
1098 return 0;
1099 }
1100 mntget(&parent->mnt);
1101 mountpoint = dget(mnt->mnt_mountpoint);
1102 read_sequnlock_excl(&mount_lock);
1103 dput(path->dentry);
1104 path->dentry = mountpoint;
1105 mntput(path->mnt);
1106 path->mnt = &parent->mnt;
1107 return 1;
1108 }
1109 EXPORT_SYMBOL(follow_up);
1110
1111 /*
1112 * Perform an automount
1113 * - return -EISDIR to tell follow_managed() to stop and return the path we
1114 * were called with.
1115 */
follow_automount(struct path * path,struct nameidata * nd,bool * need_mntput)1116 static int follow_automount(struct path *path, struct nameidata *nd,
1117 bool *need_mntput)
1118 {
1119 struct vfsmount *mnt;
1120 int err;
1121
1122 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1123 return -EREMOTE;
1124
1125 /* We don't want to mount if someone's just doing a stat -
1126 * unless they're stat'ing a directory and appended a '/' to
1127 * the name.
1128 *
1129 * We do, however, want to mount if someone wants to open or
1130 * create a file of any type under the mountpoint, wants to
1131 * traverse through the mountpoint or wants to open the
1132 * mounted directory. Also, autofs may mark negative dentries
1133 * as being automount points. These will need the attentions
1134 * of the daemon to instantiate them before they can be used.
1135 */
1136 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1137 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1138 path->dentry->d_inode)
1139 return -EISDIR;
1140
1141 nd->total_link_count++;
1142 if (nd->total_link_count >= 40)
1143 return -ELOOP;
1144
1145 mnt = path->dentry->d_op->d_automount(path);
1146 if (IS_ERR(mnt)) {
1147 /*
1148 * The filesystem is allowed to return -EISDIR here to indicate
1149 * it doesn't want to automount. For instance, autofs would do
1150 * this so that its userspace daemon can mount on this dentry.
1151 *
1152 * However, we can only permit this if it's a terminal point in
1153 * the path being looked up; if it wasn't then the remainder of
1154 * the path is inaccessible and we should say so.
1155 */
1156 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
1157 return -EREMOTE;
1158 return PTR_ERR(mnt);
1159 }
1160
1161 if (!mnt) /* mount collision */
1162 return 0;
1163
1164 if (!*need_mntput) {
1165 /* lock_mount() may release path->mnt on error */
1166 mntget(path->mnt);
1167 *need_mntput = true;
1168 }
1169 err = finish_automount(mnt, path);
1170
1171 switch (err) {
1172 case -EBUSY:
1173 /* Someone else made a mount here whilst we were busy */
1174 return 0;
1175 case 0:
1176 path_put(path);
1177 path->mnt = mnt;
1178 path->dentry = dget(mnt->mnt_root);
1179 return 0;
1180 default:
1181 return err;
1182 }
1183
1184 }
1185
1186 /*
1187 * Handle a dentry that is managed in some way.
1188 * - Flagged for transit management (autofs)
1189 * - Flagged as mountpoint
1190 * - Flagged as automount point
1191 *
1192 * This may only be called in refwalk mode.
1193 *
1194 * Serialization is taken care of in namespace.c
1195 */
follow_managed(struct path * path,struct nameidata * nd)1196 static int follow_managed(struct path *path, struct nameidata *nd)
1197 {
1198 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
1199 unsigned managed;
1200 bool need_mntput = false;
1201 int ret = 0;
1202
1203 /* Given that we're not holding a lock here, we retain the value in a
1204 * local variable for each dentry as we look at it so that we don't see
1205 * the components of that value change under us */
1206 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1207 managed &= DCACHE_MANAGED_DENTRY,
1208 unlikely(managed != 0)) {
1209 /* Allow the filesystem to manage the transit without i_mutex
1210 * being held. */
1211 if (managed & DCACHE_MANAGE_TRANSIT) {
1212 BUG_ON(!path->dentry->d_op);
1213 BUG_ON(!path->dentry->d_op->d_manage);
1214 ret = path->dentry->d_op->d_manage(path->dentry, false);
1215 if (ret < 0)
1216 break;
1217 }
1218
1219 /* Transit to a mounted filesystem. */
1220 if (managed & DCACHE_MOUNTED) {
1221 struct vfsmount *mounted = lookup_mnt(path);
1222 if (mounted) {
1223 dput(path->dentry);
1224 if (need_mntput)
1225 mntput(path->mnt);
1226 path->mnt = mounted;
1227 path->dentry = dget(mounted->mnt_root);
1228 need_mntput = true;
1229 continue;
1230 }
1231
1232 /* Something is mounted on this dentry in another
1233 * namespace and/or whatever was mounted there in this
1234 * namespace got unmounted before lookup_mnt() could
1235 * get it */
1236 }
1237
1238 /* Handle an automount point */
1239 if (managed & DCACHE_NEED_AUTOMOUNT) {
1240 ret = follow_automount(path, nd, &need_mntput);
1241 if (ret < 0)
1242 break;
1243 continue;
1244 }
1245
1246 /* We didn't change the current path point */
1247 break;
1248 }
1249
1250 if (need_mntput && path->mnt == mnt)
1251 mntput(path->mnt);
1252 if (ret == -EISDIR || !ret)
1253 ret = 1;
1254 if (need_mntput)
1255 nd->flags |= LOOKUP_JUMPED;
1256 if (unlikely(ret < 0))
1257 path_put_conditional(path, nd);
1258 return ret;
1259 }
1260
follow_down_one(struct path * path)1261 int follow_down_one(struct path *path)
1262 {
1263 struct vfsmount *mounted;
1264
1265 mounted = lookup_mnt(path);
1266 if (mounted) {
1267 dput(path->dentry);
1268 mntput(path->mnt);
1269 path->mnt = mounted;
1270 path->dentry = dget(mounted->mnt_root);
1271 return 1;
1272 }
1273 return 0;
1274 }
1275 EXPORT_SYMBOL(follow_down_one);
1276
managed_dentry_rcu(struct dentry * dentry)1277 static inline int managed_dentry_rcu(struct dentry *dentry)
1278 {
1279 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1280 dentry->d_op->d_manage(dentry, true) : 0;
1281 }
1282
1283 /*
1284 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1285 * we meet a managed dentry that would need blocking.
1286 */
__follow_mount_rcu(struct nameidata * nd,struct path * path,struct inode ** inode,unsigned * seqp)1287 static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
1288 struct inode **inode, unsigned *seqp)
1289 {
1290 for (;;) {
1291 struct mount *mounted;
1292 /*
1293 * Don't forget we might have a non-mountpoint managed dentry
1294 * that wants to block transit.
1295 */
1296 switch (managed_dentry_rcu(path->dentry)) {
1297 case -ECHILD:
1298 default:
1299 return false;
1300 case -EISDIR:
1301 return true;
1302 case 0:
1303 break;
1304 }
1305
1306 if (!d_mountpoint(path->dentry))
1307 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1308
1309 mounted = __lookup_mnt(path->mnt, path->dentry);
1310 if (!mounted)
1311 break;
1312 path->mnt = &mounted->mnt;
1313 path->dentry = mounted->mnt.mnt_root;
1314 nd->flags |= LOOKUP_JUMPED;
1315 *seqp = read_seqcount_begin(&path->dentry->d_seq);
1316 /*
1317 * Update the inode too. We don't need to re-check the
1318 * dentry sequence number here after this d_inode read,
1319 * because a mount-point is always pinned.
1320 */
1321 *inode = path->dentry->d_inode;
1322 }
1323 return !read_seqretry(&mount_lock, nd->m_seq) &&
1324 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
1325 }
1326
follow_dotdot_rcu(struct nameidata * nd)1327 static int follow_dotdot_rcu(struct nameidata *nd)
1328 {
1329 struct inode *inode = nd->inode;
1330
1331 while (1) {
1332 if (path_equal(&nd->path, &nd->root))
1333 break;
1334 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1335 struct dentry *old = nd->path.dentry;
1336 struct dentry *parent = old->d_parent;
1337 unsigned seq;
1338
1339 inode = parent->d_inode;
1340 seq = read_seqcount_begin(&parent->d_seq);
1341 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1342 return -ECHILD;
1343 nd->path.dentry = parent;
1344 nd->seq = seq;
1345 if (unlikely(!path_connected(&nd->path)))
1346 return -ENOENT;
1347 break;
1348 } else {
1349 struct mount *mnt = real_mount(nd->path.mnt);
1350 struct mount *mparent = mnt->mnt_parent;
1351 struct dentry *mountpoint = mnt->mnt_mountpoint;
1352 struct inode *inode2 = mountpoint->d_inode;
1353 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1354 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1355 return -ECHILD;
1356 if (&mparent->mnt == nd->path.mnt)
1357 break;
1358 /* we know that mountpoint was pinned */
1359 nd->path.dentry = mountpoint;
1360 nd->path.mnt = &mparent->mnt;
1361 inode = inode2;
1362 nd->seq = seq;
1363 }
1364 }
1365 while (unlikely(d_mountpoint(nd->path.dentry))) {
1366 struct mount *mounted;
1367 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
1368 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1369 return -ECHILD;
1370 if (!mounted)
1371 break;
1372 nd->path.mnt = &mounted->mnt;
1373 nd->path.dentry = mounted->mnt.mnt_root;
1374 inode = nd->path.dentry->d_inode;
1375 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
1376 }
1377 nd->inode = inode;
1378 return 0;
1379 }
1380
1381 /*
1382 * Follow down to the covering mount currently visible to userspace. At each
1383 * point, the filesystem owning that dentry may be queried as to whether the
1384 * caller is permitted to proceed or not.
1385 */
follow_down(struct path * path)1386 int follow_down(struct path *path)
1387 {
1388 unsigned managed;
1389 int ret;
1390
1391 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1392 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1393 /* Allow the filesystem to manage the transit without i_mutex
1394 * being held.
1395 *
1396 * We indicate to the filesystem if someone is trying to mount
1397 * something here. This gives autofs the chance to deny anyone
1398 * other than its daemon the right to mount on its
1399 * superstructure.
1400 *
1401 * The filesystem may sleep at this point.
1402 */
1403 if (managed & DCACHE_MANAGE_TRANSIT) {
1404 BUG_ON(!path->dentry->d_op);
1405 BUG_ON(!path->dentry->d_op->d_manage);
1406 ret = path->dentry->d_op->d_manage(
1407 path->dentry, false);
1408 if (ret < 0)
1409 return ret == -EISDIR ? 0 : ret;
1410 }
1411
1412 /* Transit to a mounted filesystem. */
1413 if (managed & DCACHE_MOUNTED) {
1414 struct vfsmount *mounted = lookup_mnt(path);
1415 if (!mounted)
1416 break;
1417 dput(path->dentry);
1418 mntput(path->mnt);
1419 path->mnt = mounted;
1420 path->dentry = dget(mounted->mnt_root);
1421 continue;
1422 }
1423
1424 /* Don't handle automount points here */
1425 break;
1426 }
1427 return 0;
1428 }
1429 EXPORT_SYMBOL(follow_down);
1430
1431 /*
1432 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1433 */
follow_mount(struct path * path)1434 static void follow_mount(struct path *path)
1435 {
1436 while (d_mountpoint(path->dentry)) {
1437 struct vfsmount *mounted = lookup_mnt(path);
1438 if (!mounted)
1439 break;
1440 dput(path->dentry);
1441 mntput(path->mnt);
1442 path->mnt = mounted;
1443 path->dentry = dget(mounted->mnt_root);
1444 }
1445 }
1446
path_parent_directory(struct path * path)1447 static int path_parent_directory(struct path *path)
1448 {
1449 struct dentry *old = path->dentry;
1450 /* rare case of legitimate dget_parent()... */
1451 path->dentry = dget_parent(path->dentry);
1452 dput(old);
1453 if (unlikely(!path_connected(path)))
1454 return -ENOENT;
1455 return 0;
1456 }
1457
follow_dotdot(struct nameidata * nd)1458 static int follow_dotdot(struct nameidata *nd)
1459 {
1460 while(1) {
1461 if (nd->path.dentry == nd->root.dentry &&
1462 nd->path.mnt == nd->root.mnt) {
1463 break;
1464 }
1465 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1466 int ret = path_parent_directory(&nd->path);
1467 if (ret)
1468 return ret;
1469 break;
1470 }
1471 if (!follow_up(&nd->path))
1472 break;
1473 }
1474 follow_mount(&nd->path);
1475 nd->inode = nd->path.dentry->d_inode;
1476 return 0;
1477 }
1478
1479 /*
1480 * This looks up the name in dcache and possibly revalidates the found dentry.
1481 * NULL is returned if the dentry does not exist in the cache.
1482 */
lookup_dcache(const struct qstr * name,struct dentry * dir,unsigned int flags)1483 static struct dentry *lookup_dcache(const struct qstr *name,
1484 struct dentry *dir,
1485 unsigned int flags)
1486 {
1487 struct dentry *dentry;
1488 int error;
1489
1490 dentry = d_lookup(dir, name);
1491 if (dentry) {
1492 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
1493 error = d_revalidate(dentry, flags);
1494 if (unlikely(error <= 0)) {
1495 if (!error)
1496 d_invalidate(dentry);
1497 dput(dentry);
1498 return ERR_PTR(error);
1499 }
1500 }
1501 }
1502 return dentry;
1503 }
1504
1505 /*
1506 * Call i_op->lookup on the dentry. The dentry must be negative and
1507 * unhashed.
1508 *
1509 * dir->d_inode->i_mutex must be held
1510 */
lookup_real(struct inode * dir,struct dentry * dentry,unsigned int flags)1511 static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
1512 unsigned int flags)
1513 {
1514 struct dentry *old;
1515
1516 /* Don't create child dentry for a dead directory. */
1517 if (unlikely(IS_DEADDIR(dir))) {
1518 dput(dentry);
1519 return ERR_PTR(-ENOENT);
1520 }
1521
1522 old = dir->i_op->lookup(dir, dentry, flags);
1523 if (unlikely(old)) {
1524 dput(dentry);
1525 dentry = old;
1526 }
1527 return dentry;
1528 }
1529
__lookup_hash(const struct qstr * name,struct dentry * base,unsigned int flags)1530 static struct dentry *__lookup_hash(const struct qstr *name,
1531 struct dentry *base, unsigned int flags)
1532 {
1533 struct dentry *dentry = lookup_dcache(name, base, flags);
1534
1535 if (dentry)
1536 return dentry;
1537
1538 dentry = d_alloc(base, name);
1539 if (unlikely(!dentry))
1540 return ERR_PTR(-ENOMEM);
1541
1542 return lookup_real(base->d_inode, dentry, flags);
1543 }
1544
lookup_fast(struct nameidata * nd,struct path * path,struct inode ** inode,unsigned * seqp)1545 static int lookup_fast(struct nameidata *nd,
1546 struct path *path, struct inode **inode,
1547 unsigned *seqp)
1548 {
1549 struct vfsmount *mnt = nd->path.mnt;
1550 struct dentry *dentry, *parent = nd->path.dentry;
1551 int status = 1;
1552 int err;
1553
1554 /*
1555 * Rename seqlock is not required here because in the off chance
1556 * of a false negative due to a concurrent rename, the caller is
1557 * going to fall back to non-racy lookup.
1558 */
1559 if (nd->flags & LOOKUP_RCU) {
1560 unsigned seq;
1561 bool negative;
1562 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
1563 if (unlikely(!dentry)) {
1564 if (unlazy_walk(nd, NULL, 0))
1565 return -ECHILD;
1566 return 0;
1567 }
1568
1569 /*
1570 * This sequence count validates that the inode matches
1571 * the dentry name information from lookup.
1572 */
1573 *inode = d_backing_inode(dentry);
1574 negative = d_is_negative(dentry);
1575 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
1576 return -ECHILD;
1577
1578 /*
1579 * This sequence count validates that the parent had no
1580 * changes while we did the lookup of the dentry above.
1581 *
1582 * The memory barrier in read_seqcount_begin of child is
1583 * enough, we can use __read_seqcount_retry here.
1584 */
1585 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
1586 return -ECHILD;
1587
1588 *seqp = seq;
1589 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1590 status = d_revalidate(dentry, nd->flags);
1591 if (unlikely(status <= 0)) {
1592 if (unlazy_walk(nd, dentry, seq))
1593 return -ECHILD;
1594 if (status == -ECHILD)
1595 status = d_revalidate(dentry, nd->flags);
1596 } else {
1597 /*
1598 * Note: do negative dentry check after revalidation in
1599 * case that drops it.
1600 */
1601 if (unlikely(negative))
1602 return -ENOENT;
1603 path->mnt = mnt;
1604 path->dentry = dentry;
1605 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
1606 return 1;
1607 if (unlazy_walk(nd, dentry, seq))
1608 return -ECHILD;
1609 }
1610 } else {
1611 dentry = __d_lookup(parent, &nd->last);
1612 if (unlikely(!dentry))
1613 return 0;
1614 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1615 status = d_revalidate(dentry, nd->flags);
1616 }
1617 if (unlikely(status <= 0)) {
1618 if (!status)
1619 d_invalidate(dentry);
1620 dput(dentry);
1621 return status;
1622 }
1623 if (unlikely(d_is_negative(dentry))) {
1624 dput(dentry);
1625 return -ENOENT;
1626 }
1627
1628 path->mnt = mnt;
1629 path->dentry = dentry;
1630 err = follow_managed(path, nd);
1631 if (likely(err > 0))
1632 *inode = d_backing_inode(path->dentry);
1633 return err;
1634 }
1635
1636 /* Fast lookup failed, do it the slow way */
lookup_slow(const struct qstr * name,struct dentry * dir,unsigned int flags)1637 static struct dentry *lookup_slow(const struct qstr *name,
1638 struct dentry *dir,
1639 unsigned int flags)
1640 {
1641 struct dentry *dentry = ERR_PTR(-ENOENT), *old;
1642 struct inode *inode = dir->d_inode;
1643 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
1644
1645 inode_lock_shared(inode);
1646 /* Don't go there if it's already dead */
1647 if (unlikely(IS_DEADDIR(inode)))
1648 goto out;
1649 again:
1650 dentry = d_alloc_parallel(dir, name, &wq);
1651 if (IS_ERR(dentry))
1652 goto out;
1653 if (unlikely(!d_in_lookup(dentry))) {
1654 if ((dentry->d_flags & DCACHE_OP_REVALIDATE) &&
1655 !(flags & LOOKUP_NO_REVAL)) {
1656 int error = d_revalidate(dentry, flags);
1657 if (unlikely(error <= 0)) {
1658 if (!error) {
1659 d_invalidate(dentry);
1660 dput(dentry);
1661 goto again;
1662 }
1663 dput(dentry);
1664 dentry = ERR_PTR(error);
1665 }
1666 }
1667 } else {
1668 old = inode->i_op->lookup(inode, dentry, flags);
1669 d_lookup_done(dentry);
1670 if (unlikely(old)) {
1671 dput(dentry);
1672 dentry = old;
1673 }
1674 }
1675 out:
1676 inode_unlock_shared(inode);
1677 return dentry;
1678 }
1679
may_lookup(struct nameidata * nd)1680 static inline int may_lookup(struct nameidata *nd)
1681 {
1682 if (nd->flags & LOOKUP_RCU) {
1683 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
1684 if (err != -ECHILD)
1685 return err;
1686 if (unlazy_walk(nd, NULL, 0))
1687 return -ECHILD;
1688 }
1689 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
1690 }
1691
handle_dots(struct nameidata * nd,int type)1692 static inline int handle_dots(struct nameidata *nd, int type)
1693 {
1694 if (type == LAST_DOTDOT) {
1695 if (!nd->root.mnt)
1696 set_root(nd);
1697 if (nd->flags & LOOKUP_RCU) {
1698 return follow_dotdot_rcu(nd);
1699 } else
1700 return follow_dotdot(nd);
1701 }
1702 return 0;
1703 }
1704
pick_link(struct nameidata * nd,struct path * link,struct inode * inode,unsigned seq)1705 static int pick_link(struct nameidata *nd, struct path *link,
1706 struct inode *inode, unsigned seq)
1707 {
1708 int error;
1709 struct saved *last;
1710 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
1711 path_to_nameidata(link, nd);
1712 return -ELOOP;
1713 }
1714 if (!(nd->flags & LOOKUP_RCU)) {
1715 if (link->mnt == nd->path.mnt)
1716 mntget(link->mnt);
1717 }
1718 error = nd_alloc_stack(nd);
1719 if (unlikely(error)) {
1720 if (error == -ECHILD) {
1721 if (unlikely(unlazy_link(nd, link, seq)))
1722 return -ECHILD;
1723 error = nd_alloc_stack(nd);
1724 }
1725 if (error) {
1726 path_put(link);
1727 return error;
1728 }
1729 }
1730
1731 last = nd->stack + nd->depth++;
1732 last->link = *link;
1733 clear_delayed_call(&last->done);
1734 nd->link_inode = inode;
1735 last->seq = seq;
1736 return 1;
1737 }
1738
1739 /*
1740 * Do we need to follow links? We _really_ want to be able
1741 * to do this check without having to look at inode->i_op,
1742 * so we keep a cache of "no, this doesn't need follow_link"
1743 * for the common case.
1744 */
should_follow_link(struct nameidata * nd,struct path * link,int follow,struct inode * inode,unsigned seq)1745 static inline int should_follow_link(struct nameidata *nd, struct path *link,
1746 int follow,
1747 struct inode *inode, unsigned seq)
1748 {
1749 if (likely(!d_is_symlink(link->dentry)))
1750 return 0;
1751 if (!follow)
1752 return 0;
1753 /* make sure that d_is_symlink above matches inode */
1754 if (nd->flags & LOOKUP_RCU) {
1755 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1756 return -ECHILD;
1757 }
1758 return pick_link(nd, link, inode, seq);
1759 }
1760
1761 enum {WALK_GET = 1, WALK_PUT = 2};
1762
walk_component(struct nameidata * nd,int flags)1763 static int walk_component(struct nameidata *nd, int flags)
1764 {
1765 struct path path;
1766 struct inode *inode;
1767 unsigned seq;
1768 int err;
1769 /*
1770 * "." and ".." are special - ".." especially so because it has
1771 * to be able to know about the current root directory and
1772 * parent relationships.
1773 */
1774 if (unlikely(nd->last_type != LAST_NORM)) {
1775 err = handle_dots(nd, nd->last_type);
1776 if (flags & WALK_PUT)
1777 put_link(nd);
1778 return err;
1779 }
1780 err = lookup_fast(nd, &path, &inode, &seq);
1781 if (unlikely(err <= 0)) {
1782 if (err < 0)
1783 return err;
1784 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1785 nd->flags);
1786 if (IS_ERR(path.dentry))
1787 return PTR_ERR(path.dentry);
1788
1789 path.mnt = nd->path.mnt;
1790 err = follow_managed(&path, nd);
1791 if (unlikely(err < 0))
1792 return err;
1793
1794 if (unlikely(d_is_negative(path.dentry))) {
1795 path_to_nameidata(&path, nd);
1796 return -ENOENT;
1797 }
1798
1799 seq = 0; /* we are already out of RCU mode */
1800 inode = d_backing_inode(path.dentry);
1801 }
1802
1803 if (flags & WALK_PUT)
1804 put_link(nd);
1805 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
1806 if (unlikely(err))
1807 return err;
1808 path_to_nameidata(&path, nd);
1809 nd->inode = inode;
1810 nd->seq = seq;
1811 return 0;
1812 }
1813
1814 /*
1815 * We can do the critical dentry name comparison and hashing
1816 * operations one word at a time, but we are limited to:
1817 *
1818 * - Architectures with fast unaligned word accesses. We could
1819 * do a "get_unaligned()" if this helps and is sufficiently
1820 * fast.
1821 *
1822 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1823 * do not trap on the (extremely unlikely) case of a page
1824 * crossing operation.
1825 *
1826 * - Furthermore, we need an efficient 64-bit compile for the
1827 * 64-bit case in order to generate the "number of bytes in
1828 * the final mask". Again, that could be replaced with a
1829 * efficient population count instruction or similar.
1830 */
1831 #ifdef CONFIG_DCACHE_WORD_ACCESS
1832
1833 #include <asm/word-at-a-time.h>
1834
1835 #ifdef HASH_MIX
1836
1837 /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1838
1839 #elif defined(CONFIG_64BIT)
1840 /*
1841 * Register pressure in the mixing function is an issue, particularly
1842 * on 32-bit x86, but almost any function requires one state value and
1843 * one temporary. Instead, use a function designed for two state values
1844 * and no temporaries.
1845 *
1846 * This function cannot create a collision in only two iterations, so
1847 * we have two iterations to achieve avalanche. In those two iterations,
1848 * we have six layers of mixing, which is enough to spread one bit's
1849 * influence out to 2^6 = 64 state bits.
1850 *
1851 * Rotate constants are scored by considering either 64 one-bit input
1852 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1853 * probability of that delta causing a change to each of the 128 output
1854 * bits, using a sample of random initial states.
1855 *
1856 * The Shannon entropy of the computed probabilities is then summed
1857 * to produce a score. Ideally, any input change has a 50% chance of
1858 * toggling any given output bit.
1859 *
1860 * Mixing scores (in bits) for (12,45):
1861 * Input delta: 1-bit 2-bit
1862 * 1 round: 713.3 42542.6
1863 * 2 rounds: 2753.7 140389.8
1864 * 3 rounds: 5954.1 233458.2
1865 * 4 rounds: 7862.6 256672.2
1866 * Perfect: 8192 258048
1867 * (64*128) (64*63/2 * 128)
1868 */
1869 #define HASH_MIX(x, y, a) \
1870 ( x ^= (a), \
1871 y ^= x, x = rol64(x,12),\
1872 x += y, y = rol64(y,45),\
1873 y *= 9 )
1874
1875 /*
1876 * Fold two longs into one 32-bit hash value. This must be fast, but
1877 * latency isn't quite as critical, as there is a fair bit of additional
1878 * work done before the hash value is used.
1879 */
fold_hash(unsigned long x,unsigned long y)1880 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1881 {
1882 y ^= x * GOLDEN_RATIO_64;
1883 y *= GOLDEN_RATIO_64;
1884 return y >> 32;
1885 }
1886
1887 #else /* 32-bit case */
1888
1889 /*
1890 * Mixing scores (in bits) for (7,20):
1891 * Input delta: 1-bit 2-bit
1892 * 1 round: 330.3 9201.6
1893 * 2 rounds: 1246.4 25475.4
1894 * 3 rounds: 1907.1 31295.1
1895 * 4 rounds: 2042.3 31718.6
1896 * Perfect: 2048 31744
1897 * (32*64) (32*31/2 * 64)
1898 */
1899 #define HASH_MIX(x, y, a) \
1900 ( x ^= (a), \
1901 y ^= x, x = rol32(x, 7),\
1902 x += y, y = rol32(y,20),\
1903 y *= 9 )
1904
fold_hash(unsigned long x,unsigned long y)1905 static inline unsigned int fold_hash(unsigned long x, unsigned long y)
1906 {
1907 /* Use arch-optimized multiply if one exists */
1908 return __hash_32(y ^ __hash_32(x));
1909 }
1910
1911 #endif
1912
1913 /*
1914 * Return the hash of a string of known length. This is carfully
1915 * designed to match hash_name(), which is the more critical function.
1916 * In particular, we must end by hashing a final word containing 0..7
1917 * payload bytes, to match the way that hash_name() iterates until it
1918 * finds the delimiter after the name.
1919 */
full_name_hash(const void * salt,const char * name,unsigned int len)1920 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1921 {
1922 unsigned long a, x = 0, y = (unsigned long)salt;
1923
1924 for (;;) {
1925 if (!len)
1926 goto done;
1927 a = load_unaligned_zeropad(name);
1928 if (len < sizeof(unsigned long))
1929 break;
1930 HASH_MIX(x, y, a);
1931 name += sizeof(unsigned long);
1932 len -= sizeof(unsigned long);
1933 }
1934 x ^= a & bytemask_from_count(len);
1935 done:
1936 return fold_hash(x, y);
1937 }
1938 EXPORT_SYMBOL(full_name_hash);
1939
1940 /* Return the "hash_len" (hash and length) of a null-terminated string */
hashlen_string(const void * salt,const char * name)1941 u64 hashlen_string(const void *salt, const char *name)
1942 {
1943 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1944 unsigned long adata, mask, len;
1945 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1946
1947 len = 0;
1948 goto inside;
1949
1950 do {
1951 HASH_MIX(x, y, a);
1952 len += sizeof(unsigned long);
1953 inside:
1954 a = load_unaligned_zeropad(name+len);
1955 } while (!has_zero(a, &adata, &constants));
1956
1957 adata = prep_zero_mask(a, adata, &constants);
1958 mask = create_zero_mask(adata);
1959 x ^= a & zero_bytemask(mask);
1960
1961 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1962 }
1963 EXPORT_SYMBOL(hashlen_string);
1964
1965 /*
1966 * Calculate the length and hash of the path component, and
1967 * return the "hash_len" as the result.
1968 */
hash_name(const void * salt,const char * name)1969 static inline u64 hash_name(const void *salt, const char *name)
1970 {
1971 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1972 unsigned long adata, bdata, mask, len;
1973 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1974
1975 len = 0;
1976 goto inside;
1977
1978 do {
1979 HASH_MIX(x, y, a);
1980 len += sizeof(unsigned long);
1981 inside:
1982 a = load_unaligned_zeropad(name+len);
1983 b = a ^ REPEAT_BYTE('/');
1984 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
1985
1986 adata = prep_zero_mask(a, adata, &constants);
1987 bdata = prep_zero_mask(b, bdata, &constants);
1988 mask = create_zero_mask(adata | bdata);
1989 x ^= a & zero_bytemask(mask);
1990
1991 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
1992 }
1993
1994 #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
1995
1996 /* Return the hash of a string of known length */
full_name_hash(const void * salt,const char * name,unsigned int len)1997 unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
1998 {
1999 unsigned long hash = init_name_hash(salt);
2000 while (len--)
2001 hash = partial_name_hash((unsigned char)*name++, hash);
2002 return end_name_hash(hash);
2003 }
2004 EXPORT_SYMBOL(full_name_hash);
2005
2006 /* Return the "hash_len" (hash and length) of a null-terminated string */
hashlen_string(const void * salt,const char * name)2007 u64 hashlen_string(const void *salt, const char *name)
2008 {
2009 unsigned long hash = init_name_hash(salt);
2010 unsigned long len = 0, c;
2011
2012 c = (unsigned char)*name;
2013 while (c) {
2014 len++;
2015 hash = partial_name_hash(c, hash);
2016 c = (unsigned char)name[len];
2017 }
2018 return hashlen_create(end_name_hash(hash), len);
2019 }
2020 EXPORT_SYMBOL(hashlen_string);
2021
2022 /*
2023 * We know there's a real path component here of at least
2024 * one character.
2025 */
hash_name(const void * salt,const char * name)2026 static inline u64 hash_name(const void *salt, const char *name)
2027 {
2028 unsigned long hash = init_name_hash(salt);
2029 unsigned long len = 0, c;
2030
2031 c = (unsigned char)*name;
2032 do {
2033 len++;
2034 hash = partial_name_hash(c, hash);
2035 c = (unsigned char)name[len];
2036 } while (c && c != '/');
2037 return hashlen_create(end_name_hash(hash), len);
2038 }
2039
2040 #endif
2041
2042 /*
2043 * Name resolution.
2044 * This is the basic name resolution function, turning a pathname into
2045 * the final dentry. We expect 'base' to be positive and a directory.
2046 *
2047 * Returns 0 and nd will have valid dentry and mnt on success.
2048 * Returns error and drops reference to input namei data on failure.
2049 */
link_path_walk(const char * name,struct nameidata * nd)2050 static int link_path_walk(const char *name, struct nameidata *nd)
2051 {
2052 int err;
2053
2054 while (*name=='/')
2055 name++;
2056 if (!*name)
2057 return 0;
2058
2059 /* At this point we know we have a real path component. */
2060 for(;;) {
2061 u64 hash_len;
2062 int type;
2063
2064 err = may_lookup(nd);
2065 if (err)
2066 return err;
2067
2068 hash_len = hash_name(nd->path.dentry, name);
2069
2070 type = LAST_NORM;
2071 if (name[0] == '.') switch (hashlen_len(hash_len)) {
2072 case 2:
2073 if (name[1] == '.') {
2074 type = LAST_DOTDOT;
2075 nd->flags |= LOOKUP_JUMPED;
2076 }
2077 break;
2078 case 1:
2079 type = LAST_DOT;
2080 }
2081 if (likely(type == LAST_NORM)) {
2082 struct dentry *parent = nd->path.dentry;
2083 nd->flags &= ~LOOKUP_JUMPED;
2084 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
2085 struct qstr this = { { .hash_len = hash_len }, .name = name };
2086 err = parent->d_op->d_hash(parent, &this);
2087 if (err < 0)
2088 return err;
2089 hash_len = this.hash_len;
2090 name = this.name;
2091 }
2092 }
2093
2094 nd->last.hash_len = hash_len;
2095 nd->last.name = name;
2096 nd->last_type = type;
2097
2098 name += hashlen_len(hash_len);
2099 if (!*name)
2100 goto OK;
2101 /*
2102 * If it wasn't NUL, we know it was '/'. Skip that
2103 * slash, and continue until no more slashes.
2104 */
2105 do {
2106 name++;
2107 } while (unlikely(*name == '/'));
2108 if (unlikely(!*name)) {
2109 OK:
2110 /* pathname body, done */
2111 if (!nd->depth)
2112 return 0;
2113 name = nd->stack[nd->depth - 1].name;
2114 /* trailing symlink, done */
2115 if (!name)
2116 return 0;
2117 /* last component of nested symlink */
2118 err = walk_component(nd, WALK_GET | WALK_PUT);
2119 } else {
2120 err = walk_component(nd, WALK_GET);
2121 }
2122 if (err < 0)
2123 return err;
2124
2125 if (err) {
2126 const char *s = get_link(nd);
2127
2128 if (IS_ERR(s))
2129 return PTR_ERR(s);
2130 err = 0;
2131 if (unlikely(!s)) {
2132 /* jumped */
2133 put_link(nd);
2134 } else {
2135 nd->stack[nd->depth - 1].name = name;
2136 name = s;
2137 continue;
2138 }
2139 }
2140 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2141 if (nd->flags & LOOKUP_RCU) {
2142 if (unlazy_walk(nd, NULL, 0))
2143 return -ECHILD;
2144 }
2145 return -ENOTDIR;
2146 }
2147 }
2148 }
2149
path_init(struct nameidata * nd,unsigned flags)2150 static const char *path_init(struct nameidata *nd, unsigned flags)
2151 {
2152 int retval = 0;
2153 const char *s = nd->name->name;
2154
2155 if (!*s)
2156 flags &= ~LOOKUP_RCU;
2157
2158 nd->last_type = LAST_ROOT; /* if there are only slashes... */
2159 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
2160 nd->depth = 0;
2161 if (flags & LOOKUP_ROOT) {
2162 struct dentry *root = nd->root.dentry;
2163 struct vfsmount *mnt = nd->root.mnt;
2164 struct inode *inode = root->d_inode;
2165 if (*s) {
2166 if (!d_can_lookup(root))
2167 return ERR_PTR(-ENOTDIR);
2168 retval = inode_permission2(mnt, inode, MAY_EXEC);
2169 if (retval)
2170 return ERR_PTR(retval);
2171 }
2172 nd->path = nd->root;
2173 nd->inode = inode;
2174 if (flags & LOOKUP_RCU) {
2175 rcu_read_lock();
2176 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2177 nd->root_seq = nd->seq;
2178 nd->m_seq = read_seqbegin(&mount_lock);
2179 } else {
2180 path_get(&nd->path);
2181 }
2182 return s;
2183 }
2184
2185 nd->root.mnt = NULL;
2186 nd->path.mnt = NULL;
2187 nd->path.dentry = NULL;
2188
2189 nd->m_seq = read_seqbegin(&mount_lock);
2190 if (*s == '/') {
2191 if (flags & LOOKUP_RCU)
2192 rcu_read_lock();
2193 set_root(nd);
2194 if (likely(!nd_jump_root(nd)))
2195 return s;
2196 nd->root.mnt = NULL;
2197 rcu_read_unlock();
2198 return ERR_PTR(-ECHILD);
2199 } else if (nd->dfd == AT_FDCWD) {
2200 if (flags & LOOKUP_RCU) {
2201 struct fs_struct *fs = current->fs;
2202 unsigned seq;
2203
2204 rcu_read_lock();
2205
2206 do {
2207 seq = read_seqcount_begin(&fs->seq);
2208 nd->path = fs->pwd;
2209 nd->inode = nd->path.dentry->d_inode;
2210 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2211 } while (read_seqcount_retry(&fs->seq, seq));
2212 } else {
2213 get_fs_pwd(current->fs, &nd->path);
2214 nd->inode = nd->path.dentry->d_inode;
2215 }
2216 return s;
2217 } else {
2218 /* Caller must check execute permissions on the starting path component */
2219 struct fd f = fdget_raw(nd->dfd);
2220 struct dentry *dentry;
2221
2222 if (!f.file)
2223 return ERR_PTR(-EBADF);
2224
2225 dentry = f.file->f_path.dentry;
2226
2227 if (*s) {
2228 if (!d_can_lookup(dentry)) {
2229 fdput(f);
2230 return ERR_PTR(-ENOTDIR);
2231 }
2232 }
2233
2234 nd->path = f.file->f_path;
2235 if (flags & LOOKUP_RCU) {
2236 rcu_read_lock();
2237 nd->inode = nd->path.dentry->d_inode;
2238 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
2239 } else {
2240 path_get(&nd->path);
2241 nd->inode = nd->path.dentry->d_inode;
2242 }
2243 fdput(f);
2244 return s;
2245 }
2246 }
2247
trailing_symlink(struct nameidata * nd)2248 static const char *trailing_symlink(struct nameidata *nd)
2249 {
2250 const char *s;
2251 int error = may_follow_link(nd);
2252 if (unlikely(error))
2253 return ERR_PTR(error);
2254 nd->flags |= LOOKUP_PARENT;
2255 nd->stack[0].name = NULL;
2256 s = get_link(nd);
2257 return s ? s : "";
2258 }
2259
lookup_last(struct nameidata * nd)2260 static inline int lookup_last(struct nameidata *nd)
2261 {
2262 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2263 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2264
2265 nd->flags &= ~LOOKUP_PARENT;
2266 return walk_component(nd,
2267 nd->flags & LOOKUP_FOLLOW
2268 ? nd->depth
2269 ? WALK_PUT | WALK_GET
2270 : WALK_GET
2271 : 0);
2272 }
2273
2274 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
path_lookupat(struct nameidata * nd,unsigned flags,struct path * path)2275 static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
2276 {
2277 const char *s = path_init(nd, flags);
2278 int err;
2279
2280 if (IS_ERR(s))
2281 return PTR_ERR(s);
2282 while (!(err = link_path_walk(s, nd))
2283 && ((err = lookup_last(nd)) > 0)) {
2284 s = trailing_symlink(nd);
2285 if (IS_ERR(s)) {
2286 err = PTR_ERR(s);
2287 break;
2288 }
2289 }
2290 if (!err)
2291 err = complete_walk(nd);
2292
2293 if (!err && nd->flags & LOOKUP_DIRECTORY)
2294 if (!d_can_lookup(nd->path.dentry))
2295 err = -ENOTDIR;
2296 if (!err) {
2297 *path = nd->path;
2298 nd->path.mnt = NULL;
2299 nd->path.dentry = NULL;
2300 }
2301 terminate_walk(nd);
2302 return err;
2303 }
2304
filename_lookup(int dfd,struct filename * name,unsigned flags,struct path * path,struct path * root)2305 static int filename_lookup(int dfd, struct filename *name, unsigned flags,
2306 struct path *path, struct path *root)
2307 {
2308 int retval;
2309 struct nameidata nd;
2310 if (IS_ERR(name))
2311 return PTR_ERR(name);
2312 if (unlikely(root)) {
2313 nd.root = *root;
2314 flags |= LOOKUP_ROOT;
2315 }
2316 set_nameidata(&nd, dfd, name);
2317 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
2318 if (unlikely(retval == -ECHILD))
2319 retval = path_lookupat(&nd, flags, path);
2320 if (unlikely(retval == -ESTALE))
2321 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
2322
2323 if (likely(!retval))
2324 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
2325 restore_nameidata();
2326 putname(name);
2327 return retval;
2328 }
2329
2330 /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
path_parentat(struct nameidata * nd,unsigned flags,struct path * parent)2331 static int path_parentat(struct nameidata *nd, unsigned flags,
2332 struct path *parent)
2333 {
2334 const char *s = path_init(nd, flags);
2335 int err;
2336 if (IS_ERR(s))
2337 return PTR_ERR(s);
2338 err = link_path_walk(s, nd);
2339 if (!err)
2340 err = complete_walk(nd);
2341 if (!err) {
2342 *parent = nd->path;
2343 nd->path.mnt = NULL;
2344 nd->path.dentry = NULL;
2345 }
2346 terminate_walk(nd);
2347 return err;
2348 }
2349
filename_parentat(int dfd,struct filename * name,unsigned int flags,struct path * parent,struct qstr * last,int * type)2350 static struct filename *filename_parentat(int dfd, struct filename *name,
2351 unsigned int flags, struct path *parent,
2352 struct qstr *last, int *type)
2353 {
2354 int retval;
2355 struct nameidata nd;
2356
2357 if (IS_ERR(name))
2358 return name;
2359 set_nameidata(&nd, dfd, name);
2360 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
2361 if (unlikely(retval == -ECHILD))
2362 retval = path_parentat(&nd, flags, parent);
2363 if (unlikely(retval == -ESTALE))
2364 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
2365 if (likely(!retval)) {
2366 *last = nd.last;
2367 *type = nd.last_type;
2368 audit_inode(name, parent->dentry, LOOKUP_PARENT);
2369 } else {
2370 putname(name);
2371 name = ERR_PTR(retval);
2372 }
2373 restore_nameidata();
2374 return name;
2375 }
2376
2377 /* does lookup, returns the object with parent locked */
kern_path_locked(const char * name,struct path * path)2378 struct dentry *kern_path_locked(const char *name, struct path *path)
2379 {
2380 struct filename *filename;
2381 struct dentry *d;
2382 struct qstr last;
2383 int type;
2384
2385 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2386 &last, &type);
2387 if (IS_ERR(filename))
2388 return ERR_CAST(filename);
2389 if (unlikely(type != LAST_NORM)) {
2390 path_put(path);
2391 putname(filename);
2392 return ERR_PTR(-EINVAL);
2393 }
2394 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
2395 d = __lookup_hash(&last, path->dentry, 0);
2396 if (IS_ERR(d)) {
2397 inode_unlock(path->dentry->d_inode);
2398 path_put(path);
2399 }
2400 putname(filename);
2401 return d;
2402 }
2403
kern_path(const char * name,unsigned int flags,struct path * path)2404 int kern_path(const char *name, unsigned int flags, struct path *path)
2405 {
2406 return filename_lookup(AT_FDCWD, getname_kernel(name),
2407 flags, path, NULL);
2408 }
2409 EXPORT_SYMBOL(kern_path);
2410
2411 /**
2412 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2413 * @dentry: pointer to dentry of the base directory
2414 * @mnt: pointer to vfs mount of the base directory
2415 * @name: pointer to file name
2416 * @flags: lookup flags
2417 * @path: pointer to struct path to fill
2418 */
vfs_path_lookup(struct dentry * dentry,struct vfsmount * mnt,const char * name,unsigned int flags,struct path * path)2419 int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2420 const char *name, unsigned int flags,
2421 struct path *path)
2422 {
2423 struct path root = {.mnt = mnt, .dentry = dentry};
2424 /* the first argument of filename_lookup() is ignored with root */
2425 return filename_lookup(AT_FDCWD, getname_kernel(name),
2426 flags , path, &root);
2427 }
2428 EXPORT_SYMBOL(vfs_path_lookup);
2429
2430 /**
2431 * lookup_one_len - filesystem helper to lookup single pathname component
2432 * @name: pathname component to lookup
2433 * @mnt: mount we are looking up on
2434 * @base: base directory to lookup from
2435 * @len: maximum length @len should be interpreted to
2436 *
2437 * Note that this routine is purely a helper for filesystem usage and should
2438 * not be called by generic code.
2439 *
2440 * The caller must hold base->i_mutex.
2441 */
lookup_one_len2(const char * name,struct vfsmount * mnt,struct dentry * base,int len)2442 struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
2443 {
2444 struct qstr this;
2445 unsigned int c;
2446 int err;
2447
2448 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2449
2450 this.name = name;
2451 this.len = len;
2452 this.hash = full_name_hash(base, name, len);
2453 if (!len)
2454 return ERR_PTR(-EACCES);
2455
2456 if (unlikely(name[0] == '.')) {
2457 if (len < 2 || (len == 2 && name[1] == '.'))
2458 return ERR_PTR(-EACCES);
2459 }
2460
2461 while (len--) {
2462 c = *(const unsigned char *)name++;
2463 if (c == '/' || c == '\0')
2464 return ERR_PTR(-EACCES);
2465 }
2466 /*
2467 * See if the low-level filesystem might want
2468 * to use its own hash..
2469 */
2470 if (base->d_flags & DCACHE_OP_HASH) {
2471 int err = base->d_op->d_hash(base, &this);
2472 if (err < 0)
2473 return ERR_PTR(err);
2474 }
2475
2476 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
2477 if (err)
2478 return ERR_PTR(err);
2479
2480 return __lookup_hash(&this, base, 0);
2481 }
2482 EXPORT_SYMBOL(lookup_one_len2);
2483
lookup_one_len(const char * name,struct dentry * base,int len)2484 struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2485 {
2486 return lookup_one_len2(name, NULL, base, len);
2487 }
2488 EXPORT_SYMBOL(lookup_one_len);
2489
2490 /**
2491 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2492 * @name: pathname component to lookup
2493 * @base: base directory to lookup from
2494 * @len: maximum length @len should be interpreted to
2495 *
2496 * Note that this routine is purely a helper for filesystem usage and should
2497 * not be called by generic code.
2498 *
2499 * Unlike lookup_one_len, it should be called without the parent
2500 * i_mutex held, and will take the i_mutex itself if necessary.
2501 */
lookup_one_len_unlocked(const char * name,struct dentry * base,int len)2502 struct dentry *lookup_one_len_unlocked(const char *name,
2503 struct dentry *base, int len)
2504 {
2505 struct qstr this;
2506 unsigned int c;
2507 int err;
2508 struct dentry *ret;
2509
2510 this.name = name;
2511 this.len = len;
2512 this.hash = full_name_hash(base, name, len);
2513 if (!len)
2514 return ERR_PTR(-EACCES);
2515
2516 if (unlikely(name[0] == '.')) {
2517 if (len < 2 || (len == 2 && name[1] == '.'))
2518 return ERR_PTR(-EACCES);
2519 }
2520
2521 while (len--) {
2522 c = *(const unsigned char *)name++;
2523 if (c == '/' || c == '\0')
2524 return ERR_PTR(-EACCES);
2525 }
2526 /*
2527 * See if the low-level filesystem might want
2528 * to use its own hash..
2529 */
2530 if (base->d_flags & DCACHE_OP_HASH) {
2531 int err = base->d_op->d_hash(base, &this);
2532 if (err < 0)
2533 return ERR_PTR(err);
2534 }
2535
2536 err = inode_permission(base->d_inode, MAY_EXEC);
2537 if (err)
2538 return ERR_PTR(err);
2539
2540 ret = lookup_dcache(&this, base, 0);
2541 if (!ret)
2542 ret = lookup_slow(&this, base, 0);
2543 return ret;
2544 }
2545 EXPORT_SYMBOL(lookup_one_len_unlocked);
2546
2547 #ifdef CONFIG_UNIX98_PTYS
path_pts(struct path * path)2548 int path_pts(struct path *path)
2549 {
2550 /* Find something mounted on "pts" in the same directory as
2551 * the input path.
2552 */
2553 struct dentry *child, *parent;
2554 struct qstr this;
2555 int ret;
2556
2557 ret = path_parent_directory(path);
2558 if (ret)
2559 return ret;
2560
2561 parent = path->dentry;
2562 this.name = "pts";
2563 this.len = 3;
2564 child = d_hash_and_lookup(parent, &this);
2565 if (!child)
2566 return -ENOENT;
2567
2568 path->dentry = child;
2569 dput(parent);
2570 follow_mount(path);
2571 return 0;
2572 }
2573 #endif
2574
user_path_at_empty(int dfd,const char __user * name,unsigned flags,struct path * path,int * empty)2575 int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2576 struct path *path, int *empty)
2577 {
2578 return filename_lookup(dfd, getname_flags(name, flags, empty),
2579 flags, path, NULL);
2580 }
2581 EXPORT_SYMBOL(user_path_at_empty);
2582
2583 /*
2584 * NB: most callers don't do anything directly with the reference to the
2585 * to struct filename, but the nd->last pointer points into the name string
2586 * allocated by getname. So we must hold the reference to it until all
2587 * path-walking is complete.
2588 */
2589 static inline struct filename *
user_path_parent(int dfd,const char __user * path,struct path * parent,struct qstr * last,int * type,unsigned int flags)2590 user_path_parent(int dfd, const char __user *path,
2591 struct path *parent,
2592 struct qstr *last,
2593 int *type,
2594 unsigned int flags)
2595 {
2596 /* only LOOKUP_REVAL is allowed in extra flags */
2597 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2598 parent, last, type);
2599 }
2600
2601 /**
2602 * mountpoint_last - look up last component for umount
2603 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2604 * @path: pointer to container for result
2605 *
2606 * This is a special lookup_last function just for umount. In this case, we
2607 * need to resolve the path without doing any revalidation.
2608 *
2609 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2610 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2611 * in almost all cases, this lookup will be served out of the dcache. The only
2612 * cases where it won't are if nd->last refers to a symlink or the path is
2613 * bogus and it doesn't exist.
2614 *
2615 * Returns:
2616 * -error: if there was an error during lookup. This includes -ENOENT if the
2617 * lookup found a negative dentry. The nd->path reference will also be
2618 * put in this case.
2619 *
2620 * 0: if we successfully resolved nd->path and found it to not to be a
2621 * symlink that needs to be followed. "path" will also be populated.
2622 * The nd->path reference will also be put.
2623 *
2624 * 1: if we successfully resolved nd->last and found it to be a symlink
2625 * that needs to be followed. "path" will be populated with the path
2626 * to the link, and nd->path will *not* be put.
2627 */
2628 static int
mountpoint_last(struct nameidata * nd,struct path * path)2629 mountpoint_last(struct nameidata *nd, struct path *path)
2630 {
2631 int error = 0;
2632 struct dentry *dentry;
2633 struct dentry *dir = nd->path.dentry;
2634
2635 /* If we're in rcuwalk, drop out of it to handle last component */
2636 if (nd->flags & LOOKUP_RCU) {
2637 if (unlazy_walk(nd, NULL, 0))
2638 return -ECHILD;
2639 }
2640
2641 nd->flags &= ~LOOKUP_PARENT;
2642
2643 if (unlikely(nd->last_type != LAST_NORM)) {
2644 error = handle_dots(nd, nd->last_type);
2645 if (error)
2646 return error;
2647 dentry = dget(nd->path.dentry);
2648 } else {
2649 dentry = d_lookup(dir, &nd->last);
2650 if (!dentry) {
2651 /*
2652 * No cached dentry. Mounted dentries are pinned in the
2653 * cache, so that means that this dentry is probably
2654 * a symlink or the path doesn't actually point
2655 * to a mounted dentry.
2656 */
2657 dentry = lookup_slow(&nd->last, dir,
2658 nd->flags | LOOKUP_NO_REVAL);
2659 if (IS_ERR(dentry))
2660 return PTR_ERR(dentry);
2661 }
2662 }
2663 if (d_is_negative(dentry)) {
2664 dput(dentry);
2665 return -ENOENT;
2666 }
2667 if (nd->depth)
2668 put_link(nd);
2669 path->dentry = dentry;
2670 path->mnt = nd->path.mnt;
2671 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2672 d_backing_inode(dentry), 0);
2673 if (unlikely(error))
2674 return error;
2675 mntget(path->mnt);
2676 follow_mount(path);
2677 return 0;
2678 }
2679
2680 /**
2681 * path_mountpoint - look up a path to be umounted
2682 * @nd: lookup context
2683 * @flags: lookup flags
2684 * @path: pointer to container for result
2685 *
2686 * Look up the given name, but don't attempt to revalidate the last component.
2687 * Returns 0 and "path" will be valid on success; Returns error otherwise.
2688 */
2689 static int
path_mountpoint(struct nameidata * nd,unsigned flags,struct path * path)2690 path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
2691 {
2692 const char *s = path_init(nd, flags);
2693 int err;
2694 if (IS_ERR(s))
2695 return PTR_ERR(s);
2696 while (!(err = link_path_walk(s, nd)) &&
2697 (err = mountpoint_last(nd, path)) > 0) {
2698 s = trailing_symlink(nd);
2699 if (IS_ERR(s)) {
2700 err = PTR_ERR(s);
2701 break;
2702 }
2703 }
2704 terminate_walk(nd);
2705 return err;
2706 }
2707
2708 static int
filename_mountpoint(int dfd,struct filename * name,struct path * path,unsigned int flags)2709 filename_mountpoint(int dfd, struct filename *name, struct path *path,
2710 unsigned int flags)
2711 {
2712 struct nameidata nd;
2713 int error;
2714 if (IS_ERR(name))
2715 return PTR_ERR(name);
2716 set_nameidata(&nd, dfd, name);
2717 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
2718 if (unlikely(error == -ECHILD))
2719 error = path_mountpoint(&nd, flags, path);
2720 if (unlikely(error == -ESTALE))
2721 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
2722 if (likely(!error))
2723 audit_inode(name, path->dentry, 0);
2724 restore_nameidata();
2725 putname(name);
2726 return error;
2727 }
2728
2729 /**
2730 * user_path_mountpoint_at - lookup a path from userland in order to umount it
2731 * @dfd: directory file descriptor
2732 * @name: pathname from userland
2733 * @flags: lookup flags
2734 * @path: pointer to container to hold result
2735 *
2736 * A umount is a special case for path walking. We're not actually interested
2737 * in the inode in this situation, and ESTALE errors can be a problem. We
2738 * simply want track down the dentry and vfsmount attached at the mountpoint
2739 * and avoid revalidating the last component.
2740 *
2741 * Returns 0 and populates "path" on success.
2742 */
2743 int
user_path_mountpoint_at(int dfd,const char __user * name,unsigned int flags,struct path * path)2744 user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
2745 struct path *path)
2746 {
2747 return filename_mountpoint(dfd, getname(name), path, flags);
2748 }
2749
2750 int
kern_path_mountpoint(int dfd,const char * name,struct path * path,unsigned int flags)2751 kern_path_mountpoint(int dfd, const char *name, struct path *path,
2752 unsigned int flags)
2753 {
2754 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
2755 }
2756 EXPORT_SYMBOL(kern_path_mountpoint);
2757
__check_sticky(struct inode * dir,struct inode * inode)2758 int __check_sticky(struct inode *dir, struct inode *inode)
2759 {
2760 kuid_t fsuid = current_fsuid();
2761
2762 if (uid_eq(inode->i_uid, fsuid))
2763 return 0;
2764 if (uid_eq(dir->i_uid, fsuid))
2765 return 0;
2766 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
2767 }
2768 EXPORT_SYMBOL(__check_sticky);
2769
2770 /*
2771 * Check whether we can remove a link victim from directory dir, check
2772 * whether the type of victim is right.
2773 * 1. We can't do it if dir is read-only (done in permission())
2774 * 2. We should have write and exec permissions on dir
2775 * 3. We can't remove anything from append-only dir
2776 * 4. We can't do anything with immutable dir (done in permission())
2777 * 5. If the sticky bit on dir is set we should either
2778 * a. be owner of dir, or
2779 * b. be owner of victim, or
2780 * c. have CAP_FOWNER capability
2781 * 6. If the victim is append-only or immutable we can't do antyhing with
2782 * links pointing to it.
2783 * 7. If the victim has an unknown uid or gid we can't change the inode.
2784 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2785 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2786 * 10. We can't remove a root or mountpoint.
2787 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
2788 * nfs_async_unlink().
2789 */
may_delete(struct vfsmount * mnt,struct inode * dir,struct dentry * victim,bool isdir)2790 static int may_delete(struct vfsmount *mnt, struct inode *dir, struct dentry *victim, bool isdir)
2791 {
2792 struct inode *inode = d_backing_inode(victim);
2793 int error;
2794
2795 if (d_is_negative(victim))
2796 return -ENOENT;
2797 BUG_ON(!inode);
2798
2799 BUG_ON(victim->d_parent->d_inode != dir);
2800 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
2801
2802 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
2803 if (error)
2804 return error;
2805 if (IS_APPEND(dir))
2806 return -EPERM;
2807
2808 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
2809 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
2810 return -EPERM;
2811 if (isdir) {
2812 if (!d_is_dir(victim))
2813 return -ENOTDIR;
2814 if (IS_ROOT(victim))
2815 return -EBUSY;
2816 } else if (d_is_dir(victim))
2817 return -EISDIR;
2818 if (IS_DEADDIR(dir))
2819 return -ENOENT;
2820 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2821 return -EBUSY;
2822 return 0;
2823 }
2824
2825 /* Check whether we can create an object with dentry child in directory
2826 * dir.
2827 * 1. We can't do it if child already exists (open has special treatment for
2828 * this case, but since we are inlined it's OK)
2829 * 2. We can't do it if dir is read-only (done in permission())
2830 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2831 * 4. We should have write and exec permissions on dir
2832 * 5. We can't do it if dir is immutable (done in permission())
2833 */
may_create(struct vfsmount * mnt,struct inode * dir,struct dentry * child)2834 static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
2835 {
2836 struct user_namespace *s_user_ns;
2837 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
2838 if (child->d_inode)
2839 return -EEXIST;
2840 if (IS_DEADDIR(dir))
2841 return -ENOENT;
2842 s_user_ns = dir->i_sb->s_user_ns;
2843 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2844 !kgid_has_mapping(s_user_ns, current_fsgid()))
2845 return -EOVERFLOW;
2846 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
2847 }
2848
2849 /*
2850 * p1 and p2 should be directories on the same fs.
2851 */
lock_rename(struct dentry * p1,struct dentry * p2)2852 struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2853 {
2854 struct dentry *p;
2855
2856 if (p1 == p2) {
2857 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2858 return NULL;
2859 }
2860
2861 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
2862
2863 p = d_ancestor(p2, p1);
2864 if (p) {
2865 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2866 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
2867 return p;
2868 }
2869
2870 p = d_ancestor(p1, p2);
2871 if (p) {
2872 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2873 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
2874 return p;
2875 }
2876
2877 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2878 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
2879 return NULL;
2880 }
2881 EXPORT_SYMBOL(lock_rename);
2882
unlock_rename(struct dentry * p1,struct dentry * p2)2883 void unlock_rename(struct dentry *p1, struct dentry *p2)
2884 {
2885 inode_unlock(p1->d_inode);
2886 if (p1 != p2) {
2887 inode_unlock(p2->d_inode);
2888 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
2889 }
2890 }
2891 EXPORT_SYMBOL(unlock_rename);
2892
vfs_create2(struct vfsmount * mnt,struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)2893 int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
2894 umode_t mode, bool want_excl)
2895 {
2896 int error = may_create(mnt, dir, dentry);
2897 if (error)
2898 return error;
2899
2900 if (!dir->i_op->create)
2901 return -EACCES; /* shouldn't it be ENOSYS? */
2902 mode &= S_IALLUGO;
2903 mode |= S_IFREG;
2904 error = security_inode_create(dir, dentry, mode);
2905 if (error)
2906 return error;
2907 error = dir->i_op->create(dir, dentry, mode, want_excl);
2908 if (!error)
2909 fsnotify_create(dir, dentry);
2910 return error;
2911 }
2912 EXPORT_SYMBOL(vfs_create2);
2913
vfs_create(struct inode * dir,struct dentry * dentry,umode_t mode,bool want_excl)2914 int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2915 bool want_excl)
2916 {
2917 return vfs_create2(NULL, dir, dentry, mode, want_excl);
2918 }
2919 EXPORT_SYMBOL(vfs_create);
2920
may_open_dev(const struct path * path)2921 bool may_open_dev(const struct path *path)
2922 {
2923 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2924 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2925 }
2926
may_open(struct path * path,int acc_mode,int flag)2927 static int may_open(struct path *path, int acc_mode, int flag)
2928 {
2929 struct dentry *dentry = path->dentry;
2930 struct vfsmount *mnt = path->mnt;
2931 struct inode *inode = dentry->d_inode;
2932 int error;
2933
2934 if (!inode)
2935 return -ENOENT;
2936
2937 switch (inode->i_mode & S_IFMT) {
2938 case S_IFLNK:
2939 return -ELOOP;
2940 case S_IFDIR:
2941 if (acc_mode & MAY_WRITE)
2942 return -EISDIR;
2943 break;
2944 case S_IFBLK:
2945 case S_IFCHR:
2946 if (!may_open_dev(path))
2947 return -EACCES;
2948 /*FALLTHRU*/
2949 case S_IFIFO:
2950 case S_IFSOCK:
2951 flag &= ~O_TRUNC;
2952 break;
2953 }
2954
2955 error = inode_permission2(mnt, inode, MAY_OPEN | acc_mode);
2956 if (error)
2957 return error;
2958
2959 /*
2960 * An append-only file must be opened in append mode for writing.
2961 */
2962 if (IS_APPEND(inode)) {
2963 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
2964 return -EPERM;
2965 if (flag & O_TRUNC)
2966 return -EPERM;
2967 }
2968
2969 /* O_NOATIME can only be set by the owner or superuser */
2970 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
2971 return -EPERM;
2972
2973 return 0;
2974 }
2975
handle_truncate(struct file * filp)2976 static int handle_truncate(struct file *filp)
2977 {
2978 struct path *path = &filp->f_path;
2979 struct inode *inode = path->dentry->d_inode;
2980 int error = get_write_access(inode);
2981 if (error)
2982 return error;
2983 /*
2984 * Refuse to truncate files with mandatory locks held on them.
2985 */
2986 error = locks_verify_locked(filp);
2987 if (!error)
2988 error = security_path_truncate(path);
2989 if (!error) {
2990 error = do_truncate2(path->mnt, path->dentry, 0,
2991 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
2992 filp);
2993 }
2994 put_write_access(inode);
2995 return error;
2996 }
2997
open_to_namei_flags(int flag)2998 static inline int open_to_namei_flags(int flag)
2999 {
3000 if ((flag & O_ACCMODE) == 3)
3001 flag--;
3002 return flag;
3003 }
3004
may_o_create(const struct path * dir,struct dentry * dentry,umode_t mode)3005 static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
3006 {
3007 struct user_namespace *s_user_ns;
3008 int error = security_path_mknod(dir, dentry, mode, 0);
3009 if (error)
3010 return error;
3011
3012 s_user_ns = dir->dentry->d_sb->s_user_ns;
3013 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3014 !kgid_has_mapping(s_user_ns, current_fsgid()))
3015 return -EOVERFLOW;
3016
3017 error = inode_permission2(dir->mnt, dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3018 if (error)
3019 return error;
3020
3021 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3022 }
3023
3024 /*
3025 * Attempt to atomically look up, create and open a file from a negative
3026 * dentry.
3027 *
3028 * Returns 0 if successful. The file will have been created and attached to
3029 * @file by the filesystem calling finish_open().
3030 *
3031 * Returns 1 if the file was looked up only or didn't need creating. The
3032 * caller will need to perform the open themselves. @path will have been
3033 * updated to point to the new dentry. This may be negative.
3034 *
3035 * Returns an error code otherwise.
3036 */
atomic_open(struct nameidata * nd,struct dentry * dentry,struct path * path,struct file * file,const struct open_flags * op,int open_flag,umode_t mode,int * opened)3037 static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3038 struct path *path, struct file *file,
3039 const struct open_flags *op,
3040 int open_flag, umode_t mode,
3041 int *opened)
3042 {
3043 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
3044 struct inode *dir = nd->path.dentry->d_inode;
3045 int error;
3046
3047 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
3048 open_flag &= ~O_TRUNC;
3049
3050 if (nd->flags & LOOKUP_DIRECTORY)
3051 open_flag |= O_DIRECTORY;
3052
3053 file->f_path.dentry = DENTRY_NOT_SET;
3054 file->f_path.mnt = nd->path.mnt;
3055 error = dir->i_op->atomic_open(dir, dentry, file,
3056 open_to_namei_flags(open_flag),
3057 mode, opened);
3058 d_lookup_done(dentry);
3059 if (!error) {
3060 /*
3061 * We didn't have the inode before the open, so check open
3062 * permission here.
3063 */
3064 int acc_mode = op->acc_mode;
3065 if (*opened & FILE_CREATED) {
3066 WARN_ON(!(open_flag & O_CREAT));
3067 fsnotify_create(dir, dentry);
3068 acc_mode = 0;
3069 }
3070 error = may_open(&file->f_path, acc_mode, open_flag);
3071 if (WARN_ON(error > 0))
3072 error = -EINVAL;
3073 } else if (error > 0) {
3074 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
3075 error = -EIO;
3076 } else {
3077 if (file->f_path.dentry) {
3078 dput(dentry);
3079 dentry = file->f_path.dentry;
3080 }
3081 if (*opened & FILE_CREATED)
3082 fsnotify_create(dir, dentry);
3083 if (unlikely(d_is_negative(dentry))) {
3084 error = -ENOENT;
3085 } else {
3086 path->dentry = dentry;
3087 path->mnt = nd->path.mnt;
3088 return 1;
3089 }
3090 }
3091 }
3092 dput(dentry);
3093 return error;
3094 }
3095
3096 /*
3097 * Look up and maybe create and open the last component.
3098 *
3099 * Must be called with i_mutex held on parent.
3100 *
3101 * Returns 0 if the file was successfully atomically created (if necessary) and
3102 * opened. In this case the file will be returned attached to @file.
3103 *
3104 * Returns 1 if the file was not completely opened at this time, though lookups
3105 * and creations will have been performed and the dentry returned in @path will
3106 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3107 * specified then a negative dentry may be returned.
3108 *
3109 * An error code is returned otherwise.
3110 *
3111 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3112 * cleared otherwise prior to returning.
3113 */
lookup_open(struct nameidata * nd,struct path * path,struct file * file,const struct open_flags * op,bool got_write,int * opened)3114 static int lookup_open(struct nameidata *nd, struct path *path,
3115 struct file *file,
3116 const struct open_flags *op,
3117 bool got_write, int *opened)
3118 {
3119 struct dentry *dir = nd->path.dentry;
3120 struct inode *dir_inode = dir->d_inode;
3121 int open_flag = op->open_flag;
3122 struct dentry *dentry;
3123 int error, create_error = 0;
3124 umode_t mode = op->mode;
3125 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
3126
3127 if (unlikely(IS_DEADDIR(dir_inode)))
3128 return -ENOENT;
3129
3130 *opened &= ~FILE_CREATED;
3131 dentry = d_lookup(dir, &nd->last);
3132 for (;;) {
3133 if (!dentry) {
3134 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3135 if (IS_ERR(dentry))
3136 return PTR_ERR(dentry);
3137 }
3138 if (d_in_lookup(dentry))
3139 break;
3140
3141 if (!(dentry->d_flags & DCACHE_OP_REVALIDATE))
3142 break;
3143
3144 error = d_revalidate(dentry, nd->flags);
3145 if (likely(error > 0))
3146 break;
3147 if (error)
3148 goto out_dput;
3149 d_invalidate(dentry);
3150 dput(dentry);
3151 dentry = NULL;
3152 }
3153 if (dentry->d_inode) {
3154 /* Cached positive dentry: will open in f_op->open */
3155 goto out_no_open;
3156 }
3157
3158 /*
3159 * Checking write permission is tricky, bacuse we don't know if we are
3160 * going to actually need it: O_CREAT opens should work as long as the
3161 * file exists. But checking existence breaks atomicity. The trick is
3162 * to check access and if not granted clear O_CREAT from the flags.
3163 *
3164 * Another problem is returing the "right" error value (e.g. for an
3165 * O_EXCL open we want to return EEXIST not EROFS).
3166 */
3167 if (open_flag & O_CREAT) {
3168 if (!IS_POSIXACL(dir->d_inode))
3169 mode &= ~current_umask();
3170 if (unlikely(!got_write)) {
3171 create_error = -EROFS;
3172 open_flag &= ~O_CREAT;
3173 if (open_flag & (O_EXCL | O_TRUNC))
3174 goto no_open;
3175 /* No side effects, safe to clear O_CREAT */
3176 } else {
3177 create_error = may_o_create(&nd->path, dentry, mode);
3178 if (create_error) {
3179 open_flag &= ~O_CREAT;
3180 if (open_flag & O_EXCL)
3181 goto no_open;
3182 }
3183 }
3184 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3185 unlikely(!got_write)) {
3186 /*
3187 * No O_CREATE -> atomicity not a requirement -> fall
3188 * back to lookup + open
3189 */
3190 goto no_open;
3191 }
3192
3193 if (dir_inode->i_op->atomic_open) {
3194 error = atomic_open(nd, dentry, path, file, op, open_flag,
3195 mode, opened);
3196 if (unlikely(error == -ENOENT) && create_error)
3197 error = create_error;
3198 return error;
3199 }
3200
3201 no_open:
3202 if (d_in_lookup(dentry)) {
3203 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3204 nd->flags);
3205 d_lookup_done(dentry);
3206 if (unlikely(res)) {
3207 if (IS_ERR(res)) {
3208 error = PTR_ERR(res);
3209 goto out_dput;
3210 }
3211 dput(dentry);
3212 dentry = res;
3213 }
3214 }
3215
3216 /* Negative dentry, just create the file */
3217 if (!dentry->d_inode && (open_flag & O_CREAT)) {
3218 *opened |= FILE_CREATED;
3219 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
3220 if (!dir_inode->i_op->create) {
3221 error = -EACCES;
3222 goto out_dput;
3223 }
3224 error = dir_inode->i_op->create(dir_inode, dentry, mode,
3225 open_flag & O_EXCL);
3226 if (error)
3227 goto out_dput;
3228 fsnotify_create(dir_inode, dentry);
3229 }
3230 if (unlikely(create_error) && !dentry->d_inode) {
3231 error = create_error;
3232 goto out_dput;
3233 }
3234 out_no_open:
3235 path->dentry = dentry;
3236 path->mnt = nd->path.mnt;
3237 return 1;
3238
3239 out_dput:
3240 dput(dentry);
3241 return error;
3242 }
3243
3244 /*
3245 * Handle the last step of open()
3246 */
do_last(struct nameidata * nd,struct file * file,const struct open_flags * op,int * opened)3247 static int do_last(struct nameidata *nd,
3248 struct file *file, const struct open_flags *op,
3249 int *opened)
3250 {
3251 struct dentry *dir = nd->path.dentry;
3252 int open_flag = op->open_flag;
3253 bool will_truncate = (open_flag & O_TRUNC) != 0;
3254 bool got_write = false;
3255 int acc_mode = op->acc_mode;
3256 unsigned seq;
3257 struct inode *inode;
3258 struct path path;
3259 int error;
3260
3261 nd->flags &= ~LOOKUP_PARENT;
3262 nd->flags |= op->intent;
3263
3264 if (nd->last_type != LAST_NORM) {
3265 error = handle_dots(nd, nd->last_type);
3266 if (unlikely(error))
3267 return error;
3268 goto finish_open;
3269 }
3270
3271 if (!(open_flag & O_CREAT)) {
3272 if (nd->last.name[nd->last.len])
3273 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3274 /* we _can_ be in RCU mode here */
3275 error = lookup_fast(nd, &path, &inode, &seq);
3276 if (likely(error > 0))
3277 goto finish_lookup;
3278
3279 if (error < 0)
3280 return error;
3281
3282 BUG_ON(nd->inode != dir->d_inode);
3283 BUG_ON(nd->flags & LOOKUP_RCU);
3284 } else {
3285 /* create side of things */
3286 /*
3287 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3288 * has been cleared when we got to the last component we are
3289 * about to look up
3290 */
3291 error = complete_walk(nd);
3292 if (error)
3293 return error;
3294
3295 audit_inode(nd->name, dir, LOOKUP_PARENT);
3296 /* trailing slashes? */
3297 if (unlikely(nd->last.name[nd->last.len]))
3298 return -EISDIR;
3299 }
3300
3301 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
3302 error = mnt_want_write(nd->path.mnt);
3303 if (!error)
3304 got_write = true;
3305 /*
3306 * do _not_ fail yet - we might not need that or fail with
3307 * a different error; let lookup_open() decide; we'll be
3308 * dropping this one anyway.
3309 */
3310 }
3311 if (open_flag & O_CREAT)
3312 inode_lock(dir->d_inode);
3313 else
3314 inode_lock_shared(dir->d_inode);
3315 error = lookup_open(nd, &path, file, op, got_write, opened);
3316 if (open_flag & O_CREAT)
3317 inode_unlock(dir->d_inode);
3318 else
3319 inode_unlock_shared(dir->d_inode);
3320
3321 if (error <= 0) {
3322 if (error)
3323 goto out;
3324
3325 if ((*opened & FILE_CREATED) ||
3326 !S_ISREG(file_inode(file)->i_mode))
3327 will_truncate = false;
3328
3329 audit_inode(nd->name, file->f_path.dentry, 0);
3330 goto opened;
3331 }
3332
3333 if (*opened & FILE_CREATED) {
3334 /* Don't check for write permission, don't truncate */
3335 open_flag &= ~O_TRUNC;
3336 will_truncate = false;
3337 acc_mode = 0;
3338 path_to_nameidata(&path, nd);
3339 goto finish_open_created;
3340 }
3341
3342 /*
3343 * If atomic_open() acquired write access it is dropped now due to
3344 * possible mount and symlink following (this might be optimized away if
3345 * necessary...)
3346 */
3347 if (got_write) {
3348 mnt_drop_write(nd->path.mnt);
3349 got_write = false;
3350 }
3351
3352 error = follow_managed(&path, nd);
3353 if (unlikely(error < 0))
3354 return error;
3355
3356 if (unlikely(d_is_negative(path.dentry))) {
3357 path_to_nameidata(&path, nd);
3358 return -ENOENT;
3359 }
3360
3361 /*
3362 * create/update audit record if it already exists.
3363 */
3364 audit_inode(nd->name, path.dentry, 0);
3365
3366 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3367 path_to_nameidata(&path, nd);
3368 return -EEXIST;
3369 }
3370
3371 seq = 0; /* out of RCU mode, so the value doesn't matter */
3372 inode = d_backing_inode(path.dentry);
3373 finish_lookup:
3374 if (nd->depth)
3375 put_link(nd);
3376 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3377 inode, seq);
3378 if (unlikely(error))
3379 return error;
3380
3381 path_to_nameidata(&path, nd);
3382 nd->inode = inode;
3383 nd->seq = seq;
3384 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
3385 finish_open:
3386 error = complete_walk(nd);
3387 if (error)
3388 return error;
3389 audit_inode(nd->name, nd->path.dentry, 0);
3390 error = -EISDIR;
3391 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
3392 goto out;
3393 error = -ENOTDIR;
3394 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
3395 goto out;
3396 if (!d_is_reg(nd->path.dentry))
3397 will_truncate = false;
3398
3399 if (will_truncate) {
3400 error = mnt_want_write(nd->path.mnt);
3401 if (error)
3402 goto out;
3403 got_write = true;
3404 }
3405 finish_open_created:
3406 error = may_open(&nd->path, acc_mode, open_flag);
3407 if (error)
3408 goto out;
3409 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3410 error = vfs_open(&nd->path, file, current_cred());
3411 if (error)
3412 goto out;
3413 *opened |= FILE_OPENED;
3414 opened:
3415 error = open_check_o_direct(file);
3416 if (!error)
3417 error = ima_file_check(file, op->acc_mode, *opened);
3418 if (!error && will_truncate)
3419 error = handle_truncate(file);
3420 out:
3421 if (unlikely(error) && (*opened & FILE_OPENED))
3422 fput(file);
3423 if (unlikely(error > 0)) {
3424 WARN_ON(1);
3425 error = -EINVAL;
3426 }
3427 if (got_write)
3428 mnt_drop_write(nd->path.mnt);
3429 return error;
3430 }
3431
do_tmpfile(struct nameidata * nd,unsigned flags,const struct open_flags * op,struct file * file,int * opened)3432 static int do_tmpfile(struct nameidata *nd, unsigned flags,
3433 const struct open_flags *op,
3434 struct file *file, int *opened)
3435 {
3436 static const struct qstr name = QSTR_INIT("/", 1);
3437 struct dentry *child;
3438 struct inode *dir;
3439 struct path path;
3440 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
3441 if (unlikely(error))
3442 return error;
3443 error = mnt_want_write(path.mnt);
3444 if (unlikely(error))
3445 goto out;
3446 dir = path.dentry->d_inode;
3447 /* we want directory to be writable */
3448 error = inode_permission2(nd->path.mnt, dir, MAY_WRITE | MAY_EXEC);
3449 if (error)
3450 goto out2;
3451 if (!dir->i_op->tmpfile) {
3452 error = -EOPNOTSUPP;
3453 goto out2;
3454 }
3455 child = d_alloc(path.dentry, &name);
3456 if (unlikely(!child)) {
3457 error = -ENOMEM;
3458 goto out2;
3459 }
3460 dput(path.dentry);
3461 path.dentry = child;
3462 error = dir->i_op->tmpfile(dir, child, op->mode);
3463 if (error)
3464 goto out2;
3465 audit_inode(nd->name, child, 0);
3466 /* Don't check for other permissions, the inode was just created */
3467 error = may_open(&path, 0, op->open_flag);
3468 if (error)
3469 goto out2;
3470 file->f_path.mnt = path.mnt;
3471 error = finish_open(file, child, NULL, opened);
3472 if (error)
3473 goto out2;
3474 error = open_check_o_direct(file);
3475 if (error) {
3476 fput(file);
3477 } else if (!(op->open_flag & O_EXCL)) {
3478 struct inode *inode = file_inode(file);
3479 spin_lock(&inode->i_lock);
3480 inode->i_state |= I_LINKABLE;
3481 spin_unlock(&inode->i_lock);
3482 }
3483 out2:
3484 mnt_drop_write(path.mnt);
3485 out:
3486 path_put(&path);
3487 return error;
3488 }
3489
do_o_path(struct nameidata * nd,unsigned flags,struct file * file)3490 static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3491 {
3492 struct path path;
3493 int error = path_lookupat(nd, flags, &path);
3494 if (!error) {
3495 audit_inode(nd->name, path.dentry, 0);
3496 error = vfs_open(&path, file, current_cred());
3497 path_put(&path);
3498 }
3499 return error;
3500 }
3501
path_openat(struct nameidata * nd,const struct open_flags * op,unsigned flags)3502 static struct file *path_openat(struct nameidata *nd,
3503 const struct open_flags *op, unsigned flags)
3504 {
3505 const char *s;
3506 struct file *file;
3507 int opened = 0;
3508 int error;
3509
3510 file = get_empty_filp();
3511 if (IS_ERR(file))
3512 return file;
3513
3514 file->f_flags = op->open_flag;
3515
3516 if (unlikely(file->f_flags & __O_TMPFILE)) {
3517 error = do_tmpfile(nd, flags, op, file, &opened);
3518 goto out2;
3519 }
3520
3521 if (unlikely(file->f_flags & O_PATH)) {
3522 error = do_o_path(nd, flags, file);
3523 if (!error)
3524 opened |= FILE_OPENED;
3525 goto out2;
3526 }
3527
3528 s = path_init(nd, flags);
3529 if (IS_ERR(s)) {
3530 put_filp(file);
3531 return ERR_CAST(s);
3532 }
3533 while (!(error = link_path_walk(s, nd)) &&
3534 (error = do_last(nd, file, op, &opened)) > 0) {
3535 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3536 s = trailing_symlink(nd);
3537 if (IS_ERR(s)) {
3538 error = PTR_ERR(s);
3539 break;
3540 }
3541 }
3542 terminate_walk(nd);
3543 out2:
3544 if (!(opened & FILE_OPENED)) {
3545 BUG_ON(!error);
3546 put_filp(file);
3547 }
3548 if (unlikely(error)) {
3549 if (error == -EOPENSTALE) {
3550 if (flags & LOOKUP_RCU)
3551 error = -ECHILD;
3552 else
3553 error = -ESTALE;
3554 }
3555 file = ERR_PTR(error);
3556 }
3557 return file;
3558 }
3559
do_filp_open(int dfd,struct filename * pathname,const struct open_flags * op)3560 struct file *do_filp_open(int dfd, struct filename *pathname,
3561 const struct open_flags *op)
3562 {
3563 struct nameidata nd;
3564 int flags = op->lookup_flags;
3565 struct file *filp;
3566
3567 set_nameidata(&nd, dfd, pathname);
3568 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
3569 if (unlikely(filp == ERR_PTR(-ECHILD)))
3570 filp = path_openat(&nd, op, flags);
3571 if (unlikely(filp == ERR_PTR(-ESTALE)))
3572 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
3573 restore_nameidata();
3574 return filp;
3575 }
3576
do_file_open_root(struct dentry * dentry,struct vfsmount * mnt,const char * name,const struct open_flags * op)3577 struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
3578 const char *name, const struct open_flags *op)
3579 {
3580 struct nameidata nd;
3581 struct file *file;
3582 struct filename *filename;
3583 int flags = op->lookup_flags | LOOKUP_ROOT;
3584
3585 nd.root.mnt = mnt;
3586 nd.root.dentry = dentry;
3587
3588 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
3589 return ERR_PTR(-ELOOP);
3590
3591 filename = getname_kernel(name);
3592 if (IS_ERR(filename))
3593 return ERR_CAST(filename);
3594
3595 set_nameidata(&nd, -1, filename);
3596 file = path_openat(&nd, op, flags | LOOKUP_RCU);
3597 if (unlikely(file == ERR_PTR(-ECHILD)))
3598 file = path_openat(&nd, op, flags);
3599 if (unlikely(file == ERR_PTR(-ESTALE)))
3600 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
3601 restore_nameidata();
3602 putname(filename);
3603 return file;
3604 }
3605
filename_create(int dfd,struct filename * name,struct path * path,unsigned int lookup_flags)3606 static struct dentry *filename_create(int dfd, struct filename *name,
3607 struct path *path, unsigned int lookup_flags)
3608 {
3609 struct dentry *dentry = ERR_PTR(-EEXIST);
3610 struct qstr last;
3611 int type;
3612 int err2;
3613 int error;
3614 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3615
3616 /*
3617 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3618 * other flags passed in are ignored!
3619 */
3620 lookup_flags &= LOOKUP_REVAL;
3621
3622 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3623 if (IS_ERR(name))
3624 return ERR_CAST(name);
3625
3626 /*
3627 * Yucky last component or no last component at all?
3628 * (foo/., foo/.., /////)
3629 */
3630 if (unlikely(type != LAST_NORM))
3631 goto out;
3632
3633 /* don't fail immediately if it's r/o, at least try to report other errors */
3634 err2 = mnt_want_write(path->mnt);
3635 /*
3636 * Do the final lookup.
3637 */
3638 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
3639 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
3640 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
3641 if (IS_ERR(dentry))
3642 goto unlock;
3643
3644 error = -EEXIST;
3645 if (d_is_positive(dentry))
3646 goto fail;
3647
3648 /*
3649 * Special case - lookup gave negative, but... we had foo/bar/
3650 * From the vfs_mknod() POV we just have a negative dentry -
3651 * all is fine. Let's be bastards - you had / on the end, you've
3652 * been asking for (non-existent) directory. -ENOENT for you.
3653 */
3654 if (unlikely(!is_dir && last.name[last.len])) {
3655 error = -ENOENT;
3656 goto fail;
3657 }
3658 if (unlikely(err2)) {
3659 error = err2;
3660 goto fail;
3661 }
3662 putname(name);
3663 return dentry;
3664 fail:
3665 dput(dentry);
3666 dentry = ERR_PTR(error);
3667 unlock:
3668 inode_unlock(path->dentry->d_inode);
3669 if (!err2)
3670 mnt_drop_write(path->mnt);
3671 out:
3672 path_put(path);
3673 putname(name);
3674 return dentry;
3675 }
3676
kern_path_create(int dfd,const char * pathname,struct path * path,unsigned int lookup_flags)3677 struct dentry *kern_path_create(int dfd, const char *pathname,
3678 struct path *path, unsigned int lookup_flags)
3679 {
3680 return filename_create(dfd, getname_kernel(pathname),
3681 path, lookup_flags);
3682 }
3683 EXPORT_SYMBOL(kern_path_create);
3684
done_path_create(struct path * path,struct dentry * dentry)3685 void done_path_create(struct path *path, struct dentry *dentry)
3686 {
3687 dput(dentry);
3688 inode_unlock(path->dentry->d_inode);
3689 mnt_drop_write(path->mnt);
3690 path_put(path);
3691 }
3692 EXPORT_SYMBOL(done_path_create);
3693
user_path_create(int dfd,const char __user * pathname,struct path * path,unsigned int lookup_flags)3694 inline struct dentry *user_path_create(int dfd, const char __user *pathname,
3695 struct path *path, unsigned int lookup_flags)
3696 {
3697 return filename_create(dfd, getname(pathname), path, lookup_flags);
3698 }
3699 EXPORT_SYMBOL(user_path_create);
3700
vfs_mknod2(struct vfsmount * mnt,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)3701 int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3702 {
3703 int error = may_create(mnt, dir, dentry);
3704
3705 if (error)
3706 return error;
3707
3708 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
3709 return -EPERM;
3710
3711 if (!dir->i_op->mknod)
3712 return -EPERM;
3713
3714 error = devcgroup_inode_mknod(mode, dev);
3715 if (error)
3716 return error;
3717
3718 error = security_inode_mknod(dir, dentry, mode, dev);
3719 if (error)
3720 return error;
3721
3722 error = dir->i_op->mknod(dir, dentry, mode, dev);
3723 if (!error)
3724 fsnotify_create(dir, dentry);
3725 return error;
3726 }
3727 EXPORT_SYMBOL(vfs_mknod2);
3728
vfs_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)3729 int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3730 {
3731 return vfs_mknod2(NULL, dir, dentry, mode, dev);
3732 }
3733 EXPORT_SYMBOL(vfs_mknod);
3734
may_mknod(umode_t mode)3735 static int may_mknod(umode_t mode)
3736 {
3737 switch (mode & S_IFMT) {
3738 case S_IFREG:
3739 case S_IFCHR:
3740 case S_IFBLK:
3741 case S_IFIFO:
3742 case S_IFSOCK:
3743 case 0: /* zero mode translates to S_IFREG */
3744 return 0;
3745 case S_IFDIR:
3746 return -EPERM;
3747 default:
3748 return -EINVAL;
3749 }
3750 }
3751
SYSCALL_DEFINE4(mknodat,int,dfd,const char __user *,filename,umode_t,mode,unsigned,dev)3752 SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3753 unsigned, dev)
3754 {
3755 struct dentry *dentry;
3756 struct path path;
3757 int error;
3758 unsigned int lookup_flags = 0;
3759
3760 error = may_mknod(mode);
3761 if (error)
3762 return error;
3763 retry:
3764 dentry = user_path_create(dfd, filename, &path, lookup_flags);
3765 if (IS_ERR(dentry))
3766 return PTR_ERR(dentry);
3767
3768 if (!IS_POSIXACL(path.dentry->d_inode))
3769 mode &= ~current_umask();
3770 error = security_path_mknod(&path, dentry, mode, dev);
3771 if (error)
3772 goto out;
3773 switch (mode & S_IFMT) {
3774 case 0: case S_IFREG:
3775 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true);
3776 if (!error)
3777 ima_post_path_mknod(dentry);
3778 break;
3779 case S_IFCHR: case S_IFBLK:
3780 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
3781 new_decode_dev(dev));
3782 break;
3783 case S_IFIFO: case S_IFSOCK:
3784 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
3785 break;
3786 }
3787 out:
3788 done_path_create(&path, dentry);
3789 if (retry_estale(error, lookup_flags)) {
3790 lookup_flags |= LOOKUP_REVAL;
3791 goto retry;
3792 }
3793 return error;
3794 }
3795
SYSCALL_DEFINE3(mknod,const char __user *,filename,umode_t,mode,unsigned,dev)3796 SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
3797 {
3798 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3799 }
3800
vfs_mkdir2(struct vfsmount * mnt,struct inode * dir,struct dentry * dentry,umode_t mode)3801 int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
3802 {
3803 int error = may_create(mnt, dir, dentry);
3804 unsigned max_links = dir->i_sb->s_max_links;
3805
3806 if (error)
3807 return error;
3808
3809 if (!dir->i_op->mkdir)
3810 return -EPERM;
3811
3812 mode &= (S_IRWXUGO|S_ISVTX);
3813 error = security_inode_mkdir(dir, dentry, mode);
3814 if (error)
3815 return error;
3816
3817 if (max_links && dir->i_nlink >= max_links)
3818 return -EMLINK;
3819
3820 error = dir->i_op->mkdir(dir, dentry, mode);
3821 if (!error)
3822 fsnotify_mkdir(dir, dentry);
3823 return error;
3824 }
3825 EXPORT_SYMBOL(vfs_mkdir2);
3826
vfs_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)3827 int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3828 {
3829 return vfs_mkdir2(NULL, dir, dentry, mode);
3830 }
3831 EXPORT_SYMBOL(vfs_mkdir);
3832
SYSCALL_DEFINE3(mkdirat,int,dfd,const char __user *,pathname,umode_t,mode)3833 SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3834 {
3835 struct dentry *dentry;
3836 struct path path;
3837 int error;
3838 unsigned int lookup_flags = LOOKUP_DIRECTORY;
3839
3840 retry:
3841 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
3842 if (IS_ERR(dentry))
3843 return PTR_ERR(dentry);
3844
3845 if (!IS_POSIXACL(path.dentry->d_inode))
3846 mode &= ~current_umask();
3847 error = security_path_mkdir(&path, dentry, mode);
3848 if (!error)
3849 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
3850 done_path_create(&path, dentry);
3851 if (retry_estale(error, lookup_flags)) {
3852 lookup_flags |= LOOKUP_REVAL;
3853 goto retry;
3854 }
3855 return error;
3856 }
3857
SYSCALL_DEFINE2(mkdir,const char __user *,pathname,umode_t,mode)3858 SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
3859 {
3860 return sys_mkdirat(AT_FDCWD, pathname, mode);
3861 }
3862
vfs_rmdir2(struct vfsmount * mnt,struct inode * dir,struct dentry * dentry)3863 int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
3864 {
3865 int error = may_delete(mnt, dir, dentry, 1);
3866
3867 if (error)
3868 return error;
3869
3870 if (!dir->i_op->rmdir)
3871 return -EPERM;
3872
3873 dget(dentry);
3874 inode_lock(dentry->d_inode);
3875
3876 error = -EBUSY;
3877 if (is_local_mountpoint(dentry))
3878 goto out;
3879
3880 error = security_inode_rmdir(dir, dentry);
3881 if (error)
3882 goto out;
3883
3884 shrink_dcache_parent(dentry);
3885 error = dir->i_op->rmdir(dir, dentry);
3886 if (error)
3887 goto out;
3888
3889 dentry->d_inode->i_flags |= S_DEAD;
3890 dont_mount(dentry);
3891 detach_mounts(dentry);
3892
3893 out:
3894 inode_unlock(dentry->d_inode);
3895 dput(dentry);
3896 if (!error)
3897 d_delete(dentry);
3898 return error;
3899 }
3900 EXPORT_SYMBOL(vfs_rmdir2);
3901
vfs_rmdir(struct inode * dir,struct dentry * dentry)3902 int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3903 {
3904 return vfs_rmdir2(NULL, dir, dentry);
3905 }
3906 EXPORT_SYMBOL(vfs_rmdir);
3907
do_rmdir(int dfd,const char __user * pathname)3908 static long do_rmdir(int dfd, const char __user *pathname)
3909 {
3910 int error = 0;
3911 struct filename *name;
3912 struct dentry *dentry;
3913 struct path path;
3914 struct qstr last;
3915 int type;
3916 unsigned int lookup_flags = 0;
3917 retry:
3918 name = user_path_parent(dfd, pathname,
3919 &path, &last, &type, lookup_flags);
3920 if (IS_ERR(name))
3921 return PTR_ERR(name);
3922
3923 switch (type) {
3924 case LAST_DOTDOT:
3925 error = -ENOTEMPTY;
3926 goto exit1;
3927 case LAST_DOT:
3928 error = -EINVAL;
3929 goto exit1;
3930 case LAST_ROOT:
3931 error = -EBUSY;
3932 goto exit1;
3933 }
3934
3935 error = mnt_want_write(path.mnt);
3936 if (error)
3937 goto exit1;
3938
3939 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
3940 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
3941 error = PTR_ERR(dentry);
3942 if (IS_ERR(dentry))
3943 goto exit2;
3944 if (!dentry->d_inode) {
3945 error = -ENOENT;
3946 goto exit3;
3947 }
3948 error = security_path_rmdir(&path, dentry);
3949 if (error)
3950 goto exit3;
3951 error = vfs_rmdir2(path.mnt, path.dentry->d_inode, dentry);
3952 exit3:
3953 dput(dentry);
3954 exit2:
3955 inode_unlock(path.dentry->d_inode);
3956 mnt_drop_write(path.mnt);
3957 exit1:
3958 path_put(&path);
3959 putname(name);
3960 if (retry_estale(error, lookup_flags)) {
3961 lookup_flags |= LOOKUP_REVAL;
3962 goto retry;
3963 }
3964 return error;
3965 }
3966
SYSCALL_DEFINE1(rmdir,const char __user *,pathname)3967 SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
3968 {
3969 return do_rmdir(AT_FDCWD, pathname);
3970 }
3971
3972 /**
3973 * vfs_unlink - unlink a filesystem object
3974 * @dir: parent directory
3975 * @dentry: victim
3976 * @delegated_inode: returns victim inode, if the inode is delegated.
3977 *
3978 * The caller must hold dir->i_mutex.
3979 *
3980 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3981 * return a reference to the inode in delegated_inode. The caller
3982 * should then break the delegation on that inode and retry. Because
3983 * breaking a delegation may take a long time, the caller should drop
3984 * dir->i_mutex before doing so.
3985 *
3986 * Alternatively, a caller may pass NULL for delegated_inode. This may
3987 * be appropriate for callers that expect the underlying filesystem not
3988 * to be NFS exported.
3989 */
vfs_unlink2(struct vfsmount * mnt,struct inode * dir,struct dentry * dentry,struct inode ** delegated_inode)3990 int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
3991 {
3992 struct inode *target = dentry->d_inode;
3993 int error = may_delete(mnt, dir, dentry, 0);
3994
3995 if (error)
3996 return error;
3997
3998 if (!dir->i_op->unlink)
3999 return -EPERM;
4000
4001 inode_lock(target);
4002 if (is_local_mountpoint(dentry))
4003 error = -EBUSY;
4004 else {
4005 error = security_inode_unlink(dir, dentry);
4006 if (!error) {
4007 error = try_break_deleg(target, delegated_inode);
4008 if (error)
4009 goto out;
4010 error = dir->i_op->unlink(dir, dentry);
4011 if (!error) {
4012 dont_mount(dentry);
4013 detach_mounts(dentry);
4014 }
4015 }
4016 }
4017 out:
4018 inode_unlock(target);
4019
4020 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4021 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
4022 fsnotify_link_count(target);
4023 d_delete(dentry);
4024 }
4025
4026 return error;
4027 }
4028 EXPORT_SYMBOL(vfs_unlink2);
4029
vfs_unlink(struct inode * dir,struct dentry * dentry,struct inode ** delegated_inode)4030 int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
4031 {
4032 return vfs_unlink2(NULL, dir, dentry, delegated_inode);
4033 }
4034 EXPORT_SYMBOL(vfs_unlink);
4035
4036 /*
4037 * Make sure that the actual truncation of the file will occur outside its
4038 * directory's i_mutex. Truncate can take a long time if there is a lot of
4039 * writeout happening, and we don't want to prevent access to the directory
4040 * while waiting on the I/O.
4041 */
do_unlinkat(int dfd,const char __user * pathname)4042 static long do_unlinkat(int dfd, const char __user *pathname)
4043 {
4044 int error;
4045 struct filename *name;
4046 struct dentry *dentry;
4047 struct path path;
4048 struct qstr last;
4049 int type;
4050 struct inode *inode = NULL;
4051 struct inode *delegated_inode = NULL;
4052 unsigned int lookup_flags = 0;
4053 retry:
4054 name = user_path_parent(dfd, pathname,
4055 &path, &last, &type, lookup_flags);
4056 if (IS_ERR(name))
4057 return PTR_ERR(name);
4058
4059 error = -EISDIR;
4060 if (type != LAST_NORM)
4061 goto exit1;
4062
4063 error = mnt_want_write(path.mnt);
4064 if (error)
4065 goto exit1;
4066 retry_deleg:
4067 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
4068 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
4069 error = PTR_ERR(dentry);
4070 if (!IS_ERR(dentry)) {
4071 /* Why not before? Because we want correct error value */
4072 if (last.name[last.len])
4073 goto slashes;
4074 inode = dentry->d_inode;
4075 if (d_is_negative(dentry))
4076 goto slashes;
4077 ihold(inode);
4078 error = security_path_unlink(&path, dentry);
4079 if (error)
4080 goto exit2;
4081 error = vfs_unlink2(path.mnt, path.dentry->d_inode, dentry, &delegated_inode);
4082 exit2:
4083 dput(dentry);
4084 }
4085 inode_unlock(path.dentry->d_inode);
4086 if (inode)
4087 iput(inode); /* truncate the inode here */
4088 inode = NULL;
4089 if (delegated_inode) {
4090 error = break_deleg_wait(&delegated_inode);
4091 if (!error)
4092 goto retry_deleg;
4093 }
4094 mnt_drop_write(path.mnt);
4095 exit1:
4096 path_put(&path);
4097 putname(name);
4098 if (retry_estale(error, lookup_flags)) {
4099 lookup_flags |= LOOKUP_REVAL;
4100 inode = NULL;
4101 goto retry;
4102 }
4103 return error;
4104
4105 slashes:
4106 if (d_is_negative(dentry))
4107 error = -ENOENT;
4108 else if (d_is_dir(dentry))
4109 error = -EISDIR;
4110 else
4111 error = -ENOTDIR;
4112 goto exit2;
4113 }
4114
SYSCALL_DEFINE3(unlinkat,int,dfd,const char __user *,pathname,int,flag)4115 SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
4116 {
4117 if ((flag & ~AT_REMOVEDIR) != 0)
4118 return -EINVAL;
4119
4120 if (flag & AT_REMOVEDIR)
4121 return do_rmdir(dfd, pathname);
4122
4123 return do_unlinkat(dfd, pathname);
4124 }
4125
SYSCALL_DEFINE1(unlink,const char __user *,pathname)4126 SYSCALL_DEFINE1(unlink, const char __user *, pathname)
4127 {
4128 return do_unlinkat(AT_FDCWD, pathname);
4129 }
4130
vfs_symlink2(struct vfsmount * mnt,struct inode * dir,struct dentry * dentry,const char * oldname)4131 int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
4132 {
4133 int error = may_create(mnt, dir, dentry);
4134
4135 if (error)
4136 return error;
4137
4138 if (!dir->i_op->symlink)
4139 return -EPERM;
4140
4141 error = security_inode_symlink(dir, dentry, oldname);
4142 if (error)
4143 return error;
4144
4145 error = dir->i_op->symlink(dir, dentry, oldname);
4146 if (!error)
4147 fsnotify_create(dir, dentry);
4148 return error;
4149 }
4150 EXPORT_SYMBOL(vfs_symlink2);
4151
vfs_symlink(struct inode * dir,struct dentry * dentry,const char * oldname)4152 int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4153 {
4154 return vfs_symlink2(NULL, dir, dentry, oldname);
4155 }
4156 EXPORT_SYMBOL(vfs_symlink);
4157
SYSCALL_DEFINE3(symlinkat,const char __user *,oldname,int,newdfd,const char __user *,newname)4158 SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4159 int, newdfd, const char __user *, newname)
4160 {
4161 int error;
4162 struct filename *from;
4163 struct dentry *dentry;
4164 struct path path;
4165 unsigned int lookup_flags = 0;
4166
4167 from = getname(oldname);
4168 if (IS_ERR(from))
4169 return PTR_ERR(from);
4170 retry:
4171 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
4172 error = PTR_ERR(dentry);
4173 if (IS_ERR(dentry))
4174 goto out_putname;
4175
4176 error = security_path_symlink(&path, dentry, from->name);
4177 if (!error)
4178 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name);
4179 done_path_create(&path, dentry);
4180 if (retry_estale(error, lookup_flags)) {
4181 lookup_flags |= LOOKUP_REVAL;
4182 goto retry;
4183 }
4184 out_putname:
4185 putname(from);
4186 return error;
4187 }
4188
SYSCALL_DEFINE2(symlink,const char __user *,oldname,const char __user *,newname)4189 SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
4190 {
4191 return sys_symlinkat(oldname, AT_FDCWD, newname);
4192 }
4193
4194 /**
4195 * vfs_link - create a new link
4196 * @old_dentry: object to be linked
4197 * @dir: new parent
4198 * @new_dentry: where to create the new link
4199 * @delegated_inode: returns inode needing a delegation break
4200 *
4201 * The caller must hold dir->i_mutex
4202 *
4203 * If vfs_link discovers a delegation on the to-be-linked file in need
4204 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4205 * inode in delegated_inode. The caller should then break the delegation
4206 * and retry. Because breaking a delegation may take a long time, the
4207 * caller should drop the i_mutex before doing so.
4208 *
4209 * Alternatively, a caller may pass NULL for delegated_inode. This may
4210 * be appropriate for callers that expect the underlying filesystem not
4211 * to be NFS exported.
4212 */
vfs_link2(struct vfsmount * mnt,struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry,struct inode ** delegated_inode)4213 int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4214 {
4215 struct inode *inode = old_dentry->d_inode;
4216 unsigned max_links = dir->i_sb->s_max_links;
4217 int error;
4218
4219 if (!inode)
4220 return -ENOENT;
4221
4222 error = may_create(mnt, dir, new_dentry);
4223 if (error)
4224 return error;
4225
4226 if (dir->i_sb != inode->i_sb)
4227 return -EXDEV;
4228
4229 /*
4230 * A link to an append-only or immutable file cannot be created.
4231 */
4232 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4233 return -EPERM;
4234 /*
4235 * Updating the link count will likely cause i_uid and i_gid to
4236 * be writen back improperly if their true value is unknown to
4237 * the vfs.
4238 */
4239 if (HAS_UNMAPPED_ID(inode))
4240 return -EPERM;
4241 if (!dir->i_op->link)
4242 return -EPERM;
4243 if (S_ISDIR(inode->i_mode))
4244 return -EPERM;
4245
4246 error = security_inode_link(old_dentry, dir, new_dentry);
4247 if (error)
4248 return error;
4249
4250 inode_lock(inode);
4251 /* Make sure we don't allow creating hardlink to an unlinked file */
4252 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
4253 error = -ENOENT;
4254 else if (max_links && inode->i_nlink >= max_links)
4255 error = -EMLINK;
4256 else {
4257 error = try_break_deleg(inode, delegated_inode);
4258 if (!error)
4259 error = dir->i_op->link(old_dentry, dir, new_dentry);
4260 }
4261
4262 if (!error && (inode->i_state & I_LINKABLE)) {
4263 spin_lock(&inode->i_lock);
4264 inode->i_state &= ~I_LINKABLE;
4265 spin_unlock(&inode->i_lock);
4266 }
4267 inode_unlock(inode);
4268 if (!error)
4269 fsnotify_link(dir, inode, new_dentry);
4270 return error;
4271 }
4272 EXPORT_SYMBOL(vfs_link2);
4273
vfs_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry,struct inode ** delegated_inode)4274 int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4275 {
4276 return vfs_link2(NULL, old_dentry, dir, new_dentry, delegated_inode);
4277 }
4278 EXPORT_SYMBOL(vfs_link);
4279
4280 /*
4281 * Hardlinks are often used in delicate situations. We avoid
4282 * security-related surprises by not following symlinks on the
4283 * newname. --KAB
4284 *
4285 * We don't follow them on the oldname either to be compatible
4286 * with linux 2.0, and to avoid hard-linking to directories
4287 * and other special files. --ADM
4288 */
SYSCALL_DEFINE5(linkat,int,olddfd,const char __user *,oldname,int,newdfd,const char __user *,newname,int,flags)4289 SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4290 int, newdfd, const char __user *, newname, int, flags)
4291 {
4292 struct dentry *new_dentry;
4293 struct path old_path, new_path;
4294 struct inode *delegated_inode = NULL;
4295 int how = 0;
4296 int error;
4297
4298 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
4299 return -EINVAL;
4300 /*
4301 * To use null names we require CAP_DAC_READ_SEARCH
4302 * This ensures that not everyone will be able to create
4303 * handlink using the passed filedescriptor.
4304 */
4305 if (flags & AT_EMPTY_PATH) {
4306 if (!capable(CAP_DAC_READ_SEARCH))
4307 return -ENOENT;
4308 how = LOOKUP_EMPTY;
4309 }
4310
4311 if (flags & AT_SYMLINK_FOLLOW)
4312 how |= LOOKUP_FOLLOW;
4313 retry:
4314 error = user_path_at(olddfd, oldname, how, &old_path);
4315 if (error)
4316 return error;
4317
4318 new_dentry = user_path_create(newdfd, newname, &new_path,
4319 (how & LOOKUP_REVAL));
4320 error = PTR_ERR(new_dentry);
4321 if (IS_ERR(new_dentry))
4322 goto out;
4323
4324 error = -EXDEV;
4325 if (old_path.mnt != new_path.mnt)
4326 goto out_dput;
4327 error = may_linkat(&old_path);
4328 if (unlikely(error))
4329 goto out_dput;
4330 error = security_path_link(old_path.dentry, &new_path, new_dentry);
4331 if (error)
4332 goto out_dput;
4333 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
4334 out_dput:
4335 done_path_create(&new_path, new_dentry);
4336 if (delegated_inode) {
4337 error = break_deleg_wait(&delegated_inode);
4338 if (!error) {
4339 path_put(&old_path);
4340 goto retry;
4341 }
4342 }
4343 if (retry_estale(error, how)) {
4344 path_put(&old_path);
4345 how |= LOOKUP_REVAL;
4346 goto retry;
4347 }
4348 out:
4349 path_put(&old_path);
4350
4351 return error;
4352 }
4353
SYSCALL_DEFINE2(link,const char __user *,oldname,const char __user *,newname)4354 SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
4355 {
4356 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4357 }
4358
4359 /**
4360 * vfs_rename - rename a filesystem object
4361 * @old_dir: parent of source
4362 * @old_dentry: source
4363 * @new_dir: parent of destination
4364 * @new_dentry: destination
4365 * @delegated_inode: returns an inode needing a delegation break
4366 * @flags: rename flags
4367 *
4368 * The caller must hold multiple mutexes--see lock_rename()).
4369 *
4370 * If vfs_rename discovers a delegation in need of breaking at either
4371 * the source or destination, it will return -EWOULDBLOCK and return a
4372 * reference to the inode in delegated_inode. The caller should then
4373 * break the delegation and retry. Because breaking a delegation may
4374 * take a long time, the caller should drop all locks before doing
4375 * so.
4376 *
4377 * Alternatively, a caller may pass NULL for delegated_inode. This may
4378 * be appropriate for callers that expect the underlying filesystem not
4379 * to be NFS exported.
4380 *
4381 * The worst of all namespace operations - renaming directory. "Perverted"
4382 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4383 * Problems:
4384 * a) we can get into loop creation.
4385 * b) race potential - two innocent renames can create a loop together.
4386 * That's where 4.4 screws up. Current fix: serialization on
4387 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
4388 * story.
4389 * c) we have to lock _four_ objects - parents and victim (if it exists),
4390 * and source (if it is not a directory).
4391 * And that - after we got ->i_mutex on parents (until then we don't know
4392 * whether the target exists). Solution: try to be smart with locking
4393 * order for inodes. We rely on the fact that tree topology may change
4394 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
4395 * move will be locked. Thus we can rank directories by the tree
4396 * (ancestors first) and rank all non-directories after them.
4397 * That works since everybody except rename does "lock parent, lookup,
4398 * lock child" and rename is under ->s_vfs_rename_mutex.
4399 * HOWEVER, it relies on the assumption that any object with ->lookup()
4400 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4401 * we'd better make sure that there's no link(2) for them.
4402 * d) conversion from fhandle to dentry may come in the wrong moment - when
4403 * we are removing the target. Solution: we will have to grab ->i_mutex
4404 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
4405 * ->i_mutex on parents, which works but leads to some truly excessive
4406 * locking].
4407 */
vfs_rename2(struct vfsmount * mnt,struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,struct inode ** delegated_inode,unsigned int flags)4408 int vfs_rename2(struct vfsmount *mnt,
4409 struct inode *old_dir, struct dentry *old_dentry,
4410 struct inode *new_dir, struct dentry *new_dentry,
4411 struct inode **delegated_inode, unsigned int flags)
4412 {
4413 int error;
4414 bool is_dir = d_is_dir(old_dentry);
4415 struct inode *source = old_dentry->d_inode;
4416 struct inode *target = new_dentry->d_inode;
4417 bool new_is_dir = false;
4418 unsigned max_links = new_dir->i_sb->s_max_links;
4419 struct name_snapshot old_name;
4420
4421 /*
4422 * Check source == target.
4423 * On overlayfs need to look at underlying inodes.
4424 */
4425 if (d_real_inode(old_dentry) == d_real_inode(new_dentry))
4426 return 0;
4427
4428 error = may_delete(mnt, old_dir, old_dentry, is_dir);
4429 if (error)
4430 return error;
4431
4432 if (!target) {
4433 error = may_create(mnt, new_dir, new_dentry);
4434 } else {
4435 new_is_dir = d_is_dir(new_dentry);
4436
4437 if (!(flags & RENAME_EXCHANGE))
4438 error = may_delete(mnt, new_dir, new_dentry, is_dir);
4439 else
4440 error = may_delete(mnt, new_dir, new_dentry, new_is_dir);
4441 }
4442 if (error)
4443 return error;
4444
4445 if (!old_dir->i_op->rename)
4446 return -EPERM;
4447
4448 /*
4449 * If we are going to change the parent - check write permissions,
4450 * we'll need to flip '..'.
4451 */
4452 if (new_dir != old_dir) {
4453 if (is_dir) {
4454 error = inode_permission2(mnt, source, MAY_WRITE);
4455 if (error)
4456 return error;
4457 }
4458 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4459 error = inode_permission2(mnt, target, MAY_WRITE);
4460 if (error)
4461 return error;
4462 }
4463 }
4464
4465 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4466 flags);
4467 if (error)
4468 return error;
4469
4470 take_dentry_name_snapshot(&old_name, old_dentry);
4471 dget(new_dentry);
4472 if (!is_dir || (flags & RENAME_EXCHANGE))
4473 lock_two_nondirectories(source, target);
4474 else if (target)
4475 inode_lock(target);
4476
4477 error = -EBUSY;
4478 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
4479 goto out;
4480
4481 if (max_links && new_dir != old_dir) {
4482 error = -EMLINK;
4483 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
4484 goto out;
4485 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4486 old_dir->i_nlink >= max_links)
4487 goto out;
4488 }
4489 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4490 shrink_dcache_parent(new_dentry);
4491 if (!is_dir) {
4492 error = try_break_deleg(source, delegated_inode);
4493 if (error)
4494 goto out;
4495 }
4496 if (target && !new_is_dir) {
4497 error = try_break_deleg(target, delegated_inode);
4498 if (error)
4499 goto out;
4500 }
4501 error = old_dir->i_op->rename(old_dir, old_dentry,
4502 new_dir, new_dentry, flags);
4503 if (error)
4504 goto out;
4505
4506 if (!(flags & RENAME_EXCHANGE) && target) {
4507 if (is_dir)
4508 target->i_flags |= S_DEAD;
4509 dont_mount(new_dentry);
4510 detach_mounts(new_dentry);
4511 }
4512 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4513 if (!(flags & RENAME_EXCHANGE))
4514 d_move(old_dentry, new_dentry);
4515 else
4516 d_exchange(old_dentry, new_dentry);
4517 }
4518 out:
4519 if (!is_dir || (flags & RENAME_EXCHANGE))
4520 unlock_two_nondirectories(source, target);
4521 else if (target)
4522 inode_unlock(target);
4523 dput(new_dentry);
4524 if (!error) {
4525 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
4526 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4527 if (flags & RENAME_EXCHANGE) {
4528 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4529 new_is_dir, NULL, new_dentry);
4530 }
4531 }
4532 release_dentry_name_snapshot(&old_name);
4533
4534 return error;
4535 }
4536 EXPORT_SYMBOL(vfs_rename2);
4537
vfs_rename(struct inode * old_dir,struct dentry * old_dentry,struct inode * new_dir,struct dentry * new_dentry,struct inode ** delegated_inode,unsigned int flags)4538 int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4539 struct inode *new_dir, struct dentry *new_dentry,
4540 struct inode **delegated_inode, unsigned int flags)
4541 {
4542 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry, delegated_inode, flags);
4543 }
4544 EXPORT_SYMBOL(vfs_rename);
4545
SYSCALL_DEFINE5(renameat2,int,olddfd,const char __user *,oldname,int,newdfd,const char __user *,newname,unsigned int,flags)4546 SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4547 int, newdfd, const char __user *, newname, unsigned int, flags)
4548 {
4549 struct dentry *old_dentry, *new_dentry;
4550 struct dentry *trap;
4551 struct path old_path, new_path;
4552 struct qstr old_last, new_last;
4553 int old_type, new_type;
4554 struct inode *delegated_inode = NULL;
4555 struct filename *from;
4556 struct filename *to;
4557 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
4558 bool should_retry = false;
4559 int error;
4560
4561 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
4562 return -EINVAL;
4563
4564 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4565 (flags & RENAME_EXCHANGE))
4566 return -EINVAL;
4567
4568 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4569 return -EPERM;
4570
4571 if (flags & RENAME_EXCHANGE)
4572 target_flags = 0;
4573
4574 retry:
4575 from = user_path_parent(olddfd, oldname,
4576 &old_path, &old_last, &old_type, lookup_flags);
4577 if (IS_ERR(from)) {
4578 error = PTR_ERR(from);
4579 goto exit;
4580 }
4581
4582 to = user_path_parent(newdfd, newname,
4583 &new_path, &new_last, &new_type, lookup_flags);
4584 if (IS_ERR(to)) {
4585 error = PTR_ERR(to);
4586 goto exit1;
4587 }
4588
4589 error = -EXDEV;
4590 if (old_path.mnt != new_path.mnt)
4591 goto exit2;
4592
4593 error = -EBUSY;
4594 if (old_type != LAST_NORM)
4595 goto exit2;
4596
4597 if (flags & RENAME_NOREPLACE)
4598 error = -EEXIST;
4599 if (new_type != LAST_NORM)
4600 goto exit2;
4601
4602 error = mnt_want_write(old_path.mnt);
4603 if (error)
4604 goto exit2;
4605
4606 retry_deleg:
4607 trap = lock_rename(new_path.dentry, old_path.dentry);
4608
4609 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
4610 error = PTR_ERR(old_dentry);
4611 if (IS_ERR(old_dentry))
4612 goto exit3;
4613 /* source must exist */
4614 error = -ENOENT;
4615 if (d_is_negative(old_dentry))
4616 goto exit4;
4617 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
4618 error = PTR_ERR(new_dentry);
4619 if (IS_ERR(new_dentry))
4620 goto exit4;
4621 error = -EEXIST;
4622 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4623 goto exit5;
4624 if (flags & RENAME_EXCHANGE) {
4625 error = -ENOENT;
4626 if (d_is_negative(new_dentry))
4627 goto exit5;
4628
4629 if (!d_is_dir(new_dentry)) {
4630 error = -ENOTDIR;
4631 if (new_last.name[new_last.len])
4632 goto exit5;
4633 }
4634 }
4635 /* unless the source is a directory trailing slashes give -ENOTDIR */
4636 if (!d_is_dir(old_dentry)) {
4637 error = -ENOTDIR;
4638 if (old_last.name[old_last.len])
4639 goto exit5;
4640 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
4641 goto exit5;
4642 }
4643 /* source should not be ancestor of target */
4644 error = -EINVAL;
4645 if (old_dentry == trap)
4646 goto exit5;
4647 /* target should not be an ancestor of source */
4648 if (!(flags & RENAME_EXCHANGE))
4649 error = -ENOTEMPTY;
4650 if (new_dentry == trap)
4651 goto exit5;
4652
4653 error = security_path_rename(&old_path, old_dentry,
4654 &new_path, new_dentry, flags);
4655 if (error)
4656 goto exit5;
4657 error = vfs_rename2(old_path.mnt, old_path.dentry->d_inode, old_dentry,
4658 new_path.dentry->d_inode, new_dentry,
4659 &delegated_inode, flags);
4660 exit5:
4661 dput(new_dentry);
4662 exit4:
4663 dput(old_dentry);
4664 exit3:
4665 unlock_rename(new_path.dentry, old_path.dentry);
4666 if (delegated_inode) {
4667 error = break_deleg_wait(&delegated_inode);
4668 if (!error)
4669 goto retry_deleg;
4670 }
4671 mnt_drop_write(old_path.mnt);
4672 exit2:
4673 if (retry_estale(error, lookup_flags))
4674 should_retry = true;
4675 path_put(&new_path);
4676 putname(to);
4677 exit1:
4678 path_put(&old_path);
4679 putname(from);
4680 if (should_retry) {
4681 should_retry = false;
4682 lookup_flags |= LOOKUP_REVAL;
4683 goto retry;
4684 }
4685 exit:
4686 return error;
4687 }
4688
SYSCALL_DEFINE4(renameat,int,olddfd,const char __user *,oldname,int,newdfd,const char __user *,newname)4689 SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4690 int, newdfd, const char __user *, newname)
4691 {
4692 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4693 }
4694
SYSCALL_DEFINE2(rename,const char __user *,oldname,const char __user *,newname)4695 SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
4696 {
4697 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
4698 }
4699
vfs_whiteout(struct inode * dir,struct dentry * dentry)4700 int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4701 {
4702 int error = may_create(NULL, dir, dentry);
4703 if (error)
4704 return error;
4705
4706 if (!dir->i_op->mknod)
4707 return -EPERM;
4708
4709 return dir->i_op->mknod(dir, dentry,
4710 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4711 }
4712 EXPORT_SYMBOL(vfs_whiteout);
4713
readlink_copy(char __user * buffer,int buflen,const char * link)4714 int readlink_copy(char __user *buffer, int buflen, const char *link)
4715 {
4716 int len = PTR_ERR(link);
4717 if (IS_ERR(link))
4718 goto out;
4719
4720 len = strlen(link);
4721 if (len > (unsigned) buflen)
4722 len = buflen;
4723 if (copy_to_user(buffer, link, len))
4724 len = -EFAULT;
4725 out:
4726 return len;
4727 }
4728
4729 /*
4730 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
4731 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4732 * for any given inode is up to filesystem.
4733 */
generic_readlink(struct dentry * dentry,char __user * buffer,int buflen)4734 int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4735 {
4736 DEFINE_DELAYED_CALL(done);
4737 struct inode *inode = d_inode(dentry);
4738 const char *link = inode->i_link;
4739 int res;
4740
4741 if (!link) {
4742 link = inode->i_op->get_link(dentry, inode, &done);
4743 if (IS_ERR(link))
4744 return PTR_ERR(link);
4745 }
4746 res = readlink_copy(buffer, buflen, link);
4747 do_delayed_call(&done);
4748 return res;
4749 }
4750 EXPORT_SYMBOL(generic_readlink);
4751
4752 /**
4753 * vfs_get_link - get symlink body
4754 * @dentry: dentry on which to get symbolic link
4755 * @done: caller needs to free returned data with this
4756 *
4757 * Calls security hook and i_op->get_link() on the supplied inode.
4758 *
4759 * It does not touch atime. That's up to the caller if necessary.
4760 *
4761 * Does not work on "special" symlinks like /proc/$$/fd/N
4762 */
vfs_get_link(struct dentry * dentry,struct delayed_call * done)4763 const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4764 {
4765 const char *res = ERR_PTR(-EINVAL);
4766 struct inode *inode = d_inode(dentry);
4767
4768 if (d_is_symlink(dentry)) {
4769 res = ERR_PTR(security_inode_readlink(dentry));
4770 if (!res)
4771 res = inode->i_op->get_link(dentry, inode, done);
4772 }
4773 return res;
4774 }
4775 EXPORT_SYMBOL(vfs_get_link);
4776
4777 /* get the link contents into pagecache */
page_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * callback)4778 const char *page_get_link(struct dentry *dentry, struct inode *inode,
4779 struct delayed_call *callback)
4780 {
4781 char *kaddr;
4782 struct page *page;
4783 struct address_space *mapping = inode->i_mapping;
4784
4785 if (!dentry) {
4786 page = find_get_page(mapping, 0);
4787 if (!page)
4788 return ERR_PTR(-ECHILD);
4789 if (!PageUptodate(page)) {
4790 put_page(page);
4791 return ERR_PTR(-ECHILD);
4792 }
4793 } else {
4794 page = read_mapping_page(mapping, 0, NULL);
4795 if (IS_ERR(page))
4796 return (char*)page;
4797 }
4798 set_delayed_call(callback, page_put_link, page);
4799 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4800 kaddr = page_address(page);
4801 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
4802 return kaddr;
4803 }
4804
4805 EXPORT_SYMBOL(page_get_link);
4806
page_put_link(void * arg)4807 void page_put_link(void *arg)
4808 {
4809 put_page(arg);
4810 }
4811 EXPORT_SYMBOL(page_put_link);
4812
page_readlink(struct dentry * dentry,char __user * buffer,int buflen)4813 int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4814 {
4815 DEFINE_DELAYED_CALL(done);
4816 int res = readlink_copy(buffer, buflen,
4817 page_get_link(dentry, d_inode(dentry),
4818 &done));
4819 do_delayed_call(&done);
4820 return res;
4821 }
4822 EXPORT_SYMBOL(page_readlink);
4823
4824 /*
4825 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4826 */
__page_symlink(struct inode * inode,const char * symname,int len,int nofs)4827 int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
4828 {
4829 struct address_space *mapping = inode->i_mapping;
4830 struct page *page;
4831 void *fsdata;
4832 int err;
4833 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4834 if (nofs)
4835 flags |= AOP_FLAG_NOFS;
4836
4837 retry:
4838 err = pagecache_write_begin(NULL, mapping, 0, len-1,
4839 flags, &page, &fsdata);
4840 if (err)
4841 goto fail;
4842
4843 memcpy(page_address(page), symname, len-1);
4844
4845 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4846 page, fsdata);
4847 if (err < 0)
4848 goto fail;
4849 if (err < len-1)
4850 goto retry;
4851
4852 mark_inode_dirty(inode);
4853 return 0;
4854 fail:
4855 return err;
4856 }
4857 EXPORT_SYMBOL(__page_symlink);
4858
page_symlink(struct inode * inode,const char * symname,int len)4859 int page_symlink(struct inode *inode, const char *symname, int len)
4860 {
4861 return __page_symlink(inode, symname, len,
4862 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
4863 }
4864 EXPORT_SYMBOL(page_symlink);
4865
4866 const struct inode_operations page_symlink_inode_operations = {
4867 .readlink = generic_readlink,
4868 .get_link = page_get_link,
4869 };
4870 EXPORT_SYMBOL(page_symlink_inode_operations);
4871