blob: eb4626bad88a7c99e7fa30d54628e29787a1c26b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070037#include <linux/hash.h>
George Spelvin2a18da7a2016-05-23 07:43:58 -040038#include <linux/bitops.h>
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -050039#include <linux/init_task.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41
Eric Parise81e3f42009-12-04 15:47:36 -050042#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050043#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* [Feb-1997 T. Schoebel-Theuer]
46 * Fundamental changes in the pathname lookup mechanisms (namei)
47 * were necessary because of omirr. The reason is that omirr needs
48 * to know the _real_ pathname, not the user-supplied one, in case
49 * of symlinks (and also when transname replacements occur).
50 *
51 * The new code replaces the old recursive symlink resolution with
52 * an iterative one (in case of non-nested symlink chains). It does
53 * this with calls to <fs>_follow_link().
54 * As a side effect, dir_namei(), _namei() and follow_link() are now
55 * replaced with a single function lookup_dentry() that can handle all
56 * the special cases of the former code.
57 *
58 * With the new dcache, the pathname is stored at each inode, at least as
59 * long as the refcount of the inode is positive. As a side effect, the
60 * size of the dcache depends on the inode cache and thus is dynamic.
61 *
62 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
63 * resolution to correspond with current state of the code.
64 *
65 * Note that the symlink resolution is not *completely* iterative.
66 * There is still a significant amount of tail- and mid- recursion in
67 * the algorithm. Also, note that <fs>_readlink() is not used in
68 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
69 * may return different results than <fs>_follow_link(). Many virtual
70 * filesystems (including /proc) exhibit this behavior.
71 */
72
73/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
74 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
75 * and the name already exists in form of a symlink, try to create the new
76 * name indicated by the symlink. The old code always complained that the
77 * name already exists, due to not following the symlink even if its target
78 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030079 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
81 * I don't know which semantics is the right one, since I have no access
82 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
83 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
84 * "old" one. Personally, I think the new semantics is much more logical.
85 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
86 * file does succeed in both HP-UX and SunOs, but not in Solaris
87 * and in the old Linux semantics.
88 */
89
90/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
91 * semantics. See the comments in "open_namei" and "do_link" below.
92 *
93 * [10-Sep-98 Alan Modra] Another symlink change.
94 */
95
96/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
97 * inside the path - always follow.
98 * in the last component in creation/removal/renaming - never follow.
99 * if LOOKUP_FOLLOW passed - follow.
100 * if the pathname has trailing slashes - follow.
101 * otherwise - don't follow.
102 * (applied in that order).
103 *
104 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
105 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
106 * During the 2.4 we need to fix the userland stuff depending on it -
107 * hopefully we will be able to get rid of that wart in 2.5. So far only
108 * XEmacs seems to be relying on it...
109 */
110/*
111 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800112 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * any extra contention...
114 */
115
116/* In order to reduce some races, while at the same time doing additional
117 * checking and hopefully speeding things up, we copy filenames to the
118 * kernel data space before using them..
119 *
120 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
121 * PATH_MAX includes the nul terminator --RR.
122 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400123
Al Virofd2f7cb2015-02-22 20:07:13 -0500124#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400125
David Drysdale51f39a12014-12-12 16:57:29 -0800126struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400127getname_flags(const char __user *filename, int flags, int *empty)
128{
Al Viro94b5d262015-02-22 19:38:03 -0500129 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400130 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500131 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Layton7ac86262012-10-10 15:25:28 -0400133 result = audit_reusename(filename);
134 if (result)
135 return result;
136
Jeff Layton7950e382012-10-10 16:43:13 -0400137 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700138 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500139 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jeff Layton7950e382012-10-10 16:43:13 -0400141 /*
142 * First, try to embed the struct filename inside the names_cache
143 * allocation
144 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500145 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400146 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400147
Al Viro94b5d262015-02-22 19:38:03 -0500148 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400149 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500150 __putname(result);
151 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400152 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700153
Jeff Layton7950e382012-10-10 16:43:13 -0400154 /*
155 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
156 * separate struct filename so we can dedicate the entire
157 * names_cache allocation for the pathname, and re-do the copy from
158 * userland.
159 */
Al Viro94b5d262015-02-22 19:38:03 -0500160 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500161 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400162 kname = (char *)result;
163
Al Virofd2f7cb2015-02-22 20:07:13 -0500164 /*
165 * size is chosen that way we to guarantee that
166 * result->iname[0] is within the same object and that
167 * kname can't be equal to result->iname, no matter what.
168 */
169 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500170 if (unlikely(!result)) {
171 __putname(kname);
172 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400173 }
174 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500175 len = strncpy_from_user(kname, filename, PATH_MAX);
176 if (unlikely(len < 0)) {
177 __putname(kname);
178 kfree(result);
179 return ERR_PTR(len);
180 }
181 if (unlikely(len == PATH_MAX)) {
182 __putname(kname);
183 kfree(result);
184 return ERR_PTR(-ENAMETOOLONG);
185 }
Jeff Layton7950e382012-10-10 16:43:13 -0400186 }
187
Al Viro94b5d262015-02-22 19:38:03 -0500188 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700189 /* The empty path is special. */
190 if (unlikely(!len)) {
191 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500192 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500193 if (!(flags & LOOKUP_EMPTY)) {
194 putname(result);
195 return ERR_PTR(-ENOENT);
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700198
Jeff Layton7950e382012-10-10 16:43:13 -0400199 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800200 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400201 audit_getname(result);
202 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Jeff Layton91a27b22012-10-10 15:25:28 -0400205struct filename *
206getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400207{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700208 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400209}
210
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800211struct filename *
212getname_kernel(const char * filename)
213{
214 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500215 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800216
217 result = __getname();
218 if (unlikely(!result))
219 return ERR_PTR(-ENOMEM);
220
Paul Moore08518542015-01-21 23:59:56 -0500221 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500222 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500223 } else if (len <= PATH_MAX) {
Al Viro960534a2018-04-08 11:57:10 -0400224 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500225 struct filename *tmp;
226
Al Viro960534a2018-04-08 11:57:10 -0400227 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500228 if (unlikely(!tmp)) {
229 __putname(result);
230 return ERR_PTR(-ENOMEM);
231 }
232 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500233 result = tmp;
234 } else {
235 __putname(result);
236 return ERR_PTR(-ENAMETOOLONG);
237 }
238 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800239 result->uptr = NULL;
240 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500241 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500242 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800243
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800244 return result;
245}
246
Jeff Layton91a27b22012-10-10 15:25:28 -0400247void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
Paul Moore55422d02015-01-22 00:00:23 -0500249 BUG_ON(name->refcnt <= 0);
250
251 if (--name->refcnt > 0)
252 return;
253
Al Virofd2f7cb2015-02-22 20:07:13 -0500254 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500255 __putname(name->name);
256 kfree(name);
257 } else
258 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Linus Torvaldse77819e2011-07-22 19:30:19 -0700261static int check_acl(struct inode *inode, int mask)
262{
Linus Torvalds84635d62011-07-25 22:47:03 -0700263#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700264 struct posix_acl *acl;
265
Linus Torvaldse77819e2011-07-22 19:30:19 -0700266 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400267 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
268 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700269 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400270 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100271 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400272 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300273 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700274 }
275
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800276 acl = get_acl(inode, ACL_TYPE_ACCESS);
277 if (IS_ERR(acl))
278 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700279 if (acl) {
280 int error = posix_acl_permission(inode, acl, mask);
281 posix_acl_release(acl);
282 return error;
283 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700284#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700285
286 return -EAGAIN;
287}
288
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700289/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530290 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700291 */
Al Viro7e401452011-06-20 19:12:17 -0400292static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700293{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700294 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700295
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800296 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700297 mode >>= 6;
298 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700299 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400300 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100301 if (error != -EAGAIN)
302 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700303 }
304
305 if (in_group_p(inode->i_gid))
306 mode >>= 3;
307 }
308
309 /*
310 * If the DACs are ok we don't need any capability check.
311 */
Al Viro9c2c7032011-06-20 19:06:22 -0400312 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700313 return 0;
314 return -EACCES;
315}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
317/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100318 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530320 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 *
322 * Used to check for read/write/execute permissions on a file.
323 * We use "fsuid" for this, letting us set arbitrary permissions
324 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100325 * are used for other things.
326 *
327 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
328 * request cannot be satisfied (eg. requires blocking or too much complexity).
329 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 */
Al Viro2830ba72011-06-20 19:16:29 -0400331int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700333 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530336 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 */
Al Viro7e401452011-06-20 19:12:17 -0400338 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700339 if (ret != -EACCES)
340 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Al Virod594e7e2011-06-20 19:55:42 -0400342 if (S_ISDIR(inode->i_mode)) {
343 /* DACs are overridable for directories */
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700344 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400345 return 0;
346 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700347 if (capable_wrt_inode_uidgid(inode,
348 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400349 return 0;
350 return -EACCES;
351 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 /*
353 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400354 * Executable DACs are overridable when there is
355 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 */
Al Virod594e7e2011-06-20 19:55:42 -0400357 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700358 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return 0;
360
361 /*
362 * Searching includes executable on directories, else just read.
363 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600364 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400365 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700366 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
368
369 return -EACCES;
370}
Al Viro4d359502014-03-14 12:20:17 -0400371EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700373/*
374 * We _really_ want to just do "generic_permission()" without
375 * even looking at the inode->i_op values. So we keep a cache
376 * flag in inode->i_opflags, that says "this has not special
377 * permission function, use the fast case".
378 */
379static inline int do_inode_permission(struct inode *inode, int mask)
380{
381 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
382 if (likely(inode->i_op->permission))
383 return inode->i_op->permission(inode, mask);
384
385 /* This gets set once for the inode lifetime */
386 spin_lock(&inode->i_lock);
387 inode->i_opflags |= IOP_FASTPERM;
388 spin_unlock(&inode->i_lock);
389 }
390 return generic_permission(inode, mask);
391}
392
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200393/**
David Howells0bdaea92012-06-25 12:55:46 +0100394 * __inode_permission - Check for access rights to a given inode
395 * @inode: Inode to check permission on
396 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200397 *
David Howells0bdaea92012-06-25 12:55:46 +0100398 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530399 *
400 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100401 *
402 * This does not check for a read-only file system. You probably want
403 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200404 */
David Howells0bdaea92012-06-25 12:55:46 +0100405int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
Al Viroe6305c42008-07-15 21:03:57 -0400407 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700409 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 /*
411 * Nobody gets write access to an immutable file.
412 */
413 if (IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +0800414 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -0500415
416 /*
417 * Updating mtime will likely cause i_uid and i_gid to be
418 * written back improperly if their true value is unknown
419 * to the vfs.
420 */
421 if (HAS_UNMAPPED_ID(inode))
422 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
424
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700425 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (retval)
427 return retval;
428
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700429 retval = devcgroup_inode_permission(inode, mask);
430 if (retval)
431 return retval;
432
Eric Parisd09ca732010-07-23 11:43:57 -0400433 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
Miklos Szeredibd5d0852014-10-24 00:14:35 +0200435EXPORT_SYMBOL(__inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Al Virof4d6ff82011-06-19 13:14:21 -0400437/**
David Howells0bdaea92012-06-25 12:55:46 +0100438 * sb_permission - Check superblock-level permissions
439 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700440 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100441 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
442 *
443 * Separate out file-system wide checks from inode-specific permission checks.
444 */
445static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
446{
447 if (unlikely(mask & MAY_WRITE)) {
448 umode_t mode = inode->i_mode;
449
450 /* Nobody gets write access to a read-only fs. */
451 if ((sb->s_flags & MS_RDONLY) &&
452 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
453 return -EROFS;
454 }
455 return 0;
456}
457
458/**
459 * inode_permission - Check for access rights to a given inode
460 * @inode: Inode to check permission on
461 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
462 *
463 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
464 * this, letting us set arbitrary permissions for filesystem access without
465 * changing the "normal" UIDs which are used for other things.
466 *
467 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
468 */
469int inode_permission(struct inode *inode, int mask)
470{
471 int retval;
472
473 retval = sb_permission(inode->i_sb, inode, mask);
474 if (retval)
475 return retval;
476 return __inode_permission(inode, mask);
477}
Al Viro4d359502014-03-14 12:20:17 -0400478EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100479
480/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800481 * path_get - get a reference to a path
482 * @path: path to get the reference to
483 *
484 * Given a path increment the reference count to the dentry and the vfsmount.
485 */
Al Virodcf787f2013-03-01 23:51:07 -0500486void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800487{
488 mntget(path->mnt);
489 dget(path->dentry);
490}
491EXPORT_SYMBOL(path_get);
492
493/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800494 * path_put - put a reference to a path
495 * @path: path to put the reference to
496 *
497 * Given a path decrement the reference count to the dentry and the vfsmount.
498 */
Al Virodcf787f2013-03-01 23:51:07 -0500499void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Jan Blunck1d957f92008-02-14 19:34:35 -0800501 dput(path->dentry);
502 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
Jan Blunck1d957f92008-02-14 19:34:35 -0800504EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Al Viro894bc8c2015-05-02 07:16:16 -0400506#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400507struct nameidata {
508 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400509 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400510 struct path root;
511 struct inode *inode; /* path.dentry.d_inode */
512 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400513 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400514 int last_type;
515 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100516 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400517 struct saved {
518 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500519 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400520 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400521 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400522 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400523 struct filename *name;
524 struct nameidata *saved;
Al Virofceef392015-12-29 15:58:39 -0500525 struct inode *link_inode;
Al Viro9883d182015-05-13 07:28:08 -0400526 unsigned root_seq;
527 int dfd;
Al Viro1f55a6e2014-11-01 19:30:41 -0400528};
529
Al Viro9883d182015-05-13 07:28:08 -0400530static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400531{
NeilBrown756daf22015-03-23 13:37:38 +1100532 struct nameidata *old = current->nameidata;
533 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400534 p->dfd = dfd;
535 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100536 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400537 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100538 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400539}
540
Al Viro9883d182015-05-13 07:28:08 -0400541static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400542{
Al Viro9883d182015-05-13 07:28:08 -0400543 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100544
545 current->nameidata = old;
546 if (old)
547 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500548 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100549 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400550}
551
552static int __nd_alloc_stack(struct nameidata *nd)
553{
Al Virobc40aee2015-05-09 13:04:24 -0400554 struct saved *p;
555
556 if (nd->flags & LOOKUP_RCU) {
557 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
558 GFP_ATOMIC);
559 if (unlikely(!p))
560 return -ECHILD;
561 } else {
562 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400563 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400564 if (unlikely(!p))
565 return -ENOMEM;
566 }
Al Viro894bc8c2015-05-02 07:16:16 -0400567 memcpy(p, nd->internal, sizeof(nd->internal));
568 nd->stack = p;
569 return 0;
570}
571
Eric W. Biederman397d4252015-08-15 20:27:13 -0500572/**
573 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
574 * @path: nameidate to verify
575 *
576 * Rename can sometimes move a file or directory outside of a bind
577 * mount, path_connected allows those cases to be detected.
578 */
579static bool path_connected(const struct path *path)
580{
581 struct vfsmount *mnt = path->mnt;
Eric W. Biedermaneaa95922018-03-14 18:20:29 -0500582 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500583
Eric W. Biedermaneaa95922018-03-14 18:20:29 -0500584 /* Bind mounts and multi-root filesystems can have disconnected paths */
585 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
Eric W. Biederman397d4252015-08-15 20:27:13 -0500586 return true;
587
588 return is_subdir(path->dentry, mnt->mnt_root);
589}
590
Al Viro894bc8c2015-05-02 07:16:16 -0400591static inline int nd_alloc_stack(struct nameidata *nd)
592{
Al Viroda4e0be2015-05-03 20:52:15 -0400593 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400594 return 0;
595 if (likely(nd->stack != nd->internal))
596 return 0;
597 return __nd_alloc_stack(nd);
598}
599
Al Viro79733872015-05-09 12:55:43 -0400600static void drop_links(struct nameidata *nd)
601{
602 int i = nd->depth;
603 while (i--) {
604 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500605 do_delayed_call(&last->done);
606 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400607 }
608}
609
610static void terminate_walk(struct nameidata *nd)
611{
612 drop_links(nd);
613 if (!(nd->flags & LOOKUP_RCU)) {
614 int i;
615 path_put(&nd->path);
616 for (i = 0; i < nd->depth; i++)
617 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400618 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
619 path_put(&nd->root);
620 nd->root.mnt = NULL;
621 }
Al Viro79733872015-05-09 12:55:43 -0400622 } else {
623 nd->flags &= ~LOOKUP_RCU;
624 if (!(nd->flags & LOOKUP_ROOT))
625 nd->root.mnt = NULL;
626 rcu_read_unlock();
627 }
628 nd->depth = 0;
629}
630
631/* path_put is needed afterwards regardless of success or failure */
632static bool legitimize_path(struct nameidata *nd,
633 struct path *path, unsigned seq)
634{
635 int res = __legitimize_mnt(path->mnt, nd->m_seq);
636 if (unlikely(res)) {
637 if (res > 0)
638 path->mnt = NULL;
639 path->dentry = NULL;
640 return false;
641 }
642 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
643 path->dentry = NULL;
644 return false;
645 }
646 return !read_seqcount_retry(&path->dentry->d_seq, seq);
647}
648
649static bool legitimize_links(struct nameidata *nd)
650{
651 int i;
652 for (i = 0; i < nd->depth; i++) {
653 struct saved *last = nd->stack + i;
654 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
655 drop_links(nd);
656 nd->depth = i + 1;
657 return false;
658 }
659 }
660 return true;
661}
662
Al Viro19660af2011-03-25 10:32:48 -0400663/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100664 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400665 * Documentation/filesystems/path-lookup.txt). In situations when we can't
666 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500667 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400668 * mode. Refcounts are grabbed at the last known good point before rcu-walk
669 * got stuck, so ref-walk may continue from there. If this is not successful
670 * (eg. a seqcount has changed), then failure is returned and it's up to caller
671 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100672 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100673
674/**
Al Viro19660af2011-03-25 10:32:48 -0400675 * unlazy_walk - try to switch to ref-walk mode.
676 * @nd: nameidata pathwalk data
677 * @dentry: child of nd->path.dentry or NULL
Al Viro6e9918b2015-05-05 09:26:05 -0400678 * @seq: seq number to check dentry against
Randy Dunlap39191622011-01-08 19:36:21 -0800679 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100680 *
Al Viro19660af2011-03-25 10:32:48 -0400681 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
682 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
683 * @nd or NULL. Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400684 * Nothing should touch nameidata between unlazy_walk() failure and
685 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100686 */
Al Viro6e9918b2015-05-05 09:26:05 -0400687static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Nick Piggin31e6b012011-01-07 17:49:52 +1100688{
Nick Piggin31e6b012011-01-07 17:49:52 +1100689 struct dentry *parent = nd->path.dentry;
690
691 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700692
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700693 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400694 if (unlikely(!legitimize_links(nd)))
695 goto out2;
696 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
697 goto out2;
698 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
699 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400700
Linus Torvalds15570082013-09-02 11:38:06 -0700701 /*
702 * For a negative lookup, the lookup sequence point is the parents
703 * sequence point, and it only needs to revalidate the parent dentry.
704 *
705 * For a positive lookup, we need to move both the parent and the
706 * dentry from the RCU domain to be properly refcounted. And the
707 * sequence number in the dentry validates *both* dentry counters,
708 * since we checked the sequence number of the parent after we got
709 * the child sequence number. So we know the parent must still
710 * be valid if the child sequence number is still valid.
711 */
Al Viro19660af2011-03-25 10:32:48 -0400712 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700713 if (read_seqcount_retry(&parent->d_seq, nd->seq))
714 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400715 BUG_ON(nd->inode != parent->d_inode);
716 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700717 if (!lockref_get_not_dead(&dentry->d_lockref))
718 goto out;
Al Viro6e9918b2015-05-05 09:26:05 -0400719 if (read_seqcount_retry(&dentry->d_seq, seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700720 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400721 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700722
723 /*
724 * Sequence counts matched. Now make sure that the root is
725 * still valid and get it if required.
726 */
727 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400728 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
729 rcu_read_unlock();
730 dput(dentry);
731 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400732 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100733 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100734
Al Viro8b61e742013-11-08 12:45:01 -0500735 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100736 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400737
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700738drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500739 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700740 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700741 goto drop_root_mnt;
Al Viro79733872015-05-09 12:55:43 -0400742out2:
743 nd->path.mnt = NULL;
744out1:
745 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700746out:
Al Viro8b61e742013-11-08 12:45:01 -0500747 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700748drop_root_mnt:
749 if (!(nd->flags & LOOKUP_ROOT))
750 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100751 return -ECHILD;
752}
753
Al Viro79733872015-05-09 12:55:43 -0400754static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
755{
756 if (unlikely(!legitimize_path(nd, link, seq))) {
757 drop_links(nd);
758 nd->depth = 0;
759 nd->flags &= ~LOOKUP_RCU;
760 nd->path.mnt = NULL;
761 nd->path.dentry = NULL;
762 if (!(nd->flags & LOOKUP_ROOT))
763 nd->root.mnt = NULL;
764 rcu_read_unlock();
765 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
766 return 0;
767 }
768 path_put(link);
769 return -ECHILD;
770}
771
Al Viro4ce16ef32012-06-10 16:10:59 -0400772static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100773{
Al Viro4ce16ef32012-06-10 16:10:59 -0400774 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100775}
776
Al Viro9f1fafe2011-03-25 11:00:12 -0400777/**
778 * complete_walk - successful completion of path walk
779 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500780 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400781 * If we had been in RCU mode, drop out of it and legitimize nd->path.
782 * Revalidate the final result, unless we'd already done that during
783 * the path walk or the filesystem doesn't ask for it. Return 0 on
784 * success, -error on failure. In case of failure caller does not
785 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500786 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400787static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500788{
Al Viro16c2cd72011-02-22 15:50:10 -0500789 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500790 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500791
Al Viro9f1fafe2011-03-25 11:00:12 -0400792 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400793 if (!(nd->flags & LOOKUP_ROOT))
794 nd->root.mnt = NULL;
Al Viro6e9918b2015-05-05 09:26:05 -0400795 if (unlikely(unlazy_walk(nd, NULL, 0)))
Al Viro48a066e2013-09-29 22:06:07 -0400796 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400797 }
798
Al Viro16c2cd72011-02-22 15:50:10 -0500799 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500800 return 0;
801
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500802 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500803 return 0;
804
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500805 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500806 if (status > 0)
807 return 0;
808
Al Viro16c2cd72011-02-22 15:50:10 -0500809 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500810 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500811
Jeff Layton39159de2009-12-07 12:01:50 -0500812 return status;
813}
814
Al Viro18d8c862015-05-12 16:32:34 -0400815static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400816{
Al Viro7bd88372014-09-13 21:55:46 -0400817 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100818
Al Viro9e6697e2015-12-05 20:07:21 -0500819 if (nd->flags & LOOKUP_RCU) {
820 unsigned seq;
821
822 do {
823 seq = read_seqcount_begin(&fs->seq);
824 nd->root = fs->root;
825 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
826 } while (read_seqcount_retry(&fs->seq, seq));
827 } else {
828 get_fs_root(fs, &nd->root);
829 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100830}
831
Jan Blunck1d957f92008-02-14 19:34:35 -0800832static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700833{
834 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800835 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700836 mntput(path->mnt);
837}
838
Nick Piggin7b9337a2011-01-14 08:42:43 +0000839static inline void path_to_nameidata(const struct path *path,
840 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700841{
Nick Piggin31e6b012011-01-07 17:49:52 +1100842 if (!(nd->flags & LOOKUP_RCU)) {
843 dput(nd->path.dentry);
844 if (nd->path.mnt != path->mnt)
845 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800846 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100847 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800848 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700849}
850
Al Viro248fb5b2015-12-05 20:51:58 -0500851static int nd_jump_root(struct nameidata *nd)
852{
853 if (nd->flags & LOOKUP_RCU) {
854 struct dentry *d;
855 nd->path = nd->root;
856 d = nd->path.dentry;
857 nd->inode = d->d_inode;
858 nd->seq = nd->root_seq;
859 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
860 return -ECHILD;
861 } else {
862 path_put(&nd->path);
863 nd->path = nd->root;
864 path_get(&nd->path);
865 nd->inode = nd->path.dentry->d_inode;
866 }
867 nd->flags |= LOOKUP_JUMPED;
868 return 0;
869}
870
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400871/*
Al Viro6b255392015-11-17 10:20:54 -0500872 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400873 * caller must have taken a reference to path beforehand.
874 */
Al Viro6e771372015-05-02 13:37:52 -0400875void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400876{
Al Viro6e771372015-05-02 13:37:52 -0400877 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400878 path_put(&nd->path);
879
880 nd->path = *path;
881 nd->inode = nd->path.dentry->d_inode;
882 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400883}
884
Al Virob9ff4422015-05-02 20:19:23 -0400885static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400886{
Al Viro21c30032015-05-03 21:06:24 -0400887 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -0500888 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -0400889 if (!(nd->flags & LOOKUP_RCU))
890 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400891}
892
Linus Torvalds561ec642012-10-26 10:05:07 -0700893int sysctl_protected_symlinks __read_mostly = 0;
894int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca0c41bee2018-08-23 17:00:35 -0700895int sysctl_protected_fifos __read_mostly;
896int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -0700897
898/**
899 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700900 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700901 *
902 * In the case of the sysctl_protected_symlinks sysctl being enabled,
903 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
904 * in a sticky world-writable directory. This is to protect privileged
905 * processes from failing races against path names that may change out
906 * from under them by way of other users creating malicious symlinks.
907 * It will permit symlinks to be followed only when outside a sticky
908 * world-writable directory, or when the uid of the symlink and follower
909 * match, or when the directory owner matches the symlink's owner.
910 *
911 * Returns 0 if following the symlink is allowed, -ve on error.
912 */
Al Virofec2fa22015-05-06 15:58:18 -0400913static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700914{
915 const struct inode *inode;
916 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500917 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700918
919 if (!sysctl_protected_symlinks)
920 return 0;
921
922 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500923 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700924 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700925 return 0;
926
927 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400928 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700929 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
930 return 0;
931
932 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500933 puid = parent->i_uid;
934 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700935 return 0;
936
Al Viro319565022015-05-07 20:37:40 -0400937 if (nd->flags & LOOKUP_RCU)
938 return -ECHILD;
939
Al Viro1cf26652015-05-06 16:01:56 -0400940 audit_log_link_denied("follow_link", &nd->stack[0].link);
Kees Cook800179c2012-07-25 17:29:07 -0700941 return -EACCES;
942}
943
944/**
945 * safe_hardlink_source - Check for safe hardlink conditions
946 * @inode: the source inode to hardlink from
947 *
948 * Return false if at least one of the following conditions:
949 * - inode is not a regular file
950 * - inode is setuid
951 * - inode is setgid and group-exec
952 * - access failure for read and write
953 *
954 * Otherwise returns true.
955 */
956static bool safe_hardlink_source(struct inode *inode)
957{
958 umode_t mode = inode->i_mode;
959
960 /* Special files should not get pinned to the filesystem. */
961 if (!S_ISREG(mode))
962 return false;
963
964 /* Setuid files should not get pinned to the filesystem. */
965 if (mode & S_ISUID)
966 return false;
967
968 /* Executable setgid files should not get pinned to the filesystem. */
969 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
970 return false;
971
972 /* Hardlinking to unreadable or unwritable sources is dangerous. */
973 if (inode_permission(inode, MAY_READ | MAY_WRITE))
974 return false;
975
976 return true;
977}
978
979/**
980 * may_linkat - Check permissions for creating a hardlink
981 * @link: the source to hardlink from
982 *
983 * Block hardlink when all of:
984 * - sysctl_protected_hardlinks enabled
985 * - fsuid does not match inode
986 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200987 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700988 *
989 * Returns 0 if successful, -ve on error.
990 */
991static int may_linkat(struct path *link)
992{
Kees Cook800179c2012-07-25 17:29:07 -0700993 struct inode *inode;
994
995 if (!sysctl_protected_hardlinks)
996 return 0;
997
Kees Cook800179c2012-07-25 17:29:07 -0700998 inode = link->dentry->d_inode;
999
1000 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1001 * otherwise, it must be a safe source.
1002 */
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001003 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
Kees Cook800179c2012-07-25 17:29:07 -07001004 return 0;
1005
Kees Cooka51d9ea2012-07-25 17:29:08 -07001006 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -07001007 return -EPERM;
1008}
1009
Salvatore Mesoraca0c41bee2018-08-23 17:00:35 -07001010/**
1011 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1012 * should be allowed, or not, on files that already
1013 * exist.
1014 * @dir: the sticky parent directory
1015 * @inode: the inode of the file to open
1016 *
1017 * Block an O_CREAT open of a FIFO (or a regular file) when:
1018 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1019 * - the file already exists
1020 * - we are in a sticky directory
1021 * - we don't own the file
1022 * - the owner of the directory doesn't own the file
1023 * - the directory is world writable
1024 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1025 * the directory doesn't have to be world writable: being group writable will
1026 * be enough.
1027 *
1028 * Returns 0 if the open is allowed, -ve on error.
1029 */
1030static int may_create_in_sticky(struct dentry * const dir,
1031 struct inode * const inode)
1032{
1033 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1034 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1035 likely(!(dir->d_inode->i_mode & S_ISVTX)) ||
1036 uid_eq(inode->i_uid, dir->d_inode->i_uid) ||
1037 uid_eq(current_fsuid(), inode->i_uid))
1038 return 0;
1039
1040 if (likely(dir->d_inode->i_mode & 0002) ||
1041 (dir->d_inode->i_mode & 0020 &&
1042 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1043 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1044 return -EACCES;
1045 }
1046 return 0;
1047}
1048
Al Viro3b2e7f72015-04-19 00:53:50 -04001049static __always_inline
1050const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001051{
Al Viroab104922015-05-10 11:50:01 -04001052 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001053 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -05001054 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001055 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001056 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001057
NeilBrown8fa9dd22015-03-23 13:37:40 +11001058 if (!(nd->flags & LOOKUP_RCU)) {
1059 touch_atime(&last->link);
1060 cond_resched();
Miklos Szeredi598e3c82016-09-16 12:44:20 +02001061 } else if (atime_needs_update_rcu(&last->link, inode)) {
NeilBrown8fa9dd22015-03-23 13:37:40 +11001062 if (unlikely(unlazy_walk(nd, NULL, 0)))
1063 return ERR_PTR(-ECHILD);
1064 touch_atime(&last->link);
1065 }
1066
NeilBrownbda0be72015-03-23 13:37:39 +11001067 error = security_inode_follow_link(dentry, inode,
1068 nd->flags & LOOKUP_RCU);
1069 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001070 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001071
Al Viro86acdca12009-12-22 23:45:11 -05001072 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001073 res = inode->i_link;
1074 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001075 const char * (*get)(struct dentry *, struct inode *,
1076 struct delayed_call *);
1077 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001078 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001079 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001080 if (res == ERR_PTR(-ECHILD)) {
1081 if (unlikely(unlazy_walk(nd, NULL, 0)))
1082 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001083 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001084 }
1085 } else {
Al Virofceef392015-12-29 15:58:39 -05001086 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001087 }
Al Virofceef392015-12-29 15:58:39 -05001088 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001089 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001090 }
Al Virofab51e82015-05-10 11:01:00 -04001091 if (*res == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05001092 if (!nd->root.mnt)
1093 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05001094 if (unlikely(nd_jump_root(nd)))
1095 return ERR_PTR(-ECHILD);
Al Virofab51e82015-05-10 11:01:00 -04001096 while (unlikely(*++res == '/'))
1097 ;
1098 }
1099 if (!*res)
1100 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001101 return res;
1102}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001103
David Howellsf015f1262012-06-25 12:55:28 +01001104/*
1105 * follow_up - Find the mountpoint of path's vfsmount
1106 *
1107 * Given a path, find the mountpoint of its source file system.
1108 * Replace @path with the path of the mountpoint in the parent mount.
1109 * Up is towards /.
1110 *
1111 * Return 1 if we went up a level and 0 if we were already at the
1112 * root.
1113 */
Al Virobab77eb2009-04-18 03:26:48 -04001114int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Al Viro0714a532011-11-24 22:19:58 -05001116 struct mount *mnt = real_mount(path->mnt);
1117 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001119
Al Viro48a066e2013-09-29 22:06:07 -04001120 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001121 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001122 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001123 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 return 0;
1125 }
Al Viro0714a532011-11-24 22:19:58 -05001126 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001127 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001128 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001129 dput(path->dentry);
1130 path->dentry = mountpoint;
1131 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001132 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return 1;
1134}
Al Viro4d359502014-03-14 12:20:17 -04001135EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001137/*
David Howells9875cf82011-01-14 18:45:21 +00001138 * Perform an automount
1139 * - return -EISDIR to tell follow_managed() to stop and return the path we
1140 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 */
NeilBrown756daf22015-03-23 13:37:38 +11001142static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001143 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001144{
David Howells9875cf82011-01-14 18:45:21 +00001145 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001146 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001147
David Howells9875cf82011-01-14 18:45:21 +00001148 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1149 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001150
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001151 /* We don't want to mount if someone's just doing a stat -
1152 * unless they're stat'ing a directory and appended a '/' to
1153 * the name.
1154 *
1155 * We do, however, want to mount if someone wants to open or
1156 * create a file of any type under the mountpoint, wants to
1157 * traverse through the mountpoint or wants to open the
1158 * mounted directory. Also, autofs may mark negative dentries
1159 * as being automount points. These will need the attentions
1160 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001161 */
NeilBrown756daf22015-03-23 13:37:38 +11001162 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1163 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001164 path->dentry->d_inode)
1165 return -EISDIR;
1166
NeilBrown756daf22015-03-23 13:37:38 +11001167 nd->total_link_count++;
1168 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001169 return -ELOOP;
1170
1171 mnt = path->dentry->d_op->d_automount(path);
1172 if (IS_ERR(mnt)) {
1173 /*
1174 * The filesystem is allowed to return -EISDIR here to indicate
1175 * it doesn't want to automount. For instance, autofs would do
1176 * this so that its userspace daemon can mount on this dentry.
1177 *
1178 * However, we can only permit this if it's a terminal point in
1179 * the path being looked up; if it wasn't then the remainder of
1180 * the path is inaccessible and we should say so.
1181 */
NeilBrown756daf22015-03-23 13:37:38 +11001182 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001183 return -EREMOTE;
1184 return PTR_ERR(mnt);
1185 }
David Howellsea5b7782011-01-14 19:10:03 +00001186
David Howells9875cf82011-01-14 18:45:21 +00001187 if (!mnt) /* mount collision */
1188 return 0;
1189
Al Viro8aef1882011-06-16 15:10:06 +01001190 if (!*need_mntput) {
1191 /* lock_mount() may release path->mnt on error */
1192 mntget(path->mnt);
1193 *need_mntput = true;
1194 }
Al Viro19a167a2011-01-17 01:35:23 -05001195 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001196
David Howellsea5b7782011-01-14 19:10:03 +00001197 switch (err) {
1198 case -EBUSY:
1199 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001200 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001201 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001202 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001203 path->mnt = mnt;
1204 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001205 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001206 default:
1207 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001208 }
Al Viro19a167a2011-01-17 01:35:23 -05001209
David Howells9875cf82011-01-14 18:45:21 +00001210}
1211
1212/*
1213 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001214 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001215 * - Flagged as mountpoint
1216 * - Flagged as automount point
1217 *
1218 * This may only be called in refwalk mode.
1219 *
1220 * Serialization is taken care of in namespace.c
1221 */
NeilBrown756daf22015-03-23 13:37:38 +11001222static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001223{
Al Viro8aef1882011-06-16 15:10:06 +01001224 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001225 unsigned managed;
1226 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001227 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001228
1229 /* Given that we're not holding a lock here, we retain the value in a
1230 * local variable for each dentry as we look at it so that we don't see
1231 * the components of that value change under us */
1232 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1233 managed &= DCACHE_MANAGED_DENTRY,
1234 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001235 /* Allow the filesystem to manage the transit without i_mutex
1236 * being held. */
1237 if (managed & DCACHE_MANAGE_TRANSIT) {
1238 BUG_ON(!path->dentry->d_op);
1239 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001240 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001241 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001242 break;
David Howellscc53ce52011-01-14 18:45:26 +00001243 }
1244
David Howells9875cf82011-01-14 18:45:21 +00001245 /* Transit to a mounted filesystem. */
1246 if (managed & DCACHE_MOUNTED) {
1247 struct vfsmount *mounted = lookup_mnt(path);
1248 if (mounted) {
1249 dput(path->dentry);
1250 if (need_mntput)
1251 mntput(path->mnt);
1252 path->mnt = mounted;
1253 path->dentry = dget(mounted->mnt_root);
1254 need_mntput = true;
1255 continue;
1256 }
1257
1258 /* Something is mounted on this dentry in another
1259 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001260 * namespace got unmounted before lookup_mnt() could
1261 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001262 }
1263
1264 /* Handle an automount point */
1265 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001266 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001267 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001268 break;
David Howells9875cf82011-01-14 18:45:21 +00001269 continue;
1270 }
1271
1272 /* We didn't change the current path point */
1273 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
Al Viro8aef1882011-06-16 15:10:06 +01001275
1276 if (need_mntput && path->mnt == mnt)
1277 mntput(path->mnt);
Al Viroe9742b52016-03-05 22:04:59 -05001278 if (ret == -EISDIR || !ret)
1279 ret = 1;
Al Viro84027522015-04-22 10:30:08 -04001280 if (need_mntput)
1281 nd->flags |= LOOKUP_JUMPED;
1282 if (unlikely(ret < 0))
1283 path_put_conditional(path, nd);
1284 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285}
1286
David Howellscc53ce52011-01-14 18:45:26 +00001287int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288{
1289 struct vfsmount *mounted;
1290
Al Viro1c755af2009-04-18 14:06:57 -04001291 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001293 dput(path->dentry);
1294 mntput(path->mnt);
1295 path->mnt = mounted;
1296 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return 1;
1298 }
1299 return 0;
1300}
Al Viro4d359502014-03-14 12:20:17 -04001301EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
NeilBrownb8faf032014-08-04 17:06:29 +10001303static inline int managed_dentry_rcu(struct dentry *dentry)
Ian Kent62a73752011-03-25 01:51:02 +08001304{
NeilBrownb8faf032014-08-04 17:06:29 +10001305 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1306 dentry->d_op->d_manage(dentry, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001307}
1308
David Howells9875cf82011-01-14 18:45:21 +00001309/*
Al Viro287548e2011-05-27 06:50:06 -04001310 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1311 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001312 */
1313static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001314 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001315{
Ian Kent62a73752011-03-25 01:51:02 +08001316 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001317 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001318 /*
1319 * Don't forget we might have a non-mountpoint managed dentry
1320 * that wants to block transit.
1321 */
NeilBrownb8faf032014-08-04 17:06:29 +10001322 switch (managed_dentry_rcu(path->dentry)) {
1323 case -ECHILD:
1324 default:
David Howellsab909112011-01-14 18:46:51 +00001325 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001326 case -EISDIR:
1327 return true;
1328 case 0:
1329 break;
1330 }
Ian Kent62a73752011-03-25 01:51:02 +08001331
1332 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001333 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001334
Al Viro474279d2013-10-01 16:11:26 -04001335 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001336 if (!mounted)
1337 break;
Al Viroc7105362011-11-24 18:22:03 -05001338 path->mnt = &mounted->mnt;
1339 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001340 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001341 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001342 /*
1343 * Update the inode too. We don't need to re-check the
1344 * dentry sequence number here after this d_inode read,
1345 * because a mount-point is always pinned.
1346 */
1347 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001348 }
Al Virof5be3e29122014-09-13 21:50:45 -04001349 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001350 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001351}
1352
Nick Piggin31e6b012011-01-07 17:49:52 +11001353static int follow_dotdot_rcu(struct nameidata *nd)
1354{
Al Viro4023bfc2014-09-13 21:59:43 -04001355 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001356
David Howells9875cf82011-01-14 18:45:21 +00001357 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001358 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001359 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001360 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1361 struct dentry *old = nd->path.dentry;
1362 struct dentry *parent = old->d_parent;
1363 unsigned seq;
1364
Al Viro4023bfc2014-09-13 21:59:43 -04001365 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001366 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001367 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1368 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001369 nd->path.dentry = parent;
1370 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001371 if (unlikely(!path_connected(&nd->path)))
1372 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001373 break;
Al Viroaed434a2015-05-12 12:22:47 -04001374 } else {
1375 struct mount *mnt = real_mount(nd->path.mnt);
1376 struct mount *mparent = mnt->mnt_parent;
1377 struct dentry *mountpoint = mnt->mnt_mountpoint;
1378 struct inode *inode2 = mountpoint->d_inode;
1379 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1380 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1381 return -ECHILD;
1382 if (&mparent->mnt == nd->path.mnt)
1383 break;
1384 /* we know that mountpoint was pinned */
1385 nd->path.dentry = mountpoint;
1386 nd->path.mnt = &mparent->mnt;
1387 inode = inode2;
1388 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001389 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001390 }
Al Viroaed434a2015-05-12 12:22:47 -04001391 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001392 struct mount *mounted;
1393 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001394 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1395 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001396 if (!mounted)
1397 break;
1398 nd->path.mnt = &mounted->mnt;
1399 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001400 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001401 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001402 }
Al Viro4023bfc2014-09-13 21:59:43 -04001403 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001404 return 0;
1405}
1406
David Howells9875cf82011-01-14 18:45:21 +00001407/*
David Howellscc53ce52011-01-14 18:45:26 +00001408 * Follow down to the covering mount currently visible to userspace. At each
1409 * point, the filesystem owning that dentry may be queried as to whether the
1410 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001411 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001412int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001413{
1414 unsigned managed;
1415 int ret;
1416
1417 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1418 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1419 /* Allow the filesystem to manage the transit without i_mutex
1420 * being held.
1421 *
1422 * We indicate to the filesystem if someone is trying to mount
1423 * something here. This gives autofs the chance to deny anyone
1424 * other than its daemon the right to mount on its
1425 * superstructure.
1426 *
1427 * The filesystem may sleep at this point.
1428 */
1429 if (managed & DCACHE_MANAGE_TRANSIT) {
1430 BUG_ON(!path->dentry->d_op);
1431 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001432 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001433 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001434 if (ret < 0)
1435 return ret == -EISDIR ? 0 : ret;
1436 }
1437
1438 /* Transit to a mounted filesystem. */
1439 if (managed & DCACHE_MOUNTED) {
1440 struct vfsmount *mounted = lookup_mnt(path);
1441 if (!mounted)
1442 break;
1443 dput(path->dentry);
1444 mntput(path->mnt);
1445 path->mnt = mounted;
1446 path->dentry = dget(mounted->mnt_root);
1447 continue;
1448 }
1449
1450 /* Don't handle automount points here */
1451 break;
1452 }
1453 return 0;
1454}
Al Viro4d359502014-03-14 12:20:17 -04001455EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001456
1457/*
David Howells9875cf82011-01-14 18:45:21 +00001458 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1459 */
1460static void follow_mount(struct path *path)
1461{
1462 while (d_mountpoint(path->dentry)) {
1463 struct vfsmount *mounted = lookup_mnt(path);
1464 if (!mounted)
1465 break;
1466 dput(path->dentry);
1467 mntput(path->mnt);
1468 path->mnt = mounted;
1469 path->dentry = dget(mounted->mnt_root);
1470 }
1471}
1472
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001473static int path_parent_directory(struct path *path)
1474{
1475 struct dentry *old = path->dentry;
1476 /* rare case of legitimate dget_parent()... */
1477 path->dentry = dget_parent(path->dentry);
1478 dput(old);
1479 if (unlikely(!path_connected(path)))
1480 return -ENOENT;
1481 return 0;
1482}
1483
Eric W. Biederman397d4252015-08-15 20:27:13 -05001484static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
1486 while(1) {
Al Viro2a737872009-04-07 11:49:53 -04001487 if (nd->path.dentry == nd->root.dentry &&
1488 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001491 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001492 int ret = path_parent_directory(&nd->path);
1493 if (ret)
1494 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 break;
1496 }
Al Viro3088dd72010-01-30 15:47:29 -05001497 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 }
Al Viro79ed0222009-04-18 13:59:41 -04001500 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001501 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001502 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503}
1504
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001506 * This looks up the name in dcache and possibly revalidates the found dentry.
1507 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001508 */
Al Viroe3c13922016-03-06 14:03:27 -05001509static struct dentry *lookup_dcache(const struct qstr *name,
1510 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001511 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001512{
Nick Pigginbaa03892010-08-18 04:37:31 +10001513 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001514 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001515
Miklos Szeredibad61182012-03-26 12:54:24 +02001516 dentry = d_lookup(dir, name);
1517 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001518 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001519 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001520 if (unlikely(error <= 0)) {
Al Viro74ff0ff2016-03-05 22:37:46 -05001521 if (!error)
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001522 d_invalidate(dentry);
Al Viro74ff0ff2016-03-05 22:37:46 -05001523 dput(dentry);
1524 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001525 }
1526 }
1527 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001528 return dentry;
1529}
1530
1531/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001532 * Call i_op->lookup on the dentry. The dentry must be negative and
1533 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001534 *
1535 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001536 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001537static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001538 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001539{
Josef Bacik44396f42011-05-31 11:58:49 -04001540 struct dentry *old;
1541
1542 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001543 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001544 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001545 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001546 }
Josef Bacik44396f42011-05-31 11:58:49 -04001547
Al Viro72bd8662012-06-10 17:17:17 -04001548 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001549 if (unlikely(old)) {
1550 dput(dentry);
1551 dentry = old;
1552 }
1553 return dentry;
1554}
1555
Al Viroe3c13922016-03-06 14:03:27 -05001556static struct dentry *__lookup_hash(const struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001557 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001558{
Al Viro6c51e512016-03-05 20:09:32 -05001559 struct dentry *dentry = lookup_dcache(name, base, flags);
Al Viroa3255542012-03-30 14:41:51 -04001560
Al Viro6c51e512016-03-05 20:09:32 -05001561 if (dentry)
Miklos Szeredibad61182012-03-26 12:54:24 +02001562 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001563
Al Viro6c51e512016-03-05 20:09:32 -05001564 dentry = d_alloc(base, name);
1565 if (unlikely(!dentry))
1566 return ERR_PTR(-ENOMEM);
1567
Al Viro72bd8662012-06-10 17:17:17 -04001568 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001569}
1570
Al Viroe97cdc82013-01-24 18:16:00 -05001571static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001572 struct path *path, struct inode **inode,
1573 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Jan Blunck4ac91372008-02-14 19:34:32 -08001575 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001576 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001577 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001578 int err;
1579
Al Viro3cac2602009-08-13 18:27:43 +04001580 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001581 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001582 * of a false negative due to a concurrent rename, the caller is
1583 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001584 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001585 if (nd->flags & LOOKUP_RCU) {
1586 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001587 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001588 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001589 if (unlikely(!dentry)) {
1590 if (unlazy_walk(nd, NULL, 0))
1591 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001592 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001593 }
Al Viro5a18fff2011-03-11 04:44:53 -05001594
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001595 /*
1596 * This sequence count validates that the inode matches
1597 * the dentry name information from lookup.
1598 */
David Howells63afdfc2015-05-06 15:59:00 +01001599 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001600 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001601 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001602 return -ECHILD;
1603
1604 /*
1605 * This sequence count validates that the parent had no
1606 * changes while we did the lookup of the dentry above.
1607 *
1608 * The memory barrier in read_seqcount_begin of child is
1609 * enough, we can use __read_seqcount_retry here.
1610 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001611 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001612 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001613
Al Viro254cf582015-05-05 09:40:46 -04001614 *seqp = seq;
Al Viro5d0f49c2016-03-05 21:32:53 -05001615 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro4ce16ef32012-06-10 16:10:59 -04001616 status = d_revalidate(dentry, nd->flags);
Al Viro5d0f49c2016-03-05 21:32:53 -05001617 if (unlikely(status <= 0)) {
1618 if (unlazy_walk(nd, dentry, seq))
1619 return -ECHILD;
1620 if (status == -ECHILD)
1621 status = d_revalidate(dentry, nd->flags);
1622 } else {
1623 /*
1624 * Note: do negative dentry check after revalidation in
1625 * case that drops it.
1626 */
1627 if (unlikely(negative))
1628 return -ENOENT;
1629 path->mnt = mnt;
1630 path->dentry = dentry;
1631 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001632 return 1;
Al Viro5d0f49c2016-03-05 21:32:53 -05001633 if (unlazy_walk(nd, dentry, seq))
1634 return -ECHILD;
Al Viro24643082011-02-15 01:26:22 -05001635 }
Al Viro5a18fff2011-03-11 04:44:53 -05001636 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001637 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001638 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001639 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001640 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1641 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001642 }
Al Viro5a18fff2011-03-11 04:44:53 -05001643 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001644 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001645 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001646 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001647 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001648 }
Al Viro766c4cb2015-05-07 19:24:57 -04001649 if (unlikely(d_is_negative(dentry))) {
1650 dput(dentry);
1651 return -ENOENT;
1652 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001653
David Howells9875cf82011-01-14 18:45:21 +00001654 path->mnt = mnt;
1655 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001656 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001657 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001658 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001659 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001660}
1661
1662/* Fast lookup failed, do it the slow way */
Al Viroe3c13922016-03-06 14:03:27 -05001663static struct dentry *lookup_slow(const struct qstr *name,
1664 struct dentry *dir,
1665 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001666{
Al Viro94bdd652016-04-15 02:42:04 -04001667 struct dentry *dentry = ERR_PTR(-ENOENT), *old;
Al Viro19363862016-04-14 19:33:34 -04001668 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001669 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001670
Al Viro9902af72016-04-15 15:08:36 -04001671 inode_lock_shared(inode);
Al Viro19363862016-04-14 19:33:34 -04001672 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001673 if (unlikely(IS_DEADDIR(inode)))
1674 goto out;
1675again:
Al Virod9171b92016-04-15 03:33:13 -04001676 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001677 if (IS_ERR(dentry))
1678 goto out;
1679 if (unlikely(!d_in_lookup(dentry))) {
Al Viro949a8522016-03-06 14:20:52 -05001680 if ((dentry->d_flags & DCACHE_OP_REVALIDATE) &&
1681 !(flags & LOOKUP_NO_REVAL)) {
1682 int error = d_revalidate(dentry, flags);
1683 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001684 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001685 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001686 dput(dentry);
1687 goto again;
1688 }
Al Viro949a8522016-03-06 14:20:52 -05001689 dput(dentry);
1690 dentry = ERR_PTR(error);
1691 }
1692 }
Al Viro94bdd652016-04-15 02:42:04 -04001693 } else {
1694 old = inode->i_op->lookup(inode, dentry, flags);
1695 d_lookup_done(dentry);
1696 if (unlikely(old)) {
1697 dput(dentry);
1698 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001699 }
1700 }
Al Viro94bdd652016-04-15 02:42:04 -04001701out:
Al Viro9902af72016-04-15 15:08:36 -04001702 inode_unlock_shared(inode);
Al Viroe3c13922016-03-06 14:03:27 -05001703 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Al Viro52094c82011-02-21 21:34:47 -05001706static inline int may_lookup(struct nameidata *nd)
1707{
1708 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001709 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001710 if (err != -ECHILD)
1711 return err;
Al Viro6e9918b2015-05-05 09:26:05 -04001712 if (unlazy_walk(nd, NULL, 0))
Al Viro52094c82011-02-21 21:34:47 -05001713 return -ECHILD;
1714 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001715 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001716}
1717
Al Viro9856fa12011-03-04 14:22:06 -05001718static inline int handle_dots(struct nameidata *nd, int type)
1719{
1720 if (type == LAST_DOTDOT) {
Al Viro9e6697e2015-12-05 20:07:21 -05001721 if (!nd->root.mnt)
1722 set_root(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001723 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001724 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001725 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001726 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001727 }
1728 return 0;
1729}
1730
Al Viro181548c2015-05-07 19:54:34 -04001731static int pick_link(struct nameidata *nd, struct path *link,
1732 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001733{
Al Viro626de992015-05-04 18:26:59 -04001734 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001735 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001736 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001737 path_to_nameidata(link, nd);
1738 return -ELOOP;
1739 }
Al Virobc40aee2015-05-09 13:04:24 -04001740 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001741 if (link->mnt == nd->path.mnt)
1742 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001743 }
Al Viro626de992015-05-04 18:26:59 -04001744 error = nd_alloc_stack(nd);
1745 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001746 if (error == -ECHILD) {
1747 if (unlikely(unlazy_link(nd, link, seq)))
1748 return -ECHILD;
1749 error = nd_alloc_stack(nd);
1750 }
1751 if (error) {
1752 path_put(link);
1753 return error;
1754 }
Al Viro626de992015-05-04 18:26:59 -04001755 }
1756
Al Viroab104922015-05-10 11:50:01 -04001757 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001758 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001759 clear_delayed_call(&last->done);
1760 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001761 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001762 return 1;
1763}
1764
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001765/*
1766 * Do we need to follow links? We _really_ want to be able
1767 * to do this check without having to look at inode->i_op,
1768 * so we keep a cache of "no, this doesn't need follow_link"
1769 * for the common case.
1770 */
Al Viro254cf582015-05-05 09:40:46 -04001771static inline int should_follow_link(struct nameidata *nd, struct path *link,
Al Viro181548c2015-05-07 19:54:34 -04001772 int follow,
1773 struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001774{
Al Virod63ff282015-05-04 18:13:23 -04001775 if (likely(!d_is_symlink(link->dentry)))
1776 return 0;
1777 if (!follow)
1778 return 0;
Al Viroa7f77542016-02-27 19:31:01 -05001779 /* make sure that d_is_symlink above matches inode */
1780 if (nd->flags & LOOKUP_RCU) {
1781 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1782 return -ECHILD;
1783 }
Al Viro181548c2015-05-07 19:54:34 -04001784 return pick_link(nd, link, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001785}
1786
Al Viro4693a542015-05-04 17:47:11 -04001787enum {WALK_GET = 1, WALK_PUT = 2};
1788
1789static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001790{
Al Virocaa85632015-04-22 17:52:47 -04001791 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001792 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001793 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001794 int err;
1795 /*
1796 * "." and ".." are special - ".." especially so because it has
1797 * to be able to know about the current root directory and
1798 * parent relationships.
1799 */
Al Viro4693a542015-05-04 17:47:11 -04001800 if (unlikely(nd->last_type != LAST_NORM)) {
1801 err = handle_dots(nd, nd->last_type);
1802 if (flags & WALK_PUT)
1803 put_link(nd);
1804 return err;
1805 }
Al Viro254cf582015-05-05 09:40:46 -04001806 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001807 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001808 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001809 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001810 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1811 nd->flags);
1812 if (IS_ERR(path.dentry))
1813 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001814
Al Viroe3c13922016-03-06 14:03:27 -05001815 path.mnt = nd->path.mnt;
1816 err = follow_managed(&path, nd);
1817 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001818 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001819
Al Viro7500c382016-03-31 00:23:05 -04001820 if (unlikely(d_is_negative(path.dentry))) {
1821 path_to_nameidata(&path, nd);
1822 return -ENOENT;
1823 }
1824
Al Viro254cf582015-05-05 09:40:46 -04001825 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001826 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001827 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001828
Al Viro4693a542015-05-04 17:47:11 -04001829 if (flags & WALK_PUT)
1830 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04001831 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
Al Virod63ff282015-05-04 18:13:23 -04001832 if (unlikely(err))
1833 return err;
Al Virocaa85632015-04-22 17:52:47 -04001834 path_to_nameidata(&path, nd);
Al Viroce57dfc2011-03-13 19:58:58 -04001835 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04001836 nd->seq = seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001837 return 0;
1838}
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001841 * We can do the critical dentry name comparison and hashing
1842 * operations one word at a time, but we are limited to:
1843 *
1844 * - Architectures with fast unaligned word accesses. We could
1845 * do a "get_unaligned()" if this helps and is sufficiently
1846 * fast.
1847 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001848 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1849 * do not trap on the (extremely unlikely) case of a page
1850 * crossing operation.
1851 *
1852 * - Furthermore, we need an efficient 64-bit compile for the
1853 * 64-bit case in order to generate the "number of bytes in
1854 * the final mask". Again, that could be replaced with a
1855 * efficient population count instruction or similar.
1856 */
1857#ifdef CONFIG_DCACHE_WORD_ACCESS
1858
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001859#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001860
George Spelvin468a9422016-05-26 22:11:51 -04001861#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001862
George Spelvin468a9422016-05-26 22:11:51 -04001863/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1864
1865#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001866/*
1867 * Register pressure in the mixing function is an issue, particularly
1868 * on 32-bit x86, but almost any function requires one state value and
1869 * one temporary. Instead, use a function designed for two state values
1870 * and no temporaries.
1871 *
1872 * This function cannot create a collision in only two iterations, so
1873 * we have two iterations to achieve avalanche. In those two iterations,
1874 * we have six layers of mixing, which is enough to spread one bit's
1875 * influence out to 2^6 = 64 state bits.
1876 *
1877 * Rotate constants are scored by considering either 64 one-bit input
1878 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1879 * probability of that delta causing a change to each of the 128 output
1880 * bits, using a sample of random initial states.
1881 *
1882 * The Shannon entropy of the computed probabilities is then summed
1883 * to produce a score. Ideally, any input change has a 50% chance of
1884 * toggling any given output bit.
1885 *
1886 * Mixing scores (in bits) for (12,45):
1887 * Input delta: 1-bit 2-bit
1888 * 1 round: 713.3 42542.6
1889 * 2 rounds: 2753.7 140389.8
1890 * 3 rounds: 5954.1 233458.2
1891 * 4 rounds: 7862.6 256672.2
1892 * Perfect: 8192 258048
1893 * (64*128) (64*63/2 * 128)
1894 */
1895#define HASH_MIX(x, y, a) \
1896 ( x ^= (a), \
1897 y ^= x, x = rol64(x,12),\
1898 x += y, y = rol64(y,45),\
1899 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001900
George Spelvin0fed3ac2016-05-02 06:31:01 -04001901/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001902 * Fold two longs into one 32-bit hash value. This must be fast, but
1903 * latency isn't quite as critical, as there is a fair bit of additional
1904 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001905 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001906static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001907{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001908 y ^= x * GOLDEN_RATIO_64;
1909 y *= GOLDEN_RATIO_64;
1910 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001911}
1912
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001913#else /* 32-bit case */
1914
George Spelvin2a18da7a2016-05-23 07:43:58 -04001915/*
1916 * Mixing scores (in bits) for (7,20):
1917 * Input delta: 1-bit 2-bit
1918 * 1 round: 330.3 9201.6
1919 * 2 rounds: 1246.4 25475.4
1920 * 3 rounds: 1907.1 31295.1
1921 * 4 rounds: 2042.3 31718.6
1922 * Perfect: 2048 31744
1923 * (32*64) (32*31/2 * 64)
1924 */
1925#define HASH_MIX(x, y, a) \
1926 ( x ^= (a), \
1927 y ^= x, x = rol32(x, 7),\
1928 x += y, y = rol32(y,20),\
1929 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001930
George Spelvin2a18da7a2016-05-23 07:43:58 -04001931static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001932{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001933 /* Use arch-optimized multiply if one exists */
1934 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001935}
1936
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001937#endif
1938
George Spelvin2a18da7a2016-05-23 07:43:58 -04001939/*
1940 * Return the hash of a string of known length. This is carfully
1941 * designed to match hash_name(), which is the more critical function.
1942 * In particular, we must end by hashing a final word containing 0..7
1943 * payload bytes, to match the way that hash_name() iterates until it
1944 * finds the delimiter after the name.
1945 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001946unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001947{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001948 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001949
1950 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001951 if (!len)
1952 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001953 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001954 if (len < sizeof(unsigned long))
1955 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001956 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001957 name += sizeof(unsigned long);
1958 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001959 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001960 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001961done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001962 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001963}
1964EXPORT_SYMBOL(full_name_hash);
1965
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001966/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001967u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001968{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001969 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1970 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001971 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1972
Linus Torvalds8387ff22016-06-10 07:51:30 -07001973 len = 0;
1974 goto inside;
1975
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001976 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001977 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001978 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001979inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001980 a = load_unaligned_zeropad(name+len);
1981 } while (!has_zero(a, &adata, &constants));
1982
1983 adata = prep_zero_mask(a, adata, &constants);
1984 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001985 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001986
George Spelvin2a18da7a2016-05-23 07:43:58 -04001987 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001988}
1989EXPORT_SYMBOL(hashlen_string);
1990
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001991/*
1992 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001993 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001994 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001995static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001996{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001997 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1998 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07001999 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002000
Linus Torvalds8387ff22016-06-10 07:51:30 -07002001 len = 0;
2002 goto inside;
2003
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002004 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002005 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002006 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002007inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002008 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002009 b = a ^ REPEAT_BYTE('/');
2010 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002011
Linus Torvalds36126f82012-05-26 10:43:17 -07002012 adata = prep_zero_mask(a, adata, &constants);
2013 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002014 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002015 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002016
George Spelvin2a18da7a2016-05-23 07:43:58 -04002017 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002018}
2019
George Spelvin2a18da7a2016-05-23 07:43:58 -04002020#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002021
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002022/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002023unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002024{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002025 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002026 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002027 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002028 return end_name_hash(hash);
2029}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002030EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002031
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002032/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002033u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002034{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002035 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002036 unsigned long len = 0, c;
2037
2038 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002039 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002040 len++;
2041 hash = partial_name_hash(c, hash);
2042 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002043 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002044 return hashlen_create(end_name_hash(hash), len);
2045}
George Spelvinf2a031b2016-05-29 01:26:41 -04002046EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002047
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002048/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002049 * We know there's a real path component here of at least
2050 * one character.
2051 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002052static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002053{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002054 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002055 unsigned long len = 0, c;
2056
2057 c = (unsigned char)*name;
2058 do {
2059 len++;
2060 hash = partial_name_hash(c, hash);
2061 c = (unsigned char)name[len];
2062 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002063 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002064}
2065
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002066#endif
2067
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002068/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002070 * This is the basic name resolution function, turning a pathname into
2071 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002073 * Returns 0 and nd will have valid dentry and mnt on success.
2074 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 */
Al Viro6de88d72009-08-09 01:41:57 +04002076static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 while (*name=='/')
2081 name++;
2082 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 /* At this point we know we have a real path component. */
2086 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002087 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002088 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Al Viro52094c82011-02-21 21:34:47 -05002090 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002091 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002092 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
Linus Torvalds8387ff22016-06-10 07:51:30 -07002094 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Al Virofe479a52011-02-22 15:10:03 -05002096 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002097 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002098 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002099 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002100 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002101 nd->flags |= LOOKUP_JUMPED;
2102 }
Al Virofe479a52011-02-22 15:10:03 -05002103 break;
2104 case 1:
2105 type = LAST_DOT;
2106 }
Al Viro5a202bc2011-03-08 14:17:44 -05002107 if (likely(type == LAST_NORM)) {
2108 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002109 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002110 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002111 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002112 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002113 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002114 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002115 hash_len = this.hash_len;
2116 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002117 }
2118 }
Al Virofe479a52011-02-22 15:10:03 -05002119
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002120 nd->last.hash_len = hash_len;
2121 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002122 nd->last_type = type;
2123
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002124 name += hashlen_len(hash_len);
2125 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002126 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002127 /*
2128 * If it wasn't NUL, we know it was '/'. Skip that
2129 * slash, and continue until no more slashes.
2130 */
2131 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002132 name++;
2133 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002134 if (unlikely(!*name)) {
2135OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002136 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002137 if (!nd->depth)
2138 return 0;
2139 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002140 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002141 if (!name)
2142 return 0;
2143 /* last component of nested symlink */
Al Viro4693a542015-05-04 17:47:11 -04002144 err = walk_component(nd, WALK_GET | WALK_PUT);
Al Viro8620c232015-05-04 08:58:35 -04002145 } else {
Al Viro4693a542015-05-04 17:47:11 -04002146 err = walk_component(nd, WALK_GET);
Al Viro8620c232015-05-04 08:58:35 -04002147 }
Al Viroce57dfc2011-03-13 19:58:58 -04002148 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002149 return err;
Al Virofe479a52011-02-22 15:10:03 -05002150
Al Viroce57dfc2011-03-13 19:58:58 -04002151 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002152 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002153
Viresh Kumara1c83682015-08-12 15:59:44 +05302154 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002155 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002156 err = 0;
2157 if (unlikely(!s)) {
2158 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002159 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002160 } else {
Al Virofab51e82015-05-10 11:01:00 -04002161 nd->stack[nd->depth - 1].name = name;
2162 name = s;
2163 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002164 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002165 }
Al Viro97242f92015-08-01 19:59:28 -04002166 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2167 if (nd->flags & LOOKUP_RCU) {
2168 if (unlazy_walk(nd, NULL, 0))
2169 return -ECHILD;
2170 }
Al Viro3595e232015-05-09 16:54:45 -04002171 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174}
2175
Al Viroc8a53ee2015-05-12 18:43:07 -04002176static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177{
Prasanna Medaea3834d2005-04-29 16:00:17 +01002178 int retval = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -04002179 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180
Linus Torvalds012e79b2017-04-02 17:10:08 -07002181 if (!*s)
2182 flags &= ~LOOKUP_RCU;
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002185 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002187 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002188 struct dentry *root = nd->root.dentry;
2189 struct inode *inode = root->d_inode;
Al Virofd2f7cb2015-02-22 20:07:13 -05002190 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002191 if (!d_can_lookup(root))
Al Viro368ee9b2015-05-08 17:19:59 -04002192 return ERR_PTR(-ENOTDIR);
Al Viro73d049a2011-03-11 12:08:24 -05002193 retval = inode_permission(inode, MAY_EXEC);
2194 if (retval)
Al Viro368ee9b2015-05-08 17:19:59 -04002195 return ERR_PTR(retval);
Al Viro73d049a2011-03-11 12:08:24 -05002196 }
Al Viro5b6ca022011-03-09 23:04:47 -05002197 nd->path = nd->root;
2198 nd->inode = inode;
2199 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002200 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05002201 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002202 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002203 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002204 } else {
2205 path_get(&nd->path);
2206 }
Al Viro368ee9b2015-05-08 17:19:59 -04002207 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002208 }
2209
Al Viro2a737872009-04-07 11:49:53 -04002210 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002211 nd->path.mnt = NULL;
2212 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
Al Viro48a066e2013-09-29 22:06:07 -04002214 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002215 if (*s == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05002216 if (flags & LOOKUP_RCU)
Al Viro8b61e742013-11-08 12:45:01 -05002217 rcu_read_lock();
Al Viro9e6697e2015-12-05 20:07:21 -05002218 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05002219 if (likely(!nd_jump_root(nd)))
Al Viroef55d912015-12-05 20:25:06 -05002220 return s;
Al Viro248fb5b2015-12-05 20:51:58 -05002221 nd->root.mnt = NULL;
Al Viroef55d912015-12-05 20:25:06 -05002222 rcu_read_unlock();
2223 return ERR_PTR(-ECHILD);
Al Viroc8a53ee2015-05-12 18:43:07 -04002224 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002225 if (flags & LOOKUP_RCU) {
2226 struct fs_struct *fs = current->fs;
2227 unsigned seq;
2228
Al Viro8b61e742013-11-08 12:45:01 -05002229 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002230
2231 do {
2232 seq = read_seqcount_begin(&fs->seq);
2233 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002234 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002235 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2236 } while (read_seqcount_retry(&fs->seq, seq));
2237 } else {
2238 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002239 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002240 }
Al Viroef55d912015-12-05 20:25:06 -05002241 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002242 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002243 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002244 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002245 struct dentry *dentry;
2246
Al Viro2903ff02012-08-28 12:52:22 -04002247 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002248 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002249
Al Viro2903ff02012-08-28 12:52:22 -04002250 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002251
Al Virofd2f7cb2015-02-22 20:07:13 -05002252 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002253 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002254 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002255 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002256 }
Al Virof52e0c12011-03-14 18:56:51 -04002257 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002258
Al Viro2903ff02012-08-28 12:52:22 -04002259 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002260 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002261 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002262 nd->inode = nd->path.dentry->d_inode;
2263 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002264 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002265 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002266 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002267 }
Al Viro34a26b92015-05-11 08:05:05 -04002268 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002269 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002271}
2272
Al Viro3bdba282015-05-08 17:37:07 -04002273static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002274{
2275 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002276 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002277 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002278 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002279 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002280 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002281 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002282 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002283}
2284
Al Virocaa85632015-04-22 17:52:47 -04002285static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002286{
2287 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2288 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2289
2290 nd->flags &= ~LOOKUP_PARENT;
Al Virodeb106c2015-05-08 18:05:21 -04002291 return walk_component(nd,
Al Viro4693a542015-05-04 17:47:11 -04002292 nd->flags & LOOKUP_FOLLOW
2293 ? nd->depth
2294 ? WALK_PUT | WALK_GET
2295 : WALK_GET
2296 : 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002297}
2298
Al Viro9b4a9b12009-04-07 11:44:16 -04002299/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002300static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002301{
Al Viroc8a53ee2015-05-12 18:43:07 -04002302 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002303 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002304
Al Viro368ee9b2015-05-08 17:19:59 -04002305 if (IS_ERR(s))
2306 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002307 while (!(err = link_path_walk(s, nd))
2308 && ((err = lookup_last(nd)) > 0)) {
2309 s = trailing_symlink(nd);
2310 if (IS_ERR(s)) {
2311 err = PTR_ERR(s);
2312 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002313 }
2314 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002315 if (!err)
2316 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002317
Al Virodeb106c2015-05-08 18:05:21 -04002318 if (!err && nd->flags & LOOKUP_DIRECTORY)
2319 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002320 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002321 if (!err) {
2322 *path = nd->path;
2323 nd->path.mnt = NULL;
2324 nd->path.dentry = NULL;
2325 }
2326 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002327 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002328}
Nick Piggin31e6b012011-01-07 17:49:52 +11002329
Al Viro625b6d12015-05-12 16:36:12 -04002330static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002331 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002332{
Al Viro894bc8c2015-05-02 07:16:16 -04002333 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002334 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002335 if (IS_ERR(name))
2336 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002337 if (unlikely(root)) {
2338 nd.root = *root;
2339 flags |= LOOKUP_ROOT;
2340 }
Al Viro9883d182015-05-13 07:28:08 -04002341 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002342 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002343 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002344 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002345 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002346 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002347
2348 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002349 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002350 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002351 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002352 return retval;
2353}
2354
Al Viro8bcb77f2015-05-08 16:59:20 -04002355/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002356static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002357 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002358{
Al Viroc8a53ee2015-05-12 18:43:07 -04002359 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002360 int err;
2361 if (IS_ERR(s))
2362 return PTR_ERR(s);
2363 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002364 if (!err)
2365 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002366 if (!err) {
2367 *parent = nd->path;
2368 nd->path.mnt = NULL;
2369 nd->path.dentry = NULL;
2370 }
2371 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002372 return err;
2373}
2374
Al Viro5c31b6c2015-05-12 17:32:54 -04002375static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002376 unsigned int flags, struct path *parent,
2377 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002378{
2379 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002380 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002381
Al Viro5c31b6c2015-05-12 17:32:54 -04002382 if (IS_ERR(name))
2383 return name;
Al Viro9883d182015-05-13 07:28:08 -04002384 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002385 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002386 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002387 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002388 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002389 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002390 if (likely(!retval)) {
2391 *last = nd.last;
2392 *type = nd.last_type;
2393 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002394 } else {
2395 putname(name);
2396 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002397 }
Al Viro9883d182015-05-13 07:28:08 -04002398 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002399 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002400}
2401
Al Viro79714f72012-06-15 03:01:42 +04002402/* does lookup, returns the object with parent locked */
2403struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002404{
Al Viro5c31b6c2015-05-12 17:32:54 -04002405 struct filename *filename;
2406 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002407 struct qstr last;
2408 int type;
Paul Moore51689102015-01-22 00:00:03 -05002409
Al Viro5c31b6c2015-05-12 17:32:54 -04002410 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2411 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002412 if (IS_ERR(filename))
2413 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002414 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002415 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002416 putname(filename);
2417 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002418 }
Al Viro59551022016-01-22 15:40:57 -05002419 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002420 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002421 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002422 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002423 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002424 }
Paul Moore51689102015-01-22 00:00:03 -05002425 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002426 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002427}
2428
Al Virod1811462008-08-02 00:49:18 -04002429int kern_path(const char *name, unsigned int flags, struct path *path)
2430{
Al Viroabc9f5b2015-05-12 16:53:42 -04002431 return filename_lookup(AT_FDCWD, getname_kernel(name),
2432 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002433}
Al Viro4d359502014-03-14 12:20:17 -04002434EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002435
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002436/**
2437 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2438 * @dentry: pointer to dentry of the base directory
2439 * @mnt: pointer to vfs mount of the base directory
2440 * @name: pointer to file name
2441 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002442 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002443 */
2444int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2445 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002446 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002447{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002448 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002449 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002450 return filename_lookup(AT_FDCWD, getname_kernel(name),
2451 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002452}
Al Viro4d359502014-03-14 12:20:17 -04002453EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002454
Christoph Hellwigeead1912007-10-16 23:25:38 -07002455/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002456 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002457 * @name: pathname component to lookup
2458 * @base: base directory to lookup from
2459 * @len: maximum length @len should be interpreted to
2460 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002461 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002462 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002463 *
2464 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002465 */
James Morris057f6c02007-04-26 00:12:05 -07002466struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2467{
James Morris057f6c02007-04-26 00:12:05 -07002468 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002469 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002470 int err;
James Morris057f6c02007-04-26 00:12:05 -07002471
Al Viro59551022016-01-22 15:40:57 -05002472 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002473
Al Viro6a96ba52011-03-07 23:49:20 -05002474 this.name = name;
2475 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002476 this.hash = full_name_hash(base, name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002477 if (!len)
2478 return ERR_PTR(-EACCES);
2479
Al Viro21d8a152012-11-29 22:17:21 -05002480 if (unlikely(name[0] == '.')) {
2481 if (len < 2 || (len == 2 && name[1] == '.'))
2482 return ERR_PTR(-EACCES);
2483 }
2484
Al Viro6a96ba52011-03-07 23:49:20 -05002485 while (len--) {
2486 c = *(const unsigned char *)name++;
2487 if (c == '/' || c == '\0')
2488 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002489 }
Al Viro5a202bc2011-03-08 14:17:44 -05002490 /*
2491 * See if the low-level filesystem might want
2492 * to use its own hash..
2493 */
2494 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002495 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002496 if (err < 0)
2497 return ERR_PTR(err);
2498 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002499
Miklos Szeredicda309d2012-03-26 12:54:21 +02002500 err = inode_permission(base->d_inode, MAY_EXEC);
2501 if (err)
2502 return ERR_PTR(err);
2503
Al Viro72bd8662012-06-10 17:17:17 -04002504 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002505}
Al Viro4d359502014-03-14 12:20:17 -04002506EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002507
NeilBrownbbddca82016-01-07 16:08:20 -05002508/**
2509 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2510 * @name: pathname component to lookup
2511 * @base: base directory to lookup from
2512 * @len: maximum length @len should be interpreted to
2513 *
2514 * Note that this routine is purely a helper for filesystem usage and should
2515 * not be called by generic code.
2516 *
2517 * Unlike lookup_one_len, it should be called without the parent
2518 * i_mutex held, and will take the i_mutex itself if necessary.
2519 */
2520struct dentry *lookup_one_len_unlocked(const char *name,
2521 struct dentry *base, int len)
2522{
2523 struct qstr this;
2524 unsigned int c;
2525 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002526 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002527
2528 this.name = name;
2529 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002530 this.hash = full_name_hash(base, name, len);
NeilBrownbbddca82016-01-07 16:08:20 -05002531 if (!len)
2532 return ERR_PTR(-EACCES);
2533
2534 if (unlikely(name[0] == '.')) {
2535 if (len < 2 || (len == 2 && name[1] == '.'))
2536 return ERR_PTR(-EACCES);
2537 }
2538
2539 while (len--) {
2540 c = *(const unsigned char *)name++;
2541 if (c == '/' || c == '\0')
2542 return ERR_PTR(-EACCES);
2543 }
2544 /*
2545 * See if the low-level filesystem might want
2546 * to use its own hash..
2547 */
2548 if (base->d_flags & DCACHE_OP_HASH) {
2549 int err = base->d_op->d_hash(base, &this);
2550 if (err < 0)
2551 return ERR_PTR(err);
2552 }
2553
2554 err = inode_permission(base->d_inode, MAY_EXEC);
2555 if (err)
2556 return ERR_PTR(err);
2557
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002558 ret = lookup_dcache(&this, base, 0);
2559 if (!ret)
2560 ret = lookup_slow(&this, base, 0);
2561 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002562}
2563EXPORT_SYMBOL(lookup_one_len_unlocked);
2564
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002565#ifdef CONFIG_UNIX98_PTYS
2566int path_pts(struct path *path)
2567{
2568 /* Find something mounted on "pts" in the same directory as
2569 * the input path.
2570 */
2571 struct dentry *child, *parent;
2572 struct qstr this;
2573 int ret;
2574
2575 ret = path_parent_directory(path);
2576 if (ret)
2577 return ret;
2578
2579 parent = path->dentry;
2580 this.name = "pts";
2581 this.len = 3;
2582 child = d_hash_and_lookup(parent, &this);
2583 if (!child)
2584 return -ENOENT;
2585
2586 path->dentry = child;
2587 dput(parent);
2588 follow_mount(path);
2589 return 0;
2590}
2591#endif
2592
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002593int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2594 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Al Viroabc9f5b2015-05-12 16:53:42 -04002596 return filename_lookup(dfd, getname_flags(name, flags, empty),
2597 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598}
Al Virob853a162015-05-13 09:12:02 -04002599EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002600
Jeff Layton873f1ee2012-10-10 15:25:29 -04002601/*
2602 * NB: most callers don't do anything directly with the reference to the
2603 * to struct filename, but the nd->last pointer points into the name string
2604 * allocated by getname. So we must hold the reference to it until all
2605 * path-walking is complete.
2606 */
Al Viroa2ec4a22015-05-13 06:57:49 -04002607static inline struct filename *
Al Virof5beed72015-04-30 16:09:11 -04002608user_path_parent(int dfd, const char __user *path,
2609 struct path *parent,
2610 struct qstr *last,
2611 int *type,
Jeff Layton9e790bd2012-12-11 12:10:09 -05002612 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002613{
Jeff Layton9e790bd2012-12-11 12:10:09 -05002614 /* only LOOKUP_REVAL is allowed in extra flags */
Al Viro5c31b6c2015-05-12 17:32:54 -04002615 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2616 parent, last, type);
Al Viro2ad94ae2008-07-21 09:32:51 -04002617}
2618
Jeff Layton80334262013-07-26 06:23:25 -04002619/**
Al Viro197df042013-09-08 14:03:27 -04002620 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002621 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2622 * @path: pointer to container for result
2623 *
2624 * This is a special lookup_last function just for umount. In this case, we
2625 * need to resolve the path without doing any revalidation.
2626 *
2627 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2628 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2629 * in almost all cases, this lookup will be served out of the dcache. The only
2630 * cases where it won't are if nd->last refers to a symlink or the path is
2631 * bogus and it doesn't exist.
2632 *
2633 * Returns:
2634 * -error: if there was an error during lookup. This includes -ENOENT if the
2635 * lookup found a negative dentry. The nd->path reference will also be
2636 * put in this case.
2637 *
2638 * 0: if we successfully resolved nd->path and found it to not to be a
2639 * symlink that needs to be followed. "path" will also be populated.
2640 * The nd->path reference will also be put.
2641 *
2642 * 1: if we successfully resolved nd->last and found it to be a symlink
2643 * that needs to be followed. "path" will be populated with the path
2644 * to the link, and nd->path will *not* be put.
2645 */
2646static int
Al Viro197df042013-09-08 14:03:27 -04002647mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002648{
2649 int error = 0;
2650 struct dentry *dentry;
2651 struct dentry *dir = nd->path.dentry;
2652
Al Viro35759522013-09-08 13:41:33 -04002653 /* If we're in rcuwalk, drop out of it to handle last component */
2654 if (nd->flags & LOOKUP_RCU) {
Al Viro6e9918b2015-05-05 09:26:05 -04002655 if (unlazy_walk(nd, NULL, 0))
Al Virodeb106c2015-05-08 18:05:21 -04002656 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002657 }
2658
2659 nd->flags &= ~LOOKUP_PARENT;
2660
2661 if (unlikely(nd->last_type != LAST_NORM)) {
2662 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002663 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002664 return error;
Al Viro35759522013-09-08 13:41:33 -04002665 dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002666 } else {
2667 dentry = d_lookup(dir, &nd->last);
Jeff Layton80334262013-07-26 06:23:25 -04002668 if (!dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002669 /*
2670 * No cached dentry. Mounted dentries are pinned in the
2671 * cache, so that means that this dentry is probably
2672 * a symlink or the path doesn't actually point
2673 * to a mounted dentry.
2674 */
2675 dentry = lookup_slow(&nd->last, dir,
2676 nd->flags | LOOKUP_NO_REVAL);
2677 if (IS_ERR(dentry))
2678 return PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002679 }
Jeff Layton80334262013-07-26 06:23:25 -04002680 }
David Howells698934d2015-03-17 17:33:52 +00002681 if (d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002682 dput(dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002683 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002684 }
Al Viro191d7f72015-05-04 08:26:45 -04002685 if (nd->depth)
2686 put_link(nd);
Al Viro35759522013-09-08 13:41:33 -04002687 path->dentry = dentry;
Vasily Averin295dc392014-07-21 12:30:23 +04002688 path->mnt = nd->path.mnt;
Al Viro181548c2015-05-07 19:54:34 -04002689 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2690 d_backing_inode(dentry), 0);
Al Virodeb106c2015-05-08 18:05:21 -04002691 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04002692 return error;
Vasily Averin295dc392014-07-21 12:30:23 +04002693 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002694 follow_mount(path);
Al Virodeb106c2015-05-08 18:05:21 -04002695 return 0;
Jeff Layton80334262013-07-26 06:23:25 -04002696}
2697
2698/**
Al Viro197df042013-09-08 14:03:27 -04002699 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b852015-09-09 15:39:23 -07002700 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002701 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002702 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002703 *
2704 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002705 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002706 */
2707static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002708path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002709{
Al Viroc8a53ee2015-05-12 18:43:07 -04002710 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002711 int err;
2712 if (IS_ERR(s))
2713 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002714 while (!(err = link_path_walk(s, nd)) &&
2715 (err = mountpoint_last(nd, path)) > 0) {
2716 s = trailing_symlink(nd);
2717 if (IS_ERR(s)) {
2718 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002719 break;
Al Viro3bdba282015-05-08 17:37:07 -04002720 }
Jeff Layton80334262013-07-26 06:23:25 -04002721 }
Al Virodeb106c2015-05-08 18:05:21 -04002722 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002723 return err;
2724}
2725
Al Viro2d864652013-09-08 20:18:44 -04002726static int
Al Viro668696d2015-02-22 19:44:00 -05002727filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002728 unsigned int flags)
2729{
Al Viro9883d182015-05-13 07:28:08 -04002730 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002731 int error;
Al Viro668696d2015-02-22 19:44:00 -05002732 if (IS_ERR(name))
2733 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002734 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002735 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002736 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002737 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002738 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002739 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002740 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002741 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002742 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002743 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002744 return error;
2745}
2746
Jeff Layton80334262013-07-26 06:23:25 -04002747/**
Al Viro197df042013-09-08 14:03:27 -04002748 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002749 * @dfd: directory file descriptor
2750 * @name: pathname from userland
2751 * @flags: lookup flags
2752 * @path: pointer to container to hold result
2753 *
2754 * A umount is a special case for path walking. We're not actually interested
2755 * in the inode in this situation, and ESTALE errors can be a problem. We
2756 * simply want track down the dentry and vfsmount attached at the mountpoint
2757 * and avoid revalidating the last component.
2758 *
2759 * Returns 0 and populates "path" on success.
2760 */
2761int
Al Viro197df042013-09-08 14:03:27 -04002762user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002763 struct path *path)
2764{
Al Virocbaab2d2015-01-22 02:49:00 -05002765 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002766}
2767
Al Viro2d864652013-09-08 20:18:44 -04002768int
2769kern_path_mountpoint(int dfd, const char *name, struct path *path,
2770 unsigned int flags)
2771{
Al Virocbaab2d2015-01-22 02:49:00 -05002772 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002773}
2774EXPORT_SYMBOL(kern_path_mountpoint);
2775
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002776int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002778 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002779
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002780 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002782 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002784 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002786EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788/*
2789 * Check whether we can remove a link victim from directory dir, check
2790 * whether the type of victim is right.
2791 * 1. We can't do it if dir is read-only (done in permission())
2792 * 2. We should have write and exec permissions on dir
2793 * 3. We can't remove anything from append-only dir
2794 * 4. We can't do anything with immutable dir (done in permission())
2795 * 5. If the sticky bit on dir is set we should either
2796 * a. be owner of dir, or
2797 * b. be owner of victim, or
2798 * c. have CAP_FOWNER capability
2799 * 6. If the victim is append-only or immutable we can't do antyhing with
2800 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002801 * 7. If the victim has an unknown uid or gid we can't change the inode.
2802 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2803 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2804 * 10. We can't remove a root or mountpoint.
2805 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 * nfs_async_unlink().
2807 */
David Howellsb18825a2013-09-12 19:22:53 +01002808static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
David Howells63afdfc2015-05-06 15:59:00 +01002810 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 int error;
2812
David Howellsb18825a2013-09-12 19:22:53 +01002813 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002815 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
2817 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002818 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
Al Virof419a2e2008-07-22 00:07:17 -04002820 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 if (error)
2822 return error;
2823 if (IS_APPEND(dir))
2824 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002825
2826 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002827 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return -EPERM;
2829 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002830 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 return -ENOTDIR;
2832 if (IS_ROOT(victim))
2833 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002834 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return -EISDIR;
2836 if (IS_DEADDIR(dir))
2837 return -ENOENT;
2838 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2839 return -EBUSY;
2840 return 0;
2841}
2842
2843/* Check whether we can create an object with dentry child in directory
2844 * dir.
2845 * 1. We can't do it if child already exists (open has special treatment for
2846 * this case, but since we are inlined it's OK)
2847 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002848 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2849 * 4. We should have write and exec permissions on dir
2850 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002852static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002854 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002855 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 if (child->d_inode)
2857 return -EEXIST;
2858 if (IS_DEADDIR(dir))
2859 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002860 s_user_ns = dir->i_sb->s_user_ns;
2861 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2862 !kgid_has_mapping(s_user_ns, current_fsgid()))
2863 return -EOVERFLOW;
Al Virof419a2e2008-07-22 00:07:17 -04002864 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867/*
2868 * p1 and p2 should be directories on the same fs.
2869 */
2870struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2871{
2872 struct dentry *p;
2873
2874 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002875 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 return NULL;
2877 }
2878
Al Virofc640052016-04-10 01:33:30 -04002879 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002881 p = d_ancestor(p2, p1);
2882 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002883 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2884 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002885 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 }
2887
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002888 p = d_ancestor(p1, p2);
2889 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002890 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2891 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002892 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 }
2894
Al Viro59551022016-01-22 15:40:57 -05002895 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2896 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 return NULL;
2898}
Al Viro4d359502014-03-14 12:20:17 -04002899EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900
2901void unlock_rename(struct dentry *p1, struct dentry *p2)
2902{
Al Viro59551022016-01-22 15:40:57 -05002903 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002905 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002906 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 }
2908}
Al Viro4d359502014-03-14 12:20:17 -04002909EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
Al Viro4acdaf22011-07-26 01:42:34 -04002911int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002912 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002914 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (error)
2916 return error;
2917
Al Viroacfa4382008-12-04 10:06:33 -05002918 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return -EACCES; /* shouldn't it be ENOSYS? */
2920 mode &= S_IALLUGO;
2921 mode |= S_IFREG;
2922 error = security_inode_create(dir, dentry, mode);
2923 if (error)
2924 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002925 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002926 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002927 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return error;
2929}
Al Viro4d359502014-03-14 12:20:17 -04002930EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002932bool may_open_dev(const struct path *path)
2933{
2934 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2935 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2936}
2937
Al Viro73d049a2011-03-11 12:08:24 -05002938static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002940 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 struct inode *inode = dentry->d_inode;
2942 int error;
2943
2944 if (!inode)
2945 return -ENOENT;
2946
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002947 switch (inode->i_mode & S_IFMT) {
2948 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002950 case S_IFDIR:
2951 if (acc_mode & MAY_WRITE)
2952 return -EISDIR;
2953 break;
2954 case S_IFBLK:
2955 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002956 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002958 /*FALLTHRU*/
2959 case S_IFIFO:
2960 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002962 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002963 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002964
Al Viro62fb4a12015-12-26 22:33:24 -05002965 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002966 if (error)
2967 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002968
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 /*
2970 * An append-only file must be opened in append mode for writing.
2971 */
2972 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002973 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002974 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002976 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978
2979 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002980 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002981 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002983 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002984}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Jeff Laytone1181ee2010-12-07 16:19:50 -05002986static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002987{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002988 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002989 struct inode *inode = path->dentry->d_inode;
2990 int error = get_write_access(inode);
2991 if (error)
2992 return error;
2993 /*
2994 * Refuse to truncate files with mandatory locks held on them.
2995 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002996 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002997 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002998 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002999 if (!error) {
3000 error = do_truncate(path->dentry, 0,
3001 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003002 filp);
Al Viro7715b522009-12-16 03:54:00 -05003003 }
3004 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003005 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006}
3007
Dave Hansend57999e2008-02-15 14:37:27 -08003008static inline int open_to_namei_flags(int flag)
3009{
Al Viro8a5e9292011-06-25 19:15:54 -04003010 if ((flag & O_ACCMODE) == 3)
3011 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003012 return flag;
3013}
3014
Al Virod3607752016-03-25 15:21:09 -04003015static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003016{
Seth Forshee2e727b82017-10-07 22:36:55 +00003017 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003018 int error = security_path_mknod(dir, dentry, mode, 0);
3019 if (error)
3020 return error;
3021
Seth Forshee2e727b82017-10-07 22:36:55 +00003022 s_user_ns = dir->dentry->d_sb->s_user_ns;
3023 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3024 !kgid_has_mapping(s_user_ns, current_fsgid()))
3025 return -EOVERFLOW;
3026
Miklos Szeredid18e9002012-06-05 15:10:17 +02003027 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3028 if (error)
3029 return error;
3030
3031 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3032}
3033
David Howells1acf0af2012-06-14 16:13:46 +01003034/*
3035 * Attempt to atomically look up, create and open a file from a negative
3036 * dentry.
3037 *
3038 * Returns 0 if successful. The file will have been created and attached to
3039 * @file by the filesystem calling finish_open().
3040 *
3041 * Returns 1 if the file was looked up only or didn't need creating. The
3042 * caller will need to perform the open themselves. @path will have been
3043 * updated to point to the new dentry. This may be negative.
3044 *
3045 * Returns an error code otherwise.
3046 */
Al Viro2675a4e2012-06-22 12:41:10 +04003047static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3048 struct path *path, struct file *file,
3049 const struct open_flags *op,
Al Viro1643b432016-04-27 19:14:10 -04003050 int open_flag, umode_t mode,
Al Viro2675a4e2012-06-22 12:41:10 +04003051 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003052{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003053 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003054 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003055 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003056
Al Viro384f26e2016-04-28 02:03:55 -04003057 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003058 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003059
Miklos Szeredid18e9002012-06-05 15:10:17 +02003060 if (nd->flags & LOOKUP_DIRECTORY)
3061 open_flag |= O_DIRECTORY;
3062
Al Viro30d90492012-06-22 12:40:19 +04003063 file->f_path.dentry = DENTRY_NOT_SET;
3064 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003065 error = dir->i_op->atomic_open(dir, dentry, file,
3066 open_to_namei_flags(open_flag),
3067 mode, opened);
Al Viro6fbd0712016-04-28 11:50:59 -04003068 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003069 if (!error) {
3070 /*
3071 * We didn't have the inode before the open, so check open
3072 * permission here.
3073 */
3074 int acc_mode = op->acc_mode;
3075 if (*opened & FILE_CREATED) {
3076 WARN_ON(!(open_flag & O_CREAT));
3077 fsnotify_create(dir, dentry);
3078 acc_mode = 0;
3079 }
3080 error = may_open(&file->f_path, acc_mode, open_flag);
3081 if (WARN_ON(error > 0))
3082 error = -EINVAL;
3083 } else if (error > 0) {
Al Viro30d90492012-06-22 12:40:19 +04003084 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003085 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003086 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003087 if (file->f_path.dentry) {
3088 dput(dentry);
3089 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003090 }
Al Viro384f26e2016-04-28 02:03:55 -04003091 if (*opened & FILE_CREATED)
3092 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04003093 if (unlikely(d_is_negative(dentry))) {
3094 error = -ENOENT;
3095 } else {
3096 path->dentry = dentry;
3097 path->mnt = nd->path.mnt;
3098 return 1;
3099 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003100 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003101 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003102 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003103 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003104}
3105
Nick Piggin31e6b012011-01-07 17:49:52 +11003106/*
David Howells1acf0af2012-06-14 16:13:46 +01003107 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003108 *
3109 * Must be called with i_mutex held on parent.
3110 *
David Howells1acf0af2012-06-14 16:13:46 +01003111 * Returns 0 if the file was successfully atomically created (if necessary) and
3112 * opened. In this case the file will be returned attached to @file.
3113 *
3114 * Returns 1 if the file was not completely opened at this time, though lookups
3115 * and creations will have been performed and the dentry returned in @path will
3116 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3117 * specified then a negative dentry may be returned.
3118 *
3119 * An error code is returned otherwise.
3120 *
3121 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3122 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003123 */
Al Viro2675a4e2012-06-22 12:41:10 +04003124static int lookup_open(struct nameidata *nd, struct path *path,
3125 struct file *file,
3126 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04003127 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003128{
3129 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003130 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003131 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003132 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003133 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003134 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003135 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003136
Al Viroce8644f2016-04-26 14:17:56 -04003137 if (unlikely(IS_DEADDIR(dir_inode)))
3138 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003139
Al Viro47237682012-06-10 05:01:45 -04003140 *opened &= ~FILE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003141 dentry = d_lookup(dir, &nd->last);
3142 for (;;) {
3143 if (!dentry) {
3144 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3145 if (IS_ERR(dentry))
3146 return PTR_ERR(dentry);
3147 }
3148 if (d_in_lookup(dentry))
3149 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003150
Al Viro6fbd0712016-04-28 11:50:59 -04003151 if (!(dentry->d_flags & DCACHE_OP_REVALIDATE))
3152 break;
3153
3154 error = d_revalidate(dentry, nd->flags);
3155 if (likely(error > 0))
3156 break;
3157 if (error)
3158 goto out_dput;
3159 d_invalidate(dentry);
3160 dput(dentry);
3161 dentry = NULL;
3162 }
3163 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003164 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003165 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003166 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003167
Al Viro1643b432016-04-27 19:14:10 -04003168 /*
3169 * Checking write permission is tricky, bacuse we don't know if we are
3170 * going to actually need it: O_CREAT opens should work as long as the
3171 * file exists. But checking existence breaks atomicity. The trick is
3172 * to check access and if not granted clear O_CREAT from the flags.
3173 *
3174 * Another problem is returing the "right" error value (e.g. for an
3175 * O_EXCL open we want to return EEXIST not EROFS).
3176 */
3177 if (open_flag & O_CREAT) {
3178 if (!IS_POSIXACL(dir->d_inode))
3179 mode &= ~current_umask();
3180 if (unlikely(!got_write)) {
3181 create_error = -EROFS;
3182 open_flag &= ~O_CREAT;
3183 if (open_flag & (O_EXCL | O_TRUNC))
3184 goto no_open;
3185 /* No side effects, safe to clear O_CREAT */
3186 } else {
3187 create_error = may_o_create(&nd->path, dentry, mode);
3188 if (create_error) {
3189 open_flag &= ~O_CREAT;
3190 if (open_flag & O_EXCL)
3191 goto no_open;
3192 }
3193 }
3194 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3195 unlikely(!got_write)) {
3196 /*
3197 * No O_CREATE -> atomicity not a requirement -> fall
3198 * back to lookup + open
3199 */
3200 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003201 }
3202
Al Viro1643b432016-04-27 19:14:10 -04003203 if (dir_inode->i_op->atomic_open) {
3204 error = atomic_open(nd, dentry, path, file, op, open_flag,
3205 mode, opened);
3206 if (unlikely(error == -ENOENT) && create_error)
3207 error = create_error;
3208 return error;
3209 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003210
Al Viro1643b432016-04-27 19:14:10 -04003211no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003212 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003213 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3214 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003215 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003216 if (unlikely(res)) {
3217 if (IS_ERR(res)) {
3218 error = PTR_ERR(res);
3219 goto out_dput;
3220 }
3221 dput(dentry);
3222 dentry = res;
3223 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003224 }
3225
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003226 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003227 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro47237682012-06-10 05:01:45 -04003228 *opened |= FILE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003229 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003230 if (!dir_inode->i_op->create) {
3231 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003232 goto out_dput;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003233 }
Al Viroce8644f2016-04-26 14:17:56 -04003234 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003235 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003236 if (error)
3237 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003238 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003239 }
Al Viro1643b432016-04-27 19:14:10 -04003240 if (unlikely(create_error) && !dentry->d_inode) {
3241 error = create_error;
3242 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003243 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003244out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003245 path->dentry = dentry;
3246 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04003247 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003248
3249out_dput:
3250 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003251 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003252}
3253
3254/*
Al Virofe2d35f2011-03-05 22:58:25 -05003255 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003256 */
Al Viro896475d2015-04-22 18:02:17 -04003257static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04003258 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04003259 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05003260{
Al Viroa1e28032009-12-24 02:12:06 -05003261 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003262 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003263 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003264 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003265 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003266 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003267 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003268 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003269 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003270
Al Viroc3e380b2011-02-23 13:39:45 -05003271 nd->flags &= ~LOOKUP_PARENT;
3272 nd->flags |= op->intent;
3273
Al Virobc77daa2013-06-06 09:12:33 -04003274 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003275 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003276 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003277 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003278 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003279 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003280
Al Viroca344a892011-03-09 00:36:45 -05003281 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003282 if (nd->last.name[nd->last.len])
3283 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3284 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003285 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003286 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003287 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003288
Miklos Szeredi71574862012-06-05 15:10:14 +02003289 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003290 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003291
Miklos Szeredi71574862012-06-05 15:10:14 +02003292 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003293 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003294 } else {
3295 /* create side of things */
3296 /*
3297 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3298 * has been cleared when we got to the last component we are
3299 * about to look up
3300 */
3301 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003302 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003303 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003304
Al Viro76ae2a52015-05-12 18:44:32 -04003305 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003306 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003307 if (unlikely(nd->last.name[nd->last.len]))
3308 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003309 }
3310
Al Viro9cf843e2016-04-28 19:35:16 -04003311 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003312 error = mnt_want_write(nd->path.mnt);
3313 if (!error)
3314 got_write = true;
3315 /*
3316 * do _not_ fail yet - we might not need that or fail with
3317 * a different error; let lookup_open() decide; we'll be
3318 * dropping this one anyway.
3319 */
3320 }
Al Viro9cf843e2016-04-28 19:35:16 -04003321 if (open_flag & O_CREAT)
3322 inode_lock(dir->d_inode);
3323 else
3324 inode_lock_shared(dir->d_inode);
Al Viro896475d2015-04-22 18:02:17 -04003325 error = lookup_open(nd, &path, file, op, got_write, opened);
Al Viro9cf843e2016-04-28 19:35:16 -04003326 if (open_flag & O_CREAT)
3327 inode_unlock(dir->d_inode);
3328 else
3329 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003330
Al Viro2675a4e2012-06-22 12:41:10 +04003331 if (error <= 0) {
3332 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003333 goto out;
3334
Al Viro47237682012-06-10 05:01:45 -04003335 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003336 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003337 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003338
Al Viro76ae2a52015-05-12 18:44:32 -04003339 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003340 goto opened;
3341 }
Al Virofb1cc552009-12-24 01:58:28 -05003342
Al Viro47237682012-06-10 05:01:45 -04003343 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003344 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003345 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003346 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003347 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003348 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003349 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003350 }
3351
3352 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003353 * If atomic_open() acquired write access it is dropped now due to
3354 * possible mount and symlink following (this might be optimized away if
3355 * necessary...)
3356 */
Al Viro64894cf2012-07-31 00:53:35 +04003357 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003358 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003359 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003360 }
3361
Al Viroe6ec03a2016-06-05 00:23:09 -04003362 error = follow_managed(&path, nd);
3363 if (unlikely(error < 0))
3364 return error;
3365
Al Viro6583fe22016-03-05 18:14:03 -05003366 if (unlikely(d_is_negative(path.dentry))) {
3367 path_to_nameidata(&path, nd);
3368 return -ENOENT;
3369 }
3370
3371 /*
3372 * create/update audit record if it already exists.
3373 */
3374 audit_inode(nd->name, path.dentry, 0);
3375
Al Virodeb106c2015-05-08 18:05:21 -04003376 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3377 path_to_nameidata(&path, nd);
3378 return -EEXIST;
3379 }
Al Virofb1cc552009-12-24 01:58:28 -05003380
Al Viro254cf582015-05-05 09:40:46 -04003381 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003382 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003383finish_lookup:
Al Virod63ff282015-05-04 18:13:23 -04003384 if (nd->depth)
3385 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04003386 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3387 inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003388 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003389 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003390
Al Virofac7d192016-06-04 11:41:49 -04003391 path_to_nameidata(&path, nd);
Miklos Szeredidecf3402012-05-21 17:30:08 +02003392 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04003393 nd->seq = seq;
Al Viroa3fbbde2011-11-07 21:21:26 +00003394 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003395finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003396 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003397 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003398 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003399 audit_inode(nd->name, nd->path.dentry, 0);
Salvatore Mesoraca0c41bee2018-08-23 17:00:35 -07003400 if (open_flag & O_CREAT) {
3401 error = -EISDIR;
3402 if (d_is_dir(nd->path.dentry))
3403 goto out;
3404 error = may_create_in_sticky(dir,
3405 d_backing_inode(nd->path.dentry));
3406 if (unlikely(error))
3407 goto out;
3408 }
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003409 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003410 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003411 goto out;
David Howells4bbcbd32015-03-17 22:16:40 +00003412 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003413 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003414
Al Viro0f9d1a12011-03-09 00:13:14 -05003415 if (will_truncate) {
3416 error = mnt_want_write(nd->path.mnt);
3417 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003418 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003419 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003420 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003421finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003422 error = may_open(&nd->path, acc_mode, open_flag);
3423 if (error)
3424 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003425 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3426 error = vfs_open(&nd->path, file, current_cred());
Al Virofac7d192016-06-04 11:41:49 -04003427 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003428 goto out;
Al Virofac7d192016-06-04 11:41:49 -04003429 *opened |= FILE_OPENED;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003430opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003431 error = open_check_o_direct(file);
Al Virofe9ec822016-04-27 03:14:20 -04003432 if (!error)
3433 error = ima_file_check(file, op->acc_mode, *opened);
3434 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003435 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003436out:
Al Virofe9ec822016-04-27 03:14:20 -04003437 if (unlikely(error) && (*opened & FILE_OPENED))
3438 fput(file);
Al Viroc80567c2016-02-27 19:17:33 -05003439 if (unlikely(error > 0)) {
3440 WARN_ON(1);
3441 error = -EINVAL;
3442 }
Al Viro64894cf2012-07-31 00:53:35 +04003443 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003444 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003445 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003446}
3447
Al Viroc8a53ee2015-05-12 18:43:07 -04003448static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003449 const struct open_flags *op,
3450 struct file *file, int *opened)
3451{
3452 static const struct qstr name = QSTR_INIT("/", 1);
Al Viro625b6d12015-05-12 16:36:12 -04003453 struct dentry *child;
Al Viro60545d02013-06-07 01:20:27 -04003454 struct inode *dir;
Al Viro625b6d12015-05-12 16:36:12 -04003455 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003456 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003457 if (unlikely(error))
3458 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003459 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003460 if (unlikely(error))
3461 goto out;
Al Viro625b6d12015-05-12 16:36:12 -04003462 dir = path.dentry->d_inode;
Al Viro60545d02013-06-07 01:20:27 -04003463 /* we want directory to be writable */
Al Viro625b6d12015-05-12 16:36:12 -04003464 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Al Viro60545d02013-06-07 01:20:27 -04003465 if (error)
3466 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003467 if (!dir->i_op->tmpfile) {
3468 error = -EOPNOTSUPP;
3469 goto out2;
3470 }
Al Viro625b6d12015-05-12 16:36:12 -04003471 child = d_alloc(path.dentry, &name);
Al Viro60545d02013-06-07 01:20:27 -04003472 if (unlikely(!child)) {
3473 error = -ENOMEM;
3474 goto out2;
3475 }
Al Viro625b6d12015-05-12 16:36:12 -04003476 dput(path.dentry);
3477 path.dentry = child;
3478 error = dir->i_op->tmpfile(dir, child, op->mode);
Al Viro60545d02013-06-07 01:20:27 -04003479 if (error)
3480 goto out2;
Al Viroc8a53ee2015-05-12 18:43:07 -04003481 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003482 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003483 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003484 if (error)
3485 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003486 file->f_path.mnt = path.mnt;
3487 error = finish_open(file, child, NULL, opened);
Al Viro60545d02013-06-07 01:20:27 -04003488 if (error)
3489 goto out2;
3490 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003491 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003492 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003493 } else if (!(op->open_flag & O_EXCL)) {
3494 struct inode *inode = file_inode(file);
3495 spin_lock(&inode->i_lock);
3496 inode->i_state |= I_LINKABLE;
3497 spin_unlock(&inode->i_lock);
3498 }
Al Viro60545d02013-06-07 01:20:27 -04003499out2:
Al Viro625b6d12015-05-12 16:36:12 -04003500 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003501out:
Al Viro625b6d12015-05-12 16:36:12 -04003502 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003503 return error;
3504}
3505
Al Viro6ac08702016-04-26 00:02:50 -04003506static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3507{
3508 struct path path;
3509 int error = path_lookupat(nd, flags, &path);
3510 if (!error) {
3511 audit_inode(nd->name, path.dentry, 0);
3512 error = vfs_open(&path, file, current_cred());
3513 path_put(&path);
3514 }
3515 return error;
3516}
3517
Al Viroc8a53ee2015-05-12 18:43:07 -04003518static struct file *path_openat(struct nameidata *nd,
3519 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
Al Viro368ee9b2015-05-08 17:19:59 -04003521 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003522 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003523 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003524 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003525
Al Viro30d90492012-06-22 12:40:19 +04003526 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003527 if (IS_ERR(file))
3528 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003529
Al Viro30d90492012-06-22 12:40:19 +04003530 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003531
Al Virobb458c62013-07-13 13:26:37 +04003532 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003533 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003534 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003535 }
3536
Al Viro6ac08702016-04-26 00:02:50 -04003537 if (unlikely(file->f_flags & O_PATH)) {
3538 error = do_o_path(nd, flags, file);
3539 if (!error)
3540 opened |= FILE_OPENED;
3541 goto out2;
3542 }
3543
Al Viroc8a53ee2015-05-12 18:43:07 -04003544 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003545 if (IS_ERR(s)) {
3546 put_filp(file);
3547 return ERR_CAST(s);
3548 }
Al Viro3bdba282015-05-08 17:37:07 -04003549 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003550 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003551 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003552 s = trailing_symlink(nd);
3553 if (IS_ERR(s)) {
3554 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003555 break;
Al Viro3bdba282015-05-08 17:37:07 -04003556 }
Al Viro806b6812009-12-26 07:16:40 -05003557 }
Al Virodeb106c2015-05-08 18:05:21 -04003558 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003559out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003560 if (!(opened & FILE_OPENED)) {
3561 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003562 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003563 }
Al Viro2675a4e2012-06-22 12:41:10 +04003564 if (unlikely(error)) {
3565 if (error == -EOPENSTALE) {
3566 if (flags & LOOKUP_RCU)
3567 error = -ECHILD;
3568 else
3569 error = -ESTALE;
3570 }
3571 file = ERR_PTR(error);
3572 }
3573 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574}
3575
Jeff Layton669abf42012-10-10 16:43:10 -04003576struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003577 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003578{
Al Viro9883d182015-05-13 07:28:08 -04003579 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003580 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003581 struct file *filp;
3582
Al Viro9883d182015-05-13 07:28:08 -04003583 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003584 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003585 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003586 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003587 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003588 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003589 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003590 return filp;
3591}
3592
Al Viro73d049a2011-03-11 12:08:24 -05003593struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003594 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003595{
Al Viro9883d182015-05-13 07:28:08 -04003596 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003597 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003598 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003599 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003600
3601 nd.root.mnt = mnt;
3602 nd.root.dentry = dentry;
3603
David Howellsb18825a2013-09-12 19:22:53 +01003604 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003605 return ERR_PTR(-ELOOP);
3606
Paul Moore51689102015-01-22 00:00:03 -05003607 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303608 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003609 return ERR_CAST(filename);
3610
Al Viro9883d182015-05-13 07:28:08 -04003611 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003612 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003613 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003614 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003615 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003616 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003617 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003618 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003619 return file;
3620}
3621
Al Virofa14a0b2015-01-22 02:16:49 -05003622static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003623 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003625 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003626 struct qstr last;
3627 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003628 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003629 int error;
3630 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3631
3632 /*
3633 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3634 * other flags passed in are ignored!
3635 */
3636 lookup_flags &= LOOKUP_REVAL;
3637
Al Viro5c31b6c2015-05-12 17:32:54 -04003638 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3639 if (IS_ERR(name))
3640 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003642 /*
3643 * Yucky last component or no last component at all?
3644 * (foo/., foo/.., /////)
3645 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003646 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003647 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003648
Jan Karac30dabf2012-06-12 16:20:30 +02003649 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003650 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003651 /*
3652 * Do the final lookup.
3653 */
Al Viro391172c2015-05-09 11:19:16 -04003654 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003655 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003656 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003658 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003659
Al Viroa8104a92012-07-20 02:25:00 +04003660 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003661 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003662 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003663
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003664 /*
3665 * Special case - lookup gave negative, but... we had foo/bar/
3666 * From the vfs_mknod() POV we just have a negative dentry -
3667 * all is fine. Let's be bastards - you had / on the end, you've
3668 * been asking for (non-existent) directory. -ENOENT for you.
3669 */
Al Viro391172c2015-05-09 11:19:16 -04003670 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003671 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003672 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003673 }
Jan Karac30dabf2012-06-12 16:20:30 +02003674 if (unlikely(err2)) {
3675 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003676 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003677 }
Al Viro181c37b2015-05-12 17:21:25 -04003678 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680fail:
Al Viroa8104a92012-07-20 02:25:00 +04003681 dput(dentry);
3682 dentry = ERR_PTR(error);
3683unlock:
Al Viro59551022016-01-22 15:40:57 -05003684 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003685 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003686 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003687out:
Al Viro391172c2015-05-09 11:19:16 -04003688 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003689 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 return dentry;
3691}
Al Virofa14a0b2015-01-22 02:16:49 -05003692
3693struct dentry *kern_path_create(int dfd, const char *pathname,
3694 struct path *path, unsigned int lookup_flags)
3695{
Al Viro181c37b2015-05-12 17:21:25 -04003696 return filename_create(dfd, getname_kernel(pathname),
3697 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003698}
Al Virodae6ad82011-06-26 11:50:15 -04003699EXPORT_SYMBOL(kern_path_create);
3700
Al Viro921a1652012-07-20 01:15:31 +04003701void done_path_create(struct path *path, struct dentry *dentry)
3702{
3703 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003704 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003705 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003706 path_put(path);
3707}
3708EXPORT_SYMBOL(done_path_create);
3709
Al Viro520ae682015-05-13 07:00:28 -04003710inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003711 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003712{
Al Viro181c37b2015-05-12 17:21:25 -04003713 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003714}
3715EXPORT_SYMBOL(user_path_create);
3716
Al Viro1a67aaf2011-07-26 01:52:52 -04003717int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003719 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
3721 if (error)
3722 return error;
3723
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003724 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 return -EPERM;
3726
Al Viroacfa4382008-12-04 10:06:33 -05003727 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 return -EPERM;
3729
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003730 error = devcgroup_inode_mknod(mode, dev);
3731 if (error)
3732 return error;
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 error = security_inode_mknod(dir, dentry, mode, dev);
3735 if (error)
3736 return error;
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003739 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003740 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741 return error;
3742}
Al Viro4d359502014-03-14 12:20:17 -04003743EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744
Al Virof69aac02011-07-26 04:31:40 -04003745static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003746{
3747 switch (mode & S_IFMT) {
3748 case S_IFREG:
3749 case S_IFCHR:
3750 case S_IFBLK:
3751 case S_IFIFO:
3752 case S_IFSOCK:
3753 case 0: /* zero mode translates to S_IFREG */
3754 return 0;
3755 case S_IFDIR:
3756 return -EPERM;
3757 default:
3758 return -EINVAL;
3759 }
3760}
3761
Al Viro8208a222011-07-25 17:32:17 -04003762SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003763 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764{
Al Viro2ad94ae2008-07-21 09:32:51 -04003765 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003766 struct path path;
3767 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003768 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769
Al Viro8e4bfca2012-07-20 01:17:26 +04003770 error = may_mknod(mode);
3771 if (error)
3772 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003773retry:
3774 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003775 if (IS_ERR(dentry))
3776 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003777
Al Virodae6ad82011-06-26 11:50:15 -04003778 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003779 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003780 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003781 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003782 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003783 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003785 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003786 if (!error)
3787 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 break;
3789 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003790 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 new_decode_dev(dev));
3792 break;
3793 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003794 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 }
Al Viroa8104a92012-07-20 02:25:00 +04003797out:
Al Viro921a1652012-07-20 01:15:31 +04003798 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003799 if (retry_estale(error, lookup_flags)) {
3800 lookup_flags |= LOOKUP_REVAL;
3801 goto retry;
3802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return error;
3804}
3805
Al Viro8208a222011-07-25 17:32:17 -04003806SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003807{
3808 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3809}
3810
Al Viro18bb1db2011-07-26 01:41:39 -04003811int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003813 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003814 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
3816 if (error)
3817 return error;
3818
Al Viroacfa4382008-12-04 10:06:33 -05003819 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 return -EPERM;
3821
3822 mode &= (S_IRWXUGO|S_ISVTX);
3823 error = security_inode_mkdir(dir, dentry, mode);
3824 if (error)
3825 return error;
3826
Al Viro8de52772012-02-06 12:45:27 -05003827 if (max_links && dir->i_nlink >= max_links)
3828 return -EMLINK;
3829
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003831 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003832 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 return error;
3834}
Al Viro4d359502014-03-14 12:20:17 -04003835EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836
Al Viroa218d0f2011-11-21 14:59:34 -05003837SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838{
Dave Hansen6902d922006-09-30 23:29:01 -07003839 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003840 struct path path;
3841 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003842 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003844retry:
3845 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003846 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003847 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003848
Al Virodae6ad82011-06-26 11:50:15 -04003849 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003850 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003851 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003852 if (!error)
3853 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003854 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003855 if (retry_estale(error, lookup_flags)) {
3856 lookup_flags |= LOOKUP_REVAL;
3857 goto retry;
3858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 return error;
3860}
3861
Al Viroa218d0f2011-11-21 14:59:34 -05003862SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003863{
3864 return sys_mkdirat(AT_FDCWD, pathname, mode);
3865}
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3868{
3869 int error = may_delete(dir, dentry, 1);
3870
3871 if (error)
3872 return error;
3873
Al Viroacfa4382008-12-04 10:06:33 -05003874 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 return -EPERM;
3876
Al Viro1d2ef592011-09-14 18:55:41 +01003877 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003878 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879
Sage Weil912dbc12011-05-24 13:06:11 -07003880 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003881 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003882 goto out;
3883
3884 error = security_inode_rmdir(dir, dentry);
3885 if (error)
3886 goto out;
3887
Sage Weil3cebde22011-05-29 21:20:59 -07003888 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003889 error = dir->i_op->rmdir(dir, dentry);
3890 if (error)
3891 goto out;
3892
3893 dentry->d_inode->i_flags |= S_DEAD;
3894 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003895 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003896
3897out:
Al Viro59551022016-01-22 15:40:57 -05003898 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003899 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003900 if (!error)
3901 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return error;
3903}
Al Viro4d359502014-03-14 12:20:17 -04003904EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003906static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
3908 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003909 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003911 struct path path;
3912 struct qstr last;
3913 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003914 unsigned int lookup_flags = 0;
3915retry:
Al Virof5beed72015-04-30 16:09:11 -04003916 name = user_path_parent(dfd, pathname,
3917 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003918 if (IS_ERR(name))
3919 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920
Al Virof5beed72015-04-30 16:09:11 -04003921 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003922 case LAST_DOTDOT:
3923 error = -ENOTEMPTY;
3924 goto exit1;
3925 case LAST_DOT:
3926 error = -EINVAL;
3927 goto exit1;
3928 case LAST_ROOT:
3929 error = -EBUSY;
3930 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003932
Al Virof5beed72015-04-30 16:09:11 -04003933 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003934 if (error)
3935 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003936
Al Viro59551022016-01-22 15:40:57 -05003937 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003938 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003940 if (IS_ERR(dentry))
3941 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003942 if (!dentry->d_inode) {
3943 error = -ENOENT;
3944 goto exit3;
3945 }
Al Virof5beed72015-04-30 16:09:11 -04003946 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003947 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003948 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003949 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003950exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003951 dput(dentry);
3952exit2:
Al Viro59551022016-01-22 15:40:57 -05003953 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003954 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955exit1:
Al Virof5beed72015-04-30 16:09:11 -04003956 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003958 if (retry_estale(error, lookup_flags)) {
3959 lookup_flags |= LOOKUP_REVAL;
3960 goto retry;
3961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 return error;
3963}
3964
Heiko Carstens3cdad422009-01-14 14:14:22 +01003965SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003966{
3967 return do_rmdir(AT_FDCWD, pathname);
3968}
3969
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003970/**
3971 * vfs_unlink - unlink a filesystem object
3972 * @dir: parent directory
3973 * @dentry: victim
3974 * @delegated_inode: returns victim inode, if the inode is delegated.
3975 *
3976 * The caller must hold dir->i_mutex.
3977 *
3978 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3979 * return a reference to the inode in delegated_inode. The caller
3980 * should then break the delegation on that inode and retry. Because
3981 * breaking a delegation may take a long time, the caller should drop
3982 * dir->i_mutex before doing so.
3983 *
3984 * Alternatively, a caller may pass NULL for delegated_inode. This may
3985 * be appropriate for callers that expect the underlying filesystem not
3986 * to be NFS exported.
3987 */
3988int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003990 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 int error = may_delete(dir, dentry, 0);
3992
3993 if (error)
3994 return error;
3995
Al Viroacfa4382008-12-04 10:06:33 -05003996 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 return -EPERM;
3998
Al Viro59551022016-01-22 15:40:57 -05003999 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004000 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 error = -EBUSY;
4002 else {
4003 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004004 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004005 error = try_break_deleg(target, delegated_inode);
4006 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004007 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004009 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004010 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004011 detach_mounts(dentry);
4012 }
Al Virobec10522010-03-03 14:12:08 -05004013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004015out:
Al Viro59551022016-01-22 15:40:57 -05004016 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
4018 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4019 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004020 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 }
Robert Love0eeca282005-07-12 17:06:03 -04004023
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 return error;
4025}
Al Viro4d359502014-03-14 12:20:17 -04004026EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028/*
4029 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004030 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 * writeout happening, and we don't want to prevent access to the directory
4032 * while waiting on the I/O.
4033 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004034static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035{
Al Viro2ad94ae2008-07-21 09:32:51 -04004036 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004037 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004039 struct path path;
4040 struct qstr last;
4041 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004043 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004044 unsigned int lookup_flags = 0;
4045retry:
Al Virof5beed72015-04-30 16:09:11 -04004046 name = user_path_parent(dfd, pathname,
4047 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004048 if (IS_ERR(name))
4049 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004052 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004054
Al Virof5beed72015-04-30 16:09:11 -04004055 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004056 if (error)
4057 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004058retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004059 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004060 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 error = PTR_ERR(dentry);
4062 if (!IS_ERR(dentry)) {
4063 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004064 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004065 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004067 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004068 goto slashes;
4069 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004070 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004071 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004072 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04004073 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004074exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 dput(dentry);
4076 }
Al Viro59551022016-01-22 15:40:57 -05004077 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 if (inode)
4079 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004080 inode = NULL;
4081 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004082 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004083 if (!error)
4084 goto retry_deleg;
4085 }
Al Virof5beed72015-04-30 16:09:11 -04004086 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087exit1:
Al Virof5beed72015-04-30 16:09:11 -04004088 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05004090 if (retry_estale(error, lookup_flags)) {
4091 lookup_flags |= LOOKUP_REVAL;
4092 inode = NULL;
4093 goto retry;
4094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return error;
4096
4097slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004098 if (d_is_negative(dentry))
4099 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004100 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004101 error = -EISDIR;
4102 else
4103 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 goto exit2;
4105}
4106
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004107SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004108{
4109 if ((flag & ~AT_REMOVEDIR) != 0)
4110 return -EINVAL;
4111
4112 if (flag & AT_REMOVEDIR)
4113 return do_rmdir(dfd, pathname);
4114
4115 return do_unlinkat(dfd, pathname);
4116}
4117
Heiko Carstens3480b252009-01-14 14:14:16 +01004118SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004119{
4120 return do_unlinkat(AT_FDCWD, pathname);
4121}
4122
Miklos Szeredidb2e7472008-06-24 16:50:16 +02004123int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124{
Miklos Szeredia95164d2008-07-30 15:08:48 +02004125 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
4127 if (error)
4128 return error;
4129
Al Viroacfa4382008-12-04 10:06:33 -05004130 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 return -EPERM;
4132
4133 error = security_inode_symlink(dir, dentry, oldname);
4134 if (error)
4135 return error;
4136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004138 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004139 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return error;
4141}
Al Viro4d359502014-03-14 12:20:17 -04004142EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004144SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4145 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Al Viro2ad94ae2008-07-21 09:32:51 -04004147 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004148 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004149 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004150 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004151 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004154 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004156retry:
4157 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004158 error = PTR_ERR(dentry);
4159 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004160 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004161
Jeff Layton91a27b22012-10-10 15:25:28 -04004162 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004163 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04004164 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004165 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004166 if (retry_estale(error, lookup_flags)) {
4167 lookup_flags |= LOOKUP_REVAL;
4168 goto retry;
4169 }
Dave Hansen6902d922006-09-30 23:29:01 -07004170out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 putname(from);
4172 return error;
4173}
4174
Heiko Carstens3480b252009-01-14 14:14:16 +01004175SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004176{
4177 return sys_symlinkat(oldname, AT_FDCWD, newname);
4178}
4179
J. Bruce Fields146a8592011-09-20 17:14:31 -04004180/**
4181 * vfs_link - create a new link
4182 * @old_dentry: object to be linked
4183 * @dir: new parent
4184 * @new_dentry: where to create the new link
4185 * @delegated_inode: returns inode needing a delegation break
4186 *
4187 * The caller must hold dir->i_mutex
4188 *
4189 * If vfs_link discovers a delegation on the to-be-linked file in need
4190 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4191 * inode in delegated_inode. The caller should then break the delegation
4192 * and retry. Because breaking a delegation may take a long time, the
4193 * caller should drop the i_mutex before doing so.
4194 *
4195 * Alternatively, a caller may pass NULL for delegated_inode. This may
4196 * be appropriate for callers that expect the underlying filesystem not
4197 * to be NFS exported.
4198 */
4199int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200{
4201 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004202 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 int error;
4204
4205 if (!inode)
4206 return -ENOENT;
4207
Miklos Szeredia95164d2008-07-30 15:08:48 +02004208 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 if (error)
4210 return error;
4211
4212 if (dir->i_sb != inode->i_sb)
4213 return -EXDEV;
4214
4215 /*
4216 * A link to an append-only or immutable file cannot be created.
4217 */
4218 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4219 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004220 /*
4221 * Updating the link count will likely cause i_uid and i_gid to
4222 * be writen back improperly if their true value is unknown to
4223 * the vfs.
4224 */
4225 if (HAS_UNMAPPED_ID(inode))
4226 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004227 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004229 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return -EPERM;
4231
4232 error = security_inode_link(old_dentry, dir, new_dentry);
4233 if (error)
4234 return error;
4235
Al Viro59551022016-01-22 15:40:57 -05004236 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304237 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004238 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304239 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004240 else if (max_links && inode->i_nlink >= max_links)
4241 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004242 else {
4243 error = try_break_deleg(inode, delegated_inode);
4244 if (!error)
4245 error = dir->i_op->link(old_dentry, dir, new_dentry);
4246 }
Al Virof4e0c302013-06-11 08:34:36 +04004247
4248 if (!error && (inode->i_state & I_LINKABLE)) {
4249 spin_lock(&inode->i_lock);
4250 inode->i_state &= ~I_LINKABLE;
4251 spin_unlock(&inode->i_lock);
4252 }
Al Viro59551022016-01-22 15:40:57 -05004253 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004254 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004255 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 return error;
4257}
Al Viro4d359502014-03-14 12:20:17 -04004258EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260/*
4261 * Hardlinks are often used in delicate situations. We avoid
4262 * security-related surprises by not following symlinks on the
4263 * newname. --KAB
4264 *
4265 * We don't follow them on the oldname either to be compatible
4266 * with linux 2.0, and to avoid hard-linking to directories
4267 * and other special files. --ADM
4268 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004269SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4270 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271{
4272 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004273 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004274 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304275 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304278 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004279 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304280 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004281 * To use null names we require CAP_DAC_READ_SEARCH
4282 * This ensures that not everyone will be able to create
4283 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304284 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004285 if (flags & AT_EMPTY_PATH) {
4286 if (!capable(CAP_DAC_READ_SEARCH))
4287 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304288 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004289 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004290
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304291 if (flags & AT_SYMLINK_FOLLOW)
4292 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004293retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304294 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004296 return error;
4297
Jeff Layton442e31c2012-12-20 16:15:38 -05004298 new_dentry = user_path_create(newdfd, newname, &new_path,
4299 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004301 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004302 goto out;
4303
4304 error = -EXDEV;
4305 if (old_path.mnt != new_path.mnt)
4306 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004307 error = may_linkat(&old_path);
4308 if (unlikely(error))
4309 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004310 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004311 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004312 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004313 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004314out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004315 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004316 if (delegated_inode) {
4317 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004318 if (!error) {
4319 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004320 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004321 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004322 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004323 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004324 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004325 how |= LOOKUP_REVAL;
4326 goto retry;
4327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328out:
Al Viro2d8f3032008-07-22 09:59:21 -04004329 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330
4331 return error;
4332}
4333
Heiko Carstens3480b252009-01-14 14:14:16 +01004334SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004335{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004336 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004337}
4338
Miklos Szeredibc270272014-04-01 17:08:42 +02004339/**
4340 * vfs_rename - rename a filesystem object
4341 * @old_dir: parent of source
4342 * @old_dentry: source
4343 * @new_dir: parent of destination
4344 * @new_dentry: destination
4345 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004346 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004347 *
4348 * The caller must hold multiple mutexes--see lock_rename()).
4349 *
4350 * If vfs_rename discovers a delegation in need of breaking at either
4351 * the source or destination, it will return -EWOULDBLOCK and return a
4352 * reference to the inode in delegated_inode. The caller should then
4353 * break the delegation and retry. Because breaking a delegation may
4354 * take a long time, the caller should drop all locks before doing
4355 * so.
4356 *
4357 * Alternatively, a caller may pass NULL for delegated_inode. This may
4358 * be appropriate for callers that expect the underlying filesystem not
4359 * to be NFS exported.
4360 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 * The worst of all namespace operations - renaming directory. "Perverted"
4362 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4363 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004364 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 * b) race potential - two innocent renames can create a loop together.
4366 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004367 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004369 * c) we have to lock _four_ objects - parents and victim (if it exists),
4370 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004371 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 * whether the target exists). Solution: try to be smart with locking
4373 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004374 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004375 * move will be locked. Thus we can rank directories by the tree
4376 * (ancestors first) and rank all non-directories after them.
4377 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004378 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 * HOWEVER, it relies on the assumption that any object with ->lookup()
4380 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4381 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004382 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004383 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004385 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 * locking].
4387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004389 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004390 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391{
4392 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004393 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004394 struct inode *source = old_dentry->d_inode;
4395 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004396 bool new_is_dir = false;
4397 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viroad25f112017-07-07 14:51:19 -04004398 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399
Miklos Szeredi9409e222016-05-11 01:16:37 +02004400 /*
4401 * Check source == target.
4402 * On overlayfs need to look at underlying inodes.
4403 */
Miklos Szeredi2d902672016-06-30 08:53:27 +02004404 if (d_real_inode(old_dentry) == d_real_inode(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004405 return 0;
4406
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 error = may_delete(old_dir, old_dentry, is_dir);
4408 if (error)
4409 return error;
4410
Miklos Szeredida1ce062014-04-01 17:08:43 +02004411 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004412 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004413 } else {
4414 new_is_dir = d_is_dir(new_dentry);
4415
4416 if (!(flags & RENAME_EXCHANGE))
4417 error = may_delete(new_dir, new_dentry, is_dir);
4418 else
4419 error = may_delete(new_dir, new_dentry, new_is_dir);
4420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 if (error)
4422 return error;
4423
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004424 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 return -EPERM;
4426
Miklos Szeredibc270272014-04-01 17:08:42 +02004427 /*
4428 * If we are going to change the parent - check write permissions,
4429 * we'll need to flip '..'.
4430 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004431 if (new_dir != old_dir) {
4432 if (is_dir) {
4433 error = inode_permission(source, MAY_WRITE);
4434 if (error)
4435 return error;
4436 }
4437 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4438 error = inode_permission(target, MAY_WRITE);
4439 if (error)
4440 return error;
4441 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004442 }
Robert Love0eeca282005-07-12 17:06:03 -04004443
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004444 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4445 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004446 if (error)
4447 return error;
4448
Al Viroad25f112017-07-07 14:51:19 -04004449 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004450 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004451 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004452 lock_two_nondirectories(source, target);
4453 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004454 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004455
4456 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004457 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004458 goto out;
4459
Miklos Szeredida1ce062014-04-01 17:08:43 +02004460 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004461 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004462 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004463 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004464 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4465 old_dir->i_nlink >= max_links)
4466 goto out;
4467 }
4468 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4469 shrink_dcache_parent(new_dentry);
4470 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004471 error = try_break_deleg(source, delegated_inode);
4472 if (error)
4473 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004474 }
4475 if (target && !new_is_dir) {
4476 error = try_break_deleg(target, delegated_inode);
4477 if (error)
4478 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004479 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004480 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004481 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004482 if (error)
4483 goto out;
4484
Miklos Szeredida1ce062014-04-01 17:08:43 +02004485 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004486 if (is_dir)
4487 target->i_flags |= S_DEAD;
4488 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004489 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004490 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004491 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4492 if (!(flags & RENAME_EXCHANGE))
4493 d_move(old_dentry, new_dentry);
4494 else
4495 d_exchange(old_dentry, new_dentry);
4496 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004497out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004498 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004499 unlock_two_nondirectories(source, target);
4500 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004501 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004502 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004503 if (!error) {
Al Viroad25f112017-07-07 14:51:19 -04004504 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004505 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4506 if (flags & RENAME_EXCHANGE) {
4507 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4508 new_is_dir, NULL, new_dentry);
4509 }
4510 }
Al Viroad25f112017-07-07 14:51:19 -04004511 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return error;
4514}
Al Viro4d359502014-03-14 12:20:17 -04004515EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004517SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4518 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
Al Viro2ad94ae2008-07-21 09:32:51 -04004520 struct dentry *old_dentry, *new_dentry;
4521 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004522 struct path old_path, new_path;
4523 struct qstr old_last, new_last;
4524 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004525 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004526 struct filename *from;
4527 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004528 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004529 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004530 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004531
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004532 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004533 return -EINVAL;
4534
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004535 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4536 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004537 return -EINVAL;
4538
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004539 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4540 return -EPERM;
4541
Al Virof5beed72015-04-30 16:09:11 -04004542 if (flags & RENAME_EXCHANGE)
4543 target_flags = 0;
4544
Jeff Laytonc6a94282012-12-11 12:10:10 -05004545retry:
Al Virof5beed72015-04-30 16:09:11 -04004546 from = user_path_parent(olddfd, oldname,
4547 &old_path, &old_last, &old_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004548 if (IS_ERR(from)) {
4549 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004551 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Al Virof5beed72015-04-30 16:09:11 -04004553 to = user_path_parent(newdfd, newname,
4554 &new_path, &new_last, &new_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004555 if (IS_ERR(to)) {
4556 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559
4560 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004561 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 goto exit2;
4563
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004565 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 goto exit2;
4567
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004568 if (flags & RENAME_NOREPLACE)
4569 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004570 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 goto exit2;
4572
Al Virof5beed72015-04-30 16:09:11 -04004573 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004574 if (error)
4575 goto exit2;
4576
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004577retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004578 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Al Virof5beed72015-04-30 16:09:11 -04004580 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 error = PTR_ERR(old_dentry);
4582 if (IS_ERR(old_dentry))
4583 goto exit3;
4584 /* source must exist */
4585 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004586 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004588 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 error = PTR_ERR(new_dentry);
4590 if (IS_ERR(new_dentry))
4591 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004592 error = -EEXIST;
4593 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4594 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004595 if (flags & RENAME_EXCHANGE) {
4596 error = -ENOENT;
4597 if (d_is_negative(new_dentry))
4598 goto exit5;
4599
4600 if (!d_is_dir(new_dentry)) {
4601 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004602 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004603 goto exit5;
4604 }
4605 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004606 /* unless the source is a directory trailing slashes give -ENOTDIR */
4607 if (!d_is_dir(old_dentry)) {
4608 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004609 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004610 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004611 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004612 goto exit5;
4613 }
4614 /* source should not be ancestor of target */
4615 error = -EINVAL;
4616 if (old_dentry == trap)
4617 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004619 if (!(flags & RENAME_EXCHANGE))
4620 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 if (new_dentry == trap)
4622 goto exit5;
4623
Al Virof5beed72015-04-30 16:09:11 -04004624 error = security_path_rename(&old_path, old_dentry,
4625 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004626 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004627 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004628 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4629 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004630 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631exit5:
4632 dput(new_dentry);
4633exit4:
4634 dput(old_dentry);
4635exit3:
Al Virof5beed72015-04-30 16:09:11 -04004636 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004637 if (delegated_inode) {
4638 error = break_deleg_wait(&delegated_inode);
4639 if (!error)
4640 goto retry_deleg;
4641 }
Al Virof5beed72015-04-30 16:09:11 -04004642 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004644 if (retry_estale(error, lookup_flags))
4645 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004646 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004647 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648exit1:
Al Virof5beed72015-04-30 16:09:11 -04004649 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004651 if (should_retry) {
4652 should_retry = false;
4653 lookup_flags |= LOOKUP_REVAL;
4654 goto retry;
4655 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004656exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 return error;
4658}
4659
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004660SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4661 int, newdfd, const char __user *, newname)
4662{
4663 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4664}
4665
Heiko Carstensa26eab22009-01-14 14:14:17 +01004666SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004667{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004668 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004669}
4670
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004671int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4672{
4673 int error = may_create(dir, dentry);
4674 if (error)
4675 return error;
4676
4677 if (!dir->i_op->mknod)
4678 return -EPERM;
4679
4680 return dir->i_op->mknod(dir, dentry,
4681 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4682}
4683EXPORT_SYMBOL(vfs_whiteout);
4684
Al Viro5d826c82014-03-14 13:42:45 -04004685int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686{
Al Viro5d826c82014-03-14 13:42:45 -04004687 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 if (IS_ERR(link))
4689 goto out;
4690
4691 len = strlen(link);
4692 if (len > (unsigned) buflen)
4693 len = buflen;
4694 if (copy_to_user(buffer, link, len))
4695 len = -EFAULT;
4696out:
4697 return len;
4698}
4699
4700/*
4701 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
Al Viro6b255392015-11-17 10:20:54 -05004702 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4703 * for any given inode is up to filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704 */
4705int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4706{
Al Virofceef392015-12-29 15:58:39 -05004707 DEFINE_DELAYED_CALL(done);
Al Viro5f2c4172015-05-07 11:14:26 -04004708 struct inode *inode = d_inode(dentry);
4709 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004710 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004711
Al Virod4dee482015-04-30 20:08:02 -04004712 if (!link) {
Al Virofceef392015-12-29 15:58:39 -05004713 link = inode->i_op->get_link(dentry, inode, &done);
Al Virod4dee482015-04-30 20:08:02 -04004714 if (IS_ERR(link))
4715 return PTR_ERR(link);
4716 }
Al Viro680baac2015-05-02 13:32:22 -04004717 res = readlink_copy(buffer, buflen, link);
Al Virofceef392015-12-29 15:58:39 -05004718 do_delayed_call(&done);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004719 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
Al Viro4d359502014-03-14 12:20:17 -04004721EXPORT_SYMBOL(generic_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
Miklos Szeredid60874c2016-10-04 14:40:45 +02004723/**
4724 * vfs_get_link - get symlink body
4725 * @dentry: dentry on which to get symbolic link
4726 * @done: caller needs to free returned data with this
4727 *
4728 * Calls security hook and i_op->get_link() on the supplied inode.
4729 *
4730 * It does not touch atime. That's up to the caller if necessary.
4731 *
4732 * Does not work on "special" symlinks like /proc/$$/fd/N
4733 */
4734const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4735{
4736 const char *res = ERR_PTR(-EINVAL);
4737 struct inode *inode = d_inode(dentry);
4738
4739 if (d_is_symlink(dentry)) {
4740 res = ERR_PTR(security_inode_readlink(dentry));
4741 if (!res)
4742 res = inode->i_op->get_link(dentry, inode, done);
4743 }
4744 return res;
4745}
4746EXPORT_SYMBOL(vfs_get_link);
4747
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004749const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004750 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004752 char *kaddr;
4753 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004754 struct address_space *mapping = inode->i_mapping;
4755
Al Virod3883d42015-11-17 10:41:04 -05004756 if (!dentry) {
4757 page = find_get_page(mapping, 0);
4758 if (!page)
4759 return ERR_PTR(-ECHILD);
4760 if (!PageUptodate(page)) {
4761 put_page(page);
4762 return ERR_PTR(-ECHILD);
4763 }
4764 } else {
4765 page = read_mapping_page(mapping, 0, NULL);
4766 if (IS_ERR(page))
4767 return (char*)page;
4768 }
Al Virofceef392015-12-29 15:58:39 -05004769 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004770 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4771 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004772 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004773 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774}
4775
Al Viro6b255392015-11-17 10:20:54 -05004776EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Al Virofceef392015-12-29 15:58:39 -05004778void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779{
Al Virofceef392015-12-29 15:58:39 -05004780 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781}
Al Viro4d359502014-03-14 12:20:17 -04004782EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783
Al Viroaa80dea2015-11-16 18:26:34 -05004784int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4785{
Al Virofceef392015-12-29 15:58:39 -05004786 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004787 int res = readlink_copy(buffer, buflen,
4788 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004789 &done));
4790 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004791 return res;
4792}
4793EXPORT_SYMBOL(page_readlink);
4794
Nick Piggin54566b22009-01-04 12:00:53 -08004795/*
4796 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4797 */
4798int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799{
4800 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004801 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004802 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004803 int err;
Nick Piggin54566b22009-01-04 12:00:53 -08004804 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4805 if (nofs)
4806 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
NeilBrown7e53cac2006-03-25 03:07:57 -08004808retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004809 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004810 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004812 goto fail;
4813
Al Viro21fc61c2015-11-17 01:07:57 -05004814 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004815
4816 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4817 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818 if (err < 0)
4819 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004820 if (err < len-1)
4821 goto retry;
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 mark_inode_dirty(inode);
4824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825fail:
4826 return err;
4827}
Al Viro4d359502014-03-14 12:20:17 -04004828EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004830int page_symlink(struct inode *inode, const char *symname, int len)
4831{
4832 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004833 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004834}
Al Viro4d359502014-03-14 12:20:17 -04004835EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004836
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004837const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05004839 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841EXPORT_SYMBOL(page_symlink_inode_operations);