blob: 7fd801af7e50aa044c34c79cd3077d86baf8d43f [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Eric Parise81e3f42009-12-04 15:47:36 -050043#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050044#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050045
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/* [Feb-1997 T. Schoebel-Theuer]
47 * Fundamental changes in the pathname lookup mechanisms (namei)
48 * were necessary because of omirr. The reason is that omirr needs
49 * to know the _real_ pathname, not the user-supplied one, in case
50 * of symlinks (and also when transname replacements occur).
51 *
52 * The new code replaces the old recursive symlink resolution with
53 * an iterative one (in case of non-nested symlink chains). It does
54 * this with calls to <fs>_follow_link().
55 * As a side effect, dir_namei(), _namei() and follow_link() are now
56 * replaced with a single function lookup_dentry() that can handle all
57 * the special cases of the former code.
58 *
59 * With the new dcache, the pathname is stored at each inode, at least as
60 * long as the refcount of the inode is positive. As a side effect, the
61 * size of the dcache depends on the inode cache and thus is dynamic.
62 *
63 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
64 * resolution to correspond with current state of the code.
65 *
66 * Note that the symlink resolution is not *completely* iterative.
67 * There is still a significant amount of tail- and mid- recursion in
68 * the algorithm. Also, note that <fs>_readlink() is not used in
69 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
70 * may return different results than <fs>_follow_link(). Many virtual
71 * filesystems (including /proc) exhibit this behavior.
72 */
73
74/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
75 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
76 * and the name already exists in form of a symlink, try to create the new
77 * name indicated by the symlink. The old code always complained that the
78 * name already exists, due to not following the symlink even if its target
79 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030080 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *
82 * I don't know which semantics is the right one, since I have no access
83 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
84 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
85 * "old" one. Personally, I think the new semantics is much more logical.
86 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
87 * file does succeed in both HP-UX and SunOs, but not in Solaris
88 * and in the old Linux semantics.
89 */
90
91/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
92 * semantics. See the comments in "open_namei" and "do_link" below.
93 *
94 * [10-Sep-98 Alan Modra] Another symlink change.
95 */
96
97/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
98 * inside the path - always follow.
99 * in the last component in creation/removal/renaming - never follow.
100 * if LOOKUP_FOLLOW passed - follow.
101 * if the pathname has trailing slashes - follow.
102 * otherwise - don't follow.
103 * (applied in that order).
104 *
105 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
106 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
107 * During the 2.4 we need to fix the userland stuff depending on it -
108 * hopefully we will be able to get rid of that wart in 2.5. So far only
109 * XEmacs seems to be relying on it...
110 */
111/*
112 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800113 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * any extra contention...
115 */
116
117/* In order to reduce some races, while at the same time doing additional
118 * checking and hopefully speeding things up, we copy filenames to the
119 * kernel data space before using them..
120 *
121 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
122 * PATH_MAX includes the nul terminator --RR.
123 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400124
Al Virofd2f7cb2015-02-22 20:07:13 -0500125#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400126
David Drysdale51f39a12014-12-12 16:57:29 -0800127struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400128getname_flags(const char __user *filename, int flags, int *empty)
129{
Al Viro94b5d262015-02-22 19:38:03 -0500130 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400131 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500132 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Jeff Layton7ac86262012-10-10 15:25:28 -0400134 result = audit_reusename(filename);
135 if (result)
136 return result;
137
Jeff Layton7950e382012-10-10 16:43:13 -0400138 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700139 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500140 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Jeff Layton7950e382012-10-10 16:43:13 -0400142 /*
143 * First, try to embed the struct filename inside the names_cache
144 * allocation
145 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500146 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400147 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400148
Al Viro94b5d262015-02-22 19:38:03 -0500149 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400150 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500151 __putname(result);
152 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700154
Jeff Layton7950e382012-10-10 16:43:13 -0400155 /*
156 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
157 * separate struct filename so we can dedicate the entire
158 * names_cache allocation for the pathname, and re-do the copy from
159 * userland.
160 */
Al Viro94b5d262015-02-22 19:38:03 -0500161 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500162 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400163 kname = (char *)result;
164
Al Virofd2f7cb2015-02-22 20:07:13 -0500165 /*
166 * size is chosen that way we to guarantee that
167 * result->iname[0] is within the same object and that
168 * kname can't be equal to result->iname, no matter what.
169 */
170 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500171 if (unlikely(!result)) {
172 __putname(kname);
173 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400174 }
175 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500176 len = strncpy_from_user(kname, filename, PATH_MAX);
177 if (unlikely(len < 0)) {
178 __putname(kname);
179 kfree(result);
180 return ERR_PTR(len);
181 }
182 if (unlikely(len == PATH_MAX)) {
183 __putname(kname);
184 kfree(result);
185 return ERR_PTR(-ENAMETOOLONG);
186 }
Jeff Layton7950e382012-10-10 16:43:13 -0400187 }
188
Al Viro94b5d262015-02-22 19:38:03 -0500189 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700190 /* The empty path is special. */
191 if (unlikely(!len)) {
192 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500193 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500194 if (!(flags & LOOKUP_EMPTY)) {
195 putname(result);
196 return ERR_PTR(-ENOENT);
197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700199
Jeff Layton7950e382012-10-10 16:43:13 -0400200 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800201 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400202 audit_getname(result);
203 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Jeff Layton91a27b22012-10-10 15:25:28 -0400206struct filename *
207getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400208{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700209 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400210}
211
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800212struct filename *
213getname_kernel(const char * filename)
214{
215 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500216 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800217
218 result = __getname();
219 if (unlikely(!result))
220 return ERR_PTR(-ENOMEM);
221
Paul Moore08518542015-01-21 23:59:56 -0500222 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500223 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500224 } else if (len <= PATH_MAX) {
Al Viro30ce4d12018-04-08 11:57:10 -0400225 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500226 struct filename *tmp;
227
Al Viro30ce4d12018-04-08 11:57:10 -0400228 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500229 if (unlikely(!tmp)) {
230 __putname(result);
231 return ERR_PTR(-ENOMEM);
232 }
233 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500234 result = tmp;
235 } else {
236 __putname(result);
237 return ERR_PTR(-ENAMETOOLONG);
238 }
239 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800240 result->uptr = NULL;
241 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500242 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500243 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800244
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800245 return result;
246}
247
Jeff Layton91a27b22012-10-10 15:25:28 -0400248void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
Paul Moore55422d02015-01-22 00:00:23 -0500250 BUG_ON(name->refcnt <= 0);
251
252 if (--name->refcnt > 0)
253 return;
254
Al Virofd2f7cb2015-02-22 20:07:13 -0500255 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500256 __putname(name->name);
257 kfree(name);
258 } else
259 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Linus Torvaldse77819e2011-07-22 19:30:19 -0700262static int check_acl(struct inode *inode, int mask)
263{
Linus Torvalds84635d62011-07-25 22:47:03 -0700264#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700265 struct posix_acl *acl;
266
Linus Torvaldse77819e2011-07-22 19:30:19 -0700267 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400268 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
269 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700270 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400271 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100272 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400273 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300274 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700275 }
276
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800277 acl = get_acl(inode, ACL_TYPE_ACCESS);
278 if (IS_ERR(acl))
279 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700280 if (acl) {
281 int error = posix_acl_permission(inode, acl, mask);
282 posix_acl_release(acl);
283 return error;
284 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700285#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700286
287 return -EAGAIN;
288}
289
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700290/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530291 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700292 */
Al Viro7e401452011-06-20 19:12:17 -0400293static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700294{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700295 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700296
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800297 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700298 mode >>= 6;
299 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700300 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400301 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100302 if (error != -EAGAIN)
303 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700304 }
305
306 if (in_group_p(inode->i_gid))
307 mode >>= 3;
308 }
309
310 /*
311 * If the DACs are ok we don't need any capability check.
312 */
Al Viro9c2c7032011-06-20 19:06:22 -0400313 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700314 return 0;
315 return -EACCES;
316}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
318/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100319 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530321 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 *
323 * Used to check for read/write/execute permissions on a file.
324 * We use "fsuid" for this, letting us set arbitrary permissions
325 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100326 * are used for other things.
327 *
328 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
329 * request cannot be satisfied (eg. requires blocking or too much complexity).
330 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 */
Al Viro2830ba72011-06-20 19:16:29 -0400332int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700334 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530337 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 */
Al Viro7e401452011-06-20 19:12:17 -0400339 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700340 if (ret != -EACCES)
341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Al Virod594e7e2011-06-20 19:55:42 -0400343 if (S_ISDIR(inode->i_mode)) {
344 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400345 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700346 if (capable_wrt_inode_uidgid(inode,
347 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400348 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700349 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500351 return -EACCES;
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 /*
355 * Searching includes executable on directories, else just read.
356 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600357 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400358 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700359 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500361 /*
362 * Read/write DACs are always overridable.
363 * Executable DACs are overridable when there is
364 * at least one exec bit set.
365 */
366 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
367 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
368 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 return -EACCES;
371}
Al Viro4d359502014-03-14 12:20:17 -0400372EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700374/*
375 * We _really_ want to just do "generic_permission()" without
376 * even looking at the inode->i_op values. So we keep a cache
377 * flag in inode->i_opflags, that says "this has not special
378 * permission function, use the fast case".
379 */
380static inline int do_inode_permission(struct inode *inode, int mask)
381{
382 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
383 if (likely(inode->i_op->permission))
384 return inode->i_op->permission(inode, mask);
385
386 /* This gets set once for the inode lifetime */
387 spin_lock(&inode->i_lock);
388 inode->i_opflags |= IOP_FASTPERM;
389 spin_unlock(&inode->i_lock);
390 }
391 return generic_permission(inode, mask);
392}
393
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200394/**
David Howells0bdaea92012-06-25 12:55:46 +0100395 * sb_permission - Check superblock-level permissions
396 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700397 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100398 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
399 *
400 * Separate out file-system wide checks from inode-specific permission checks.
401 */
402static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
403{
404 if (unlikely(mask & MAY_WRITE)) {
405 umode_t mode = inode->i_mode;
406
407 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100408 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100409 return -EROFS;
410 }
411 return 0;
412}
413
414/**
415 * inode_permission - Check for access rights to a given inode
416 * @inode: Inode to check permission on
417 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
418 *
419 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
420 * this, letting us set arbitrary permissions for filesystem access without
421 * changing the "normal" UIDs which are used for other things.
422 *
423 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
424 */
425int inode_permission(struct inode *inode, int mask)
426{
427 int retval;
428
429 retval = sb_permission(inode->i_sb, inode, mask);
430 if (retval)
431 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800432
433 if (unlikely(mask & MAY_WRITE)) {
434 /*
435 * Nobody gets write access to an immutable file.
436 */
437 if (IS_IMMUTABLE(inode))
438 return -EPERM;
439
440 /*
441 * Updating mtime will likely cause i_uid and i_gid to be
442 * written back improperly if their true value is unknown
443 * to the vfs.
444 */
445 if (HAS_UNMAPPED_ID(inode))
446 return -EACCES;
447 }
448
449 retval = do_inode_permission(inode, mask);
450 if (retval)
451 return retval;
452
453 retval = devcgroup_inode_permission(inode, mask);
454 if (retval)
455 return retval;
456
457 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100458}
Al Viro4d359502014-03-14 12:20:17 -0400459EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100460
461/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800462 * path_get - get a reference to a path
463 * @path: path to get the reference to
464 *
465 * Given a path increment the reference count to the dentry and the vfsmount.
466 */
Al Virodcf787f2013-03-01 23:51:07 -0500467void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800468{
469 mntget(path->mnt);
470 dget(path->dentry);
471}
472EXPORT_SYMBOL(path_get);
473
474/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800475 * path_put - put a reference to a path
476 * @path: path to put the reference to
477 *
478 * Given a path decrement the reference count to the dentry and the vfsmount.
479 */
Al Virodcf787f2013-03-01 23:51:07 -0500480void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
Jan Blunck1d957f92008-02-14 19:34:35 -0800482 dput(path->dentry);
483 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Jan Blunck1d957f92008-02-14 19:34:35 -0800485EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Al Viro894bc8c2015-05-02 07:16:16 -0400487#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400488struct nameidata {
489 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400490 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400491 struct path root;
492 struct inode *inode; /* path.dentry.d_inode */
493 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400494 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400495 int last_type;
496 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100497 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400498 struct saved {
499 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500500 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400501 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400502 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400503 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400504 struct filename *name;
505 struct nameidata *saved;
Al Virofceef392015-12-29 15:58:39 -0500506 struct inode *link_inode;
Al Viro9883d182015-05-13 07:28:08 -0400507 unsigned root_seq;
508 int dfd;
Kees Cook3859a272016-10-28 01:22:25 -0700509} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400510
Al Viro9883d182015-05-13 07:28:08 -0400511static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400512{
NeilBrown756daf22015-03-23 13:37:38 +1100513 struct nameidata *old = current->nameidata;
514 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400515 p->dfd = dfd;
516 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100517 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400518 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100519 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400520}
521
Al Viro9883d182015-05-13 07:28:08 -0400522static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400523{
Al Viro9883d182015-05-13 07:28:08 -0400524 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100525
526 current->nameidata = old;
527 if (old)
528 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500529 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100530 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400531}
532
533static int __nd_alloc_stack(struct nameidata *nd)
534{
Al Virobc40aee2015-05-09 13:04:24 -0400535 struct saved *p;
536
537 if (nd->flags & LOOKUP_RCU) {
Kees Cook6da2ec52018-06-12 13:55:00 -0700538 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
Al Virobc40aee2015-05-09 13:04:24 -0400539 GFP_ATOMIC);
540 if (unlikely(!p))
541 return -ECHILD;
542 } else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700543 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400544 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400545 if (unlikely(!p))
546 return -ENOMEM;
547 }
Al Viro894bc8c2015-05-02 07:16:16 -0400548 memcpy(p, nd->internal, sizeof(nd->internal));
549 nd->stack = p;
550 return 0;
551}
552
Eric W. Biederman397d4252015-08-15 20:27:13 -0500553/**
554 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
555 * @path: nameidate to verify
556 *
557 * Rename can sometimes move a file or directory outside of a bind
558 * mount, path_connected allows those cases to be detected.
559 */
560static bool path_connected(const struct path *path)
561{
562 struct vfsmount *mnt = path->mnt;
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500563 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500564
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500565 /* Bind mounts and multi-root filesystems can have disconnected paths */
566 if (!(sb->s_iflags & SB_I_MULTIROOT) && (mnt->mnt_root == sb->s_root))
Eric W. Biederman397d4252015-08-15 20:27:13 -0500567 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 Viro84a2bd32019-07-16 21:20:17 -0400599 if (nd->flags & LOOKUP_ROOT_GRABBED) {
Al Viro102b8af2015-05-12 17:35:52 -0400600 path_put(&nd->root);
Al Viro84a2bd32019-07-16 21:20:17 -0400601 nd->flags &= ~LOOKUP_ROOT_GRABBED;
Al Viro102b8af2015-05-12 17:35:52 -0400602 }
Al Viro79733872015-05-09 12:55:43 -0400603 } else {
604 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400605 rcu_read_unlock();
606 }
607 nd->depth = 0;
608}
609
610/* path_put is needed afterwards regardless of success or failure */
611static bool legitimize_path(struct nameidata *nd,
612 struct path *path, unsigned seq)
613{
614 int res = __legitimize_mnt(path->mnt, nd->m_seq);
615 if (unlikely(res)) {
616 if (res > 0)
617 path->mnt = NULL;
618 path->dentry = NULL;
619 return false;
620 }
621 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
622 path->dentry = NULL;
623 return false;
624 }
625 return !read_seqcount_retry(&path->dentry->d_seq, seq);
626}
627
628static bool legitimize_links(struct nameidata *nd)
629{
630 int i;
631 for (i = 0; i < nd->depth; i++) {
632 struct saved *last = nd->stack + i;
633 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
634 drop_links(nd);
635 nd->depth = i + 1;
636 return false;
637 }
638 }
639 return true;
640}
641
Al Viroee594bf2019-07-16 21:05:36 -0400642static bool legitimize_root(struct nameidata *nd)
643{
644 if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
645 return true;
Al Viro84a2bd32019-07-16 21:20:17 -0400646 nd->flags |= LOOKUP_ROOT_GRABBED;
Al Viroee594bf2019-07-16 21:05:36 -0400647 return legitimize_path(nd, &nd->root, nd->root_seq);
648}
649
Al Viro19660af2011-03-25 10:32:48 -0400650/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100651 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400652 * Documentation/filesystems/path-lookup.txt). In situations when we can't
653 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500654 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400655 * mode. Refcounts are grabbed at the last known good point before rcu-walk
656 * got stuck, so ref-walk may continue from there. If this is not successful
657 * (eg. a seqcount has changed), then failure is returned and it's up to caller
658 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100659 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100660
661/**
Al Viro19660af2011-03-25 10:32:48 -0400662 * unlazy_walk - try to switch to ref-walk mode.
663 * @nd: nameidata pathwalk data
Randy Dunlap39191622011-01-08 19:36:21 -0800664 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100665 *
Al Viro4675ac32017-01-09 22:29:15 -0500666 * unlazy_walk attempts to legitimize the current nd->path and nd->root
667 * for ref-walk mode.
668 * Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400669 * Nothing should touch nameidata between unlazy_walk() failure and
670 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100671 */
Al Viro4675ac32017-01-09 22:29:15 -0500672static int unlazy_walk(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100673{
Nick Piggin31e6b012011-01-07 17:49:52 +1100674 struct dentry *parent = nd->path.dentry;
675
676 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700677
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700678 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400679 if (unlikely(!legitimize_links(nd)))
Al Viro4675ac32017-01-09 22:29:15 -0500680 goto out1;
Al Viro84a2bd32019-07-16 21:20:17 -0400681 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
682 goto out;
Al Viroee594bf2019-07-16 21:05:36 -0400683 if (unlikely(!legitimize_root(nd)))
684 goto out;
Al Viro4675ac32017-01-09 22:29:15 -0500685 rcu_read_unlock();
686 BUG_ON(nd->inode != parent->d_inode);
687 return 0;
688
Al Viro84a2bd32019-07-16 21:20:17 -0400689out1:
Al Viro4675ac32017-01-09 22:29:15 -0500690 nd->path.mnt = NULL;
691 nd->path.dentry = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500692out:
693 rcu_read_unlock();
694 return -ECHILD;
695}
696
697/**
698 * unlazy_child - try to switch to ref-walk mode.
699 * @nd: nameidata pathwalk data
700 * @dentry: child of nd->path.dentry
701 * @seq: seq number to check dentry against
702 * Returns: 0 on success, -ECHILD on failure
703 *
704 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
705 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
706 * @nd. Must be called from rcu-walk context.
707 * Nothing should touch nameidata between unlazy_child() failure and
708 * terminate_walk().
709 */
710static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
711{
712 BUG_ON(!(nd->flags & LOOKUP_RCU));
713
714 nd->flags &= ~LOOKUP_RCU;
715 if (unlikely(!legitimize_links(nd)))
716 goto out2;
Al Viro79733872015-05-09 12:55:43 -0400717 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
718 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500719 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro79733872015-05-09 12:55:43 -0400720 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400721
Linus Torvalds15570082013-09-02 11:38:06 -0700722 /*
Al Viro4675ac32017-01-09 22:29:15 -0500723 * We need to move both the parent and the dentry from the RCU domain
724 * to be properly refcounted. And the sequence number in the dentry
725 * validates *both* dentry counters, since we checked the sequence
726 * number of the parent after we got the child sequence number. So we
727 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700728 */
Al Viro4675ac32017-01-09 22:29:15 -0500729 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
730 goto out;
Al Viro84a2bd32019-07-16 21:20:17 -0400731 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
732 goto out_dput;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700733 /*
734 * Sequence counts matched. Now make sure that the root is
735 * still valid and get it if required.
736 */
Al Viro84a2bd32019-07-16 21:20:17 -0400737 if (unlikely(!legitimize_root(nd)))
738 goto out_dput;
Al Viro8b61e742013-11-08 12:45:01 -0500739 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100740 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400741
Al Viro79733872015-05-09 12:55:43 -0400742out2:
743 nd->path.mnt = NULL;
744out1:
745 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700746out:
Al Viro8b61e742013-11-08 12:45:01 -0500747 rcu_read_unlock();
Al Viro84a2bd32019-07-16 21:20:17 -0400748 return -ECHILD;
749out_dput:
750 rcu_read_unlock();
751 dput(dentry);
Nick Piggin31e6b012011-01-07 17:49:52 +1100752 return -ECHILD;
753}
754
Al Viro4ce16ef32012-06-10 16:10:59 -0400755static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100756{
Al Viroa89f8332017-01-09 22:25:28 -0500757 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
758 return dentry->d_op->d_revalidate(dentry, flags);
759 else
760 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100761}
762
Al Viro9f1fafe2011-03-25 11:00:12 -0400763/**
764 * complete_walk - successful completion of path walk
765 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500766 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400767 * If we had been in RCU mode, drop out of it and legitimize nd->path.
768 * Revalidate the final result, unless we'd already done that during
769 * the path walk or the filesystem doesn't ask for it. Return 0 on
770 * success, -error on failure. In case of failure caller does not
771 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500772 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400773static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500774{
Al Viro16c2cd72011-02-22 15:50:10 -0500775 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500776 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500777
Al Viro9f1fafe2011-03-25 11:00:12 -0400778 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400779 if (!(nd->flags & LOOKUP_ROOT))
780 nd->root.mnt = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500781 if (unlikely(unlazy_walk(nd)))
Al Viro48a066e2013-09-29 22:06:07 -0400782 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400783 }
784
Al Viro16c2cd72011-02-22 15:50:10 -0500785 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500786 return 0;
787
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500788 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500789 return 0;
790
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500791 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500792 if (status > 0)
793 return 0;
794
Al Viro16c2cd72011-02-22 15:50:10 -0500795 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500796 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500797
Jeff Layton39159de2009-12-07 12:01:50 -0500798 return status;
799}
800
Aleksa Sarai740a1672019-12-07 01:13:29 +1100801static int set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400802{
Al Viro7bd88372014-09-13 21:55:46 -0400803 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100804
Al Viro9e6697e2015-12-05 20:07:21 -0500805 if (nd->flags & LOOKUP_RCU) {
806 unsigned seq;
807
808 do {
809 seq = read_seqcount_begin(&fs->seq);
810 nd->root = fs->root;
811 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
812 } while (read_seqcount_retry(&fs->seq, seq));
813 } else {
814 get_fs_root(fs, &nd->root);
Al Viro84a2bd32019-07-16 21:20:17 -0400815 nd->flags |= LOOKUP_ROOT_GRABBED;
Al Viro9e6697e2015-12-05 20:07:21 -0500816 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100817 return 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100818}
819
Jan Blunck1d957f92008-02-14 19:34:35 -0800820static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700821{
822 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800823 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700824 mntput(path->mnt);
825}
826
Nick Piggin7b9337a2011-01-14 08:42:43 +0000827static inline void path_to_nameidata(const struct path *path,
828 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700829{
Nick Piggin31e6b012011-01-07 17:49:52 +1100830 if (!(nd->flags & LOOKUP_RCU)) {
831 dput(nd->path.dentry);
832 if (nd->path.mnt != path->mnt)
833 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800834 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100835 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800836 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700837}
838
Al Viro248fb5b2015-12-05 20:51:58 -0500839static int nd_jump_root(struct nameidata *nd)
840{
Aleksa Sarai740a1672019-12-07 01:13:29 +1100841 if (!nd->root.mnt) {
842 int error = set_root(nd);
843 if (error)
844 return error;
845 }
Al Viro248fb5b2015-12-05 20:51:58 -0500846 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 */
Aleksa Sarai1bc82072019-12-07 01:13:28 +1100868int nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400869{
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100870 int error = -ELOOP;
Al Viro6e77137b2015-05-02 13:37:52 -0400871 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400872
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100873 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
874 goto err;
875
876 path_put(&nd->path);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400877 nd->path = *path;
878 nd->inode = nd->path.dentry->d_inode;
879 nd->flags |= LOOKUP_JUMPED;
Aleksa Sarai1bc82072019-12-07 01:13:28 +1100880 return 0;
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100881
882err:
883 path_put(path);
884 return error;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400885}
886
Al Virob9ff4422015-05-02 20:19:23 -0400887static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400888{
Al Viro21c30032015-05-03 21:06:24 -0400889 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -0500890 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -0400891 if (!(nd->flags & LOOKUP_RCU))
892 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400893}
894
Linus Torvalds561ec642012-10-26 10:05:07 -0700895int sysctl_protected_symlinks __read_mostly = 0;
896int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -0700897int sysctl_protected_fifos __read_mostly;
898int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -0700899
900/**
901 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700902 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700903 *
904 * In the case of the sysctl_protected_symlinks sysctl being enabled,
905 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
906 * in a sticky world-writable directory. This is to protect privileged
907 * processes from failing races against path names that may change out
908 * from under them by way of other users creating malicious symlinks.
909 * It will permit symlinks to be followed only when outside a sticky
910 * world-writable directory, or when the uid of the symlink and follower
911 * match, or when the directory owner matches the symlink's owner.
912 *
913 * Returns 0 if following the symlink is allowed, -ve on error.
914 */
Al Virofec2fa22015-05-06 15:58:18 -0400915static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700916{
917 const struct inode *inode;
918 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500919 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700920
921 if (!sysctl_protected_symlinks)
922 return 0;
923
924 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500925 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700926 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700927 return 0;
928
929 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400930 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700931 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
932 return 0;
933
934 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500935 puid = parent->i_uid;
936 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700937 return 0;
938
Al Viro319565022015-05-07 20:37:40 -0400939 if (nd->flags & LOOKUP_RCU)
940 return -ECHILD;
941
Richard Guy Briggsea841ba2018-03-21 04:42:21 -0400942 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -0700943 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -0700944 return -EACCES;
945}
946
947/**
948 * safe_hardlink_source - Check for safe hardlink conditions
949 * @inode: the source inode to hardlink from
950 *
951 * Return false if at least one of the following conditions:
952 * - inode is not a regular file
953 * - inode is setuid
954 * - inode is setgid and group-exec
955 * - access failure for read and write
956 *
957 * Otherwise returns true.
958 */
959static bool safe_hardlink_source(struct inode *inode)
960{
961 umode_t mode = inode->i_mode;
962
963 /* Special files should not get pinned to the filesystem. */
964 if (!S_ISREG(mode))
965 return false;
966
967 /* Setuid files should not get pinned to the filesystem. */
968 if (mode & S_ISUID)
969 return false;
970
971 /* Executable setgid files should not get pinned to the filesystem. */
972 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
973 return false;
974
975 /* Hardlinking to unreadable or unwritable sources is dangerous. */
976 if (inode_permission(inode, MAY_READ | MAY_WRITE))
977 return false;
978
979 return true;
980}
981
982/**
983 * may_linkat - Check permissions for creating a hardlink
984 * @link: the source to hardlink from
985 *
986 * Block hardlink when all of:
987 * - sysctl_protected_hardlinks enabled
988 * - fsuid does not match inode
989 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200990 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700991 *
992 * Returns 0 if successful, -ve on error.
993 */
994static int may_linkat(struct path *link)
995{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -0500996 struct inode *inode = link->dentry->d_inode;
997
998 /* Inode writeback is not safe when the uid or gid are invalid. */
999 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
1000 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001001
1002 if (!sysctl_protected_hardlinks)
1003 return 0;
1004
Kees Cook800179c2012-07-25 17:29:07 -07001005 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1006 * otherwise, it must be a safe source.
1007 */
Kees Cookcc658db2017-06-21 09:53:06 -07001008 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
Kees Cook800179c2012-07-25 17:29:07 -07001009 return 0;
1010
Kees Cook245d7362019-10-02 16:41:58 -07001011 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001012 return -EPERM;
1013}
1014
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001015/**
1016 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1017 * should be allowed, or not, on files that already
1018 * exist.
1019 * @dir: the sticky parent directory
1020 * @inode: the inode of the file to open
1021 *
1022 * Block an O_CREAT open of a FIFO (or a regular file) when:
1023 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1024 * - the file already exists
1025 * - we are in a sticky directory
1026 * - we don't own the file
1027 * - the owner of the directory doesn't own the file
1028 * - the directory is world writable
1029 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1030 * the directory doesn't have to be world writable: being group writable will
1031 * be enough.
1032 *
1033 * Returns 0 if the open is allowed, -ve on error.
1034 */
1035static int may_create_in_sticky(struct dentry * const dir,
1036 struct inode * const inode)
1037{
1038 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1039 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
1040 likely(!(dir->d_inode->i_mode & S_ISVTX)) ||
1041 uid_eq(inode->i_uid, dir->d_inode->i_uid) ||
1042 uid_eq(current_fsuid(), inode->i_uid))
1043 return 0;
1044
1045 if (likely(dir->d_inode->i_mode & 0002) ||
1046 (dir->d_inode->i_mode & 0020 &&
1047 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1048 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001049 const char *operation = S_ISFIFO(inode->i_mode) ?
1050 "sticky_create_fifo" :
1051 "sticky_create_regular";
1052 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001053 return -EACCES;
1054 }
1055 return 0;
1056}
1057
Al Viro3b2e7f72015-04-19 00:53:50 -04001058static __always_inline
1059const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001060{
Al Viroab104922015-05-10 11:50:01 -04001061 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001062 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -05001063 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001064 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001065 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001066
Aleksa Sarai27812142019-12-07 01:13:30 +11001067 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS))
1068 return ERR_PTR(-ELOOP);
1069
NeilBrown8fa9dd22015-03-23 13:37:40 +11001070 if (!(nd->flags & LOOKUP_RCU)) {
1071 touch_atime(&last->link);
1072 cond_resched();
Miklos Szeredic6718542018-07-18 15:44:43 +02001073 } else if (atime_needs_update(&last->link, inode)) {
Al Viro4675ac32017-01-09 22:29:15 -05001074 if (unlikely(unlazy_walk(nd)))
NeilBrown8fa9dd22015-03-23 13:37:40 +11001075 return ERR_PTR(-ECHILD);
1076 touch_atime(&last->link);
1077 }
1078
NeilBrownbda0be72015-03-23 13:37:39 +11001079 error = security_inode_follow_link(dentry, inode,
1080 nd->flags & LOOKUP_RCU);
1081 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001082 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001083
Al Viro86acdca12009-12-22 23:45:11 -05001084 nd->last_type = LAST_BIND;
Eric Biggers4c4f7c12019-04-10 13:21:14 -07001085 res = READ_ONCE(inode->i_link);
Al Virod4dee482015-04-30 20:08:02 -04001086 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001087 const char * (*get)(struct dentry *, struct inode *,
1088 struct delayed_call *);
1089 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001090 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001091 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001092 if (res == ERR_PTR(-ECHILD)) {
Al Viro4675ac32017-01-09 22:29:15 -05001093 if (unlikely(unlazy_walk(nd)))
Al Viro6b255392015-11-17 10:20:54 -05001094 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001095 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001096 }
1097 } else {
Al Virofceef392015-12-29 15:58:39 -05001098 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001099 }
Al Virofceef392015-12-29 15:58:39 -05001100 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001101 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001102 }
Al Virofab51e82015-05-10 11:01:00 -04001103 if (*res == '/') {
Aleksa Sarai740a1672019-12-07 01:13:29 +11001104 error = nd_jump_root(nd);
1105 if (unlikely(error))
1106 return ERR_PTR(error);
Al Virofab51e82015-05-10 11:01:00 -04001107 while (unlikely(*++res == '/'))
1108 ;
1109 }
1110 if (!*res)
1111 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001112 return res;
1113}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001114
David Howellsf015f1262012-06-25 12:55:28 +01001115/*
1116 * follow_up - Find the mountpoint of path's vfsmount
1117 *
1118 * Given a path, find the mountpoint of its source file system.
1119 * Replace @path with the path of the mountpoint in the parent mount.
1120 * Up is towards /.
1121 *
1122 * Return 1 if we went up a level and 0 if we were already at the
1123 * root.
1124 */
Al Virobab77eb2009-04-18 03:26:48 -04001125int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Al Viro0714a532011-11-24 22:19:58 -05001127 struct mount *mnt = real_mount(path->mnt);
1128 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001130
Al Viro48a066e2013-09-29 22:06:07 -04001131 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001132 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001133 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001134 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return 0;
1136 }
Al Viro0714a532011-11-24 22:19:58 -05001137 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001138 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001139 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001140 dput(path->dentry);
1141 path->dentry = mountpoint;
1142 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001143 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 return 1;
1145}
Al Viro4d359502014-03-14 12:20:17 -04001146EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001148/*
David Howells9875cf82011-01-14 18:45:21 +00001149 * Perform an automount
1150 * - return -EISDIR to tell follow_managed() to stop and return the path we
1151 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
NeilBrown756daf22015-03-23 13:37:38 +11001153static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001154 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001155{
David Howells9875cf82011-01-14 18:45:21 +00001156 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001157 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001158
David Howells9875cf82011-01-14 18:45:21 +00001159 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1160 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001161
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001162 /* We don't want to mount if someone's just doing a stat -
1163 * unless they're stat'ing a directory and appended a '/' to
1164 * the name.
1165 *
1166 * We do, however, want to mount if someone wants to open or
1167 * create a file of any type under the mountpoint, wants to
1168 * traverse through the mountpoint or wants to open the
1169 * mounted directory. Also, autofs may mark negative dentries
1170 * as being automount points. These will need the attentions
1171 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001172 */
NeilBrown756daf22015-03-23 13:37:38 +11001173 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001174 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
1175 path->dentry->d_inode)
1176 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001177
NeilBrown756daf22015-03-23 13:37:38 +11001178 nd->total_link_count++;
1179 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001180 return -ELOOP;
1181
1182 mnt = path->dentry->d_op->d_automount(path);
1183 if (IS_ERR(mnt)) {
1184 /*
1185 * The filesystem is allowed to return -EISDIR here to indicate
1186 * it doesn't want to automount. For instance, autofs would do
1187 * this so that its userspace daemon can mount on this dentry.
1188 *
1189 * However, we can only permit this if it's a terminal point in
1190 * the path being looked up; if it wasn't then the remainder of
1191 * the path is inaccessible and we should say so.
1192 */
NeilBrown756daf22015-03-23 13:37:38 +11001193 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001194 return -EREMOTE;
1195 return PTR_ERR(mnt);
1196 }
David Howellsea5b7782011-01-14 19:10:03 +00001197
David Howells9875cf82011-01-14 18:45:21 +00001198 if (!mnt) /* mount collision */
1199 return 0;
1200
Al Viro8aef1882011-06-16 15:10:06 +01001201 if (!*need_mntput) {
1202 /* lock_mount() may release path->mnt on error */
1203 mntget(path->mnt);
1204 *need_mntput = true;
1205 }
Al Viro19a167a2011-01-17 01:35:23 -05001206 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001207
David Howellsea5b7782011-01-14 19:10:03 +00001208 switch (err) {
1209 case -EBUSY:
1210 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001211 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001212 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001213 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001214 path->mnt = mnt;
1215 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001216 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001217 default:
1218 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001219 }
Al Viro19a167a2011-01-17 01:35:23 -05001220
David Howells9875cf82011-01-14 18:45:21 +00001221}
1222
1223/*
1224 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001225 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001226 * - Flagged as mountpoint
1227 * - Flagged as automount point
1228 *
1229 * This may only be called in refwalk mode.
Al Virod41efb52019-11-04 22:30:52 -05001230 * On success path->dentry is known positive.
David Howells9875cf82011-01-14 18:45:21 +00001231 *
1232 * Serialization is taken care of in namespace.c
1233 */
NeilBrown756daf22015-03-23 13:37:38 +11001234static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001235{
Al Viro8aef1882011-06-16 15:10:06 +01001236 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
Al Virod41efb52019-11-04 22:30:52 -05001237 unsigned flags;
David Howells9875cf82011-01-14 18:45:21 +00001238 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001239 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001240
1241 /* Given that we're not holding a lock here, we retain the value in a
1242 * local variable for each dentry as we look at it so that we don't see
1243 * the components of that value change under us */
Al Viro2fa6b1e2019-11-12 16:13:06 -05001244 while (flags = smp_load_acquire(&path->dentry->d_flags),
Al Virod41efb52019-11-04 22:30:52 -05001245 unlikely(flags & DCACHE_MANAGED_DENTRY)) {
David Howellscc53ce52011-01-14 18:45:26 +00001246 /* Allow the filesystem to manage the transit without i_mutex
1247 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001248 if (flags & DCACHE_MANAGE_TRANSIT) {
David Howellscc53ce52011-01-14 18:45:26 +00001249 BUG_ON(!path->dentry->d_op);
1250 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001251 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001252 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001253 break;
David Howellscc53ce52011-01-14 18:45:26 +00001254 }
1255
David Howells9875cf82011-01-14 18:45:21 +00001256 /* Transit to a mounted filesystem. */
Al Virod41efb52019-11-04 22:30:52 -05001257 if (flags & DCACHE_MOUNTED) {
David Howells9875cf82011-01-14 18:45:21 +00001258 struct vfsmount *mounted = lookup_mnt(path);
1259 if (mounted) {
1260 dput(path->dentry);
1261 if (need_mntput)
1262 mntput(path->mnt);
1263 path->mnt = mounted;
1264 path->dentry = dget(mounted->mnt_root);
1265 need_mntput = true;
1266 continue;
1267 }
1268
1269 /* Something is mounted on this dentry in another
1270 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001271 * namespace got unmounted before lookup_mnt() could
1272 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001273 }
1274
1275 /* Handle an automount point */
Al Virod41efb52019-11-04 22:30:52 -05001276 if (flags & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001277 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001278 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001279 break;
David Howells9875cf82011-01-14 18:45:21 +00001280 continue;
1281 }
1282
1283 /* We didn't change the current path point */
1284 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Al Viro8aef1882011-06-16 15:10:06 +01001286
1287 if (need_mntput && path->mnt == mnt)
1288 mntput(path->mnt);
Al Viro84027522015-04-22 10:30:08 -04001289 if (need_mntput)
1290 nd->flags |= LOOKUP_JUMPED;
Al Virod41efb52019-11-04 22:30:52 -05001291 if (ret == -EISDIR || !ret)
1292 ret = 1;
1293 if (ret > 0 && unlikely(d_flags_negative(flags)))
1294 ret = -ENOENT;
Al Viro84027522015-04-22 10:30:08 -04001295 if (unlikely(ret < 0))
1296 path_put_conditional(path, nd);
1297 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
David Howellscc53ce52011-01-14 18:45:26 +00001300int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
1302 struct vfsmount *mounted;
1303
Al Viro1c755af2009-04-18 14:06:57 -04001304 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001306 dput(path->dentry);
1307 mntput(path->mnt);
1308 path->mnt = mounted;
1309 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 1;
1311 }
1312 return 0;
1313}
Al Viro4d359502014-03-14 12:20:17 -04001314EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Ian Kentfb5f51c2016-11-24 08:03:41 +11001316static inline int managed_dentry_rcu(const struct path *path)
Ian Kent62a73752011-03-25 01:51:02 +08001317{
Ian Kentfb5f51c2016-11-24 08:03:41 +11001318 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1319 path->dentry->d_op->d_manage(path, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001320}
1321
David Howells9875cf82011-01-14 18:45:21 +00001322/*
Al Viro287548e2011-05-27 06:50:06 -04001323 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1324 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001325 */
1326static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001327 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001328{
Ian Kent62a73752011-03-25 01:51:02 +08001329 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001330 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001331 /*
1332 * Don't forget we might have a non-mountpoint managed dentry
1333 * that wants to block transit.
1334 */
Ian Kentfb5f51c2016-11-24 08:03:41 +11001335 switch (managed_dentry_rcu(path)) {
NeilBrownb8faf032014-08-04 17:06:29 +10001336 case -ECHILD:
1337 default:
David Howellsab909112011-01-14 18:46:51 +00001338 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001339 case -EISDIR:
1340 return true;
1341 case 0:
1342 break;
1343 }
Ian Kent62a73752011-03-25 01:51:02 +08001344
1345 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001346 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001347
Al Viro474279d2013-10-01 16:11:26 -04001348 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001349 if (!mounted)
1350 break;
Al Viroc7105362011-11-24 18:22:03 -05001351 path->mnt = &mounted->mnt;
1352 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001353 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001354 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001355 /*
1356 * Update the inode too. We don't need to re-check the
1357 * dentry sequence number here after this d_inode read,
1358 * because a mount-point is always pinned.
1359 */
1360 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001361 }
Al Virof5be3e29122014-09-13 21:50:45 -04001362 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001363 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001364}
1365
Nick Piggin31e6b012011-01-07 17:49:52 +11001366static int follow_dotdot_rcu(struct nameidata *nd)
1367{
Al Viro4023bfc2014-09-13 21:59:43 -04001368 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001369
David Howells9875cf82011-01-14 18:45:21 +00001370 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001371 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001372 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001373 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1374 struct dentry *old = nd->path.dentry;
1375 struct dentry *parent = old->d_parent;
1376 unsigned seq;
1377
Al Viro4023bfc2014-09-13 21:59:43 -04001378 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001379 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001380 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1381 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001382 nd->path.dentry = parent;
1383 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001384 if (unlikely(!path_connected(&nd->path)))
Aleksa Sarai2b981492019-12-07 01:13:26 +11001385 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001386 break;
Al Viroaed434a2015-05-12 12:22:47 -04001387 } else {
1388 struct mount *mnt = real_mount(nd->path.mnt);
1389 struct mount *mparent = mnt->mnt_parent;
1390 struct dentry *mountpoint = mnt->mnt_mountpoint;
1391 struct inode *inode2 = mountpoint->d_inode;
1392 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1393 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1394 return -ECHILD;
1395 if (&mparent->mnt == nd->path.mnt)
1396 break;
1397 /* we know that mountpoint was pinned */
1398 nd->path.dentry = mountpoint;
1399 nd->path.mnt = &mparent->mnt;
1400 inode = inode2;
1401 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001402 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001403 }
Al Viroaed434a2015-05-12 12:22:47 -04001404 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001405 struct mount *mounted;
1406 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001407 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1408 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001409 if (!mounted)
1410 break;
1411 nd->path.mnt = &mounted->mnt;
1412 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001413 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001414 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001415 }
Al Viro4023bfc2014-09-13 21:59:43 -04001416 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001417 return 0;
1418}
1419
David Howells9875cf82011-01-14 18:45:21 +00001420/*
David Howellscc53ce52011-01-14 18:45:26 +00001421 * Follow down to the covering mount currently visible to userspace. At each
1422 * point, the filesystem owning that dentry may be queried as to whether the
1423 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001424 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001425int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001426{
1427 unsigned managed;
1428 int ret;
1429
Mark Rutland6aa7de02017-10-23 14:07:29 -07001430 while (managed = READ_ONCE(path->dentry->d_flags),
David Howellscc53ce52011-01-14 18:45:26 +00001431 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1432 /* Allow the filesystem to manage the transit without i_mutex
1433 * being held.
1434 *
1435 * We indicate to the filesystem if someone is trying to mount
1436 * something here. This gives autofs the chance to deny anyone
1437 * other than its daemon the right to mount on its
1438 * superstructure.
1439 *
1440 * The filesystem may sleep at this point.
1441 */
1442 if (managed & DCACHE_MANAGE_TRANSIT) {
1443 BUG_ON(!path->dentry->d_op);
1444 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001445 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001446 if (ret < 0)
1447 return ret == -EISDIR ? 0 : ret;
1448 }
1449
1450 /* Transit to a mounted filesystem. */
1451 if (managed & DCACHE_MOUNTED) {
1452 struct vfsmount *mounted = lookup_mnt(path);
1453 if (!mounted)
1454 break;
1455 dput(path->dentry);
1456 mntput(path->mnt);
1457 path->mnt = mounted;
1458 path->dentry = dget(mounted->mnt_root);
1459 continue;
1460 }
1461
1462 /* Don't handle automount points here */
1463 break;
1464 }
1465 return 0;
1466}
Al Viro4d359502014-03-14 12:20:17 -04001467EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001468
1469/*
David Howells9875cf82011-01-14 18:45:21 +00001470 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1471 */
1472static void follow_mount(struct path *path)
1473{
1474 while (d_mountpoint(path->dentry)) {
1475 struct vfsmount *mounted = lookup_mnt(path);
1476 if (!mounted)
1477 break;
1478 dput(path->dentry);
1479 mntput(path->mnt);
1480 path->mnt = mounted;
1481 path->dentry = dget(mounted->mnt_root);
1482 }
1483}
1484
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001485static int path_parent_directory(struct path *path)
1486{
1487 struct dentry *old = path->dentry;
1488 /* rare case of legitimate dget_parent()... */
1489 path->dentry = dget_parent(path->dentry);
1490 dput(old);
1491 if (unlikely(!path_connected(path)))
1492 return -ENOENT;
1493 return 0;
1494}
1495
Eric W. Biederman397d4252015-08-15 20:27:13 -05001496static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497{
1498 while(1) {
Danilo Krummrich030c7e02018-04-23 10:30:59 +02001499 if (path_equal(&nd->path, &nd->root))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 break;
Jan Blunck4ac91372008-02-14 19:34:32 -08001501 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001502 int ret = path_parent_directory(&nd->path);
1503 if (ret)
1504 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 break;
1506 }
Al Viro3088dd72010-01-30 15:47:29 -05001507 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 }
Al Viro79ed0222009-04-18 13:59:41 -04001510 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001511 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001512 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513}
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001516 * This looks up the name in dcache and possibly revalidates the found dentry.
1517 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001518 */
Al Viroe3c13922016-03-06 14:03:27 -05001519static struct dentry *lookup_dcache(const struct qstr *name,
1520 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001521 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001522{
Al Viroa89f8332017-01-09 22:25:28 -05001523 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001524 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001525 int error = d_revalidate(dentry, flags);
1526 if (unlikely(error <= 0)) {
1527 if (!error)
1528 d_invalidate(dentry);
1529 dput(dentry);
1530 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001531 }
1532 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001533 return dentry;
1534}
1535
1536/*
Al Viroa03ece52018-03-08 11:00:45 -05001537 * Parent directory has inode locked exclusive. This is one
1538 * and only case when ->lookup() gets called on non in-lookup
1539 * dentries - as the matter of fact, this only gets called
1540 * when directory is guaranteed to have no in-lookup children
1541 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001542 */
Al Viroa03ece52018-03-08 11:00:45 -05001543static struct dentry *__lookup_hash(const struct qstr *name,
1544 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001545{
Al Viroa03ece52018-03-08 11:00:45 -05001546 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001547 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001548 struct inode *dir = base->d_inode;
1549
1550 if (dentry)
1551 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001552
1553 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001554 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001555 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001556
1557 dentry = d_alloc(base, name);
1558 if (unlikely(!dentry))
1559 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001560
Al Viro72bd8662012-06-10 17:17:17 -04001561 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001562 if (unlikely(old)) {
1563 dput(dentry);
1564 dentry = old;
1565 }
1566 return dentry;
1567}
1568
Al Viroe97cdc82013-01-24 18:16:00 -05001569static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001570 struct path *path, struct inode **inode,
1571 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572{
Jan Blunck4ac91372008-02-14 19:34:32 -08001573 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001574 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001575 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001576 int err;
1577
Al Viro3cac2602009-08-13 18:27:43 +04001578 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001579 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001580 * of a false negative due to a concurrent rename, the caller is
1581 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001582 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001583 if (nd->flags & LOOKUP_RCU) {
1584 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001585 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001586 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001587 if (unlikely(!dentry)) {
Al Viro4675ac32017-01-09 22:29:15 -05001588 if (unlazy_walk(nd))
Al Viro5d0f49c2016-03-05 21:32:53 -05001589 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001590 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001591 }
Al Viro5a18fff2011-03-11 04:44:53 -05001592
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001593 /*
1594 * This sequence count validates that the inode matches
1595 * the dentry name information from lookup.
1596 */
David Howells63afdfc2015-05-06 15:59:00 +01001597 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001598 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001599 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001600 return -ECHILD;
1601
1602 /*
1603 * This sequence count validates that the parent had no
1604 * changes while we did the lookup of the dentry above.
1605 *
1606 * The memory barrier in read_seqcount_begin of child is
1607 * enough, we can use __read_seqcount_retry here.
1608 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001609 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001610 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001611
Al Viro254cf582015-05-05 09:40:46 -04001612 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001613 status = d_revalidate(dentry, nd->flags);
Al Viro209a7fb2017-01-09 01:35:39 -05001614 if (likely(status > 0)) {
Al Viro5d0f49c2016-03-05 21:32:53 -05001615 /*
1616 * Note: do negative dentry check after revalidation in
1617 * case that drops it.
1618 */
1619 if (unlikely(negative))
1620 return -ENOENT;
1621 path->mnt = mnt;
1622 path->dentry = dentry;
1623 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001624 return 1;
Al Viro24643082011-02-15 01:26:22 -05001625 }
Al Viro4675ac32017-01-09 22:29:15 -05001626 if (unlazy_child(nd, dentry, seq))
Al Viro209a7fb2017-01-09 01:35:39 -05001627 return -ECHILD;
1628 if (unlikely(status == -ECHILD))
1629 /* we'd been told to redo it in non-rcu mode */
1630 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001631 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001632 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001633 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001634 return 0;
Al Viroa89f8332017-01-09 22:25:28 -05001635 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001636 }
Al Viro5a18fff2011-03-11 04:44:53 -05001637 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001638 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001639 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001640 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001641 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001642 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001643
David Howells9875cf82011-01-14 18:45:21 +00001644 path->mnt = mnt;
1645 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001646 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001647 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001648 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001649 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001650}
1651
1652/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001653static struct dentry *__lookup_slow(const struct qstr *name,
1654 struct dentry *dir,
1655 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001656{
Al Viro88d83312018-04-06 16:43:47 -04001657 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001658 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001659 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001660
Al Viro19363862016-04-14 19:33:34 -04001661 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001662 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001663 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001664again:
Al Virod9171b92016-04-15 03:33:13 -04001665 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001666 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001667 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001668 if (unlikely(!d_in_lookup(dentry))) {
Al Viroa89f8332017-01-09 22:25:28 -05001669 if (!(flags & LOOKUP_NO_REVAL)) {
Al Viro949a8522016-03-06 14:20:52 -05001670 int error = d_revalidate(dentry, flags);
1671 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001672 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001673 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001674 dput(dentry);
1675 goto again;
1676 }
Al Viro949a8522016-03-06 14:20:52 -05001677 dput(dentry);
1678 dentry = ERR_PTR(error);
1679 }
1680 }
Al Viro94bdd652016-04-15 02:42:04 -04001681 } else {
1682 old = inode->i_op->lookup(inode, dentry, flags);
1683 d_lookup_done(dentry);
1684 if (unlikely(old)) {
1685 dput(dentry);
1686 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001687 }
1688 }
Al Viroe3c13922016-03-06 14:03:27 -05001689 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690}
1691
Al Viro88d83312018-04-06 16:43:47 -04001692static struct dentry *lookup_slow(const struct qstr *name,
1693 struct dentry *dir,
1694 unsigned int flags)
1695{
1696 struct inode *inode = dir->d_inode;
1697 struct dentry *res;
1698 inode_lock_shared(inode);
1699 res = __lookup_slow(name, dir, flags);
1700 inode_unlock_shared(inode);
1701 return res;
1702}
1703
Al Viro52094c82011-02-21 21:34:47 -05001704static inline int may_lookup(struct nameidata *nd)
1705{
1706 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001707 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001708 if (err != -ECHILD)
1709 return err;
Al Viro4675ac32017-01-09 22:29:15 -05001710 if (unlazy_walk(nd))
Al Viro52094c82011-02-21 21:34:47 -05001711 return -ECHILD;
1712 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001713 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001714}
1715
Al Viro9856fa12011-03-04 14:22:06 -05001716static inline int handle_dots(struct nameidata *nd, int type)
1717{
1718 if (type == LAST_DOTDOT) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11001719 int error = 0;
1720
1721 if (!nd->root.mnt) {
1722 error = set_root(nd);
1723 if (error)
1724 return error;
1725 }
Al Viro9856fa12011-03-04 14:22:06 -05001726 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001727 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001728 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001729 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001730 }
1731 return 0;
1732}
1733
Al Viro181548c2015-05-07 19:54:34 -04001734static int pick_link(struct nameidata *nd, struct path *link,
1735 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001736{
Al Viro626de992015-05-04 18:26:59 -04001737 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001738 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001739 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001740 path_to_nameidata(link, nd);
1741 return -ELOOP;
1742 }
Al Virobc40aee2015-05-09 13:04:24 -04001743 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001744 if (link->mnt == nd->path.mnt)
1745 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001746 }
Al Viro626de992015-05-04 18:26:59 -04001747 error = nd_alloc_stack(nd);
1748 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001749 if (error == -ECHILD) {
Al Viroad1633a2017-01-08 22:35:31 -05001750 if (unlikely(!legitimize_path(nd, link, seq))) {
1751 drop_links(nd);
1752 nd->depth = 0;
1753 nd->flags &= ~LOOKUP_RCU;
1754 nd->path.mnt = NULL;
1755 nd->path.dentry = NULL;
Al Viroad1633a2017-01-08 22:35:31 -05001756 rcu_read_unlock();
Al Viro4675ac32017-01-09 22:29:15 -05001757 } else if (likely(unlazy_walk(nd)) == 0)
Al Viroad1633a2017-01-08 22:35:31 -05001758 error = nd_alloc_stack(nd);
Al Virobc40aee2015-05-09 13:04:24 -04001759 }
1760 if (error) {
1761 path_put(link);
1762 return error;
1763 }
Al Viro626de992015-05-04 18:26:59 -04001764 }
1765
Al Viroab104922015-05-10 11:50:01 -04001766 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001767 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001768 clear_delayed_call(&last->done);
1769 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001770 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001771 return 1;
1772}
1773
Al Viro8f64fb12016-11-14 01:50:26 -05001774enum {WALK_FOLLOW = 1, WALK_MORE = 2};
Al Viro31d66bc2016-11-14 01:43:34 -05001775
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001776/*
1777 * Do we need to follow links? We _really_ want to be able
1778 * to do this check without having to look at inode->i_op,
1779 * so we keep a cache of "no, this doesn't need follow_link"
1780 * for the common case.
1781 */
Al Viro8f64fb12016-11-14 01:50:26 -05001782static inline int step_into(struct nameidata *nd, struct path *path,
1783 int flags, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001784{
Al Viro31d66bc2016-11-14 01:43:34 -05001785 if (!(flags & WALK_MORE) && nd->depth)
1786 put_link(nd);
Al Viro8f64fb12016-11-14 01:50:26 -05001787 if (likely(!d_is_symlink(path->dentry)) ||
1788 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1789 /* not a symlink or should not follow */
1790 path_to_nameidata(path, nd);
1791 nd->inode = inode;
1792 nd->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001793 return 0;
Al Viro8f64fb12016-11-14 01:50:26 -05001794 }
Al Viroa7f77542016-02-27 19:31:01 -05001795 /* make sure that d_is_symlink above matches inode */
1796 if (nd->flags & LOOKUP_RCU) {
Al Viro8f64fb12016-11-14 01:50:26 -05001797 if (read_seqcount_retry(&path->dentry->d_seq, seq))
Al Viroa7f77542016-02-27 19:31:01 -05001798 return -ECHILD;
1799 }
Al Viro8f64fb12016-11-14 01:50:26 -05001800 return pick_link(nd, path, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001801}
1802
Al Viro4693a542015-05-04 17:47:11 -04001803static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001804{
Al Virocaa856342015-04-22 17:52:47 -04001805 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001806 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001807 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001808 int err;
1809 /*
1810 * "." and ".." are special - ".." especially so because it has
1811 * to be able to know about the current root directory and
1812 * parent relationships.
1813 */
Al Viro4693a542015-05-04 17:47:11 -04001814 if (unlikely(nd->last_type != LAST_NORM)) {
1815 err = handle_dots(nd, nd->last_type);
Al Viro1c4ff1a2016-11-14 01:39:36 -05001816 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001817 put_link(nd);
1818 return err;
1819 }
Al Viro254cf582015-05-05 09:40:46 -04001820 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001821 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001822 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001823 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001824 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1825 nd->flags);
1826 if (IS_ERR(path.dentry))
1827 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001828
Al Viroe3c13922016-03-06 14:03:27 -05001829 path.mnt = nd->path.mnt;
1830 err = follow_managed(&path, nd);
1831 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001832 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001833
Al Viro254cf582015-05-05 09:40:46 -04001834 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001835 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001836 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001837
Al Viro8f64fb12016-11-14 01:50:26 -05001838 return step_into(nd, &path, flags, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001839}
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001842 * We can do the critical dentry name comparison and hashing
1843 * operations one word at a time, but we are limited to:
1844 *
1845 * - Architectures with fast unaligned word accesses. We could
1846 * do a "get_unaligned()" if this helps and is sufficiently
1847 * fast.
1848 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001849 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1850 * do not trap on the (extremely unlikely) case of a page
1851 * crossing operation.
1852 *
1853 * - Furthermore, we need an efficient 64-bit compile for the
1854 * 64-bit case in order to generate the "number of bytes in
1855 * the final mask". Again, that could be replaced with a
1856 * efficient population count instruction or similar.
1857 */
1858#ifdef CONFIG_DCACHE_WORD_ACCESS
1859
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001860#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001861
George Spelvin468a9422016-05-26 22:11:51 -04001862#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001863
George Spelvin468a9422016-05-26 22:11:51 -04001864/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1865
1866#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001867/*
1868 * Register pressure in the mixing function is an issue, particularly
1869 * on 32-bit x86, but almost any function requires one state value and
1870 * one temporary. Instead, use a function designed for two state values
1871 * and no temporaries.
1872 *
1873 * This function cannot create a collision in only two iterations, so
1874 * we have two iterations to achieve avalanche. In those two iterations,
1875 * we have six layers of mixing, which is enough to spread one bit's
1876 * influence out to 2^6 = 64 state bits.
1877 *
1878 * Rotate constants are scored by considering either 64 one-bit input
1879 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1880 * probability of that delta causing a change to each of the 128 output
1881 * bits, using a sample of random initial states.
1882 *
1883 * The Shannon entropy of the computed probabilities is then summed
1884 * to produce a score. Ideally, any input change has a 50% chance of
1885 * toggling any given output bit.
1886 *
1887 * Mixing scores (in bits) for (12,45):
1888 * Input delta: 1-bit 2-bit
1889 * 1 round: 713.3 42542.6
1890 * 2 rounds: 2753.7 140389.8
1891 * 3 rounds: 5954.1 233458.2
1892 * 4 rounds: 7862.6 256672.2
1893 * Perfect: 8192 258048
1894 * (64*128) (64*63/2 * 128)
1895 */
1896#define HASH_MIX(x, y, a) \
1897 ( x ^= (a), \
1898 y ^= x, x = rol64(x,12),\
1899 x += y, y = rol64(y,45),\
1900 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001901
George Spelvin0fed3ac2016-05-02 06:31:01 -04001902/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001903 * Fold two longs into one 32-bit hash value. This must be fast, but
1904 * latency isn't quite as critical, as there is a fair bit of additional
1905 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001906 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001907static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001908{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001909 y ^= x * GOLDEN_RATIO_64;
1910 y *= GOLDEN_RATIO_64;
1911 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001912}
1913
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001914#else /* 32-bit case */
1915
George Spelvin2a18da7a2016-05-23 07:43:58 -04001916/*
1917 * Mixing scores (in bits) for (7,20):
1918 * Input delta: 1-bit 2-bit
1919 * 1 round: 330.3 9201.6
1920 * 2 rounds: 1246.4 25475.4
1921 * 3 rounds: 1907.1 31295.1
1922 * 4 rounds: 2042.3 31718.6
1923 * Perfect: 2048 31744
1924 * (32*64) (32*31/2 * 64)
1925 */
1926#define HASH_MIX(x, y, a) \
1927 ( x ^= (a), \
1928 y ^= x, x = rol32(x, 7),\
1929 x += y, y = rol32(y,20),\
1930 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001931
George Spelvin2a18da7a2016-05-23 07:43:58 -04001932static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001933{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001934 /* Use arch-optimized multiply if one exists */
1935 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001936}
1937
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001938#endif
1939
George Spelvin2a18da7a2016-05-23 07:43:58 -04001940/*
1941 * Return the hash of a string of known length. This is carfully
1942 * designed to match hash_name(), which is the more critical function.
1943 * In particular, we must end by hashing a final word containing 0..7
1944 * payload bytes, to match the way that hash_name() iterates until it
1945 * finds the delimiter after the name.
1946 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001947unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001948{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001949 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001950
1951 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001952 if (!len)
1953 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001954 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001955 if (len < sizeof(unsigned long))
1956 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001957 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001958 name += sizeof(unsigned long);
1959 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001960 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001961 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001962done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001963 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001964}
1965EXPORT_SYMBOL(full_name_hash);
1966
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001967/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001968u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001969{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001970 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1971 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001972 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1973
Linus Torvalds8387ff22016-06-10 07:51:30 -07001974 len = 0;
1975 goto inside;
1976
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001977 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001978 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001979 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001980inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001981 a = load_unaligned_zeropad(name+len);
1982 } while (!has_zero(a, &adata, &constants));
1983
1984 adata = prep_zero_mask(a, adata, &constants);
1985 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001986 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001987
George Spelvin2a18da7a2016-05-23 07:43:58 -04001988 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001989}
1990EXPORT_SYMBOL(hashlen_string);
1991
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001992/*
1993 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001994 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001995 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001996static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001997{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001998 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1999 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002000 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002001
Linus Torvalds8387ff22016-06-10 07:51:30 -07002002 len = 0;
2003 goto inside;
2004
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002005 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002006 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002007 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002008inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002009 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002010 b = a ^ REPEAT_BYTE('/');
2011 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002012
Linus Torvalds36126f82012-05-26 10:43:17 -07002013 adata = prep_zero_mask(a, adata, &constants);
2014 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002015 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002016 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002017
George Spelvin2a18da7a2016-05-23 07:43:58 -04002018 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002019}
2020
George Spelvin2a18da7a2016-05-23 07:43:58 -04002021#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002022
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002023/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002024unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002025{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002026 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002027 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002028 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002029 return end_name_hash(hash);
2030}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002031EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002032
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002033/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002034u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002035{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002036 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002037 unsigned long len = 0, c;
2038
2039 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002040 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002041 len++;
2042 hash = partial_name_hash(c, hash);
2043 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002044 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002045 return hashlen_create(end_name_hash(hash), len);
2046}
George Spelvinf2a031b2016-05-29 01:26:41 -04002047EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002048
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002049/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002050 * We know there's a real path component here of at least
2051 * one character.
2052 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002053static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002054{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002055 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002056 unsigned long len = 0, c;
2057
2058 c = (unsigned char)*name;
2059 do {
2060 len++;
2061 hash = partial_name_hash(c, hash);
2062 c = (unsigned char)name[len];
2063 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002064 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002065}
2066
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002067#endif
2068
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002069/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002071 * This is the basic name resolution function, turning a pathname into
2072 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002074 * Returns 0 and nd will have valid dentry and mnt on success.
2075 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Al Viro6de88d72009-08-09 01:41:57 +04002077static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002080
Al Viro9b5858e2018-07-09 16:33:23 -04002081 if (IS_ERR(name))
2082 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 while (*name=='/')
2084 name++;
2085 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002086 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* At this point we know we have a real path component. */
2089 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002090 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002091 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Al Viro52094c82011-02-21 21:34:47 -05002093 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002094 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002095 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Linus Torvalds8387ff22016-06-10 07:51:30 -07002097 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Al Virofe479a52011-02-22 15:10:03 -05002099 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002100 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002101 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002102 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002103 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002104 nd->flags |= LOOKUP_JUMPED;
2105 }
Al Virofe479a52011-02-22 15:10:03 -05002106 break;
2107 case 1:
2108 type = LAST_DOT;
2109 }
Al Viro5a202bc2011-03-08 14:17:44 -05002110 if (likely(type == LAST_NORM)) {
2111 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002112 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002113 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002114 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002115 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002116 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002117 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002118 hash_len = this.hash_len;
2119 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002120 }
2121 }
Al Virofe479a52011-02-22 15:10:03 -05002122
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002123 nd->last.hash_len = hash_len;
2124 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002125 nd->last_type = type;
2126
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002127 name += hashlen_len(hash_len);
2128 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002129 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002130 /*
2131 * If it wasn't NUL, we know it was '/'. Skip that
2132 * slash, and continue until no more slashes.
2133 */
2134 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002135 name++;
2136 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002137 if (unlikely(!*name)) {
2138OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002139 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002140 if (!nd->depth)
2141 return 0;
2142 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002143 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002144 if (!name)
2145 return 0;
2146 /* last component of nested symlink */
Al Viro8f64fb12016-11-14 01:50:26 -05002147 err = walk_component(nd, WALK_FOLLOW);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002148 } else {
2149 /* not the last component */
Al Viro8f64fb12016-11-14 01:50:26 -05002150 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002151 }
Al Viroce57dfc2011-03-13 19:58:58 -04002152 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002153 return err;
Al Virofe479a52011-02-22 15:10:03 -05002154
Al Viroce57dfc2011-03-13 19:58:58 -04002155 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002156 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002157
Viresh Kumara1c83682015-08-12 15:59:44 +05302158 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002159 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002160 err = 0;
2161 if (unlikely(!s)) {
2162 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002163 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002164 } else {
Al Virofab51e82015-05-10 11:01:00 -04002165 nd->stack[nd->depth - 1].name = name;
2166 name = s;
2167 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002168 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002169 }
Al Viro97242f92015-08-01 19:59:28 -04002170 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2171 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002172 if (unlazy_walk(nd))
Al Viro97242f92015-08-01 19:59:28 -04002173 return -ECHILD;
2174 }
Al Viro3595e232015-05-09 16:54:45 -04002175 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
Al Viroedc2b1d2018-07-09 16:27:23 -04002180/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002181static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002183 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002184 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002186 if (!*s)
2187 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002188 if (flags & LOOKUP_RCU)
2189 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002192 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002194 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002195 struct dentry *root = nd->root.dentry;
2196 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002197 if (*s && unlikely(!d_can_lookup(root)))
2198 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002199 nd->path = nd->root;
2200 nd->inode = inode;
2201 if (flags & LOOKUP_RCU) {
Al Viro5b6ca022011-03-09 23:04:47 -05002202 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002203 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002204 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002205 } else {
2206 path_get(&nd->path);
2207 }
Al Viro368ee9b2015-05-08 17:19:59 -04002208 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002209 }
2210
Al Viro2a737872009-04-07 11:49:53 -04002211 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002212 nd->path.mnt = NULL;
2213 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Al Viro48a066e2013-09-29 22:06:07 -04002215 nd->m_seq = read_seqbegin(&mount_lock);
Aleksa Sarai740a1672019-12-07 01:13:29 +11002216
2217 /* Figure out the starting path and root (if needed). */
Al Virofd2f7cb2015-02-22 20:07:13 -05002218 if (*s == '/') {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002219 error = nd_jump_root(nd);
2220 if (unlikely(error))
2221 return ERR_PTR(error);
2222 return s;
Al Viroc8a53ee2015-05-12 18:43:07 -04002223 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002224 if (flags & LOOKUP_RCU) {
2225 struct fs_struct *fs = current->fs;
2226 unsigned seq;
2227
Al Viroe41f7d42011-02-22 14:02:58 -05002228 do {
2229 seq = read_seqcount_begin(&fs->seq);
2230 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002231 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002232 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2233 } while (read_seqcount_retry(&fs->seq, seq));
2234 } else {
2235 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002236 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002237 }
Al Viroef55d912015-12-05 20:25:06 -05002238 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002239 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002240 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002241 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002242 struct dentry *dentry;
2243
Al Viro2903ff02012-08-28 12:52:22 -04002244 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002245 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002246
Al Viro2903ff02012-08-28 12:52:22 -04002247 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002248
Al Viroedc2b1d2018-07-09 16:27:23 -04002249 if (*s && unlikely(!d_can_lookup(dentry))) {
2250 fdput(f);
2251 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002252 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002253
Al Viro2903ff02012-08-28 12:52:22 -04002254 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002255 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002256 nd->inode = nd->path.dentry->d_inode;
2257 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002258 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002259 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002260 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002261 }
Al Viro34a26b92015-05-11 08:05:05 -04002262 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002263 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002265}
2266
Al Viro3bdba282015-05-08 17:37:07 -04002267static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002268{
2269 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002270 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002271 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002272 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002273 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002274 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002275 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002276 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002277}
2278
Al Virocaa856342015-04-22 17:52:47 -04002279static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002280{
2281 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2282 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2283
2284 nd->flags &= ~LOOKUP_PARENT;
Al Viro1c4ff1a2016-11-14 01:39:36 -05002285 return walk_component(nd, 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002286}
2287
Al Viro4f757f32017-04-15 17:31:22 -04002288static int handle_lookup_down(struct nameidata *nd)
2289{
2290 struct path path = nd->path;
2291 struct inode *inode = nd->inode;
2292 unsigned seq = nd->seq;
2293 int err;
2294
2295 if (nd->flags & LOOKUP_RCU) {
2296 /*
2297 * don't bother with unlazy_walk on failure - we are
2298 * at the very beginning of walk, so we lose nothing
2299 * if we simply redo everything in non-RCU mode
2300 */
2301 if (unlikely(!__follow_mount_rcu(nd, &path, &inode, &seq)))
2302 return -ECHILD;
2303 } else {
2304 dget(path.dentry);
2305 err = follow_managed(&path, nd);
2306 if (unlikely(err < 0))
2307 return err;
2308 inode = d_backing_inode(path.dentry);
2309 seq = 0;
2310 }
2311 path_to_nameidata(&path, nd);
2312 nd->inode = inode;
2313 nd->seq = seq;
2314 return 0;
2315}
2316
Al Viro9b4a9b12009-04-07 11:44:16 -04002317/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002318static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002319{
Al Viroc8a53ee2015-05-12 18:43:07 -04002320 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002321 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002322
Al Viro9b5858e2018-07-09 16:33:23 -04002323 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002324 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002325 if (unlikely(err < 0))
2326 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002327 }
2328
Al Viro3bdba282015-05-08 17:37:07 -04002329 while (!(err = link_path_walk(s, nd))
2330 && ((err = lookup_last(nd)) > 0)) {
2331 s = trailing_symlink(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002332 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002333 if (!err)
2334 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002335
Al Virodeb106c2015-05-08 18:05:21 -04002336 if (!err && nd->flags & LOOKUP_DIRECTORY)
2337 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002338 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002339 if (!err) {
2340 *path = nd->path;
2341 nd->path.mnt = NULL;
2342 nd->path.dentry = NULL;
2343 }
2344 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002345 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002346}
Nick Piggin31e6b012011-01-07 17:49:52 +11002347
David Howells31d921c2018-11-01 23:07:24 +00002348int filename_lookup(int dfd, struct filename *name, unsigned flags,
2349 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002350{
Al Viro894bc8c2015-05-02 07:16:16 -04002351 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002352 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002353 if (IS_ERR(name))
2354 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002355 if (unlikely(root)) {
2356 nd.root = *root;
2357 flags |= LOOKUP_ROOT;
2358 }
Al Viro9883d182015-05-13 07:28:08 -04002359 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002360 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002361 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002362 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002363 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002364 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002365
2366 if (likely(!retval))
Al Viroff0ebee2019-07-14 12:18:05 -04002367 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002368 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002369 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002370 return retval;
2371}
2372
Al Viro8bcb77f2015-05-08 16:59:20 -04002373/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002374static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002375 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002376{
Al Viroc8a53ee2015-05-12 18:43:07 -04002377 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002378 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002379 if (!err)
2380 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002381 if (!err) {
2382 *parent = nd->path;
2383 nd->path.mnt = NULL;
2384 nd->path.dentry = NULL;
2385 }
2386 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002387 return err;
2388}
2389
Al Viro5c31b6c2015-05-12 17:32:54 -04002390static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002391 unsigned int flags, struct path *parent,
2392 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002393{
2394 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002395 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002396
Al Viro5c31b6c2015-05-12 17:32:54 -04002397 if (IS_ERR(name))
2398 return name;
Al Viro9883d182015-05-13 07:28:08 -04002399 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002400 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002401 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002402 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002403 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002404 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002405 if (likely(!retval)) {
2406 *last = nd.last;
2407 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002408 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002409 } else {
2410 putname(name);
2411 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002412 }
Al Viro9883d182015-05-13 07:28:08 -04002413 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002414 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002415}
2416
Al Viro79714f72012-06-15 03:01:42 +04002417/* does lookup, returns the object with parent locked */
2418struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002419{
Al Viro5c31b6c2015-05-12 17:32:54 -04002420 struct filename *filename;
2421 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002422 struct qstr last;
2423 int type;
Paul Moore51689102015-01-22 00:00:03 -05002424
Al Viro5c31b6c2015-05-12 17:32:54 -04002425 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2426 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002427 if (IS_ERR(filename))
2428 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002429 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002430 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002431 putname(filename);
2432 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002433 }
Al Viro59551022016-01-22 15:40:57 -05002434 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002435 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002436 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002437 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002438 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002439 }
Paul Moore51689102015-01-22 00:00:03 -05002440 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002441 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002442}
2443
Al Virod1811462008-08-02 00:49:18 -04002444int kern_path(const char *name, unsigned int flags, struct path *path)
2445{
Al Viroabc9f5b2015-05-12 16:53:42 -04002446 return filename_lookup(AT_FDCWD, getname_kernel(name),
2447 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002448}
Al Viro4d359502014-03-14 12:20:17 -04002449EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002450
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002451/**
2452 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2453 * @dentry: pointer to dentry of the base directory
2454 * @mnt: pointer to vfs mount of the base directory
2455 * @name: pointer to file name
2456 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002457 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002458 */
2459int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2460 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002461 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002462{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002463 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002464 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002465 return filename_lookup(AT_FDCWD, getname_kernel(name),
2466 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002467}
Al Viro4d359502014-03-14 12:20:17 -04002468EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002469
Al Viro3c95f0d2018-04-06 16:32:38 -04002470static int lookup_one_len_common(const char *name, struct dentry *base,
2471 int len, struct qstr *this)
2472{
2473 this->name = name;
2474 this->len = len;
2475 this->hash = full_name_hash(base, name, len);
2476 if (!len)
2477 return -EACCES;
2478
2479 if (unlikely(name[0] == '.')) {
2480 if (len < 2 || (len == 2 && name[1] == '.'))
2481 return -EACCES;
2482 }
2483
2484 while (len--) {
2485 unsigned int c = *(const unsigned char *)name++;
2486 if (c == '/' || c == '\0')
2487 return -EACCES;
2488 }
2489 /*
2490 * See if the low-level filesystem might want
2491 * to use its own hash..
2492 */
2493 if (base->d_flags & DCACHE_OP_HASH) {
2494 int err = base->d_op->d_hash(base, this);
2495 if (err < 0)
2496 return err;
2497 }
2498
2499 return inode_permission(base->d_inode, MAY_EXEC);
2500}
2501
Christoph Hellwigeead1912007-10-16 23:25:38 -07002502/**
David Howells0da0b7f2018-06-15 15:19:22 +01002503 * try_lookup_one_len - filesystem helper to lookup single pathname component
2504 * @name: pathname component to lookup
2505 * @base: base directory to lookup from
2506 * @len: maximum length @len should be interpreted to
2507 *
2508 * Look up a dentry by name in the dcache, returning NULL if it does not
2509 * currently exist. The function does not try to create a dentry.
2510 *
2511 * Note that this routine is purely a helper for filesystem usage and should
2512 * not be called by generic code.
2513 *
2514 * The caller must hold base->i_mutex.
2515 */
2516struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2517{
2518 struct qstr this;
2519 int err;
2520
2521 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2522
2523 err = lookup_one_len_common(name, base, len, &this);
2524 if (err)
2525 return ERR_PTR(err);
2526
2527 return lookup_dcache(&this, base, 0);
2528}
2529EXPORT_SYMBOL(try_lookup_one_len);
2530
2531/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002532 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002533 * @name: pathname component to lookup
2534 * @base: base directory to lookup from
2535 * @len: maximum length @len should be interpreted to
2536 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002537 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002538 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002539 *
2540 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002541 */
James Morris057f6c02007-04-26 00:12:05 -07002542struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2543{
Al Viro8613a202018-04-06 16:45:33 -04002544 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002545 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002546 int err;
James Morris057f6c02007-04-26 00:12:05 -07002547
Al Viro59551022016-01-22 15:40:57 -05002548 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002549
Al Viro3c95f0d2018-04-06 16:32:38 -04002550 err = lookup_one_len_common(name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002551 if (err)
2552 return ERR_PTR(err);
2553
Al Viro8613a202018-04-06 16:45:33 -04002554 dentry = lookup_dcache(&this, base, 0);
2555 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002556}
Al Viro4d359502014-03-14 12:20:17 -04002557EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002558
NeilBrownbbddca82016-01-07 16:08:20 -05002559/**
2560 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2561 * @name: pathname component to lookup
2562 * @base: base directory to lookup from
2563 * @len: maximum length @len should be interpreted to
2564 *
2565 * Note that this routine is purely a helper for filesystem usage and should
2566 * not be called by generic code.
2567 *
2568 * Unlike lookup_one_len, it should be called without the parent
2569 * i_mutex held, and will take the i_mutex itself if necessary.
2570 */
2571struct dentry *lookup_one_len_unlocked(const char *name,
2572 struct dentry *base, int len)
2573{
2574 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002575 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002576 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002577
Al Viro3c95f0d2018-04-06 16:32:38 -04002578 err = lookup_one_len_common(name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002579 if (err)
2580 return ERR_PTR(err);
2581
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002582 ret = lookup_dcache(&this, base, 0);
2583 if (!ret)
2584 ret = lookup_slow(&this, base, 0);
2585 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002586}
2587EXPORT_SYMBOL(lookup_one_len_unlocked);
2588
Al Viro6c2d47982019-10-31 01:21:58 -04002589/*
2590 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2591 * on negatives. Returns known positive or ERR_PTR(); that's what
2592 * most of the users want. Note that pinned negative with unlocked parent
2593 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2594 * need to be very careful; pinned positives have ->d_inode stable, so
2595 * this one avoids such problems.
2596 */
2597struct dentry *lookup_positive_unlocked(const char *name,
2598 struct dentry *base, int len)
2599{
2600 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
Al Viro2fa6b1e2019-11-12 16:13:06 -05002601 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
Al Viro6c2d47982019-10-31 01:21:58 -04002602 dput(ret);
2603 ret = ERR_PTR(-ENOENT);
2604 }
2605 return ret;
2606}
2607EXPORT_SYMBOL(lookup_positive_unlocked);
2608
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002609#ifdef CONFIG_UNIX98_PTYS
2610int path_pts(struct path *path)
2611{
2612 /* Find something mounted on "pts" in the same directory as
2613 * the input path.
2614 */
2615 struct dentry *child, *parent;
2616 struct qstr this;
2617 int ret;
2618
2619 ret = path_parent_directory(path);
2620 if (ret)
2621 return ret;
2622
2623 parent = path->dentry;
2624 this.name = "pts";
2625 this.len = 3;
2626 child = d_hash_and_lookup(parent, &this);
2627 if (!child)
2628 return -ENOENT;
2629
2630 path->dentry = child;
2631 dput(parent);
2632 follow_mount(path);
2633 return 0;
2634}
2635#endif
2636
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002637int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2638 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639{
Al Viroabc9f5b2015-05-12 16:53:42 -04002640 return filename_lookup(dfd, getname_flags(name, flags, empty),
2641 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642}
Al Virob853a162015-05-13 09:12:02 -04002643EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002644
Jeff Layton80334262013-07-26 06:23:25 -04002645/**
Al Viro197df042013-09-08 14:03:27 -04002646 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002647 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
Jeff Layton80334262013-07-26 06:23:25 -04002648 *
2649 * This is a special lookup_last function just for umount. In this case, we
2650 * need to resolve the path without doing any revalidation.
2651 *
2652 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2653 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2654 * in almost all cases, this lookup will be served out of the dcache. The only
2655 * cases where it won't are if nd->last refers to a symlink or the path is
2656 * bogus and it doesn't exist.
2657 *
2658 * Returns:
2659 * -error: if there was an error during lookup. This includes -ENOENT if the
Al Viroba8f4612016-11-14 00:40:33 -05002660 * lookup found a negative dentry.
Jeff Layton80334262013-07-26 06:23:25 -04002661 *
Al Viroba8f4612016-11-14 00:40:33 -05002662 * 0: if we successfully resolved nd->last and found it to not to be a
2663 * symlink that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002664 *
2665 * 1: if we successfully resolved nd->last and found it to be a symlink
Al Viroba8f4612016-11-14 00:40:33 -05002666 * that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002667 */
2668static int
Al Viroba8f4612016-11-14 00:40:33 -05002669mountpoint_last(struct nameidata *nd)
Jeff Layton80334262013-07-26 06:23:25 -04002670{
2671 int error = 0;
Jeff Layton80334262013-07-26 06:23:25 -04002672 struct dentry *dir = nd->path.dentry;
Al Viroba8f4612016-11-14 00:40:33 -05002673 struct path path;
Jeff Layton80334262013-07-26 06:23:25 -04002674
Al Viro35759522013-09-08 13:41:33 -04002675 /* If we're in rcuwalk, drop out of it to handle last component */
2676 if (nd->flags & LOOKUP_RCU) {
Al Viro4675ac32017-01-09 22:29:15 -05002677 if (unlazy_walk(nd))
Al Virodeb106c2015-05-08 18:05:21 -04002678 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002679 }
2680
2681 nd->flags &= ~LOOKUP_PARENT;
2682
2683 if (unlikely(nd->last_type != LAST_NORM)) {
2684 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002685 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002686 return error;
Al Viroba8f4612016-11-14 00:40:33 -05002687 path.dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002688 } else {
Al Viroba8f4612016-11-14 00:40:33 -05002689 path.dentry = d_lookup(dir, &nd->last);
2690 if (!path.dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002691 /*
2692 * No cached dentry. Mounted dentries are pinned in the
2693 * cache, so that means that this dentry is probably
2694 * a symlink or the path doesn't actually point
2695 * to a mounted dentry.
2696 */
Al Viroba8f4612016-11-14 00:40:33 -05002697 path.dentry = lookup_slow(&nd->last, dir,
Al Viro949a8522016-03-06 14:20:52 -05002698 nd->flags | LOOKUP_NO_REVAL);
Al Viroba8f4612016-11-14 00:40:33 -05002699 if (IS_ERR(path.dentry))
2700 return PTR_ERR(path.dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002701 }
Jeff Layton80334262013-07-26 06:23:25 -04002702 }
Al Viro2fa6b1e2019-11-12 16:13:06 -05002703 if (d_flags_negative(smp_load_acquire(&path.dentry->d_flags))) {
Al Viroba8f4612016-11-14 00:40:33 -05002704 dput(path.dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002705 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002706 }
Al Viroba8f4612016-11-14 00:40:33 -05002707 path.mnt = nd->path.mnt;
Al Viro8f64fb12016-11-14 01:50:26 -05002708 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
Jeff Layton80334262013-07-26 06:23:25 -04002709}
2710
2711/**
Al Viro197df042013-09-08 14:03:27 -04002712 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b8572015-09-09 15:39:23 -07002713 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002714 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002715 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002716 *
2717 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002718 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002719 */
2720static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002721path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002722{
Al Viroc8a53ee2015-05-12 18:43:07 -04002723 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002724 int err;
Al Viro9b5858e2018-07-09 16:33:23 -04002725
Al Viro3bdba282015-05-08 17:37:07 -04002726 while (!(err = link_path_walk(s, nd)) &&
Al Viroba8f4612016-11-14 00:40:33 -05002727 (err = mountpoint_last(nd)) > 0) {
Al Viro3bdba282015-05-08 17:37:07 -04002728 s = trailing_symlink(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002729 }
Al Viroba8f4612016-11-14 00:40:33 -05002730 if (!err) {
2731 *path = nd->path;
2732 nd->path.mnt = NULL;
2733 nd->path.dentry = NULL;
2734 follow_mount(path);
2735 }
Al Virodeb106c2015-05-08 18:05:21 -04002736 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002737 return err;
2738}
2739
Al Viro2d864652013-09-08 20:18:44 -04002740static int
Al Viro668696d2015-02-22 19:44:00 -05002741filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002742 unsigned int flags)
2743{
Al Viro9883d182015-05-13 07:28:08 -04002744 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002745 int error;
Al Viro668696d2015-02-22 19:44:00 -05002746 if (IS_ERR(name))
2747 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002748 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002749 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002750 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002751 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002752 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002753 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002754 if (likely(!error))
Al Viroc9b07ea2019-07-14 13:22:27 -04002755 audit_inode(name, path->dentry, AUDIT_INODE_NOEVAL);
Al Viro9883d182015-05-13 07:28:08 -04002756 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002757 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002758 return error;
2759}
2760
Jeff Layton80334262013-07-26 06:23:25 -04002761/**
Al Viro197df042013-09-08 14:03:27 -04002762 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002763 * @dfd: directory file descriptor
2764 * @name: pathname from userland
2765 * @flags: lookup flags
2766 * @path: pointer to container to hold result
2767 *
2768 * A umount is a special case for path walking. We're not actually interested
2769 * in the inode in this situation, and ESTALE errors can be a problem. We
2770 * simply want track down the dentry and vfsmount attached at the mountpoint
2771 * and avoid revalidating the last component.
2772 *
2773 * Returns 0 and populates "path" on success.
2774 */
2775int
Al Viro197df042013-09-08 14:03:27 -04002776user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002777 struct path *path)
2778{
Al Virocbaab2d2015-01-22 02:49:00 -05002779 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002780}
2781
Al Viro2d864652013-09-08 20:18:44 -04002782int
2783kern_path_mountpoint(int dfd, const char *name, struct path *path,
2784 unsigned int flags)
2785{
Al Virocbaab2d2015-01-22 02:49:00 -05002786 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002787}
2788EXPORT_SYMBOL(kern_path_mountpoint);
2789
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002790int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002792 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002793
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002794 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002796 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002798 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002800EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
2802/*
2803 * Check whether we can remove a link victim from directory dir, check
2804 * whether the type of victim is right.
2805 * 1. We can't do it if dir is read-only (done in permission())
2806 * 2. We should have write and exec permissions on dir
2807 * 3. We can't remove anything from append-only dir
2808 * 4. We can't do anything with immutable dir (done in permission())
2809 * 5. If the sticky bit on dir is set we should either
2810 * a. be owner of dir, or
2811 * b. be owner of victim, or
2812 * c. have CAP_FOWNER capability
2813 * 6. If the victim is append-only or immutable we can't do antyhing with
2814 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002815 * 7. If the victim has an unknown uid or gid we can't change the inode.
2816 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2817 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2818 * 10. We can't remove a root or mountpoint.
2819 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 * nfs_async_unlink().
2821 */
David Howellsb18825a2013-09-12 19:22:53 +01002822static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
David Howells63afdfc2015-05-06 15:59:00 +01002824 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 int error;
2826
David Howellsb18825a2013-09-12 19:22:53 +01002827 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002829 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002832
2833 /* Inode writeback is not safe when the uid or gid are invalid. */
2834 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2835 return -EOVERFLOW;
2836
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002837 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Al Virof419a2e2008-07-22 00:07:17 -04002839 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 if (error)
2841 return error;
2842 if (IS_APPEND(dir))
2843 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002844
2845 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002846 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 return -EPERM;
2848 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002849 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 return -ENOTDIR;
2851 if (IS_ROOT(victim))
2852 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002853 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 return -EISDIR;
2855 if (IS_DEADDIR(dir))
2856 return -ENOENT;
2857 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2858 return -EBUSY;
2859 return 0;
2860}
2861
2862/* Check whether we can create an object with dentry child in directory
2863 * dir.
2864 * 1. We can't do it if child already exists (open has special treatment for
2865 * this case, but since we are inlined it's OK)
2866 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002867 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2868 * 4. We should have write and exec permissions on dir
2869 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002871static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002873 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002874 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (child->d_inode)
2876 return -EEXIST;
2877 if (IS_DEADDIR(dir))
2878 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002879 s_user_ns = dir->i_sb->s_user_ns;
2880 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2881 !kgid_has_mapping(s_user_ns, current_fsgid()))
2882 return -EOVERFLOW;
Al Virof419a2e2008-07-22 00:07:17 -04002883 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884}
2885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886/*
2887 * p1 and p2 should be directories on the same fs.
2888 */
2889struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2890{
2891 struct dentry *p;
2892
2893 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002894 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 return NULL;
2896 }
2897
Al Virofc640052016-04-10 01:33:30 -04002898 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002900 p = d_ancestor(p2, p1);
2901 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002902 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2903 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002904 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 }
2906
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002907 p = d_ancestor(p1, p2);
2908 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002909 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2910 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002911 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 }
2913
Al Viro59551022016-01-22 15:40:57 -05002914 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2915 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 return NULL;
2917}
Al Viro4d359502014-03-14 12:20:17 -04002918EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920void unlock_rename(struct dentry *p1, struct dentry *p2)
2921{
Al Viro59551022016-01-22 15:40:57 -05002922 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002924 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002925 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927}
Al Viro4d359502014-03-14 12:20:17 -04002928EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Al Viro4acdaf22011-07-26 01:42:34 -04002930int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002931 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002933 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (error)
2935 return error;
2936
Al Viroacfa4382008-12-04 10:06:33 -05002937 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 return -EACCES; /* shouldn't it be ENOSYS? */
2939 mode &= S_IALLUGO;
2940 mode |= S_IFREG;
2941 error = security_inode_create(dir, dentry, mode);
2942 if (error)
2943 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002944 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002945 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002946 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 return error;
2948}
Al Viro4d359502014-03-14 12:20:17 -04002949EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
Al Viro8e6c8482017-12-01 17:12:45 -05002951int vfs_mkobj(struct dentry *dentry, umode_t mode,
2952 int (*f)(struct dentry *, umode_t, void *),
2953 void *arg)
2954{
2955 struct inode *dir = dentry->d_parent->d_inode;
2956 int error = may_create(dir, dentry);
2957 if (error)
2958 return error;
2959
2960 mode &= S_IALLUGO;
2961 mode |= S_IFREG;
2962 error = security_inode_create(dir, dentry, mode);
2963 if (error)
2964 return error;
2965 error = f(dentry, mode, arg);
2966 if (!error)
2967 fsnotify_create(dir, dentry);
2968 return error;
2969}
2970EXPORT_SYMBOL(vfs_mkobj);
2971
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002972bool may_open_dev(const struct path *path)
2973{
2974 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2975 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2976}
2977
Al Virof0bb5aa2016-11-20 20:27:12 -05002978static int may_open(const struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002980 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 struct inode *inode = dentry->d_inode;
2982 int error;
2983
2984 if (!inode)
2985 return -ENOENT;
2986
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002987 switch (inode->i_mode & S_IFMT) {
2988 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002990 case S_IFDIR:
2991 if (acc_mode & MAY_WRITE)
2992 return -EISDIR;
2993 break;
2994 case S_IFBLK:
2995 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002996 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002998 /*FALLTHRU*/
2999 case S_IFIFO:
3000 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01003002 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08003003 }
Dave Hansenb41572e2007-10-16 23:31:14 -07003004
Al Viro62fb4a12015-12-26 22:33:24 -05003005 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07003006 if (error)
3007 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05003008
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 /*
3010 * An append-only file must be opened in append mode for writing.
3011 */
3012 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05003013 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05003014 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05003016 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 }
3018
3019 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07003020 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05003021 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04003023 return 0;
Al Viro7715b522009-12-16 03:54:00 -05003024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
Jeff Laytone1181ee2010-12-07 16:19:50 -05003026static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05003027{
Al Virof0bb5aa2016-11-20 20:27:12 -05003028 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05003029 struct inode *inode = path->dentry->d_inode;
3030 int error = get_write_access(inode);
3031 if (error)
3032 return error;
3033 /*
3034 * Refuse to truncate files with mandatory locks held on them.
3035 */
Jeff Laytond7a06982014-03-10 09:54:15 -04003036 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05003037 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09003038 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05003039 if (!error) {
3040 error = do_truncate(path->dentry, 0,
3041 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05003042 filp);
Al Viro7715b522009-12-16 03:54:00 -05003043 }
3044 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04003045 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Dave Hansend57999e2008-02-15 14:37:27 -08003048static inline int open_to_namei_flags(int flag)
3049{
Al Viro8a5e9292011-06-25 19:15:54 -04003050 if ((flag & O_ACCMODE) == 3)
3051 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003052 return flag;
3053}
3054
Al Virod3607752016-03-25 15:21:09 -04003055static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003056{
Seth Forshee1328c722017-01-26 14:33:46 -06003057 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003058 int error = security_path_mknod(dir, dentry, mode, 0);
3059 if (error)
3060 return error;
3061
Seth Forshee1328c722017-01-26 14:33:46 -06003062 s_user_ns = dir->dentry->d_sb->s_user_ns;
3063 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3064 !kgid_has_mapping(s_user_ns, current_fsgid()))
3065 return -EOVERFLOW;
3066
Miklos Szeredid18e9002012-06-05 15:10:17 +02003067 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
3068 if (error)
3069 return error;
3070
3071 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3072}
3073
David Howells1acf0af2012-06-14 16:13:46 +01003074/*
3075 * Attempt to atomically look up, create and open a file from a negative
3076 * dentry.
3077 *
3078 * Returns 0 if successful. The file will have been created and attached to
3079 * @file by the filesystem calling finish_open().
3080 *
Al Viro00a07c12018-07-09 19:30:20 -04003081 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3082 * be set. The caller will need to perform the open themselves. @path will
3083 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003084 *
3085 * Returns an error code otherwise.
3086 */
Al Viro2675a4e2012-06-22 12:41:10 +04003087static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3088 struct path *path, struct file *file,
3089 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003090 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003091{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003092 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003093 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003094 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003095
Al Viro384f26e2016-04-28 02:03:55 -04003096 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003097 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003098
Miklos Szeredid18e9002012-06-05 15:10:17 +02003099 if (nd->flags & LOOKUP_DIRECTORY)
3100 open_flag |= O_DIRECTORY;
3101
Al Viro30d90492012-06-22 12:40:19 +04003102 file->f_path.dentry = DENTRY_NOT_SET;
3103 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003104 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003105 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003106 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003107 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003108 if (file->f_mode & FMODE_OPENED) {
3109 /*
3110 * We didn't have the inode before the open, so check open
3111 * permission here.
3112 */
3113 int acc_mode = op->acc_mode;
3114 if (file->f_mode & FMODE_CREATED) {
3115 WARN_ON(!(open_flag & O_CREAT));
3116 fsnotify_create(dir, dentry);
3117 acc_mode = 0;
3118 }
3119 error = may_open(&file->f_path, acc_mode, open_flag);
3120 if (WARN_ON(error > 0))
3121 error = -EINVAL;
3122 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003123 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003124 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003125 if (file->f_path.dentry) {
3126 dput(dentry);
3127 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003128 }
Al Viro73a09dd2018-06-08 13:22:02 -04003129 if (file->f_mode & FMODE_CREATED)
Al Viro384f26e2016-04-28 02:03:55 -04003130 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04003131 if (unlikely(d_is_negative(dentry))) {
3132 error = -ENOENT;
3133 } else {
3134 path->dentry = dentry;
3135 path->mnt = nd->path.mnt;
Al Viro00a07c12018-07-09 19:30:20 -04003136 return 0;
Al Viroa01e7182016-06-07 21:53:51 -04003137 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003138 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003139 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003140 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003141 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003142}
3143
Nick Piggin31e6b012011-01-07 17:49:52 +11003144/*
David Howells1acf0af2012-06-14 16:13:46 +01003145 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003146 *
Al Viro00a07c12018-07-09 19:30:20 -04003147 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003148 *
Al Viro00a07c12018-07-09 19:30:20 -04003149 * Returns 0 on success, that is, if
3150 * the file was successfully atomically created (if necessary) and opened, or
3151 * the file was not completely opened at this time, though lookups and
3152 * creations were performed.
3153 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3154 * In the latter case dentry returned in @path might be negative if O_CREAT
3155 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003156 *
Al Viro00a07c12018-07-09 19:30:20 -04003157 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003158 */
Al Viro2675a4e2012-06-22 12:41:10 +04003159static int lookup_open(struct nameidata *nd, struct path *path,
3160 struct file *file,
3161 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003162 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003163{
3164 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003165 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003166 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003167 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003168 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003169 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003170 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003171
Al Viroce8644f2016-04-26 14:17:56 -04003172 if (unlikely(IS_DEADDIR(dir_inode)))
3173 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003174
Al Viro73a09dd2018-06-08 13:22:02 -04003175 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003176 dentry = d_lookup(dir, &nd->last);
3177 for (;;) {
3178 if (!dentry) {
3179 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3180 if (IS_ERR(dentry))
3181 return PTR_ERR(dentry);
3182 }
3183 if (d_in_lookup(dentry))
3184 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003185
Al Viro6fbd0712016-04-28 11:50:59 -04003186 error = d_revalidate(dentry, nd->flags);
3187 if (likely(error > 0))
3188 break;
3189 if (error)
3190 goto out_dput;
3191 d_invalidate(dentry);
3192 dput(dentry);
3193 dentry = NULL;
3194 }
3195 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003196 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003197 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003198 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003199
Al Viro1643b432016-04-27 19:14:10 -04003200 /*
3201 * Checking write permission is tricky, bacuse we don't know if we are
3202 * going to actually need it: O_CREAT opens should work as long as the
3203 * file exists. But checking existence breaks atomicity. The trick is
3204 * to check access and if not granted clear O_CREAT from the flags.
3205 *
3206 * Another problem is returing the "right" error value (e.g. for an
3207 * O_EXCL open we want to return EEXIST not EROFS).
3208 */
3209 if (open_flag & O_CREAT) {
3210 if (!IS_POSIXACL(dir->d_inode))
3211 mode &= ~current_umask();
3212 if (unlikely(!got_write)) {
3213 create_error = -EROFS;
3214 open_flag &= ~O_CREAT;
3215 if (open_flag & (O_EXCL | O_TRUNC))
3216 goto no_open;
3217 /* No side effects, safe to clear O_CREAT */
3218 } else {
3219 create_error = may_o_create(&nd->path, dentry, mode);
3220 if (create_error) {
3221 open_flag &= ~O_CREAT;
3222 if (open_flag & O_EXCL)
3223 goto no_open;
3224 }
3225 }
3226 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3227 unlikely(!got_write)) {
3228 /*
3229 * No O_CREATE -> atomicity not a requirement -> fall
3230 * back to lookup + open
3231 */
3232 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003233 }
3234
Al Viro1643b432016-04-27 19:14:10 -04003235 if (dir_inode->i_op->atomic_open) {
3236 error = atomic_open(nd, dentry, path, file, op, open_flag,
Al Viro3ec2eef2018-06-08 13:43:47 -04003237 mode);
Al Viro1643b432016-04-27 19:14:10 -04003238 if (unlikely(error == -ENOENT) && create_error)
3239 error = create_error;
3240 return error;
3241 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003242
Al Viro1643b432016-04-27 19:14:10 -04003243no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003244 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003245 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3246 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003247 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003248 if (unlikely(res)) {
3249 if (IS_ERR(res)) {
3250 error = PTR_ERR(res);
3251 goto out_dput;
3252 }
3253 dput(dentry);
3254 dentry = res;
3255 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003256 }
3257
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003258 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003259 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003260 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003261 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003262 if (!dir_inode->i_op->create) {
3263 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003264 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003265 }
Al Viroce8644f2016-04-26 14:17:56 -04003266 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003267 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003268 if (error)
3269 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003270 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003271 }
Al Viro1643b432016-04-27 19:14:10 -04003272 if (unlikely(create_error) && !dentry->d_inode) {
3273 error = create_error;
3274 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003275 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003276out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003277 path->dentry = dentry;
3278 path->mnt = nd->path.mnt;
Al Viro00a07c12018-07-09 19:30:20 -04003279 return 0;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003280
3281out_dput:
3282 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003283 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003284}
3285
3286/*
Al Virofe2d35f2011-03-05 22:58:25 -05003287 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003288 */
Al Viro896475d2015-04-22 18:02:17 -04003289static int do_last(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003290 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003291{
Al Viroa1e28032009-12-24 02:12:06 -05003292 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003293 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003294 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003295 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003296 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003297 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003298 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003299 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003300 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003301
Al Viroc3e380b2011-02-23 13:39:45 -05003302 nd->flags &= ~LOOKUP_PARENT;
3303 nd->flags |= op->intent;
3304
Al Virobc77daa2013-06-06 09:12:33 -04003305 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003306 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003307 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003308 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003309 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003310 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003311
Al Viroca344a892011-03-09 00:36:45 -05003312 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003313 if (nd->last.name[nd->last.len])
3314 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3315 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003316 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003317 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003318 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003319
Miklos Szeredi71574862012-06-05 15:10:14 +02003320 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003321 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003322
Miklos Szeredi71574862012-06-05 15:10:14 +02003323 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003324 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003325 } else {
3326 /* create side of things */
3327 /*
3328 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3329 * has been cleared when we got to the last component we are
3330 * about to look up
3331 */
3332 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003333 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003334 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003335
Al Viroc9b07ea2019-07-14 13:22:27 -04003336 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003337 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003338 if (unlikely(nd->last.name[nd->last.len]))
3339 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003340 }
3341
Al Viro9cf843e2016-04-28 19:35:16 -04003342 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003343 error = mnt_want_write(nd->path.mnt);
3344 if (!error)
3345 got_write = true;
3346 /*
3347 * do _not_ fail yet - we might not need that or fail with
3348 * a different error; let lookup_open() decide; we'll be
3349 * dropping this one anyway.
3350 */
3351 }
Al Viro9cf843e2016-04-28 19:35:16 -04003352 if (open_flag & O_CREAT)
3353 inode_lock(dir->d_inode);
3354 else
3355 inode_lock_shared(dir->d_inode);
Al Viro3ec2eef2018-06-08 13:43:47 -04003356 error = lookup_open(nd, &path, file, op, got_write);
Al Viro9cf843e2016-04-28 19:35:16 -04003357 if (open_flag & O_CREAT)
3358 inode_unlock(dir->d_inode);
3359 else
3360 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003361
Al Viro00a07c12018-07-09 19:30:20 -04003362 if (error)
3363 goto out;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003364
Al Viro00a07c12018-07-09 19:30:20 -04003365 if (file->f_mode & FMODE_OPENED) {
Al Viro73a09dd2018-06-08 13:22:02 -04003366 if ((file->f_mode & FMODE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003367 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003368 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003369
Al Viro76ae2a52015-05-12 18:44:32 -04003370 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003371 goto opened;
3372 }
Al Virofb1cc552009-12-24 01:58:28 -05003373
Al Viro73a09dd2018-06-08 13:22:02 -04003374 if (file->f_mode & FMODE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003375 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003376 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003377 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003378 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003379 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003380 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003381 }
3382
3383 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003384 * If atomic_open() acquired write access it is dropped now due to
3385 * possible mount and symlink following (this might be optimized away if
3386 * necessary...)
3387 */
Al Viro64894cf2012-07-31 00:53:35 +04003388 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003389 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003390 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003391 }
3392
Al Viroe6ec03a2016-06-05 00:23:09 -04003393 error = follow_managed(&path, nd);
3394 if (unlikely(error < 0))
3395 return error;
3396
Al Viro6583fe22016-03-05 18:14:03 -05003397 /*
3398 * create/update audit record if it already exists.
3399 */
3400 audit_inode(nd->name, path.dentry, 0);
3401
Al Virodeb106c2015-05-08 18:05:21 -04003402 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3403 path_to_nameidata(&path, nd);
3404 return -EEXIST;
3405 }
Al Virofb1cc552009-12-24 01:58:28 -05003406
Al Viro254cf582015-05-05 09:40:46 -04003407 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003408 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003409finish_lookup:
Al Viro8f64fb12016-11-14 01:50:26 -05003410 error = step_into(nd, &path, 0, inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003411 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003412 return error;
Al Virobc77daa2013-06-06 09:12:33 -04003413finish_open:
Al Viro8f64fb12016-11-14 01:50:26 -05003414 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Viroa3fbbde2011-11-07 21:21:26 +00003415 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003416 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003417 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003418 audit_inode(nd->name, nd->path.dentry, 0);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003419 if (open_flag & O_CREAT) {
3420 error = -EISDIR;
3421 if (d_is_dir(nd->path.dentry))
3422 goto out;
3423 error = may_create_in_sticky(dir,
3424 d_backing_inode(nd->path.dentry));
3425 if (unlikely(error))
3426 goto out;
3427 }
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003428 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003429 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003430 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003431 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003432 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003433
Al Viro0f9d1a12011-03-09 00:13:14 -05003434 if (will_truncate) {
3435 error = mnt_want_write(nd->path.mnt);
3436 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003437 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003438 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003439 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003440finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003441 error = may_open(&nd->path, acc_mode, open_flag);
3442 if (error)
3443 goto out;
Al Viroaad888f2018-06-08 12:58:04 -04003444 BUG_ON(file->f_mode & FMODE_OPENED); /* once it's opened, it's opened */
Al Viroae2bb292018-07-10 13:22:28 -04003445 error = vfs_open(&nd->path, file);
Al Virofac7d192016-06-04 11:41:49 -04003446 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003447 goto out;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003448opened:
Al Viro6035a272018-06-08 13:40:10 -04003449 error = ima_file_check(file, op->acc_mode);
Al Virofe9ec822016-04-27 03:14:20 -04003450 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003451 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003452out:
Al Viroc80567c2016-02-27 19:17:33 -05003453 if (unlikely(error > 0)) {
3454 WARN_ON(1);
3455 error = -EINVAL;
3456 }
Al Viro64894cf2012-07-31 00:53:35 +04003457 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003458 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003459 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003460}
3461
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003462struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3463{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003464 struct dentry *child = NULL;
3465 struct inode *dir = dentry->d_inode;
3466 struct inode *inode;
3467 int error;
3468
3469 /* we want directory to be writable */
3470 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
3471 if (error)
3472 goto out_err;
3473 error = -EOPNOTSUPP;
3474 if (!dir->i_op->tmpfile)
3475 goto out_err;
3476 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003477 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003478 if (unlikely(!child))
3479 goto out_err;
3480 error = dir->i_op->tmpfile(dir, child, mode);
3481 if (error)
3482 goto out_err;
3483 error = -ENOENT;
3484 inode = child->d_inode;
3485 if (unlikely(!inode))
3486 goto out_err;
3487 if (!(open_flag & O_EXCL)) {
3488 spin_lock(&inode->i_lock);
3489 inode->i_state |= I_LINKABLE;
3490 spin_unlock(&inode->i_lock);
3491 }
Mimi Zoharfdb24102019-01-22 14:06:49 -06003492 ima_post_create_tmpfile(inode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003493 return child;
3494
3495out_err:
3496 dput(child);
3497 return ERR_PTR(error);
3498}
3499EXPORT_SYMBOL(vfs_tmpfile);
3500
Al Viroc8a53ee2015-05-12 18:43:07 -04003501static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003502 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003503 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003504{
Al Viro625b6d12015-05-12 16:36:12 -04003505 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003506 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003507 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003508 if (unlikely(error))
3509 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003510 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003511 if (unlikely(error))
3512 goto out;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003513 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3514 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003515 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003516 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003517 dput(path.dentry);
3518 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003519 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003520 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003521 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003522 if (error)
3523 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003524 file->f_path.mnt = path.mnt;
Al Virobe12af32018-06-08 11:44:56 -04003525 error = finish_open(file, child, NULL);
Al Viro60545d02013-06-07 01:20:27 -04003526out2:
Al Viro625b6d12015-05-12 16:36:12 -04003527 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003528out:
Al Viro625b6d12015-05-12 16:36:12 -04003529 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003530 return error;
3531}
3532
Al Viro6ac08702016-04-26 00:02:50 -04003533static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3534{
3535 struct path path;
3536 int error = path_lookupat(nd, flags, &path);
3537 if (!error) {
3538 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003539 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003540 path_put(&path);
3541 }
3542 return error;
3543}
3544
Al Viroc8a53ee2015-05-12 18:43:07 -04003545static struct file *path_openat(struct nameidata *nd,
3546 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547{
Al Viro30d90492012-06-22 12:40:19 +04003548 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003549 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003550
Al Viroea73ea72018-07-11 15:00:04 -04003551 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003552 if (IS_ERR(file))
3553 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003554
Al Virobb458c62013-07-13 13:26:37 +04003555 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003556 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003557 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003558 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003559 } else {
3560 const char *s = path_init(nd, flags);
3561 while (!(error = link_path_walk(s, nd)) &&
3562 (error = do_last(nd, file, op)) > 0) {
3563 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
3564 s = trailing_symlink(nd);
3565 }
3566 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003567 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003568 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003569 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003570 return file;
3571 WARN_ON(1);
3572 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003573 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003574 fput(file);
3575 if (error == -EOPENSTALE) {
3576 if (flags & LOOKUP_RCU)
3577 error = -ECHILD;
3578 else
3579 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003580 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003581 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582}
3583
Jeff Layton669abf42012-10-10 16:43:10 -04003584struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003585 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003586{
Al Viro9883d182015-05-13 07:28:08 -04003587 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003588 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003589 struct file *filp;
3590
Al Viro9883d182015-05-13 07:28:08 -04003591 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003592 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003593 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003594 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003595 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003596 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003597 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003598 return filp;
3599}
3600
Al Viro73d049a2011-03-11 12:08:24 -05003601struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003602 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003603{
Al Viro9883d182015-05-13 07:28:08 -04003604 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003605 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003606 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003607 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003608
3609 nd.root.mnt = mnt;
3610 nd.root.dentry = dentry;
3611
David Howellsb18825a2013-09-12 19:22:53 +01003612 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003613 return ERR_PTR(-ELOOP);
3614
Paul Moore51689102015-01-22 00:00:03 -05003615 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303616 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003617 return ERR_CAST(filename);
3618
Al Viro9883d182015-05-13 07:28:08 -04003619 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003620 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003621 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003622 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003623 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003624 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003625 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003626 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003627 return file;
3628}
3629
Al Virofa14a0b2015-01-22 02:16:49 -05003630static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003631 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003633 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003634 struct qstr last;
3635 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003636 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003637 int error;
3638 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3639
3640 /*
3641 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3642 * other flags passed in are ignored!
3643 */
3644 lookup_flags &= LOOKUP_REVAL;
3645
Al Viro5c31b6c2015-05-12 17:32:54 -04003646 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3647 if (IS_ERR(name))
3648 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003650 /*
3651 * Yucky last component or no last component at all?
3652 * (foo/., foo/.., /////)
3653 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003654 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003655 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003656
Jan Karac30dabf2012-06-12 16:20:30 +02003657 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003658 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003659 /*
3660 * Do the final lookup.
3661 */
Al Viro391172c2015-05-09 11:19:16 -04003662 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003663 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003664 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003666 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003667
Al Viroa8104a92012-07-20 02:25:00 +04003668 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003669 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003670 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003671
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003672 /*
3673 * Special case - lookup gave negative, but... we had foo/bar/
3674 * From the vfs_mknod() POV we just have a negative dentry -
3675 * all is fine. Let's be bastards - you had / on the end, you've
3676 * been asking for (non-existent) directory. -ENOENT for you.
3677 */
Al Viro391172c2015-05-09 11:19:16 -04003678 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003679 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003680 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003681 }
Jan Karac30dabf2012-06-12 16:20:30 +02003682 if (unlikely(err2)) {
3683 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003684 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003685 }
Al Viro181c37b2015-05-12 17:21:25 -04003686 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688fail:
Al Viroa8104a92012-07-20 02:25:00 +04003689 dput(dentry);
3690 dentry = ERR_PTR(error);
3691unlock:
Al Viro59551022016-01-22 15:40:57 -05003692 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003693 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003694 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003695out:
Al Viro391172c2015-05-09 11:19:16 -04003696 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003697 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 return dentry;
3699}
Al Virofa14a0b2015-01-22 02:16:49 -05003700
3701struct dentry *kern_path_create(int dfd, const char *pathname,
3702 struct path *path, unsigned int lookup_flags)
3703{
Al Viro181c37b2015-05-12 17:21:25 -04003704 return filename_create(dfd, getname_kernel(pathname),
3705 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003706}
Al Virodae6ad82011-06-26 11:50:15 -04003707EXPORT_SYMBOL(kern_path_create);
3708
Al Viro921a1652012-07-20 01:15:31 +04003709void done_path_create(struct path *path, struct dentry *dentry)
3710{
3711 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003712 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003713 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003714 path_put(path);
3715}
3716EXPORT_SYMBOL(done_path_create);
3717
Al Viro520ae682015-05-13 07:00:28 -04003718inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003719 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003720{
Al Viro181c37b2015-05-12 17:21:25 -04003721 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003722}
3723EXPORT_SYMBOL(user_path_create);
3724
Al Viro1a67aaf2011-07-26 01:52:52 -04003725int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003727 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728
3729 if (error)
3730 return error;
3731
Christian Brauner94f82002018-07-05 17:51:20 +02003732 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 return -EPERM;
3734
Al Viroacfa4382008-12-04 10:06:33 -05003735 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return -EPERM;
3737
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003738 error = devcgroup_inode_mknod(mode, dev);
3739 if (error)
3740 return error;
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 error = security_inode_mknod(dir, dentry, mode, dev);
3743 if (error)
3744 return error;
3745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003747 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003748 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 return error;
3750}
Al Viro4d359502014-03-14 12:20:17 -04003751EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752
Al Virof69aac02011-07-26 04:31:40 -04003753static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003754{
3755 switch (mode & S_IFMT) {
3756 case S_IFREG:
3757 case S_IFCHR:
3758 case S_IFBLK:
3759 case S_IFIFO:
3760 case S_IFSOCK:
3761 case 0: /* zero mode translates to S_IFREG */
3762 return 0;
3763 case S_IFDIR:
3764 return -EPERM;
3765 default:
3766 return -EINVAL;
3767 }
3768}
3769
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003770long do_mknodat(int dfd, const char __user *filename, umode_t mode,
3771 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
Al Viro2ad94ae2008-07-21 09:32:51 -04003773 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003774 struct path path;
3775 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003776 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Al Viro8e4bfca2012-07-20 01:17:26 +04003778 error = may_mknod(mode);
3779 if (error)
3780 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003781retry:
3782 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003783 if (IS_ERR(dentry))
3784 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003785
Al Virodae6ad82011-06-26 11:50:15 -04003786 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003787 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003788 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003789 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003790 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003791 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003793 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003794 if (!error)
3795 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 break;
3797 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003798 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 new_decode_dev(dev));
3800 break;
3801 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003802 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 }
Al Viroa8104a92012-07-20 02:25:00 +04003805out:
Al Viro921a1652012-07-20 01:15:31 +04003806 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003807 if (retry_estale(error, lookup_flags)) {
3808 lookup_flags |= LOOKUP_REVAL;
3809 goto retry;
3810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return error;
3812}
3813
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003814SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3815 unsigned int, dev)
3816{
3817 return do_mknodat(dfd, filename, mode, dev);
3818}
3819
Al Viro8208a222011-07-25 17:32:17 -04003820SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003821{
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003822 return do_mknodat(AT_FDCWD, filename, mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003823}
3824
Al Viro18bb1db2011-07-26 01:41:39 -04003825int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003827 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003828 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829
3830 if (error)
3831 return error;
3832
Al Viroacfa4382008-12-04 10:06:33 -05003833 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 return -EPERM;
3835
3836 mode &= (S_IRWXUGO|S_ISVTX);
3837 error = security_inode_mkdir(dir, dentry, mode);
3838 if (error)
3839 return error;
3840
Al Viro8de52772012-02-06 12:45:27 -05003841 if (max_links && dir->i_nlink >= max_links)
3842 return -EMLINK;
3843
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003845 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003846 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 return error;
3848}
Al Viro4d359502014-03-14 12:20:17 -04003849EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Dominik Brodowski0101db72018-03-11 11:34:49 +01003851long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852{
Dave Hansen6902d922006-09-30 23:29:01 -07003853 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003854 struct path path;
3855 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003856 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003858retry:
3859 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003860 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003861 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003862
Al Virodae6ad82011-06-26 11:50:15 -04003863 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003864 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003865 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003866 if (!error)
3867 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003868 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003869 if (retry_estale(error, lookup_flags)) {
3870 lookup_flags |= LOOKUP_REVAL;
3871 goto retry;
3872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return error;
3874}
3875
Dominik Brodowski0101db72018-03-11 11:34:49 +01003876SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3877{
3878 return do_mkdirat(dfd, pathname, mode);
3879}
3880
Al Viroa218d0f2011-11-21 14:59:34 -05003881SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003882{
Dominik Brodowski0101db72018-03-11 11:34:49 +01003883 return do_mkdirat(AT_FDCWD, pathname, mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003884}
3885
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3887{
3888 int error = may_delete(dir, dentry, 1);
3889
3890 if (error)
3891 return error;
3892
Al Viroacfa4382008-12-04 10:06:33 -05003893 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 return -EPERM;
3895
Al Viro1d2ef592011-09-14 18:55:41 +01003896 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003897 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898
Sage Weil912dbc12011-05-24 13:06:11 -07003899 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003900 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003901 goto out;
3902
3903 error = security_inode_rmdir(dir, dentry);
3904 if (error)
3905 goto out;
3906
3907 error = dir->i_op->rmdir(dir, dentry);
3908 if (error)
3909 goto out;
3910
Al Viro87677122018-05-27 16:23:51 -04003911 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003912 dentry->d_inode->i_flags |= S_DEAD;
3913 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003914 detach_mounts(dentry);
Amir Goldstein116b9732019-05-26 17:34:02 +03003915 fsnotify_rmdir(dir, dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003916
3917out:
Al Viro59551022016-01-22 15:40:57 -05003918 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003919 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003920 if (!error)
3921 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 return error;
3923}
Al Viro4d359502014-03-14 12:20:17 -04003924EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925
Dominik Brodowskif459dffa2018-03-11 11:34:48 +01003926long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927{
3928 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003929 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003931 struct path path;
3932 struct qstr last;
3933 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003934 unsigned int lookup_flags = 0;
3935retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04003936 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3937 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003938 if (IS_ERR(name))
3939 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Al Virof5beed72015-04-30 16:09:11 -04003941 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003942 case LAST_DOTDOT:
3943 error = -ENOTEMPTY;
3944 goto exit1;
3945 case LAST_DOT:
3946 error = -EINVAL;
3947 goto exit1;
3948 case LAST_ROOT:
3949 error = -EBUSY;
3950 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003952
Al Virof5beed72015-04-30 16:09:11 -04003953 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003954 if (error)
3955 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003956
Al Viro59551022016-01-22 15:40:57 -05003957 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003958 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003960 if (IS_ERR(dentry))
3961 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003962 if (!dentry->d_inode) {
3963 error = -ENOENT;
3964 goto exit3;
3965 }
Al Virof5beed72015-04-30 16:09:11 -04003966 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003967 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003968 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003969 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003970exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003971 dput(dentry);
3972exit2:
Al Viro59551022016-01-22 15:40:57 -05003973 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003974 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975exit1:
Al Virof5beed72015-04-30 16:09:11 -04003976 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003978 if (retry_estale(error, lookup_flags)) {
3979 lookup_flags |= LOOKUP_REVAL;
3980 goto retry;
3981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 return error;
3983}
3984
Heiko Carstens3cdad422009-01-14 14:14:22 +01003985SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003986{
3987 return do_rmdir(AT_FDCWD, pathname);
3988}
3989
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003990/**
3991 * vfs_unlink - unlink a filesystem object
3992 * @dir: parent directory
3993 * @dentry: victim
3994 * @delegated_inode: returns victim inode, if the inode is delegated.
3995 *
3996 * The caller must hold dir->i_mutex.
3997 *
3998 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3999 * return a reference to the inode in delegated_inode. The caller
4000 * should then break the delegation on that inode and retry. Because
4001 * breaking a delegation may take a long time, the caller should drop
4002 * dir->i_mutex before doing so.
4003 *
4004 * Alternatively, a caller may pass NULL for delegated_inode. This may
4005 * be appropriate for callers that expect the underlying filesystem not
4006 * to be NFS exported.
4007 */
4008int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004010 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 int error = may_delete(dir, dentry, 0);
4012
4013 if (error)
4014 return error;
4015
Al Viroacfa4382008-12-04 10:06:33 -05004016 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return -EPERM;
4018
Al Viro59551022016-01-22 15:40:57 -05004019 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004020 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 error = -EBUSY;
4022 else {
4023 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004024 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004025 error = try_break_deleg(target, delegated_inode);
4026 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004029 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004030 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004031 detach_mounts(dentry);
Amir Goldstein116b9732019-05-26 17:34:02 +03004032 fsnotify_unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004033 }
Al Virobec10522010-03-03 14:12:08 -05004034 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004036out:
Al Viro59551022016-01-22 15:40:57 -05004037 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
4039 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4040 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004041 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 }
Robert Love0eeca282005-07-12 17:06:03 -04004044
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 return error;
4046}
Al Viro4d359502014-03-14 12:20:17 -04004047EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
4049/*
4050 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004051 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 * writeout happening, and we don't want to prevent access to the directory
4053 * while waiting on the I/O.
4054 */
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004055long do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
Al Viro2ad94ae2008-07-21 09:32:51 -04004057 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004059 struct path path;
4060 struct qstr last;
4061 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004063 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004064 unsigned int lookup_flags = 0;
4065retry:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004066 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004067 if (IS_ERR(name))
4068 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004069
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004071 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004073
Al Virof5beed72015-04-30 16:09:11 -04004074 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004075 if (error)
4076 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004077retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004078 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004079 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 error = PTR_ERR(dentry);
4081 if (!IS_ERR(dentry)) {
4082 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004083 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004084 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004086 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004087 goto slashes;
4088 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004089 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004090 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004091 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04004092 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004093exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 dput(dentry);
4095 }
Al Viro59551022016-01-22 15:40:57 -05004096 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 if (inode)
4098 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004099 inode = NULL;
4100 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004101 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004102 if (!error)
4103 goto retry_deleg;
4104 }
Al Virof5beed72015-04-30 16:09:11 -04004105 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106exit1:
Al Virof5beed72015-04-30 16:09:11 -04004107 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05004108 if (retry_estale(error, lookup_flags)) {
4109 lookup_flags |= LOOKUP_REVAL;
4110 inode = NULL;
4111 goto retry;
4112 }
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004113 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 return error;
4115
4116slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004117 if (d_is_negative(dentry))
4118 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004119 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004120 error = -EISDIR;
4121 else
4122 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 goto exit2;
4124}
4125
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004126SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004127{
4128 if ((flag & ~AT_REMOVEDIR) != 0)
4129 return -EINVAL;
4130
4131 if (flag & AT_REMOVEDIR)
4132 return do_rmdir(dfd, pathname);
4133
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004134 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004135}
4136
Heiko Carstens3480b252009-01-14 14:14:16 +01004137SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004138{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004139 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004140}
4141
Miklos Szeredidb2e7472008-06-24 16:50:16 +02004142int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
Miklos Szeredia95164d2008-07-30 15:08:48 +02004144 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146 if (error)
4147 return error;
4148
Al Viroacfa4382008-12-04 10:06:33 -05004149 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 return -EPERM;
4151
4152 error = security_inode_symlink(dir, dentry, oldname);
4153 if (error)
4154 return error;
4155
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004157 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004158 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 return error;
4160}
Al Viro4d359502014-03-14 12:20:17 -04004161EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162
Dominik Brodowskib724e842018-03-11 11:34:49 +01004163long do_symlinkat(const char __user *oldname, int newdfd,
4164 const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165{
Al Viro2ad94ae2008-07-21 09:32:51 -04004166 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004167 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004168 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004169 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004170 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171
4172 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004173 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004175retry:
4176 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004177 error = PTR_ERR(dentry);
4178 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004179 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004180
Jeff Layton91a27b22012-10-10 15:25:28 -04004181 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004182 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04004183 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004184 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004185 if (retry_estale(error, lookup_flags)) {
4186 lookup_flags |= LOOKUP_REVAL;
4187 goto retry;
4188 }
Dave Hansen6902d922006-09-30 23:29:01 -07004189out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 putname(from);
4191 return error;
4192}
4193
Dominik Brodowskib724e842018-03-11 11:34:49 +01004194SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4195 int, newdfd, const char __user *, newname)
4196{
4197 return do_symlinkat(oldname, newdfd, newname);
4198}
4199
Heiko Carstens3480b252009-01-14 14:14:16 +01004200SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004201{
Dominik Brodowskib724e842018-03-11 11:34:49 +01004202 return do_symlinkat(oldname, AT_FDCWD, newname);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004203}
4204
J. Bruce Fields146a8592011-09-20 17:14:31 -04004205/**
4206 * vfs_link - create a new link
4207 * @old_dentry: object to be linked
4208 * @dir: new parent
4209 * @new_dentry: where to create the new link
4210 * @delegated_inode: returns inode needing a delegation break
4211 *
4212 * The caller must hold dir->i_mutex
4213 *
4214 * If vfs_link discovers a delegation on the to-be-linked file in need
4215 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4216 * inode in delegated_inode. The caller should then break the delegation
4217 * and retry. Because breaking a delegation may take a long time, the
4218 * caller should drop the i_mutex before doing so.
4219 *
4220 * Alternatively, a caller may pass NULL for delegated_inode. This may
4221 * be appropriate for callers that expect the underlying filesystem not
4222 * to be NFS exported.
4223 */
4224int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004227 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 int error;
4229
4230 if (!inode)
4231 return -ENOENT;
4232
Miklos Szeredia95164d2008-07-30 15:08:48 +02004233 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 if (error)
4235 return error;
4236
4237 if (dir->i_sb != inode->i_sb)
4238 return -EXDEV;
4239
4240 /*
4241 * A link to an append-only or immutable file cannot be created.
4242 */
4243 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4244 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004245 /*
4246 * Updating the link count will likely cause i_uid and i_gid to
4247 * be writen back improperly if their true value is unknown to
4248 * the vfs.
4249 */
4250 if (HAS_UNMAPPED_ID(inode))
4251 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004252 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004254 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 return -EPERM;
4256
4257 error = security_inode_link(old_dentry, dir, new_dentry);
4258 if (error)
4259 return error;
4260
Al Viro59551022016-01-22 15:40:57 -05004261 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304262 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004263 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304264 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004265 else if (max_links && inode->i_nlink >= max_links)
4266 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004267 else {
4268 error = try_break_deleg(inode, delegated_inode);
4269 if (!error)
4270 error = dir->i_op->link(old_dentry, dir, new_dentry);
4271 }
Al Virof4e0c302013-06-11 08:34:36 +04004272
4273 if (!error && (inode->i_state & I_LINKABLE)) {
4274 spin_lock(&inode->i_lock);
4275 inode->i_state &= ~I_LINKABLE;
4276 spin_unlock(&inode->i_lock);
4277 }
Al Viro59551022016-01-22 15:40:57 -05004278 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004279 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004280 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 return error;
4282}
Al Viro4d359502014-03-14 12:20:17 -04004283EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285/*
4286 * Hardlinks are often used in delicate situations. We avoid
4287 * security-related surprises by not following symlinks on the
4288 * newname. --KAB
4289 *
4290 * We don't follow them on the oldname either to be compatible
4291 * with linux 2.0, and to avoid hard-linking to directories
4292 * and other special files. --ADM
4293 */
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004294int do_linkat(int olddfd, const char __user *oldname, int newdfd,
4295 const char __user *newname, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296{
4297 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004298 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004299 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304300 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304303 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004304 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304305 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004306 * To use null names we require CAP_DAC_READ_SEARCH
4307 * This ensures that not everyone will be able to create
4308 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304309 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004310 if (flags & AT_EMPTY_PATH) {
4311 if (!capable(CAP_DAC_READ_SEARCH))
4312 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304313 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004314 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004315
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304316 if (flags & AT_SYMLINK_FOLLOW)
4317 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004318retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304319 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004321 return error;
4322
Jeff Layton442e31c2012-12-20 16:15:38 -05004323 new_dentry = user_path_create(newdfd, newname, &new_path,
4324 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004326 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004327 goto out;
4328
4329 error = -EXDEV;
4330 if (old_path.mnt != new_path.mnt)
4331 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004332 error = may_linkat(&old_path);
4333 if (unlikely(error))
4334 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004335 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004336 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004337 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004338 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004339out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004340 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004341 if (delegated_inode) {
4342 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004343 if (!error) {
4344 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004345 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004346 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004347 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004348 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004349 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004350 how |= LOOKUP_REVAL;
4351 goto retry;
4352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353out:
Al Viro2d8f3032008-07-22 09:59:21 -04004354 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355
4356 return error;
4357}
4358
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004359SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4360 int, newdfd, const char __user *, newname, int, flags)
4361{
4362 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4363}
4364
Heiko Carstens3480b252009-01-14 14:14:16 +01004365SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004366{
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004367 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004368}
4369
Miklos Szeredibc270272014-04-01 17:08:42 +02004370/**
4371 * vfs_rename - rename a filesystem object
4372 * @old_dir: parent of source
4373 * @old_dentry: source
4374 * @new_dir: parent of destination
4375 * @new_dentry: destination
4376 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004377 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004378 *
4379 * The caller must hold multiple mutexes--see lock_rename()).
4380 *
4381 * If vfs_rename discovers a delegation in need of breaking at either
4382 * the source or destination, it will return -EWOULDBLOCK and return a
4383 * reference to the inode in delegated_inode. The caller should then
4384 * break the delegation and retry. Because breaking a delegation may
4385 * take a long time, the caller should drop all locks before doing
4386 * so.
4387 *
4388 * Alternatively, a caller may pass NULL for delegated_inode. This may
4389 * be appropriate for callers that expect the underlying filesystem not
4390 * to be NFS exported.
4391 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 * The worst of all namespace operations - renaming directory. "Perverted"
4393 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4394 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004395 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004396 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 * b) race potential - two innocent renames can create a loop together.
4398 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004399 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004401 * c) we have to lock _four_ objects - parents and victim (if it exists),
4402 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004403 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404 * whether the target exists). Solution: try to be smart with locking
4405 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004406 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 * move will be locked. Thus we can rank directories by the tree
4408 * (ancestors first) and rank all non-directories after them.
4409 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004410 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 * HOWEVER, it relies on the assumption that any object with ->lookup()
4412 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4413 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004414 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004415 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004417 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 * locking].
4419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004421 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004422 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423{
4424 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004425 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004426 struct inode *source = old_dentry->d_inode;
4427 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004428 bool new_is_dir = false;
4429 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004430 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004432 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004433 return 0;
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 error = may_delete(old_dir, old_dentry, is_dir);
4436 if (error)
4437 return error;
4438
Miklos Szeredida1ce062014-04-01 17:08:43 +02004439 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004440 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004441 } else {
4442 new_is_dir = d_is_dir(new_dentry);
4443
4444 if (!(flags & RENAME_EXCHANGE))
4445 error = may_delete(new_dir, new_dentry, is_dir);
4446 else
4447 error = may_delete(new_dir, new_dentry, new_is_dir);
4448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449 if (error)
4450 return error;
4451
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004452 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 return -EPERM;
4454
Miklos Szeredibc270272014-04-01 17:08:42 +02004455 /*
4456 * If we are going to change the parent - check write permissions,
4457 * we'll need to flip '..'.
4458 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004459 if (new_dir != old_dir) {
4460 if (is_dir) {
4461 error = inode_permission(source, MAY_WRITE);
4462 if (error)
4463 return error;
4464 }
4465 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4466 error = inode_permission(target, MAY_WRITE);
4467 if (error)
4468 return error;
4469 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004470 }
Robert Love0eeca282005-07-12 17:06:03 -04004471
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004472 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4473 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004474 if (error)
4475 return error;
4476
Al Viro49d31c22017-07-07 14:51:19 -04004477 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004478 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004479 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004480 lock_two_nondirectories(source, target);
4481 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004482 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004483
4484 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004485 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004486 goto out;
4487
Miklos Szeredida1ce062014-04-01 17:08:43 +02004488 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004489 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004490 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004491 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004492 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4493 old_dir->i_nlink >= max_links)
4494 goto out;
4495 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004496 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004497 error = try_break_deleg(source, delegated_inode);
4498 if (error)
4499 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004500 }
4501 if (target && !new_is_dir) {
4502 error = try_break_deleg(target, delegated_inode);
4503 if (error)
4504 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004505 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004506 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004507 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004508 if (error)
4509 goto out;
4510
Miklos Szeredida1ce062014-04-01 17:08:43 +02004511 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004512 if (is_dir) {
4513 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004514 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004515 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004516 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004517 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004518 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004519 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4520 if (!(flags & RENAME_EXCHANGE))
4521 d_move(old_dentry, new_dentry);
4522 else
4523 d_exchange(old_dentry, new_dentry);
4524 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004525out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004526 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004527 unlock_two_nondirectories(source, target);
4528 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004529 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004530 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004531 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004532 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004533 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4534 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004535 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004536 new_is_dir, NULL, new_dentry);
4537 }
4538 }
Al Viro49d31c22017-07-07 14:51:19 -04004539 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004540
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 return error;
4542}
Al Viro4d359502014-03-14 12:20:17 -04004543EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004545static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4546 const char __user *newname, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
Al Viro2ad94ae2008-07-21 09:32:51 -04004548 struct dentry *old_dentry, *new_dentry;
4549 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004550 struct path old_path, new_path;
4551 struct qstr old_last, new_last;
4552 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004553 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004554 struct filename *from;
4555 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004556 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004557 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004558 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004559
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004560 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004561 return -EINVAL;
4562
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004563 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4564 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004565 return -EINVAL;
4566
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004567 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4568 return -EPERM;
4569
Al Virof5beed72015-04-30 16:09:11 -04004570 if (flags & RENAME_EXCHANGE)
4571 target_flags = 0;
4572
Jeff Laytonc6a94282012-12-11 12:10:10 -05004573retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04004574 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4575 &old_path, &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004576 if (IS_ERR(from)) {
4577 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Al Viroc1d4dd22016-06-05 16:38:18 -04004581 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4582 &new_path, &new_last, &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004583 if (IS_ERR(to)) {
4584 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004589 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 goto exit2;
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004593 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 goto exit2;
4595
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004596 if (flags & RENAME_NOREPLACE)
4597 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004598 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 goto exit2;
4600
Al Virof5beed72015-04-30 16:09:11 -04004601 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004602 if (error)
4603 goto exit2;
4604
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004605retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004606 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607
Al Virof5beed72015-04-30 16:09:11 -04004608 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 error = PTR_ERR(old_dentry);
4610 if (IS_ERR(old_dentry))
4611 goto exit3;
4612 /* source must exist */
4613 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004614 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004616 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 error = PTR_ERR(new_dentry);
4618 if (IS_ERR(new_dentry))
4619 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004620 error = -EEXIST;
4621 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4622 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004623 if (flags & RENAME_EXCHANGE) {
4624 error = -ENOENT;
4625 if (d_is_negative(new_dentry))
4626 goto exit5;
4627
4628 if (!d_is_dir(new_dentry)) {
4629 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004630 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004631 goto exit5;
4632 }
4633 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004634 /* unless the source is a directory trailing slashes give -ENOTDIR */
4635 if (!d_is_dir(old_dentry)) {
4636 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004637 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004638 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004639 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004640 goto exit5;
4641 }
4642 /* source should not be ancestor of target */
4643 error = -EINVAL;
4644 if (old_dentry == trap)
4645 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004647 if (!(flags & RENAME_EXCHANGE))
4648 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 if (new_dentry == trap)
4650 goto exit5;
4651
Al Virof5beed72015-04-30 16:09:11 -04004652 error = security_path_rename(&old_path, old_dentry,
4653 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004654 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004655 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004656 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4657 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004658 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659exit5:
4660 dput(new_dentry);
4661exit4:
4662 dput(old_dentry);
4663exit3:
Al Virof5beed72015-04-30 16:09:11 -04004664 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004665 if (delegated_inode) {
4666 error = break_deleg_wait(&delegated_inode);
4667 if (!error)
4668 goto retry_deleg;
4669 }
Al Virof5beed72015-04-30 16:09:11 -04004670 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004672 if (retry_estale(error, lookup_flags))
4673 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004674 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004675 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676exit1:
Al Virof5beed72015-04-30 16:09:11 -04004677 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004679 if (should_retry) {
4680 should_retry = false;
4681 lookup_flags |= LOOKUP_REVAL;
4682 goto retry;
4683 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004684exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 return error;
4686}
4687
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004688SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4689 int, newdfd, const char __user *, newname, unsigned int, flags)
4690{
4691 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4692}
4693
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004694SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4695 int, newdfd, const char __user *, newname)
4696{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004697 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004698}
4699
Heiko Carstensa26eab22009-01-14 14:14:17 +01004700SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004701{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004702 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004703}
4704
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004705int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4706{
4707 int error = may_create(dir, dentry);
4708 if (error)
4709 return error;
4710
4711 if (!dir->i_op->mknod)
4712 return -EPERM;
4713
4714 return dir->i_op->mknod(dir, dentry,
4715 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4716}
4717EXPORT_SYMBOL(vfs_whiteout);
4718
Al Viro5d826c82014-03-14 13:42:45 -04004719int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720{
Al Viro5d826c82014-03-14 13:42:45 -04004721 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 if (IS_ERR(link))
4723 goto out;
4724
4725 len = strlen(link);
4726 if (len > (unsigned) buflen)
4727 len = buflen;
4728 if (copy_to_user(buffer, link, len))
4729 len = -EFAULT;
4730out:
4731 return len;
4732}
4733
Miklos Szeredid60874c2016-10-04 14:40:45 +02004734/**
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004735 * vfs_readlink - copy symlink body into userspace buffer
4736 * @dentry: dentry on which to get symbolic link
4737 * @buffer: user memory pointer
4738 * @buflen: size of buffer
4739 *
4740 * Does not touch atime. That's up to the caller if necessary
4741 *
4742 * Does not call security hook.
4743 */
4744int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4745{
4746 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004747 DEFINE_DELAYED_CALL(done);
4748 const char *link;
4749 int res;
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004750
Miklos Szeredi76fca902016-12-09 16:45:04 +01004751 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4752 if (unlikely(inode->i_op->readlink))
4753 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004754
Miklos Szeredi76fca902016-12-09 16:45:04 +01004755 if (!d_is_symlink(dentry))
4756 return -EINVAL;
4757
4758 spin_lock(&inode->i_lock);
4759 inode->i_opflags |= IOP_DEFAULT_READLINK;
4760 spin_unlock(&inode->i_lock);
4761 }
4762
Eric Biggers4c4f7c12019-04-10 13:21:14 -07004763 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04004764 if (!link) {
4765 link = inode->i_op->get_link(dentry, inode, &done);
4766 if (IS_ERR(link))
4767 return PTR_ERR(link);
4768 }
4769 res = readlink_copy(buffer, buflen, link);
4770 do_delayed_call(&done);
4771 return res;
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004772}
4773EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Miklos Szeredid60874c2016-10-04 14:40:45 +02004775/**
4776 * vfs_get_link - get symlink body
4777 * @dentry: dentry on which to get symbolic link
4778 * @done: caller needs to free returned data with this
4779 *
4780 * Calls security hook and i_op->get_link() on the supplied inode.
4781 *
4782 * It does not touch atime. That's up to the caller if necessary.
4783 *
4784 * Does not work on "special" symlinks like /proc/$$/fd/N
4785 */
4786const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4787{
4788 const char *res = ERR_PTR(-EINVAL);
4789 struct inode *inode = d_inode(dentry);
4790
4791 if (d_is_symlink(dentry)) {
4792 res = ERR_PTR(security_inode_readlink(dentry));
4793 if (!res)
4794 res = inode->i_op->get_link(dentry, inode, done);
4795 }
4796 return res;
4797}
4798EXPORT_SYMBOL(vfs_get_link);
4799
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004801const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004802 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004804 char *kaddr;
4805 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004806 struct address_space *mapping = inode->i_mapping;
4807
Al Virod3883d42015-11-17 10:41:04 -05004808 if (!dentry) {
4809 page = find_get_page(mapping, 0);
4810 if (!page)
4811 return ERR_PTR(-ECHILD);
4812 if (!PageUptodate(page)) {
4813 put_page(page);
4814 return ERR_PTR(-ECHILD);
4815 }
4816 } else {
4817 page = read_mapping_page(mapping, 0, NULL);
4818 if (IS_ERR(page))
4819 return (char*)page;
4820 }
Al Virofceef392015-12-29 15:58:39 -05004821 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004822 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4823 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004824 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004825 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
Al Viro6b255392015-11-17 10:20:54 -05004828EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829
Al Virofceef392015-12-29 15:58:39 -05004830void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Al Virofceef392015-12-29 15:58:39 -05004832 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833}
Al Viro4d359502014-03-14 12:20:17 -04004834EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835
Al Viroaa80dea2015-11-16 18:26:34 -05004836int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4837{
Al Virofceef392015-12-29 15:58:39 -05004838 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004839 int res = readlink_copy(buffer, buflen,
4840 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004841 &done));
4842 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004843 return res;
4844}
4845EXPORT_SYMBOL(page_readlink);
4846
Nick Piggin54566b22009-01-04 12:00:53 -08004847/*
4848 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4849 */
4850int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
4852 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004853 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004854 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004855 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004856 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004857 if (nofs)
4858 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
NeilBrown7e53cac2006-03-25 03:07:57 -08004860retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004861 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004862 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004864 goto fail;
4865
Al Viro21fc61c2015-11-17 01:07:57 -05004866 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004867
4868 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4869 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 if (err < 0)
4871 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004872 if (err < len-1)
4873 goto retry;
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 mark_inode_dirty(inode);
4876 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004877fail:
4878 return err;
4879}
Al Viro4d359502014-03-14 12:20:17 -04004880EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004882int page_symlink(struct inode *inode, const char *symname, int len)
4883{
4884 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004885 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004886}
Al Viro4d359502014-03-14 12:20:17 -04004887EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004888
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004889const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004890 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892EXPORT_SYMBOL(page_symlink_inode_operations);