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