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