blob: 117b118853f26dc92f3db11305b29aead903e59a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/namei.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 */
7
8/*
9 * Some corrections by tytso.
10 */
11
12/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
13 * lookup logic.
14 */
15/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
16 */
17
18#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050019#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070020#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/slab.h>
22#include <linux/fs.h>
23#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040025#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/personality.h>
27#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050028#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/syscalls.h>
30#include <linux/mount.h>
31#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080032#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070033#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080034#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070035#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040036#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070037#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070038#include <linux/hash.h>
George Spelvin2a18da7a2016-05-23 07:43:58 -040039#include <linux/bitops.h>
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -050040#include <linux/init_task.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080041#include <linux/uaccess.h>
Rasmus Villemoes1c9498432018-03-01 00:19:21 +010042#include <linux/build_bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Eric Parise81e3f42009-12-04 15:47:36 -050044#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050045#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050046
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/* [Feb-1997 T. Schoebel-Theuer]
48 * Fundamental changes in the pathname lookup mechanisms (namei)
49 * were necessary because of omirr. The reason is that omirr needs
50 * to know the _real_ pathname, not the user-supplied one, in case
51 * of symlinks (and also when transname replacements occur).
52 *
53 * The new code replaces the old recursive symlink resolution with
54 * an iterative one (in case of non-nested symlink chains). It does
55 * this with calls to <fs>_follow_link().
56 * As a side effect, dir_namei(), _namei() and follow_link() are now
57 * replaced with a single function lookup_dentry() that can handle all
58 * the special cases of the former code.
59 *
60 * With the new dcache, the pathname is stored at each inode, at least as
61 * long as the refcount of the inode is positive. As a side effect, the
62 * size of the dcache depends on the inode cache and thus is dynamic.
63 *
64 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
65 * resolution to correspond with current state of the code.
66 *
67 * Note that the symlink resolution is not *completely* iterative.
68 * There is still a significant amount of tail- and mid- recursion in
69 * the algorithm. Also, note that <fs>_readlink() is not used in
70 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
71 * may return different results than <fs>_follow_link(). Many virtual
72 * filesystems (including /proc) exhibit this behavior.
73 */
74
75/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
76 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
77 * and the name already exists in form of a symlink, try to create the new
78 * name indicated by the symlink. The old code always complained that the
79 * name already exists, due to not following the symlink even if its target
80 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030081 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 * I don't know which semantics is the right one, since I have no access
84 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
85 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
86 * "old" one. Personally, I think the new semantics is much more logical.
87 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
88 * file does succeed in both HP-UX and SunOs, but not in Solaris
89 * and in the old Linux semantics.
90 */
91
92/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
93 * semantics. See the comments in "open_namei" and "do_link" below.
94 *
95 * [10-Sep-98 Alan Modra] Another symlink change.
96 */
97
98/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
99 * inside the path - always follow.
100 * in the last component in creation/removal/renaming - never follow.
101 * if LOOKUP_FOLLOW passed - follow.
102 * if the pathname has trailing slashes - follow.
103 * otherwise - don't follow.
104 * (applied in that order).
105 *
106 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
107 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
108 * During the 2.4 we need to fix the userland stuff depending on it -
109 * hopefully we will be able to get rid of that wart in 2.5. So far only
110 * XEmacs seems to be relying on it...
111 */
112/*
113 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800114 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * any extra contention...
116 */
117
118/* In order to reduce some races, while at the same time doing additional
119 * checking and hopefully speeding things up, we copy filenames to the
120 * kernel data space before using them..
121 *
122 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
123 * PATH_MAX includes the nul terminator --RR.
124 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400125
Al Virofd2f7cb2015-02-22 20:07:13 -0500126#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400127
David Drysdale51f39a12014-12-12 16:57:29 -0800128struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400129getname_flags(const char __user *filename, int flags, int *empty)
130{
Al Viro94b5d262015-02-22 19:38:03 -0500131 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400132 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500133 int len;
Rasmus Villemoes1c9498432018-03-01 00:19:21 +0100134 BUILD_BUG_ON(offsetof(struct filename, iname) % sizeof(long) != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Jeff Layton7ac86262012-10-10 15:25:28 -0400136 result = audit_reusename(filename);
137 if (result)
138 return result;
139
Jeff Layton7950e382012-10-10 16:43:13 -0400140 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700141 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500142 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jeff Layton7950e382012-10-10 16:43:13 -0400144 /*
145 * First, try to embed the struct filename inside the names_cache
146 * allocation
147 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500148 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400149 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400150
Al Viro94b5d262015-02-22 19:38:03 -0500151 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400152 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500153 __putname(result);
154 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400155 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700156
Jeff Layton7950e382012-10-10 16:43:13 -0400157 /*
158 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
159 * separate struct filename so we can dedicate the entire
160 * names_cache allocation for the pathname, and re-do the copy from
161 * userland.
162 */
Al Viro94b5d262015-02-22 19:38:03 -0500163 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500164 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400165 kname = (char *)result;
166
Al Virofd2f7cb2015-02-22 20:07:13 -0500167 /*
168 * size is chosen that way we to guarantee that
169 * result->iname[0] is within the same object and that
170 * kname can't be equal to result->iname, no matter what.
171 */
172 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500173 if (unlikely(!result)) {
174 __putname(kname);
175 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400176 }
177 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500178 len = strncpy_from_user(kname, filename, PATH_MAX);
179 if (unlikely(len < 0)) {
180 __putname(kname);
181 kfree(result);
182 return ERR_PTR(len);
183 }
184 if (unlikely(len == PATH_MAX)) {
185 __putname(kname);
186 kfree(result);
187 return ERR_PTR(-ENAMETOOLONG);
188 }
Jeff Layton7950e382012-10-10 16:43:13 -0400189 }
190
Al Viro94b5d262015-02-22 19:38:03 -0500191 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700192 /* The empty path is special. */
193 if (unlikely(!len)) {
194 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500195 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500196 if (!(flags & LOOKUP_EMPTY)) {
197 putname(result);
198 return ERR_PTR(-ENOENT);
199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700201
Jeff Layton7950e382012-10-10 16:43:13 -0400202 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800203 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400204 audit_getname(result);
205 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
Jeff Layton91a27b22012-10-10 15:25:28 -0400208struct filename *
209getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400210{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700211 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400212}
213
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800214struct filename *
215getname_kernel(const char * filename)
216{
217 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500218 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800219
220 result = __getname();
221 if (unlikely(!result))
222 return ERR_PTR(-ENOMEM);
223
Paul Moore08518542015-01-21 23:59:56 -0500224 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500225 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500226 } else if (len <= PATH_MAX) {
Al Viro30ce4d12018-04-08 11:57:10 -0400227 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500228 struct filename *tmp;
229
Al Viro30ce4d12018-04-08 11:57:10 -0400230 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500231 if (unlikely(!tmp)) {
232 __putname(result);
233 return ERR_PTR(-ENOMEM);
234 }
235 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500236 result = tmp;
237 } else {
238 __putname(result);
239 return ERR_PTR(-ENAMETOOLONG);
240 }
241 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800242 result->uptr = NULL;
243 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500244 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500245 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800246
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800247 return result;
248}
249
Jeff Layton91a27b22012-10-10 15:25:28 -0400250void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
Paul Moore55422d02015-01-22 00:00:23 -0500252 BUG_ON(name->refcnt <= 0);
253
254 if (--name->refcnt > 0)
255 return;
256
Al Virofd2f7cb2015-02-22 20:07:13 -0500257 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500258 __putname(name->name);
259 kfree(name);
260 } else
261 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Linus Torvaldse77819e2011-07-22 19:30:19 -0700264static int check_acl(struct inode *inode, int mask)
265{
Linus Torvalds84635d62011-07-25 22:47:03 -0700266#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700267 struct posix_acl *acl;
268
Linus Torvaldse77819e2011-07-22 19:30:19 -0700269 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400270 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
271 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700272 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400273 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100274 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400275 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300276 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700277 }
278
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800279 acl = get_acl(inode, ACL_TYPE_ACCESS);
280 if (IS_ERR(acl))
281 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700282 if (acl) {
283 int error = posix_acl_permission(inode, acl, mask);
284 posix_acl_release(acl);
285 return error;
286 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700287#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700288
289 return -EAGAIN;
290}
291
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700292/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530293 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700294 */
Al Viro7e401452011-06-20 19:12:17 -0400295static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700296{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700297 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700298
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800299 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700300 mode >>= 6;
301 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700302 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400303 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100304 if (error != -EAGAIN)
305 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700306 }
307
308 if (in_group_p(inode->i_gid))
309 mode >>= 3;
310 }
311
312 /*
313 * If the DACs are ok we don't need any capability check.
314 */
Al Viro9c2c7032011-06-20 19:06:22 -0400315 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700316 return 0;
317 return -EACCES;
318}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100321 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530323 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 *
325 * Used to check for read/write/execute permissions on a file.
326 * We use "fsuid" for this, letting us set arbitrary permissions
327 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100328 * are used for other things.
329 *
330 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
331 * request cannot be satisfied (eg. requires blocking or too much complexity).
332 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
Al Viro2830ba72011-06-20 19:16:29 -0400334int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700336 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530339 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 */
Al Viro7e401452011-06-20 19:12:17 -0400341 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700342 if (ret != -EACCES)
343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Al Virod594e7e2011-06-20 19:55:42 -0400345 if (S_ISDIR(inode->i_mode)) {
346 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400347 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700348 if (capable_wrt_inode_uidgid(inode,
349 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400350 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700351 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500353 return -EACCES;
354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
356 /*
357 * Searching includes executable on directories, else just read.
358 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600359 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400360 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700361 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500363 /*
364 * Read/write DACs are always overridable.
365 * Executable DACs are overridable when there is
366 * at least one exec bit set.
367 */
368 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
369 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
370 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 return -EACCES;
373}
Al Viro4d359502014-03-14 12:20:17 -0400374EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700376/*
377 * We _really_ want to just do "generic_permission()" without
378 * even looking at the inode->i_op values. So we keep a cache
379 * flag in inode->i_opflags, that says "this has not special
380 * permission function, use the fast case".
381 */
382static inline int do_inode_permission(struct inode *inode, int mask)
383{
384 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
385 if (likely(inode->i_op->permission))
386 return inode->i_op->permission(inode, mask);
387
388 /* This gets set once for the inode lifetime */
389 spin_lock(&inode->i_lock);
390 inode->i_opflags |= IOP_FASTPERM;
391 spin_unlock(&inode->i_lock);
392 }
393 return generic_permission(inode, mask);
394}
395
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200396/**
David Howells0bdaea92012-06-25 12:55:46 +0100397 * sb_permission - Check superblock-level permissions
398 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700399 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100400 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
401 *
402 * Separate out file-system wide checks from inode-specific permission checks.
403 */
404static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
405{
406 if (unlikely(mask & MAY_WRITE)) {
407 umode_t mode = inode->i_mode;
408
409 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100410 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100411 return -EROFS;
412 }
413 return 0;
414}
415
416/**
417 * inode_permission - Check for access rights to a given inode
418 * @inode: Inode to check permission on
419 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
420 *
421 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
422 * this, letting us set arbitrary permissions for filesystem access without
423 * changing the "normal" UIDs which are used for other things.
424 *
425 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
426 */
427int inode_permission(struct inode *inode, int mask)
428{
429 int retval;
430
431 retval = sb_permission(inode->i_sb, inode, mask);
432 if (retval)
433 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800434
435 if (unlikely(mask & MAY_WRITE)) {
436 /*
437 * Nobody gets write access to an immutable file.
438 */
439 if (IS_IMMUTABLE(inode))
440 return -EPERM;
441
442 /*
443 * Updating mtime will likely cause i_uid and i_gid to be
444 * written back improperly if their true value is unknown
445 * to the vfs.
446 */
447 if (HAS_UNMAPPED_ID(inode))
448 return -EACCES;
449 }
450
451 retval = do_inode_permission(inode, mask);
452 if (retval)
453 return retval;
454
455 retval = devcgroup_inode_permission(inode, mask);
456 if (retval)
457 return retval;
458
459 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100460}
Al Viro4d359502014-03-14 12:20:17 -0400461EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100462
463/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800464 * path_get - get a reference to a path
465 * @path: path to get the reference to
466 *
467 * Given a path increment the reference count to the dentry and the vfsmount.
468 */
Al Virodcf787f2013-03-01 23:51:07 -0500469void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800470{
471 mntget(path->mnt);
472 dget(path->dentry);
473}
474EXPORT_SYMBOL(path_get);
475
476/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800477 * path_put - put a reference to a path
478 * @path: path to put the reference to
479 *
480 * Given a path decrement the reference count to the dentry and the vfsmount.
481 */
Al Virodcf787f2013-03-01 23:51:07 -0500482void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Jan Blunck1d957f92008-02-14 19:34:35 -0800484 dput(path->dentry);
485 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
Jan Blunck1d957f92008-02-14 19:34:35 -0800487EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Al Viro894bc8c2015-05-02 07:16:16 -0400489#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400490struct nameidata {
491 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400492 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400493 struct path root;
494 struct inode *inode; /* path.dentry.d_inode */
495 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400496 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400497 int last_type;
498 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100499 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400500 struct saved {
501 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500502 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400503 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400504 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400505 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400506 struct filename *name;
507 struct nameidata *saved;
Al Virofceef392015-12-29 15:58:39 -0500508 struct inode *link_inode;
Al Viro9883d182015-05-13 07:28:08 -0400509 unsigned root_seq;
510 int dfd;
Kees Cook3859a272016-10-28 01:22:25 -0700511} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400512
Al Viro9883d182015-05-13 07:28:08 -0400513static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400514{
NeilBrown756daf22015-03-23 13:37:38 +1100515 struct nameidata *old = current->nameidata;
516 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400517 p->dfd = dfd;
518 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100519 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400520 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100521 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400522}
523
Al Viro9883d182015-05-13 07:28:08 -0400524static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400525{
Al Viro9883d182015-05-13 07:28:08 -0400526 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100527
528 current->nameidata = old;
529 if (old)
530 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500531 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100532 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400533}
534
535static int __nd_alloc_stack(struct nameidata *nd)
536{
Al Virobc40aee2015-05-09 13:04:24 -0400537 struct saved *p;
538
539 if (nd->flags & LOOKUP_RCU) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700540 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
Al Virobc40aee2015-05-09 13:04:24 -0400541 GFP_ATOMIC);
542 if (unlikely(!p))
543 return -ECHILD;
544 } else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700545 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400546 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400547 if (unlikely(!p))
548 return -ENOMEM;
549 }
Al Viro894bc8c2015-05-02 07:16:16 -0400550 memcpy(p, nd->internal, sizeof(nd->internal));
551 nd->stack = p;
552 return 0;
553}
554
Eric W. Biederman397d4252015-08-15 20:27:13 -0500555/**
556 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
557 * @path: nameidate to verify
558 *
559 * Rename can sometimes move a file or directory outside of a bind
560 * mount, path_connected allows those cases to be detected.
561 */
562static bool path_connected(const struct path *path)
563{
564 struct vfsmount *mnt = path->mnt;
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500565 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500566
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500567 /* Bind mounts and multi-root filesystems can have disconnected paths */
568 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
Eric W. Biederman397d4252015-08-15 20:27:13 -0500569 return true;
570
571 return is_subdir(path->dentry, mnt->mnt_root);
572}
573
Al Viro894bc8c2015-05-02 07:16:16 -0400574static inline int nd_alloc_stack(struct nameidata *nd)
575{
Al Viroda4e0be2015-05-03 20:52:15 -0400576 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400577 return 0;
578 if (likely(nd->stack != nd->internal))
579 return 0;
580 return __nd_alloc_stack(nd);
581}
582
Al Viro79733872015-05-09 12:55:43 -0400583static void drop_links(struct nameidata *nd)
584{
585 int i = nd->depth;
586 while (i--) {
587 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500588 do_delayed_call(&last->done);
589 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400590 }
591}
592
593static void terminate_walk(struct nameidata *nd)
594{
595 drop_links(nd);
596 if (!(nd->flags & LOOKUP_RCU)) {
597 int i;
598 path_put(&nd->path);
599 for (i = 0; i < nd->depth; i++)
600 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400601 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
602 path_put(&nd->root);
603 nd->root.mnt = NULL;
604 }
Al Viro79733872015-05-09 12:55:43 -0400605 } else {
606 nd->flags &= ~LOOKUP_RCU;
607 if (!(nd->flags & LOOKUP_ROOT))
608 nd->root.mnt = NULL;
609 rcu_read_unlock();
610 }
611 nd->depth = 0;
612}
613
614/* path_put is needed afterwards regardless of success or failure */
615static bool legitimize_path(struct nameidata *nd,
616 struct path *path, unsigned seq)
617{
618 int res = __legitimize_mnt(path->mnt, nd->m_seq);
619 if (unlikely(res)) {
620 if (res > 0)
621 path->mnt = NULL;
622 path->dentry = NULL;
623 return false;
624 }
625 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
626 path->dentry = NULL;
627 return false;
628 }
629 return !read_seqcount_retry(&path->dentry->d_seq, seq);
630}
631
632static bool legitimize_links(struct nameidata *nd)
633{
634 int i;
635 for (i = 0; i < nd->depth; i++) {
636 struct saved *last = nd->stack + i;
637 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
638 drop_links(nd);
639 nd->depth = i + 1;
640 return false;
641 }
642 }
643 return true;
644}
645
Al Viro19660af2011-03-25 10:32:48 -0400646/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100647 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400648 * Documentation/filesystems/path-lookup.txt). In situations when we can't
649 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500650 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400651 * mode. Refcounts are grabbed at the last known good point before rcu-walk
652 * got stuck, so ref-walk may continue from there. If this is not successful
653 * (eg. a seqcount has changed), then failure is returned and it's up to caller
654 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100655 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100656
657/**
Al Viro19660af2011-03-25 10:32:48 -0400658 * unlazy_walk - try to switch to ref-walk mode.
659 * @nd: nameidata pathwalk data
Randy Dunlap39191622011-01-08 19:36:21 -0800660 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100661 *
Al Viro4675ac32017-01-09 22:29:15 -0500662 * unlazy_walk attempts to legitimize the current nd->path and nd->root
663 * for ref-walk mode.
664 * Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400665 * Nothing should touch nameidata between unlazy_walk() failure and
666 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100667 */
Al Viro4675ac32017-01-09 22:29:15 -0500668static int unlazy_walk(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100669{
Nick Piggin31e6b012011-01-07 17:49:52 +1100670 struct dentry *parent = nd->path.dentry;
671
672 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700673
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700674 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400675 if (unlikely(!legitimize_links(nd)))
676 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500677 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
678 goto out1;
679 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
680 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq)))
681 goto out;
682 }
683 rcu_read_unlock();
684 BUG_ON(nd->inode != parent->d_inode);
685 return 0;
686
687out2:
688 nd->path.mnt = NULL;
689 nd->path.dentry = NULL;
690out1:
691 if (!(nd->flags & LOOKUP_ROOT))
692 nd->root.mnt = NULL;
693out:
694 rcu_read_unlock();
695 return -ECHILD;
696}
697
698/**
699 * unlazy_child - try to switch to ref-walk mode.
700 * @nd: nameidata pathwalk data
701 * @dentry: child of nd->path.dentry
702 * @seq: seq number to check dentry against
703 * Returns: 0 on success, -ECHILD on failure
704 *
705 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
706 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
707 * @nd. Must be called from rcu-walk context.
708 * Nothing should touch nameidata between unlazy_child() failure and
709 * terminate_walk().
710 */
711static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
712{
713 BUG_ON(!(nd->flags & LOOKUP_RCU));
714
715 nd->flags &= ~LOOKUP_RCU;
716 if (unlikely(!legitimize_links(nd)))
717 goto out2;
Al Viro79733872015-05-09 12:55:43 -0400718 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
719 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500720 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro79733872015-05-09 12:55:43 -0400721 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400722
Linus Torvalds15570082013-09-02 11:38:06 -0700723 /*
Al Viro4675ac32017-01-09 22:29:15 -0500724 * We need to move both the parent and the dentry from the RCU domain
725 * to be properly refcounted. And the sequence number in the dentry
726 * validates *both* dentry counters, since we checked the sequence
727 * number of the parent after we got the child sequence number. So we
728 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700729 */
Al Viro4675ac32017-01-09 22:29:15 -0500730 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
731 goto out;
732 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) {
733 rcu_read_unlock();
734 dput(dentry);
735 goto drop_root_mnt;
Al Viro19660af2011-03-25 10:32:48 -0400736 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700737 /*
738 * Sequence counts matched. Now make sure that the root is
739 * still valid and get it if required.
740 */
741 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400742 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
743 rcu_read_unlock();
744 dput(dentry);
745 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400746 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100747 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100748
Al Viro8b61e742013-11-08 12:45:01 -0500749 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100750 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400751
Al Viro79733872015-05-09 12:55:43 -0400752out2:
753 nd->path.mnt = NULL;
754out1:
755 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700756out:
Al Viro8b61e742013-11-08 12:45:01 -0500757 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700758drop_root_mnt:
759 if (!(nd->flags & LOOKUP_ROOT))
760 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100761 return -ECHILD;
762}
763
Al Viro4ce16ef32012-06-10 16:10:59 -0400764static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100765{
Al Viroa89f8332017-01-09 22:25:28 -0500766 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
767 return dentry->d_op->d_revalidate(dentry, flags);
768 else
769 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100770}
771
Al Viro9f1fafe2011-03-25 11:00:12 -0400772/**
773 * complete_walk - successful completion of path walk
774 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500775 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400776 * If we had been in RCU mode, drop out of it and legitimize nd->path.
777 * Revalidate the final result, unless we'd already done that during
778 * the path walk or the filesystem doesn't ask for it. Return 0 on
779 * success, -error on failure. In case of failure caller does not
780 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500781 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400782static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500783{
Al Viro16c2cd72011-02-22 15:50:10 -0500784 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500785 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500786
Al Viro9f1fafe2011-03-25 11:00:12 -0400787 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400788 if (!(nd->flags & LOOKUP_ROOT))
789 nd->root.mnt = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500790 if (unlikely(unlazy_walk(nd)))
Al Viro48a066e2013-09-29 22:06:07 -0400791 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400792 }
793
Al Viro16c2cd72011-02-22 15:50:10 -0500794 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500795 return 0;
796
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500797 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500798 return 0;
799
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500800 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500801 if (status > 0)
802 return 0;
803
Al Viro16c2cd72011-02-22 15:50:10 -0500804 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500805 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500806
Jeff Layton39159de2009-12-07 12:01:50 -0500807 return status;
808}
809
Al Viro18d8c862015-05-12 16:32:34 -0400810static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400811{
Al Viro7bd88372014-09-13 21:55:46 -0400812 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100813
Al Viro9e6697e2015-12-05 20:07:21 -0500814 if (nd->flags & LOOKUP_RCU) {
815 unsigned seq;
816
817 do {
818 seq = read_seqcount_begin(&fs->seq);
819 nd->root = fs->root;
820 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
821 } while (read_seqcount_retry(&fs->seq, seq));
822 } else {
823 get_fs_root(fs, &nd->root);
824 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100825}
826
Jan Blunck1d957f92008-02-14 19:34:35 -0800827static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700828{
829 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800830 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700831 mntput(path->mnt);
832}
833
Nick Piggin7b9337a2011-01-14 08:42:43 +0000834static inline void path_to_nameidata(const struct path *path,
835 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700836{
Nick Piggin31e6b012011-01-07 17:49:52 +1100837 if (!(nd->flags & LOOKUP_RCU)) {
838 dput(nd->path.dentry);
839 if (nd->path.mnt != path->mnt)
840 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800841 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100842 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800843 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700844}
845
Al Viro248fb5b2015-12-05 20:51:58 -0500846static int nd_jump_root(struct nameidata *nd)
847{
848 if (nd->flags & LOOKUP_RCU) {
849 struct dentry *d;
850 nd->path = nd->root;
851 d = nd->path.dentry;
852 nd->inode = d->d_inode;
853 nd->seq = nd->root_seq;
854 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
855 return -ECHILD;
856 } else {
857 path_put(&nd->path);
858 nd->path = nd->root;
859 path_get(&nd->path);
860 nd->inode = nd->path.dentry->d_inode;
861 }
862 nd->flags |= LOOKUP_JUMPED;
863 return 0;
864}
865
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400866/*
Al Viro6b255392015-11-17 10:20:54 -0500867 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400868 * caller must have taken a reference to path beforehand.
869 */
Al Viro6e77137b2015-05-02 13:37:52 -0400870void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400871{
Al Viro6e77137b2015-05-02 13:37:52 -0400872 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400873 path_put(&nd->path);
874
875 nd->path = *path;
876 nd->inode = nd->path.dentry->d_inode;
877 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400878}
879
Al Virob9ff4422015-05-02 20:19:23 -0400880static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400881{
Al Viro21c30032015-05-03 21:06:24 -0400882 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -0500883 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -0400884 if (!(nd->flags & LOOKUP_RCU))
885 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400886}
887
Linus Torvalds561ec642012-10-26 10:05:07 -0700888int sysctl_protected_symlinks __read_mostly = 0;
889int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700890
891/**
892 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700893 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700894 *
895 * In the case of the sysctl_protected_symlinks sysctl being enabled,
896 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
897 * in a sticky world-writable directory. This is to protect privileged
898 * processes from failing races against path names that may change out
899 * from under them by way of other users creating malicious symlinks.
900 * It will permit symlinks to be followed only when outside a sticky
901 * world-writable directory, or when the uid of the symlink and follower
902 * match, or when the directory owner matches the symlink's owner.
903 *
904 * Returns 0 if following the symlink is allowed, -ve on error.
905 */
Al Virofec2fa22015-05-06 15:58:18 -0400906static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700907{
908 const struct inode *inode;
909 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500910 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700911
912 if (!sysctl_protected_symlinks)
913 return 0;
914
915 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500916 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700917 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700918 return 0;
919
920 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400921 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700922 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
923 return 0;
924
925 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500926 puid = parent->i_uid;
927 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700928 return 0;
929
Al Viro319565022015-05-07 20:37:40 -0400930 if (nd->flags & LOOKUP_RCU)
931 return -ECHILD;
932
Richard Guy Briggsea841ba2018-03-21 04:42:21 -0400933 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Richard Guy Briggs94b9d9b2018-03-21 04:42:20 -0400934 audit_log_link_denied("follow_link");
Kees Cook800179c2012-07-25 17:29:07 -0700935 return -EACCES;
936}
937
938/**
939 * safe_hardlink_source - Check for safe hardlink conditions
940 * @inode: the source inode to hardlink from
941 *
942 * Return false if at least one of the following conditions:
943 * - inode is not a regular file
944 * - inode is setuid
945 * - inode is setgid and group-exec
946 * - access failure for read and write
947 *
948 * Otherwise returns true.
949 */
950static bool safe_hardlink_source(struct inode *inode)
951{
952 umode_t mode = inode->i_mode;
953
954 /* Special files should not get pinned to the filesystem. */
955 if (!S_ISREG(mode))
956 return false;
957
958 /* Setuid files should not get pinned to the filesystem. */
959 if (mode & S_ISUID)
960 return false;
961
962 /* Executable setgid files should not get pinned to the filesystem. */
963 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
964 return false;
965
966 /* Hardlinking to unreadable or unwritable sources is dangerous. */
967 if (inode_permission(inode, MAY_READ | MAY_WRITE))
968 return false;
969
970 return true;
971}
972
973/**
974 * may_linkat - Check permissions for creating a hardlink
975 * @link: the source to hardlink from
976 *
977 * Block hardlink when all of:
978 * - sysctl_protected_hardlinks enabled
979 * - fsuid does not match inode
980 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200981 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700982 *
983 * Returns 0 if successful, -ve on error.
984 */
985static int may_linkat(struct path *link)
986{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -0500987 struct inode *inode = link->dentry->d_inode;
988
989 /* Inode writeback is not safe when the uid or gid are invalid. */
990 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
991 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -0700992
993 if (!sysctl_protected_hardlinks)
994 return 0;
995
Kees Cook800179c2012-07-25 17:29:07 -0700996 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
997 * otherwise, it must be a safe source.
998 */
Kees Cookcc658db2017-06-21 09:53:06 -0700999 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
Kees Cook800179c2012-07-25 17:29:07 -07001000 return 0;
1001
Richard Guy Briggs94b9d9b2018-03-21 04:42:20 -04001002 audit_log_link_denied("linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001003 return -EPERM;
1004}
1005
Al Viro3b2e7f72015-04-19 00:53:50 -04001006static __always_inline
1007const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001008{
Al Viroab104922015-05-10 11:50:01 -04001009 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001010 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -05001011 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001012 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001013 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001014
NeilBrown8fa9dd22015-03-23 13:37:40 +11001015 if (!(nd->flags & LOOKUP_RCU)) {
1016 touch_atime(&last->link);
1017 cond_resched();
Miklos Szeredi598e3c82016-09-16 12:44:20 +02001018 } else if (atime_needs_update_rcu(&last->link, inode)) {
Al Viro4675ac32017-01-09 22:29:15 -05001019 if (unlikely(unlazy_walk(nd)))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001020 return ERR_PTR(-ECHILD);
1021 touch_atime(&last->link);
1022 }
1023
NeilBrownbda0be72015-03-23 13:37:39 +11001024 error = security_inode_follow_link(dentry, inode,
1025 nd->flags & LOOKUP_RCU);
1026 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001027 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001028
Al Viro86acdca12009-12-22 23:45:11 -05001029 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001030 res = inode->i_link;
1031 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001032 const char * (*get)(struct dentry *, struct inode *,
1033 struct delayed_call *);
1034 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001035 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001036 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001037 if (res == ERR_PTR(-ECHILD)) {
Al Viro4675ac32017-01-09 22:29:15 -05001038 if (unlikely(unlazy_walk(nd)))
Al Viro6b255392015-11-17 10:20:54 -05001039 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001040 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001041 }
1042 } else {
Al Virofceef392015-12-29 15:58:39 -05001043 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001044 }
Al Virofceef392015-12-29 15:58:39 -05001045 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001046 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001047 }
Al Virofab51e82015-05-10 11:01:00 -04001048 if (*res == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05001049 if (!nd->root.mnt)
1050 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05001051 if (unlikely(nd_jump_root(nd)))
1052 return ERR_PTR(-ECHILD);
Al Virofab51e82015-05-10 11:01:00 -04001053 while (unlikely(*++res == '/'))
1054 ;
1055 }
1056 if (!*res)
1057 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001058 return res;
1059}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001060
David Howellsf015f1262012-06-25 12:55:28 +01001061/*
1062 * follow_up - Find the mountpoint of path's vfsmount
1063 *
1064 * Given a path, find the mountpoint of its source file system.
1065 * Replace @path with the path of the mountpoint in the parent mount.
1066 * Up is towards /.
1067 *
1068 * Return 1 if we went up a level and 0 if we were already at the
1069 * root.
1070 */
Al Virobab77eb2009-04-18 03:26:48 -04001071int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072{
Al Viro0714a532011-11-24 22:19:58 -05001073 struct mount *mnt = real_mount(path->mnt);
1074 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001076
Al Viro48a066e2013-09-29 22:06:07 -04001077 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001078 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001079 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001080 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 0;
1082 }
Al Viro0714a532011-11-24 22:19:58 -05001083 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001084 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001085 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001086 dput(path->dentry);
1087 path->dentry = mountpoint;
1088 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001089 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 1;
1091}
Al Viro4d359502014-03-14 12:20:17 -04001092EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001094/*
David Howells9875cf82011-01-14 18:45:21 +00001095 * Perform an automount
1096 * - return -EISDIR to tell follow_managed() to stop and return the path we
1097 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
NeilBrown756daf22015-03-23 13:37:38 +11001099static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001100 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001101{
David Howells9875cf82011-01-14 18:45:21 +00001102 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001103 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001104
David Howells9875cf82011-01-14 18:45:21 +00001105 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1106 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001107
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001108 /* We don't want to mount if someone's just doing a stat -
1109 * unless they're stat'ing a directory and appended a '/' to
1110 * the name.
1111 *
1112 * We do, however, want to mount if someone wants to open or
1113 * create a file of any type under the mountpoint, wants to
1114 * traverse through the mountpoint or wants to open the
1115 * mounted directory. Also, autofs may mark negative dentries
1116 * as being automount points. These will need the attentions
1117 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001118 */
NeilBrown756daf22015-03-23 13:37:38 +11001119 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001120 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1121 path->dentry->d_inode)
1122 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001123
NeilBrown756daf22015-03-23 13:37:38 +11001124 nd->total_link_count++;
1125 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001126 return -ELOOP;
1127
1128 mnt = path->dentry->d_op->d_automount(path);
1129 if (IS_ERR(mnt)) {
1130 /*
1131 * The filesystem is allowed to return -EISDIR here to indicate
1132 * it doesn't want to automount. For instance, autofs would do
1133 * this so that its userspace daemon can mount on this dentry.
1134 *
1135 * However, we can only permit this if it's a terminal point in
1136 * the path being looked up; if it wasn't then the remainder of
1137 * the path is inaccessible and we should say so.
1138 */
NeilBrown756daf22015-03-23 13:37:38 +11001139 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001140 return -EREMOTE;
1141 return PTR_ERR(mnt);
1142 }
David Howellsea5b7782011-01-14 19:10:03 +00001143
David Howells9875cf82011-01-14 18:45:21 +00001144 if (!mnt) /* mount collision */
1145 return 0;
1146
Al Viro8aef1882011-06-16 15:10:06 +01001147 if (!*need_mntput) {
1148 /* lock_mount() may release path->mnt on error */
1149 mntget(path->mnt);
1150 *need_mntput = true;
1151 }
Al Viro19a167a2011-01-17 01:35:23 -05001152 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001153
David Howellsea5b7782011-01-14 19:10:03 +00001154 switch (err) {
1155 case -EBUSY:
1156 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001157 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001158 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001159 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001160 path->mnt = mnt;
1161 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001162 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001163 default:
1164 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001165 }
Al Viro19a167a2011-01-17 01:35:23 -05001166
David Howells9875cf82011-01-14 18:45:21 +00001167}
1168
1169/*
1170 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001171 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001172 * - Flagged as mountpoint
1173 * - Flagged as automount point
1174 *
1175 * This may only be called in refwalk mode.
1176 *
1177 * Serialization is taken care of in namespace.c
1178 */
NeilBrown756daf22015-03-23 13:37:38 +11001179static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001180{
Al Viro8aef1882011-06-16 15:10:06 +01001181 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001182 unsigned managed;
1183 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001184 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001185
1186 /* Given that we're not holding a lock here, we retain the value in a
1187 * local variable for each dentry as we look at it so that we don't see
1188 * the components of that value change under us */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001189 while (managed = READ_ONCE(path->dentry->d_flags),
David Howells9875cf82011-01-14 18:45:21 +00001190 managed &= DCACHE_MANAGED_DENTRY,
1191 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001192 /* Allow the filesystem to manage the transit without i_mutex
1193 * being held. */
1194 if (managed & DCACHE_MANAGE_TRANSIT) {
1195 BUG_ON(!path->dentry->d_op);
1196 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001197 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001198 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001199 break;
David Howellscc53ce52011-01-14 18:45:26 +00001200 }
1201
David Howells9875cf82011-01-14 18:45:21 +00001202 /* Transit to a mounted filesystem. */
1203 if (managed & DCACHE_MOUNTED) {
1204 struct vfsmount *mounted = lookup_mnt(path);
1205 if (mounted) {
1206 dput(path->dentry);
1207 if (need_mntput)
1208 mntput(path->mnt);
1209 path->mnt = mounted;
1210 path->dentry = dget(mounted->mnt_root);
1211 need_mntput = true;
1212 continue;
1213 }
1214
1215 /* Something is mounted on this dentry in another
1216 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001217 * namespace got unmounted before lookup_mnt() could
1218 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001219 }
1220
1221 /* Handle an automount point */
1222 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001223 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001224 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001225 break;
David Howells9875cf82011-01-14 18:45:21 +00001226 continue;
1227 }
1228
1229 /* We didn't change the current path point */
1230 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Al Viro8aef1882011-06-16 15:10:06 +01001232
1233 if (need_mntput && path->mnt == mnt)
1234 mntput(path->mnt);
Al Viroe9742b52016-03-05 22:04:59 -05001235 if (ret == -EISDIR || !ret)
1236 ret = 1;
Al Viro84027522015-04-22 10:30:08 -04001237 if (need_mntput)
1238 nd->flags |= LOOKUP_JUMPED;
1239 if (unlikely(ret < 0))
1240 path_put_conditional(path, nd);
1241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
David Howellscc53ce52011-01-14 18:45:26 +00001244int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245{
1246 struct vfsmount *mounted;
1247
Al Viro1c755af2009-04-18 14:06:57 -04001248 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001250 dput(path->dentry);
1251 mntput(path->mnt);
1252 path->mnt = mounted;
1253 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return 1;
1255 }
1256 return 0;
1257}
Al Viro4d359502014-03-14 12:20:17 -04001258EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Ian Kentfb5f51c2016-11-24 08:03:41 +11001260static inline int managed_dentry_rcu(const struct path *path)
Ian Kent62a73752011-03-25 01:51:02 +08001261{
Ian Kentfb5f51c2016-11-24 08:03:41 +11001262 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1263 path->dentry->d_op->d_manage(path, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001264}
1265
David Howells9875cf82011-01-14 18:45:21 +00001266/*
Al Viro287548e2011-05-27 06:50:06 -04001267 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1268 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001269 */
1270static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001271 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001272{
Ian Kent62a73752011-03-25 01:51:02 +08001273 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001274 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001275 /*
1276 * Don't forget we might have a non-mountpoint managed dentry
1277 * that wants to block transit.
1278 */
Ian Kentfb5f51c2016-11-24 08:03:41 +11001279 switch (managed_dentry_rcu(path)) {
NeilBrownb8faf032014-08-04 17:06:29 +10001280 case -ECHILD:
1281 default:
David Howellsab909112011-01-14 18:46:51 +00001282 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001283 case -EISDIR:
1284 return true;
1285 case 0:
1286 break;
1287 }
Ian Kent62a73752011-03-25 01:51:02 +08001288
1289 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001290 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001291
Al Viro474279d2013-10-01 16:11:26 -04001292 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001293 if (!mounted)
1294 break;
Al Viroc7105362011-11-24 18:22:03 -05001295 path->mnt = &mounted->mnt;
1296 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001297 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001298 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001299 /*
1300 * Update the inode too. We don't need to re-check the
1301 * dentry sequence number here after this d_inode read,
1302 * because a mount-point is always pinned.
1303 */
1304 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001305 }
Al Virof5be3e29122014-09-13 21:50:45 -04001306 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001307 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001308}
1309
Nick Piggin31e6b012011-01-07 17:49:52 +11001310static int follow_dotdot_rcu(struct nameidata *nd)
1311{
Al Viro4023bfc2014-09-13 21:59:43 -04001312 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001313
David Howells9875cf82011-01-14 18:45:21 +00001314 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001315 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001316 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001317 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1318 struct dentry *old = nd->path.dentry;
1319 struct dentry *parent = old->d_parent;
1320 unsigned seq;
1321
Al Viro4023bfc2014-09-13 21:59:43 -04001322 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001323 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001324 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1325 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001326 nd->path.dentry = parent;
1327 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001328 if (unlikely(!path_connected(&nd->path)))
1329 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001330 break;
Al Viroaed434a2015-05-12 12:22:47 -04001331 } else {
1332 struct mount *mnt = real_mount(nd->path.mnt);
1333 struct mount *mparent = mnt->mnt_parent;
1334 struct dentry *mountpoint = mnt->mnt_mountpoint;
1335 struct inode *inode2 = mountpoint->d_inode;
1336 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1337 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1338 return -ECHILD;
1339 if (&mparent->mnt == nd->path.mnt)
1340 break;
1341 /* we know that mountpoint was pinned */
1342 nd->path.dentry = mountpoint;
1343 nd->path.mnt = &mparent->mnt;
1344 inode = inode2;
1345 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001346 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001347 }
Al Viroaed434a2015-05-12 12:22:47 -04001348 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001349 struct mount *mounted;
1350 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001351 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1352 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001353 if (!mounted)
1354 break;
1355 nd->path.mnt = &mounted->mnt;
1356 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001357 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001358 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001359 }
Al Viro4023bfc2014-09-13 21:59:43 -04001360 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001361 return 0;
1362}
1363
David Howells9875cf82011-01-14 18:45:21 +00001364/*
David Howellscc53ce52011-01-14 18:45:26 +00001365 * Follow down to the covering mount currently visible to userspace. At each
1366 * point, the filesystem owning that dentry may be queried as to whether the
1367 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001368 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001369int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001370{
1371 unsigned managed;
1372 int ret;
1373
Mark Rutland6aa7de02017-10-23 14:07:29 -07001374 while (managed = READ_ONCE(path->dentry->d_flags),
David Howellscc53ce52011-01-14 18:45:26 +00001375 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1376 /* Allow the filesystem to manage the transit without i_mutex
1377 * being held.
1378 *
1379 * We indicate to the filesystem if someone is trying to mount
1380 * something here. This gives autofs the chance to deny anyone
1381 * other than its daemon the right to mount on its
1382 * superstructure.
1383 *
1384 * The filesystem may sleep at this point.
1385 */
1386 if (managed & DCACHE_MANAGE_TRANSIT) {
1387 BUG_ON(!path->dentry->d_op);
1388 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001389 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001390 if (ret < 0)
1391 return ret == -EISDIR ? 0 : ret;
1392 }
1393
1394 /* Transit to a mounted filesystem. */
1395 if (managed & DCACHE_MOUNTED) {
1396 struct vfsmount *mounted = lookup_mnt(path);
1397 if (!mounted)
1398 break;
1399 dput(path->dentry);
1400 mntput(path->mnt);
1401 path->mnt = mounted;
1402 path->dentry = dget(mounted->mnt_root);
1403 continue;
1404 }
1405
1406 /* Don't handle automount points here */
1407 break;
1408 }
1409 return 0;
1410}
Al Viro4d359502014-03-14 12:20:17 -04001411EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001412
1413/*
David Howells9875cf82011-01-14 18:45:21 +00001414 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1415 */
1416static void follow_mount(struct path *path)
1417{
1418 while (d_mountpoint(path->dentry)) {
1419 struct vfsmount *mounted = lookup_mnt(path);
1420 if (!mounted)
1421 break;
1422 dput(path->dentry);
1423 mntput(path->mnt);
1424 path->mnt = mounted;
1425 path->dentry = dget(mounted->mnt_root);
1426 }
1427}
1428
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001429static int path_parent_directory(struct path *path)
1430{
1431 struct dentry *old = path->dentry;
1432 /* rare case of legitimate dget_parent()... */
1433 path->dentry = dget_parent(path->dentry);
1434 dput(old);
1435 if (unlikely(!path_connected(path)))
1436 return -ENOENT;
1437 return 0;
1438}
1439
Eric W. Biederman397d4252015-08-15 20:27:13 -05001440static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 while(1) {
Danilo Krummrich030c7e02018-04-23 10:30:59 +02001443 if (path_equal(&nd->path, &nd->root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 break;
Jan Blunck4ac91372008-02-14 19:34:32 -08001445 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001446 int ret = path_parent_directory(&nd->path);
1447 if (ret)
1448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 break;
1450 }
Al Viro3088dd72010-01-30 15:47:29 -05001451 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Al Viro79ed0222009-04-18 13:59:41 -04001454 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001455 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457}
1458
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001460 * This looks up the name in dcache and possibly revalidates the found dentry.
1461 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001462 */
Al Viroe3c13922016-03-06 14:03:27 -05001463static struct dentry *lookup_dcache(const struct qstr *name,
1464 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001465 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001466{
Al Viroa89f8332017-01-09 22:25:28 -05001467 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001468 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001469 int error = d_revalidate(dentry, flags);
1470 if (unlikely(error <= 0)) {
1471 if (!error)
1472 d_invalidate(dentry);
1473 dput(dentry);
1474 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001475 }
1476 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001477 return dentry;
1478}
1479
1480/*
Al Viroa03ece52018-03-08 11:00:45 -05001481 * Parent directory has inode locked exclusive. This is one
1482 * and only case when ->lookup() gets called on non in-lookup
1483 * dentries - as the matter of fact, this only gets called
1484 * when directory is guaranteed to have no in-lookup children
1485 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001486 */
Al Viroa03ece52018-03-08 11:00:45 -05001487static struct dentry *__lookup_hash(const struct qstr *name,
1488 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001489{
Al Viroa03ece52018-03-08 11:00:45 -05001490 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001491 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001492 struct inode *dir = base->d_inode;
1493
1494 if (dentry)
1495 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001496
1497 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001498 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001499 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001500
1501 dentry = d_alloc(base, name);
1502 if (unlikely(!dentry))
1503 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001504
Al Viro72bd8662012-06-10 17:17:17 -04001505 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001506 if (unlikely(old)) {
1507 dput(dentry);
1508 dentry = old;
1509 }
1510 return dentry;
1511}
1512
Al Viroe97cdc82013-01-24 18:16:00 -05001513static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001514 struct path *path, struct inode **inode,
1515 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jan Blunck4ac91372008-02-14 19:34:32 -08001517 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001518 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001519 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001520 int err;
1521
Al Viro3cac2602009-08-13 18:27:43 +04001522 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001523 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001524 * of a false negative due to a concurrent rename, the caller is
1525 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001526 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001527 if (nd->flags & LOOKUP_RCU) {
1528 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001529 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001530 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001531 if (unlikely(!dentry)) {
Al Viro4675ac32017-01-09 22:29:15 -05001532 if (unlazy_walk(nd))
Al Viro5d0f49c2016-03-05 21:32:53 -05001533 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001534 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001535 }
Al Viro5a18fff2011-03-11 04:44:53 -05001536
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001537 /*
1538 * This sequence count validates that the inode matches
1539 * the dentry name information from lookup.
1540 */
David Howells63afdfc2015-05-06 15:59:00 +01001541 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001542 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001543 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001544 return -ECHILD;
1545
1546 /*
1547 * This sequence count validates that the parent had no
1548 * changes while we did the lookup of the dentry above.
1549 *
1550 * The memory barrier in read_seqcount_begin of child is
1551 * enough, we can use __read_seqcount_retry here.
1552 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001553 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001554 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001555
Al Viro254cf582015-05-05 09:40:46 -04001556 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001557 status = d_revalidate(dentry, nd->flags);
Al Viro209a7fb2017-01-09 01:35:39 -05001558 if (likely(status > 0)) {
Al Viro5d0f49c2016-03-05 21:32:53 -05001559 /*
1560 * Note: do negative dentry check after revalidation in
1561 * case that drops it.
1562 */
1563 if (unlikely(negative))
1564 return -ENOENT;
1565 path->mnt = mnt;
1566 path->dentry = dentry;
1567 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001568 return 1;
Al Viro24643082011-02-15 01:26:22 -05001569 }
Al Viro4675ac32017-01-09 22:29:15 -05001570 if (unlazy_child(nd, dentry, seq))
Al Viro209a7fb2017-01-09 01:35:39 -05001571 return -ECHILD;
1572 if (unlikely(status == -ECHILD))
1573 /* we'd been told to redo it in non-rcu mode */
1574 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001575 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001576 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001577 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001578 return 0;
Al Viroa89f8332017-01-09 22:25:28 -05001579 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001580 }
Al Viro5a18fff2011-03-11 04:44:53 -05001581 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001582 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001583 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001584 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001585 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001586 }
Al Viro766c4cb2015-05-07 19:24:57 -04001587 if (unlikely(d_is_negative(dentry))) {
1588 dput(dentry);
1589 return -ENOENT;
1590 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001591
David Howells9875cf82011-01-14 18:45:21 +00001592 path->mnt = mnt;
1593 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001594 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001595 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001596 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001597 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001598}
1599
1600/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001601static struct dentry *__lookup_slow(const struct qstr *name,
1602 struct dentry *dir,
1603 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001604{
Al Viro88d83312018-04-06 16:43:47 -04001605 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001606 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001607 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001608
Al Viro19363862016-04-14 19:33:34 -04001609 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001610 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001611 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001612again:
Al Virod9171b92016-04-15 03:33:13 -04001613 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001614 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001615 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001616 if (unlikely(!d_in_lookup(dentry))) {
Al Viroa89f8332017-01-09 22:25:28 -05001617 if (!(flags & LOOKUP_NO_REVAL)) {
Al Viro949a8522016-03-06 14:20:52 -05001618 int error = d_revalidate(dentry, flags);
1619 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001620 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001621 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001622 dput(dentry);
1623 goto again;
1624 }
Al Viro949a8522016-03-06 14:20:52 -05001625 dput(dentry);
1626 dentry = ERR_PTR(error);
1627 }
1628 }
Al Viro94bdd652016-04-15 02:42:04 -04001629 } else {
1630 old = inode->i_op->lookup(inode, dentry, flags);
1631 d_lookup_done(dentry);
1632 if (unlikely(old)) {
1633 dput(dentry);
1634 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001635 }
1636 }
Al Viroe3c13922016-03-06 14:03:27 -05001637 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Al Viro88d83312018-04-06 16:43:47 -04001640static struct dentry *lookup_slow(const struct qstr *name,
1641 struct dentry *dir,
1642 unsigned int flags)
1643{
1644 struct inode *inode = dir->d_inode;
1645 struct dentry *res;
1646 inode_lock_shared(inode);
1647 res = __lookup_slow(name, dir, flags);
1648 inode_unlock_shared(inode);
1649 return res;
1650}
1651
Al Viro52094c82011-02-21 21:34:47 -05001652static inline int may_lookup(struct nameidata *nd)
1653{
1654 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001655 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001656 if (err != -ECHILD)
1657 return err;
Al Viro4675ac32017-01-09 22:29:15 -05001658 if (unlazy_walk(nd))
Al Viro52094c82011-02-21 21:34:47 -05001659 return -ECHILD;
1660 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001661 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001662}
1663
Al Viro9856fa12011-03-04 14:22:06 -05001664static inline int handle_dots(struct nameidata *nd, int type)
1665{
1666 if (type == LAST_DOTDOT) {
Al Viro9e6697e2015-12-05 20:07:21 -05001667 if (!nd->root.mnt)
1668 set_root(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001669 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001670 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001671 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001672 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001673 }
1674 return 0;
1675}
1676
Al Viro181548c2015-05-07 19:54:34 -04001677static int pick_link(struct nameidata *nd, struct path *link,
1678 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001679{
Al Viro626de992015-05-04 18:26:59 -04001680 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001681 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001682 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001683 path_to_nameidata(link, nd);
1684 return -ELOOP;
1685 }
Al Virobc40aee2015-05-09 13:04:24 -04001686 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001687 if (link->mnt == nd->path.mnt)
1688 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001689 }
Al Viro626de992015-05-04 18:26:59 -04001690 error = nd_alloc_stack(nd);
1691 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001692 if (error == -ECHILD) {
Al Viroad1633a2017-01-08 22:35:31 -05001693 if (unlikely(!legitimize_path(nd, link, seq))) {
1694 drop_links(nd);
1695 nd->depth = 0;
1696 nd->flags &= ~LOOKUP_RCU;
1697 nd->path.mnt = NULL;
1698 nd->path.dentry = NULL;
1699 if (!(nd->flags & LOOKUP_ROOT))
1700 nd->root.mnt = NULL;
1701 rcu_read_unlock();
Al Viro4675ac32017-01-09 22:29:15 -05001702 } else if (likely(unlazy_walk(nd)) == 0)
Al Viroad1633a2017-01-08 22:35:31 -05001703 error = nd_alloc_stack(nd);
Al Virobc40aee2015-05-09 13:04:24 -04001704 }
1705 if (error) {
1706 path_put(link);
1707 return error;
1708 }
Al Viro626de992015-05-04 18:26:59 -04001709 }
1710
Al Viroab104922015-05-10 11:50:01 -04001711 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001712 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001713 clear_delayed_call(&last->done);
1714 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001715 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001716 return 1;
1717}
1718
Al Viro8f64fb12016-11-14 01:50:26 -05001719enum {WALK_FOLLOW = 1, WALK_MORE = 2};
Al Viro31d66bc2016-11-14 01:43:34 -05001720
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001721/*
1722 * Do we need to follow links? We _really_ want to be able
1723 * to do this check without having to look at inode->i_op,
1724 * so we keep a cache of "no, this doesn't need follow_link"
1725 * for the common case.
1726 */
Al Viro8f64fb12016-11-14 01:50:26 -05001727static inline int step_into(struct nameidata *nd, struct path *path,
1728 int flags, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001729{
Al Viro31d66bc2016-11-14 01:43:34 -05001730 if (!(flags & WALK_MORE) && nd->depth)
1731 put_link(nd);
Al Viro8f64fb12016-11-14 01:50:26 -05001732 if (likely(!d_is_symlink(path->dentry)) ||
1733 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1734 /* not a symlink or should not follow */
1735 path_to_nameidata(path, nd);
1736 nd->inode = inode;
1737 nd->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001738 return 0;
Al Viro8f64fb12016-11-14 01:50:26 -05001739 }
Al Viroa7f77542016-02-27 19:31:01 -05001740 /* make sure that d_is_symlink above matches inode */
1741 if (nd->flags & LOOKUP_RCU) {
Al Viro8f64fb12016-11-14 01:50:26 -05001742 if (read_seqcount_retry(&path->dentry->d_seq, seq))
Al Viroa7f77542016-02-27 19:31:01 -05001743 return -ECHILD;
1744 }
Al Viro8f64fb12016-11-14 01:50:26 -05001745 return pick_link(nd, path, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001746}
1747
Al Viro4693a542015-05-04 17:47:11 -04001748static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001749{
Al Virocaa856342015-04-22 17:52:47 -04001750 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001751 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001752 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001753 int err;
1754 /*
1755 * "." and ".." are special - ".." especially so because it has
1756 * to be able to know about the current root directory and
1757 * parent relationships.
1758 */
Al Viro4693a542015-05-04 17:47:11 -04001759 if (unlikely(nd->last_type != LAST_NORM)) {
1760 err = handle_dots(nd, nd->last_type);
Al Viro1c4ff1a2016-11-14 01:39:36 -05001761 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001762 put_link(nd);
1763 return err;
1764 }
Al Viro254cf582015-05-05 09:40:46 -04001765 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001766 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001767 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001768 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001769 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1770 nd->flags);
1771 if (IS_ERR(path.dentry))
1772 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001773
Al Viroe3c13922016-03-06 14:03:27 -05001774 path.mnt = nd->path.mnt;
1775 err = follow_managed(&path, nd);
1776 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001777 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001778
Al Viro7500c382016-03-31 00:23:05 -04001779 if (unlikely(d_is_negative(path.dentry))) {
1780 path_to_nameidata(&path, nd);
1781 return -ENOENT;
1782 }
1783
Al Viro254cf582015-05-05 09:40:46 -04001784 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001785 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001786 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001787
Al Viro8f64fb12016-11-14 01:50:26 -05001788 return step_into(nd, &path, flags, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001789}
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001792 * We can do the critical dentry name comparison and hashing
1793 * operations one word at a time, but we are limited to:
1794 *
1795 * - Architectures with fast unaligned word accesses. We could
1796 * do a "get_unaligned()" if this helps and is sufficiently
1797 * fast.
1798 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001799 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1800 * do not trap on the (extremely unlikely) case of a page
1801 * crossing operation.
1802 *
1803 * - Furthermore, we need an efficient 64-bit compile for the
1804 * 64-bit case in order to generate the "number of bytes in
1805 * the final mask". Again, that could be replaced with a
1806 * efficient population count instruction or similar.
1807 */
1808#ifdef CONFIG_DCACHE_WORD_ACCESS
1809
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001810#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001811
George Spelvin468a9422016-05-26 22:11:51 -04001812#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001813
George Spelvin468a9422016-05-26 22:11:51 -04001814/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1815
1816#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001817/*
1818 * Register pressure in the mixing function is an issue, particularly
1819 * on 32-bit x86, but almost any function requires one state value and
1820 * one temporary. Instead, use a function designed for two state values
1821 * and no temporaries.
1822 *
1823 * This function cannot create a collision in only two iterations, so
1824 * we have two iterations to achieve avalanche. In those two iterations,
1825 * we have six layers of mixing, which is enough to spread one bit's
1826 * influence out to 2^6 = 64 state bits.
1827 *
1828 * Rotate constants are scored by considering either 64 one-bit input
1829 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1830 * probability of that delta causing a change to each of the 128 output
1831 * bits, using a sample of random initial states.
1832 *
1833 * The Shannon entropy of the computed probabilities is then summed
1834 * to produce a score. Ideally, any input change has a 50% chance of
1835 * toggling any given output bit.
1836 *
1837 * Mixing scores (in bits) for (12,45):
1838 * Input delta: 1-bit 2-bit
1839 * 1 round: 713.3 42542.6
1840 * 2 rounds: 2753.7 140389.8
1841 * 3 rounds: 5954.1 233458.2
1842 * 4 rounds: 7862.6 256672.2
1843 * Perfect: 8192 258048
1844 * (64*128) (64*63/2 * 128)
1845 */
1846#define HASH_MIX(x, y, a) \
1847 ( x ^= (a), \
1848 y ^= x, x = rol64(x,12),\
1849 x += y, y = rol64(y,45),\
1850 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001851
George Spelvin0fed3ac2016-05-02 06:31:01 -04001852/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001853 * Fold two longs into one 32-bit hash value. This must be fast, but
1854 * latency isn't quite as critical, as there is a fair bit of additional
1855 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001856 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001857static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001858{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001859 y ^= x * GOLDEN_RATIO_64;
1860 y *= GOLDEN_RATIO_64;
1861 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001862}
1863
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001864#else /* 32-bit case */
1865
George Spelvin2a18da7a2016-05-23 07:43:58 -04001866/*
1867 * Mixing scores (in bits) for (7,20):
1868 * Input delta: 1-bit 2-bit
1869 * 1 round: 330.3 9201.6
1870 * 2 rounds: 1246.4 25475.4
1871 * 3 rounds: 1907.1 31295.1
1872 * 4 rounds: 2042.3 31718.6
1873 * Perfect: 2048 31744
1874 * (32*64) (32*31/2 * 64)
1875 */
1876#define HASH_MIX(x, y, a) \
1877 ( x ^= (a), \
1878 y ^= x, x = rol32(x, 7),\
1879 x += y, y = rol32(y,20),\
1880 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001881
George Spelvin2a18da7a2016-05-23 07:43:58 -04001882static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001883{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001884 /* Use arch-optimized multiply if one exists */
1885 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001886}
1887
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001888#endif
1889
George Spelvin2a18da7a2016-05-23 07:43:58 -04001890/*
1891 * Return the hash of a string of known length. This is carfully
1892 * designed to match hash_name(), which is the more critical function.
1893 * In particular, we must end by hashing a final word containing 0..7
1894 * payload bytes, to match the way that hash_name() iterates until it
1895 * finds the delimiter after the name.
1896 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001897unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001898{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001899 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001900
1901 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001902 if (!len)
1903 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001904 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001905 if (len < sizeof(unsigned long))
1906 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001907 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001908 name += sizeof(unsigned long);
1909 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001910 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001911 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001912done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001913 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001914}
1915EXPORT_SYMBOL(full_name_hash);
1916
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001917/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001918u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001919{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001920 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1921 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001922 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1923
Linus Torvalds8387ff22016-06-10 07:51:30 -07001924 len = 0;
1925 goto inside;
1926
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001927 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001928 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001929 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001930inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001931 a = load_unaligned_zeropad(name+len);
1932 } while (!has_zero(a, &adata, &constants));
1933
1934 adata = prep_zero_mask(a, adata, &constants);
1935 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001936 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001937
George Spelvin2a18da7a2016-05-23 07:43:58 -04001938 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001939}
1940EXPORT_SYMBOL(hashlen_string);
1941
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001942/*
1943 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001944 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001945 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001946static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001947{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001948 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1949 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07001950 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001951
Linus Torvalds8387ff22016-06-10 07:51:30 -07001952 len = 0;
1953 goto inside;
1954
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001955 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001956 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001957 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001958inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001959 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001960 b = a ^ REPEAT_BYTE('/');
1961 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001962
Linus Torvalds36126f82012-05-26 10:43:17 -07001963 adata = prep_zero_mask(a, adata, &constants);
1964 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07001965 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001966 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07001967
George Spelvin2a18da7a2016-05-23 07:43:58 -04001968 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001969}
1970
George Spelvin2a18da7a2016-05-23 07:43:58 -04001971#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001972
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001973/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001974unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08001975{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001976 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001977 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001978 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001979 return end_name_hash(hash);
1980}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001981EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001982
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001983/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001984u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001985{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001986 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001987 unsigned long len = 0, c;
1988
1989 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04001990 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001991 len++;
1992 hash = partial_name_hash(c, hash);
1993 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04001994 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001995 return hashlen_create(end_name_hash(hash), len);
1996}
George Spelvinf2a031b2016-05-29 01:26:41 -04001997EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001998
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001999/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002000 * We know there's a real path component here of at least
2001 * one character.
2002 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002003static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002004{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002005 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002006 unsigned long len = 0, c;
2007
2008 c = (unsigned char)*name;
2009 do {
2010 len++;
2011 hash = partial_name_hash(c, hash);
2012 c = (unsigned char)name[len];
2013 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002014 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002015}
2016
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002017#endif
2018
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002019/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002021 * This is the basic name resolution function, turning a pathname into
2022 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002024 * Returns 0 and nd will have valid dentry and mnt on success.
2025 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 */
Al Viro6de88d72009-08-09 01:41:57 +04002027static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 while (*name=='/')
2032 name++;
2033 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002034 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* At this point we know we have a real path component. */
2037 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002038 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002039 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040
Al Viro52094c82011-02-21 21:34:47 -05002041 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002042 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002043 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds8387ff22016-06-10 07:51:30 -07002045 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
Al Virofe479a52011-02-22 15:10:03 -05002047 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002048 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002049 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002050 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002051 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002052 nd->flags |= LOOKUP_JUMPED;
2053 }
Al Virofe479a52011-02-22 15:10:03 -05002054 break;
2055 case 1:
2056 type = LAST_DOT;
2057 }
Al Viro5a202bc2011-03-08 14:17:44 -05002058 if (likely(type == LAST_NORM)) {
2059 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002060 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002061 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002062 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002063 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002064 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002065 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002066 hash_len = this.hash_len;
2067 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002068 }
2069 }
Al Virofe479a52011-02-22 15:10:03 -05002070
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002071 nd->last.hash_len = hash_len;
2072 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002073 nd->last_type = type;
2074
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002075 name += hashlen_len(hash_len);
2076 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002077 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002078 /*
2079 * If it wasn't NUL, we know it was '/'. Skip that
2080 * slash, and continue until no more slashes.
2081 */
2082 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002083 name++;
2084 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002085 if (unlikely(!*name)) {
2086OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002087 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002088 if (!nd->depth)
2089 return 0;
2090 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002091 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002092 if (!name)
2093 return 0;
2094 /* last component of nested symlink */
Al Viro8f64fb12016-11-14 01:50:26 -05002095 err = walk_component(nd, WALK_FOLLOW);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002096 } else {
2097 /* not the last component */
Al Viro8f64fb12016-11-14 01:50:26 -05002098 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002099 }
Al Viroce57dfc2011-03-13 19:58:58 -04002100 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002101 return err;
Al Virofe479a52011-02-22 15:10:03 -05002102
Al Viroce57dfc2011-03-13 19:58:58 -04002103 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002104 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002105
Viresh Kumara1c83682015-08-12 15:59:44 +05302106 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002107 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002108 err = 0;
2109 if (unlikely(!s)) {
2110 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002111 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002112 } else {
Al Virofab51e82015-05-10 11:01:00 -04002113 nd->stack[nd->depth - 1].name = name;
2114 name = s;
2115 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002116 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002117 }
Al Viro97242f92015-08-01 19:59:28 -04002118 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2119 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002120 if (unlazy_walk(nd))
Al Viro97242f92015-08-01 19:59:28 -04002121 return -ECHILD;
2122 }
Al Viro3595e232015-05-09 16:54:45 -04002123 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002124 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
Al Viroc8a53ee2015-05-12 18:43:07 -04002128static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129{
Al Viroc8a53ee2015-05-12 18:43:07 -04002130 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002132 if (!*s)
2133 flags &= ~LOOKUP_RCU;
2134
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002136 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002138 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002139 struct dentry *root = nd->root.dentry;
2140 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002141 if (*s && unlikely(!d_can_lookup(root)))
2142 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002143 nd->path = nd->root;
2144 nd->inode = inode;
2145 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002146 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05002147 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002148 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002149 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002150 } else {
2151 path_get(&nd->path);
2152 }
Al Viro368ee9b2015-05-08 17:19:59 -04002153 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002154 }
2155
Al Viro2a737872009-04-07 11:49:53 -04002156 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002157 nd->path.mnt = NULL;
2158 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Al Viro48a066e2013-09-29 22:06:07 -04002160 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002161 if (*s == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05002162 if (flags & LOOKUP_RCU)
Al Viro8b61e742013-11-08 12:45:01 -05002163 rcu_read_lock();
Al Viro9e6697e2015-12-05 20:07:21 -05002164 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05002165 if (likely(!nd_jump_root(nd)))
Al Viroef55d912015-12-05 20:25:06 -05002166 return s;
Al Viro248fb5b2015-12-05 20:51:58 -05002167 nd->root.mnt = NULL;
Al Viroef55d912015-12-05 20:25:06 -05002168 rcu_read_unlock();
2169 return ERR_PTR(-ECHILD);
Al Viroc8a53ee2015-05-12 18:43:07 -04002170 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002171 if (flags & LOOKUP_RCU) {
2172 struct fs_struct *fs = current->fs;
2173 unsigned seq;
2174
Al Viro8b61e742013-11-08 12:45:01 -05002175 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002176
2177 do {
2178 seq = read_seqcount_begin(&fs->seq);
2179 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002180 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002181 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2182 } while (read_seqcount_retry(&fs->seq, seq));
2183 } else {
2184 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002185 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002186 }
Al Viroef55d912015-12-05 20:25:06 -05002187 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002188 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002189 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002190 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002191 struct dentry *dentry;
2192
Al Viro2903ff02012-08-28 12:52:22 -04002193 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002194 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002195
Al Viro2903ff02012-08-28 12:52:22 -04002196 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002197
Al Virofd2f7cb2015-02-22 20:07:13 -05002198 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002199 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002200 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002201 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002202 }
Al Virof52e0c12011-03-14 18:56:51 -04002203 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002204
Al Viro2903ff02012-08-28 12:52:22 -04002205 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002206 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002207 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002208 nd->inode = nd->path.dentry->d_inode;
2209 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002210 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002211 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002212 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002213 }
Al Viro34a26b92015-05-11 08:05:05 -04002214 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002215 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002217}
2218
Al Viro3bdba282015-05-08 17:37:07 -04002219static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002220{
2221 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002222 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002223 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002224 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002225 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002226 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002227 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002228 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002229}
2230
Al Virocaa856342015-04-22 17:52:47 -04002231static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002232{
2233 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2234 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2235
2236 nd->flags &= ~LOOKUP_PARENT;
Al Viro1c4ff1a2016-11-14 01:39:36 -05002237 return walk_component(nd, 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002238}
2239
Al Viro4f757f32017-04-15 17:31:22 -04002240static int handle_lookup_down(struct nameidata *nd)
2241{
2242 struct path path = nd->path;
2243 struct inode *inode = nd->inode;
2244 unsigned seq = nd->seq;
2245 int err;
2246
2247 if (nd->flags & LOOKUP_RCU) {
2248 /*
2249 * don't bother with unlazy_walk on failure - we are
2250 * at the very beginning of walk, so we lose nothing
2251 * if we simply redo everything in non-RCU mode
2252 */
2253 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2254 return -ECHILD;
2255 } else {
2256 dget(path.dentry);
2257 err = follow_managed(&path, nd);
2258 if (unlikely(err < 0))
2259 return err;
2260 inode = d_backing_inode(path.dentry);
2261 seq = 0;
2262 }
2263 path_to_nameidata(&path, nd);
2264 nd->inode = inode;
2265 nd->seq = seq;
2266 return 0;
2267}
2268
Al Viro9b4a9b12009-04-07 11:44:16 -04002269/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002270static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002271{
Al Viroc8a53ee2015-05-12 18:43:07 -04002272 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002273 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002274
Al Viro368ee9b2015-05-08 17:19:59 -04002275 if (IS_ERR(s))
2276 return PTR_ERR(s);
Al Viro4f757f32017-04-15 17:31:22 -04002277
2278 if (unlikely(flags & LOOKUP_DOWN)) {
2279 err = handle_lookup_down(nd);
2280 if (unlikely(err < 0)) {
2281 terminate_walk(nd);
2282 return err;
2283 }
2284 }
2285
Al Viro3bdba282015-05-08 17:37:07 -04002286 while (!(err = link_path_walk(s, nd))
2287 && ((err = lookup_last(nd)) > 0)) {
2288 s = trailing_symlink(nd);
2289 if (IS_ERR(s)) {
2290 err = PTR_ERR(s);
2291 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002292 }
2293 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002294 if (!err)
2295 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002296
Al Virodeb106c2015-05-08 18:05:21 -04002297 if (!err && nd->flags & LOOKUP_DIRECTORY)
2298 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002299 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002300 if (!err) {
2301 *path = nd->path;
2302 nd->path.mnt = NULL;
2303 nd->path.dentry = NULL;
2304 }
2305 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002306 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002307}
Nick Piggin31e6b012011-01-07 17:49:52 +11002308
Al Viro625b6d12015-05-12 16:36:12 -04002309static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002310 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002311{
Al Viro894bc8c2015-05-02 07:16:16 -04002312 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002313 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002314 if (IS_ERR(name))
2315 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002316 if (unlikely(root)) {
2317 nd.root = *root;
2318 flags |= LOOKUP_ROOT;
2319 }
Al Viro9883d182015-05-13 07:28:08 -04002320 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002321 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002322 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002323 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002324 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002325 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002326
2327 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002328 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002329 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002330 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002331 return retval;
2332}
2333
Al Viro8bcb77f2015-05-08 16:59:20 -04002334/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002335static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002336 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002337{
Al Viroc8a53ee2015-05-12 18:43:07 -04002338 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002339 int err;
2340 if (IS_ERR(s))
2341 return PTR_ERR(s);
2342 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002343 if (!err)
2344 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002345 if (!err) {
2346 *parent = nd->path;
2347 nd->path.mnt = NULL;
2348 nd->path.dentry = NULL;
2349 }
2350 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002351 return err;
2352}
2353
Al Viro5c31b6c2015-05-12 17:32:54 -04002354static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002355 unsigned int flags, struct path *parent,
2356 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002357{
2358 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002359 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002360
Al Viro5c31b6c2015-05-12 17:32:54 -04002361 if (IS_ERR(name))
2362 return name;
Al Viro9883d182015-05-13 07:28:08 -04002363 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002364 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002365 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002366 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002367 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002368 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002369 if (likely(!retval)) {
2370 *last = nd.last;
2371 *type = nd.last_type;
2372 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002373 } else {
2374 putname(name);
2375 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002376 }
Al Viro9883d182015-05-13 07:28:08 -04002377 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002378 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002379}
2380
Al Viro79714f72012-06-15 03:01:42 +04002381/* does lookup, returns the object with parent locked */
2382struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002383{
Al Viro5c31b6c2015-05-12 17:32:54 -04002384 struct filename *filename;
2385 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002386 struct qstr last;
2387 int type;
Paul Moore51689102015-01-22 00:00:03 -05002388
Al Viro5c31b6c2015-05-12 17:32:54 -04002389 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2390 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002391 if (IS_ERR(filename))
2392 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002393 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002394 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002395 putname(filename);
2396 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002397 }
Al Viro59551022016-01-22 15:40:57 -05002398 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002399 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002400 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002401 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002402 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002403 }
Paul Moore51689102015-01-22 00:00:03 -05002404 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002405 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002406}
2407
Al Virod1811462008-08-02 00:49:18 -04002408int kern_path(const char *name, unsigned int flags, struct path *path)
2409{
Al Viroabc9f5b2015-05-12 16:53:42 -04002410 return filename_lookup(AT_FDCWD, getname_kernel(name),
2411 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002412}
Al Viro4d359502014-03-14 12:20:17 -04002413EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002414
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002415/**
2416 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2417 * @dentry: pointer to dentry of the base directory
2418 * @mnt: pointer to vfs mount of the base directory
2419 * @name: pointer to file name
2420 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002421 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002422 */
2423int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2424 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002425 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002426{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002427 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002428 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002429 return filename_lookup(AT_FDCWD, getname_kernel(name),
2430 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002431}
Al Viro4d359502014-03-14 12:20:17 -04002432EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002433
Al Viro3c95f0d2018-04-06 16:32:38 -04002434static int lookup_one_len_common(const char *name, struct dentry *base,
2435 int len, struct qstr *this)
2436{
2437 this->name = name;
2438 this->len = len;
2439 this->hash = full_name_hash(base, name, len);
2440 if (!len)
2441 return -EACCES;
2442
2443 if (unlikely(name[0] == '.')) {
2444 if (len < 2 || (len == 2 && name[1] == '.'))
2445 return -EACCES;
2446 }
2447
2448 while (len--) {
2449 unsigned int c = *(const unsigned char *)name++;
2450 if (c == '/' || c == '\0')
2451 return -EACCES;
2452 }
2453 /*
2454 * See if the low-level filesystem might want
2455 * to use its own hash..
2456 */
2457 if (base->d_flags & DCACHE_OP_HASH) {
2458 int err = base->d_op->d_hash(base, this);
2459 if (err < 0)
2460 return err;
2461 }
2462
2463 return inode_permission(base->d_inode, MAY_EXEC);
2464}
2465
Christoph Hellwigeead1912007-10-16 23:25:38 -07002466/**
David Howells0da0b7f2018-06-15 15:19:22 +01002467 * try_lookup_one_len - filesystem helper to lookup single pathname component
2468 * @name: pathname component to lookup
2469 * @base: base directory to lookup from
2470 * @len: maximum length @len should be interpreted to
2471 *
2472 * Look up a dentry by name in the dcache, returning NULL if it does not
2473 * currently exist. The function does not try to create a dentry.
2474 *
2475 * Note that this routine is purely a helper for filesystem usage and should
2476 * not be called by generic code.
2477 *
2478 * The caller must hold base->i_mutex.
2479 */
2480struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2481{
2482 struct qstr this;
2483 int err;
2484
2485 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2486
2487 err = lookup_one_len_common(name, base, len, &this);
2488 if (err)
2489 return ERR_PTR(err);
2490
2491 return lookup_dcache(&this, base, 0);
2492}
2493EXPORT_SYMBOL(try_lookup_one_len);
2494
2495/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002496 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002497 * @name: pathname component to lookup
2498 * @base: base directory to lookup from
2499 * @len: maximum length @len should be interpreted to
2500 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002501 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002502 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002503 *
2504 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002505 */
James Morris057f6c02007-04-26 00:12:05 -07002506struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2507{
Al Viro8613a202018-04-06 16:45:33 -04002508 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002509 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002510 int err;
James Morris057f6c02007-04-26 00:12:05 -07002511
Al Viro59551022016-01-22 15:40:57 -05002512 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002513
Al Viro3c95f0d2018-04-06 16:32:38 -04002514 err = lookup_one_len_common(name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002515 if (err)
2516 return ERR_PTR(err);
2517
Al Viro8613a202018-04-06 16:45:33 -04002518 dentry = lookup_dcache(&this, base, 0);
2519 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002520}
Al Viro4d359502014-03-14 12:20:17 -04002521EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002522
NeilBrownbbddca82016-01-07 16:08:20 -05002523/**
2524 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2525 * @name: pathname component to lookup
2526 * @base: base directory to lookup from
2527 * @len: maximum length @len should be interpreted to
2528 *
2529 * Note that this routine is purely a helper for filesystem usage and should
2530 * not be called by generic code.
2531 *
2532 * Unlike lookup_one_len, it should be called without the parent
2533 * i_mutex held, and will take the i_mutex itself if necessary.
2534 */
2535struct dentry *lookup_one_len_unlocked(const char *name,
2536 struct dentry *base, int len)
2537{
2538 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002539 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002540 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002541
Al Viro3c95f0d2018-04-06 16:32:38 -04002542 err = lookup_one_len_common(name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002543 if (err)
2544 return ERR_PTR(err);
2545
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002546 ret = lookup_dcache(&this, base, 0);
2547 if (!ret)
2548 ret = lookup_slow(&this, base, 0);
2549 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002550}
2551EXPORT_SYMBOL(lookup_one_len_unlocked);
2552
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002553#ifdef CONFIG_UNIX98_PTYS
2554int path_pts(struct path *path)
2555{
2556 /* Find something mounted on "pts" in the same directory as
2557 * the input path.
2558 */
2559 struct dentry *child, *parent;
2560 struct qstr this;
2561 int ret;
2562
2563 ret = path_parent_directory(path);
2564 if (ret)
2565 return ret;
2566
2567 parent = path->dentry;
2568 this.name = "pts";
2569 this.len = 3;
2570 child = d_hash_and_lookup(parent, &this);
2571 if (!child)
2572 return -ENOENT;
2573
2574 path->dentry = child;
2575 dput(parent);
2576 follow_mount(path);
2577 return 0;
2578}
2579#endif
2580
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002581int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2582 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Al Viroabc9f5b2015-05-12 16:53:42 -04002584 return filename_lookup(dfd, getname_flags(name, flags, empty),
2585 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586}
Al Virob853a162015-05-13 09:12:02 -04002587EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002588
Jeff Layton80334262013-07-26 06:23:25 -04002589/**
Al Viro197df042013-09-08 14:03:27 -04002590 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002591 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
Jeff Layton80334262013-07-26 06:23:25 -04002592 *
2593 * This is a special lookup_last function just for umount. In this case, we
2594 * need to resolve the path without doing any revalidation.
2595 *
2596 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2597 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2598 * in almost all cases, this lookup will be served out of the dcache. The only
2599 * cases where it won't are if nd->last refers to a symlink or the path is
2600 * bogus and it doesn't exist.
2601 *
2602 * Returns:
2603 * -error: if there was an error during lookup. This includes -ENOENT if the
Al Viroba8f4612016-11-14 00:40:33 -05002604 * lookup found a negative dentry.
Jeff Layton80334262013-07-26 06:23:25 -04002605 *
Al Viroba8f4612016-11-14 00:40:33 -05002606 * 0: if we successfully resolved nd->last and found it to not to be a
2607 * symlink that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002608 *
2609 * 1: if we successfully resolved nd->last and found it to be a symlink
Al Viroba8f4612016-11-14 00:40:33 -05002610 * that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002611 */
2612static int
Al Viroba8f4612016-11-14 00:40:33 -05002613mountpoint_last(struct nameidata *nd)
Jeff Layton80334262013-07-26 06:23:25 -04002614{
2615 int error = 0;
Jeff Layton80334262013-07-26 06:23:25 -04002616 struct dentry *dir = nd->path.dentry;
Al Viroba8f4612016-11-14 00:40:33 -05002617 struct path path;
Jeff Layton80334262013-07-26 06:23:25 -04002618
Al Viro35759522013-09-08 13:41:33 -04002619 /* If we're in rcuwalk, drop out of it to handle last component */
2620 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002621 if (unlazy_walk(nd))
Al Virodeb106c2015-05-08 18:05:21 -04002622 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002623 }
2624
2625 nd->flags &= ~LOOKUP_PARENT;
2626
2627 if (unlikely(nd->last_type != LAST_NORM)) {
2628 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002629 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002630 return error;
Al Viroba8f4612016-11-14 00:40:33 -05002631 path.dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002632 } else {
Al Viroba8f4612016-11-14 00:40:33 -05002633 path.dentry = d_lookup(dir, &nd->last);
2634 if (!path.dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002635 /*
2636 * No cached dentry. Mounted dentries are pinned in the
2637 * cache, so that means that this dentry is probably
2638 * a symlink or the path doesn't actually point
2639 * to a mounted dentry.
2640 */
Al Viroba8f4612016-11-14 00:40:33 -05002641 path.dentry = lookup_slow(&nd->last, dir,
Al Viro949a8522016-03-06 14:20:52 -05002642 nd->flags | LOOKUP_NO_REVAL);
Al Viroba8f4612016-11-14 00:40:33 -05002643 if (IS_ERR(path.dentry))
2644 return PTR_ERR(path.dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002645 }
Jeff Layton80334262013-07-26 06:23:25 -04002646 }
Al Viroba8f4612016-11-14 00:40:33 -05002647 if (d_is_negative(path.dentry)) {
2648 dput(path.dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002649 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002650 }
Al Viroba8f4612016-11-14 00:40:33 -05002651 path.mnt = nd->path.mnt;
Al Viro8f64fb12016-11-14 01:50:26 -05002652 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
Jeff Layton80334262013-07-26 06:23:25 -04002653}
2654
2655/**
Al Viro197df042013-09-08 14:03:27 -04002656 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b8572015-09-09 15:39:23 -07002657 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002658 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002659 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002660 *
2661 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002662 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002663 */
2664static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002665path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002666{
Al Viroc8a53ee2015-05-12 18:43:07 -04002667 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002668 int err;
2669 if (IS_ERR(s))
2670 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002671 while (!(err = link_path_walk(s, nd)) &&
Al Viroba8f4612016-11-14 00:40:33 -05002672 (err = mountpoint_last(nd)) > 0) {
Al Viro3bdba282015-05-08 17:37:07 -04002673 s = trailing_symlink(nd);
2674 if (IS_ERR(s)) {
2675 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002676 break;
Al Viro3bdba282015-05-08 17:37:07 -04002677 }
Jeff Layton80334262013-07-26 06:23:25 -04002678 }
Al Viroba8f4612016-11-14 00:40:33 -05002679 if (!err) {
2680 *path = nd->path;
2681 nd->path.mnt = NULL;
2682 nd->path.dentry = NULL;
2683 follow_mount(path);
2684 }
Al Virodeb106c2015-05-08 18:05:21 -04002685 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002686 return err;
2687}
2688
Al Viro2d864652013-09-08 20:18:44 -04002689static int
Al Viro668696d2015-02-22 19:44:00 -05002690filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002691 unsigned int flags)
2692{
Al Viro9883d182015-05-13 07:28:08 -04002693 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002694 int error;
Al Viro668696d2015-02-22 19:44:00 -05002695 if (IS_ERR(name))
2696 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002697 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002698 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002699 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002700 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002701 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002702 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002703 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002704 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002705 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002706 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002707 return error;
2708}
2709
Jeff Layton80334262013-07-26 06:23:25 -04002710/**
Al Viro197df042013-09-08 14:03:27 -04002711 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002712 * @dfd: directory file descriptor
2713 * @name: pathname from userland
2714 * @flags: lookup flags
2715 * @path: pointer to container to hold result
2716 *
2717 * A umount is a special case for path walking. We're not actually interested
2718 * in the inode in this situation, and ESTALE errors can be a problem. We
2719 * simply want track down the dentry and vfsmount attached at the mountpoint
2720 * and avoid revalidating the last component.
2721 *
2722 * Returns 0 and populates "path" on success.
2723 */
2724int
Al Viro197df042013-09-08 14:03:27 -04002725user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002726 struct path *path)
2727{
Al Virocbaab2d2015-01-22 02:49:00 -05002728 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002729}
2730
Al Viro2d864652013-09-08 20:18:44 -04002731int
2732kern_path_mountpoint(int dfd, const char *name, struct path *path,
2733 unsigned int flags)
2734{
Al Virocbaab2d2015-01-22 02:49:00 -05002735 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002736}
2737EXPORT_SYMBOL(kern_path_mountpoint);
2738
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002739int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002741 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002742
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002743 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002745 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002747 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002749EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751/*
2752 * Check whether we can remove a link victim from directory dir, check
2753 * whether the type of victim is right.
2754 * 1. We can't do it if dir is read-only (done in permission())
2755 * 2. We should have write and exec permissions on dir
2756 * 3. We can't remove anything from append-only dir
2757 * 4. We can't do anything with immutable dir (done in permission())
2758 * 5. If the sticky bit on dir is set we should either
2759 * a. be owner of dir, or
2760 * b. be owner of victim, or
2761 * c. have CAP_FOWNER capability
2762 * 6. If the victim is append-only or immutable we can't do antyhing with
2763 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002764 * 7. If the victim has an unknown uid or gid we can't change the inode.
2765 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2766 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2767 * 10. We can't remove a root or mountpoint.
2768 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 * nfs_async_unlink().
2770 */
David Howellsb18825a2013-09-12 19:22:53 +01002771static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772{
David Howells63afdfc2015-05-06 15:59:00 +01002773 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 int error;
2775
David Howellsb18825a2013-09-12 19:22:53 +01002776 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002778 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002781
2782 /* Inode writeback is not safe when the uid or gid are invalid. */
2783 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2784 return -EOVERFLOW;
2785
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002786 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Al Virof419a2e2008-07-22 00:07:17 -04002788 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 if (error)
2790 return error;
2791 if (IS_APPEND(dir))
2792 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002793
2794 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002795 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 return -EPERM;
2797 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002798 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 return -ENOTDIR;
2800 if (IS_ROOT(victim))
2801 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002802 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return -EISDIR;
2804 if (IS_DEADDIR(dir))
2805 return -ENOENT;
2806 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2807 return -EBUSY;
2808 return 0;
2809}
2810
2811/* Check whether we can create an object with dentry child in directory
2812 * dir.
2813 * 1. We can't do it if child already exists (open has special treatment for
2814 * this case, but since we are inlined it's OK)
2815 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002816 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2817 * 4. We should have write and exec permissions on dir
2818 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002820static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002822 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002823 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 if (child->d_inode)
2825 return -EEXIST;
2826 if (IS_DEADDIR(dir))
2827 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002828 s_user_ns = dir->i_sb->s_user_ns;
2829 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2830 !kgid_has_mapping(s_user_ns, current_fsgid()))
2831 return -EOVERFLOW;
Al Virof419a2e2008-07-22 00:07:17 -04002832 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833}
2834
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835/*
2836 * p1 and p2 should be directories on the same fs.
2837 */
2838struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2839{
2840 struct dentry *p;
2841
2842 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002843 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 return NULL;
2845 }
2846
Al Virofc640052016-04-10 01:33:30 -04002847 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002849 p = d_ancestor(p2, p1);
2850 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002851 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2852 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002853 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 }
2855
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002856 p = d_ancestor(p1, p2);
2857 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002858 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2859 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002860 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
2862
Al Viro59551022016-01-22 15:40:57 -05002863 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2864 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return NULL;
2866}
Al Viro4d359502014-03-14 12:20:17 -04002867EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869void unlock_rename(struct dentry *p1, struct dentry *p2)
2870{
Al Viro59551022016-01-22 15:40:57 -05002871 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002873 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002874 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 }
2876}
Al Viro4d359502014-03-14 12:20:17 -04002877EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878
Al Viro4acdaf22011-07-26 01:42:34 -04002879int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002880 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002882 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (error)
2884 return error;
2885
Al Viroacfa4382008-12-04 10:06:33 -05002886 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 return -EACCES; /* shouldn't it be ENOSYS? */
2888 mode &= S_IALLUGO;
2889 mode |= S_IFREG;
2890 error = security_inode_create(dir, dentry, mode);
2891 if (error)
2892 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002893 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002894 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002895 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 return error;
2897}
Al Viro4d359502014-03-14 12:20:17 -04002898EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Al Viro8e6c8482017-12-01 17:12:45 -05002900int vfs_mkobj(struct dentry *dentry, umode_t mode,
2901 int (*f)(struct dentry *, umode_t, void *),
2902 void *arg)
2903{
2904 struct inode *dir = dentry->d_parent->d_inode;
2905 int error = may_create(dir, dentry);
2906 if (error)
2907 return error;
2908
2909 mode &= S_IALLUGO;
2910 mode |= S_IFREG;
2911 error = security_inode_create(dir, dentry, mode);
2912 if (error)
2913 return error;
2914 error = f(dentry, mode, arg);
2915 if (!error)
2916 fsnotify_create(dir, dentry);
2917 return error;
2918}
2919EXPORT_SYMBOL(vfs_mkobj);
2920
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002921bool may_open_dev(const struct path *path)
2922{
2923 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2924 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2925}
2926
Al Virof0bb5aa2016-11-20 20:27:12 -05002927static int may_open(const struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002929 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 struct inode *inode = dentry->d_inode;
2931 int error;
2932
2933 if (!inode)
2934 return -ENOENT;
2935
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002936 switch (inode->i_mode & S_IFMT) {
2937 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002939 case S_IFDIR:
2940 if (acc_mode & MAY_WRITE)
2941 return -EISDIR;
2942 break;
2943 case S_IFBLK:
2944 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002945 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002947 /*FALLTHRU*/
2948 case S_IFIFO:
2949 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002951 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002952 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002953
Al Viro62fb4a12015-12-26 22:33:24 -05002954 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002955 if (error)
2956 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * An append-only file must be opened in append mode for writing.
2960 */
2961 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002962 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002963 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002965 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967
2968 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002969 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002970 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002972 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Jeff Laytone1181ee2010-12-07 16:19:50 -05002975static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002976{
Al Virof0bb5aa2016-11-20 20:27:12 -05002977 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002978 struct inode *inode = path->dentry->d_inode;
2979 int error = get_write_access(inode);
2980 if (error)
2981 return error;
2982 /*
2983 * Refuse to truncate files with mandatory locks held on them.
2984 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002985 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002986 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002987 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002988 if (!error) {
2989 error = do_truncate(path->dentry, 0,
2990 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002991 filp);
Al Viro7715b522009-12-16 03:54:00 -05002992 }
2993 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002994 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Dave Hansend57999e2008-02-15 14:37:27 -08002997static inline int open_to_namei_flags(int flag)
2998{
Al Viro8a5e9292011-06-25 19:15:54 -04002999 if ((flag & O_ACCMODE) == 3)
3000 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003001 return flag;
3002}
3003
Al Virod3607752016-03-25 15:21:09 -04003004static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003005{
Seth Forshee1328c722017-01-26 14:33:46 -06003006 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003007 int error = security_path_mknod(dir, dentry, mode, 0);
3008 if (error)
3009 return error;
3010
Seth Forshee1328c722017-01-26 14:33:46 -06003011 s_user_ns = dir->dentry->d_sb->s_user_ns;
3012 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3013 !kgid_has_mapping(s_user_ns, current_fsgid()))
3014 return -EOVERFLOW;
3015
Miklos Szeredid18e9002012-06-05 15:10:17 +02003016 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3017 if (error)
3018 return error;
3019
3020 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3021}
3022
David Howells1acf0af2012-06-14 16:13:46 +01003023/*
3024 * Attempt to atomically look up, create and open a file from a negative
3025 * dentry.
3026 *
3027 * Returns 0 if successful. The file will have been created and attached to
3028 * @file by the filesystem calling finish_open().
3029 *
3030 * Returns 1 if the file was looked up only or didn't need creating. The
3031 * caller will need to perform the open themselves. @path will have been
3032 * updated to point to the new dentry. This may be negative.
3033 *
3034 * Returns an error code otherwise.
3035 */
Al Viro2675a4e2012-06-22 12:41:10 +04003036static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3037 struct path *path, struct file *file,
3038 const struct open_flags *op,
Al Viro1643b432016-04-27 19:14:10 -04003039 int open_flag, umode_t mode,
Al Viro2675a4e2012-06-22 12:41:10 +04003040 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003041{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003042 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003043 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003044 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003045
Al Viro384f26e2016-04-28 02:03:55 -04003046 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003047 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003048
Miklos Szeredid18e9002012-06-05 15:10:17 +02003049 if (nd->flags & LOOKUP_DIRECTORY)
3050 open_flag |= O_DIRECTORY;
3051
Al Viro30d90492012-06-22 12:40:19 +04003052 file->f_path.dentry = DENTRY_NOT_SET;
3053 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003054 error = dir->i_op->atomic_open(dir, dentry, file,
3055 open_to_namei_flags(open_flag),
3056 mode, opened);
Al Viro6fbd0712016-04-28 11:50:59 -04003057 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003058 if (!error) {
3059 /*
3060 * We didn't have the inode before the open, so check open
3061 * permission here.
3062 */
3063 int acc_mode = op->acc_mode;
Al Viro73a09dd2018-06-08 13:22:02 -04003064 if (file->f_mode & FMODE_CREATED) {
Al Viro384f26e2016-04-28 02:03:55 -04003065 WARN_ON(!(open_flag & O_CREAT));
3066 fsnotify_create(dir, dentry);
3067 acc_mode = 0;
3068 }
3069 error = may_open(&file->f_path, acc_mode, open_flag);
3070 if (WARN_ON(error > 0))
3071 error = -EINVAL;
3072 } else if (error > 0) {
Al Viro30d90492012-06-22 12:40:19 +04003073 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003074 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003075 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003076 if (file->f_path.dentry) {
3077 dput(dentry);
3078 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003079 }
Al Viro73a09dd2018-06-08 13:22:02 -04003080 if (file->f_mode & FMODE_CREATED)
Al Viro384f26e2016-04-28 02:03:55 -04003081 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04003082 if (unlikely(d_is_negative(dentry))) {
3083 error = -ENOENT;
3084 } else {
3085 path->dentry = dentry;
3086 path->mnt = nd->path.mnt;
3087 return 1;
3088 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003089 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003090 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003091 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003092 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003093}
3094
Nick Piggin31e6b012011-01-07 17:49:52 +11003095/*
David Howells1acf0af2012-06-14 16:13:46 +01003096 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003097 *
3098 * Must be called with i_mutex held on parent.
3099 *
David Howells1acf0af2012-06-14 16:13:46 +01003100 * Returns 0 if the file was successfully atomically created (if necessary) and
3101 * opened. In this case the file will be returned attached to @file.
3102 *
3103 * Returns 1 if the file was not completely opened at this time, though lookups
3104 * and creations will have been performed and the dentry returned in @path will
3105 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3106 * specified then a negative dentry may be returned.
3107 *
3108 * An error code is returned otherwise.
3109 *
3110 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3111 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003112 */
Al Viro2675a4e2012-06-22 12:41:10 +04003113static int lookup_open(struct nameidata *nd, struct path *path,
3114 struct file *file,
3115 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04003116 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003117{
3118 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003119 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003120 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003121 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003122 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003123 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003124 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003125
Al Viroce8644f2016-04-26 14:17:56 -04003126 if (unlikely(IS_DEADDIR(dir_inode)))
3127 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003128
Al Viro73a09dd2018-06-08 13:22:02 -04003129 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003130 dentry = d_lookup(dir, &nd->last);
3131 for (;;) {
3132 if (!dentry) {
3133 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3134 if (IS_ERR(dentry))
3135 return PTR_ERR(dentry);
3136 }
3137 if (d_in_lookup(dentry))
3138 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003139
Al Viro6fbd0712016-04-28 11:50:59 -04003140 error = d_revalidate(dentry, nd->flags);
3141 if (likely(error > 0))
3142 break;
3143 if (error)
3144 goto out_dput;
3145 d_invalidate(dentry);
3146 dput(dentry);
3147 dentry = NULL;
3148 }
3149 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003150 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003151 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003152 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003153
Al Viro1643b432016-04-27 19:14:10 -04003154 /*
3155 * Checking write permission is tricky, bacuse we don't know if we are
3156 * going to actually need it: O_CREAT opens should work as long as the
3157 * file exists. But checking existence breaks atomicity. The trick is
3158 * to check access and if not granted clear O_CREAT from the flags.
3159 *
3160 * Another problem is returing the "right" error value (e.g. for an
3161 * O_EXCL open we want to return EEXIST not EROFS).
3162 */
3163 if (open_flag & O_CREAT) {
3164 if (!IS_POSIXACL(dir->d_inode))
3165 mode &= ~current_umask();
3166 if (unlikely(!got_write)) {
3167 create_error = -EROFS;
3168 open_flag &= ~O_CREAT;
3169 if (open_flag & (O_EXCL | O_TRUNC))
3170 goto no_open;
3171 /* No side effects, safe to clear O_CREAT */
3172 } else {
3173 create_error = may_o_create(&nd->path, dentry, mode);
3174 if (create_error) {
3175 open_flag &= ~O_CREAT;
3176 if (open_flag & O_EXCL)
3177 goto no_open;
3178 }
3179 }
3180 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3181 unlikely(!got_write)) {
3182 /*
3183 * No O_CREATE -> atomicity not a requirement -> fall
3184 * back to lookup + open
3185 */
3186 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003187 }
3188
Al Viro1643b432016-04-27 19:14:10 -04003189 if (dir_inode->i_op->atomic_open) {
3190 error = atomic_open(nd, dentry, path, file, op, open_flag,
3191 mode, opened);
3192 if (unlikely(error == -ENOENT) && create_error)
3193 error = create_error;
3194 return error;
3195 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003196
Al Viro1643b432016-04-27 19:14:10 -04003197no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003198 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003199 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3200 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003201 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003202 if (unlikely(res)) {
3203 if (IS_ERR(res)) {
3204 error = PTR_ERR(res);
3205 goto out_dput;
3206 }
3207 dput(dentry);
3208 dentry = res;
3209 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003210 }
3211
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003212 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003213 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003214 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003215 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003216 if (!dir_inode->i_op->create) {
3217 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003218 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003219 }
Al Viroce8644f2016-04-26 14:17:56 -04003220 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003221 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003222 if (error)
3223 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003224 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003225 }
Al Viro1643b432016-04-27 19:14:10 -04003226 if (unlikely(create_error) && !dentry->d_inode) {
3227 error = create_error;
3228 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003229 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003230out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003231 path->dentry = dentry;
3232 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04003233 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003234
3235out_dput:
3236 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003237 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003238}
3239
3240/*
Al Virofe2d35f2011-03-05 22:58:25 -05003241 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003242 */
Al Viro896475d2015-04-22 18:02:17 -04003243static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04003244 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04003245 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05003246{
Al Viroa1e28032009-12-24 02:12:06 -05003247 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003248 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003249 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003250 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003251 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003252 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003253 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003254 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003255 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003256
Al Viroc3e380b2011-02-23 13:39:45 -05003257 nd->flags &= ~LOOKUP_PARENT;
3258 nd->flags |= op->intent;
3259
Al Virobc77daa2013-06-06 09:12:33 -04003260 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003261 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003262 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003263 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003264 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003265 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003266
Al Viroca344a892011-03-09 00:36:45 -05003267 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003268 if (nd->last.name[nd->last.len])
3269 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3270 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003271 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003272 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003273 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003274
Miklos Szeredi71574862012-06-05 15:10:14 +02003275 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003276 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003277
Miklos Szeredi71574862012-06-05 15:10:14 +02003278 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003279 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003280 } else {
3281 /* create side of things */
3282 /*
3283 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3284 * has been cleared when we got to the last component we are
3285 * about to look up
3286 */
3287 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003288 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003289 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003290
Al Viro76ae2a52015-05-12 18:44:32 -04003291 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003292 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003293 if (unlikely(nd->last.name[nd->last.len]))
3294 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003295 }
3296
Al Viro9cf843e2016-04-28 19:35:16 -04003297 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003298 error = mnt_want_write(nd->path.mnt);
3299 if (!error)
3300 got_write = true;
3301 /*
3302 * do _not_ fail yet - we might not need that or fail with
3303 * a different error; let lookup_open() decide; we'll be
3304 * dropping this one anyway.
3305 */
3306 }
Al Viro9cf843e2016-04-28 19:35:16 -04003307 if (open_flag & O_CREAT)
3308 inode_lock(dir->d_inode);
3309 else
3310 inode_lock_shared(dir->d_inode);
Al Viro896475d2015-04-22 18:02:17 -04003311 error = lookup_open(nd, &path, file, op, got_write, opened);
Al Viro9cf843e2016-04-28 19:35:16 -04003312 if (open_flag & O_CREAT)
3313 inode_unlock(dir->d_inode);
3314 else
3315 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003316
Al Viro2675a4e2012-06-22 12:41:10 +04003317 if (error <= 0) {
3318 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003319 goto out;
3320
Al Viro73a09dd2018-06-08 13:22:02 -04003321 if ((file->f_mode & FMODE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003322 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003323 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003324
Al Viro76ae2a52015-05-12 18:44:32 -04003325 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003326 goto opened;
3327 }
Al Virofb1cc552009-12-24 01:58:28 -05003328
Al Viro73a09dd2018-06-08 13:22:02 -04003329 if (file->f_mode & FMODE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003330 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003331 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003332 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003333 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003334 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003335 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003336 }
3337
3338 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003339 * If atomic_open() acquired write access it is dropped now due to
3340 * possible mount and symlink following (this might be optimized away if
3341 * necessary...)
3342 */
Al Viro64894cf2012-07-31 00:53:35 +04003343 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003344 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003345 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003346 }
3347
Al Viroe6ec03a2016-06-05 00:23:09 -04003348 error = follow_managed(&path, nd);
3349 if (unlikely(error < 0))
3350 return error;
3351
Al Viro6583fe22016-03-05 18:14:03 -05003352 if (unlikely(d_is_negative(path.dentry))) {
3353 path_to_nameidata(&path, nd);
3354 return -ENOENT;
3355 }
3356
3357 /*
3358 * create/update audit record if it already exists.
3359 */
3360 audit_inode(nd->name, path.dentry, 0);
3361
Al Virodeb106c2015-05-08 18:05:21 -04003362 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3363 path_to_nameidata(&path, nd);
3364 return -EEXIST;
3365 }
Al Virofb1cc552009-12-24 01:58:28 -05003366
Al Viro254cf582015-05-05 09:40:46 -04003367 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003368 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003369finish_lookup:
Al Viro8f64fb12016-11-14 01:50:26 -05003370 error = step_into(nd, &path, 0, inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003371 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003372 return error;
Al Virobc77daa2013-06-06 09:12:33 -04003373finish_open:
Al Viro8f64fb12016-11-14 01:50:26 -05003374 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Viroa3fbbde2011-11-07 21:21:26 +00003375 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003376 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003377 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003378 audit_inode(nd->name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003379 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003380 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003381 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003382 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003383 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003384 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003385 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003386 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003387
Al Viro0f9d1a12011-03-09 00:13:14 -05003388 if (will_truncate) {
3389 error = mnt_want_write(nd->path.mnt);
3390 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003391 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003392 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003393 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003394finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003395 error = may_open(&nd->path, acc_mode, open_flag);
3396 if (error)
3397 goto out;
Al Viroaad888f2018-06-08 12:58:04 -04003398 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
Al Viroae2bb292018-07-10 13:22:28 -04003399 error = vfs_open(&nd->path, file);
Al Virofac7d192016-06-04 11:41:49 -04003400 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003401 goto out;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003402opened:
Al Viro6035a272018-06-08 13:40:10 -04003403 error = ima_file_check(file, op->acc_mode);
Al Virofe9ec822016-04-27 03:14:20 -04003404 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003405 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003406out:
Al Viroc80567c2016-02-27 19:17:33 -05003407 if (unlikely(error > 0)) {
3408 WARN_ON(1);
3409 error = -EINVAL;
3410 }
Al Viro64894cf2012-07-31 00:53:35 +04003411 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003412 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003413 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003414}
3415
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003416struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3417{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003418 struct dentry *child = NULL;
3419 struct inode *dir = dentry->d_inode;
3420 struct inode *inode;
3421 int error;
3422
3423 /* we want directory to be writable */
3424 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3425 if (error)
3426 goto out_err;
3427 error = -EOPNOTSUPP;
3428 if (!dir->i_op->tmpfile)
3429 goto out_err;
3430 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003431 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003432 if (unlikely(!child))
3433 goto out_err;
3434 error = dir->i_op->tmpfile(dir, child, mode);
3435 if (error)
3436 goto out_err;
3437 error = -ENOENT;
3438 inode = child->d_inode;
3439 if (unlikely(!inode))
3440 goto out_err;
3441 if (!(open_flag & O_EXCL)) {
3442 spin_lock(&inode->i_lock);
3443 inode->i_state |= I_LINKABLE;
3444 spin_unlock(&inode->i_lock);
3445 }
3446 return child;
3447
3448out_err:
3449 dput(child);
3450 return ERR_PTR(error);
3451}
3452EXPORT_SYMBOL(vfs_tmpfile);
3453
Al Viroc8a53ee2015-05-12 18:43:07 -04003454static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003455 const struct open_flags *op,
3456 struct file *file, int *opened)
3457{
Al Viro625b6d12015-05-12 16:36:12 -04003458 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003459 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003460 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003461 if (unlikely(error))
3462 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003463 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003464 if (unlikely(error))
3465 goto out;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003466 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3467 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003468 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003469 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003470 dput(path.dentry);
3471 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003472 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003473 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003474 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003475 if (error)
3476 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003477 file->f_path.mnt = path.mnt;
Al Virobe12af32018-06-08 11:44:56 -04003478 error = finish_open(file, child, NULL);
Al Viro60545d02013-06-07 01:20:27 -04003479out2:
Al Viro625b6d12015-05-12 16:36:12 -04003480 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003481out:
Al Viro625b6d12015-05-12 16:36:12 -04003482 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003483 return error;
3484}
3485
Al Viro6ac08702016-04-26 00:02:50 -04003486static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3487{
3488 struct path path;
3489 int error = path_lookupat(nd, flags, &path);
3490 if (!error) {
3491 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003492 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003493 path_put(&path);
3494 }
3495 return error;
3496}
3497
Al Viroc8a53ee2015-05-12 18:43:07 -04003498static struct file *path_openat(struct nameidata *nd,
3499 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500{
Al Viro368ee9b2015-05-08 17:19:59 -04003501 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003502 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003503 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003504 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003505
Al Viroea73ea72018-07-11 15:00:04 -04003506 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003507 if (IS_ERR(file))
3508 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003509
Al Virobb458c62013-07-13 13:26:37 +04003510 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003511 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003512 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003513 }
3514
Al Viro6ac08702016-04-26 00:02:50 -04003515 if (unlikely(file->f_flags & O_PATH)) {
3516 error = do_o_path(nd, flags, file);
Al Viro6ac08702016-04-26 00:02:50 -04003517 goto out2;
3518 }
3519
Al Viroc8a53ee2015-05-12 18:43:07 -04003520 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003521 if (IS_ERR(s)) {
Al Viro4d27f322018-07-09 11:14:39 -04003522 fput(file);
Al Viro368ee9b2015-05-08 17:19:59 -04003523 return ERR_CAST(s);
3524 }
Al Viro3bdba282015-05-08 17:37:07 -04003525 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003526 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003527 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003528 s = trailing_symlink(nd);
3529 if (IS_ERR(s)) {
3530 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003531 break;
Al Viro3bdba282015-05-08 17:37:07 -04003532 }
Al Viro806b6812009-12-26 07:16:40 -05003533 }
Al Virodeb106c2015-05-08 18:05:21 -04003534 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003535out2:
Al Viro7c1c01e2018-06-08 12:56:55 -04003536 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003537 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003538 return file;
3539 WARN_ON(1);
3540 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003541 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003542 fput(file);
3543 if (error == -EOPENSTALE) {
3544 if (flags & LOOKUP_RCU)
3545 error = -ECHILD;
3546 else
3547 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003548 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003549 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550}
3551
Jeff Layton669abf42012-10-10 16:43:10 -04003552struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003553 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003554{
Al Viro9883d182015-05-13 07:28:08 -04003555 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003556 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003557 struct file *filp;
3558
Al Viro9883d182015-05-13 07:28:08 -04003559 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003560 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003561 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003562 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003563 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003564 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003565 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003566 return filp;
3567}
3568
Al Viro73d049a2011-03-11 12:08:24 -05003569struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003570 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003571{
Al Viro9883d182015-05-13 07:28:08 -04003572 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003573 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003574 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003575 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003576
3577 nd.root.mnt = mnt;
3578 nd.root.dentry = dentry;
3579
David Howellsb18825a2013-09-12 19:22:53 +01003580 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003581 return ERR_PTR(-ELOOP);
3582
Paul Moore51689102015-01-22 00:00:03 -05003583 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303584 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003585 return ERR_CAST(filename);
3586
Al Viro9883d182015-05-13 07:28:08 -04003587 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003588 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003589 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003590 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003591 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003592 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003593 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003594 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003595 return file;
3596}
3597
Al Virofa14a0b2015-01-22 02:16:49 -05003598static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003599 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003601 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003602 struct qstr last;
3603 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003604 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003605 int error;
3606 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3607
3608 /*
3609 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3610 * other flags passed in are ignored!
3611 */
3612 lookup_flags &= LOOKUP_REVAL;
3613
Al Viro5c31b6c2015-05-12 17:32:54 -04003614 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3615 if (IS_ERR(name))
3616 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003618 /*
3619 * Yucky last component or no last component at all?
3620 * (foo/., foo/.., /////)
3621 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003622 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003623 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003624
Jan Karac30dabf2012-06-12 16:20:30 +02003625 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003626 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003627 /*
3628 * Do the final lookup.
3629 */
Al Viro391172c2015-05-09 11:19:16 -04003630 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003631 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003632 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003634 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003635
Al Viroa8104a92012-07-20 02:25:00 +04003636 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003637 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003638 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003639
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003640 /*
3641 * Special case - lookup gave negative, but... we had foo/bar/
3642 * From the vfs_mknod() POV we just have a negative dentry -
3643 * all is fine. Let's be bastards - you had / on the end, you've
3644 * been asking for (non-existent) directory. -ENOENT for you.
3645 */
Al Viro391172c2015-05-09 11:19:16 -04003646 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003647 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003648 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003649 }
Jan Karac30dabf2012-06-12 16:20:30 +02003650 if (unlikely(err2)) {
3651 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003652 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003653 }
Al Viro181c37b2015-05-12 17:21:25 -04003654 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656fail:
Al Viroa8104a92012-07-20 02:25:00 +04003657 dput(dentry);
3658 dentry = ERR_PTR(error);
3659unlock:
Al Viro59551022016-01-22 15:40:57 -05003660 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003661 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003662 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003663out:
Al Viro391172c2015-05-09 11:19:16 -04003664 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003665 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 return dentry;
3667}
Al Virofa14a0b2015-01-22 02:16:49 -05003668
3669struct dentry *kern_path_create(int dfd, const char *pathname,
3670 struct path *path, unsigned int lookup_flags)
3671{
Al Viro181c37b2015-05-12 17:21:25 -04003672 return filename_create(dfd, getname_kernel(pathname),
3673 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003674}
Al Virodae6ad82011-06-26 11:50:15 -04003675EXPORT_SYMBOL(kern_path_create);
3676
Al Viro921a1652012-07-20 01:15:31 +04003677void done_path_create(struct path *path, struct dentry *dentry)
3678{
3679 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003680 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003681 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003682 path_put(path);
3683}
3684EXPORT_SYMBOL(done_path_create);
3685
Al Viro520ae682015-05-13 07:00:28 -04003686inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003687 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003688{
Al Viro181c37b2015-05-12 17:21:25 -04003689 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003690}
3691EXPORT_SYMBOL(user_path_create);
3692
Al Viro1a67aaf2011-07-26 01:52:52 -04003693int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003695 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
3697 if (error)
3698 return error;
3699
Eric W. Biederman55956b52018-05-23 15:24:18 -05003700 if ((S_ISCHR(mode) || S_ISBLK(mode)) &&
3701 !ns_capable(dentry->d_sb->s_user_ns, CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 return -EPERM;
3703
Al Viroacfa4382008-12-04 10:06:33 -05003704 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 return -EPERM;
3706
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003707 error = devcgroup_inode_mknod(mode, dev);
3708 if (error)
3709 return error;
3710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 error = security_inode_mknod(dir, dentry, mode, dev);
3712 if (error)
3713 return error;
3714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003716 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003717 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 return error;
3719}
Al Viro4d359502014-03-14 12:20:17 -04003720EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
Al Virof69aac02011-07-26 04:31:40 -04003722static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003723{
3724 switch (mode & S_IFMT) {
3725 case S_IFREG:
3726 case S_IFCHR:
3727 case S_IFBLK:
3728 case S_IFIFO:
3729 case S_IFSOCK:
3730 case 0: /* zero mode translates to S_IFREG */
3731 return 0;
3732 case S_IFDIR:
3733 return -EPERM;
3734 default:
3735 return -EINVAL;
3736 }
3737}
3738
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003739long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3740 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741{
Al Viro2ad94ae2008-07-21 09:32:51 -04003742 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003743 struct path path;
3744 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003745 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
Al Viro8e4bfca2012-07-20 01:17:26 +04003747 error = may_mknod(mode);
3748 if (error)
3749 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003750retry:
3751 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003752 if (IS_ERR(dentry))
3753 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003754
Al Virodae6ad82011-06-26 11:50:15 -04003755 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003756 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003757 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003758 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003759 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003760 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003762 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003763 if (!error)
3764 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 break;
3766 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003767 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 new_decode_dev(dev));
3769 break;
3770 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003771 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 }
Al Viroa8104a92012-07-20 02:25:00 +04003774out:
Al Viro921a1652012-07-20 01:15:31 +04003775 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003776 if (retry_estale(error, lookup_flags)) {
3777 lookup_flags |= LOOKUP_REVAL;
3778 goto retry;
3779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780 return error;
3781}
3782
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003783SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3784 unsigned int, dev)
3785{
3786 return do_mknodat(dfd, filename, mode, dev);
3787}
3788
Al Viro8208a222011-07-25 17:32:17 -04003789SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003790{
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003791 return do_mknodat(AT_FDCWD, filename, mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003792}
3793
Al Viro18bb1db2011-07-26 01:41:39 -04003794int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003796 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003797 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798
3799 if (error)
3800 return error;
3801
Al Viroacfa4382008-12-04 10:06:33 -05003802 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 return -EPERM;
3804
3805 mode &= (S_IRWXUGO|S_ISVTX);
3806 error = security_inode_mkdir(dir, dentry, mode);
3807 if (error)
3808 return error;
3809
Al Viro8de52772012-02-06 12:45:27 -05003810 if (max_links && dir->i_nlink >= max_links)
3811 return -EMLINK;
3812
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003814 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003815 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 return error;
3817}
Al Viro4d359502014-03-14 12:20:17 -04003818EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Dominik Brodowski0101db72018-03-11 11:34:49 +01003820long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Dave Hansen6902d922006-09-30 23:29:01 -07003822 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003823 struct path path;
3824 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003825 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003827retry:
3828 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003829 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003830 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003831
Al Virodae6ad82011-06-26 11:50:15 -04003832 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003833 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003834 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003835 if (!error)
3836 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003837 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003838 if (retry_estale(error, lookup_flags)) {
3839 lookup_flags |= LOOKUP_REVAL;
3840 goto retry;
3841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 return error;
3843}
3844
Dominik Brodowski0101db72018-03-11 11:34:49 +01003845SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3846{
3847 return do_mkdirat(dfd, pathname, mode);
3848}
3849
Al Viroa218d0f2011-11-21 14:59:34 -05003850SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003851{
Dominik Brodowski0101db72018-03-11 11:34:49 +01003852 return do_mkdirat(AT_FDCWD, pathname, mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003853}
3854
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3856{
3857 int error = may_delete(dir, dentry, 1);
3858
3859 if (error)
3860 return error;
3861
Al Viroacfa4382008-12-04 10:06:33 -05003862 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return -EPERM;
3864
Al Viro1d2ef592011-09-14 18:55:41 +01003865 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003866 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
Sage Weil912dbc12011-05-24 13:06:11 -07003868 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003869 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003870 goto out;
3871
3872 error = security_inode_rmdir(dir, dentry);
3873 if (error)
3874 goto out;
3875
3876 error = dir->i_op->rmdir(dir, dentry);
3877 if (error)
3878 goto out;
3879
Al Viro87677122018-05-27 16:23:51 -04003880 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003881 dentry->d_inode->i_flags |= S_DEAD;
3882 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003883 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003884
3885out:
Al Viro59551022016-01-22 15:40:57 -05003886 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003887 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003888 if (!error)
3889 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return error;
3891}
Al Viro4d359502014-03-14 12:20:17 -04003892EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893
Dominik Brodowskif459dffa2018-03-11 11:34:48 +01003894long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895{
3896 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003897 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003899 struct path path;
3900 struct qstr last;
3901 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003902 unsigned int lookup_flags = 0;
3903retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04003904 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3905 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003906 if (IS_ERR(name))
3907 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Al Virof5beed72015-04-30 16:09:11 -04003909 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003910 case LAST_DOTDOT:
3911 error = -ENOTEMPTY;
3912 goto exit1;
3913 case LAST_DOT:
3914 error = -EINVAL;
3915 goto exit1;
3916 case LAST_ROOT:
3917 error = -EBUSY;
3918 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003920
Al Virof5beed72015-04-30 16:09:11 -04003921 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003922 if (error)
3923 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003924
Al Viro59551022016-01-22 15:40:57 -05003925 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003926 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003928 if (IS_ERR(dentry))
3929 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003930 if (!dentry->d_inode) {
3931 error = -ENOENT;
3932 goto exit3;
3933 }
Al Virof5beed72015-04-30 16:09:11 -04003934 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003935 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003936 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003937 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003938exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003939 dput(dentry);
3940exit2:
Al Viro59551022016-01-22 15:40:57 -05003941 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003942 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943exit1:
Al Virof5beed72015-04-30 16:09:11 -04003944 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003946 if (retry_estale(error, lookup_flags)) {
3947 lookup_flags |= LOOKUP_REVAL;
3948 goto retry;
3949 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 return error;
3951}
3952
Heiko Carstens3cdad422009-01-14 14:14:22 +01003953SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003954{
3955 return do_rmdir(AT_FDCWD, pathname);
3956}
3957
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003958/**
3959 * vfs_unlink - unlink a filesystem object
3960 * @dir: parent directory
3961 * @dentry: victim
3962 * @delegated_inode: returns victim inode, if the inode is delegated.
3963 *
3964 * The caller must hold dir->i_mutex.
3965 *
3966 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3967 * return a reference to the inode in delegated_inode. The caller
3968 * should then break the delegation on that inode and retry. Because
3969 * breaking a delegation may take a long time, the caller should drop
3970 * dir->i_mutex before doing so.
3971 *
3972 * Alternatively, a caller may pass NULL for delegated_inode. This may
3973 * be appropriate for callers that expect the underlying filesystem not
3974 * to be NFS exported.
3975 */
3976int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003978 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 int error = may_delete(dir, dentry, 0);
3980
3981 if (error)
3982 return error;
3983
Al Viroacfa4382008-12-04 10:06:33 -05003984 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 return -EPERM;
3986
Al Viro59551022016-01-22 15:40:57 -05003987 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003988 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 error = -EBUSY;
3990 else {
3991 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003992 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003993 error = try_break_deleg(target, delegated_inode);
3994 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003995 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003997 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003998 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003999 detach_mounts(dentry);
4000 }
Al Virobec10522010-03-03 14:12:08 -05004001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004003out:
Al Viro59551022016-01-22 15:40:57 -05004004 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
4006 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4007 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004008 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 }
Robert Love0eeca282005-07-12 17:06:03 -04004011
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 return error;
4013}
Al Viro4d359502014-03-14 12:20:17 -04004014EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
4016/*
4017 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004018 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 * writeout happening, and we don't want to prevent access to the directory
4020 * while waiting on the I/O.
4021 */
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004022long do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Al Viro2ad94ae2008-07-21 09:32:51 -04004024 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004026 struct path path;
4027 struct qstr last;
4028 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004030 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004031 unsigned int lookup_flags = 0;
4032retry:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004033 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004034 if (IS_ERR(name))
4035 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004038 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004040
Al Virof5beed72015-04-30 16:09:11 -04004041 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004042 if (error)
4043 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004044retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004045 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004046 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 error = PTR_ERR(dentry);
4048 if (!IS_ERR(dentry)) {
4049 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004050 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004051 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004053 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004054 goto slashes;
4055 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004056 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004057 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004058 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04004059 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004060exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 dput(dentry);
4062 }
Al Viro59551022016-01-22 15:40:57 -05004063 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 if (inode)
4065 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004066 inode = NULL;
4067 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004068 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004069 if (!error)
4070 goto retry_deleg;
4071 }
Al Virof5beed72015-04-30 16:09:11 -04004072 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073exit1:
Al Virof5beed72015-04-30 16:09:11 -04004074 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004075 if (retry_estale(error, lookup_flags)) {
4076 lookup_flags |= LOOKUP_REVAL;
4077 inode = NULL;
4078 goto retry;
4079 }
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004080 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 return error;
4082
4083slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004084 if (d_is_negative(dentry))
4085 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004086 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004087 error = -EISDIR;
4088 else
4089 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 goto exit2;
4091}
4092
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004093SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004094{
4095 if ((flag & ~AT_REMOVEDIR) != 0)
4096 return -EINVAL;
4097
4098 if (flag & AT_REMOVEDIR)
4099 return do_rmdir(dfd, pathname);
4100
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004101 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004102}
4103
Heiko Carstens3480b252009-01-14 14:14:16 +01004104SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004105{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004106 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004107}
4108
Miklos Szeredidb2e7472008-06-24 16:50:16 +02004109int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Miklos Szeredia95164d2008-07-30 15:08:48 +02004111 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
4113 if (error)
4114 return error;
4115
Al Viroacfa4382008-12-04 10:06:33 -05004116 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 return -EPERM;
4118
4119 error = security_inode_symlink(dir, dentry, oldname);
4120 if (error)
4121 return error;
4122
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004124 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004125 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 return error;
4127}
Al Viro4d359502014-03-14 12:20:17 -04004128EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Dominik Brodowskib724e842018-03-11 11:34:49 +01004130long do_symlinkat(const char __user *oldname, int newdfd,
4131 const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132{
Al Viro2ad94ae2008-07-21 09:32:51 -04004133 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004134 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004135 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004136 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004137 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138
4139 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004140 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004142retry:
4143 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004144 error = PTR_ERR(dentry);
4145 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004146 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004147
Jeff Layton91a27b22012-10-10 15:25:28 -04004148 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004149 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04004150 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004151 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004152 if (retry_estale(error, lookup_flags)) {
4153 lookup_flags |= LOOKUP_REVAL;
4154 goto retry;
4155 }
Dave Hansen6902d922006-09-30 23:29:01 -07004156out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 putname(from);
4158 return error;
4159}
4160
Dominik Brodowskib724e842018-03-11 11:34:49 +01004161SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4162 int, newdfd, const char __user *, newname)
4163{
4164 return do_symlinkat(oldname, newdfd, newname);
4165}
4166
Heiko Carstens3480b252009-01-14 14:14:16 +01004167SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004168{
Dominik Brodowskib724e842018-03-11 11:34:49 +01004169 return do_symlinkat(oldname, AT_FDCWD, newname);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004170}
4171
J. Bruce Fields146a8592011-09-20 17:14:31 -04004172/**
4173 * vfs_link - create a new link
4174 * @old_dentry: object to be linked
4175 * @dir: new parent
4176 * @new_dentry: where to create the new link
4177 * @delegated_inode: returns inode needing a delegation break
4178 *
4179 * The caller must hold dir->i_mutex
4180 *
4181 * If vfs_link discovers a delegation on the to-be-linked file in need
4182 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4183 * inode in delegated_inode. The caller should then break the delegation
4184 * and retry. Because breaking a delegation may take a long time, the
4185 * caller should drop the i_mutex before doing so.
4186 *
4187 * Alternatively, a caller may pass NULL for delegated_inode. This may
4188 * be appropriate for callers that expect the underlying filesystem not
4189 * to be NFS exported.
4190 */
4191int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192{
4193 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004194 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 int error;
4196
4197 if (!inode)
4198 return -ENOENT;
4199
Miklos Szeredia95164d2008-07-30 15:08:48 +02004200 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 if (error)
4202 return error;
4203
4204 if (dir->i_sb != inode->i_sb)
4205 return -EXDEV;
4206
4207 /*
4208 * A link to an append-only or immutable file cannot be created.
4209 */
4210 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4211 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004212 /*
4213 * Updating the link count will likely cause i_uid and i_gid to
4214 * be writen back improperly if their true value is unknown to
4215 * the vfs.
4216 */
4217 if (HAS_UNMAPPED_ID(inode))
4218 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004219 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004221 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 return -EPERM;
4223
4224 error = security_inode_link(old_dentry, dir, new_dentry);
4225 if (error)
4226 return error;
4227
Al Viro59551022016-01-22 15:40:57 -05004228 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304229 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004230 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304231 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004232 else if (max_links && inode->i_nlink >= max_links)
4233 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004234 else {
4235 error = try_break_deleg(inode, delegated_inode);
4236 if (!error)
4237 error = dir->i_op->link(old_dentry, dir, new_dentry);
4238 }
Al Virof4e0c302013-06-11 08:34:36 +04004239
4240 if (!error && (inode->i_state & I_LINKABLE)) {
4241 spin_lock(&inode->i_lock);
4242 inode->i_state &= ~I_LINKABLE;
4243 spin_unlock(&inode->i_lock);
4244 }
Al Viro59551022016-01-22 15:40:57 -05004245 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004246 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004247 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return error;
4249}
Al Viro4d359502014-03-14 12:20:17 -04004250EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
4252/*
4253 * Hardlinks are often used in delicate situations. We avoid
4254 * security-related surprises by not following symlinks on the
4255 * newname. --KAB
4256 *
4257 * We don't follow them on the oldname either to be compatible
4258 * with linux 2.0, and to avoid hard-linking to directories
4259 * and other special files. --ADM
4260 */
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004261int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4262 const char __user *newname, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004265 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004266 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304267 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304270 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004271 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304272 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004273 * To use null names we require CAP_DAC_READ_SEARCH
4274 * This ensures that not everyone will be able to create
4275 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304276 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004277 if (flags & AT_EMPTY_PATH) {
4278 if (!capable(CAP_DAC_READ_SEARCH))
4279 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304280 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004281 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004282
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304283 if (flags & AT_SYMLINK_FOLLOW)
4284 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004285retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304286 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004288 return error;
4289
Jeff Layton442e31c2012-12-20 16:15:38 -05004290 new_dentry = user_path_create(newdfd, newname, &new_path,
4291 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004293 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004294 goto out;
4295
4296 error = -EXDEV;
4297 if (old_path.mnt != new_path.mnt)
4298 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004299 error = may_linkat(&old_path);
4300 if (unlikely(error))
4301 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004302 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004303 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004304 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004305 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004306out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004307 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004308 if (delegated_inode) {
4309 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004310 if (!error) {
4311 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004312 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004313 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004314 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004315 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004316 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004317 how |= LOOKUP_REVAL;
4318 goto retry;
4319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320out:
Al Viro2d8f3032008-07-22 09:59:21 -04004321 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
4323 return error;
4324}
4325
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004326SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4327 int, newdfd, const char __user *, newname, int, flags)
4328{
4329 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4330}
4331
Heiko Carstens3480b252009-01-14 14:14:16 +01004332SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004333{
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004334 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004335}
4336
Miklos Szeredibc270272014-04-01 17:08:42 +02004337/**
4338 * vfs_rename - rename a filesystem object
4339 * @old_dir: parent of source
4340 * @old_dentry: source
4341 * @new_dir: parent of destination
4342 * @new_dentry: destination
4343 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004344 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004345 *
4346 * The caller must hold multiple mutexes--see lock_rename()).
4347 *
4348 * If vfs_rename discovers a delegation in need of breaking at either
4349 * the source or destination, it will return -EWOULDBLOCK and return a
4350 * reference to the inode in delegated_inode. The caller should then
4351 * break the delegation and retry. Because breaking a delegation may
4352 * take a long time, the caller should drop all locks before doing
4353 * so.
4354 *
4355 * Alternatively, a caller may pass NULL for delegated_inode. This may
4356 * be appropriate for callers that expect the underlying filesystem not
4357 * to be NFS exported.
4358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 * The worst of all namespace operations - renaming directory. "Perverted"
4360 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4361 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004362 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004363 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 * b) race potential - two innocent renames can create a loop together.
4365 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004366 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004368 * c) we have to lock _four_ objects - parents and victim (if it exists),
4369 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004370 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 * whether the target exists). Solution: try to be smart with locking
4372 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004373 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 * move will be locked. Thus we can rank directories by the tree
4375 * (ancestors first) and rank all non-directories after them.
4376 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004377 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 * HOWEVER, it relies on the assumption that any object with ->lookup()
4379 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4380 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004381 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004382 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004384 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 * locking].
4386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004388 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004389 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390{
4391 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004392 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004393 struct inode *source = old_dentry->d_inode;
4394 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004395 bool new_is_dir = false;
4396 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004397 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004399 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004400 return 0;
4401
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402 error = may_delete(old_dir, old_dentry, is_dir);
4403 if (error)
4404 return error;
4405
Miklos Szeredida1ce062014-04-01 17:08:43 +02004406 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004407 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004408 } else {
4409 new_is_dir = d_is_dir(new_dentry);
4410
4411 if (!(flags & RENAME_EXCHANGE))
4412 error = may_delete(new_dir, new_dentry, is_dir);
4413 else
4414 error = may_delete(new_dir, new_dentry, new_is_dir);
4415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 if (error)
4417 return error;
4418
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004419 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 return -EPERM;
4421
Miklos Szeredibc270272014-04-01 17:08:42 +02004422 /*
4423 * If we are going to change the parent - check write permissions,
4424 * we'll need to flip '..'.
4425 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004426 if (new_dir != old_dir) {
4427 if (is_dir) {
4428 error = inode_permission(source, MAY_WRITE);
4429 if (error)
4430 return error;
4431 }
4432 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4433 error = inode_permission(target, MAY_WRITE);
4434 if (error)
4435 return error;
4436 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004437 }
Robert Love0eeca282005-07-12 17:06:03 -04004438
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004439 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4440 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004441 if (error)
4442 return error;
4443
Al Viro49d31c22017-07-07 14:51:19 -04004444 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004445 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004446 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004447 lock_two_nondirectories(source, target);
4448 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004449 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004450
4451 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004452 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004453 goto out;
4454
Miklos Szeredida1ce062014-04-01 17:08:43 +02004455 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004456 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004457 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004458 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004459 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4460 old_dir->i_nlink >= max_links)
4461 goto out;
4462 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004463 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004464 error = try_break_deleg(source, delegated_inode);
4465 if (error)
4466 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004467 }
4468 if (target && !new_is_dir) {
4469 error = try_break_deleg(target, delegated_inode);
4470 if (error)
4471 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004472 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004473 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004474 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004475 if (error)
4476 goto out;
4477
Miklos Szeredida1ce062014-04-01 17:08:43 +02004478 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004479 if (is_dir) {
4480 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004481 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004482 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004483 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004484 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004485 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004486 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4487 if (!(flags & RENAME_EXCHANGE))
4488 d_move(old_dentry, new_dentry);
4489 else
4490 d_exchange(old_dentry, new_dentry);
4491 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004492out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004493 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004494 unlock_two_nondirectories(source, target);
4495 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004496 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004497 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004498 if (!error) {
Al Viro49d31c22017-07-07 14:51:19 -04004499 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004500 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4501 if (flags & RENAME_EXCHANGE) {
4502 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4503 new_is_dir, NULL, new_dentry);
4504 }
4505 }
Al Viro49d31c22017-07-07 14:51:19 -04004506 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004507
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 return error;
4509}
Al Viro4d359502014-03-14 12:20:17 -04004510EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004512static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4513 const char __user *newname, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
Al Viro2ad94ae2008-07-21 09:32:51 -04004515 struct dentry *old_dentry, *new_dentry;
4516 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004517 struct path old_path, new_path;
4518 struct qstr old_last, new_last;
4519 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004520 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004521 struct filename *from;
4522 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004523 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004524 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004525 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004526
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004527 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004528 return -EINVAL;
4529
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004530 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4531 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004532 return -EINVAL;
4533
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004534 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4535 return -EPERM;
4536
Al Virof5beed72015-04-30 16:09:11 -04004537 if (flags & RENAME_EXCHANGE)
4538 target_flags = 0;
4539
Jeff Laytonc6a94282012-12-11 12:10:10 -05004540retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04004541 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4542 &old_path, &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004543 if (IS_ERR(from)) {
4544 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
Al Viroc1d4dd22016-06-05 16:38:18 -04004548 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4549 &new_path, &new_last, &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004550 if (IS_ERR(to)) {
4551 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
4555 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004556 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 goto exit2;
4558
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004560 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 goto exit2;
4562
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004563 if (flags & RENAME_NOREPLACE)
4564 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004565 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 goto exit2;
4567
Al Virof5beed72015-04-30 16:09:11 -04004568 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004569 if (error)
4570 goto exit2;
4571
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004572retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004573 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Al Virof5beed72015-04-30 16:09:11 -04004575 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 error = PTR_ERR(old_dentry);
4577 if (IS_ERR(old_dentry))
4578 goto exit3;
4579 /* source must exist */
4580 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004581 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004583 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 error = PTR_ERR(new_dentry);
4585 if (IS_ERR(new_dentry))
4586 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004587 error = -EEXIST;
4588 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4589 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004590 if (flags & RENAME_EXCHANGE) {
4591 error = -ENOENT;
4592 if (d_is_negative(new_dentry))
4593 goto exit5;
4594
4595 if (!d_is_dir(new_dentry)) {
4596 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004597 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004598 goto exit5;
4599 }
4600 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004601 /* unless the source is a directory trailing slashes give -ENOTDIR */
4602 if (!d_is_dir(old_dentry)) {
4603 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004604 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004605 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004606 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004607 goto exit5;
4608 }
4609 /* source should not be ancestor of target */
4610 error = -EINVAL;
4611 if (old_dentry == trap)
4612 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004614 if (!(flags & RENAME_EXCHANGE))
4615 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 if (new_dentry == trap)
4617 goto exit5;
4618
Al Virof5beed72015-04-30 16:09:11 -04004619 error = security_path_rename(&old_path, old_dentry,
4620 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004621 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004622 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004623 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4624 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004625 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626exit5:
4627 dput(new_dentry);
4628exit4:
4629 dput(old_dentry);
4630exit3:
Al Virof5beed72015-04-30 16:09:11 -04004631 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004632 if (delegated_inode) {
4633 error = break_deleg_wait(&delegated_inode);
4634 if (!error)
4635 goto retry_deleg;
4636 }
Al Virof5beed72015-04-30 16:09:11 -04004637 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004639 if (retry_estale(error, lookup_flags))
4640 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004641 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004642 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643exit1:
Al Virof5beed72015-04-30 16:09:11 -04004644 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004646 if (should_retry) {
4647 should_retry = false;
4648 lookup_flags |= LOOKUP_REVAL;
4649 goto retry;
4650 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004651exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 return error;
4653}
4654
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004655SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4656 int, newdfd, const char __user *, newname, unsigned int, flags)
4657{
4658 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4659}
4660
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004661SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4662 int, newdfd, const char __user *, newname)
4663{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004664 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004665}
4666
Heiko Carstensa26eab22009-01-14 14:14:17 +01004667SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004668{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004669 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004670}
4671
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004672int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4673{
4674 int error = may_create(dir, dentry);
4675 if (error)
4676 return error;
4677
4678 if (!dir->i_op->mknod)
4679 return -EPERM;
4680
4681 return dir->i_op->mknod(dir, dentry,
4682 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4683}
4684EXPORT_SYMBOL(vfs_whiteout);
4685
Al Viro5d826c82014-03-14 13:42:45 -04004686int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
Al Viro5d826c82014-03-14 13:42:45 -04004688 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 if (IS_ERR(link))
4690 goto out;
4691
4692 len = strlen(link);
4693 if (len > (unsigned) buflen)
4694 len = buflen;
4695 if (copy_to_user(buffer, link, len))
4696 len = -EFAULT;
4697out:
4698 return len;
4699}
4700
4701/*
4702 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
Al Viro6b255392015-11-17 10:20:54 -05004703 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4704 * for any given inode is up to filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 */
Miklos Szeredid16744e2016-12-09 16:45:04 +01004706static int generic_readlink(struct dentry *dentry, char __user *buffer,
4707 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
Al Virofceef392015-12-29 15:58:39 -05004709 DEFINE_DELAYED_CALL(done);
Al Viro5f2c4172015-05-07 11:14:26 -04004710 struct inode *inode = d_inode(dentry);
4711 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004712 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004713
Al Virod4dee482015-04-30 20:08:02 -04004714 if (!link) {
Al Virofceef392015-12-29 15:58:39 -05004715 link = inode->i_op->get_link(dentry, inode, &done);
Al Virod4dee482015-04-30 20:08:02 -04004716 if (IS_ERR(link))
4717 return PTR_ERR(link);
4718 }
Al Viro680baac2015-05-02 13:32:22 -04004719 res = readlink_copy(buffer, buflen, link);
Al Virofceef392015-12-29 15:58:39 -05004720 do_delayed_call(&done);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004721 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
Miklos Szeredid60874c2016-10-04 14:40:45 +02004724/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004725 * vfs_readlink - copy symlink body into userspace buffer
4726 * @dentry: dentry on which to get symbolic link
4727 * @buffer: user memory pointer
4728 * @buflen: size of buffer
4729 *
4730 * Does not touch atime. That's up to the caller if necessary
4731 *
4732 * Does not call security hook.
4733 */
4734int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4735{
4736 struct inode *inode = d_inode(dentry);
4737
Miklos Szeredi76fca902016-12-09 16:45:04 +01004738 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4739 if (unlikely(inode->i_op->readlink))
4740 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004741
Miklos Szeredi76fca902016-12-09 16:45:04 +01004742 if (!d_is_symlink(dentry))
4743 return -EINVAL;
4744
4745 spin_lock(&inode->i_lock);
4746 inode->i_opflags |= IOP_DEFAULT_READLINK;
4747 spin_unlock(&inode->i_lock);
4748 }
4749
4750 return generic_readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004751}
4752EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753
Miklos Szeredid60874c2016-10-04 14:40:45 +02004754/**
4755 * vfs_get_link - get symlink body
4756 * @dentry: dentry on which to get symbolic link
4757 * @done: caller needs to free returned data with this
4758 *
4759 * Calls security hook and i_op->get_link() on the supplied inode.
4760 *
4761 * It does not touch atime. That's up to the caller if necessary.
4762 *
4763 * Does not work on "special" symlinks like /proc/$$/fd/N
4764 */
4765const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4766{
4767 const char *res = ERR_PTR(-EINVAL);
4768 struct inode *inode = d_inode(dentry);
4769
4770 if (d_is_symlink(dentry)) {
4771 res = ERR_PTR(security_inode_readlink(dentry));
4772 if (!res)
4773 res = inode->i_op->get_link(dentry, inode, done);
4774 }
4775 return res;
4776}
4777EXPORT_SYMBOL(vfs_get_link);
4778
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004780const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004781 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004783 char *kaddr;
4784 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004785 struct address_space *mapping = inode->i_mapping;
4786
Al Virod3883d42015-11-17 10:41:04 -05004787 if (!dentry) {
4788 page = find_get_page(mapping, 0);
4789 if (!page)
4790 return ERR_PTR(-ECHILD);
4791 if (!PageUptodate(page)) {
4792 put_page(page);
4793 return ERR_PTR(-ECHILD);
4794 }
4795 } else {
4796 page = read_mapping_page(mapping, 0, NULL);
4797 if (IS_ERR(page))
4798 return (char*)page;
4799 }
Al Virofceef392015-12-29 15:58:39 -05004800 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004801 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4802 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004803 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004804 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805}
4806
Al Viro6b255392015-11-17 10:20:54 -05004807EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Al Virofceef392015-12-29 15:58:39 -05004809void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004810{
Al Virofceef392015-12-29 15:58:39 -05004811 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004812}
Al Viro4d359502014-03-14 12:20:17 -04004813EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814
Al Viroaa80dea2015-11-16 18:26:34 -05004815int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4816{
Al Virofceef392015-12-29 15:58:39 -05004817 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004818 int res = readlink_copy(buffer, buflen,
4819 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004820 &done));
4821 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004822 return res;
4823}
4824EXPORT_SYMBOL(page_readlink);
4825
Nick Piggin54566b22009-01-04 12:00:53 -08004826/*
4827 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4828 */
4829int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830{
4831 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004832 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004833 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004834 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004835 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004836 if (nofs)
4837 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
NeilBrown7e53cac2006-03-25 03:07:57 -08004839retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004840 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004841 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004843 goto fail;
4844
Al Viro21fc61c2015-11-17 01:07:57 -05004845 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004846
4847 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4848 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 if (err < 0)
4850 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004851 if (err < len-1)
4852 goto retry;
4853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 mark_inode_dirty(inode);
4855 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856fail:
4857 return err;
4858}
Al Viro4d359502014-03-14 12:20:17 -04004859EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004861int page_symlink(struct inode *inode, const char *symname, int len)
4862{
4863 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004864 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004865}
Al Viro4d359502014-03-14 12:20:17 -04004866EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004867
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004868const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004869 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871EXPORT_SYMBOL(page_symlink_inode_operations);