blob: 327844fedf3d2e44bca1f5e986442f3993fb3e39 [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 Viro6e771372015-05-02 13:37:52 -0400870void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400871{
Al Viro6e771372015-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;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -0700890int sysctl_protected_fifos __read_mostly;
891int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -0700892
893/**
894 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700895 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700896 *
897 * In the case of the sysctl_protected_symlinks sysctl being enabled,
898 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
899 * in a sticky world-writable directory. This is to protect privileged
900 * processes from failing races against path names that may change out
901 * from under them by way of other users creating malicious symlinks.
902 * It will permit symlinks to be followed only when outside a sticky
903 * world-writable directory, or when the uid of the symlink and follower
904 * match, or when the directory owner matches the symlink's owner.
905 *
906 * Returns 0 if following the symlink is allowed, -ve on error.
907 */
Al Virofec2fa22015-05-06 15:58:18 -0400908static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700909{
910 const struct inode *inode;
911 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500912 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700913
914 if (!sysctl_protected_symlinks)
915 return 0;
916
917 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500918 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700919 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700920 return 0;
921
922 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400923 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700924 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
925 return 0;
926
927 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500928 puid = parent->i_uid;
929 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700930 return 0;
931
Al Viro319565022015-05-07 20:37:40 -0400932 if (nd->flags & LOOKUP_RCU)
933 return -ECHILD;
934
Richard Guy Briggsea841ba2018-03-21 04:42:21 -0400935 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Richard Guy Briggs94b9d9b2018-03-21 04:42:20 -0400936 audit_log_link_denied("follow_link");
Kees Cook800179c2012-07-25 17:29:07 -0700937 return -EACCES;
938}
939
940/**
941 * safe_hardlink_source - Check for safe hardlink conditions
942 * @inode: the source inode to hardlink from
943 *
944 * Return false if at least one of the following conditions:
945 * - inode is not a regular file
946 * - inode is setuid
947 * - inode is setgid and group-exec
948 * - access failure for read and write
949 *
950 * Otherwise returns true.
951 */
952static bool safe_hardlink_source(struct inode *inode)
953{
954 umode_t mode = inode->i_mode;
955
956 /* Special files should not get pinned to the filesystem. */
957 if (!S_ISREG(mode))
958 return false;
959
960 /* Setuid files should not get pinned to the filesystem. */
961 if (mode & S_ISUID)
962 return false;
963
964 /* Executable setgid files should not get pinned to the filesystem. */
965 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
966 return false;
967
968 /* Hardlinking to unreadable or unwritable sources is dangerous. */
969 if (inode_permission(inode, MAY_READ | MAY_WRITE))
970 return false;
971
972 return true;
973}
974
975/**
976 * may_linkat - Check permissions for creating a hardlink
977 * @link: the source to hardlink from
978 *
979 * Block hardlink when all of:
980 * - sysctl_protected_hardlinks enabled
981 * - fsuid does not match inode
982 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200983 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700984 *
985 * Returns 0 if successful, -ve on error.
986 */
987static int may_linkat(struct path *link)
988{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -0500989 struct inode *inode = link->dentry->d_inode;
990
991 /* Inode writeback is not safe when the uid or gid are invalid. */
992 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
993 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -0700994
995 if (!sysctl_protected_hardlinks)
996 return 0;
997
Kees Cook800179c2012-07-25 17:29:07 -0700998 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
999 * otherwise, it must be a safe source.
1000 */
Kees Cookcc658db2017-06-21 09:53:06 -07001001 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
Kees Cook800179c2012-07-25 17:29:07 -07001002 return 0;
1003
Richard Guy Briggs94b9d9b2018-03-21 04:42:20 -04001004 audit_log_link_denied("linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001005 return -EPERM;
1006}
1007
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001008/**
1009 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1010 * should be allowed, or not, on files that already
1011 * exist.
Al Viro752f72e2020-01-26 09:29:34 -05001012 * @dir_mode: mode bits of directory
1013 * @dir_uid: owner of directory
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001014 * @inode: the inode of the file to open
1015 *
1016 * Block an O_CREAT open of a FIFO (or a regular file) when:
1017 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1018 * - the file already exists
1019 * - we are in a sticky directory
1020 * - we don't own the file
1021 * - the owner of the directory doesn't own the file
1022 * - the directory is world writable
1023 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1024 * the directory doesn't have to be world writable: being group writable will
1025 * be enough.
1026 *
1027 * Returns 0 if the open is allowed, -ve on error.
1028 */
Al Viro752f72e2020-01-26 09:29:34 -05001029static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001030 struct inode * const inode)
1031{
1032 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1033 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Viro752f72e2020-01-26 09:29:34 -05001034 likely(!(dir_mode & S_ISVTX)) ||
1035 uid_eq(inode->i_uid, dir_uid) ||
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001036 uid_eq(current_fsuid(), inode->i_uid))
1037 return 0;
1038
Al Viro752f72e2020-01-26 09:29:34 -05001039 if (likely(dir_mode & 0002) ||
1040 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001041 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1042 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
1043 return -EACCES;
1044 }
1045 return 0;
1046}
1047
Al Viro3b2e7f72015-04-19 00:53:50 -04001048static __always_inline
1049const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001050{
Al Viroab104922015-05-10 11:50:01 -04001051 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001052 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -05001053 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001054 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001055 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001056
NeilBrown8fa9dd22015-03-23 13:37:40 +11001057 if (!(nd->flags & LOOKUP_RCU)) {
1058 touch_atime(&last->link);
1059 cond_resched();
Miklos Szeredic6718542018-07-18 15:44:43 +02001060 } else if (atime_needs_update(&last->link, inode)) {
Al Viro4675ac32017-01-09 22:29:15 -05001061 if (unlikely(unlazy_walk(nd)))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001062 return ERR_PTR(-ECHILD);
1063 touch_atime(&last->link);
1064 }
1065
NeilBrownbda0be72015-03-23 13:37:39 +11001066 error = security_inode_follow_link(dentry, inode,
1067 nd->flags & LOOKUP_RCU);
1068 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001069 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001070
Al Viro86acdca12009-12-22 23:45:11 -05001071 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001072 res = inode->i_link;
1073 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001074 const char * (*get)(struct dentry *, struct inode *,
1075 struct delayed_call *);
1076 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001077 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001078 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001079 if (res == ERR_PTR(-ECHILD)) {
Al Viro4675ac32017-01-09 22:29:15 -05001080 if (unlikely(unlazy_walk(nd)))
Al Viro6b255392015-11-17 10:20:54 -05001081 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001082 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001083 }
1084 } else {
Al Virofceef392015-12-29 15:58:39 -05001085 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001086 }
Al Virofceef392015-12-29 15:58:39 -05001087 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001088 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001089 }
Al Virofab51e82015-05-10 11:01:00 -04001090 if (*res == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05001091 if (!nd->root.mnt)
1092 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05001093 if (unlikely(nd_jump_root(nd)))
1094 return ERR_PTR(-ECHILD);
Al Virofab51e82015-05-10 11:01:00 -04001095 while (unlikely(*++res == '/'))
1096 ;
1097 }
1098 if (!*res)
1099 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001100 return res;
1101}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001102
David Howellsf015f1262012-06-25 12:55:28 +01001103/*
1104 * follow_up - Find the mountpoint of path's vfsmount
1105 *
1106 * Given a path, find the mountpoint of its source file system.
1107 * Replace @path with the path of the mountpoint in the parent mount.
1108 * Up is towards /.
1109 *
1110 * Return 1 if we went up a level and 0 if we were already at the
1111 * root.
1112 */
Al Virobab77eb2009-04-18 03:26:48 -04001113int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Al Viro0714a532011-11-24 22:19:58 -05001115 struct mount *mnt = real_mount(path->mnt);
1116 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001118
Al Viro48a066e2013-09-29 22:06:07 -04001119 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001120 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001121 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001122 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return 0;
1124 }
Al Viro0714a532011-11-24 22:19:58 -05001125 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001126 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001127 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001128 dput(path->dentry);
1129 path->dentry = mountpoint;
1130 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001131 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 return 1;
1133}
Al Viro4d359502014-03-14 12:20:17 -04001134EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001136/*
David Howells9875cf82011-01-14 18:45:21 +00001137 * Perform an automount
1138 * - return -EISDIR to tell follow_managed() to stop and return the path we
1139 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
NeilBrown756daf22015-03-23 13:37:38 +11001141static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001142 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001143{
David Howells9875cf82011-01-14 18:45:21 +00001144 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001145 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001146
David Howells9875cf82011-01-14 18:45:21 +00001147 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1148 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001149
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001150 /* We don't want to mount if someone's just doing a stat -
1151 * unless they're stat'ing a directory and appended a '/' to
1152 * the name.
1153 *
1154 * We do, however, want to mount if someone wants to open or
1155 * create a file of any type under the mountpoint, wants to
1156 * traverse through the mountpoint or wants to open the
1157 * mounted directory. Also, autofs may mark negative dentries
1158 * as being automount points. These will need the attentions
1159 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001160 */
NeilBrown756daf22015-03-23 13:37:38 +11001161 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001162 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1163 path->dentry->d_inode)
1164 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001165
NeilBrown756daf22015-03-23 13:37:38 +11001166 nd->total_link_count++;
1167 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001168 return -ELOOP;
1169
1170 mnt = path->dentry->d_op->d_automount(path);
1171 if (IS_ERR(mnt)) {
1172 /*
1173 * The filesystem is allowed to return -EISDIR here to indicate
1174 * it doesn't want to automount. For instance, autofs would do
1175 * this so that its userspace daemon can mount on this dentry.
1176 *
1177 * However, we can only permit this if it's a terminal point in
1178 * the path being looked up; if it wasn't then the remainder of
1179 * the path is inaccessible and we should say so.
1180 */
NeilBrown756daf22015-03-23 13:37:38 +11001181 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001182 return -EREMOTE;
1183 return PTR_ERR(mnt);
1184 }
David Howellsea5b7782011-01-14 19:10:03 +00001185
David Howells9875cf82011-01-14 18:45:21 +00001186 if (!mnt) /* mount collision */
1187 return 0;
1188
Al Viro8aef1882011-06-16 15:10:06 +01001189 if (!*need_mntput) {
1190 /* lock_mount() may release path->mnt on error */
1191 mntget(path->mnt);
1192 *need_mntput = true;
1193 }
Al Viro19a167a2011-01-17 01:35:23 -05001194 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001195
David Howellsea5b7782011-01-14 19:10:03 +00001196 switch (err) {
1197 case -EBUSY:
1198 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001199 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001200 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001201 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001202 path->mnt = mnt;
1203 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001204 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001205 default:
1206 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001207 }
Al Viro19a167a2011-01-17 01:35:23 -05001208
David Howells9875cf82011-01-14 18:45:21 +00001209}
1210
1211/*
1212 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001213 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001214 * - Flagged as mountpoint
1215 * - Flagged as automount point
1216 *
1217 * This may only be called in refwalk mode.
1218 *
1219 * Serialization is taken care of in namespace.c
1220 */
NeilBrown756daf22015-03-23 13:37:38 +11001221static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001222{
Al Viro8aef1882011-06-16 15:10:06 +01001223 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001224 unsigned managed;
1225 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001226 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001227
1228 /* Given that we're not holding a lock here, we retain the value in a
1229 * local variable for each dentry as we look at it so that we don't see
1230 * the components of that value change under us */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001231 while (managed = READ_ONCE(path->dentry->d_flags),
David Howells9875cf82011-01-14 18:45:21 +00001232 managed &= DCACHE_MANAGED_DENTRY,
1233 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001234 /* Allow the filesystem to manage the transit without i_mutex
1235 * being held. */
1236 if (managed & DCACHE_MANAGE_TRANSIT) {
1237 BUG_ON(!path->dentry->d_op);
1238 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001239 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001240 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001241 break;
David Howellscc53ce52011-01-14 18:45:26 +00001242 }
1243
David Howells9875cf82011-01-14 18:45:21 +00001244 /* Transit to a mounted filesystem. */
1245 if (managed & DCACHE_MOUNTED) {
1246 struct vfsmount *mounted = lookup_mnt(path);
1247 if (mounted) {
1248 dput(path->dentry);
1249 if (need_mntput)
1250 mntput(path->mnt);
1251 path->mnt = mounted;
1252 path->dentry = dget(mounted->mnt_root);
1253 need_mntput = true;
1254 continue;
1255 }
1256
1257 /* Something is mounted on this dentry in another
1258 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001259 * namespace got unmounted before lookup_mnt() could
1260 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001261 }
1262
1263 /* Handle an automount point */
1264 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001265 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001266 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001267 break;
David Howells9875cf82011-01-14 18:45:21 +00001268 continue;
1269 }
1270
1271 /* We didn't change the current path point */
1272 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 }
Al Viro8aef1882011-06-16 15:10:06 +01001274
1275 if (need_mntput && path->mnt == mnt)
1276 mntput(path->mnt);
Al Viroe9742b52016-03-05 22:04:59 -05001277 if (ret == -EISDIR || !ret)
1278 ret = 1;
Al Viro84027522015-04-22 10:30:08 -04001279 if (need_mntput)
1280 nd->flags |= LOOKUP_JUMPED;
1281 if (unlikely(ret < 0))
1282 path_put_conditional(path, nd);
1283 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
David Howellscc53ce52011-01-14 18:45:26 +00001286int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 struct vfsmount *mounted;
1289
Al Viro1c755af2009-04-18 14:06:57 -04001290 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001292 dput(path->dentry);
1293 mntput(path->mnt);
1294 path->mnt = mounted;
1295 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 1;
1297 }
1298 return 0;
1299}
Al Viro4d359502014-03-14 12:20:17 -04001300EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Ian Kentfb5f51c2016-11-24 08:03:41 +11001302static inline int managed_dentry_rcu(const struct path *path)
Ian Kent62a73752011-03-25 01:51:02 +08001303{
Ian Kentfb5f51c2016-11-24 08:03:41 +11001304 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1305 path->dentry->d_op->d_manage(path, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001306}
1307
David Howells9875cf82011-01-14 18:45:21 +00001308/*
Al Viro287548e2011-05-27 06:50:06 -04001309 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1310 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001311 */
1312static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001313 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001314{
Ian Kent62a73752011-03-25 01:51:02 +08001315 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001316 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001317 /*
1318 * Don't forget we might have a non-mountpoint managed dentry
1319 * that wants to block transit.
1320 */
Ian Kentfb5f51c2016-11-24 08:03:41 +11001321 switch (managed_dentry_rcu(path)) {
NeilBrownb8faf032014-08-04 17:06:29 +10001322 case -ECHILD:
1323 default:
David Howellsab909112011-01-14 18:46:51 +00001324 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001325 case -EISDIR:
1326 return true;
1327 case 0:
1328 break;
1329 }
Ian Kent62a73752011-03-25 01:51:02 +08001330
1331 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001332 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001333
Al Viro474279d2013-10-01 16:11:26 -04001334 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001335 if (!mounted)
1336 break;
Al Viroc7105362011-11-24 18:22:03 -05001337 path->mnt = &mounted->mnt;
1338 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001339 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001340 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001341 /*
1342 * Update the inode too. We don't need to re-check the
1343 * dentry sequence number here after this d_inode read,
1344 * because a mount-point is always pinned.
1345 */
1346 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001347 }
Al Virof5be3e29122014-09-13 21:50:45 -04001348 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001349 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001350}
1351
Nick Piggin31e6b012011-01-07 17:49:52 +11001352static int follow_dotdot_rcu(struct nameidata *nd)
1353{
Al Viro4023bfc2014-09-13 21:59:43 -04001354 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001355
David Howells9875cf82011-01-14 18:45:21 +00001356 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001357 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001358 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001359 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1360 struct dentry *old = nd->path.dentry;
1361 struct dentry *parent = old->d_parent;
1362 unsigned seq;
1363
Al Viro4023bfc2014-09-13 21:59:43 -04001364 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001365 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001366 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1367 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001368 nd->path.dentry = parent;
1369 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001370 if (unlikely(!path_connected(&nd->path)))
Aleksa Sarai94b41202019-12-07 01:13:26 +11001371 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001372 break;
Al Viroaed434a2015-05-12 12:22:47 -04001373 } else {
1374 struct mount *mnt = real_mount(nd->path.mnt);
1375 struct mount *mparent = mnt->mnt_parent;
1376 struct dentry *mountpoint = mnt->mnt_mountpoint;
1377 struct inode *inode2 = mountpoint->d_inode;
1378 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1379 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1380 return -ECHILD;
1381 if (&mparent->mnt == nd->path.mnt)
1382 break;
1383 /* we know that mountpoint was pinned */
1384 nd->path.dentry = mountpoint;
1385 nd->path.mnt = &mparent->mnt;
1386 inode = inode2;
1387 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001388 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001389 }
Al Viroaed434a2015-05-12 12:22:47 -04001390 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001391 struct mount *mounted;
1392 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001393 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1394 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001395 if (!mounted)
1396 break;
1397 nd->path.mnt = &mounted->mnt;
1398 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001399 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001400 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001401 }
Al Viro4023bfc2014-09-13 21:59:43 -04001402 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001403 return 0;
1404}
1405
David Howells9875cf82011-01-14 18:45:21 +00001406/*
David Howellscc53ce52011-01-14 18:45:26 +00001407 * Follow down to the covering mount currently visible to userspace. At each
1408 * point, the filesystem owning that dentry may be queried as to whether the
1409 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001410 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001411int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001412{
1413 unsigned managed;
1414 int ret;
1415
Mark Rutland6aa7de02017-10-23 14:07:29 -07001416 while (managed = READ_ONCE(path->dentry->d_flags),
David Howellscc53ce52011-01-14 18:45:26 +00001417 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1418 /* Allow the filesystem to manage the transit without i_mutex
1419 * being held.
1420 *
1421 * We indicate to the filesystem if someone is trying to mount
1422 * something here. This gives autofs the chance to deny anyone
1423 * other than its daemon the right to mount on its
1424 * superstructure.
1425 *
1426 * The filesystem may sleep at this point.
1427 */
1428 if (managed & DCACHE_MANAGE_TRANSIT) {
1429 BUG_ON(!path->dentry->d_op);
1430 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001431 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001432 if (ret < 0)
1433 return ret == -EISDIR ? 0 : ret;
1434 }
1435
1436 /* Transit to a mounted filesystem. */
1437 if (managed & DCACHE_MOUNTED) {
1438 struct vfsmount *mounted = lookup_mnt(path);
1439 if (!mounted)
1440 break;
1441 dput(path->dentry);
1442 mntput(path->mnt);
1443 path->mnt = mounted;
1444 path->dentry = dget(mounted->mnt_root);
1445 continue;
1446 }
1447
1448 /* Don't handle automount points here */
1449 break;
1450 }
1451 return 0;
1452}
Al Viro4d359502014-03-14 12:20:17 -04001453EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001454
1455/*
David Howells9875cf82011-01-14 18:45:21 +00001456 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1457 */
1458static void follow_mount(struct path *path)
1459{
1460 while (d_mountpoint(path->dentry)) {
1461 struct vfsmount *mounted = lookup_mnt(path);
1462 if (!mounted)
1463 break;
1464 dput(path->dentry);
1465 mntput(path->mnt);
1466 path->mnt = mounted;
1467 path->dentry = dget(mounted->mnt_root);
1468 }
1469}
1470
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001471static int path_parent_directory(struct path *path)
1472{
1473 struct dentry *old = path->dentry;
1474 /* rare case of legitimate dget_parent()... */
1475 path->dentry = dget_parent(path->dentry);
1476 dput(old);
1477 if (unlikely(!path_connected(path)))
1478 return -ENOENT;
1479 return 0;
1480}
1481
Eric W. Biederman397d4252015-08-15 20:27:13 -05001482static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483{
1484 while(1) {
Danilo Krummrich030c7e02018-04-23 10:30:59 +02001485 if (path_equal(&nd->path, &nd->root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 break;
Jan Blunck4ac91372008-02-14 19:34:32 -08001487 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001488 int ret = path_parent_directory(&nd->path);
1489 if (ret)
1490 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
1492 }
Al Viro3088dd72010-01-30 15:47:29 -05001493 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
Al Viro79ed0222009-04-18 13:59:41 -04001496 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001497 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001498 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001502 * This looks up the name in dcache and possibly revalidates the found dentry.
1503 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001504 */
Al Viroe3c13922016-03-06 14:03:27 -05001505static struct dentry *lookup_dcache(const struct qstr *name,
1506 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001507 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001508{
Al Viroa89f8332017-01-09 22:25:28 -05001509 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001510 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001511 int error = d_revalidate(dentry, flags);
1512 if (unlikely(error <= 0)) {
1513 if (!error)
1514 d_invalidate(dentry);
1515 dput(dentry);
1516 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001517 }
1518 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001519 return dentry;
1520}
1521
1522/*
Al Viroa03ece52018-03-08 11:00:45 -05001523 * Parent directory has inode locked exclusive. This is one
1524 * and only case when ->lookup() gets called on non in-lookup
1525 * dentries - as the matter of fact, this only gets called
1526 * when directory is guaranteed to have no in-lookup children
1527 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001528 */
Al Viroa03ece52018-03-08 11:00:45 -05001529static struct dentry *__lookup_hash(const struct qstr *name,
1530 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001531{
Al Viroa03ece52018-03-08 11:00:45 -05001532 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001533 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001534 struct inode *dir = base->d_inode;
1535
1536 if (dentry)
1537 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001538
1539 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001540 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001541 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001542
1543 dentry = d_alloc(base, name);
1544 if (unlikely(!dentry))
1545 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001546
Al Viro72bd8662012-06-10 17:17:17 -04001547 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001548 if (unlikely(old)) {
1549 dput(dentry);
1550 dentry = old;
1551 }
1552 return dentry;
1553}
1554
Al Viroe97cdc82013-01-24 18:16:00 -05001555static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001556 struct path *path, struct inode **inode,
1557 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558{
Jan Blunck4ac91372008-02-14 19:34:32 -08001559 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001560 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001561 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001562 int err;
1563
Al Viro3cac2602009-08-13 18:27:43 +04001564 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001565 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001566 * of a false negative due to a concurrent rename, the caller is
1567 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001568 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001569 if (nd->flags & LOOKUP_RCU) {
1570 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001571 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001572 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001573 if (unlikely(!dentry)) {
Al Viro4675ac32017-01-09 22:29:15 -05001574 if (unlazy_walk(nd))
Al Viro5d0f49c2016-03-05 21:32:53 -05001575 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001576 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001577 }
Al Viro5a18fff2011-03-11 04:44:53 -05001578
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001579 /*
1580 * This sequence count validates that the inode matches
1581 * the dentry name information from lookup.
1582 */
David Howells63afdfc2015-05-06 15:59:00 +01001583 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001584 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001585 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001586 return -ECHILD;
1587
1588 /*
1589 * This sequence count validates that the parent had no
1590 * changes while we did the lookup of the dentry above.
1591 *
1592 * The memory barrier in read_seqcount_begin of child is
1593 * enough, we can use __read_seqcount_retry here.
1594 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001595 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001596 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001597
Al Viro254cf582015-05-05 09:40:46 -04001598 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001599 status = d_revalidate(dentry, nd->flags);
Al Viro209a7fb2017-01-09 01:35:39 -05001600 if (likely(status > 0)) {
Al Viro5d0f49c2016-03-05 21:32:53 -05001601 /*
1602 * Note: do negative dentry check after revalidation in
1603 * case that drops it.
1604 */
1605 if (unlikely(negative))
1606 return -ENOENT;
1607 path->mnt = mnt;
1608 path->dentry = dentry;
1609 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001610 return 1;
Al Viro24643082011-02-15 01:26:22 -05001611 }
Al Viro4675ac32017-01-09 22:29:15 -05001612 if (unlazy_child(nd, dentry, seq))
Al Viro209a7fb2017-01-09 01:35:39 -05001613 return -ECHILD;
1614 if (unlikely(status == -ECHILD))
1615 /* we'd been told to redo it in non-rcu mode */
1616 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001617 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001618 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001619 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001620 return 0;
Al Viroa89f8332017-01-09 22:25:28 -05001621 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001622 }
Al Viro5a18fff2011-03-11 04:44:53 -05001623 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001624 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001625 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001626 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001627 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001628 }
Al Viro766c4cb2015-05-07 19:24:57 -04001629 if (unlikely(d_is_negative(dentry))) {
1630 dput(dentry);
1631 return -ENOENT;
1632 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001633
David Howells9875cf82011-01-14 18:45:21 +00001634 path->mnt = mnt;
1635 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001636 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001637 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001638 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001639 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001640}
1641
1642/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001643static struct dentry *__lookup_slow(const struct qstr *name,
1644 struct dentry *dir,
1645 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001646{
Al Viro88d83312018-04-06 16:43:47 -04001647 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001648 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001649 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001650
Al Viro19363862016-04-14 19:33:34 -04001651 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001652 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001653 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001654again:
Al Virod9171b92016-04-15 03:33:13 -04001655 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001656 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001657 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001658 if (unlikely(!d_in_lookup(dentry))) {
Al Viroa89f8332017-01-09 22:25:28 -05001659 if (!(flags & LOOKUP_NO_REVAL)) {
Al Viro949a8522016-03-06 14:20:52 -05001660 int error = d_revalidate(dentry, flags);
1661 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001662 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001663 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001664 dput(dentry);
1665 goto again;
1666 }
Al Viro949a8522016-03-06 14:20:52 -05001667 dput(dentry);
1668 dentry = ERR_PTR(error);
1669 }
1670 }
Al Viro94bdd652016-04-15 02:42:04 -04001671 } else {
1672 old = inode->i_op->lookup(inode, dentry, flags);
1673 d_lookup_done(dentry);
1674 if (unlikely(old)) {
1675 dput(dentry);
1676 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001677 }
1678 }
Al Viroe3c13922016-03-06 14:03:27 -05001679 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680}
1681
Al Viro88d83312018-04-06 16:43:47 -04001682static struct dentry *lookup_slow(const struct qstr *name,
1683 struct dentry *dir,
1684 unsigned int flags)
1685{
1686 struct inode *inode = dir->d_inode;
1687 struct dentry *res;
1688 inode_lock_shared(inode);
1689 res = __lookup_slow(name, dir, flags);
1690 inode_unlock_shared(inode);
1691 return res;
1692}
1693
Al Viro52094c82011-02-21 21:34:47 -05001694static inline int may_lookup(struct nameidata *nd)
1695{
1696 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001697 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001698 if (err != -ECHILD)
1699 return err;
Al Viro4675ac32017-01-09 22:29:15 -05001700 if (unlazy_walk(nd))
Al Viro52094c82011-02-21 21:34:47 -05001701 return -ECHILD;
1702 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001703 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001704}
1705
Al Viro9856fa12011-03-04 14:22:06 -05001706static inline int handle_dots(struct nameidata *nd, int type)
1707{
1708 if (type == LAST_DOTDOT) {
Al Viro9e6697e2015-12-05 20:07:21 -05001709 if (!nd->root.mnt)
1710 set_root(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001711 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001712 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001713 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001714 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001715 }
1716 return 0;
1717}
1718
Al Viro181548c2015-05-07 19:54:34 -04001719static int pick_link(struct nameidata *nd, struct path *link,
1720 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001721{
Al Viro626de992015-05-04 18:26:59 -04001722 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001723 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001724 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001725 path_to_nameidata(link, nd);
1726 return -ELOOP;
1727 }
Al Virobc40aee2015-05-09 13:04:24 -04001728 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001729 if (link->mnt == nd->path.mnt)
1730 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001731 }
Al Viro626de992015-05-04 18:26:59 -04001732 error = nd_alloc_stack(nd);
1733 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001734 if (error == -ECHILD) {
Al Viroad1633a2017-01-08 22:35:31 -05001735 if (unlikely(!legitimize_path(nd, link, seq))) {
1736 drop_links(nd);
1737 nd->depth = 0;
1738 nd->flags &= ~LOOKUP_RCU;
1739 nd->path.mnt = NULL;
1740 nd->path.dentry = NULL;
1741 if (!(nd->flags & LOOKUP_ROOT))
1742 nd->root.mnt = NULL;
1743 rcu_read_unlock();
Al Viro4675ac32017-01-09 22:29:15 -05001744 } else if (likely(unlazy_walk(nd)) == 0)
Al Viroad1633a2017-01-08 22:35:31 -05001745 error = nd_alloc_stack(nd);
Al Virobc40aee2015-05-09 13:04:24 -04001746 }
1747 if (error) {
1748 path_put(link);
1749 return error;
1750 }
Al Viro626de992015-05-04 18:26:59 -04001751 }
1752
Al Viroab104922015-05-10 11:50:01 -04001753 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001754 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001755 clear_delayed_call(&last->done);
1756 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001757 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001758 return 1;
1759}
1760
Al Viro8f64fb12016-11-14 01:50:26 -05001761enum {WALK_FOLLOW = 1, WALK_MORE = 2};
Al Viro31d66bc2016-11-14 01:43:34 -05001762
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001763/*
1764 * Do we need to follow links? We _really_ want to be able
1765 * to do this check without having to look at inode->i_op,
1766 * so we keep a cache of "no, this doesn't need follow_link"
1767 * for the common case.
1768 */
Al Viro8f64fb12016-11-14 01:50:26 -05001769static inline int step_into(struct nameidata *nd, struct path *path,
1770 int flags, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001771{
Al Viro31d66bc2016-11-14 01:43:34 -05001772 if (!(flags & WALK_MORE) && nd->depth)
1773 put_link(nd);
Al Viro8f64fb12016-11-14 01:50:26 -05001774 if (likely(!d_is_symlink(path->dentry)) ||
1775 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1776 /* not a symlink or should not follow */
1777 path_to_nameidata(path, nd);
1778 nd->inode = inode;
1779 nd->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001780 return 0;
Al Viro8f64fb12016-11-14 01:50:26 -05001781 }
Al Viroa7f77542016-02-27 19:31:01 -05001782 /* make sure that d_is_symlink above matches inode */
1783 if (nd->flags & LOOKUP_RCU) {
Al Viro8f64fb12016-11-14 01:50:26 -05001784 if (read_seqcount_retry(&path->dentry->d_seq, seq))
Al Viroa7f77542016-02-27 19:31:01 -05001785 return -ECHILD;
1786 }
Al Viro8f64fb12016-11-14 01:50:26 -05001787 return pick_link(nd, path, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001788}
1789
Al Viro4693a542015-05-04 17:47:11 -04001790static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001791{
Al Virocaa85632015-04-22 17:52:47 -04001792 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001793 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001794 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001795 int err;
1796 /*
1797 * "." and ".." are special - ".." especially so because it has
1798 * to be able to know about the current root directory and
1799 * parent relationships.
1800 */
Al Viro4693a542015-05-04 17:47:11 -04001801 if (unlikely(nd->last_type != LAST_NORM)) {
1802 err = handle_dots(nd, nd->last_type);
Al Viro1c4ff1a2016-11-14 01:39:36 -05001803 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001804 put_link(nd);
1805 return err;
1806 }
Al Viro254cf582015-05-05 09:40:46 -04001807 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001808 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001809 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001810 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001811 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1812 nd->flags);
1813 if (IS_ERR(path.dentry))
1814 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001815
Al Viroe3c13922016-03-06 14:03:27 -05001816 path.mnt = nd->path.mnt;
1817 err = follow_managed(&path, nd);
1818 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001819 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001820
Al Viro7500c382016-03-31 00:23:05 -04001821 if (unlikely(d_is_negative(path.dentry))) {
1822 path_to_nameidata(&path, nd);
1823 return -ENOENT;
1824 }
1825
Al Viro254cf582015-05-05 09:40:46 -04001826 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001827 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001828 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001829
Al Viro8f64fb12016-11-14 01:50:26 -05001830 return step_into(nd, &path, flags, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001831}
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001834 * We can do the critical dentry name comparison and hashing
1835 * operations one word at a time, but we are limited to:
1836 *
1837 * - Architectures with fast unaligned word accesses. We could
1838 * do a "get_unaligned()" if this helps and is sufficiently
1839 * fast.
1840 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001841 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1842 * do not trap on the (extremely unlikely) case of a page
1843 * crossing operation.
1844 *
1845 * - Furthermore, we need an efficient 64-bit compile for the
1846 * 64-bit case in order to generate the "number of bytes in
1847 * the final mask". Again, that could be replaced with a
1848 * efficient population count instruction or similar.
1849 */
1850#ifdef CONFIG_DCACHE_WORD_ACCESS
1851
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001852#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001853
George Spelvin468a9422016-05-26 22:11:51 -04001854#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001855
George Spelvin468a9422016-05-26 22:11:51 -04001856/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1857
1858#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001859/*
1860 * Register pressure in the mixing function is an issue, particularly
1861 * on 32-bit x86, but almost any function requires one state value and
1862 * one temporary. Instead, use a function designed for two state values
1863 * and no temporaries.
1864 *
1865 * This function cannot create a collision in only two iterations, so
1866 * we have two iterations to achieve avalanche. In those two iterations,
1867 * we have six layers of mixing, which is enough to spread one bit's
1868 * influence out to 2^6 = 64 state bits.
1869 *
1870 * Rotate constants are scored by considering either 64 one-bit input
1871 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1872 * probability of that delta causing a change to each of the 128 output
1873 * bits, using a sample of random initial states.
1874 *
1875 * The Shannon entropy of the computed probabilities is then summed
1876 * to produce a score. Ideally, any input change has a 50% chance of
1877 * toggling any given output bit.
1878 *
1879 * Mixing scores (in bits) for (12,45):
1880 * Input delta: 1-bit 2-bit
1881 * 1 round: 713.3 42542.6
1882 * 2 rounds: 2753.7 140389.8
1883 * 3 rounds: 5954.1 233458.2
1884 * 4 rounds: 7862.6 256672.2
1885 * Perfect: 8192 258048
1886 * (64*128) (64*63/2 * 128)
1887 */
1888#define HASH_MIX(x, y, a) \
1889 ( x ^= (a), \
1890 y ^= x, x = rol64(x,12),\
1891 x += y, y = rol64(y,45),\
1892 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001893
George Spelvin0fed3ac2016-05-02 06:31:01 -04001894/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001895 * Fold two longs into one 32-bit hash value. This must be fast, but
1896 * latency isn't quite as critical, as there is a fair bit of additional
1897 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001898 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001899static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001900{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001901 y ^= x * GOLDEN_RATIO_64;
1902 y *= GOLDEN_RATIO_64;
1903 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001904}
1905
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001906#else /* 32-bit case */
1907
George Spelvin2a18da7a2016-05-23 07:43:58 -04001908/*
1909 * Mixing scores (in bits) for (7,20):
1910 * Input delta: 1-bit 2-bit
1911 * 1 round: 330.3 9201.6
1912 * 2 rounds: 1246.4 25475.4
1913 * 3 rounds: 1907.1 31295.1
1914 * 4 rounds: 2042.3 31718.6
1915 * Perfect: 2048 31744
1916 * (32*64) (32*31/2 * 64)
1917 */
1918#define HASH_MIX(x, y, a) \
1919 ( x ^= (a), \
1920 y ^= x, x = rol32(x, 7),\
1921 x += y, y = rol32(y,20),\
1922 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001923
George Spelvin2a18da7a2016-05-23 07:43:58 -04001924static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001925{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001926 /* Use arch-optimized multiply if one exists */
1927 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001928}
1929
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001930#endif
1931
George Spelvin2a18da7a2016-05-23 07:43:58 -04001932/*
1933 * Return the hash of a string of known length. This is carfully
1934 * designed to match hash_name(), which is the more critical function.
1935 * In particular, we must end by hashing a final word containing 0..7
1936 * payload bytes, to match the way that hash_name() iterates until it
1937 * finds the delimiter after the name.
1938 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001939unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001940{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001941 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001942
1943 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001944 if (!len)
1945 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001946 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001947 if (len < sizeof(unsigned long))
1948 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001949 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001950 name += sizeof(unsigned long);
1951 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001952 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001953 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001954done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001955 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001956}
1957EXPORT_SYMBOL(full_name_hash);
1958
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001959/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001960u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001961{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001962 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1963 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001964 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1965
Linus Torvalds8387ff22016-06-10 07:51:30 -07001966 len = 0;
1967 goto inside;
1968
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001969 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001970 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001971 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001972inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001973 a = load_unaligned_zeropad(name+len);
1974 } while (!has_zero(a, &adata, &constants));
1975
1976 adata = prep_zero_mask(a, adata, &constants);
1977 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001978 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001979
George Spelvin2a18da7a2016-05-23 07:43:58 -04001980 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001981}
1982EXPORT_SYMBOL(hashlen_string);
1983
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001984/*
1985 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001986 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001987 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001988static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001989{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001990 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1991 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07001992 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001993
Linus Torvalds8387ff22016-06-10 07:51:30 -07001994 len = 0;
1995 goto inside;
1996
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001997 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001998 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001999 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002000inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002001 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002002 b = a ^ REPEAT_BYTE('/');
2003 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002004
Linus Torvalds36126f82012-05-26 10:43:17 -07002005 adata = prep_zero_mask(a, adata, &constants);
2006 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002007 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002008 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002009
George Spelvin2a18da7a2016-05-23 07:43:58 -04002010 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002011}
2012
George Spelvin2a18da7a2016-05-23 07:43:58 -04002013#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002014
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002015/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002016unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002017{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002018 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002019 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002020 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002021 return end_name_hash(hash);
2022}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002023EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002024
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002025/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002026u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002027{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002028 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002029 unsigned long len = 0, c;
2030
2031 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002032 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002033 len++;
2034 hash = partial_name_hash(c, hash);
2035 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002036 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002037 return hashlen_create(end_name_hash(hash), len);
2038}
George Spelvinf2a031b2016-05-29 01:26:41 -04002039EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002040
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002041/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002042 * We know there's a real path component here of at least
2043 * one character.
2044 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002045static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002046{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002047 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002048 unsigned long len = 0, c;
2049
2050 c = (unsigned char)*name;
2051 do {
2052 len++;
2053 hash = partial_name_hash(c, hash);
2054 c = (unsigned char)name[len];
2055 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002056 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002057}
2058
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002059#endif
2060
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002061/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002063 * This is the basic name resolution function, turning a pathname into
2064 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002066 * Returns 0 and nd will have valid dentry and mnt on success.
2067 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 */
Al Viro6de88d72009-08-09 01:41:57 +04002069static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002072
Al Viro9b5858e2018-07-09 16:33:23 -04002073 if (IS_ERR(name))
2074 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 while (*name=='/')
2076 name++;
2077 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 /* At this point we know we have a real path component. */
2081 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002082 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002083 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Al Viro52094c82011-02-21 21:34:47 -05002085 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002086 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002087 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
Linus Torvalds8387ff22016-06-10 07:51:30 -07002089 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
Al Virofe479a52011-02-22 15:10:03 -05002091 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002092 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002093 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002094 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002095 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002096 nd->flags |= LOOKUP_JUMPED;
2097 }
Al Virofe479a52011-02-22 15:10:03 -05002098 break;
2099 case 1:
2100 type = LAST_DOT;
2101 }
Al Viro5a202bc2011-03-08 14:17:44 -05002102 if (likely(type == LAST_NORM)) {
2103 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002104 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002105 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002106 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002107 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002108 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002109 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002110 hash_len = this.hash_len;
2111 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002112 }
2113 }
Al Virofe479a52011-02-22 15:10:03 -05002114
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002115 nd->last.hash_len = hash_len;
2116 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002117 nd->last_type = type;
2118
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002119 name += hashlen_len(hash_len);
2120 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002121 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002122 /*
2123 * If it wasn't NUL, we know it was '/'. Skip that
2124 * slash, and continue until no more slashes.
2125 */
2126 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002127 name++;
2128 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002129 if (unlikely(!*name)) {
2130OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002131 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002132 if (!nd->depth)
2133 return 0;
2134 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002135 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002136 if (!name)
2137 return 0;
2138 /* last component of nested symlink */
Al Viro8f64fb12016-11-14 01:50:26 -05002139 err = walk_component(nd, WALK_FOLLOW);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002140 } else {
2141 /* not the last component */
Al Viro8f64fb12016-11-14 01:50:26 -05002142 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002143 }
Al Viroce57dfc2011-03-13 19:58:58 -04002144 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002145 return err;
Al Virofe479a52011-02-22 15:10:03 -05002146
Al Viroce57dfc2011-03-13 19:58:58 -04002147 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002148 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002149
Viresh Kumara1c83682015-08-12 15:59:44 +05302150 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002151 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002152 err = 0;
2153 if (unlikely(!s)) {
2154 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002155 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002156 } else {
Al Virofab51e82015-05-10 11:01:00 -04002157 nd->stack[nd->depth - 1].name = name;
2158 name = s;
2159 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002160 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002161 }
Al Viro97242f92015-08-01 19:59:28 -04002162 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2163 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002164 if (unlazy_walk(nd))
Al Viro97242f92015-08-01 19:59:28 -04002165 return -ECHILD;
2166 }
Al Viro3595e232015-05-09 16:54:45 -04002167 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170}
2171
Al Viroedc2b1d2018-07-09 16:27:23 -04002172/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002173static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174{
Al Viroc8a53ee2015-05-12 18:43:07 -04002175 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002177 if (!*s)
2178 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002179 if (flags & LOOKUP_RCU)
2180 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002181
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002183 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002185 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002186 struct dentry *root = nd->root.dentry;
2187 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002188 if (*s && unlikely(!d_can_lookup(root)))
2189 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002190 nd->path = nd->root;
2191 nd->inode = inode;
2192 if (flags & LOOKUP_RCU) {
Al Viro5b6ca022011-03-09 23:04:47 -05002193 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002194 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002195 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002196 } else {
2197 path_get(&nd->path);
2198 }
Al Viro368ee9b2015-05-08 17:19:59 -04002199 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002200 }
2201
Al Viro2a737872009-04-07 11:49:53 -04002202 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002203 nd->path.mnt = NULL;
2204 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Al Viro48a066e2013-09-29 22:06:07 -04002206 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002207 if (*s == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05002208 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05002209 if (likely(!nd_jump_root(nd)))
Al Viroef55d912015-12-05 20:25:06 -05002210 return s;
Al Viroef55d912015-12-05 20:25:06 -05002211 return ERR_PTR(-ECHILD);
Al Viroc8a53ee2015-05-12 18:43:07 -04002212 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002213 if (flags & LOOKUP_RCU) {
2214 struct fs_struct *fs = current->fs;
2215 unsigned seq;
2216
Al Viroe41f7d42011-02-22 14:02:58 -05002217 do {
2218 seq = read_seqcount_begin(&fs->seq);
2219 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002220 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002221 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2222 } while (read_seqcount_retry(&fs->seq, seq));
2223 } else {
2224 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002225 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002226 }
Al Viroef55d912015-12-05 20:25:06 -05002227 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002228 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002229 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002230 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002231 struct dentry *dentry;
2232
Al Viro2903ff02012-08-28 12:52:22 -04002233 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002234 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002235
Al Viro2903ff02012-08-28 12:52:22 -04002236 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002237
Al Viroedc2b1d2018-07-09 16:27:23 -04002238 if (*s && unlikely(!d_can_lookup(dentry))) {
2239 fdput(f);
2240 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002241 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002242
Al Viro2903ff02012-08-28 12:52:22 -04002243 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002244 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002245 nd->inode = nd->path.dentry->d_inode;
2246 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002247 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002248 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002249 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002250 }
Al Viro34a26b92015-05-11 08:05:05 -04002251 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002252 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002254}
2255
Al Viro3bdba282015-05-08 17:37:07 -04002256static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002257{
2258 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002259 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002260 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002261 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002262 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002263 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002264 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002265 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002266}
2267
Al Virocaa85632015-04-22 17:52:47 -04002268static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002269{
2270 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2271 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2272
2273 nd->flags &= ~LOOKUP_PARENT;
Al Viro1c4ff1a2016-11-14 01:39:36 -05002274 return walk_component(nd, 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002275}
2276
Al Viro4f757f32017-04-15 17:31:22 -04002277static int handle_lookup_down(struct nameidata *nd)
2278{
2279 struct path path = nd->path;
2280 struct inode *inode = nd->inode;
2281 unsigned seq = nd->seq;
2282 int err;
2283
2284 if (nd->flags & LOOKUP_RCU) {
2285 /*
2286 * don't bother with unlazy_walk on failure - we are
2287 * at the very beginning of walk, so we lose nothing
2288 * if we simply redo everything in non-RCU mode
2289 */
2290 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2291 return -ECHILD;
2292 } else {
2293 dget(path.dentry);
2294 err = follow_managed(&path, nd);
2295 if (unlikely(err < 0))
2296 return err;
2297 inode = d_backing_inode(path.dentry);
2298 seq = 0;
2299 }
2300 path_to_nameidata(&path, nd);
2301 nd->inode = inode;
2302 nd->seq = seq;
2303 return 0;
2304}
2305
Al Viro9b4a9b12009-04-07 11:44:16 -04002306/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002307static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002308{
Al Viroc8a53ee2015-05-12 18:43:07 -04002309 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002310 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002311
Al Viro9b5858e2018-07-09 16:33:23 -04002312 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002313 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002314 if (unlikely(err < 0))
2315 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002316 }
2317
Al Viro3bdba282015-05-08 17:37:07 -04002318 while (!(err = link_path_walk(s, nd))
2319 && ((err = lookup_last(nd)) > 0)) {
2320 s = trailing_symlink(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002321 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002322 if (!err)
2323 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002324
Al Virodeb106c2015-05-08 18:05:21 -04002325 if (!err && nd->flags & LOOKUP_DIRECTORY)
2326 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002327 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002328 if (!err) {
2329 *path = nd->path;
2330 nd->path.mnt = NULL;
2331 nd->path.dentry = NULL;
2332 }
2333 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002334 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002335}
Nick Piggin31e6b012011-01-07 17:49:52 +11002336
Al Viro625b6d12015-05-12 16:36:12 -04002337static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002338 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002339{
Al Viro894bc8c2015-05-02 07:16:16 -04002340 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002341 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002342 if (IS_ERR(name))
2343 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002344 if (unlikely(root)) {
2345 nd.root = *root;
2346 flags |= LOOKUP_ROOT;
2347 }
Al Viro9883d182015-05-13 07:28:08 -04002348 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002349 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002350 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002351 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002352 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002353 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002354
2355 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002356 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002357 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002358 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002359 return retval;
2360}
2361
Al Viro8bcb77f2015-05-08 16:59:20 -04002362/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002363static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002364 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002365{
Al Viroc8a53ee2015-05-12 18:43:07 -04002366 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002367 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002368 if (!err)
2369 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002370 if (!err) {
2371 *parent = nd->path;
2372 nd->path.mnt = NULL;
2373 nd->path.dentry = NULL;
2374 }
2375 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002376 return err;
2377}
2378
Al Viro5c31b6c2015-05-12 17:32:54 -04002379static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002380 unsigned int flags, struct path *parent,
2381 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002382{
2383 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002384 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002385
Al Viro5c31b6c2015-05-12 17:32:54 -04002386 if (IS_ERR(name))
2387 return name;
Al Viro9883d182015-05-13 07:28:08 -04002388 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002389 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002390 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002391 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002392 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002393 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002394 if (likely(!retval)) {
2395 *last = nd.last;
2396 *type = nd.last_type;
2397 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002398 } else {
2399 putname(name);
2400 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002401 }
Al Viro9883d182015-05-13 07:28:08 -04002402 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002403 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002404}
2405
Al Viro79714f72012-06-15 03:01:42 +04002406/* does lookup, returns the object with parent locked */
2407struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002408{
Al Viro5c31b6c2015-05-12 17:32:54 -04002409 struct filename *filename;
2410 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002411 struct qstr last;
2412 int type;
Paul Moore51689102015-01-22 00:00:03 -05002413
Al Viro5c31b6c2015-05-12 17:32:54 -04002414 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2415 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002416 if (IS_ERR(filename))
2417 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002418 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002419 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002420 putname(filename);
2421 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002422 }
Al Viro59551022016-01-22 15:40:57 -05002423 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002424 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002425 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002426 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002427 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002428 }
Paul Moore51689102015-01-22 00:00:03 -05002429 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002430 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002431}
2432
Al Virod1811462008-08-02 00:49:18 -04002433int kern_path(const char *name, unsigned int flags, struct path *path)
2434{
Al Viroabc9f5b2015-05-12 16:53:42 -04002435 return filename_lookup(AT_FDCWD, getname_kernel(name),
2436 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002437}
Al Viro4d359502014-03-14 12:20:17 -04002438EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002439
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002440/**
2441 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2442 * @dentry: pointer to dentry of the base directory
2443 * @mnt: pointer to vfs mount of the base directory
2444 * @name: pointer to file name
2445 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002446 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002447 */
2448int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2449 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002450 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002451{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002452 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002453 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002454 return filename_lookup(AT_FDCWD, getname_kernel(name),
2455 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002456}
Al Viro4d359502014-03-14 12:20:17 -04002457EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002458
Al Viro3c95f0d2018-04-06 16:32:38 -04002459static int lookup_one_len_common(const char *name, struct dentry *base,
2460 int len, struct qstr *this)
2461{
2462 this->name = name;
2463 this->len = len;
2464 this->hash = full_name_hash(base, name, len);
2465 if (!len)
2466 return -EACCES;
2467
2468 if (unlikely(name[0] == '.')) {
2469 if (len < 2 || (len == 2 && name[1] == '.'))
2470 return -EACCES;
2471 }
2472
2473 while (len--) {
2474 unsigned int c = *(const unsigned char *)name++;
2475 if (c == '/' || c == '\0')
2476 return -EACCES;
2477 }
2478 /*
2479 * See if the low-level filesystem might want
2480 * to use its own hash..
2481 */
2482 if (base->d_flags & DCACHE_OP_HASH) {
2483 int err = base->d_op->d_hash(base, this);
2484 if (err < 0)
2485 return err;
2486 }
2487
2488 return inode_permission(base->d_inode, MAY_EXEC);
2489}
2490
Christoph Hellwigeead1912007-10-16 23:25:38 -07002491/**
David Howells0da0b7f2018-06-15 15:19:22 +01002492 * try_lookup_one_len - filesystem helper to lookup single pathname component
2493 * @name: pathname component to lookup
2494 * @base: base directory to lookup from
2495 * @len: maximum length @len should be interpreted to
2496 *
2497 * Look up a dentry by name in the dcache, returning NULL if it does not
2498 * currently exist. The function does not try to create a dentry.
2499 *
2500 * Note that this routine is purely a helper for filesystem usage and should
2501 * not be called by generic code.
2502 *
2503 * The caller must hold base->i_mutex.
2504 */
2505struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2506{
2507 struct qstr this;
2508 int err;
2509
2510 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2511
2512 err = lookup_one_len_common(name, base, len, &this);
2513 if (err)
2514 return ERR_PTR(err);
2515
2516 return lookup_dcache(&this, base, 0);
2517}
2518EXPORT_SYMBOL(try_lookup_one_len);
2519
2520/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002521 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002522 * @name: pathname component to lookup
2523 * @base: base directory to lookup from
2524 * @len: maximum length @len should be interpreted to
2525 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002526 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002527 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002528 *
2529 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002530 */
James Morris057f6c02007-04-26 00:12:05 -07002531struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2532{
Al Viro8613a202018-04-06 16:45:33 -04002533 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002534 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002535 int err;
James Morris057f6c02007-04-26 00:12:05 -07002536
Al Viro59551022016-01-22 15:40:57 -05002537 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002538
Al Viro3c95f0d2018-04-06 16:32:38 -04002539 err = lookup_one_len_common(name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002540 if (err)
2541 return ERR_PTR(err);
2542
Al Viro8613a202018-04-06 16:45:33 -04002543 dentry = lookup_dcache(&this, base, 0);
2544 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002545}
Al Viro4d359502014-03-14 12:20:17 -04002546EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002547
NeilBrownbbddca82016-01-07 16:08:20 -05002548/**
2549 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2550 * @name: pathname component to lookup
2551 * @base: base directory to lookup from
2552 * @len: maximum length @len should be interpreted to
2553 *
2554 * Note that this routine is purely a helper for filesystem usage and should
2555 * not be called by generic code.
2556 *
2557 * Unlike lookup_one_len, it should be called without the parent
2558 * i_mutex held, and will take the i_mutex itself if necessary.
2559 */
2560struct dentry *lookup_one_len_unlocked(const char *name,
2561 struct dentry *base, int len)
2562{
2563 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002564 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002565 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002566
Al Viro3c95f0d2018-04-06 16:32:38 -04002567 err = lookup_one_len_common(name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002568 if (err)
2569 return ERR_PTR(err);
2570
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002571 ret = lookup_dcache(&this, base, 0);
2572 if (!ret)
2573 ret = lookup_slow(&this, base, 0);
2574 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002575}
2576EXPORT_SYMBOL(lookup_one_len_unlocked);
2577
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002578#ifdef CONFIG_UNIX98_PTYS
2579int path_pts(struct path *path)
2580{
2581 /* Find something mounted on "pts" in the same directory as
2582 * the input path.
2583 */
2584 struct dentry *child, *parent;
2585 struct qstr this;
2586 int ret;
2587
2588 ret = path_parent_directory(path);
2589 if (ret)
2590 return ret;
2591
2592 parent = path->dentry;
2593 this.name = "pts";
2594 this.len = 3;
2595 child = d_hash_and_lookup(parent, &this);
2596 if (!child)
2597 return -ENOENT;
2598
2599 path->dentry = child;
2600 dput(parent);
2601 follow_mount(path);
2602 return 0;
2603}
2604#endif
2605
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002606int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2607 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Al Viroabc9f5b2015-05-12 16:53:42 -04002609 return filename_lookup(dfd, getname_flags(name, flags, empty),
2610 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611}
Al Virob853a162015-05-13 09:12:02 -04002612EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002613
Jeff Layton80334262013-07-26 06:23:25 -04002614/**
Al Viro197df042013-09-08 14:03:27 -04002615 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002616 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
Jeff Layton80334262013-07-26 06:23:25 -04002617 *
2618 * This is a special lookup_last function just for umount. In this case, we
2619 * need to resolve the path without doing any revalidation.
2620 *
2621 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2622 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2623 * in almost all cases, this lookup will be served out of the dcache. The only
2624 * cases where it won't are if nd->last refers to a symlink or the path is
2625 * bogus and it doesn't exist.
2626 *
2627 * Returns:
2628 * -error: if there was an error during lookup. This includes -ENOENT if the
Al Viroba8f4612016-11-14 00:40:33 -05002629 * lookup found a negative dentry.
Jeff Layton80334262013-07-26 06:23:25 -04002630 *
Al Viroba8f4612016-11-14 00:40:33 -05002631 * 0: if we successfully resolved nd->last and found it to not to be a
2632 * symlink that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002633 *
2634 * 1: if we successfully resolved nd->last and found it to be a symlink
Al Viroba8f4612016-11-14 00:40:33 -05002635 * that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002636 */
2637static int
Al Viroba8f4612016-11-14 00:40:33 -05002638mountpoint_last(struct nameidata *nd)
Jeff Layton80334262013-07-26 06:23:25 -04002639{
2640 int error = 0;
Jeff Layton80334262013-07-26 06:23:25 -04002641 struct dentry *dir = nd->path.dentry;
Al Viroba8f4612016-11-14 00:40:33 -05002642 struct path path;
Jeff Layton80334262013-07-26 06:23:25 -04002643
Al Viro35759522013-09-08 13:41:33 -04002644 /* If we're in rcuwalk, drop out of it to handle last component */
2645 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002646 if (unlazy_walk(nd))
Al Virodeb106c2015-05-08 18:05:21 -04002647 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002648 }
2649
2650 nd->flags &= ~LOOKUP_PARENT;
2651
2652 if (unlikely(nd->last_type != LAST_NORM)) {
2653 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002654 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002655 return error;
Al Viroba8f4612016-11-14 00:40:33 -05002656 path.dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002657 } else {
Al Viroba8f4612016-11-14 00:40:33 -05002658 path.dentry = d_lookup(dir, &nd->last);
2659 if (!path.dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002660 /*
2661 * No cached dentry. Mounted dentries are pinned in the
2662 * cache, so that means that this dentry is probably
2663 * a symlink or the path doesn't actually point
2664 * to a mounted dentry.
2665 */
Al Viroba8f4612016-11-14 00:40:33 -05002666 path.dentry = lookup_slow(&nd->last, dir,
Al Viro949a8522016-03-06 14:20:52 -05002667 nd->flags | LOOKUP_NO_REVAL);
Al Viroba8f4612016-11-14 00:40:33 -05002668 if (IS_ERR(path.dentry))
2669 return PTR_ERR(path.dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002670 }
Jeff Layton80334262013-07-26 06:23:25 -04002671 }
Al Viroba8f4612016-11-14 00:40:33 -05002672 if (d_is_negative(path.dentry)) {
2673 dput(path.dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002674 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002675 }
Al Viroba8f4612016-11-14 00:40:33 -05002676 path.mnt = nd->path.mnt;
Al Viro8f64fb12016-11-14 01:50:26 -05002677 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
Jeff Layton80334262013-07-26 06:23:25 -04002678}
2679
2680/**
Al Viro197df042013-09-08 14:03:27 -04002681 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b852015-09-09 15:39:23 -07002682 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002683 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002684 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002685 *
2686 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002687 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002688 */
2689static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002690path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002691{
Al Viroc8a53ee2015-05-12 18:43:07 -04002692 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002693 int err;
Al Viro9b5858e2018-07-09 16:33:23 -04002694
Al Viro3bdba282015-05-08 17:37:07 -04002695 while (!(err = link_path_walk(s, nd)) &&
Al Viroba8f4612016-11-14 00:40:33 -05002696 (err = mountpoint_last(nd)) > 0) {
Al Viro3bdba282015-05-08 17:37:07 -04002697 s = trailing_symlink(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002698 }
Al Viroba8f4612016-11-14 00:40:33 -05002699 if (!err) {
2700 *path = nd->path;
2701 nd->path.mnt = NULL;
2702 nd->path.dentry = NULL;
2703 follow_mount(path);
2704 }
Al Virodeb106c2015-05-08 18:05:21 -04002705 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002706 return err;
2707}
2708
Al Viro2d864652013-09-08 20:18:44 -04002709static int
Al Viro668696d2015-02-22 19:44:00 -05002710filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002711 unsigned int flags)
2712{
Al Viro9883d182015-05-13 07:28:08 -04002713 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002714 int error;
Al Viro668696d2015-02-22 19:44:00 -05002715 if (IS_ERR(name))
2716 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002717 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002718 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002719 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002720 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002721 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002722 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002723 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002724 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002725 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002726 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002727 return error;
2728}
2729
Jeff Layton80334262013-07-26 06:23:25 -04002730/**
Al Viro197df042013-09-08 14:03:27 -04002731 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002732 * @dfd: directory file descriptor
2733 * @name: pathname from userland
2734 * @flags: lookup flags
2735 * @path: pointer to container to hold result
2736 *
2737 * A umount is a special case for path walking. We're not actually interested
2738 * in the inode in this situation, and ESTALE errors can be a problem. We
2739 * simply want track down the dentry and vfsmount attached at the mountpoint
2740 * and avoid revalidating the last component.
2741 *
2742 * Returns 0 and populates "path" on success.
2743 */
2744int
Al Viro197df042013-09-08 14:03:27 -04002745user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002746 struct path *path)
2747{
Al Virocbaab2d2015-01-22 02:49:00 -05002748 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002749}
2750
Al Viro2d864652013-09-08 20:18:44 -04002751int
2752kern_path_mountpoint(int dfd, const char *name, struct path *path,
2753 unsigned int flags)
2754{
Al Virocbaab2d2015-01-22 02:49:00 -05002755 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002756}
2757EXPORT_SYMBOL(kern_path_mountpoint);
2758
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002759int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002761 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002762
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002763 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002765 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002767 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002769EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771/*
2772 * Check whether we can remove a link victim from directory dir, check
2773 * whether the type of victim is right.
2774 * 1. We can't do it if dir is read-only (done in permission())
2775 * 2. We should have write and exec permissions on dir
2776 * 3. We can't remove anything from append-only dir
2777 * 4. We can't do anything with immutable dir (done in permission())
2778 * 5. If the sticky bit on dir is set we should either
2779 * a. be owner of dir, or
2780 * b. be owner of victim, or
2781 * c. have CAP_FOWNER capability
2782 * 6. If the victim is append-only or immutable we can't do antyhing with
2783 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002784 * 7. If the victim has an unknown uid or gid we can't change the inode.
2785 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2786 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2787 * 10. We can't remove a root or mountpoint.
2788 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 * nfs_async_unlink().
2790 */
David Howellsb18825a2013-09-12 19:22:53 +01002791static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
David Howells63afdfc2015-05-06 15:59:00 +01002793 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 int error;
2795
David Howellsb18825a2013-09-12 19:22:53 +01002796 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002798 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002801
2802 /* Inode writeback is not safe when the uid or gid are invalid. */
2803 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2804 return -EOVERFLOW;
2805
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002806 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Al Virof419a2e2008-07-22 00:07:17 -04002808 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (error)
2810 return error;
2811 if (IS_APPEND(dir))
2812 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002813
2814 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002815 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return -EPERM;
2817 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002818 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 return -ENOTDIR;
2820 if (IS_ROOT(victim))
2821 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002822 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return -EISDIR;
2824 if (IS_DEADDIR(dir))
2825 return -ENOENT;
2826 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2827 return -EBUSY;
2828 return 0;
2829}
2830
2831/* Check whether we can create an object with dentry child in directory
2832 * dir.
2833 * 1. We can't do it if child already exists (open has special treatment for
2834 * this case, but since we are inlined it's OK)
2835 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002836 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2837 * 4. We should have write and exec permissions on dir
2838 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002840static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002842 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002843 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 if (child->d_inode)
2845 return -EEXIST;
2846 if (IS_DEADDIR(dir))
2847 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002848 s_user_ns = dir->i_sb->s_user_ns;
2849 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2850 !kgid_has_mapping(s_user_ns, current_fsgid()))
2851 return -EOVERFLOW;
Al Virof419a2e2008-07-22 00:07:17 -04002852 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855/*
2856 * p1 and p2 should be directories on the same fs.
2857 */
2858struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2859{
2860 struct dentry *p;
2861
2862 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002863 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 return NULL;
2865 }
2866
Al Virofc640052016-04-10 01:33:30 -04002867 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002869 p = d_ancestor(p2, p1);
2870 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002871 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2872 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002873 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
2875
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002876 p = d_ancestor(p1, p2);
2877 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002878 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2879 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002880 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 }
2882
Al Viro59551022016-01-22 15:40:57 -05002883 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2884 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return NULL;
2886}
Al Viro4d359502014-03-14 12:20:17 -04002887EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889void unlock_rename(struct dentry *p1, struct dentry *p2)
2890{
Al Viro59551022016-01-22 15:40:57 -05002891 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002893 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002894 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 }
2896}
Al Viro4d359502014-03-14 12:20:17 -04002897EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898
Al Viro4acdaf22011-07-26 01:42:34 -04002899int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002900 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002902 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 if (error)
2904 return error;
2905
Al Viroacfa4382008-12-04 10:06:33 -05002906 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return -EACCES; /* shouldn't it be ENOSYS? */
2908 mode &= S_IALLUGO;
2909 mode |= S_IFREG;
2910 error = security_inode_create(dir, dentry, mode);
2911 if (error)
2912 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002913 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002914 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002915 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return error;
2917}
Al Viro4d359502014-03-14 12:20:17 -04002918EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
Al Viro8e6c8482017-12-01 17:12:45 -05002920int vfs_mkobj(struct dentry *dentry, umode_t mode,
2921 int (*f)(struct dentry *, umode_t, void *),
2922 void *arg)
2923{
2924 struct inode *dir = dentry->d_parent->d_inode;
2925 int error = may_create(dir, dentry);
2926 if (error)
2927 return error;
2928
2929 mode &= S_IALLUGO;
2930 mode |= S_IFREG;
2931 error = security_inode_create(dir, dentry, mode);
2932 if (error)
2933 return error;
2934 error = f(dentry, mode, arg);
2935 if (!error)
2936 fsnotify_create(dir, dentry);
2937 return error;
2938}
2939EXPORT_SYMBOL(vfs_mkobj);
2940
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002941bool may_open_dev(const struct path *path)
2942{
2943 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2944 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2945}
2946
Al Virof0bb5aa2016-11-20 20:27:12 -05002947static int may_open(const struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002949 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 struct inode *inode = dentry->d_inode;
2951 int error;
2952
2953 if (!inode)
2954 return -ENOENT;
2955
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002956 switch (inode->i_mode & S_IFMT) {
2957 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002959 case S_IFDIR:
2960 if (acc_mode & MAY_WRITE)
2961 return -EISDIR;
2962 break;
2963 case S_IFBLK:
2964 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002965 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002967 /*FALLTHRU*/
2968 case S_IFIFO:
2969 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002971 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002972 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002973
Al Viro62fb4a12015-12-26 22:33:24 -05002974 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002975 if (error)
2976 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 /*
2979 * An append-only file must be opened in append mode for writing.
2980 */
2981 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002982 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002983 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002985 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987
2988 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002989 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002990 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002992 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002993}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
Jeff Laytone1181ee2010-12-07 16:19:50 -05002995static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002996{
Al Virof0bb5aa2016-11-20 20:27:12 -05002997 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002998 struct inode *inode = path->dentry->d_inode;
2999 int error = get_write_access(inode);
3000 if (error)
3001 return error;
3002 /*
3003 * Refuse to truncate files with mandatory locks held on them.
3004 */
Jeff Laytond7a06982014-03-10 09:54:15 -04003005 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05003006 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09003007 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003008 if (!error) {
3009 error = do_truncate(path->dentry, 0,
3010 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003011 filp);
Al Viro7715b522009-12-16 03:54:00 -05003012 }
3013 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003014 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
Dave Hansend57999e2008-02-15 14:37:27 -08003017static inline int open_to_namei_flags(int flag)
3018{
Al Viro8a5e9292011-06-25 19:15:54 -04003019 if ((flag & O_ACCMODE) == 3)
3020 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003021 return flag;
3022}
3023
Al Virod3607752016-03-25 15:21:09 -04003024static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003025{
Seth Forshee1328c722017-01-26 14:33:46 -06003026 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003027 int error = security_path_mknod(dir, dentry, mode, 0);
3028 if (error)
3029 return error;
3030
Seth Forshee1328c722017-01-26 14:33:46 -06003031 s_user_ns = dir->dentry->d_sb->s_user_ns;
3032 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3033 !kgid_has_mapping(s_user_ns, current_fsgid()))
3034 return -EOVERFLOW;
3035
Miklos Szeredid18e9002012-06-05 15:10:17 +02003036 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3037 if (error)
3038 return error;
3039
3040 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3041}
3042
David Howells1acf0af2012-06-14 16:13:46 +01003043/*
3044 * Attempt to atomically look up, create and open a file from a negative
3045 * dentry.
3046 *
3047 * Returns 0 if successful. The file will have been created and attached to
3048 * @file by the filesystem calling finish_open().
3049 *
Al Viro00a07c12018-07-09 19:30:20 -04003050 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3051 * be set. The caller will need to perform the open themselves. @path will
3052 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003053 *
3054 * Returns an error code otherwise.
3055 */
Al Viro2675a4e2012-06-22 12:41:10 +04003056static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3057 struct path *path, struct file *file,
3058 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003059 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003060{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003061 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003062 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003063 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003064
Al Viro384f26e2016-04-28 02:03:55 -04003065 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003066 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003067
Miklos Szeredid18e9002012-06-05 15:10:17 +02003068 if (nd->flags & LOOKUP_DIRECTORY)
3069 open_flag |= O_DIRECTORY;
3070
Al Viro30d90492012-06-22 12:40:19 +04003071 file->f_path.dentry = DENTRY_NOT_SET;
3072 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003073 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003074 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003075 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003076 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003077 if (file->f_mode & FMODE_OPENED) {
3078 /*
3079 * We didn't have the inode before the open, so check open
3080 * permission here.
3081 */
3082 int acc_mode = op->acc_mode;
3083 if (file->f_mode & FMODE_CREATED) {
3084 WARN_ON(!(open_flag & O_CREAT));
3085 fsnotify_create(dir, dentry);
3086 acc_mode = 0;
3087 }
3088 error = may_open(&file->f_path, acc_mode, open_flag);
3089 if (WARN_ON(error > 0))
3090 error = -EINVAL;
3091 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003092 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003093 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003094 if (file->f_path.dentry) {
3095 dput(dentry);
3096 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003097 }
Al Viro73a09dd2018-06-08 13:22:02 -04003098 if (file->f_mode & FMODE_CREATED)
Al Viro384f26e2016-04-28 02:03:55 -04003099 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04003100 if (unlikely(d_is_negative(dentry))) {
3101 error = -ENOENT;
3102 } else {
3103 path->dentry = dentry;
3104 path->mnt = nd->path.mnt;
Al Viro00a07c12018-07-09 19:30:20 -04003105 return 0;
Al Viroa01e7182016-06-07 21:53:51 -04003106 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003107 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003108 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003109 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003110 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003111}
3112
Nick Piggin31e6b012011-01-07 17:49:52 +11003113/*
David Howells1acf0af2012-06-14 16:13:46 +01003114 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003115 *
Al Viro00a07c12018-07-09 19:30:20 -04003116 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003117 *
Al Viro00a07c12018-07-09 19:30:20 -04003118 * Returns 0 on success, that is, if
3119 * the file was successfully atomically created (if necessary) and opened, or
3120 * the file was not completely opened at this time, though lookups and
3121 * creations were performed.
3122 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3123 * In the latter case dentry returned in @path might be negative if O_CREAT
3124 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003125 *
Al Viro00a07c12018-07-09 19:30:20 -04003126 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003127 */
Al Viro2675a4e2012-06-22 12:41:10 +04003128static int lookup_open(struct nameidata *nd, struct path *path,
3129 struct file *file,
3130 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003131 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003132{
3133 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003134 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003135 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003136 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003137 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003138 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003139 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003140
Al Viroce8644f2016-04-26 14:17:56 -04003141 if (unlikely(IS_DEADDIR(dir_inode)))
3142 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003143
Al Viro73a09dd2018-06-08 13:22:02 -04003144 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003145 dentry = d_lookup(dir, &nd->last);
3146 for (;;) {
3147 if (!dentry) {
3148 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3149 if (IS_ERR(dentry))
3150 return PTR_ERR(dentry);
3151 }
3152 if (d_in_lookup(dentry))
3153 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003154
Al Viro6fbd0712016-04-28 11:50:59 -04003155 error = d_revalidate(dentry, nd->flags);
3156 if (likely(error > 0))
3157 break;
3158 if (error)
3159 goto out_dput;
3160 d_invalidate(dentry);
3161 dput(dentry);
3162 dentry = NULL;
3163 }
3164 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003165 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003166 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003167 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003168
Al Viro1643b432016-04-27 19:14:10 -04003169 /*
3170 * Checking write permission is tricky, bacuse we don't know if we are
3171 * going to actually need it: O_CREAT opens should work as long as the
3172 * file exists. But checking existence breaks atomicity. The trick is
3173 * to check access and if not granted clear O_CREAT from the flags.
3174 *
3175 * Another problem is returing the "right" error value (e.g. for an
3176 * O_EXCL open we want to return EEXIST not EROFS).
3177 */
3178 if (open_flag & O_CREAT) {
3179 if (!IS_POSIXACL(dir->d_inode))
3180 mode &= ~current_umask();
3181 if (unlikely(!got_write)) {
3182 create_error = -EROFS;
3183 open_flag &= ~O_CREAT;
3184 if (open_flag & (O_EXCL | O_TRUNC))
3185 goto no_open;
3186 /* No side effects, safe to clear O_CREAT */
3187 } else {
3188 create_error = may_o_create(&nd->path, dentry, mode);
3189 if (create_error) {
3190 open_flag &= ~O_CREAT;
3191 if (open_flag & O_EXCL)
3192 goto no_open;
3193 }
3194 }
3195 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3196 unlikely(!got_write)) {
3197 /*
3198 * No O_CREATE -> atomicity not a requirement -> fall
3199 * back to lookup + open
3200 */
3201 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003202 }
3203
Al Viro1643b432016-04-27 19:14:10 -04003204 if (dir_inode->i_op->atomic_open) {
3205 error = atomic_open(nd, dentry, path, file, op, open_flag,
Al Viro3ec2eef2018-06-08 13:43:47 -04003206 mode);
Al Viro1643b432016-04-27 19:14:10 -04003207 if (unlikely(error == -ENOENT) && create_error)
3208 error = create_error;
3209 return error;
3210 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003211
Al Viro1643b432016-04-27 19:14:10 -04003212no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003213 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003214 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3215 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003216 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003217 if (unlikely(res)) {
3218 if (IS_ERR(res)) {
3219 error = PTR_ERR(res);
3220 goto out_dput;
3221 }
3222 dput(dentry);
3223 dentry = res;
3224 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003225 }
3226
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003227 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003228 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003229 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003230 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003231 if (!dir_inode->i_op->create) {
3232 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003233 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003234 }
Al Viroce8644f2016-04-26 14:17:56 -04003235 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003236 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003237 if (error)
3238 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003239 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003240 }
Al Viro1643b432016-04-27 19:14:10 -04003241 if (unlikely(create_error) && !dentry->d_inode) {
3242 error = create_error;
3243 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003244 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003245out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003246 path->dentry = dentry;
3247 path->mnt = nd->path.mnt;
Al Viro00a07c12018-07-09 19:30:20 -04003248 return 0;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003249
3250out_dput:
3251 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003252 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003253}
3254
3255/*
Al Virofe2d35f2011-03-05 22:58:25 -05003256 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003257 */
Al Viro896475d2015-04-22 18:02:17 -04003258static int do_last(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003259 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003260{
Al Viroa1e28032009-12-24 02:12:06 -05003261 struct dentry *dir = nd->path.dentry;
Al Viro8d7a5102020-02-01 16:26:45 +00003262 kuid_t dir_uid = nd->inode->i_uid;
3263 umode_t dir_mode = nd->inode->i_mode;
Al Viroca344a892011-03-09 00:36:45 -05003264 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003265 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003266 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003267 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003268 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003269 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003270 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003271 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003272
Al Viroc3e380b2011-02-23 13:39:45 -05003273 nd->flags &= ~LOOKUP_PARENT;
3274 nd->flags |= op->intent;
3275
Al Virobc77daa2013-06-06 09:12:33 -04003276 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003277 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003278 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003279 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003280 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003281 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003282
Al Viroca344a892011-03-09 00:36:45 -05003283 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003284 if (nd->last.name[nd->last.len])
3285 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3286 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003287 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003288 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003289 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003290
Miklos Szeredi71574862012-06-05 15:10:14 +02003291 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003292 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003293
Miklos Szeredi71574862012-06-05 15:10:14 +02003294 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003295 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003296 } else {
3297 /* create side of things */
3298 /*
3299 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3300 * has been cleared when we got to the last component we are
3301 * about to look up
3302 */
3303 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003304 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003305 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003306
Al Viro76ae2a52015-05-12 18:44:32 -04003307 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003308 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003309 if (unlikely(nd->last.name[nd->last.len]))
3310 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003311 }
3312
Al Viro9cf843e2016-04-28 19:35:16 -04003313 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003314 error = mnt_want_write(nd->path.mnt);
3315 if (!error)
3316 got_write = true;
3317 /*
3318 * do _not_ fail yet - we might not need that or fail with
3319 * a different error; let lookup_open() decide; we'll be
3320 * dropping this one anyway.
3321 */
3322 }
Al Viro9cf843e2016-04-28 19:35:16 -04003323 if (open_flag & O_CREAT)
3324 inode_lock(dir->d_inode);
3325 else
3326 inode_lock_shared(dir->d_inode);
Al Viro3ec2eef2018-06-08 13:43:47 -04003327 error = lookup_open(nd, &path, file, op, got_write);
Al Viro9cf843e2016-04-28 19:35:16 -04003328 if (open_flag & O_CREAT)
3329 inode_unlock(dir->d_inode);
3330 else
3331 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003332
Al Viro00a07c12018-07-09 19:30:20 -04003333 if (error)
3334 goto out;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003335
Al Viro00a07c12018-07-09 19:30:20 -04003336 if (file->f_mode & FMODE_OPENED) {
Al Viro73a09dd2018-06-08 13:22:02 -04003337 if ((file->f_mode & FMODE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003338 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003339 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003340
Al Viro76ae2a52015-05-12 18:44:32 -04003341 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003342 goto opened;
3343 }
Al Virofb1cc552009-12-24 01:58:28 -05003344
Al Viro73a09dd2018-06-08 13:22:02 -04003345 if (file->f_mode & FMODE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003346 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003347 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003348 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003349 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003350 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003351 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003352 }
3353
3354 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003355 * If atomic_open() acquired write access it is dropped now due to
3356 * possible mount and symlink following (this might be optimized away if
3357 * necessary...)
3358 */
Al Viro64894cf2012-07-31 00:53:35 +04003359 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003360 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003361 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003362 }
3363
Al Viroe6ec03a2016-06-05 00:23:09 -04003364 error = follow_managed(&path, nd);
3365 if (unlikely(error < 0))
3366 return error;
3367
Al Viro6583fe22016-03-05 18:14:03 -05003368 if (unlikely(d_is_negative(path.dentry))) {
3369 path_to_nameidata(&path, nd);
3370 return -ENOENT;
3371 }
3372
3373 /*
3374 * create/update audit record if it already exists.
3375 */
3376 audit_inode(nd->name, path.dentry, 0);
3377
Al Virodeb106c2015-05-08 18:05:21 -04003378 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3379 path_to_nameidata(&path, nd);
3380 return -EEXIST;
3381 }
Al Virofb1cc552009-12-24 01:58:28 -05003382
Al Viro254cf582015-05-05 09:40:46 -04003383 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003384 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003385finish_lookup:
Al Viro8f64fb12016-11-14 01:50:26 -05003386 error = step_into(nd, &path, 0, inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003387 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003388 return error;
Al Virobc77daa2013-06-06 09:12:33 -04003389finish_open:
Al Viro8f64fb12016-11-14 01:50:26 -05003390 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Viroa3fbbde2011-11-07 21:21:26 +00003391 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003392 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003393 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003394 audit_inode(nd->name, nd->path.dentry, 0);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003395 if (open_flag & O_CREAT) {
3396 error = -EISDIR;
3397 if (d_is_dir(nd->path.dentry))
3398 goto out;
Al Viro752f72e2020-01-26 09:29:34 -05003399 error = may_create_in_sticky(dir_mode, dir_uid,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003400 d_backing_inode(nd->path.dentry));
3401 if (unlikely(error))
3402 goto out;
3403 }
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003404 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003405 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003406 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003407 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003408 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003409
Al Viro0f9d1a12011-03-09 00:13:14 -05003410 if (will_truncate) {
3411 error = mnt_want_write(nd->path.mnt);
3412 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003413 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003414 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003415 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003416finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003417 error = may_open(&nd->path, acc_mode, open_flag);
3418 if (error)
3419 goto out;
Al Viroaad888f2018-06-08 12:58:04 -04003420 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
Al Viroae2bb292018-07-10 13:22:28 -04003421 error = vfs_open(&nd->path, file);
Al Virofac7d192016-06-04 11:41:49 -04003422 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003423 goto out;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003424opened:
Al Viro6035a272018-06-08 13:40:10 -04003425 error = ima_file_check(file, op->acc_mode);
Al Virofe9ec822016-04-27 03:14:20 -04003426 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003427 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003428out:
Al Viroc80567c2016-02-27 19:17:33 -05003429 if (unlikely(error > 0)) {
3430 WARN_ON(1);
3431 error = -EINVAL;
3432 }
Al Viro64894cf2012-07-31 00:53:35 +04003433 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003434 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003435 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003436}
3437
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003438struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3439{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003440 struct dentry *child = NULL;
3441 struct inode *dir = dentry->d_inode;
3442 struct inode *inode;
3443 int error;
3444
3445 /* we want directory to be writable */
3446 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3447 if (error)
3448 goto out_err;
3449 error = -EOPNOTSUPP;
3450 if (!dir->i_op->tmpfile)
3451 goto out_err;
3452 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003453 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003454 if (unlikely(!child))
3455 goto out_err;
3456 error = dir->i_op->tmpfile(dir, child, mode);
3457 if (error)
3458 goto out_err;
3459 error = -ENOENT;
3460 inode = child->d_inode;
3461 if (unlikely(!inode))
3462 goto out_err;
3463 if (!(open_flag & O_EXCL)) {
3464 spin_lock(&inode->i_lock);
3465 inode->i_state |= I_LINKABLE;
3466 spin_unlock(&inode->i_lock);
3467 }
3468 return child;
3469
3470out_err:
3471 dput(child);
3472 return ERR_PTR(error);
3473}
3474EXPORT_SYMBOL(vfs_tmpfile);
3475
Al Viroc8a53ee2015-05-12 18:43:07 -04003476static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003477 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003478 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003479{
Al Viro625b6d12015-05-12 16:36:12 -04003480 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003481 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003482 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003483 if (unlikely(error))
3484 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003485 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003486 if (unlikely(error))
3487 goto out;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003488 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3489 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003490 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003491 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003492 dput(path.dentry);
3493 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003494 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003495 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003496 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003497 if (error)
3498 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003499 file->f_path.mnt = path.mnt;
Al Virobe12af32018-06-08 11:44:56 -04003500 error = finish_open(file, child, NULL);
Al Viro60545d02013-06-07 01:20:27 -04003501out2:
Al Viro625b6d12015-05-12 16:36:12 -04003502 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003503out:
Al Viro625b6d12015-05-12 16:36:12 -04003504 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003505 return error;
3506}
3507
Al Viro6ac08702016-04-26 00:02:50 -04003508static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3509{
3510 struct path path;
3511 int error = path_lookupat(nd, flags, &path);
3512 if (!error) {
3513 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003514 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003515 path_put(&path);
3516 }
3517 return error;
3518}
3519
Al Viroc8a53ee2015-05-12 18:43:07 -04003520static struct file *path_openat(struct nameidata *nd,
3521 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522{
Al Viro30d90492012-06-22 12:40:19 +04003523 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003524 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003525
Al Viroea73ea72018-07-11 15:00:04 -04003526 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003527 if (IS_ERR(file))
3528 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003529
Al Virobb458c62013-07-13 13:26:37 +04003530 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003531 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003532 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003533 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003534 } else {
3535 const char *s = path_init(nd, flags);
3536 while (!(error = link_path_walk(s, nd)) &&
3537 (error = do_last(nd, file, op)) > 0) {
3538 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3539 s = trailing_symlink(nd);
3540 }
3541 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003542 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003543 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003544 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003545 return file;
3546 WARN_ON(1);
3547 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003548 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003549 fput(file);
3550 if (error == -EOPENSTALE) {
3551 if (flags & LOOKUP_RCU)
3552 error = -ECHILD;
3553 else
3554 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003555 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003556 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557}
3558
Jeff Layton669abf42012-10-10 16:43:10 -04003559struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003560 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003561{
Al Viro9883d182015-05-13 07:28:08 -04003562 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003563 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003564 struct file *filp;
3565
Al Viro9883d182015-05-13 07:28:08 -04003566 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003567 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003568 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003569 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003570 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003571 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003572 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003573 return filp;
3574}
3575
Al Viro73d049a2011-03-11 12:08:24 -05003576struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003577 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003578{
Al Viro9883d182015-05-13 07:28:08 -04003579 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003580 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003581 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003582 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003583
3584 nd.root.mnt = mnt;
3585 nd.root.dentry = dentry;
3586
David Howellsb18825a2013-09-12 19:22:53 +01003587 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003588 return ERR_PTR(-ELOOP);
3589
Paul Moore51689102015-01-22 00:00:03 -05003590 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303591 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003592 return ERR_CAST(filename);
3593
Al Viro9883d182015-05-13 07:28:08 -04003594 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003595 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003596 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003597 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003598 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003599 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003600 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003601 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003602 return file;
3603}
3604
Al Virofa14a0b2015-01-22 02:16:49 -05003605static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003606 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003608 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003609 struct qstr last;
3610 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003611 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003612 int error;
3613 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3614
3615 /*
3616 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3617 * other flags passed in are ignored!
3618 */
3619 lookup_flags &= LOOKUP_REVAL;
3620
Al Viro5c31b6c2015-05-12 17:32:54 -04003621 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3622 if (IS_ERR(name))
3623 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003625 /*
3626 * Yucky last component or no last component at all?
3627 * (foo/., foo/.., /////)
3628 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003629 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003630 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003631
Jan Karac30dabf2012-06-12 16:20:30 +02003632 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003633 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003634 /*
3635 * Do the final lookup.
3636 */
Al Viro391172c2015-05-09 11:19:16 -04003637 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003638 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003639 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003641 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003642
Al Viroa8104a92012-07-20 02:25:00 +04003643 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003644 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003645 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003646
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003647 /*
3648 * Special case - lookup gave negative, but... we had foo/bar/
3649 * From the vfs_mknod() POV we just have a negative dentry -
3650 * all is fine. Let's be bastards - you had / on the end, you've
3651 * been asking for (non-existent) directory. -ENOENT for you.
3652 */
Al Viro391172c2015-05-09 11:19:16 -04003653 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003654 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003655 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003656 }
Jan Karac30dabf2012-06-12 16:20:30 +02003657 if (unlikely(err2)) {
3658 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003659 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003660 }
Al Viro181c37b2015-05-12 17:21:25 -04003661 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663fail:
Al Viroa8104a92012-07-20 02:25:00 +04003664 dput(dentry);
3665 dentry = ERR_PTR(error);
3666unlock:
Al Viro59551022016-01-22 15:40:57 -05003667 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003668 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003669 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003670out:
Al Viro391172c2015-05-09 11:19:16 -04003671 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003672 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 return dentry;
3674}
Al Virofa14a0b2015-01-22 02:16:49 -05003675
3676struct dentry *kern_path_create(int dfd, const char *pathname,
3677 struct path *path, unsigned int lookup_flags)
3678{
Al Viro181c37b2015-05-12 17:21:25 -04003679 return filename_create(dfd, getname_kernel(pathname),
3680 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003681}
Al Virodae6ad82011-06-26 11:50:15 -04003682EXPORT_SYMBOL(kern_path_create);
3683
Al Viro921a1652012-07-20 01:15:31 +04003684void done_path_create(struct path *path, struct dentry *dentry)
3685{
3686 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003687 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003688 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003689 path_put(path);
3690}
3691EXPORT_SYMBOL(done_path_create);
3692
Al Viro520ae682015-05-13 07:00:28 -04003693inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003694 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003695{
Al Viro181c37b2015-05-12 17:21:25 -04003696 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003697}
3698EXPORT_SYMBOL(user_path_create);
3699
Al Viro1a67aaf2011-07-26 01:52:52 -04003700int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003702 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 if (error)
3705 return error;
3706
Christian Brauner9c5ccad2018-07-05 17:51:20 +02003707 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return -EPERM;
3709
Al Viroacfa4382008-12-04 10:06:33 -05003710 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 return -EPERM;
3712
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003713 error = devcgroup_inode_mknod(mode, dev);
3714 if (error)
3715 return error;
3716
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 error = security_inode_mknod(dir, dentry, mode, dev);
3718 if (error)
3719 return error;
3720
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003722 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003723 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 return error;
3725}
Al Viro4d359502014-03-14 12:20:17 -04003726EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727
Al Virof69aac02011-07-26 04:31:40 -04003728static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003729{
3730 switch (mode & S_IFMT) {
3731 case S_IFREG:
3732 case S_IFCHR:
3733 case S_IFBLK:
3734 case S_IFIFO:
3735 case S_IFSOCK:
3736 case 0: /* zero mode translates to S_IFREG */
3737 return 0;
3738 case S_IFDIR:
3739 return -EPERM;
3740 default:
3741 return -EINVAL;
3742 }
3743}
3744
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003745long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3746 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747{
Al Viro2ad94ae2008-07-21 09:32:51 -04003748 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003749 struct path path;
3750 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003751 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Al Viro8e4bfca2012-07-20 01:17:26 +04003753 error = may_mknod(mode);
3754 if (error)
3755 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003756retry:
3757 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003758 if (IS_ERR(dentry))
3759 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003760
Al Virodae6ad82011-06-26 11:50:15 -04003761 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003762 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003763 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003764 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003765 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003766 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003768 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003769 if (!error)
3770 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771 break;
3772 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003773 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 new_decode_dev(dev));
3775 break;
3776 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003777 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
Al Viroa8104a92012-07-20 02:25:00 +04003780out:
Al Viro921a1652012-07-20 01:15:31 +04003781 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003782 if (retry_estale(error, lookup_flags)) {
3783 lookup_flags |= LOOKUP_REVAL;
3784 goto retry;
3785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return error;
3787}
3788
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003789SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3790 unsigned int, dev)
3791{
3792 return do_mknodat(dfd, filename, mode, dev);
3793}
3794
Al Viro8208a222011-07-25 17:32:17 -04003795SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003796{
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003797 return do_mknodat(AT_FDCWD, filename, mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003798}
3799
Al Viro18bb1db2011-07-26 01:41:39 -04003800int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003802 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003803 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804
3805 if (error)
3806 return error;
3807
Al Viroacfa4382008-12-04 10:06:33 -05003808 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 return -EPERM;
3810
3811 mode &= (S_IRWXUGO|S_ISVTX);
3812 error = security_inode_mkdir(dir, dentry, mode);
3813 if (error)
3814 return error;
3815
Al Viro8de52772012-02-06 12:45:27 -05003816 if (max_links && dir->i_nlink >= max_links)
3817 return -EMLINK;
3818
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003820 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003821 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 return error;
3823}
Al Viro4d359502014-03-14 12:20:17 -04003824EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Dominik Brodowski0101db72018-03-11 11:34:49 +01003826long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827{
Dave Hansen6902d922006-09-30 23:29:01 -07003828 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003829 struct path path;
3830 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003831 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003833retry:
3834 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003835 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003836 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003837
Al Virodae6ad82011-06-26 11:50:15 -04003838 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003839 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003840 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003841 if (!error)
3842 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003843 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003844 if (retry_estale(error, lookup_flags)) {
3845 lookup_flags |= LOOKUP_REVAL;
3846 goto retry;
3847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 return error;
3849}
3850
Dominik Brodowski0101db72018-03-11 11:34:49 +01003851SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3852{
3853 return do_mkdirat(dfd, pathname, mode);
3854}
3855
Al Viroa218d0f2011-11-21 14:59:34 -05003856SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003857{
Dominik Brodowski0101db72018-03-11 11:34:49 +01003858 return do_mkdirat(AT_FDCWD, pathname, mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003859}
3860
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3862{
3863 int error = may_delete(dir, dentry, 1);
3864
3865 if (error)
3866 return error;
3867
Al Viroacfa4382008-12-04 10:06:33 -05003868 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 return -EPERM;
3870
Al Viro1d2ef592011-09-14 18:55:41 +01003871 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003872 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Sage Weil912dbc12011-05-24 13:06:11 -07003874 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003875 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003876 goto out;
3877
3878 error = security_inode_rmdir(dir, dentry);
3879 if (error)
3880 goto out;
3881
3882 error = dir->i_op->rmdir(dir, dentry);
3883 if (error)
3884 goto out;
3885
Al Viro87677122018-05-27 16:23:51 -04003886 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003887 dentry->d_inode->i_flags |= S_DEAD;
3888 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003889 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003890
3891out:
Al Viro59551022016-01-22 15:40:57 -05003892 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003893 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003894 if (!error)
3895 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return error;
3897}
Al Viro4d359502014-03-14 12:20:17 -04003898EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899
Dominik Brodowskif459dffa2018-03-11 11:34:48 +01003900long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901{
3902 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003903 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003905 struct path path;
3906 struct qstr last;
3907 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003908 unsigned int lookup_flags = 0;
3909retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04003910 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3911 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003912 if (IS_ERR(name))
3913 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914
Al Virof5beed72015-04-30 16:09:11 -04003915 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003916 case LAST_DOTDOT:
3917 error = -ENOTEMPTY;
3918 goto exit1;
3919 case LAST_DOT:
3920 error = -EINVAL;
3921 goto exit1;
3922 case LAST_ROOT:
3923 error = -EBUSY;
3924 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003926
Al Virof5beed72015-04-30 16:09:11 -04003927 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003928 if (error)
3929 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003930
Al Viro59551022016-01-22 15:40:57 -05003931 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003932 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003934 if (IS_ERR(dentry))
3935 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003936 if (!dentry->d_inode) {
3937 error = -ENOENT;
3938 goto exit3;
3939 }
Al Virof5beed72015-04-30 16:09:11 -04003940 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003941 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003942 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003943 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003944exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003945 dput(dentry);
3946exit2:
Al Viro59551022016-01-22 15:40:57 -05003947 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003948 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949exit1:
Al Virof5beed72015-04-30 16:09:11 -04003950 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003952 if (retry_estale(error, lookup_flags)) {
3953 lookup_flags |= LOOKUP_REVAL;
3954 goto retry;
3955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 return error;
3957}
3958
Heiko Carstens3cdad422009-01-14 14:14:22 +01003959SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003960{
3961 return do_rmdir(AT_FDCWD, pathname);
3962}
3963
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003964/**
3965 * vfs_unlink - unlink a filesystem object
3966 * @dir: parent directory
3967 * @dentry: victim
3968 * @delegated_inode: returns victim inode, if the inode is delegated.
3969 *
3970 * The caller must hold dir->i_mutex.
3971 *
3972 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3973 * return a reference to the inode in delegated_inode. The caller
3974 * should then break the delegation on that inode and retry. Because
3975 * breaking a delegation may take a long time, the caller should drop
3976 * dir->i_mutex before doing so.
3977 *
3978 * Alternatively, a caller may pass NULL for delegated_inode. This may
3979 * be appropriate for callers that expect the underlying filesystem not
3980 * to be NFS exported.
3981 */
3982int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003984 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 int error = may_delete(dir, dentry, 0);
3986
3987 if (error)
3988 return error;
3989
Al Viroacfa4382008-12-04 10:06:33 -05003990 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 return -EPERM;
3992
Al Viro59551022016-01-22 15:40:57 -05003993 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003994 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 error = -EBUSY;
3996 else {
3997 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003998 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003999 error = try_break_deleg(target, delegated_inode);
4000 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004001 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004003 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004004 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004005 detach_mounts(dentry);
4006 }
Al Virobec10522010-03-03 14:12:08 -05004007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004009out:
Al Viro59551022016-01-22 15:40:57 -05004010 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011
4012 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4013 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004014 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 }
Robert Love0eeca282005-07-12 17:06:03 -04004017
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 return error;
4019}
Al Viro4d359502014-03-14 12:20:17 -04004020EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021
4022/*
4023 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004024 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 * writeout happening, and we don't want to prevent access to the directory
4026 * while waiting on the I/O.
4027 */
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004028long do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029{
Al Viro2ad94ae2008-07-21 09:32:51 -04004030 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004032 struct path path;
4033 struct qstr last;
4034 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004036 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004037 unsigned int lookup_flags = 0;
4038retry:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004039 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004040 if (IS_ERR(name))
4041 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004042
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004044 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004046
Al Virof5beed72015-04-30 16:09:11 -04004047 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004048 if (error)
4049 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004050retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004051 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004052 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 error = PTR_ERR(dentry);
4054 if (!IS_ERR(dentry)) {
4055 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004056 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004057 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004059 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004060 goto slashes;
4061 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004062 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004063 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004064 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04004065 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004066exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 dput(dentry);
4068 }
Al Viro59551022016-01-22 15:40:57 -05004069 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 if (inode)
4071 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004072 inode = NULL;
4073 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004074 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004075 if (!error)
4076 goto retry_deleg;
4077 }
Al Virof5beed72015-04-30 16:09:11 -04004078 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079exit1:
Al Virof5beed72015-04-30 16:09:11 -04004080 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004081 if (retry_estale(error, lookup_flags)) {
4082 lookup_flags |= LOOKUP_REVAL;
4083 inode = NULL;
4084 goto retry;
4085 }
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004086 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 return error;
4088
4089slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004090 if (d_is_negative(dentry))
4091 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004092 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004093 error = -EISDIR;
4094 else
4095 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 goto exit2;
4097}
4098
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004099SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004100{
4101 if ((flag & ~AT_REMOVEDIR) != 0)
4102 return -EINVAL;
4103
4104 if (flag & AT_REMOVEDIR)
4105 return do_rmdir(dfd, pathname);
4106
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004107 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004108}
4109
Heiko Carstens3480b252009-01-14 14:14:16 +01004110SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004111{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004112 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004113}
4114
Miklos Szeredidb2e7472008-06-24 16:50:16 +02004115int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116{
Miklos Szeredia95164d2008-07-30 15:08:48 +02004117 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
4119 if (error)
4120 return error;
4121
Al Viroacfa4382008-12-04 10:06:33 -05004122 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 return -EPERM;
4124
4125 error = security_inode_symlink(dir, dentry, oldname);
4126 if (error)
4127 return error;
4128
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004130 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004131 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return error;
4133}
Al Viro4d359502014-03-14 12:20:17 -04004134EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Dominik Brodowskib724e842018-03-11 11:34:49 +01004136long do_symlinkat(const char __user *oldname, int newdfd,
4137 const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138{
Al Viro2ad94ae2008-07-21 09:32:51 -04004139 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004140 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004141 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004142 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004143 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144
4145 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004146 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004148retry:
4149 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004150 error = PTR_ERR(dentry);
4151 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004152 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004153
Jeff Layton91a27b22012-10-10 15:25:28 -04004154 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004155 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04004156 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004157 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004158 if (retry_estale(error, lookup_flags)) {
4159 lookup_flags |= LOOKUP_REVAL;
4160 goto retry;
4161 }
Dave Hansen6902d922006-09-30 23:29:01 -07004162out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 putname(from);
4164 return error;
4165}
4166
Dominik Brodowskib724e842018-03-11 11:34:49 +01004167SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4168 int, newdfd, const char __user *, newname)
4169{
4170 return do_symlinkat(oldname, newdfd, newname);
4171}
4172
Heiko Carstens3480b252009-01-14 14:14:16 +01004173SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004174{
Dominik Brodowskib724e842018-03-11 11:34:49 +01004175 return do_symlinkat(oldname, AT_FDCWD, newname);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004176}
4177
J. Bruce Fields146a8592011-09-20 17:14:31 -04004178/**
4179 * vfs_link - create a new link
4180 * @old_dentry: object to be linked
4181 * @dir: new parent
4182 * @new_dentry: where to create the new link
4183 * @delegated_inode: returns inode needing a delegation break
4184 *
4185 * The caller must hold dir->i_mutex
4186 *
4187 * If vfs_link discovers a delegation on the to-be-linked file in need
4188 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4189 * inode in delegated_inode. The caller should then break the delegation
4190 * and retry. Because breaking a delegation may take a long time, the
4191 * caller should drop the i_mutex before doing so.
4192 *
4193 * Alternatively, a caller may pass NULL for delegated_inode. This may
4194 * be appropriate for callers that expect the underlying filesystem not
4195 * to be NFS exported.
4196 */
4197int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198{
4199 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004200 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 int error;
4202
4203 if (!inode)
4204 return -ENOENT;
4205
Miklos Szeredia95164d2008-07-30 15:08:48 +02004206 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (error)
4208 return error;
4209
4210 if (dir->i_sb != inode->i_sb)
4211 return -EXDEV;
4212
4213 /*
4214 * A link to an append-only or immutable file cannot be created.
4215 */
4216 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4217 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004218 /*
4219 * Updating the link count will likely cause i_uid and i_gid to
4220 * be writen back improperly if their true value is unknown to
4221 * the vfs.
4222 */
4223 if (HAS_UNMAPPED_ID(inode))
4224 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004225 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004227 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 return -EPERM;
4229
4230 error = security_inode_link(old_dentry, dir, new_dentry);
4231 if (error)
4232 return error;
4233
Al Viro59551022016-01-22 15:40:57 -05004234 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304235 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004236 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304237 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004238 else if (max_links && inode->i_nlink >= max_links)
4239 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004240 else {
4241 error = try_break_deleg(inode, delegated_inode);
4242 if (!error)
4243 error = dir->i_op->link(old_dentry, dir, new_dentry);
4244 }
Al Virof4e0c302013-06-11 08:34:36 +04004245
4246 if (!error && (inode->i_state & I_LINKABLE)) {
4247 spin_lock(&inode->i_lock);
4248 inode->i_state &= ~I_LINKABLE;
4249 spin_unlock(&inode->i_lock);
4250 }
Al Viro59551022016-01-22 15:40:57 -05004251 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004252 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004253 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 return error;
4255}
Al Viro4d359502014-03-14 12:20:17 -04004256EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258/*
4259 * Hardlinks are often used in delicate situations. We avoid
4260 * security-related surprises by not following symlinks on the
4261 * newname. --KAB
4262 *
4263 * We don't follow them on the oldname either to be compatible
4264 * with linux 2.0, and to avoid hard-linking to directories
4265 * and other special files. --ADM
4266 */
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004267int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4268 const char __user *newname, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
4270 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004271 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004272 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304273 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304276 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004277 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304278 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004279 * To use null names we require CAP_DAC_READ_SEARCH
4280 * This ensures that not everyone will be able to create
4281 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304282 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004283 if (flags & AT_EMPTY_PATH) {
4284 if (!capable(CAP_DAC_READ_SEARCH))
4285 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304286 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004287 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004288
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304289 if (flags & AT_SYMLINK_FOLLOW)
4290 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004291retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304292 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004294 return error;
4295
Jeff Layton442e31c2012-12-20 16:15:38 -05004296 new_dentry = user_path_create(newdfd, newname, &new_path,
4297 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004299 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004300 goto out;
4301
4302 error = -EXDEV;
4303 if (old_path.mnt != new_path.mnt)
4304 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004305 error = may_linkat(&old_path);
4306 if (unlikely(error))
4307 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004308 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004309 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004310 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004311 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004312out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004313 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004314 if (delegated_inode) {
4315 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004316 if (!error) {
4317 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004318 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004319 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004320 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004321 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004322 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004323 how |= LOOKUP_REVAL;
4324 goto retry;
4325 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326out:
Al Viro2d8f3032008-07-22 09:59:21 -04004327 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328
4329 return error;
4330}
4331
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004332SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4333 int, newdfd, const char __user *, newname, int, flags)
4334{
4335 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4336}
4337
Heiko Carstens3480b252009-01-14 14:14:16 +01004338SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004339{
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004340 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004341}
4342
Miklos Szeredibc270272014-04-01 17:08:42 +02004343/**
4344 * vfs_rename - rename a filesystem object
4345 * @old_dir: parent of source
4346 * @old_dentry: source
4347 * @new_dir: parent of destination
4348 * @new_dentry: destination
4349 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004350 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004351 *
4352 * The caller must hold multiple mutexes--see lock_rename()).
4353 *
4354 * If vfs_rename discovers a delegation in need of breaking at either
4355 * the source or destination, it will return -EWOULDBLOCK and return a
4356 * reference to the inode in delegated_inode. The caller should then
4357 * break the delegation and retry. Because breaking a delegation may
4358 * take a long time, the caller should drop all locks before doing
4359 * so.
4360 *
4361 * Alternatively, a caller may pass NULL for delegated_inode. This may
4362 * be appropriate for callers that expect the underlying filesystem not
4363 * to be NFS exported.
4364 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 * The worst of all namespace operations - renaming directory. "Perverted"
4366 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4367 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004368 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004369 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 * b) race potential - two innocent renames can create a loop together.
4371 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004372 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004374 * c) we have to lock _four_ objects - parents and victim (if it exists),
4375 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004376 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 * whether the target exists). Solution: try to be smart with locking
4378 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004379 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 * move will be locked. Thus we can rank directories by the tree
4381 * (ancestors first) and rank all non-directories after them.
4382 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004383 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 * HOWEVER, it relies on the assumption that any object with ->lookup()
4385 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4386 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004387 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004388 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004390 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 * locking].
4392 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004394 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004395 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396{
4397 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004398 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004399 struct inode *source = old_dentry->d_inode;
4400 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004401 bool new_is_dir = false;
4402 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004403 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004405 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004406 return 0;
4407
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 error = may_delete(old_dir, old_dentry, is_dir);
4409 if (error)
4410 return error;
4411
Miklos Szeredida1ce062014-04-01 17:08:43 +02004412 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004413 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004414 } else {
4415 new_is_dir = d_is_dir(new_dentry);
4416
4417 if (!(flags & RENAME_EXCHANGE))
4418 error = may_delete(new_dir, new_dentry, is_dir);
4419 else
4420 error = may_delete(new_dir, new_dentry, new_is_dir);
4421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422 if (error)
4423 return error;
4424
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004425 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 return -EPERM;
4427
Miklos Szeredibc270272014-04-01 17:08:42 +02004428 /*
4429 * If we are going to change the parent - check write permissions,
4430 * we'll need to flip '..'.
4431 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004432 if (new_dir != old_dir) {
4433 if (is_dir) {
4434 error = inode_permission(source, MAY_WRITE);
4435 if (error)
4436 return error;
4437 }
4438 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4439 error = inode_permission(target, MAY_WRITE);
4440 if (error)
4441 return error;
4442 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004443 }
Robert Love0eeca282005-07-12 17:06:03 -04004444
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004445 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4446 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004447 if (error)
4448 return error;
4449
Al Viro49d31c22017-07-07 14:51:19 -04004450 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004451 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004452 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004453 lock_two_nondirectories(source, target);
4454 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004455 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004456
4457 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004458 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004459 goto out;
4460
Miklos Szeredida1ce062014-04-01 17:08:43 +02004461 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004462 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004463 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004464 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004465 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4466 old_dir->i_nlink >= max_links)
4467 goto out;
4468 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004469 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004470 error = try_break_deleg(source, delegated_inode);
4471 if (error)
4472 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004473 }
4474 if (target && !new_is_dir) {
4475 error = try_break_deleg(target, delegated_inode);
4476 if (error)
4477 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004478 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004479 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004480 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004481 if (error)
4482 goto out;
4483
Miklos Szeredida1ce062014-04-01 17:08:43 +02004484 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004485 if (is_dir) {
4486 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004487 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004488 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004489 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004490 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004491 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004492 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4493 if (!(flags & RENAME_EXCHANGE))
4494 d_move(old_dentry, new_dentry);
4495 else
4496 d_exchange(old_dentry, new_dentry);
4497 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004498out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004499 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004500 unlock_two_nondirectories(source, target);
4501 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004502 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004503 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004504 if (!error) {
Al Viro49d31c22017-07-07 14:51:19 -04004505 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004506 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4507 if (flags & RENAME_EXCHANGE) {
4508 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4509 new_is_dir, NULL, new_dentry);
4510 }
4511 }
Al Viro49d31c22017-07-07 14:51:19 -04004512 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004513
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return error;
4515}
Al Viro4d359502014-03-14 12:20:17 -04004516EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004518static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4519 const char __user *newname, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520{
Al Viro2ad94ae2008-07-21 09:32:51 -04004521 struct dentry *old_dentry, *new_dentry;
4522 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004523 struct path old_path, new_path;
4524 struct qstr old_last, new_last;
4525 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004526 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004527 struct filename *from;
4528 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004529 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004530 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004531 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004532
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004533 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004534 return -EINVAL;
4535
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004536 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4537 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004538 return -EINVAL;
4539
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004540 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4541 return -EPERM;
4542
Al Virof5beed72015-04-30 16:09:11 -04004543 if (flags & RENAME_EXCHANGE)
4544 target_flags = 0;
4545
Jeff Laytonc6a94282012-12-11 12:10:10 -05004546retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04004547 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4548 &old_path, &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004549 if (IS_ERR(from)) {
4550 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553
Al Viroc1d4dd22016-06-05 16:38:18 -04004554 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4555 &new_path, &new_last, &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004556 if (IS_ERR(to)) {
4557 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
4561 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004562 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 goto exit2;
4564
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004566 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 goto exit2;
4568
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004569 if (flags & RENAME_NOREPLACE)
4570 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004571 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 goto exit2;
4573
Al Virof5beed72015-04-30 16:09:11 -04004574 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004575 if (error)
4576 goto exit2;
4577
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004578retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004579 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Al Virof5beed72015-04-30 16:09:11 -04004581 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 error = PTR_ERR(old_dentry);
4583 if (IS_ERR(old_dentry))
4584 goto exit3;
4585 /* source must exist */
4586 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004587 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004589 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 error = PTR_ERR(new_dentry);
4591 if (IS_ERR(new_dentry))
4592 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004593 error = -EEXIST;
4594 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4595 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004596 if (flags & RENAME_EXCHANGE) {
4597 error = -ENOENT;
4598 if (d_is_negative(new_dentry))
4599 goto exit5;
4600
4601 if (!d_is_dir(new_dentry)) {
4602 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004603 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004604 goto exit5;
4605 }
4606 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004607 /* unless the source is a directory trailing slashes give -ENOTDIR */
4608 if (!d_is_dir(old_dentry)) {
4609 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004610 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004611 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004612 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004613 goto exit5;
4614 }
4615 /* source should not be ancestor of target */
4616 error = -EINVAL;
4617 if (old_dentry == trap)
4618 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004620 if (!(flags & RENAME_EXCHANGE))
4621 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (new_dentry == trap)
4623 goto exit5;
4624
Al Virof5beed72015-04-30 16:09:11 -04004625 error = security_path_rename(&old_path, old_dentry,
4626 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004627 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004628 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004629 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4630 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004631 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632exit5:
4633 dput(new_dentry);
4634exit4:
4635 dput(old_dentry);
4636exit3:
Al Virof5beed72015-04-30 16:09:11 -04004637 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004638 if (delegated_inode) {
4639 error = break_deleg_wait(&delegated_inode);
4640 if (!error)
4641 goto retry_deleg;
4642 }
Al Virof5beed72015-04-30 16:09:11 -04004643 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004645 if (retry_estale(error, lookup_flags))
4646 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004647 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004648 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649exit1:
Al Virof5beed72015-04-30 16:09:11 -04004650 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004652 if (should_retry) {
4653 should_retry = false;
4654 lookup_flags |= LOOKUP_REVAL;
4655 goto retry;
4656 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004657exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 return error;
4659}
4660
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004661SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4662 int, newdfd, const char __user *, newname, unsigned int, flags)
4663{
4664 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4665}
4666
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004667SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4668 int, newdfd, const char __user *, newname)
4669{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004670 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004671}
4672
Heiko Carstensa26eab22009-01-14 14:14:17 +01004673SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004674{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004675 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004676}
4677
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004678int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4679{
4680 int error = may_create(dir, dentry);
4681 if (error)
4682 return error;
4683
4684 if (!dir->i_op->mknod)
4685 return -EPERM;
4686
4687 return dir->i_op->mknod(dir, dentry,
4688 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4689}
4690EXPORT_SYMBOL(vfs_whiteout);
4691
Al Viro5d826c82014-03-14 13:42:45 -04004692int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Al Viro5d826c82014-03-14 13:42:45 -04004694 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 if (IS_ERR(link))
4696 goto out;
4697
4698 len = strlen(link);
4699 if (len > (unsigned) buflen)
4700 len = buflen;
4701 if (copy_to_user(buffer, link, len))
4702 len = -EFAULT;
4703out:
4704 return len;
4705}
4706
Miklos Szeredid60874c2016-10-04 14:40:45 +02004707/**
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004708 * vfs_readlink - copy symlink body into userspace buffer
4709 * @dentry: dentry on which to get symbolic link
4710 * @buffer: user memory pointer
4711 * @buflen: size of buffer
4712 *
4713 * Does not touch atime. That's up to the caller if necessary
4714 *
4715 * Does not call security hook.
4716 */
4717int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4718{
4719 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004720 DEFINE_DELAYED_CALL(done);
4721 const char *link;
4722 int res;
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004723
Miklos Szeredi76fca902016-12-09 16:45:04 +01004724 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4725 if (unlikely(inode->i_op->readlink))
4726 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004727
Miklos Szeredi76fca902016-12-09 16:45:04 +01004728 if (!d_is_symlink(dentry))
4729 return -EINVAL;
4730
4731 spin_lock(&inode->i_lock);
4732 inode->i_opflags |= IOP_DEFAULT_READLINK;
4733 spin_unlock(&inode->i_lock);
4734 }
4735
Al Virof2df5da2018-07-19 17:35:51 -04004736 link = inode->i_link;
4737 if (!link) {
4738 link = inode->i_op->get_link(dentry, inode, &done);
4739 if (IS_ERR(link))
4740 return PTR_ERR(link);
4741 }
4742 res = readlink_copy(buffer, buflen, link);
4743 do_delayed_call(&done);
4744 return res;
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004745}
4746EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
Miklos Szeredid60874c2016-10-04 14:40:45 +02004748/**
4749 * vfs_get_link - get symlink body
4750 * @dentry: dentry on which to get symbolic link
4751 * @done: caller needs to free returned data with this
4752 *
4753 * Calls security hook and i_op->get_link() on the supplied inode.
4754 *
4755 * It does not touch atime. That's up to the caller if necessary.
4756 *
4757 * Does not work on "special" symlinks like /proc/$$/fd/N
4758 */
4759const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4760{
4761 const char *res = ERR_PTR(-EINVAL);
4762 struct inode *inode = d_inode(dentry);
4763
4764 if (d_is_symlink(dentry)) {
4765 res = ERR_PTR(security_inode_readlink(dentry));
4766 if (!res)
4767 res = inode->i_op->get_link(dentry, inode, done);
4768 }
4769 return res;
4770}
4771EXPORT_SYMBOL(vfs_get_link);
4772
Linus Torvalds1da177e2005-04-16 15:20:36 -07004773/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004774const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004775 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004777 char *kaddr;
4778 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004779 struct address_space *mapping = inode->i_mapping;
4780
Al Virod3883d42015-11-17 10:41:04 -05004781 if (!dentry) {
4782 page = find_get_page(mapping, 0);
4783 if (!page)
4784 return ERR_PTR(-ECHILD);
4785 if (!PageUptodate(page)) {
4786 put_page(page);
4787 return ERR_PTR(-ECHILD);
4788 }
4789 } else {
4790 page = read_mapping_page(mapping, 0, NULL);
4791 if (IS_ERR(page))
4792 return (char*)page;
4793 }
Al Virofceef392015-12-29 15:58:39 -05004794 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004795 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4796 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004797 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004798 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799}
4800
Al Viro6b255392015-11-17 10:20:54 -05004801EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802
Al Virofceef392015-12-29 15:58:39 -05004803void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804{
Al Virofceef392015-12-29 15:58:39 -05004805 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806}
Al Viro4d359502014-03-14 12:20:17 -04004807EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808
Al Viroaa80dea2015-11-16 18:26:34 -05004809int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4810{
Al Virofceef392015-12-29 15:58:39 -05004811 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004812 int res = readlink_copy(buffer, buflen,
4813 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004814 &done));
4815 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004816 return res;
4817}
4818EXPORT_SYMBOL(page_readlink);
4819
Nick Piggin54566b22009-01-04 12:00:53 -08004820/*
4821 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4822 */
4823int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824{
4825 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004826 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004827 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004828 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004829 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004830 if (nofs)
4831 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
NeilBrown7e53cac2006-03-25 03:07:57 -08004833retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004834 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004835 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004837 goto fail;
4838
Al Viro21fc61c2015-11-17 01:07:57 -05004839 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004840
4841 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4842 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 if (err < 0)
4844 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004845 if (err < len-1)
4846 goto retry;
4847
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 mark_inode_dirty(inode);
4849 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850fail:
4851 return err;
4852}
Al Viro4d359502014-03-14 12:20:17 -04004853EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004855int page_symlink(struct inode *inode, const char *symname, int len)
4856{
4857 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004858 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004859}
Al Viro4d359502014-03-14 12:20:17 -04004860EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004861
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004862const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004863 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865EXPORT_SYMBOL(page_symlink_inode_operations);