blob: 9cbd5e79ad914d0b7c7c0b93aa6d455f247130c7 [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) {
227 struct filename *tmp;
228
229 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
230 if (unlikely(!tmp)) {
231 __putname(result);
232 return ERR_PTR(-ENOMEM);
233 }
234 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500235 result = tmp;
236 } else {
237 __putname(result);
238 return ERR_PTR(-ENAMETOOLONG);
239 }
240 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800241 result->uptr = NULL;
242 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500243 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500244 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800245
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800246 return result;
247}
248
Jeff Layton91a27b22012-10-10 15:25:28 -0400249void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
Paul Moore55422d02015-01-22 00:00:23 -0500251 BUG_ON(name->refcnt <= 0);
252
253 if (--name->refcnt > 0)
254 return;
255
Al Virofd2f7cb2015-02-22 20:07:13 -0500256 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500257 __putname(name->name);
258 kfree(name);
259 } else
260 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Linus Torvaldse77819e2011-07-22 19:30:19 -0700263static int check_acl(struct inode *inode, int mask)
264{
Linus Torvalds84635d62011-07-25 22:47:03 -0700265#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700266 struct posix_acl *acl;
267
Linus Torvaldse77819e2011-07-22 19:30:19 -0700268 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400269 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
270 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700271 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400272 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100273 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400274 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300275 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700276 }
277
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800278 acl = get_acl(inode, ACL_TYPE_ACCESS);
279 if (IS_ERR(acl))
280 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700281 if (acl) {
282 int error = posix_acl_permission(inode, acl, mask);
283 posix_acl_release(acl);
284 return error;
285 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700286#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700287
288 return -EAGAIN;
289}
290
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700291/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530292 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700293 */
Al Viro7e401452011-06-20 19:12:17 -0400294static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700295{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700296 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700297
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800298 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700299 mode >>= 6;
300 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700301 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400302 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100303 if (error != -EAGAIN)
304 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700305 }
306
307 if (in_group_p(inode->i_gid))
308 mode >>= 3;
309 }
310
311 /*
312 * If the DACs are ok we don't need any capability check.
313 */
Al Viro9c2c7032011-06-20 19:06:22 -0400314 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700315 return 0;
316 return -EACCES;
317}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100320 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530322 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
324 * Used to check for read/write/execute permissions on a file.
325 * We use "fsuid" for this, letting us set arbitrary permissions
326 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100327 * are used for other things.
328 *
329 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
330 * request cannot be satisfied (eg. requires blocking or too much complexity).
331 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
Al Viro2830ba72011-06-20 19:16:29 -0400333int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700335 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530338 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Al Viro7e401452011-06-20 19:12:17 -0400340 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700341 if (ret != -EACCES)
342 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Al Virod594e7e2011-06-20 19:55:42 -0400344 if (S_ISDIR(inode->i_mode)) {
345 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400346 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700347 if (capable_wrt_inode_uidgid(inode,
348 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400349 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700350 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500352 return -EACCES;
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 /*
356 * Searching includes executable on directories, else just read.
357 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600358 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400359 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700360 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500362 /*
363 * Read/write DACs are always overridable.
364 * Executable DACs are overridable when there is
365 * at least one exec bit set.
366 */
367 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
368 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
369 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 return -EACCES;
372}
Al Viro4d359502014-03-14 12:20:17 -0400373EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700375/*
376 * We _really_ want to just do "generic_permission()" without
377 * even looking at the inode->i_op values. So we keep a cache
378 * flag in inode->i_opflags, that says "this has not special
379 * permission function, use the fast case".
380 */
381static inline int do_inode_permission(struct inode *inode, int mask)
382{
383 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
384 if (likely(inode->i_op->permission))
385 return inode->i_op->permission(inode, mask);
386
387 /* This gets set once for the inode lifetime */
388 spin_lock(&inode->i_lock);
389 inode->i_opflags |= IOP_FASTPERM;
390 spin_unlock(&inode->i_lock);
391 }
392 return generic_permission(inode, mask);
393}
394
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200395/**
David Howells0bdaea92012-06-25 12:55:46 +0100396 * sb_permission - Check superblock-level permissions
397 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700398 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100399 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
400 *
401 * Separate out file-system wide checks from inode-specific permission checks.
402 */
403static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
404{
405 if (unlikely(mask & MAY_WRITE)) {
406 umode_t mode = inode->i_mode;
407
408 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100409 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100410 return -EROFS;
411 }
412 return 0;
413}
414
415/**
416 * inode_permission - Check for access rights to a given inode
417 * @inode: Inode to check permission on
418 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
419 *
420 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
421 * this, letting us set arbitrary permissions for filesystem access without
422 * changing the "normal" UIDs which are used for other things.
423 *
424 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
425 */
426int inode_permission(struct inode *inode, int mask)
427{
428 int retval;
429
430 retval = sb_permission(inode->i_sb, inode, mask);
431 if (retval)
432 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800433
434 if (unlikely(mask & MAY_WRITE)) {
435 /*
436 * Nobody gets write access to an immutable file.
437 */
438 if (IS_IMMUTABLE(inode))
439 return -EPERM;
440
441 /*
442 * Updating mtime will likely cause i_uid and i_gid to be
443 * written back improperly if their true value is unknown
444 * to the vfs.
445 */
446 if (HAS_UNMAPPED_ID(inode))
447 return -EACCES;
448 }
449
450 retval = do_inode_permission(inode, mask);
451 if (retval)
452 return retval;
453
454 retval = devcgroup_inode_permission(inode, mask);
455 if (retval)
456 return retval;
457
458 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100459}
Al Viro4d359502014-03-14 12:20:17 -0400460EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100461
462/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800463 * path_get - get a reference to a path
464 * @path: path to get the reference to
465 *
466 * Given a path increment the reference count to the dentry and the vfsmount.
467 */
Al Virodcf787f2013-03-01 23:51:07 -0500468void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800469{
470 mntget(path->mnt);
471 dget(path->dentry);
472}
473EXPORT_SYMBOL(path_get);
474
475/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800476 * path_put - put a reference to a path
477 * @path: path to put the reference to
478 *
479 * Given a path decrement the reference count to the dentry and the vfsmount.
480 */
Al Virodcf787f2013-03-01 23:51:07 -0500481void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482{
Jan Blunck1d957f92008-02-14 19:34:35 -0800483 dput(path->dentry);
484 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
Jan Blunck1d957f92008-02-14 19:34:35 -0800486EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Al Viro894bc8c2015-05-02 07:16:16 -0400488#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400489struct nameidata {
490 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400491 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400492 struct path root;
493 struct inode *inode; /* path.dentry.d_inode */
494 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400495 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400496 int last_type;
497 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100498 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400499 struct saved {
500 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500501 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400502 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400503 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400504 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400505 struct filename *name;
506 struct nameidata *saved;
Al Virofceef392015-12-29 15:58:39 -0500507 struct inode *link_inode;
Al Viro9883d182015-05-13 07:28:08 -0400508 unsigned root_seq;
509 int dfd;
Kees Cook3859a272016-10-28 01:22:25 -0700510} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400511
Al Viro9883d182015-05-13 07:28:08 -0400512static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400513{
NeilBrown756daf22015-03-23 13:37:38 +1100514 struct nameidata *old = current->nameidata;
515 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400516 p->dfd = dfd;
517 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100518 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400519 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100520 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400521}
522
Al Viro9883d182015-05-13 07:28:08 -0400523static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400524{
Al Viro9883d182015-05-13 07:28:08 -0400525 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100526
527 current->nameidata = old;
528 if (old)
529 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500530 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100531 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400532}
533
534static int __nd_alloc_stack(struct nameidata *nd)
535{
Al Virobc40aee2015-05-09 13:04:24 -0400536 struct saved *p;
537
538 if (nd->flags & LOOKUP_RCU) {
539 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
540 GFP_ATOMIC);
541 if (unlikely(!p))
542 return -ECHILD;
543 } else {
544 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400545 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400546 if (unlikely(!p))
547 return -ENOMEM;
548 }
Al Viro894bc8c2015-05-02 07:16:16 -0400549 memcpy(p, nd->internal, sizeof(nd->internal));
550 nd->stack = p;
551 return 0;
552}
553
Eric W. Biederman397d4252015-08-15 20:27:13 -0500554/**
555 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
556 * @path: nameidate to verify
557 *
558 * Rename can sometimes move a file or directory outside of a bind
559 * mount, path_connected allows those cases to be detected.
560 */
561static bool path_connected(const struct path *path)
562{
563 struct vfsmount *mnt = path->mnt;
564
565 /* Only bind mounts can have disconnected paths */
566 if (mnt->mnt_root == mnt->mnt_sb->s_root)
567 return true;
568
569 return is_subdir(path->dentry, mnt->mnt_root);
570}
571
Al Viro894bc8c2015-05-02 07:16:16 -0400572static inline int nd_alloc_stack(struct nameidata *nd)
573{
Al Viroda4e0be2015-05-03 20:52:15 -0400574 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400575 return 0;
576 if (likely(nd->stack != nd->internal))
577 return 0;
578 return __nd_alloc_stack(nd);
579}
580
Al Viro79733872015-05-09 12:55:43 -0400581static void drop_links(struct nameidata *nd)
582{
583 int i = nd->depth;
584 while (i--) {
585 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500586 do_delayed_call(&last->done);
587 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400588 }
589}
590
591static void terminate_walk(struct nameidata *nd)
592{
593 drop_links(nd);
594 if (!(nd->flags & LOOKUP_RCU)) {
595 int i;
596 path_put(&nd->path);
597 for (i = 0; i < nd->depth; i++)
598 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400599 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
600 path_put(&nd->root);
601 nd->root.mnt = NULL;
602 }
Al Viro79733872015-05-09 12:55:43 -0400603 } else {
604 nd->flags &= ~LOOKUP_RCU;
605 if (!(nd->flags & LOOKUP_ROOT))
606 nd->root.mnt = NULL;
607 rcu_read_unlock();
608 }
609 nd->depth = 0;
610}
611
612/* path_put is needed afterwards regardless of success or failure */
613static bool legitimize_path(struct nameidata *nd,
614 struct path *path, unsigned seq)
615{
616 int res = __legitimize_mnt(path->mnt, nd->m_seq);
617 if (unlikely(res)) {
618 if (res > 0)
619 path->mnt = NULL;
620 path->dentry = NULL;
621 return false;
622 }
623 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
624 path->dentry = NULL;
625 return false;
626 }
627 return !read_seqcount_retry(&path->dentry->d_seq, seq);
628}
629
630static bool legitimize_links(struct nameidata *nd)
631{
632 int i;
633 for (i = 0; i < nd->depth; i++) {
634 struct saved *last = nd->stack + i;
635 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
636 drop_links(nd);
637 nd->depth = i + 1;
638 return false;
639 }
640 }
641 return true;
642}
643
Al Viro19660af2011-03-25 10:32:48 -0400644/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100645 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400646 * Documentation/filesystems/path-lookup.txt). In situations when we can't
647 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500648 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400649 * mode. Refcounts are grabbed at the last known good point before rcu-walk
650 * got stuck, so ref-walk may continue from there. If this is not successful
651 * (eg. a seqcount has changed), then failure is returned and it's up to caller
652 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100653 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100654
655/**
Al Viro19660af2011-03-25 10:32:48 -0400656 * unlazy_walk - try to switch to ref-walk mode.
657 * @nd: nameidata pathwalk data
Randy Dunlap39191622011-01-08 19:36:21 -0800658 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100659 *
Al Viro4675ac32017-01-09 22:29:15 -0500660 * unlazy_walk attempts to legitimize the current nd->path and nd->root
661 * for ref-walk mode.
662 * Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400663 * Nothing should touch nameidata between unlazy_walk() failure and
664 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100665 */
Al Viro4675ac32017-01-09 22:29:15 -0500666static int unlazy_walk(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100667{
Nick Piggin31e6b012011-01-07 17:49:52 +1100668 struct dentry *parent = nd->path.dentry;
669
670 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700671
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700672 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400673 if (unlikely(!legitimize_links(nd)))
674 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500675 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
676 goto out1;
677 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
678 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq)))
679 goto out;
680 }
681 rcu_read_unlock();
682 BUG_ON(nd->inode != parent->d_inode);
683 return 0;
684
685out2:
686 nd->path.mnt = NULL;
687 nd->path.dentry = NULL;
688out1:
689 if (!(nd->flags & LOOKUP_ROOT))
690 nd->root.mnt = NULL;
691out:
692 rcu_read_unlock();
693 return -ECHILD;
694}
695
696/**
697 * unlazy_child - try to switch to ref-walk mode.
698 * @nd: nameidata pathwalk data
699 * @dentry: child of nd->path.dentry
700 * @seq: seq number to check dentry against
701 * Returns: 0 on success, -ECHILD on failure
702 *
703 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
704 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
705 * @nd. Must be called from rcu-walk context.
706 * Nothing should touch nameidata between unlazy_child() failure and
707 * terminate_walk().
708 */
709static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
710{
711 BUG_ON(!(nd->flags & LOOKUP_RCU));
712
713 nd->flags &= ~LOOKUP_RCU;
714 if (unlikely(!legitimize_links(nd)))
715 goto out2;
Al Viro79733872015-05-09 12:55:43 -0400716 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
717 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500718 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro79733872015-05-09 12:55:43 -0400719 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400720
Linus Torvalds15570082013-09-02 11:38:06 -0700721 /*
Al Viro4675ac32017-01-09 22:29:15 -0500722 * We need to move both the parent and the dentry from the RCU domain
723 * to be properly refcounted. And the sequence number in the dentry
724 * validates *both* dentry counters, since we checked the sequence
725 * number of the parent after we got the child sequence number. So we
726 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700727 */
Al Viro4675ac32017-01-09 22:29:15 -0500728 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
729 goto out;
730 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq))) {
731 rcu_read_unlock();
732 dput(dentry);
733 goto drop_root_mnt;
Al Viro19660af2011-03-25 10:32:48 -0400734 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700735 /*
736 * Sequence counts matched. Now make sure that the root is
737 * still valid and get it if required.
738 */
739 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400740 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
741 rcu_read_unlock();
742 dput(dentry);
743 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400744 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100745 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100746
Al Viro8b61e742013-11-08 12:45:01 -0500747 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100748 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400749
Al Viro79733872015-05-09 12:55:43 -0400750out2:
751 nd->path.mnt = NULL;
752out1:
753 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700754out:
Al Viro8b61e742013-11-08 12:45:01 -0500755 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700756drop_root_mnt:
757 if (!(nd->flags & LOOKUP_ROOT))
758 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100759 return -ECHILD;
760}
761
Al Viro4ce16ef32012-06-10 16:10:59 -0400762static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100763{
Al Viroa89f8332017-01-09 22:25:28 -0500764 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
765 return dentry->d_op->d_revalidate(dentry, flags);
766 else
767 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100768}
769
Al Viro9f1fafe2011-03-25 11:00:12 -0400770/**
771 * complete_walk - successful completion of path walk
772 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500773 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400774 * If we had been in RCU mode, drop out of it and legitimize nd->path.
775 * Revalidate the final result, unless we'd already done that during
776 * the path walk or the filesystem doesn't ask for it. Return 0 on
777 * success, -error on failure. In case of failure caller does not
778 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500779 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400780static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500781{
Al Viro16c2cd72011-02-22 15:50:10 -0500782 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500783 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500784
Al Viro9f1fafe2011-03-25 11:00:12 -0400785 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400786 if (!(nd->flags & LOOKUP_ROOT))
787 nd->root.mnt = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500788 if (unlikely(unlazy_walk(nd)))
Al Viro48a066e2013-09-29 22:06:07 -0400789 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400790 }
791
Al Viro16c2cd72011-02-22 15:50:10 -0500792 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500793 return 0;
794
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500795 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500796 return 0;
797
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500798 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500799 if (status > 0)
800 return 0;
801
Al Viro16c2cd72011-02-22 15:50:10 -0500802 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500803 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500804
Jeff Layton39159de2009-12-07 12:01:50 -0500805 return status;
806}
807
Al Viro18d8c862015-05-12 16:32:34 -0400808static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400809{
Al Viro7bd88372014-09-13 21:55:46 -0400810 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100811
Al Viro9e6697e2015-12-05 20:07:21 -0500812 if (nd->flags & LOOKUP_RCU) {
813 unsigned seq;
814
815 do {
816 seq = read_seqcount_begin(&fs->seq);
817 nd->root = fs->root;
818 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
819 } while (read_seqcount_retry(&fs->seq, seq));
820 } else {
821 get_fs_root(fs, &nd->root);
822 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100823}
824
Jan Blunck1d957f92008-02-14 19:34:35 -0800825static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700826{
827 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800828 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700829 mntput(path->mnt);
830}
831
Nick Piggin7b9337a2011-01-14 08:42:43 +0000832static inline void path_to_nameidata(const struct path *path,
833 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700834{
Nick Piggin31e6b012011-01-07 17:49:52 +1100835 if (!(nd->flags & LOOKUP_RCU)) {
836 dput(nd->path.dentry);
837 if (nd->path.mnt != path->mnt)
838 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800839 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100840 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800841 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700842}
843
Al Viro248fb5b2015-12-05 20:51:58 -0500844static int nd_jump_root(struct nameidata *nd)
845{
846 if (nd->flags & LOOKUP_RCU) {
847 struct dentry *d;
848 nd->path = nd->root;
849 d = nd->path.dentry;
850 nd->inode = d->d_inode;
851 nd->seq = nd->root_seq;
852 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
853 return -ECHILD;
854 } else {
855 path_put(&nd->path);
856 nd->path = nd->root;
857 path_get(&nd->path);
858 nd->inode = nd->path.dentry->d_inode;
859 }
860 nd->flags |= LOOKUP_JUMPED;
861 return 0;
862}
863
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400864/*
Al Viro6b255392015-11-17 10:20:54 -0500865 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400866 * caller must have taken a reference to path beforehand.
867 */
Al Viro6e77137b2015-05-02 13:37:52 -0400868void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400869{
Al Viro6e77137b2015-05-02 13:37:52 -0400870 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400871 path_put(&nd->path);
872
873 nd->path = *path;
874 nd->inode = nd->path.dentry->d_inode;
875 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400876}
877
Al Virob9ff4422015-05-02 20:19:23 -0400878static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400879{
Al Viro21c30032015-05-03 21:06:24 -0400880 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -0500881 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -0400882 if (!(nd->flags & LOOKUP_RCU))
883 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400884}
885
Linus Torvalds561ec642012-10-26 10:05:07 -0700886int sysctl_protected_symlinks __read_mostly = 0;
887int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700888
889/**
890 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700891 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700892 *
893 * In the case of the sysctl_protected_symlinks sysctl being enabled,
894 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
895 * in a sticky world-writable directory. This is to protect privileged
896 * processes from failing races against path names that may change out
897 * from under them by way of other users creating malicious symlinks.
898 * It will permit symlinks to be followed only when outside a sticky
899 * world-writable directory, or when the uid of the symlink and follower
900 * match, or when the directory owner matches the symlink's owner.
901 *
902 * Returns 0 if following the symlink is allowed, -ve on error.
903 */
Al Virofec2fa22015-05-06 15:58:18 -0400904static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700905{
906 const struct inode *inode;
907 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500908 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700909
910 if (!sysctl_protected_symlinks)
911 return 0;
912
913 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500914 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700915 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700916 return 0;
917
918 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400919 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700920 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
921 return 0;
922
923 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500924 puid = parent->i_uid;
925 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700926 return 0;
927
Al Viro319565022015-05-07 20:37:40 -0400928 if (nd->flags & LOOKUP_RCU)
929 return -ECHILD;
930
Al Viro1cf26652015-05-06 16:01:56 -0400931 audit_log_link_denied("follow_link", &nd->stack[0].link);
Kees Cook800179c2012-07-25 17:29:07 -0700932 return -EACCES;
933}
934
935/**
936 * safe_hardlink_source - Check for safe hardlink conditions
937 * @inode: the source inode to hardlink from
938 *
939 * Return false if at least one of the following conditions:
940 * - inode is not a regular file
941 * - inode is setuid
942 * - inode is setgid and group-exec
943 * - access failure for read and write
944 *
945 * Otherwise returns true.
946 */
947static bool safe_hardlink_source(struct inode *inode)
948{
949 umode_t mode = inode->i_mode;
950
951 /* Special files should not get pinned to the filesystem. */
952 if (!S_ISREG(mode))
953 return false;
954
955 /* Setuid files should not get pinned to the filesystem. */
956 if (mode & S_ISUID)
957 return false;
958
959 /* Executable setgid files should not get pinned to the filesystem. */
960 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
961 return false;
962
963 /* Hardlinking to unreadable or unwritable sources is dangerous. */
964 if (inode_permission(inode, MAY_READ | MAY_WRITE))
965 return false;
966
967 return true;
968}
969
970/**
971 * may_linkat - Check permissions for creating a hardlink
972 * @link: the source to hardlink from
973 *
974 * Block hardlink when all of:
975 * - sysctl_protected_hardlinks enabled
976 * - fsuid does not match inode
977 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200978 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700979 *
980 * Returns 0 if successful, -ve on error.
981 */
982static int may_linkat(struct path *link)
983{
Kees Cook800179c2012-07-25 17:29:07 -0700984 struct inode *inode;
985
986 if (!sysctl_protected_hardlinks)
987 return 0;
988
Kees Cook800179c2012-07-25 17:29:07 -0700989 inode = link->dentry->d_inode;
990
991 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
992 * otherwise, it must be a safe source.
993 */
Kees Cookcc658db2017-06-21 09:53:06 -0700994 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
Kees Cook800179c2012-07-25 17:29:07 -0700995 return 0;
996
Kees Cooka51d9ea2012-07-25 17:29:08 -0700997 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700998 return -EPERM;
999}
1000
Al Viro3b2e7f72015-04-19 00:53:50 -04001001static __always_inline
1002const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001003{
Al Viroab104922015-05-10 11:50:01 -04001004 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001005 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -05001006 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001007 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001008 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001009
NeilBrown8fa9dd22015-03-23 13:37:40 +11001010 if (!(nd->flags & LOOKUP_RCU)) {
1011 touch_atime(&last->link);
1012 cond_resched();
Miklos Szeredi598e3c82016-09-16 12:44:20 +02001013 } else if (atime_needs_update_rcu(&last->link, inode)) {
Al Viro4675ac32017-01-09 22:29:15 -05001014 if (unlikely(unlazy_walk(nd)))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001015 return ERR_PTR(-ECHILD);
1016 touch_atime(&last->link);
1017 }
1018
NeilBrownbda0be72015-03-23 13:37:39 +11001019 error = security_inode_follow_link(dentry, inode,
1020 nd->flags & LOOKUP_RCU);
1021 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001022 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001023
Al Viro86acdca12009-12-22 23:45:11 -05001024 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001025 res = inode->i_link;
1026 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001027 const char * (*get)(struct dentry *, struct inode *,
1028 struct delayed_call *);
1029 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001030 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001031 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001032 if (res == ERR_PTR(-ECHILD)) {
Al Viro4675ac32017-01-09 22:29:15 -05001033 if (unlikely(unlazy_walk(nd)))
Al Viro6b255392015-11-17 10:20:54 -05001034 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001035 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001036 }
1037 } else {
Al Virofceef392015-12-29 15:58:39 -05001038 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001039 }
Al Virofceef392015-12-29 15:58:39 -05001040 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001041 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001042 }
Al Virofab51e82015-05-10 11:01:00 -04001043 if (*res == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05001044 if (!nd->root.mnt)
1045 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05001046 if (unlikely(nd_jump_root(nd)))
1047 return ERR_PTR(-ECHILD);
Al Virofab51e82015-05-10 11:01:00 -04001048 while (unlikely(*++res == '/'))
1049 ;
1050 }
1051 if (!*res)
1052 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001053 return res;
1054}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001055
David Howellsf015f1262012-06-25 12:55:28 +01001056/*
1057 * follow_up - Find the mountpoint of path's vfsmount
1058 *
1059 * Given a path, find the mountpoint of its source file system.
1060 * Replace @path with the path of the mountpoint in the parent mount.
1061 * Up is towards /.
1062 *
1063 * Return 1 if we went up a level and 0 if we were already at the
1064 * root.
1065 */
Al Virobab77eb2009-04-18 03:26:48 -04001066int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Al Viro0714a532011-11-24 22:19:58 -05001068 struct mount *mnt = real_mount(path->mnt);
1069 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001071
Al Viro48a066e2013-09-29 22:06:07 -04001072 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001073 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001074 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001075 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 return 0;
1077 }
Al Viro0714a532011-11-24 22:19:58 -05001078 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001079 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001080 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001081 dput(path->dentry);
1082 path->dentry = mountpoint;
1083 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001084 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 return 1;
1086}
Al Viro4d359502014-03-14 12:20:17 -04001087EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001089/*
David Howells9875cf82011-01-14 18:45:21 +00001090 * Perform an automount
1091 * - return -EISDIR to tell follow_managed() to stop and return the path we
1092 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 */
NeilBrown756daf22015-03-23 13:37:38 +11001094static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001095 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001096{
David Howells9875cf82011-01-14 18:45:21 +00001097 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001098 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001099
David Howells9875cf82011-01-14 18:45:21 +00001100 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1101 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001102
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001103 /* We don't want to mount if someone's just doing a stat -
1104 * unless they're stat'ing a directory and appended a '/' to
1105 * the name.
1106 *
1107 * We do, however, want to mount if someone wants to open or
1108 * create a file of any type under the mountpoint, wants to
1109 * traverse through the mountpoint or wants to open the
1110 * mounted directory. Also, autofs may mark negative dentries
1111 * as being automount points. These will need the attentions
1112 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001113 */
NeilBrown756daf22015-03-23 13:37:38 +11001114 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001115 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1116 path->dentry->d_inode)
1117 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001118
NeilBrown756daf22015-03-23 13:37:38 +11001119 nd->total_link_count++;
1120 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001121 return -ELOOP;
1122
1123 mnt = path->dentry->d_op->d_automount(path);
1124 if (IS_ERR(mnt)) {
1125 /*
1126 * The filesystem is allowed to return -EISDIR here to indicate
1127 * it doesn't want to automount. For instance, autofs would do
1128 * this so that its userspace daemon can mount on this dentry.
1129 *
1130 * However, we can only permit this if it's a terminal point in
1131 * the path being looked up; if it wasn't then the remainder of
1132 * the path is inaccessible and we should say so.
1133 */
NeilBrown756daf22015-03-23 13:37:38 +11001134 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001135 return -EREMOTE;
1136 return PTR_ERR(mnt);
1137 }
David Howellsea5b7782011-01-14 19:10:03 +00001138
David Howells9875cf82011-01-14 18:45:21 +00001139 if (!mnt) /* mount collision */
1140 return 0;
1141
Al Viro8aef1882011-06-16 15:10:06 +01001142 if (!*need_mntput) {
1143 /* lock_mount() may release path->mnt on error */
1144 mntget(path->mnt);
1145 *need_mntput = true;
1146 }
Al Viro19a167a2011-01-17 01:35:23 -05001147 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001148
David Howellsea5b7782011-01-14 19:10:03 +00001149 switch (err) {
1150 case -EBUSY:
1151 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001152 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001153 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001154 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001155 path->mnt = mnt;
1156 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001157 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001158 default:
1159 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001160 }
Al Viro19a167a2011-01-17 01:35:23 -05001161
David Howells9875cf82011-01-14 18:45:21 +00001162}
1163
1164/*
1165 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001166 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001167 * - Flagged as mountpoint
1168 * - Flagged as automount point
1169 *
1170 * This may only be called in refwalk mode.
1171 *
1172 * Serialization is taken care of in namespace.c
1173 */
NeilBrown756daf22015-03-23 13:37:38 +11001174static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001175{
Al Viro8aef1882011-06-16 15:10:06 +01001176 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001177 unsigned managed;
1178 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001179 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001180
1181 /* Given that we're not holding a lock here, we retain the value in a
1182 * local variable for each dentry as we look at it so that we don't see
1183 * the components of that value change under us */
Mark Rutland6aa7de02017-10-23 14:07:29 -07001184 while (managed = READ_ONCE(path->dentry->d_flags),
David Howells9875cf82011-01-14 18:45:21 +00001185 managed &= DCACHE_MANAGED_DENTRY,
1186 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001187 /* Allow the filesystem to manage the transit without i_mutex
1188 * being held. */
1189 if (managed & DCACHE_MANAGE_TRANSIT) {
1190 BUG_ON(!path->dentry->d_op);
1191 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001192 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001193 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001194 break;
David Howellscc53ce52011-01-14 18:45:26 +00001195 }
1196
David Howells9875cf82011-01-14 18:45:21 +00001197 /* Transit to a mounted filesystem. */
1198 if (managed & DCACHE_MOUNTED) {
1199 struct vfsmount *mounted = lookup_mnt(path);
1200 if (mounted) {
1201 dput(path->dentry);
1202 if (need_mntput)
1203 mntput(path->mnt);
1204 path->mnt = mounted;
1205 path->dentry = dget(mounted->mnt_root);
1206 need_mntput = true;
1207 continue;
1208 }
1209
1210 /* Something is mounted on this dentry in another
1211 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001212 * namespace got unmounted before lookup_mnt() could
1213 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001214 }
1215
1216 /* Handle an automount point */
1217 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001218 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001219 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001220 break;
David Howells9875cf82011-01-14 18:45:21 +00001221 continue;
1222 }
1223
1224 /* We didn't change the current path point */
1225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
Al Viro8aef1882011-06-16 15:10:06 +01001227
1228 if (need_mntput && path->mnt == mnt)
1229 mntput(path->mnt);
Al Viroe9742b52016-03-05 22:04:59 -05001230 if (ret == -EISDIR || !ret)
1231 ret = 1;
Al Viro84027522015-04-22 10:30:08 -04001232 if (need_mntput)
1233 nd->flags |= LOOKUP_JUMPED;
1234 if (unlikely(ret < 0))
1235 path_put_conditional(path, nd);
1236 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237}
1238
David Howellscc53ce52011-01-14 18:45:26 +00001239int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct vfsmount *mounted;
1242
Al Viro1c755af2009-04-18 14:06:57 -04001243 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001245 dput(path->dentry);
1246 mntput(path->mnt);
1247 path->mnt = mounted;
1248 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 return 1;
1250 }
1251 return 0;
1252}
Al Viro4d359502014-03-14 12:20:17 -04001253EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Ian Kentfb5f51c2016-11-24 08:03:41 +11001255static inline int managed_dentry_rcu(const struct path *path)
Ian Kent62a73752011-03-25 01:51:02 +08001256{
Ian Kentfb5f51c2016-11-24 08:03:41 +11001257 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1258 path->dentry->d_op->d_manage(path, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001259}
1260
David Howells9875cf82011-01-14 18:45:21 +00001261/*
Al Viro287548e2011-05-27 06:50:06 -04001262 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1263 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001264 */
1265static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001266 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001267{
Ian Kent62a73752011-03-25 01:51:02 +08001268 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001269 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001270 /*
1271 * Don't forget we might have a non-mountpoint managed dentry
1272 * that wants to block transit.
1273 */
Ian Kentfb5f51c2016-11-24 08:03:41 +11001274 switch (managed_dentry_rcu(path)) {
NeilBrownb8faf032014-08-04 17:06:29 +10001275 case -ECHILD:
1276 default:
David Howellsab909112011-01-14 18:46:51 +00001277 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001278 case -EISDIR:
1279 return true;
1280 case 0:
1281 break;
1282 }
Ian Kent62a73752011-03-25 01:51:02 +08001283
1284 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001285 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001286
Al Viro474279d2013-10-01 16:11:26 -04001287 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001288 if (!mounted)
1289 break;
Al Viroc7105362011-11-24 18:22:03 -05001290 path->mnt = &mounted->mnt;
1291 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001292 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001293 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001294 /*
1295 * Update the inode too. We don't need to re-check the
1296 * dentry sequence number here after this d_inode read,
1297 * because a mount-point is always pinned.
1298 */
1299 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001300 }
Al Virof5be3e29122014-09-13 21:50:45 -04001301 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001302 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001303}
1304
Nick Piggin31e6b012011-01-07 17:49:52 +11001305static int follow_dotdot_rcu(struct nameidata *nd)
1306{
Al Viro4023bfc2014-09-13 21:59:43 -04001307 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001308
David Howells9875cf82011-01-14 18:45:21 +00001309 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001310 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001311 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001312 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1313 struct dentry *old = nd->path.dentry;
1314 struct dentry *parent = old->d_parent;
1315 unsigned seq;
1316
Al Viro4023bfc2014-09-13 21:59:43 -04001317 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001318 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001319 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1320 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001321 nd->path.dentry = parent;
1322 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001323 if (unlikely(!path_connected(&nd->path)))
1324 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001325 break;
Al Viroaed434a2015-05-12 12:22:47 -04001326 } else {
1327 struct mount *mnt = real_mount(nd->path.mnt);
1328 struct mount *mparent = mnt->mnt_parent;
1329 struct dentry *mountpoint = mnt->mnt_mountpoint;
1330 struct inode *inode2 = mountpoint->d_inode;
1331 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1332 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1333 return -ECHILD;
1334 if (&mparent->mnt == nd->path.mnt)
1335 break;
1336 /* we know that mountpoint was pinned */
1337 nd->path.dentry = mountpoint;
1338 nd->path.mnt = &mparent->mnt;
1339 inode = inode2;
1340 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001341 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001342 }
Al Viroaed434a2015-05-12 12:22:47 -04001343 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001344 struct mount *mounted;
1345 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001346 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1347 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001348 if (!mounted)
1349 break;
1350 nd->path.mnt = &mounted->mnt;
1351 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001352 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001353 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001354 }
Al Viro4023bfc2014-09-13 21:59:43 -04001355 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001356 return 0;
1357}
1358
David Howells9875cf82011-01-14 18:45:21 +00001359/*
David Howellscc53ce52011-01-14 18:45:26 +00001360 * Follow down to the covering mount currently visible to userspace. At each
1361 * point, the filesystem owning that dentry may be queried as to whether the
1362 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001363 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001364int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001365{
1366 unsigned managed;
1367 int ret;
1368
Mark Rutland6aa7de02017-10-23 14:07:29 -07001369 while (managed = READ_ONCE(path->dentry->d_flags),
David Howellscc53ce52011-01-14 18:45:26 +00001370 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1371 /* Allow the filesystem to manage the transit without i_mutex
1372 * being held.
1373 *
1374 * We indicate to the filesystem if someone is trying to mount
1375 * something here. This gives autofs the chance to deny anyone
1376 * other than its daemon the right to mount on its
1377 * superstructure.
1378 *
1379 * The filesystem may sleep at this point.
1380 */
1381 if (managed & DCACHE_MANAGE_TRANSIT) {
1382 BUG_ON(!path->dentry->d_op);
1383 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001384 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001385 if (ret < 0)
1386 return ret == -EISDIR ? 0 : ret;
1387 }
1388
1389 /* Transit to a mounted filesystem. */
1390 if (managed & DCACHE_MOUNTED) {
1391 struct vfsmount *mounted = lookup_mnt(path);
1392 if (!mounted)
1393 break;
1394 dput(path->dentry);
1395 mntput(path->mnt);
1396 path->mnt = mounted;
1397 path->dentry = dget(mounted->mnt_root);
1398 continue;
1399 }
1400
1401 /* Don't handle automount points here */
1402 break;
1403 }
1404 return 0;
1405}
Al Viro4d359502014-03-14 12:20:17 -04001406EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001407
1408/*
David Howells9875cf82011-01-14 18:45:21 +00001409 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1410 */
1411static void follow_mount(struct path *path)
1412{
1413 while (d_mountpoint(path->dentry)) {
1414 struct vfsmount *mounted = lookup_mnt(path);
1415 if (!mounted)
1416 break;
1417 dput(path->dentry);
1418 mntput(path->mnt);
1419 path->mnt = mounted;
1420 path->dentry = dget(mounted->mnt_root);
1421 }
1422}
1423
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001424static int path_parent_directory(struct path *path)
1425{
1426 struct dentry *old = path->dentry;
1427 /* rare case of legitimate dget_parent()... */
1428 path->dentry = dget_parent(path->dentry);
1429 dput(old);
1430 if (unlikely(!path_connected(path)))
1431 return -ENOENT;
1432 return 0;
1433}
1434
Eric W. Biederman397d4252015-08-15 20:27:13 -05001435static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436{
1437 while(1) {
Al Viro2a737872009-04-07 11:49:53 -04001438 if (nd->path.dentry == nd->root.dentry &&
1439 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 break;
1441 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001442 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001443 int ret = path_parent_directory(&nd->path);
1444 if (ret)
1445 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 break;
1447 }
Al Viro3088dd72010-01-30 15:47:29 -05001448 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
Al Viro79ed0222009-04-18 13:59:41 -04001451 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001452 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454}
1455
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001457 * This looks up the name in dcache and possibly revalidates the found dentry.
1458 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001459 */
Al Viroe3c13922016-03-06 14:03:27 -05001460static struct dentry *lookup_dcache(const struct qstr *name,
1461 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001462 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001463{
Al Viroa89f8332017-01-09 22:25:28 -05001464 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001465 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001466 int error = d_revalidate(dentry, flags);
1467 if (unlikely(error <= 0)) {
1468 if (!error)
1469 d_invalidate(dentry);
1470 dput(dentry);
1471 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001472 }
1473 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001474 return dentry;
1475}
1476
1477/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001478 * Call i_op->lookup on the dentry. The dentry must be negative and
1479 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001480 *
1481 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001482 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001483static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001484 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001485{
Josef Bacik44396f42011-05-31 11:58:49 -04001486 struct dentry *old;
1487
1488 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001489 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001490 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001491 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001492 }
Josef Bacik44396f42011-05-31 11:58:49 -04001493
Al Viro72bd8662012-06-10 17:17:17 -04001494 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001495 if (unlikely(old)) {
1496 dput(dentry);
1497 dentry = old;
1498 }
1499 return dentry;
1500}
1501
Al Viroe3c13922016-03-06 14:03:27 -05001502static struct dentry *__lookup_hash(const struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001503 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001504{
Al Viro6c51e512016-03-05 20:09:32 -05001505 struct dentry *dentry = lookup_dcache(name, base, flags);
Al Viroa3255542012-03-30 14:41:51 -04001506
Al Viro6c51e512016-03-05 20:09:32 -05001507 if (dentry)
Miklos Szeredibad61182012-03-26 12:54:24 +02001508 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001509
Al Viro6c51e512016-03-05 20:09:32 -05001510 dentry = d_alloc(base, name);
1511 if (unlikely(!dentry))
1512 return ERR_PTR(-ENOMEM);
1513
Al Viro72bd8662012-06-10 17:17:17 -04001514 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001515}
1516
Al Viroe97cdc82013-01-24 18:16:00 -05001517static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001518 struct path *path, struct inode **inode,
1519 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
Jan Blunck4ac91372008-02-14 19:34:32 -08001521 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001522 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001523 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001524 int err;
1525
Al Viro3cac2602009-08-13 18:27:43 +04001526 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001527 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001528 * of a false negative due to a concurrent rename, the caller is
1529 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001530 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001531 if (nd->flags & LOOKUP_RCU) {
1532 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001533 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001534 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001535 if (unlikely(!dentry)) {
Al Viro4675ac32017-01-09 22:29:15 -05001536 if (unlazy_walk(nd))
Al Viro5d0f49c2016-03-05 21:32:53 -05001537 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001538 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001539 }
Al Viro5a18fff2011-03-11 04:44:53 -05001540
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001541 /*
1542 * This sequence count validates that the inode matches
1543 * the dentry name information from lookup.
1544 */
David Howells63afdfc2015-05-06 15:59:00 +01001545 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001546 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001547 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001548 return -ECHILD;
1549
1550 /*
1551 * This sequence count validates that the parent had no
1552 * changes while we did the lookup of the dentry above.
1553 *
1554 * The memory barrier in read_seqcount_begin of child is
1555 * enough, we can use __read_seqcount_retry here.
1556 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001557 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001558 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001559
Al Viro254cf582015-05-05 09:40:46 -04001560 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001561 status = d_revalidate(dentry, nd->flags);
Al Viro209a7fb2017-01-09 01:35:39 -05001562 if (likely(status > 0)) {
Al Viro5d0f49c2016-03-05 21:32:53 -05001563 /*
1564 * Note: do negative dentry check after revalidation in
1565 * case that drops it.
1566 */
1567 if (unlikely(negative))
1568 return -ENOENT;
1569 path->mnt = mnt;
1570 path->dentry = dentry;
1571 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001572 return 1;
Al Viro24643082011-02-15 01:26:22 -05001573 }
Al Viro4675ac32017-01-09 22:29:15 -05001574 if (unlazy_child(nd, dentry, seq))
Al Viro209a7fb2017-01-09 01:35:39 -05001575 return -ECHILD;
1576 if (unlikely(status == -ECHILD))
1577 /* we'd been told to redo it in non-rcu mode */
1578 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001579 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001580 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001581 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001582 return 0;
Al Viroa89f8332017-01-09 22:25:28 -05001583 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001584 }
Al Viro5a18fff2011-03-11 04:44:53 -05001585 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001586 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001587 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001588 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001589 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001590 }
Al Viro766c4cb2015-05-07 19:24:57 -04001591 if (unlikely(d_is_negative(dentry))) {
1592 dput(dentry);
1593 return -ENOENT;
1594 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001595
David Howells9875cf82011-01-14 18:45:21 +00001596 path->mnt = mnt;
1597 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001598 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001599 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001600 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001601 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001602}
1603
1604/* Fast lookup failed, do it the slow way */
Al Viroe3c13922016-03-06 14:03:27 -05001605static struct dentry *lookup_slow(const struct qstr *name,
1606 struct dentry *dir,
1607 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001608{
Al Viro94bdd652016-04-15 02:42:04 -04001609 struct dentry *dentry = ERR_PTR(-ENOENT), *old;
Al Viro19363862016-04-14 19:33:34 -04001610 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001611 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001612
Al Viro9902af72016-04-15 15:08:36 -04001613 inode_lock_shared(inode);
Al Viro19363862016-04-14 19:33:34 -04001614 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001615 if (unlikely(IS_DEADDIR(inode)))
1616 goto out;
1617again:
Al Virod9171b92016-04-15 03:33:13 -04001618 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001619 if (IS_ERR(dentry))
1620 goto out;
1621 if (unlikely(!d_in_lookup(dentry))) {
Al Viroa89f8332017-01-09 22:25:28 -05001622 if (!(flags & LOOKUP_NO_REVAL)) {
Al Viro949a8522016-03-06 14:20:52 -05001623 int error = d_revalidate(dentry, flags);
1624 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001625 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001626 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001627 dput(dentry);
1628 goto again;
1629 }
Al Viro949a8522016-03-06 14:20:52 -05001630 dput(dentry);
1631 dentry = ERR_PTR(error);
1632 }
1633 }
Al Viro94bdd652016-04-15 02:42:04 -04001634 } else {
1635 old = inode->i_op->lookup(inode, dentry, flags);
1636 d_lookup_done(dentry);
1637 if (unlikely(old)) {
1638 dput(dentry);
1639 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001640 }
1641 }
Al Viro94bdd652016-04-15 02:42:04 -04001642out:
Al Viro9902af72016-04-15 15:08:36 -04001643 inode_unlock_shared(inode);
Al Viroe3c13922016-03-06 14:03:27 -05001644 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645}
1646
Al Viro52094c82011-02-21 21:34:47 -05001647static inline int may_lookup(struct nameidata *nd)
1648{
1649 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001650 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001651 if (err != -ECHILD)
1652 return err;
Al Viro4675ac32017-01-09 22:29:15 -05001653 if (unlazy_walk(nd))
Al Viro52094c82011-02-21 21:34:47 -05001654 return -ECHILD;
1655 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001656 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001657}
1658
Al Viro9856fa12011-03-04 14:22:06 -05001659static inline int handle_dots(struct nameidata *nd, int type)
1660{
1661 if (type == LAST_DOTDOT) {
Al Viro9e6697e2015-12-05 20:07:21 -05001662 if (!nd->root.mnt)
1663 set_root(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001664 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001665 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001666 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001667 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001668 }
1669 return 0;
1670}
1671
Al Viro181548c2015-05-07 19:54:34 -04001672static int pick_link(struct nameidata *nd, struct path *link,
1673 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001674{
Al Viro626de992015-05-04 18:26:59 -04001675 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001676 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001677 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001678 path_to_nameidata(link, nd);
1679 return -ELOOP;
1680 }
Al Virobc40aee2015-05-09 13:04:24 -04001681 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001682 if (link->mnt == nd->path.mnt)
1683 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001684 }
Al Viro626de992015-05-04 18:26:59 -04001685 error = nd_alloc_stack(nd);
1686 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001687 if (error == -ECHILD) {
Al Viroad1633a2017-01-08 22:35:31 -05001688 if (unlikely(!legitimize_path(nd, link, seq))) {
1689 drop_links(nd);
1690 nd->depth = 0;
1691 nd->flags &= ~LOOKUP_RCU;
1692 nd->path.mnt = NULL;
1693 nd->path.dentry = NULL;
1694 if (!(nd->flags & LOOKUP_ROOT))
1695 nd->root.mnt = NULL;
1696 rcu_read_unlock();
Al Viro4675ac32017-01-09 22:29:15 -05001697 } else if (likely(unlazy_walk(nd)) == 0)
Al Viroad1633a2017-01-08 22:35:31 -05001698 error = nd_alloc_stack(nd);
Al Virobc40aee2015-05-09 13:04:24 -04001699 }
1700 if (error) {
1701 path_put(link);
1702 return error;
1703 }
Al Viro626de992015-05-04 18:26:59 -04001704 }
1705
Al Viroab104922015-05-10 11:50:01 -04001706 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001707 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001708 clear_delayed_call(&last->done);
1709 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001710 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001711 return 1;
1712}
1713
Al Viro8f64fb12016-11-14 01:50:26 -05001714enum {WALK_FOLLOW = 1, WALK_MORE = 2};
Al Viro31d66bc2016-11-14 01:43:34 -05001715
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001716/*
1717 * Do we need to follow links? We _really_ want to be able
1718 * to do this check without having to look at inode->i_op,
1719 * so we keep a cache of "no, this doesn't need follow_link"
1720 * for the common case.
1721 */
Al Viro8f64fb12016-11-14 01:50:26 -05001722static inline int step_into(struct nameidata *nd, struct path *path,
1723 int flags, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001724{
Al Viro31d66bc2016-11-14 01:43:34 -05001725 if (!(flags & WALK_MORE) && nd->depth)
1726 put_link(nd);
Al Viro8f64fb12016-11-14 01:50:26 -05001727 if (likely(!d_is_symlink(path->dentry)) ||
1728 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1729 /* not a symlink or should not follow */
1730 path_to_nameidata(path, nd);
1731 nd->inode = inode;
1732 nd->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001733 return 0;
Al Viro8f64fb12016-11-14 01:50:26 -05001734 }
Al Viroa7f77542016-02-27 19:31:01 -05001735 /* make sure that d_is_symlink above matches inode */
1736 if (nd->flags & LOOKUP_RCU) {
Al Viro8f64fb12016-11-14 01:50:26 -05001737 if (read_seqcount_retry(&path->dentry->d_seq, seq))
Al Viroa7f77542016-02-27 19:31:01 -05001738 return -ECHILD;
1739 }
Al Viro8f64fb12016-11-14 01:50:26 -05001740 return pick_link(nd, path, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001741}
1742
Al Viro4693a542015-05-04 17:47:11 -04001743static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001744{
Al Virocaa856342015-04-22 17:52:47 -04001745 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001746 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001747 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001748 int err;
1749 /*
1750 * "." and ".." are special - ".." especially so because it has
1751 * to be able to know about the current root directory and
1752 * parent relationships.
1753 */
Al Viro4693a542015-05-04 17:47:11 -04001754 if (unlikely(nd->last_type != LAST_NORM)) {
1755 err = handle_dots(nd, nd->last_type);
Al Viro1c4ff1a2016-11-14 01:39:36 -05001756 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001757 put_link(nd);
1758 return err;
1759 }
Al Viro254cf582015-05-05 09:40:46 -04001760 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001761 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001762 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001763 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001764 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1765 nd->flags);
1766 if (IS_ERR(path.dentry))
1767 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001768
Al Viroe3c13922016-03-06 14:03:27 -05001769 path.mnt = nd->path.mnt;
1770 err = follow_managed(&path, nd);
1771 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001772 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001773
Al Viro7500c382016-03-31 00:23:05 -04001774 if (unlikely(d_is_negative(path.dentry))) {
1775 path_to_nameidata(&path, nd);
1776 return -ENOENT;
1777 }
1778
Al Viro254cf582015-05-05 09:40:46 -04001779 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001780 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001781 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001782
Al Viro8f64fb12016-11-14 01:50:26 -05001783 return step_into(nd, &path, flags, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001784}
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001787 * We can do the critical dentry name comparison and hashing
1788 * operations one word at a time, but we are limited to:
1789 *
1790 * - Architectures with fast unaligned word accesses. We could
1791 * do a "get_unaligned()" if this helps and is sufficiently
1792 * fast.
1793 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001794 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1795 * do not trap on the (extremely unlikely) case of a page
1796 * crossing operation.
1797 *
1798 * - Furthermore, we need an efficient 64-bit compile for the
1799 * 64-bit case in order to generate the "number of bytes in
1800 * the final mask". Again, that could be replaced with a
1801 * efficient population count instruction or similar.
1802 */
1803#ifdef CONFIG_DCACHE_WORD_ACCESS
1804
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001805#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001806
George Spelvin468a9422016-05-26 22:11:51 -04001807#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001808
George Spelvin468a9422016-05-26 22:11:51 -04001809/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1810
1811#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001812/*
1813 * Register pressure in the mixing function is an issue, particularly
1814 * on 32-bit x86, but almost any function requires one state value and
1815 * one temporary. Instead, use a function designed for two state values
1816 * and no temporaries.
1817 *
1818 * This function cannot create a collision in only two iterations, so
1819 * we have two iterations to achieve avalanche. In those two iterations,
1820 * we have six layers of mixing, which is enough to spread one bit's
1821 * influence out to 2^6 = 64 state bits.
1822 *
1823 * Rotate constants are scored by considering either 64 one-bit input
1824 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1825 * probability of that delta causing a change to each of the 128 output
1826 * bits, using a sample of random initial states.
1827 *
1828 * The Shannon entropy of the computed probabilities is then summed
1829 * to produce a score. Ideally, any input change has a 50% chance of
1830 * toggling any given output bit.
1831 *
1832 * Mixing scores (in bits) for (12,45):
1833 * Input delta: 1-bit 2-bit
1834 * 1 round: 713.3 42542.6
1835 * 2 rounds: 2753.7 140389.8
1836 * 3 rounds: 5954.1 233458.2
1837 * 4 rounds: 7862.6 256672.2
1838 * Perfect: 8192 258048
1839 * (64*128) (64*63/2 * 128)
1840 */
1841#define HASH_MIX(x, y, a) \
1842 ( x ^= (a), \
1843 y ^= x, x = rol64(x,12),\
1844 x += y, y = rol64(y,45),\
1845 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001846
George Spelvin0fed3ac2016-05-02 06:31:01 -04001847/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001848 * Fold two longs into one 32-bit hash value. This must be fast, but
1849 * latency isn't quite as critical, as there is a fair bit of additional
1850 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001851 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001852static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001853{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001854 y ^= x * GOLDEN_RATIO_64;
1855 y *= GOLDEN_RATIO_64;
1856 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001857}
1858
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001859#else /* 32-bit case */
1860
George Spelvin2a18da7a2016-05-23 07:43:58 -04001861/*
1862 * Mixing scores (in bits) for (7,20):
1863 * Input delta: 1-bit 2-bit
1864 * 1 round: 330.3 9201.6
1865 * 2 rounds: 1246.4 25475.4
1866 * 3 rounds: 1907.1 31295.1
1867 * 4 rounds: 2042.3 31718.6
1868 * Perfect: 2048 31744
1869 * (32*64) (32*31/2 * 64)
1870 */
1871#define HASH_MIX(x, y, a) \
1872 ( x ^= (a), \
1873 y ^= x, x = rol32(x, 7),\
1874 x += y, y = rol32(y,20),\
1875 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001876
George Spelvin2a18da7a2016-05-23 07:43:58 -04001877static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001878{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001879 /* Use arch-optimized multiply if one exists */
1880 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001881}
1882
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001883#endif
1884
George Spelvin2a18da7a2016-05-23 07:43:58 -04001885/*
1886 * Return the hash of a string of known length. This is carfully
1887 * designed to match hash_name(), which is the more critical function.
1888 * In particular, we must end by hashing a final word containing 0..7
1889 * payload bytes, to match the way that hash_name() iterates until it
1890 * finds the delimiter after the name.
1891 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001892unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001893{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001894 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001895
1896 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001897 if (!len)
1898 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001899 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001900 if (len < sizeof(unsigned long))
1901 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001902 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001903 name += sizeof(unsigned long);
1904 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001905 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001906 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001907done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001908 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001909}
1910EXPORT_SYMBOL(full_name_hash);
1911
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001912/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001913u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001914{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001915 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1916 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001917 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1918
Linus Torvalds8387ff22016-06-10 07:51:30 -07001919 len = 0;
1920 goto inside;
1921
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001922 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001923 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001924 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001925inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001926 a = load_unaligned_zeropad(name+len);
1927 } while (!has_zero(a, &adata, &constants));
1928
1929 adata = prep_zero_mask(a, adata, &constants);
1930 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001931 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001932
George Spelvin2a18da7a2016-05-23 07:43:58 -04001933 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001934}
1935EXPORT_SYMBOL(hashlen_string);
1936
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001937/*
1938 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001939 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001940 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001941static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001942{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001943 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1944 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07001945 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001946
Linus Torvalds8387ff22016-06-10 07:51:30 -07001947 len = 0;
1948 goto inside;
1949
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001950 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001951 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001952 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001953inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001954 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001955 b = a ^ REPEAT_BYTE('/');
1956 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001957
Linus Torvalds36126f82012-05-26 10:43:17 -07001958 adata = prep_zero_mask(a, adata, &constants);
1959 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07001960 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001961 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07001962
George Spelvin2a18da7a2016-05-23 07:43:58 -04001963 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001964}
1965
George Spelvin2a18da7a2016-05-23 07:43:58 -04001966#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001967
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001968/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001969unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08001970{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001971 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001972 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001973 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001974 return end_name_hash(hash);
1975}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001976EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001977
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001978/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001979u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001980{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001981 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001982 unsigned long len = 0, c;
1983
1984 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04001985 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001986 len++;
1987 hash = partial_name_hash(c, hash);
1988 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04001989 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001990 return hashlen_create(end_name_hash(hash), len);
1991}
George Spelvinf2a031b2016-05-29 01:26:41 -04001992EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001993
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001994/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001995 * We know there's a real path component here of at least
1996 * one character.
1997 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001998static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001999{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002000 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002001 unsigned long len = 0, c;
2002
2003 c = (unsigned char)*name;
2004 do {
2005 len++;
2006 hash = partial_name_hash(c, hash);
2007 c = (unsigned char)name[len];
2008 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002009 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002010}
2011
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002012#endif
2013
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002014/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002016 * This is the basic name resolution function, turning a pathname into
2017 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002019 * Returns 0 and nd will have valid dentry and mnt on success.
2020 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 */
Al Viro6de88d72009-08-09 01:41:57 +04002022static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 while (*name=='/')
2027 name++;
2028 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /* At this point we know we have a real path component. */
2032 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002033 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002034 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Al Viro52094c82011-02-21 21:34:47 -05002036 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002037 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
Linus Torvalds8387ff22016-06-10 07:51:30 -07002040 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
Al Virofe479a52011-02-22 15:10:03 -05002042 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002043 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002044 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002045 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002046 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002047 nd->flags |= LOOKUP_JUMPED;
2048 }
Al Virofe479a52011-02-22 15:10:03 -05002049 break;
2050 case 1:
2051 type = LAST_DOT;
2052 }
Al Viro5a202bc2011-03-08 14:17:44 -05002053 if (likely(type == LAST_NORM)) {
2054 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002055 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002056 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002057 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002058 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002059 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002060 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002061 hash_len = this.hash_len;
2062 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002063 }
2064 }
Al Virofe479a52011-02-22 15:10:03 -05002065
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002066 nd->last.hash_len = hash_len;
2067 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002068 nd->last_type = type;
2069
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002070 name += hashlen_len(hash_len);
2071 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002072 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002073 /*
2074 * If it wasn't NUL, we know it was '/'. Skip that
2075 * slash, and continue until no more slashes.
2076 */
2077 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002078 name++;
2079 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002080 if (unlikely(!*name)) {
2081OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002082 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002083 if (!nd->depth)
2084 return 0;
2085 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002086 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002087 if (!name)
2088 return 0;
2089 /* last component of nested symlink */
Al Viro8f64fb12016-11-14 01:50:26 -05002090 err = walk_component(nd, WALK_FOLLOW);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002091 } else {
2092 /* not the last component */
Al Viro8f64fb12016-11-14 01:50:26 -05002093 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002094 }
Al Viroce57dfc2011-03-13 19:58:58 -04002095 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002096 return err;
Al Virofe479a52011-02-22 15:10:03 -05002097
Al Viroce57dfc2011-03-13 19:58:58 -04002098 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002099 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002100
Viresh Kumara1c83682015-08-12 15:59:44 +05302101 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002102 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002103 err = 0;
2104 if (unlikely(!s)) {
2105 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002106 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002107 } else {
Al Virofab51e82015-05-10 11:01:00 -04002108 nd->stack[nd->depth - 1].name = name;
2109 name = s;
2110 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002111 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002112 }
Al Viro97242f92015-08-01 19:59:28 -04002113 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2114 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002115 if (unlazy_walk(nd))
Al Viro97242f92015-08-01 19:59:28 -04002116 return -ECHILD;
2117 }
Al Viro3595e232015-05-09 16:54:45 -04002118 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
2122
Al Viroc8a53ee2015-05-12 18:43:07 -04002123static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Al Viroc8a53ee2015-05-12 18:43:07 -04002125 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002127 if (!*s)
2128 flags &= ~LOOKUP_RCU;
2129
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002131 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002133 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002134 struct dentry *root = nd->root.dentry;
2135 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002136 if (*s && unlikely(!d_can_lookup(root)))
2137 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002138 nd->path = nd->root;
2139 nd->inode = inode;
2140 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002141 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05002142 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002143 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002144 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002145 } else {
2146 path_get(&nd->path);
2147 }
Al Viro368ee9b2015-05-08 17:19:59 -04002148 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002149 }
2150
Al Viro2a737872009-04-07 11:49:53 -04002151 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002152 nd->path.mnt = NULL;
2153 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Al Viro48a066e2013-09-29 22:06:07 -04002155 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002156 if (*s == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05002157 if (flags & LOOKUP_RCU)
Al Viro8b61e742013-11-08 12:45:01 -05002158 rcu_read_lock();
Al Viro9e6697e2015-12-05 20:07:21 -05002159 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05002160 if (likely(!nd_jump_root(nd)))
Al Viroef55d912015-12-05 20:25:06 -05002161 return s;
Al Viro248fb5b2015-12-05 20:51:58 -05002162 nd->root.mnt = NULL;
Al Viroef55d912015-12-05 20:25:06 -05002163 rcu_read_unlock();
2164 return ERR_PTR(-ECHILD);
Al Viroc8a53ee2015-05-12 18:43:07 -04002165 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002166 if (flags & LOOKUP_RCU) {
2167 struct fs_struct *fs = current->fs;
2168 unsigned seq;
2169
Al Viro8b61e742013-11-08 12:45:01 -05002170 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002171
2172 do {
2173 seq = read_seqcount_begin(&fs->seq);
2174 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002175 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002176 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2177 } while (read_seqcount_retry(&fs->seq, seq));
2178 } else {
2179 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002180 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002181 }
Al Viroef55d912015-12-05 20:25:06 -05002182 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002183 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002184 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002185 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002186 struct dentry *dentry;
2187
Al Viro2903ff02012-08-28 12:52:22 -04002188 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002189 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002190
Al Viro2903ff02012-08-28 12:52:22 -04002191 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002192
Al Virofd2f7cb2015-02-22 20:07:13 -05002193 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002194 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002195 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002196 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002197 }
Al Virof52e0c12011-03-14 18:56:51 -04002198 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002199
Al Viro2903ff02012-08-28 12:52:22 -04002200 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002201 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002202 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002203 nd->inode = nd->path.dentry->d_inode;
2204 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002205 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002206 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002207 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002208 }
Al Viro34a26b92015-05-11 08:05:05 -04002209 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002210 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002212}
2213
Al Viro3bdba282015-05-08 17:37:07 -04002214static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002215{
2216 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002217 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002218 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002219 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002220 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002221 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002222 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002223 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002224}
2225
Al Virocaa856342015-04-22 17:52:47 -04002226static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002227{
2228 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2229 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2230
2231 nd->flags &= ~LOOKUP_PARENT;
Al Viro1c4ff1a2016-11-14 01:39:36 -05002232 return walk_component(nd, 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002233}
2234
Al Viro4f757f32017-04-15 17:31:22 -04002235static int handle_lookup_down(struct nameidata *nd)
2236{
2237 struct path path = nd->path;
2238 struct inode *inode = nd->inode;
2239 unsigned seq = nd->seq;
2240 int err;
2241
2242 if (nd->flags & LOOKUP_RCU) {
2243 /*
2244 * don't bother with unlazy_walk on failure - we are
2245 * at the very beginning of walk, so we lose nothing
2246 * if we simply redo everything in non-RCU mode
2247 */
2248 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2249 return -ECHILD;
2250 } else {
2251 dget(path.dentry);
2252 err = follow_managed(&path, nd);
2253 if (unlikely(err < 0))
2254 return err;
2255 inode = d_backing_inode(path.dentry);
2256 seq = 0;
2257 }
2258 path_to_nameidata(&path, nd);
2259 nd->inode = inode;
2260 nd->seq = seq;
2261 return 0;
2262}
2263
Al Viro9b4a9b12009-04-07 11:44:16 -04002264/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002265static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002266{
Al Viroc8a53ee2015-05-12 18:43:07 -04002267 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002268 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002269
Al Viro368ee9b2015-05-08 17:19:59 -04002270 if (IS_ERR(s))
2271 return PTR_ERR(s);
Al Viro4f757f32017-04-15 17:31:22 -04002272
2273 if (unlikely(flags & LOOKUP_DOWN)) {
2274 err = handle_lookup_down(nd);
2275 if (unlikely(err < 0)) {
2276 terminate_walk(nd);
2277 return err;
2278 }
2279 }
2280
Al Viro3bdba282015-05-08 17:37:07 -04002281 while (!(err = link_path_walk(s, nd))
2282 && ((err = lookup_last(nd)) > 0)) {
2283 s = trailing_symlink(nd);
2284 if (IS_ERR(s)) {
2285 err = PTR_ERR(s);
2286 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002287 }
2288 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002289 if (!err)
2290 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002291
Al Virodeb106c2015-05-08 18:05:21 -04002292 if (!err && nd->flags & LOOKUP_DIRECTORY)
2293 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002294 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002295 if (!err) {
2296 *path = nd->path;
2297 nd->path.mnt = NULL;
2298 nd->path.dentry = NULL;
2299 }
2300 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002301 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002302}
Nick Piggin31e6b012011-01-07 17:49:52 +11002303
Al Viro625b6d12015-05-12 16:36:12 -04002304static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002305 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002306{
Al Viro894bc8c2015-05-02 07:16:16 -04002307 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002308 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002309 if (IS_ERR(name))
2310 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002311 if (unlikely(root)) {
2312 nd.root = *root;
2313 flags |= LOOKUP_ROOT;
2314 }
Al Viro9883d182015-05-13 07:28:08 -04002315 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002316 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002317 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002318 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002319 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002320 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002321
2322 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002323 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002324 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002325 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002326 return retval;
2327}
2328
Al Viro8bcb77f2015-05-08 16:59:20 -04002329/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002330static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002331 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002332{
Al Viroc8a53ee2015-05-12 18:43:07 -04002333 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002334 int err;
2335 if (IS_ERR(s))
2336 return PTR_ERR(s);
2337 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002338 if (!err)
2339 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002340 if (!err) {
2341 *parent = nd->path;
2342 nd->path.mnt = NULL;
2343 nd->path.dentry = NULL;
2344 }
2345 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002346 return err;
2347}
2348
Al Viro5c31b6c2015-05-12 17:32:54 -04002349static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002350 unsigned int flags, struct path *parent,
2351 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002352{
2353 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002354 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002355
Al Viro5c31b6c2015-05-12 17:32:54 -04002356 if (IS_ERR(name))
2357 return name;
Al Viro9883d182015-05-13 07:28:08 -04002358 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002359 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002360 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002361 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002362 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002363 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002364 if (likely(!retval)) {
2365 *last = nd.last;
2366 *type = nd.last_type;
2367 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002368 } else {
2369 putname(name);
2370 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002371 }
Al Viro9883d182015-05-13 07:28:08 -04002372 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002373 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002374}
2375
Al Viro79714f72012-06-15 03:01:42 +04002376/* does lookup, returns the object with parent locked */
2377struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002378{
Al Viro5c31b6c2015-05-12 17:32:54 -04002379 struct filename *filename;
2380 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002381 struct qstr last;
2382 int type;
Paul Moore51689102015-01-22 00:00:03 -05002383
Al Viro5c31b6c2015-05-12 17:32:54 -04002384 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2385 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002386 if (IS_ERR(filename))
2387 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002388 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002389 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002390 putname(filename);
2391 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002392 }
Al Viro59551022016-01-22 15:40:57 -05002393 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002394 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002395 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002396 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002397 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002398 }
Paul Moore51689102015-01-22 00:00:03 -05002399 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002400 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002401}
2402
Al Virod1811462008-08-02 00:49:18 -04002403int kern_path(const char *name, unsigned int flags, struct path *path)
2404{
Al Viroabc9f5b2015-05-12 16:53:42 -04002405 return filename_lookup(AT_FDCWD, getname_kernel(name),
2406 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002407}
Al Viro4d359502014-03-14 12:20:17 -04002408EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002409
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002410/**
2411 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2412 * @dentry: pointer to dentry of the base directory
2413 * @mnt: pointer to vfs mount of the base directory
2414 * @name: pointer to file name
2415 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002416 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002417 */
2418int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2419 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002420 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002421{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002422 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002423 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002424 return filename_lookup(AT_FDCWD, getname_kernel(name),
2425 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002426}
Al Viro4d359502014-03-14 12:20:17 -04002427EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002428
Christoph Hellwigeead1912007-10-16 23:25:38 -07002429/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002430 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002431 * @name: pathname component to lookup
2432 * @base: base directory to lookup from
2433 * @len: maximum length @len should be interpreted to
2434 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002435 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002436 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002437 *
2438 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002439 */
James Morris057f6c02007-04-26 00:12:05 -07002440struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2441{
James Morris057f6c02007-04-26 00:12:05 -07002442 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002443 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002444 int err;
James Morris057f6c02007-04-26 00:12:05 -07002445
Al Viro59551022016-01-22 15:40:57 -05002446 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002447
Al Viro6a96ba52011-03-07 23:49:20 -05002448 this.name = name;
2449 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002450 this.hash = full_name_hash(base, name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002451 if (!len)
2452 return ERR_PTR(-EACCES);
2453
Al Viro21d8a152012-11-29 22:17:21 -05002454 if (unlikely(name[0] == '.')) {
2455 if (len < 2 || (len == 2 && name[1] == '.'))
2456 return ERR_PTR(-EACCES);
2457 }
2458
Al Viro6a96ba52011-03-07 23:49:20 -05002459 while (len--) {
2460 c = *(const unsigned char *)name++;
2461 if (c == '/' || c == '\0')
2462 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002463 }
Al Viro5a202bc2011-03-08 14:17:44 -05002464 /*
2465 * See if the low-level filesystem might want
2466 * to use its own hash..
2467 */
2468 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002469 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002470 if (err < 0)
2471 return ERR_PTR(err);
2472 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002473
Miklos Szeredicda309d2012-03-26 12:54:21 +02002474 err = inode_permission(base->d_inode, MAY_EXEC);
2475 if (err)
2476 return ERR_PTR(err);
2477
Al Viro72bd8662012-06-10 17:17:17 -04002478 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002479}
Al Viro4d359502014-03-14 12:20:17 -04002480EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002481
NeilBrownbbddca82016-01-07 16:08:20 -05002482/**
2483 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2484 * @name: pathname component to lookup
2485 * @base: base directory to lookup from
2486 * @len: maximum length @len should be interpreted to
2487 *
2488 * Note that this routine is purely a helper for filesystem usage and should
2489 * not be called by generic code.
2490 *
2491 * Unlike lookup_one_len, it should be called without the parent
2492 * i_mutex held, and will take the i_mutex itself if necessary.
2493 */
2494struct dentry *lookup_one_len_unlocked(const char *name,
2495 struct dentry *base, int len)
2496{
2497 struct qstr this;
2498 unsigned int c;
2499 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002500 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002501
2502 this.name = name;
2503 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002504 this.hash = full_name_hash(base, name, len);
NeilBrownbbddca82016-01-07 16:08:20 -05002505 if (!len)
2506 return ERR_PTR(-EACCES);
2507
2508 if (unlikely(name[0] == '.')) {
2509 if (len < 2 || (len == 2 && name[1] == '.'))
2510 return ERR_PTR(-EACCES);
2511 }
2512
2513 while (len--) {
2514 c = *(const unsigned char *)name++;
2515 if (c == '/' || c == '\0')
2516 return ERR_PTR(-EACCES);
2517 }
2518 /*
2519 * See if the low-level filesystem might want
2520 * to use its own hash..
2521 */
2522 if (base->d_flags & DCACHE_OP_HASH) {
2523 int err = base->d_op->d_hash(base, &this);
2524 if (err < 0)
2525 return ERR_PTR(err);
2526 }
2527
2528 err = inode_permission(base->d_inode, MAY_EXEC);
2529 if (err)
2530 return ERR_PTR(err);
2531
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002532 ret = lookup_dcache(&this, base, 0);
2533 if (!ret)
2534 ret = lookup_slow(&this, base, 0);
2535 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002536}
2537EXPORT_SYMBOL(lookup_one_len_unlocked);
2538
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002539#ifdef CONFIG_UNIX98_PTYS
2540int path_pts(struct path *path)
2541{
2542 /* Find something mounted on "pts" in the same directory as
2543 * the input path.
2544 */
2545 struct dentry *child, *parent;
2546 struct qstr this;
2547 int ret;
2548
2549 ret = path_parent_directory(path);
2550 if (ret)
2551 return ret;
2552
2553 parent = path->dentry;
2554 this.name = "pts";
2555 this.len = 3;
2556 child = d_hash_and_lookup(parent, &this);
2557 if (!child)
2558 return -ENOENT;
2559
2560 path->dentry = child;
2561 dput(parent);
2562 follow_mount(path);
2563 return 0;
2564}
2565#endif
2566
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002567int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2568 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569{
Al Viroabc9f5b2015-05-12 16:53:42 -04002570 return filename_lookup(dfd, getname_flags(name, flags, empty),
2571 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572}
Al Virob853a162015-05-13 09:12:02 -04002573EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002574
Jeff Layton80334262013-07-26 06:23:25 -04002575/**
Al Viro197df042013-09-08 14:03:27 -04002576 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002577 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
Jeff Layton80334262013-07-26 06:23:25 -04002578 *
2579 * This is a special lookup_last function just for umount. In this case, we
2580 * need to resolve the path without doing any revalidation.
2581 *
2582 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2583 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2584 * in almost all cases, this lookup will be served out of the dcache. The only
2585 * cases where it won't are if nd->last refers to a symlink or the path is
2586 * bogus and it doesn't exist.
2587 *
2588 * Returns:
2589 * -error: if there was an error during lookup. This includes -ENOENT if the
Al Viroba8f4612016-11-14 00:40:33 -05002590 * lookup found a negative dentry.
Jeff Layton80334262013-07-26 06:23:25 -04002591 *
Al Viroba8f4612016-11-14 00:40:33 -05002592 * 0: if we successfully resolved nd->last and found it to not to be a
2593 * symlink that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002594 *
2595 * 1: if we successfully resolved nd->last and found it to be a symlink
Al Viroba8f4612016-11-14 00:40:33 -05002596 * that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002597 */
2598static int
Al Viroba8f4612016-11-14 00:40:33 -05002599mountpoint_last(struct nameidata *nd)
Jeff Layton80334262013-07-26 06:23:25 -04002600{
2601 int error = 0;
Jeff Layton80334262013-07-26 06:23:25 -04002602 struct dentry *dir = nd->path.dentry;
Al Viroba8f4612016-11-14 00:40:33 -05002603 struct path path;
Jeff Layton80334262013-07-26 06:23:25 -04002604
Al Viro35759522013-09-08 13:41:33 -04002605 /* If we're in rcuwalk, drop out of it to handle last component */
2606 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002607 if (unlazy_walk(nd))
Al Virodeb106c2015-05-08 18:05:21 -04002608 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002609 }
2610
2611 nd->flags &= ~LOOKUP_PARENT;
2612
2613 if (unlikely(nd->last_type != LAST_NORM)) {
2614 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002615 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002616 return error;
Al Viroba8f4612016-11-14 00:40:33 -05002617 path.dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002618 } else {
Al Viroba8f4612016-11-14 00:40:33 -05002619 path.dentry = d_lookup(dir, &nd->last);
2620 if (!path.dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002621 /*
2622 * No cached dentry. Mounted dentries are pinned in the
2623 * cache, so that means that this dentry is probably
2624 * a symlink or the path doesn't actually point
2625 * to a mounted dentry.
2626 */
Al Viroba8f4612016-11-14 00:40:33 -05002627 path.dentry = lookup_slow(&nd->last, dir,
Al Viro949a8522016-03-06 14:20:52 -05002628 nd->flags | LOOKUP_NO_REVAL);
Al Viroba8f4612016-11-14 00:40:33 -05002629 if (IS_ERR(path.dentry))
2630 return PTR_ERR(path.dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002631 }
Jeff Layton80334262013-07-26 06:23:25 -04002632 }
Al Viroba8f4612016-11-14 00:40:33 -05002633 if (d_is_negative(path.dentry)) {
2634 dput(path.dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002635 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002636 }
Al Viroba8f4612016-11-14 00:40:33 -05002637 path.mnt = nd->path.mnt;
Al Viro8f64fb12016-11-14 01:50:26 -05002638 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
Jeff Layton80334262013-07-26 06:23:25 -04002639}
2640
2641/**
Al Viro197df042013-09-08 14:03:27 -04002642 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b8572015-09-09 15:39:23 -07002643 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002644 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002645 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002646 *
2647 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002648 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002649 */
2650static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002651path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002652{
Al Viroc8a53ee2015-05-12 18:43:07 -04002653 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002654 int err;
2655 if (IS_ERR(s))
2656 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002657 while (!(err = link_path_walk(s, nd)) &&
Al Viroba8f4612016-11-14 00:40:33 -05002658 (err = mountpoint_last(nd)) > 0) {
Al Viro3bdba282015-05-08 17:37:07 -04002659 s = trailing_symlink(nd);
2660 if (IS_ERR(s)) {
2661 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002662 break;
Al Viro3bdba282015-05-08 17:37:07 -04002663 }
Jeff Layton80334262013-07-26 06:23:25 -04002664 }
Al Viroba8f4612016-11-14 00:40:33 -05002665 if (!err) {
2666 *path = nd->path;
2667 nd->path.mnt = NULL;
2668 nd->path.dentry = NULL;
2669 follow_mount(path);
2670 }
Al Virodeb106c2015-05-08 18:05:21 -04002671 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002672 return err;
2673}
2674
Al Viro2d864652013-09-08 20:18:44 -04002675static int
Al Viro668696d2015-02-22 19:44:00 -05002676filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002677 unsigned int flags)
2678{
Al Viro9883d182015-05-13 07:28:08 -04002679 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002680 int error;
Al Viro668696d2015-02-22 19:44:00 -05002681 if (IS_ERR(name))
2682 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002683 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002684 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002685 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002686 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002687 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002688 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002689 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002690 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002691 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002692 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002693 return error;
2694}
2695
Jeff Layton80334262013-07-26 06:23:25 -04002696/**
Al Viro197df042013-09-08 14:03:27 -04002697 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002698 * @dfd: directory file descriptor
2699 * @name: pathname from userland
2700 * @flags: lookup flags
2701 * @path: pointer to container to hold result
2702 *
2703 * A umount is a special case for path walking. We're not actually interested
2704 * in the inode in this situation, and ESTALE errors can be a problem. We
2705 * simply want track down the dentry and vfsmount attached at the mountpoint
2706 * and avoid revalidating the last component.
2707 *
2708 * Returns 0 and populates "path" on success.
2709 */
2710int
Al Viro197df042013-09-08 14:03:27 -04002711user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002712 struct path *path)
2713{
Al Virocbaab2d2015-01-22 02:49:00 -05002714 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002715}
2716
Al Viro2d864652013-09-08 20:18:44 -04002717int
2718kern_path_mountpoint(int dfd, const char *name, struct path *path,
2719 unsigned int flags)
2720{
Al Virocbaab2d2015-01-22 02:49:00 -05002721 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002722}
2723EXPORT_SYMBOL(kern_path_mountpoint);
2724
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002725int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002727 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002728
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002729 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002731 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002733 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002735EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
2737/*
2738 * Check whether we can remove a link victim from directory dir, check
2739 * whether the type of victim is right.
2740 * 1. We can't do it if dir is read-only (done in permission())
2741 * 2. We should have write and exec permissions on dir
2742 * 3. We can't remove anything from append-only dir
2743 * 4. We can't do anything with immutable dir (done in permission())
2744 * 5. If the sticky bit on dir is set we should either
2745 * a. be owner of dir, or
2746 * b. be owner of victim, or
2747 * c. have CAP_FOWNER capability
2748 * 6. If the victim is append-only or immutable we can't do antyhing with
2749 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002750 * 7. If the victim has an unknown uid or gid we can't change the inode.
2751 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2752 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2753 * 10. We can't remove a root or mountpoint.
2754 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 * nfs_async_unlink().
2756 */
David Howellsb18825a2013-09-12 19:22:53 +01002757static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
David Howells63afdfc2015-05-06 15:59:00 +01002759 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 int error;
2761
David Howellsb18825a2013-09-12 19:22:53 +01002762 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002764 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002767 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
Al Virof419a2e2008-07-22 00:07:17 -04002769 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 if (error)
2771 return error;
2772 if (IS_APPEND(dir))
2773 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002774
2775 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002776 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return -EPERM;
2778 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002779 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 return -ENOTDIR;
2781 if (IS_ROOT(victim))
2782 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002783 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return -EISDIR;
2785 if (IS_DEADDIR(dir))
2786 return -ENOENT;
2787 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2788 return -EBUSY;
2789 return 0;
2790}
2791
2792/* Check whether we can create an object with dentry child in directory
2793 * dir.
2794 * 1. We can't do it if child already exists (open has special treatment for
2795 * this case, but since we are inlined it's OK)
2796 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002797 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2798 * 4. We should have write and exec permissions on dir
2799 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002801static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002803 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002804 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 if (child->d_inode)
2806 return -EEXIST;
2807 if (IS_DEADDIR(dir))
2808 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002809 s_user_ns = dir->i_sb->s_user_ns;
2810 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2811 !kgid_has_mapping(s_user_ns, current_fsgid()))
2812 return -EOVERFLOW;
Al Virof419a2e2008-07-22 00:07:17 -04002813 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816/*
2817 * p1 and p2 should be directories on the same fs.
2818 */
2819struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2820{
2821 struct dentry *p;
2822
2823 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002824 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return NULL;
2826 }
2827
Al Virofc640052016-04-10 01:33:30 -04002828 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002830 p = d_ancestor(p2, p1);
2831 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002832 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2833 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002834 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002837 p = d_ancestor(p1, p2);
2838 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002839 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2840 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002841 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 }
2843
Al Viro59551022016-01-22 15:40:57 -05002844 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2845 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return NULL;
2847}
Al Viro4d359502014-03-14 12:20:17 -04002848EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850void unlock_rename(struct dentry *p1, struct dentry *p2)
2851{
Al Viro59551022016-01-22 15:40:57 -05002852 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002854 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002855 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 }
2857}
Al Viro4d359502014-03-14 12:20:17 -04002858EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Al Viro4acdaf22011-07-26 01:42:34 -04002860int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002861 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002863 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (error)
2865 return error;
2866
Al Viroacfa4382008-12-04 10:06:33 -05002867 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return -EACCES; /* shouldn't it be ENOSYS? */
2869 mode &= S_IALLUGO;
2870 mode |= S_IFREG;
2871 error = security_inode_create(dir, dentry, mode);
2872 if (error)
2873 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002874 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002875 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002876 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return error;
2878}
Al Viro4d359502014-03-14 12:20:17 -04002879EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Al Viro8e6c8482017-12-01 17:12:45 -05002881int vfs_mkobj(struct dentry *dentry, umode_t mode,
2882 int (*f)(struct dentry *, umode_t, void *),
2883 void *arg)
2884{
2885 struct inode *dir = dentry->d_parent->d_inode;
2886 int error = may_create(dir, dentry);
2887 if (error)
2888 return error;
2889
2890 mode &= S_IALLUGO;
2891 mode |= S_IFREG;
2892 error = security_inode_create(dir, dentry, mode);
2893 if (error)
2894 return error;
2895 error = f(dentry, mode, arg);
2896 if (!error)
2897 fsnotify_create(dir, dentry);
2898 return error;
2899}
2900EXPORT_SYMBOL(vfs_mkobj);
2901
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002902bool may_open_dev(const struct path *path)
2903{
2904 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2905 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2906}
2907
Al Virof0bb5aa2016-11-20 20:27:12 -05002908static int may_open(const struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002910 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 struct inode *inode = dentry->d_inode;
2912 int error;
2913
2914 if (!inode)
2915 return -ENOENT;
2916
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002917 switch (inode->i_mode & S_IFMT) {
2918 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002920 case S_IFDIR:
2921 if (acc_mode & MAY_WRITE)
2922 return -EISDIR;
2923 break;
2924 case S_IFBLK:
2925 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002926 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002928 /*FALLTHRU*/
2929 case S_IFIFO:
2930 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002932 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002933 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002934
Al Viro62fb4a12015-12-26 22:33:24 -05002935 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002936 if (error)
2937 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 /*
2940 * An append-only file must be opened in append mode for writing.
2941 */
2942 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002943 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002944 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002946 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
2948
2949 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002950 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002951 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002953 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002954}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Jeff Laytone1181ee2010-12-07 16:19:50 -05002956static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002957{
Al Virof0bb5aa2016-11-20 20:27:12 -05002958 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002959 struct inode *inode = path->dentry->d_inode;
2960 int error = get_write_access(inode);
2961 if (error)
2962 return error;
2963 /*
2964 * Refuse to truncate files with mandatory locks held on them.
2965 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002966 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002967 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002968 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002969 if (!error) {
2970 error = do_truncate(path->dentry, 0,
2971 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002972 filp);
Al Viro7715b522009-12-16 03:54:00 -05002973 }
2974 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002975 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976}
2977
Dave Hansend57999e2008-02-15 14:37:27 -08002978static inline int open_to_namei_flags(int flag)
2979{
Al Viro8a5e9292011-06-25 19:15:54 -04002980 if ((flag & O_ACCMODE) == 3)
2981 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002982 return flag;
2983}
2984
Al Virod3607752016-03-25 15:21:09 -04002985static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002986{
Seth Forshee1328c722017-01-26 14:33:46 -06002987 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002988 int error = security_path_mknod(dir, dentry, mode, 0);
2989 if (error)
2990 return error;
2991
Seth Forshee1328c722017-01-26 14:33:46 -06002992 s_user_ns = dir->dentry->d_sb->s_user_ns;
2993 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2994 !kgid_has_mapping(s_user_ns, current_fsgid()))
2995 return -EOVERFLOW;
2996
Miklos Szeredid18e9002012-06-05 15:10:17 +02002997 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2998 if (error)
2999 return error;
3000
3001 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3002}
3003
David Howells1acf0af2012-06-14 16:13:46 +01003004/*
3005 * Attempt to atomically look up, create and open a file from a negative
3006 * dentry.
3007 *
3008 * Returns 0 if successful. The file will have been created and attached to
3009 * @file by the filesystem calling finish_open().
3010 *
3011 * Returns 1 if the file was looked up only or didn't need creating. The
3012 * caller will need to perform the open themselves. @path will have been
3013 * updated to point to the new dentry. This may be negative.
3014 *
3015 * Returns an error code otherwise.
3016 */
Al Viro2675a4e2012-06-22 12:41:10 +04003017static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3018 struct path *path, struct file *file,
3019 const struct open_flags *op,
Al Viro1643b432016-04-27 19:14:10 -04003020 int open_flag, umode_t mode,
Al Viro2675a4e2012-06-22 12:41:10 +04003021 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003022{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003023 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003024 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003025 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003026
Al Viro384f26e2016-04-28 02:03:55 -04003027 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003028 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003029
Miklos Szeredid18e9002012-06-05 15:10:17 +02003030 if (nd->flags & LOOKUP_DIRECTORY)
3031 open_flag |= O_DIRECTORY;
3032
Al Viro30d90492012-06-22 12:40:19 +04003033 file->f_path.dentry = DENTRY_NOT_SET;
3034 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003035 error = dir->i_op->atomic_open(dir, dentry, file,
3036 open_to_namei_flags(open_flag),
3037 mode, opened);
Al Viro6fbd0712016-04-28 11:50:59 -04003038 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003039 if (!error) {
3040 /*
3041 * We didn't have the inode before the open, so check open
3042 * permission here.
3043 */
3044 int acc_mode = op->acc_mode;
3045 if (*opened & FILE_CREATED) {
3046 WARN_ON(!(open_flag & O_CREAT));
3047 fsnotify_create(dir, dentry);
3048 acc_mode = 0;
3049 }
3050 error = may_open(&file->f_path, acc_mode, open_flag);
3051 if (WARN_ON(error > 0))
3052 error = -EINVAL;
3053 } else if (error > 0) {
Al Viro30d90492012-06-22 12:40:19 +04003054 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003055 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003056 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003057 if (file->f_path.dentry) {
3058 dput(dentry);
3059 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003060 }
Al Viro384f26e2016-04-28 02:03:55 -04003061 if (*opened & FILE_CREATED)
3062 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04003063 if (unlikely(d_is_negative(dentry))) {
3064 error = -ENOENT;
3065 } else {
3066 path->dentry = dentry;
3067 path->mnt = nd->path.mnt;
3068 return 1;
3069 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003070 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003071 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003072 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003073 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003074}
3075
Nick Piggin31e6b012011-01-07 17:49:52 +11003076/*
David Howells1acf0af2012-06-14 16:13:46 +01003077 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003078 *
3079 * Must be called with i_mutex held on parent.
3080 *
David Howells1acf0af2012-06-14 16:13:46 +01003081 * Returns 0 if the file was successfully atomically created (if necessary) and
3082 * opened. In this case the file will be returned attached to @file.
3083 *
3084 * Returns 1 if the file was not completely opened at this time, though lookups
3085 * and creations will have been performed and the dentry returned in @path will
3086 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3087 * specified then a negative dentry may be returned.
3088 *
3089 * An error code is returned otherwise.
3090 *
3091 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3092 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003093 */
Al Viro2675a4e2012-06-22 12:41:10 +04003094static int lookup_open(struct nameidata *nd, struct path *path,
3095 struct file *file,
3096 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04003097 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003098{
3099 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003100 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003101 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003102 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003103 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003104 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003105 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003106
Al Viroce8644f2016-04-26 14:17:56 -04003107 if (unlikely(IS_DEADDIR(dir_inode)))
3108 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003109
Al Viro47237682012-06-10 05:01:45 -04003110 *opened &= ~FILE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003111 dentry = d_lookup(dir, &nd->last);
3112 for (;;) {
3113 if (!dentry) {
3114 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3115 if (IS_ERR(dentry))
3116 return PTR_ERR(dentry);
3117 }
3118 if (d_in_lookup(dentry))
3119 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003120
Al Viro6fbd0712016-04-28 11:50:59 -04003121 error = d_revalidate(dentry, nd->flags);
3122 if (likely(error > 0))
3123 break;
3124 if (error)
3125 goto out_dput;
3126 d_invalidate(dentry);
3127 dput(dentry);
3128 dentry = NULL;
3129 }
3130 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003131 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003132 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003133 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003134
Al Viro1643b432016-04-27 19:14:10 -04003135 /*
3136 * Checking write permission is tricky, bacuse we don't know if we are
3137 * going to actually need it: O_CREAT opens should work as long as the
3138 * file exists. But checking existence breaks atomicity. The trick is
3139 * to check access and if not granted clear O_CREAT from the flags.
3140 *
3141 * Another problem is returing the "right" error value (e.g. for an
3142 * O_EXCL open we want to return EEXIST not EROFS).
3143 */
3144 if (open_flag & O_CREAT) {
3145 if (!IS_POSIXACL(dir->d_inode))
3146 mode &= ~current_umask();
3147 if (unlikely(!got_write)) {
3148 create_error = -EROFS;
3149 open_flag &= ~O_CREAT;
3150 if (open_flag & (O_EXCL | O_TRUNC))
3151 goto no_open;
3152 /* No side effects, safe to clear O_CREAT */
3153 } else {
3154 create_error = may_o_create(&nd->path, dentry, mode);
3155 if (create_error) {
3156 open_flag &= ~O_CREAT;
3157 if (open_flag & O_EXCL)
3158 goto no_open;
3159 }
3160 }
3161 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3162 unlikely(!got_write)) {
3163 /*
3164 * No O_CREATE -> atomicity not a requirement -> fall
3165 * back to lookup + open
3166 */
3167 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003168 }
3169
Al Viro1643b432016-04-27 19:14:10 -04003170 if (dir_inode->i_op->atomic_open) {
3171 error = atomic_open(nd, dentry, path, file, op, open_flag,
3172 mode, opened);
3173 if (unlikely(error == -ENOENT) && create_error)
3174 error = create_error;
3175 return error;
3176 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003177
Al Viro1643b432016-04-27 19:14:10 -04003178no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003179 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003180 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3181 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003182 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003183 if (unlikely(res)) {
3184 if (IS_ERR(res)) {
3185 error = PTR_ERR(res);
3186 goto out_dput;
3187 }
3188 dput(dentry);
3189 dentry = res;
3190 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003191 }
3192
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003193 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003194 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro47237682012-06-10 05:01:45 -04003195 *opened |= FILE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003196 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003197 if (!dir_inode->i_op->create) {
3198 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003199 goto out_dput;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003200 }
Al Viroce8644f2016-04-26 14:17:56 -04003201 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003202 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003203 if (error)
3204 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003205 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003206 }
Al Viro1643b432016-04-27 19:14:10 -04003207 if (unlikely(create_error) && !dentry->d_inode) {
3208 error = create_error;
3209 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003210 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003211out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003212 path->dentry = dentry;
3213 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04003214 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003215
3216out_dput:
3217 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003218 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003219}
3220
3221/*
Al Virofe2d35f2011-03-05 22:58:25 -05003222 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003223 */
Al Viro896475d2015-04-22 18:02:17 -04003224static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04003225 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04003226 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05003227{
Al Viroa1e28032009-12-24 02:12:06 -05003228 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003229 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003230 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003231 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003232 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003233 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003234 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003235 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003236 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003237
Al Viroc3e380b2011-02-23 13:39:45 -05003238 nd->flags &= ~LOOKUP_PARENT;
3239 nd->flags |= op->intent;
3240
Al Virobc77daa2013-06-06 09:12:33 -04003241 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003242 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003243 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003244 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003245 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003246 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003247
Al Viroca344a892011-03-09 00:36:45 -05003248 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003249 if (nd->last.name[nd->last.len])
3250 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3251 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003252 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003253 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003254 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003255
Miklos Szeredi71574862012-06-05 15:10:14 +02003256 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003257 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003258
Miklos Szeredi71574862012-06-05 15:10:14 +02003259 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003260 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003261 } else {
3262 /* create side of things */
3263 /*
3264 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3265 * has been cleared when we got to the last component we are
3266 * about to look up
3267 */
3268 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003269 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003270 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003271
Al Viro76ae2a52015-05-12 18:44:32 -04003272 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003273 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003274 if (unlikely(nd->last.name[nd->last.len]))
3275 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003276 }
3277
Al Viro9cf843e2016-04-28 19:35:16 -04003278 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003279 error = mnt_want_write(nd->path.mnt);
3280 if (!error)
3281 got_write = true;
3282 /*
3283 * do _not_ fail yet - we might not need that or fail with
3284 * a different error; let lookup_open() decide; we'll be
3285 * dropping this one anyway.
3286 */
3287 }
Al Viro9cf843e2016-04-28 19:35:16 -04003288 if (open_flag & O_CREAT)
3289 inode_lock(dir->d_inode);
3290 else
3291 inode_lock_shared(dir->d_inode);
Al Viro896475d2015-04-22 18:02:17 -04003292 error = lookup_open(nd, &path, file, op, got_write, opened);
Al Viro9cf843e2016-04-28 19:35:16 -04003293 if (open_flag & O_CREAT)
3294 inode_unlock(dir->d_inode);
3295 else
3296 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003297
Al Viro2675a4e2012-06-22 12:41:10 +04003298 if (error <= 0) {
3299 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003300 goto out;
3301
Al Viro47237682012-06-10 05:01:45 -04003302 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003303 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003304 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003305
Al Viro76ae2a52015-05-12 18:44:32 -04003306 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003307 goto opened;
3308 }
Al Virofb1cc552009-12-24 01:58:28 -05003309
Al Viro47237682012-06-10 05:01:45 -04003310 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003311 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003312 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003313 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003314 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003315 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003316 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003317 }
3318
3319 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003320 * If atomic_open() acquired write access it is dropped now due to
3321 * possible mount and symlink following (this might be optimized away if
3322 * necessary...)
3323 */
Al Viro64894cf2012-07-31 00:53:35 +04003324 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003325 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003326 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003327 }
3328
Al Viroe6ec03a2016-06-05 00:23:09 -04003329 error = follow_managed(&path, nd);
3330 if (unlikely(error < 0))
3331 return error;
3332
Al Viro6583fe22016-03-05 18:14:03 -05003333 if (unlikely(d_is_negative(path.dentry))) {
3334 path_to_nameidata(&path, nd);
3335 return -ENOENT;
3336 }
3337
3338 /*
3339 * create/update audit record if it already exists.
3340 */
3341 audit_inode(nd->name, path.dentry, 0);
3342
Al Virodeb106c2015-05-08 18:05:21 -04003343 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3344 path_to_nameidata(&path, nd);
3345 return -EEXIST;
3346 }
Al Virofb1cc552009-12-24 01:58:28 -05003347
Al Viro254cf582015-05-05 09:40:46 -04003348 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003349 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003350finish_lookup:
Al Viro8f64fb12016-11-14 01:50:26 -05003351 error = step_into(nd, &path, 0, inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003352 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003353 return error;
Al Virobc77daa2013-06-06 09:12:33 -04003354finish_open:
Al Viro8f64fb12016-11-14 01:50:26 -05003355 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Viroa3fbbde2011-11-07 21:21:26 +00003356 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003357 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003358 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003359 audit_inode(nd->name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003360 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003361 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003362 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003363 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003364 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003365 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003366 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003367 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003368
Al Viro0f9d1a12011-03-09 00:13:14 -05003369 if (will_truncate) {
3370 error = mnt_want_write(nd->path.mnt);
3371 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003372 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003373 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003374 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003375finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003376 error = may_open(&nd->path, acc_mode, open_flag);
3377 if (error)
3378 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003379 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3380 error = vfs_open(&nd->path, file, current_cred());
Al Virofac7d192016-06-04 11:41:49 -04003381 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003382 goto out;
Al Virofac7d192016-06-04 11:41:49 -04003383 *opened |= FILE_OPENED;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003384opened:
Christoph Hellwigcab64df2018-03-20 11:30:14 +01003385 error = ima_file_check(file, op->acc_mode, *opened);
Al Virofe9ec822016-04-27 03:14:20 -04003386 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003387 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003388out:
Al Virofe9ec822016-04-27 03:14:20 -04003389 if (unlikely(error) && (*opened & FILE_OPENED))
3390 fput(file);
Al Viroc80567c2016-02-27 19:17:33 -05003391 if (unlikely(error > 0)) {
3392 WARN_ON(1);
3393 error = -EINVAL;
3394 }
Al Viro64894cf2012-07-31 00:53:35 +04003395 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003396 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003397 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003398}
3399
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003400struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3401{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003402 struct dentry *child = NULL;
3403 struct inode *dir = dentry->d_inode;
3404 struct inode *inode;
3405 int error;
3406
3407 /* we want directory to be writable */
3408 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3409 if (error)
3410 goto out_err;
3411 error = -EOPNOTSUPP;
3412 if (!dir->i_op->tmpfile)
3413 goto out_err;
3414 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003415 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003416 if (unlikely(!child))
3417 goto out_err;
3418 error = dir->i_op->tmpfile(dir, child, mode);
3419 if (error)
3420 goto out_err;
3421 error = -ENOENT;
3422 inode = child->d_inode;
3423 if (unlikely(!inode))
3424 goto out_err;
3425 if (!(open_flag & O_EXCL)) {
3426 spin_lock(&inode->i_lock);
3427 inode->i_state |= I_LINKABLE;
3428 spin_unlock(&inode->i_lock);
3429 }
3430 return child;
3431
3432out_err:
3433 dput(child);
3434 return ERR_PTR(error);
3435}
3436EXPORT_SYMBOL(vfs_tmpfile);
3437
Al Viroc8a53ee2015-05-12 18:43:07 -04003438static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003439 const struct open_flags *op,
3440 struct file *file, int *opened)
3441{
Al Viro625b6d12015-05-12 16:36:12 -04003442 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003443 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003444 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003445 if (unlikely(error))
3446 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003447 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003448 if (unlikely(error))
3449 goto out;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003450 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3451 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003452 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003453 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003454 dput(path.dentry);
3455 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003456 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003457 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003458 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003459 if (error)
3460 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003461 file->f_path.mnt = path.mnt;
3462 error = finish_open(file, child, NULL, opened);
Al Viro60545d02013-06-07 01:20:27 -04003463 if (error)
3464 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003465out2:
Al Viro625b6d12015-05-12 16:36:12 -04003466 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003467out:
Al Viro625b6d12015-05-12 16:36:12 -04003468 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003469 return error;
3470}
3471
Al Viro6ac08702016-04-26 00:02:50 -04003472static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3473{
3474 struct path path;
3475 int error = path_lookupat(nd, flags, &path);
3476 if (!error) {
3477 audit_inode(nd->name, path.dentry, 0);
3478 error = vfs_open(&path, file, current_cred());
3479 path_put(&path);
3480 }
3481 return error;
3482}
3483
Al Viroc8a53ee2015-05-12 18:43:07 -04003484static struct file *path_openat(struct nameidata *nd,
3485 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486{
Al Viro368ee9b2015-05-08 17:19:59 -04003487 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003488 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003489 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003490 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003491
Al Viro30d90492012-06-22 12:40:19 +04003492 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003493 if (IS_ERR(file))
3494 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003495
Al Viro30d90492012-06-22 12:40:19 +04003496 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003497
Al Virobb458c62013-07-13 13:26:37 +04003498 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003499 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003500 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003501 }
3502
Al Viro6ac08702016-04-26 00:02:50 -04003503 if (unlikely(file->f_flags & O_PATH)) {
3504 error = do_o_path(nd, flags, file);
3505 if (!error)
3506 opened |= FILE_OPENED;
3507 goto out2;
3508 }
3509
Al Viroc8a53ee2015-05-12 18:43:07 -04003510 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003511 if (IS_ERR(s)) {
3512 put_filp(file);
3513 return ERR_CAST(s);
3514 }
Al Viro3bdba282015-05-08 17:37:07 -04003515 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003516 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003517 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003518 s = trailing_symlink(nd);
3519 if (IS_ERR(s)) {
3520 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003521 break;
Al Viro3bdba282015-05-08 17:37:07 -04003522 }
Al Viro806b6812009-12-26 07:16:40 -05003523 }
Al Virodeb106c2015-05-08 18:05:21 -04003524 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003525out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003526 if (!(opened & FILE_OPENED)) {
3527 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003528 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003529 }
Al Viro2675a4e2012-06-22 12:41:10 +04003530 if (unlikely(error)) {
3531 if (error == -EOPENSTALE) {
3532 if (flags & LOOKUP_RCU)
3533 error = -ECHILD;
3534 else
3535 error = -ESTALE;
3536 }
3537 file = ERR_PTR(error);
3538 }
3539 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540}
3541
Jeff Layton669abf42012-10-10 16:43:10 -04003542struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003543 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003544{
Al Viro9883d182015-05-13 07:28:08 -04003545 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003546 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003547 struct file *filp;
3548
Al Viro9883d182015-05-13 07:28:08 -04003549 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003550 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003551 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003552 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003553 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003554 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003555 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003556 return filp;
3557}
3558
Al Viro73d049a2011-03-11 12:08:24 -05003559struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003560 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003561{
Al Viro9883d182015-05-13 07:28:08 -04003562 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003563 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003564 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003565 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003566
3567 nd.root.mnt = mnt;
3568 nd.root.dentry = dentry;
3569
David Howellsb18825a2013-09-12 19:22:53 +01003570 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003571 return ERR_PTR(-ELOOP);
3572
Paul Moore51689102015-01-22 00:00:03 -05003573 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303574 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003575 return ERR_CAST(filename);
3576
Al Viro9883d182015-05-13 07:28:08 -04003577 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003578 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003579 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003580 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003581 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003582 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003583 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003584 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003585 return file;
3586}
3587
Al Virofa14a0b2015-01-22 02:16:49 -05003588static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003589 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003591 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003592 struct qstr last;
3593 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003594 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003595 int error;
3596 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3597
3598 /*
3599 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3600 * other flags passed in are ignored!
3601 */
3602 lookup_flags &= LOOKUP_REVAL;
3603
Al Viro5c31b6c2015-05-12 17:32:54 -04003604 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3605 if (IS_ERR(name))
3606 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003608 /*
3609 * Yucky last component or no last component at all?
3610 * (foo/., foo/.., /////)
3611 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003612 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003613 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003614
Jan Karac30dabf2012-06-12 16:20:30 +02003615 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003616 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003617 /*
3618 * Do the final lookup.
3619 */
Al Viro391172c2015-05-09 11:19:16 -04003620 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003621 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003622 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003624 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003625
Al Viroa8104a92012-07-20 02:25:00 +04003626 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003627 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003628 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003629
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003630 /*
3631 * Special case - lookup gave negative, but... we had foo/bar/
3632 * From the vfs_mknod() POV we just have a negative dentry -
3633 * all is fine. Let's be bastards - you had / on the end, you've
3634 * been asking for (non-existent) directory. -ENOENT for you.
3635 */
Al Viro391172c2015-05-09 11:19:16 -04003636 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003637 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003638 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003639 }
Jan Karac30dabf2012-06-12 16:20:30 +02003640 if (unlikely(err2)) {
3641 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003642 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003643 }
Al Viro181c37b2015-05-12 17:21:25 -04003644 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646fail:
Al Viroa8104a92012-07-20 02:25:00 +04003647 dput(dentry);
3648 dentry = ERR_PTR(error);
3649unlock:
Al Viro59551022016-01-22 15:40:57 -05003650 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003651 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003652 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003653out:
Al Viro391172c2015-05-09 11:19:16 -04003654 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003655 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 return dentry;
3657}
Al Virofa14a0b2015-01-22 02:16:49 -05003658
3659struct dentry *kern_path_create(int dfd, const char *pathname,
3660 struct path *path, unsigned int lookup_flags)
3661{
Al Viro181c37b2015-05-12 17:21:25 -04003662 return filename_create(dfd, getname_kernel(pathname),
3663 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003664}
Al Virodae6ad82011-06-26 11:50:15 -04003665EXPORT_SYMBOL(kern_path_create);
3666
Al Viro921a1652012-07-20 01:15:31 +04003667void done_path_create(struct path *path, struct dentry *dentry)
3668{
3669 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003670 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003671 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003672 path_put(path);
3673}
3674EXPORT_SYMBOL(done_path_create);
3675
Al Viro520ae682015-05-13 07:00:28 -04003676inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003677 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003678{
Al Viro181c37b2015-05-12 17:21:25 -04003679 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003680}
3681EXPORT_SYMBOL(user_path_create);
3682
Al Viro1a67aaf2011-07-26 01:52:52 -04003683int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003685 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686
3687 if (error)
3688 return error;
3689
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003690 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return -EPERM;
3692
Al Viroacfa4382008-12-04 10:06:33 -05003693 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 return -EPERM;
3695
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003696 error = devcgroup_inode_mknod(mode, dev);
3697 if (error)
3698 return error;
3699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 error = security_inode_mknod(dir, dentry, mode, dev);
3701 if (error)
3702 return error;
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003705 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003706 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 return error;
3708}
Al Viro4d359502014-03-14 12:20:17 -04003709EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
Al Virof69aac02011-07-26 04:31:40 -04003711static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003712{
3713 switch (mode & S_IFMT) {
3714 case S_IFREG:
3715 case S_IFCHR:
3716 case S_IFBLK:
3717 case S_IFIFO:
3718 case S_IFSOCK:
3719 case 0: /* zero mode translates to S_IFREG */
3720 return 0;
3721 case S_IFDIR:
3722 return -EPERM;
3723 default:
3724 return -EINVAL;
3725 }
3726}
3727
Al Viro8208a222011-07-25 17:32:17 -04003728SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003729 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730{
Al Viro2ad94ae2008-07-21 09:32:51 -04003731 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003732 struct path path;
3733 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003734 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Al Viro8e4bfca2012-07-20 01:17:26 +04003736 error = may_mknod(mode);
3737 if (error)
3738 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003739retry:
3740 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003741 if (IS_ERR(dentry))
3742 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003743
Al Virodae6ad82011-06-26 11:50:15 -04003744 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003745 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003746 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003747 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003748 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003749 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003751 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003752 if (!error)
3753 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 break;
3755 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003756 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 new_decode_dev(dev));
3758 break;
3759 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003760 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 }
Al Viroa8104a92012-07-20 02:25:00 +04003763out:
Al Viro921a1652012-07-20 01:15:31 +04003764 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003765 if (retry_estale(error, lookup_flags)) {
3766 lookup_flags |= LOOKUP_REVAL;
3767 goto retry;
3768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 return error;
3770}
3771
Al Viro8208a222011-07-25 17:32:17 -04003772SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003773{
3774 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3775}
3776
Al Viro18bb1db2011-07-26 01:41:39 -04003777int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003779 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003780 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781
3782 if (error)
3783 return error;
3784
Al Viroacfa4382008-12-04 10:06:33 -05003785 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 return -EPERM;
3787
3788 mode &= (S_IRWXUGO|S_ISVTX);
3789 error = security_inode_mkdir(dir, dentry, mode);
3790 if (error)
3791 return error;
3792
Al Viro8de52772012-02-06 12:45:27 -05003793 if (max_links && dir->i_nlink >= max_links)
3794 return -EMLINK;
3795
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003797 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003798 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return error;
3800}
Al Viro4d359502014-03-14 12:20:17 -04003801EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Al Viroa218d0f2011-11-21 14:59:34 -05003803SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
Dave Hansen6902d922006-09-30 23:29:01 -07003805 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003806 struct path path;
3807 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003808 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003810retry:
3811 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003812 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003813 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003814
Al Virodae6ad82011-06-26 11:50:15 -04003815 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003816 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003817 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003818 if (!error)
3819 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003820 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003821 if (retry_estale(error, lookup_flags)) {
3822 lookup_flags |= LOOKUP_REVAL;
3823 goto retry;
3824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 return error;
3826}
3827
Al Viroa218d0f2011-11-21 14:59:34 -05003828SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003829{
3830 return sys_mkdirat(AT_FDCWD, pathname, mode);
3831}
3832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3834{
3835 int error = may_delete(dir, dentry, 1);
3836
3837 if (error)
3838 return error;
3839
Al Viroacfa4382008-12-04 10:06:33 -05003840 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841 return -EPERM;
3842
Al Viro1d2ef592011-09-14 18:55:41 +01003843 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003844 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845
Sage Weil912dbc12011-05-24 13:06:11 -07003846 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003847 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003848 goto out;
3849
3850 error = security_inode_rmdir(dir, dentry);
3851 if (error)
3852 goto out;
3853
Sage Weil3cebde22011-05-29 21:20:59 -07003854 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003855 error = dir->i_op->rmdir(dir, dentry);
3856 if (error)
3857 goto out;
3858
3859 dentry->d_inode->i_flags |= S_DEAD;
3860 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003861 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003862
3863out:
Al Viro59551022016-01-22 15:40:57 -05003864 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003865 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003866 if (!error)
3867 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 return error;
3869}
Al Viro4d359502014-03-14 12:20:17 -04003870EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003872static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873{
3874 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003875 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003877 struct path path;
3878 struct qstr last;
3879 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003880 unsigned int lookup_flags = 0;
3881retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04003882 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3883 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003884 if (IS_ERR(name))
3885 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Al Virof5beed72015-04-30 16:09:11 -04003887 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003888 case LAST_DOTDOT:
3889 error = -ENOTEMPTY;
3890 goto exit1;
3891 case LAST_DOT:
3892 error = -EINVAL;
3893 goto exit1;
3894 case LAST_ROOT:
3895 error = -EBUSY;
3896 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003898
Al Virof5beed72015-04-30 16:09:11 -04003899 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003900 if (error)
3901 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003902
Al Viro59551022016-01-22 15:40:57 -05003903 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003904 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003906 if (IS_ERR(dentry))
3907 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003908 if (!dentry->d_inode) {
3909 error = -ENOENT;
3910 goto exit3;
3911 }
Al Virof5beed72015-04-30 16:09:11 -04003912 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003913 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003914 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003915 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003916exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003917 dput(dentry);
3918exit2:
Al Viro59551022016-01-22 15:40:57 -05003919 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003920 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921exit1:
Al Virof5beed72015-04-30 16:09:11 -04003922 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003924 if (retry_estale(error, lookup_flags)) {
3925 lookup_flags |= LOOKUP_REVAL;
3926 goto retry;
3927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 return error;
3929}
3930
Heiko Carstens3cdad422009-01-14 14:14:22 +01003931SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003932{
3933 return do_rmdir(AT_FDCWD, pathname);
3934}
3935
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003936/**
3937 * vfs_unlink - unlink a filesystem object
3938 * @dir: parent directory
3939 * @dentry: victim
3940 * @delegated_inode: returns victim inode, if the inode is delegated.
3941 *
3942 * The caller must hold dir->i_mutex.
3943 *
3944 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3945 * return a reference to the inode in delegated_inode. The caller
3946 * should then break the delegation on that inode and retry. Because
3947 * breaking a delegation may take a long time, the caller should drop
3948 * dir->i_mutex before doing so.
3949 *
3950 * Alternatively, a caller may pass NULL for delegated_inode. This may
3951 * be appropriate for callers that expect the underlying filesystem not
3952 * to be NFS exported.
3953 */
3954int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003956 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 int error = may_delete(dir, dentry, 0);
3958
3959 if (error)
3960 return error;
3961
Al Viroacfa4382008-12-04 10:06:33 -05003962 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 return -EPERM;
3964
Al Viro59551022016-01-22 15:40:57 -05003965 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003966 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 error = -EBUSY;
3968 else {
3969 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003970 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003971 error = try_break_deleg(target, delegated_inode);
3972 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003973 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003975 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003976 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003977 detach_mounts(dentry);
3978 }
Al Virobec10522010-03-03 14:12:08 -05003979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003981out:
Al Viro59551022016-01-22 15:40:57 -05003982 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3985 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003986 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 }
Robert Love0eeca282005-07-12 17:06:03 -04003989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 return error;
3991}
Al Viro4d359502014-03-14 12:20:17 -04003992EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993
3994/*
3995 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003996 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 * writeout happening, and we don't want to prevent access to the directory
3998 * while waiting on the I/O.
3999 */
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004000long do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
Al Viro2ad94ae2008-07-21 09:32:51 -04004002 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004004 struct path path;
4005 struct qstr last;
4006 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004008 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004009 unsigned int lookup_flags = 0;
4010retry:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004011 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004012 if (IS_ERR(name))
4013 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004014
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004016 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004018
Al Virof5beed72015-04-30 16:09:11 -04004019 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004020 if (error)
4021 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004022retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004023 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004024 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 error = PTR_ERR(dentry);
4026 if (!IS_ERR(dentry)) {
4027 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004028 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004029 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004031 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004032 goto slashes;
4033 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004034 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004035 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004036 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04004037 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004038exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 dput(dentry);
4040 }
Al Viro59551022016-01-22 15:40:57 -05004041 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (inode)
4043 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004044 inode = NULL;
4045 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004046 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004047 if (!error)
4048 goto retry_deleg;
4049 }
Al Virof5beed72015-04-30 16:09:11 -04004050 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051exit1:
Al Virof5beed72015-04-30 16:09:11 -04004052 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004053 if (retry_estale(error, lookup_flags)) {
4054 lookup_flags |= LOOKUP_REVAL;
4055 inode = NULL;
4056 goto retry;
4057 }
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004058 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 return error;
4060
4061slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004062 if (d_is_negative(dentry))
4063 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004064 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004065 error = -EISDIR;
4066 else
4067 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 goto exit2;
4069}
4070
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004071SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004072{
4073 if ((flag & ~AT_REMOVEDIR) != 0)
4074 return -EINVAL;
4075
4076 if (flag & AT_REMOVEDIR)
4077 return do_rmdir(dfd, pathname);
4078
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004079 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004080}
4081
Heiko Carstens3480b252009-01-14 14:14:16 +01004082SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004083{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004084 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004085}
4086
Miklos Szeredidb2e7472008-06-24 16:50:16 +02004087int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
Miklos Szeredia95164d2008-07-30 15:08:48 +02004089 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091 if (error)
4092 return error;
4093
Al Viroacfa4382008-12-04 10:06:33 -05004094 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 return -EPERM;
4096
4097 error = security_inode_symlink(dir, dentry, oldname);
4098 if (error)
4099 return error;
4100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004102 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004103 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 return error;
4105}
Al Viro4d359502014-03-14 12:20:17 -04004106EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004108SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4109 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110{
Al Viro2ad94ae2008-07-21 09:32:51 -04004111 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004112 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004113 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004114 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004115 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116
4117 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004118 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004120retry:
4121 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004122 error = PTR_ERR(dentry);
4123 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004124 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004125
Jeff Layton91a27b22012-10-10 15:25:28 -04004126 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004127 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04004128 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004129 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004130 if (retry_estale(error, lookup_flags)) {
4131 lookup_flags |= LOOKUP_REVAL;
4132 goto retry;
4133 }
Dave Hansen6902d922006-09-30 23:29:01 -07004134out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 putname(from);
4136 return error;
4137}
4138
Heiko Carstens3480b252009-01-14 14:14:16 +01004139SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004140{
4141 return sys_symlinkat(oldname, AT_FDCWD, newname);
4142}
4143
J. Bruce Fields146a8592011-09-20 17:14:31 -04004144/**
4145 * vfs_link - create a new link
4146 * @old_dentry: object to be linked
4147 * @dir: new parent
4148 * @new_dentry: where to create the new link
4149 * @delegated_inode: returns inode needing a delegation break
4150 *
4151 * The caller must hold dir->i_mutex
4152 *
4153 * If vfs_link discovers a delegation on the to-be-linked file in need
4154 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4155 * inode in delegated_inode. The caller should then break the delegation
4156 * and retry. Because breaking a delegation may take a long time, the
4157 * caller should drop the i_mutex before doing so.
4158 *
4159 * Alternatively, a caller may pass NULL for delegated_inode. This may
4160 * be appropriate for callers that expect the underlying filesystem not
4161 * to be NFS exported.
4162 */
4163int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
4165 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004166 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 int error;
4168
4169 if (!inode)
4170 return -ENOENT;
4171
Miklos Szeredia95164d2008-07-30 15:08:48 +02004172 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 if (error)
4174 return error;
4175
4176 if (dir->i_sb != inode->i_sb)
4177 return -EXDEV;
4178
4179 /*
4180 * A link to an append-only or immutable file cannot be created.
4181 */
4182 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4183 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004184 /*
4185 * Updating the link count will likely cause i_uid and i_gid to
4186 * be writen back improperly if their true value is unknown to
4187 * the vfs.
4188 */
4189 if (HAS_UNMAPPED_ID(inode))
4190 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004191 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004193 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 return -EPERM;
4195
4196 error = security_inode_link(old_dentry, dir, new_dentry);
4197 if (error)
4198 return error;
4199
Al Viro59551022016-01-22 15:40:57 -05004200 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304201 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004202 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304203 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004204 else if (max_links && inode->i_nlink >= max_links)
4205 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004206 else {
4207 error = try_break_deleg(inode, delegated_inode);
4208 if (!error)
4209 error = dir->i_op->link(old_dentry, dir, new_dentry);
4210 }
Al Virof4e0c302013-06-11 08:34:36 +04004211
4212 if (!error && (inode->i_state & I_LINKABLE)) {
4213 spin_lock(&inode->i_lock);
4214 inode->i_state &= ~I_LINKABLE;
4215 spin_unlock(&inode->i_lock);
4216 }
Al Viro59551022016-01-22 15:40:57 -05004217 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004218 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004219 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 return error;
4221}
Al Viro4d359502014-03-14 12:20:17 -04004222EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224/*
4225 * Hardlinks are often used in delicate situations. We avoid
4226 * security-related surprises by not following symlinks on the
4227 * newname. --KAB
4228 *
4229 * We don't follow them on the oldname either to be compatible
4230 * with linux 2.0, and to avoid hard-linking to directories
4231 * and other special files. --ADM
4232 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004233SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4234 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
4236 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004237 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004238 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304239 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304242 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004243 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304244 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004245 * To use null names we require CAP_DAC_READ_SEARCH
4246 * This ensures that not everyone will be able to create
4247 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304248 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004249 if (flags & AT_EMPTY_PATH) {
4250 if (!capable(CAP_DAC_READ_SEARCH))
4251 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304252 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004253 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004254
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304255 if (flags & AT_SYMLINK_FOLLOW)
4256 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004257retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304258 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004260 return error;
4261
Jeff Layton442e31c2012-12-20 16:15:38 -05004262 new_dentry = user_path_create(newdfd, newname, &new_path,
4263 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004265 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004266 goto out;
4267
4268 error = -EXDEV;
4269 if (old_path.mnt != new_path.mnt)
4270 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004271 error = may_linkat(&old_path);
4272 if (unlikely(error))
4273 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004274 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004275 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004276 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004277 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004278out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004279 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004280 if (delegated_inode) {
4281 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004282 if (!error) {
4283 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004284 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004285 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004286 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004287 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004288 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004289 how |= LOOKUP_REVAL;
4290 goto retry;
4291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292out:
Al Viro2d8f3032008-07-22 09:59:21 -04004293 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294
4295 return error;
4296}
4297
Heiko Carstens3480b252009-01-14 14:14:16 +01004298SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004299{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004300 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004301}
4302
Miklos Szeredibc270272014-04-01 17:08:42 +02004303/**
4304 * vfs_rename - rename a filesystem object
4305 * @old_dir: parent of source
4306 * @old_dentry: source
4307 * @new_dir: parent of destination
4308 * @new_dentry: destination
4309 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004310 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004311 *
4312 * The caller must hold multiple mutexes--see lock_rename()).
4313 *
4314 * If vfs_rename discovers a delegation in need of breaking at either
4315 * the source or destination, it will return -EWOULDBLOCK and return a
4316 * reference to the inode in delegated_inode. The caller should then
4317 * break the delegation and retry. Because breaking a delegation may
4318 * take a long time, the caller should drop all locks before doing
4319 * so.
4320 *
4321 * Alternatively, a caller may pass NULL for delegated_inode. This may
4322 * be appropriate for callers that expect the underlying filesystem not
4323 * to be NFS exported.
4324 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 * The worst of all namespace operations - renaming directory. "Perverted"
4326 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4327 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004328 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004329 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 * b) race potential - two innocent renames can create a loop together.
4331 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004332 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004334 * c) we have to lock _four_ objects - parents and victim (if it exists),
4335 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004336 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 * whether the target exists). Solution: try to be smart with locking
4338 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004339 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 * move will be locked. Thus we can rank directories by the tree
4341 * (ancestors first) and rank all non-directories after them.
4342 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004343 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 * HOWEVER, it relies on the assumption that any object with ->lookup()
4345 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4346 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004347 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004348 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004350 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 * locking].
4352 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004354 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004355 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356{
4357 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004358 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004359 struct inode *source = old_dentry->d_inode;
4360 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004361 bool new_is_dir = false;
4362 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004363 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004365 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004366 return 0;
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 error = may_delete(old_dir, old_dentry, is_dir);
4369 if (error)
4370 return error;
4371
Miklos Szeredida1ce062014-04-01 17:08:43 +02004372 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004373 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004374 } else {
4375 new_is_dir = d_is_dir(new_dentry);
4376
4377 if (!(flags & RENAME_EXCHANGE))
4378 error = may_delete(new_dir, new_dentry, is_dir);
4379 else
4380 error = may_delete(new_dir, new_dentry, new_is_dir);
4381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 if (error)
4383 return error;
4384
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004385 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 return -EPERM;
4387
Miklos Szeredibc270272014-04-01 17:08:42 +02004388 /*
4389 * If we are going to change the parent - check write permissions,
4390 * we'll need to flip '..'.
4391 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004392 if (new_dir != old_dir) {
4393 if (is_dir) {
4394 error = inode_permission(source, MAY_WRITE);
4395 if (error)
4396 return error;
4397 }
4398 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4399 error = inode_permission(target, MAY_WRITE);
4400 if (error)
4401 return error;
4402 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004403 }
Robert Love0eeca282005-07-12 17:06:03 -04004404
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004405 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4406 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004407 if (error)
4408 return error;
4409
Al Viro49d31c22017-07-07 14:51:19 -04004410 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004411 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004412 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004413 lock_two_nondirectories(source, target);
4414 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004415 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004416
4417 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004418 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004419 goto out;
4420
Miklos Szeredida1ce062014-04-01 17:08:43 +02004421 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004422 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004423 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004424 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004425 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4426 old_dir->i_nlink >= max_links)
4427 goto out;
4428 }
4429 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4430 shrink_dcache_parent(new_dentry);
4431 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004432 error = try_break_deleg(source, delegated_inode);
4433 if (error)
4434 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004435 }
4436 if (target && !new_is_dir) {
4437 error = try_break_deleg(target, delegated_inode);
4438 if (error)
4439 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004440 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004441 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004442 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004443 if (error)
4444 goto out;
4445
Miklos Szeredida1ce062014-04-01 17:08:43 +02004446 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004447 if (is_dir)
4448 target->i_flags |= S_DEAD;
4449 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004450 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004451 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004452 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4453 if (!(flags & RENAME_EXCHANGE))
4454 d_move(old_dentry, new_dentry);
4455 else
4456 d_exchange(old_dentry, new_dentry);
4457 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004458out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004459 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004460 unlock_two_nondirectories(source, target);
4461 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004462 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004463 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004464 if (!error) {
Al Viro49d31c22017-07-07 14:51:19 -04004465 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004466 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4467 if (flags & RENAME_EXCHANGE) {
4468 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4469 new_is_dir, NULL, new_dentry);
4470 }
4471 }
Al Viro49d31c22017-07-07 14:51:19 -04004472 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 return error;
4475}
Al Viro4d359502014-03-14 12:20:17 -04004476EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004478SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4479 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
Al Viro2ad94ae2008-07-21 09:32:51 -04004481 struct dentry *old_dentry, *new_dentry;
4482 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004483 struct path old_path, new_path;
4484 struct qstr old_last, new_last;
4485 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004486 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004487 struct filename *from;
4488 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004489 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004490 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004491 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004492
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004493 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004494 return -EINVAL;
4495
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004496 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4497 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004498 return -EINVAL;
4499
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004500 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4501 return -EPERM;
4502
Al Virof5beed72015-04-30 16:09:11 -04004503 if (flags & RENAME_EXCHANGE)
4504 target_flags = 0;
4505
Jeff Laytonc6a94282012-12-11 12:10:10 -05004506retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04004507 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4508 &old_path, &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004509 if (IS_ERR(from)) {
4510 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004512 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513
Al Viroc1d4dd22016-06-05 16:38:18 -04004514 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4515 &new_path, &new_last, &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004516 if (IS_ERR(to)) {
4517 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
4521 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004522 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523 goto exit2;
4524
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004526 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 goto exit2;
4528
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004529 if (flags & RENAME_NOREPLACE)
4530 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004531 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 goto exit2;
4533
Al Virof5beed72015-04-30 16:09:11 -04004534 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004535 if (error)
4536 goto exit2;
4537
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004538retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004539 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
Al Virof5beed72015-04-30 16:09:11 -04004541 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 error = PTR_ERR(old_dentry);
4543 if (IS_ERR(old_dentry))
4544 goto exit3;
4545 /* source must exist */
4546 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004547 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004549 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 error = PTR_ERR(new_dentry);
4551 if (IS_ERR(new_dentry))
4552 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004553 error = -EEXIST;
4554 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4555 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004556 if (flags & RENAME_EXCHANGE) {
4557 error = -ENOENT;
4558 if (d_is_negative(new_dentry))
4559 goto exit5;
4560
4561 if (!d_is_dir(new_dentry)) {
4562 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004563 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004564 goto exit5;
4565 }
4566 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004567 /* unless the source is a directory trailing slashes give -ENOTDIR */
4568 if (!d_is_dir(old_dentry)) {
4569 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004570 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004571 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004572 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004573 goto exit5;
4574 }
4575 /* source should not be ancestor of target */
4576 error = -EINVAL;
4577 if (old_dentry == trap)
4578 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004580 if (!(flags & RENAME_EXCHANGE))
4581 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 if (new_dentry == trap)
4583 goto exit5;
4584
Al Virof5beed72015-04-30 16:09:11 -04004585 error = security_path_rename(&old_path, old_dentry,
4586 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004587 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004588 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004589 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4590 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004591 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592exit5:
4593 dput(new_dentry);
4594exit4:
4595 dput(old_dentry);
4596exit3:
Al Virof5beed72015-04-30 16:09:11 -04004597 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004598 if (delegated_inode) {
4599 error = break_deleg_wait(&delegated_inode);
4600 if (!error)
4601 goto retry_deleg;
4602 }
Al Virof5beed72015-04-30 16:09:11 -04004603 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004605 if (retry_estale(error, lookup_flags))
4606 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004607 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004608 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609exit1:
Al Virof5beed72015-04-30 16:09:11 -04004610 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004612 if (should_retry) {
4613 should_retry = false;
4614 lookup_flags |= LOOKUP_REVAL;
4615 goto retry;
4616 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004617exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return error;
4619}
4620
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004621SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4622 int, newdfd, const char __user *, newname)
4623{
4624 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4625}
4626
Heiko Carstensa26eab22009-01-14 14:14:17 +01004627SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004628{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004629 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004630}
4631
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004632int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4633{
4634 int error = may_create(dir, dentry);
4635 if (error)
4636 return error;
4637
4638 if (!dir->i_op->mknod)
4639 return -EPERM;
4640
4641 return dir->i_op->mknod(dir, dentry,
4642 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4643}
4644EXPORT_SYMBOL(vfs_whiteout);
4645
Al Viro5d826c82014-03-14 13:42:45 -04004646int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647{
Al Viro5d826c82014-03-14 13:42:45 -04004648 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (IS_ERR(link))
4650 goto out;
4651
4652 len = strlen(link);
4653 if (len > (unsigned) buflen)
4654 len = buflen;
4655 if (copy_to_user(buffer, link, len))
4656 len = -EFAULT;
4657out:
4658 return len;
4659}
4660
4661/*
4662 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
Al Viro6b255392015-11-17 10:20:54 -05004663 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4664 * for any given inode is up to filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 */
Miklos Szeredid16744e2016-12-09 16:45:04 +01004666static int generic_readlink(struct dentry *dentry, char __user *buffer,
4667 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Al Virofceef392015-12-29 15:58:39 -05004669 DEFINE_DELAYED_CALL(done);
Al Viro5f2c4172015-05-07 11:14:26 -04004670 struct inode *inode = d_inode(dentry);
4671 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004672 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004673
Al Virod4dee482015-04-30 20:08:02 -04004674 if (!link) {
Al Virofceef392015-12-29 15:58:39 -05004675 link = inode->i_op->get_link(dentry, inode, &done);
Al Virod4dee482015-04-30 20:08:02 -04004676 if (IS_ERR(link))
4677 return PTR_ERR(link);
4678 }
Al Viro680baac2015-05-02 13:32:22 -04004679 res = readlink_copy(buffer, buflen, link);
Al Virofceef392015-12-29 15:58:39 -05004680 do_delayed_call(&done);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004681 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682}
4683
Miklos Szeredid60874c2016-10-04 14:40:45 +02004684/**
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004685 * vfs_readlink - copy symlink body into userspace buffer
4686 * @dentry: dentry on which to get symbolic link
4687 * @buffer: user memory pointer
4688 * @buflen: size of buffer
4689 *
4690 * Does not touch atime. That's up to the caller if necessary
4691 *
4692 * Does not call security hook.
4693 */
4694int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4695{
4696 struct inode *inode = d_inode(dentry);
4697
Miklos Szeredi76fca902016-12-09 16:45:04 +01004698 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4699 if (unlikely(inode->i_op->readlink))
4700 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004701
Miklos Szeredi76fca902016-12-09 16:45:04 +01004702 if (!d_is_symlink(dentry))
4703 return -EINVAL;
4704
4705 spin_lock(&inode->i_lock);
4706 inode->i_opflags |= IOP_DEFAULT_READLINK;
4707 spin_unlock(&inode->i_lock);
4708 }
4709
4710 return generic_readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004711}
4712EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Miklos Szeredid60874c2016-10-04 14:40:45 +02004714/**
4715 * vfs_get_link - get symlink body
4716 * @dentry: dentry on which to get symbolic link
4717 * @done: caller needs to free returned data with this
4718 *
4719 * Calls security hook and i_op->get_link() on the supplied inode.
4720 *
4721 * It does not touch atime. That's up to the caller if necessary.
4722 *
4723 * Does not work on "special" symlinks like /proc/$$/fd/N
4724 */
4725const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4726{
4727 const char *res = ERR_PTR(-EINVAL);
4728 struct inode *inode = d_inode(dentry);
4729
4730 if (d_is_symlink(dentry)) {
4731 res = ERR_PTR(security_inode_readlink(dentry));
4732 if (!res)
4733 res = inode->i_op->get_link(dentry, inode, done);
4734 }
4735 return res;
4736}
4737EXPORT_SYMBOL(vfs_get_link);
4738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004740const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004741 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004743 char *kaddr;
4744 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004745 struct address_space *mapping = inode->i_mapping;
4746
Al Virod3883d42015-11-17 10:41:04 -05004747 if (!dentry) {
4748 page = find_get_page(mapping, 0);
4749 if (!page)
4750 return ERR_PTR(-ECHILD);
4751 if (!PageUptodate(page)) {
4752 put_page(page);
4753 return ERR_PTR(-ECHILD);
4754 }
4755 } else {
4756 page = read_mapping_page(mapping, 0, NULL);
4757 if (IS_ERR(page))
4758 return (char*)page;
4759 }
Al Virofceef392015-12-29 15:58:39 -05004760 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004761 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4762 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004763 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004764 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765}
4766
Al Viro6b255392015-11-17 10:20:54 -05004767EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Al Virofceef392015-12-29 15:58:39 -05004769void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770{
Al Virofceef392015-12-29 15:58:39 -05004771 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772}
Al Viro4d359502014-03-14 12:20:17 -04004773EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Al Viroaa80dea2015-11-16 18:26:34 -05004775int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4776{
Al Virofceef392015-12-29 15:58:39 -05004777 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004778 int res = readlink_copy(buffer, buflen,
4779 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004780 &done));
4781 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004782 return res;
4783}
4784EXPORT_SYMBOL(page_readlink);
4785
Nick Piggin54566b22009-01-04 12:00:53 -08004786/*
4787 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4788 */
4789int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
4791 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004792 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004793 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004794 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004795 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004796 if (nofs)
4797 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798
NeilBrown7e53cac2006-03-25 03:07:57 -08004799retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004800 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004801 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004803 goto fail;
4804
Al Viro21fc61c2015-11-17 01:07:57 -05004805 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004806
4807 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4808 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 if (err < 0)
4810 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004811 if (err < len-1)
4812 goto retry;
4813
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 mark_inode_dirty(inode);
4815 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816fail:
4817 return err;
4818}
Al Viro4d359502014-03-14 12:20:17 -04004819EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004821int page_symlink(struct inode *inode, const char *symname, int len)
4822{
4823 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004824 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004825}
Al Viro4d359502014-03-14 12:20:17 -04004826EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004827
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004828const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004829 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831EXPORT_SYMBOL(page_symlink_inode_operations);