blob: 342bc7d4cbc7ab4f597d85226b17d8cfca5b5e6c [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
Joel Fernandes1304fea2019-03-14 17:32:39 -040046#define CREATE_TRACE_POINTS
47#include <trace/events/namei.h>
48
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/* [Feb-1997 T. Schoebel-Theuer]
50 * Fundamental changes in the pathname lookup mechanisms (namei)
51 * were necessary because of omirr. The reason is that omirr needs
52 * to know the _real_ pathname, not the user-supplied one, in case
53 * of symlinks (and also when transname replacements occur).
54 *
55 * The new code replaces the old recursive symlink resolution with
56 * an iterative one (in case of non-nested symlink chains). It does
57 * this with calls to <fs>_follow_link().
58 * As a side effect, dir_namei(), _namei() and follow_link() are now
59 * replaced with a single function lookup_dentry() that can handle all
60 * the special cases of the former code.
61 *
62 * With the new dcache, the pathname is stored at each inode, at least as
63 * long as the refcount of the inode is positive. As a side effect, the
64 * size of the dcache depends on the inode cache and thus is dynamic.
65 *
66 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
67 * resolution to correspond with current state of the code.
68 *
69 * Note that the symlink resolution is not *completely* iterative.
70 * There is still a significant amount of tail- and mid- recursion in
71 * the algorithm. Also, note that <fs>_readlink() is not used in
72 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
73 * may return different results than <fs>_follow_link(). Many virtual
74 * filesystems (including /proc) exhibit this behavior.
75 */
76
77/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
78 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
79 * and the name already exists in form of a symlink, try to create the new
80 * name indicated by the symlink. The old code always complained that the
81 * name already exists, due to not following the symlink even if its target
82 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030083 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *
85 * I don't know which semantics is the right one, since I have no access
86 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
87 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
88 * "old" one. Personally, I think the new semantics is much more logical.
89 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
90 * file does succeed in both HP-UX and SunOs, but not in Solaris
91 * and in the old Linux semantics.
92 */
93
94/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
95 * semantics. See the comments in "open_namei" and "do_link" below.
96 *
97 * [10-Sep-98 Alan Modra] Another symlink change.
98 */
99
100/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
101 * inside the path - always follow.
102 * in the last component in creation/removal/renaming - never follow.
103 * if LOOKUP_FOLLOW passed - follow.
104 * if the pathname has trailing slashes - follow.
105 * otherwise - don't follow.
106 * (applied in that order).
107 *
108 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
109 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
110 * During the 2.4 we need to fix the userland stuff depending on it -
111 * hopefully we will be able to get rid of that wart in 2.5. So far only
112 * XEmacs seems to be relying on it...
113 */
114/*
115 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800116 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * any extra contention...
118 */
119
120/* In order to reduce some races, while at the same time doing additional
121 * checking and hopefully speeding things up, we copy filenames to the
122 * kernel data space before using them..
123 *
124 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
125 * PATH_MAX includes the nul terminator --RR.
126 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400127
Al Virofd2f7cb2015-02-22 20:07:13 -0500128#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400129
David Drysdale51f39a12014-12-12 16:57:29 -0800130struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400131getname_flags(const char __user *filename, int flags, int *empty)
132{
Al Viro94b5d262015-02-22 19:38:03 -0500133 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400134 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500135 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Jeff Layton7ac86262012-10-10 15:25:28 -0400137 result = audit_reusename(filename);
138 if (result)
139 return result;
140
Jeff Layton7950e382012-10-10 16:43:13 -0400141 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700142 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500143 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Jeff Layton7950e382012-10-10 16:43:13 -0400145 /*
146 * First, try to embed the struct filename inside the names_cache
147 * allocation
148 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500149 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400150 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400151
Al Viro94b5d262015-02-22 19:38:03 -0500152 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400153 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500154 __putname(result);
155 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400156 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700157
Jeff Layton7950e382012-10-10 16:43:13 -0400158 /*
159 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
160 * separate struct filename so we can dedicate the entire
161 * names_cache allocation for the pathname, and re-do the copy from
162 * userland.
163 */
Al Viro94b5d262015-02-22 19:38:03 -0500164 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500165 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400166 kname = (char *)result;
167
Al Virofd2f7cb2015-02-22 20:07:13 -0500168 /*
169 * size is chosen that way we to guarantee that
170 * result->iname[0] is within the same object and that
171 * kname can't be equal to result->iname, no matter what.
172 */
173 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500174 if (unlikely(!result)) {
175 __putname(kname);
176 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400177 }
178 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500179 len = strncpy_from_user(kname, filename, PATH_MAX);
180 if (unlikely(len < 0)) {
181 __putname(kname);
182 kfree(result);
183 return ERR_PTR(len);
184 }
185 if (unlikely(len == PATH_MAX)) {
186 __putname(kname);
187 kfree(result);
188 return ERR_PTR(-ENAMETOOLONG);
189 }
Jeff Layton7950e382012-10-10 16:43:13 -0400190 }
191
Al Viro94b5d262015-02-22 19:38:03 -0500192 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700193 /* The empty path is special. */
194 if (unlikely(!len)) {
195 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500196 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500197 if (!(flags & LOOKUP_EMPTY)) {
198 putname(result);
199 return ERR_PTR(-ENOENT);
200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700202
Jeff Layton7950e382012-10-10 16:43:13 -0400203 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800204 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400205 audit_getname(result);
206 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Jeff Layton91a27b22012-10-10 15:25:28 -0400209struct filename *
210getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400211{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700212 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400213}
214
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800215struct filename *
216getname_kernel(const char * filename)
217{
218 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500219 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800220
221 result = __getname();
222 if (unlikely(!result))
223 return ERR_PTR(-ENOMEM);
224
Paul Moore08518542015-01-21 23:59:56 -0500225 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500226 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500227 } else if (len <= PATH_MAX) {
Al Viro30ce4d12018-04-08 11:57:10 -0400228 const size_t size = offsetof(struct filename, iname[1]);
Paul Moore08518542015-01-21 23:59:56 -0500229 struct filename *tmp;
230
Al Viro30ce4d12018-04-08 11:57:10 -0400231 tmp = kmalloc(size, GFP_KERNEL);
Paul Moore08518542015-01-21 23:59:56 -0500232 if (unlikely(!tmp)) {
233 __putname(result);
234 return ERR_PTR(-ENOMEM);
235 }
236 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500237 result = tmp;
238 } else {
239 __putname(result);
240 return ERR_PTR(-ENAMETOOLONG);
241 }
242 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800243 result->uptr = NULL;
244 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500245 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500246 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800247
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800248 return result;
249}
250
Jeff Layton91a27b22012-10-10 15:25:28 -0400251void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Paul Moore55422d02015-01-22 00:00:23 -0500253 BUG_ON(name->refcnt <= 0);
254
255 if (--name->refcnt > 0)
256 return;
257
Al Virofd2f7cb2015-02-22 20:07:13 -0500258 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500259 __putname(name->name);
260 kfree(name);
261 } else
262 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Linus Torvaldse77819e2011-07-22 19:30:19 -0700265static int check_acl(struct inode *inode, int mask)
266{
Linus Torvalds84635d62011-07-25 22:47:03 -0700267#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700268 struct posix_acl *acl;
269
Linus Torvaldse77819e2011-07-22 19:30:19 -0700270 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400271 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
272 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700273 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400274 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100275 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400276 return -ECHILD;
Linus Torvalds63d72b92020-06-07 12:19:06 -0700277 return posix_acl_permission(inode, acl, mask);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700278 }
279
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800280 acl = get_acl(inode, ACL_TYPE_ACCESS);
281 if (IS_ERR(acl))
282 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700283 if (acl) {
284 int error = posix_acl_permission(inode, acl, mask);
285 posix_acl_release(acl);
286 return error;
287 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700288#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700289
290 return -EAGAIN;
291}
292
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700293/*
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700294 * This does the basic UNIX permission checking.
295 *
296 * Note that the POSIX ACL check cares about the MAY_NOT_BLOCK bit,
297 * for RCU walking.
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700298 */
Al Viro7e401452011-06-20 19:12:17 -0400299static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700300{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700301 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700302
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700303 /* Are we the owner? If so, ACL's don't matter */
304 if (likely(uid_eq(current_fsuid(), inode->i_uid))) {
305 mask &= 7;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700306 mode >>= 6;
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700307 return (mask & ~mode) ? -EACCES : 0;
308 }
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700309
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700310 /* Do we have ACL's? */
311 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
312 int error = check_acl(inode, mask);
313 if (error != -EAGAIN)
314 return error;
315 }
316
317 /* Only RWX matters for group/other mode bits */
318 mask &= 7;
319
320 /*
321 * Are the group permissions different from
322 * the other permissions in the bits we care
323 * about? Need to check group ownership if so.
324 */
325 if (mask & (mode ^ (mode >> 3))) {
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700326 if (in_group_p(inode->i_gid))
327 mode >>= 3;
328 }
329
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700330 /* Bits in 'mode' clear that we require? */
331 return (mask & ~mode) ? -EACCES : 0;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100335 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 * @inode: inode to check access rights for
Linus Torvalds5fc475b2020-06-05 13:40:45 -0700337 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC,
338 * %MAY_NOT_BLOCK ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 *
340 * Used to check for read/write/execute permissions on a file.
341 * We use "fsuid" for this, letting us set arbitrary permissions
342 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100343 * are used for other things.
344 *
345 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
346 * request cannot be satisfied (eg. requires blocking or too much complexity).
347 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 */
Al Viro2830ba72011-06-20 19:16:29 -0400349int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700351 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530354 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Al Viro7e401452011-06-20 19:12:17 -0400356 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700357 if (ret != -EACCES)
358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Al Virod594e7e2011-06-20 19:55:42 -0400360 if (S_ISDIR(inode->i_mode)) {
361 /* DACs are overridable for directories */
Al Virod594e7e2011-06-20 19:55:42 -0400362 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700363 if (capable_wrt_inode_uidgid(inode,
364 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400365 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700366 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500368 return -EACCES;
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 /*
372 * Searching includes executable on directories, else just read.
373 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600374 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400375 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700376 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return 0;
Stephen Smalley2a4c2242017-03-10 12:14:18 -0500378 /*
379 * Read/write DACs are always overridable.
380 * Executable DACs are overridable when there is
381 * at least one exec bit set.
382 */
383 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
384 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387 return -EACCES;
388}
Al Viro4d359502014-03-14 12:20:17 -0400389EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700391/*
392 * We _really_ want to just do "generic_permission()" without
393 * even looking at the inode->i_op values. So we keep a cache
394 * flag in inode->i_opflags, that says "this has not special
395 * permission function, use the fast case".
396 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +0200397static inline int do_inode_permission(struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700398{
399 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
400 if (likely(inode->i_op->permission))
401 return inode->i_op->permission(inode, mask);
402
403 /* This gets set once for the inode lifetime */
404 spin_lock(&inode->i_lock);
405 inode->i_opflags |= IOP_FASTPERM;
406 spin_unlock(&inode->i_lock);
407 }
408 return generic_permission(inode, mask);
409}
410
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200411/**
David Howells0bdaea92012-06-25 12:55:46 +0100412 * sb_permission - Check superblock-level permissions
413 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700414 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100415 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
416 *
417 * Separate out file-system wide checks from inode-specific permission checks.
418 */
419static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
420{
421 if (unlikely(mask & MAY_WRITE)) {
422 umode_t mode = inode->i_mode;
423
424 /* Nobody gets write access to a read-only fs. */
David Howellsbc98a422017-07-17 08:45:34 +0100425 if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
David Howells0bdaea92012-06-25 12:55:46 +0100426 return -EROFS;
427 }
428 return 0;
429}
430
431/**
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +0200432 * inode_permission - Check for access rights to a given inode
David Howells0bdaea92012-06-25 12:55:46 +0100433 * @inode: Inode to check permission on
434 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
435 *
436 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
437 * this, letting us set arbitrary permissions for filesystem access without
438 * changing the "normal" UIDs which are used for other things.
439 *
440 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
441 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +0200442int inode_permission(struct inode *inode, int mask)
David Howells0bdaea92012-06-25 12:55:46 +0100443{
444 int retval;
445
446 retval = sb_permission(inode->i_sb, inode, mask);
447 if (retval)
448 return retval;
Eric Biggers4bfd0542018-01-16 21:44:24 -0800449
450 if (unlikely(mask & MAY_WRITE)) {
451 /*
452 * Nobody gets write access to an immutable file.
453 */
454 if (IS_IMMUTABLE(inode))
455 return -EPERM;
456
457 /*
458 * Updating mtime will likely cause i_uid and i_gid to be
459 * written back improperly if their true value is unknown
460 * to the vfs.
461 */
462 if (HAS_UNMAPPED_ID(inode))
463 return -EACCES;
464 }
465
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +0200466 retval = do_inode_permission(inode, mask);
Eric Biggers4bfd0542018-01-16 21:44:24 -0800467 if (retval)
468 return retval;
469
470 retval = devcgroup_inode_permission(inode, mask);
471 if (retval)
472 return retval;
473
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +0200474 return security_inode_permission(inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100475}
Al Viro4d359502014-03-14 12:20:17 -0400476EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100477
478/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800479 * path_get - get a reference to a path
480 * @path: path to get the reference to
481 *
482 * Given a path increment the reference count to the dentry and the vfsmount.
483 */
Al Virodcf787f2013-03-01 23:51:07 -0500484void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800485{
486 mntget(path->mnt);
487 dget(path->dentry);
488}
489EXPORT_SYMBOL(path_get);
490
491/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800492 * path_put - put a reference to a path
493 * @path: path to put the reference to
494 *
495 * Given a path decrement the reference count to the dentry and the vfsmount.
496 */
Al Virodcf787f2013-03-01 23:51:07 -0500497void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Jan Blunck1d957f92008-02-14 19:34:35 -0800499 dput(path->dentry);
500 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Jan Blunck1d957f92008-02-14 19:34:35 -0800502EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Al Viro894bc8c2015-05-02 07:16:16 -0400504#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400505struct nameidata {
506 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400507 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400508 struct path root;
509 struct inode *inode; /* path.dentry.d_inode */
510 unsigned int flags;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +1100511 unsigned seq, m_seq, r_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400512 int last_type;
513 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100514 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400515 struct saved {
516 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500517 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400518 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400519 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400520 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400521 struct filename *name;
522 struct nameidata *saved;
523 unsigned root_seq;
524 int dfd;
Al Viro0f705952020-03-05 11:34:48 -0500525 kuid_t dir_uid;
526 umode_t dir_mode;
Kees Cook3859a272016-10-28 01:22:25 -0700527} __randomize_layout;
Al Viro1f55a6e2014-11-01 19:30:41 -0400528
Al Viro9883d182015-05-13 07:28:08 -0400529static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400530{
NeilBrown756daf22015-03-23 13:37:38 +1100531 struct nameidata *old = current->nameidata;
532 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400533 p->dfd = dfd;
534 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100535 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400536 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100537 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400538}
539
Al Viro9883d182015-05-13 07:28:08 -0400540static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400541{
Al Viro9883d182015-05-13 07:28:08 -0400542 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100543
544 current->nameidata = old;
545 if (old)
546 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500547 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100548 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400549}
550
Al Viro60ef60c2020-03-03 11:43:55 -0500551static bool nd_alloc_stack(struct nameidata *nd)
Al Viro894bc8c2015-05-02 07:16:16 -0400552{
Al Virobc40aee2015-05-09 13:04:24 -0400553 struct saved *p;
554
Al Viro60ef60c2020-03-03 11:43:55 -0500555 p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved),
556 nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL);
557 if (unlikely(!p))
558 return false;
Al Viro894bc8c2015-05-02 07:16:16 -0400559 memcpy(p, nd->internal, sizeof(nd->internal));
560 nd->stack = p;
Al Viro60ef60c2020-03-03 11:43:55 -0500561 return true;
Al Viro894bc8c2015-05-02 07:16:16 -0400562}
563
Eric W. Biederman397d4252015-08-15 20:27:13 -0500564/**
Al Viro6b03f7e2020-02-24 15:53:19 -0500565 * path_connected - Verify that a dentry is below mnt.mnt_root
Eric W. Biederman397d4252015-08-15 20:27:13 -0500566 *
567 * Rename can sometimes move a file or directory outside of a bind
568 * mount, path_connected allows those cases to be detected.
569 */
Al Viro6b03f7e2020-02-24 15:53:19 -0500570static bool path_connected(struct vfsmount *mnt, struct dentry *dentry)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500571{
Eric W. Biederman95dd7752018-03-14 18:20:29 -0500572 struct super_block *sb = mnt->mnt_sb;
Eric W. Biederman397d4252015-08-15 20:27:13 -0500573
Christoph Hellwig402dd2c2020-09-24 08:51:28 +0200574 /* Bind mounts can have disconnected paths */
575 if (mnt->mnt_root == sb->s_root)
Eric W. Biederman397d4252015-08-15 20:27:13 -0500576 return true;
577
Al Viro6b03f7e2020-02-24 15:53:19 -0500578 return is_subdir(dentry, mnt->mnt_root);
Al Viro894bc8c2015-05-02 07:16:16 -0400579}
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 */
Al Viro2aa38472020-02-26 19:19:05 -0500611static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq)
Al Viro79733872015-05-09 12:55:43 -0400612{
Al Viro2aa38472020-02-26 19:19:05 -0500613 int res = __legitimize_mnt(path->mnt, mseq);
Al Viro79733872015-05-09 12:55:43 -0400614 if (unlikely(res)) {
615 if (res > 0)
616 path->mnt = NULL;
617 path->dentry = NULL;
618 return false;
619 }
620 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
621 path->dentry = NULL;
622 return false;
623 }
624 return !read_seqcount_retry(&path->dentry->d_seq, seq);
625}
626
Al Viro2aa38472020-02-26 19:19:05 -0500627static inline bool legitimize_path(struct nameidata *nd,
628 struct path *path, unsigned seq)
629{
Al Viro5bd73282020-04-05 21:59:55 -0400630 return __legitimize_path(path, seq, nd->m_seq);
Al Viro2aa38472020-02-26 19:19:05 -0500631}
632
Al Viro79733872015-05-09 12:55:43 -0400633static bool legitimize_links(struct nameidata *nd)
634{
635 int i;
636 for (i = 0; i < nd->depth; i++) {
637 struct saved *last = nd->stack + i;
638 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
639 drop_links(nd);
640 nd->depth = i + 1;
641 return false;
642 }
643 }
644 return true;
645}
646
Al Viroee594bf2019-07-16 21:05:36 -0400647static bool legitimize_root(struct nameidata *nd)
648{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100649 /*
650 * For scoped-lookups (where nd->root has been zeroed), we need to
651 * restart the whole lookup from scratch -- because set_root() is wrong
652 * for these lookups (nd->dfd is the root, not the filesystem root).
653 */
654 if (!nd->root.mnt && (nd->flags & LOOKUP_IS_SCOPED))
655 return false;
656 /* Nothing to do if nd->root is zero or is managed by the VFS user. */
Al Viroee594bf2019-07-16 21:05:36 -0400657 if (!nd->root.mnt || (nd->flags & LOOKUP_ROOT))
658 return true;
Al Viro84a2bd32019-07-16 21:20:17 -0400659 nd->flags |= LOOKUP_ROOT_GRABBED;
Al Viroee594bf2019-07-16 21:05:36 -0400660 return legitimize_path(nd, &nd->root, nd->root_seq);
661}
662
Al Viro19660af2011-03-25 10:32:48 -0400663/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100664 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400665 * Documentation/filesystems/path-lookup.txt). In situations when we can't
666 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500667 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400668 * mode. Refcounts are grabbed at the last known good point before rcu-walk
669 * got stuck, so ref-walk may continue from there. If this is not successful
670 * (eg. a seqcount has changed), then failure is returned and it's up to caller
671 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100672 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100673
674/**
Jens Axboe01fd84a2020-12-17 09:19:08 -0700675 * try_to_unlazy - try to switch to ref-walk mode.
Al Viro19660af2011-03-25 10:32:48 -0400676 * @nd: nameidata pathwalk data
Jens Axboe01fd84a2020-12-17 09:19:08 -0700677 * Returns: true on success, false on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 *
Jens Axboe01fd84a2020-12-17 09:19:08 -0700679 * try_to_unlazy attempts to legitimize the current nd->path and nd->root
Al Viro4675ac32017-01-09 22:29:15 -0500680 * for ref-walk mode.
681 * Must be called from rcu-walk context.
Jens Axboe01fd84a2020-12-17 09:19:08 -0700682 * Nothing should touch nameidata between try_to_unlazy() failure and
Al Viro79733872015-05-09 12:55:43 -0400683 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100684 */
Jens Axboe01fd84a2020-12-17 09:19:08 -0700685static bool try_to_unlazy(struct nameidata *nd)
Nick Piggin31e6b012011-01-07 17:49:52 +1100686{
Nick Piggin31e6b012011-01-07 17:49:52 +1100687 struct dentry *parent = nd->path.dentry;
688
689 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700690
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700691 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400692 if (unlikely(!legitimize_links(nd)))
Al Viro4675ac32017-01-09 22:29:15 -0500693 goto out1;
Al Viro84a2bd32019-07-16 21:20:17 -0400694 if (unlikely(!legitimize_path(nd, &nd->path, nd->seq)))
695 goto out;
Al Viroee594bf2019-07-16 21:05:36 -0400696 if (unlikely(!legitimize_root(nd)))
697 goto out;
Al Viro4675ac32017-01-09 22:29:15 -0500698 rcu_read_unlock();
699 BUG_ON(nd->inode != parent->d_inode);
Jens Axboe01fd84a2020-12-17 09:19:08 -0700700 return true;
Al Viro4675ac32017-01-09 22:29:15 -0500701
Al Viro84a2bd32019-07-16 21:20:17 -0400702out1:
Al Viro4675ac32017-01-09 22:29:15 -0500703 nd->path.mnt = NULL;
704 nd->path.dentry = NULL;
Al Viro4675ac32017-01-09 22:29:15 -0500705out:
706 rcu_read_unlock();
Jens Axboe01fd84a2020-12-17 09:19:08 -0700707 return false;
Al Viro4675ac32017-01-09 22:29:15 -0500708}
709
710/**
711 * unlazy_child - try to switch to ref-walk mode.
712 * @nd: nameidata pathwalk data
713 * @dentry: child of nd->path.dentry
714 * @seq: seq number to check dentry against
715 * Returns: 0 on success, -ECHILD on failure
716 *
717 * unlazy_child attempts to legitimize the current nd->path, nd->root and dentry
718 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
719 * @nd. Must be called from rcu-walk context.
720 * Nothing should touch nameidata between unlazy_child() failure and
721 * terminate_walk().
722 */
723static int unlazy_child(struct nameidata *nd, struct dentry *dentry, unsigned seq)
724{
725 BUG_ON(!(nd->flags & LOOKUP_RCU));
726
727 nd->flags &= ~LOOKUP_RCU;
728 if (unlikely(!legitimize_links(nd)))
729 goto out2;
Al Viro79733872015-05-09 12:55:43 -0400730 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
731 goto out2;
Al Viro4675ac32017-01-09 22:29:15 -0500732 if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref)))
Al Viro79733872015-05-09 12:55:43 -0400733 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400734
Linus Torvalds15570082013-09-02 11:38:06 -0700735 /*
Al Viro4675ac32017-01-09 22:29:15 -0500736 * We need to move both the parent and the dentry from the RCU domain
737 * to be properly refcounted. And the sequence number in the dentry
738 * validates *both* dentry counters, since we checked the sequence
739 * number of the parent after we got the child sequence number. So we
740 * know the parent must still be valid if the child sequence number is
Linus Torvalds15570082013-09-02 11:38:06 -0700741 */
Al Viro4675ac32017-01-09 22:29:15 -0500742 if (unlikely(!lockref_get_not_dead(&dentry->d_lockref)))
743 goto out;
Al Viro84a2bd32019-07-16 21:20:17 -0400744 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
745 goto out_dput;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700746 /*
747 * Sequence counts matched. Now make sure that the root is
748 * still valid and get it if required.
749 */
Al Viro84a2bd32019-07-16 21:20:17 -0400750 if (unlikely(!legitimize_root(nd)))
751 goto out_dput;
Al Viro8b61e742013-11-08 12:45:01 -0500752 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100753 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400754
Al Viro79733872015-05-09 12:55:43 -0400755out2:
756 nd->path.mnt = NULL;
757out1:
758 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700759out:
Al Viro8b61e742013-11-08 12:45:01 -0500760 rcu_read_unlock();
Al Viro84a2bd32019-07-16 21:20:17 -0400761 return -ECHILD;
762out_dput:
763 rcu_read_unlock();
764 dput(dentry);
Nick Piggin31e6b012011-01-07 17:49:52 +1100765 return -ECHILD;
766}
767
Al Viro4ce16ef32012-06-10 16:10:59 -0400768static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100769{
Al Viroa89f8332017-01-09 22:25:28 -0500770 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
771 return dentry->d_op->d_revalidate(dentry, flags);
772 else
773 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100774}
775
Joel Fernandes1304fea2019-03-14 17:32:39 -0400776#define INIT_PATH_SIZE 64
777
778static void success_walk_trace(struct nameidata *nd)
779{
780 struct path *pt = &nd->path;
781 struct inode *i = nd->inode;
782 char buf[INIT_PATH_SIZE], *try_buf;
783 int cur_path_size;
784 char *p;
785
786 /* When eBPF/ tracepoint is disabled, keep overhead low. */
787 if (!trace_inodepath_enabled())
788 return;
789
790 /* First try stack allocated buffer. */
791 try_buf = buf;
792 cur_path_size = INIT_PATH_SIZE;
793
794 while (cur_path_size <= PATH_MAX) {
795 /* Free previous heap allocation if we are now trying
796 * a second or later heap allocation.
797 */
798 if (try_buf != buf)
799 kfree(try_buf);
800
801 /* All but the first alloc are on the heap. */
802 if (cur_path_size != INIT_PATH_SIZE) {
803 try_buf = kmalloc(cur_path_size, GFP_KERNEL);
804 if (!try_buf) {
805 try_buf = buf;
806 sprintf(try_buf, "error:buf_alloc_failed");
807 break;
808 }
809 }
810
811 p = d_path(pt, try_buf, cur_path_size);
812
813 if (!IS_ERR(p)) {
814 char *end = mangle_path(try_buf, p, "\n");
815
816 if (end) {
817 try_buf[end - try_buf] = 0;
818 break;
819 } else {
820 /* On mangle errors, double path size
821 * till PATH_MAX.
822 */
823 cur_path_size = cur_path_size << 1;
824 continue;
825 }
826 }
827
828 if (PTR_ERR(p) == -ENAMETOOLONG) {
829 /* If d_path complains that name is too long,
830 * then double path size till PATH_MAX.
831 */
832 cur_path_size = cur_path_size << 1;
833 continue;
834 }
835
836 sprintf(try_buf, "error:d_path_failed_%lu",
837 -1 * PTR_ERR(p));
838 break;
839 }
840
841 if (cur_path_size > PATH_MAX)
842 sprintf(try_buf, "error:d_path_name_too_long");
843
844 trace_inodepath(i, try_buf);
845
846 if (try_buf != buf)
847 kfree(try_buf);
848 return;
849}
850
Al Viro9f1fafe2011-03-25 11:00:12 -0400851/**
852 * complete_walk - successful completion of path walk
853 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500854 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400855 * If we had been in RCU mode, drop out of it and legitimize nd->path.
856 * Revalidate the final result, unless we'd already done that during
857 * the path walk or the filesystem doesn't ask for it. Return 0 on
858 * success, -error on failure. In case of failure caller does not
859 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500860 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400861static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500862{
Al Viro16c2cd72011-02-22 15:50:10 -0500863 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500864 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500865
Al Viro9f1fafe2011-03-25 11:00:12 -0400866 if (nd->flags & LOOKUP_RCU) {
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100867 /*
868 * We don't want to zero nd->root for scoped-lookups or
869 * externally-managed nd->root.
870 */
871 if (!(nd->flags & (LOOKUP_ROOT | LOOKUP_IS_SCOPED)))
Al Viro9f1fafe2011-03-25 11:00:12 -0400872 nd->root.mnt = NULL;
Jens Axboe01fd84a2020-12-17 09:19:08 -0700873 if (!try_to_unlazy(nd))
Al Viro48a066e2013-09-29 22:06:07 -0400874 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400875 }
876
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100877 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
878 /*
879 * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't
880 * ever step outside the root during lookup" and should already
881 * be guaranteed by the rest of namei, we want to avoid a namei
882 * BUG resulting in userspace being given a path that was not
883 * scoped within the root at some point during the lookup.
884 *
885 * So, do a final sanity-check to make sure that in the
886 * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED)
887 * we won't silently return an fd completely outside of the
888 * requested root to userspace.
889 *
890 * Userspace could move the path outside the root after this
891 * check, but as discussed elsewhere this is not a concern (the
892 * resolved file was inside the root at some point).
893 */
894 if (!path_is_under(&nd->path, &nd->root))
895 return -EXDEV;
896 }
897
Joel Fernandes1304fea2019-03-14 17:32:39 -0400898 if (likely(!(nd->flags & LOOKUP_JUMPED))) {
899 success_walk_trace(nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500900 return 0;
Joel Fernandes1304fea2019-03-14 17:32:39 -0400901 }
Jeff Layton39159de2009-12-07 12:01:50 -0500902
Joel Fernandes1304fea2019-03-14 17:32:39 -0400903 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) {
904 success_walk_trace(nd);
Al Viro16c2cd72011-02-22 15:50:10 -0500905 return 0;
Joel Fernandes1304fea2019-03-14 17:32:39 -0400906 }
Al Viro16c2cd72011-02-22 15:50:10 -0500907
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500908 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Joel Fernandes1304fea2019-03-14 17:32:39 -0400909 if (status > 0) {
910 success_walk_trace(nd);
Jeff Layton39159de2009-12-07 12:01:50 -0500911 return 0;
Joel Fernandes1304fea2019-03-14 17:32:39 -0400912 }
Jeff Layton39159de2009-12-07 12:01:50 -0500913
Al Viro16c2cd72011-02-22 15:50:10 -0500914 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500915 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500916
Jeff Layton39159de2009-12-07 12:01:50 -0500917 return status;
918}
919
Aleksa Sarai740a1672019-12-07 01:13:29 +1100920static int set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400921{
Al Viro7bd88372014-09-13 21:55:46 -0400922 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100923
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100924 /*
925 * Jumping to the real root in a scoped-lookup is a BUG in namei, but we
926 * still have to ensure it doesn't happen because it will cause a breakout
927 * from the dirfd.
928 */
929 if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED))
930 return -ENOTRECOVERABLE;
931
Al Viro9e6697e2015-12-05 20:07:21 -0500932 if (nd->flags & LOOKUP_RCU) {
933 unsigned seq;
934
935 do {
936 seq = read_seqcount_begin(&fs->seq);
937 nd->root = fs->root;
938 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
939 } while (read_seqcount_retry(&fs->seq, seq));
940 } else {
941 get_fs_root(fs, &nd->root);
Al Viro84a2bd32019-07-16 21:20:17 -0400942 nd->flags |= LOOKUP_ROOT_GRABBED;
Al Viro9e6697e2015-12-05 20:07:21 -0500943 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100944 return 0;
Nick Piggin31e6b012011-01-07 17:49:52 +1100945}
946
Al Viro248fb5b2015-12-05 20:51:58 -0500947static int nd_jump_root(struct nameidata *nd)
948{
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100949 if (unlikely(nd->flags & LOOKUP_BENEATH))
950 return -EXDEV;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100951 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
952 /* Absolute path arguments to path_init() are allowed. */
953 if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt)
954 return -EXDEV;
955 }
Aleksa Sarai740a1672019-12-07 01:13:29 +1100956 if (!nd->root.mnt) {
957 int error = set_root(nd);
958 if (error)
959 return error;
960 }
Al Viro248fb5b2015-12-05 20:51:58 -0500961 if (nd->flags & LOOKUP_RCU) {
962 struct dentry *d;
963 nd->path = nd->root;
964 d = nd->path.dentry;
965 nd->inode = d->d_inode;
966 nd->seq = nd->root_seq;
967 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
968 return -ECHILD;
969 } else {
970 path_put(&nd->path);
971 nd->path = nd->root;
972 path_get(&nd->path);
973 nd->inode = nd->path.dentry->d_inode;
974 }
975 nd->flags |= LOOKUP_JUMPED;
976 return 0;
977}
978
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400979/*
Al Viro6b255392015-11-17 10:20:54 -0500980 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400981 * caller must have taken a reference to path beforehand.
982 */
Aleksa Sarai1bc82072019-12-07 01:13:28 +1100983int nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400984{
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100985 int error = -ELOOP;
Al Viro6e77137b2015-05-02 13:37:52 -0400986 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400987
Aleksa Sarai4b99d492019-12-07 01:13:31 +1100988 if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS))
989 goto err;
990
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100991 error = -EXDEV;
992 if (unlikely(nd->flags & LOOKUP_NO_XDEV)) {
993 if (nd->path.mnt != path->mnt)
994 goto err;
995 }
Aleksa Saraiadb21d22019-12-07 01:13:33 +1100996 /* Not currently safe for scoped-lookups. */
997 if (unlikely(nd->flags & LOOKUP_IS_SCOPED))
998 goto err;
Aleksa Sarai72ba2922019-12-07 01:13:32 +1100999
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001000 path_put(&nd->path);
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001001 nd->path = *path;
1002 nd->inode = nd->path.dentry->d_inode;
1003 nd->flags |= LOOKUP_JUMPED;
Aleksa Sarai1bc82072019-12-07 01:13:28 +11001004 return 0;
Aleksa Sarai4b99d492019-12-07 01:13:31 +11001005
1006err:
1007 path_put(path);
1008 return error;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -04001009}
1010
Al Virob9ff4422015-05-02 20:19:23 -04001011static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -04001012{
Al Viro21c30032015-05-03 21:06:24 -04001013 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -05001014 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -04001015 if (!(nd->flags & LOOKUP_RCU))
1016 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -04001017}
1018
Linus Torvalds561ec642012-10-26 10:05:07 -07001019int sysctl_protected_symlinks __read_mostly = 0;
1020int sysctl_protected_hardlinks __read_mostly = 0;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001021int sysctl_protected_fifos __read_mostly;
1022int sysctl_protected_regular __read_mostly;
Kees Cook800179c2012-07-25 17:29:07 -07001023
1024/**
1025 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -07001026 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -07001027 *
1028 * In the case of the sysctl_protected_symlinks sysctl being enabled,
1029 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
1030 * in a sticky world-writable directory. This is to protect privileged
1031 * processes from failing races against path names that may change out
1032 * from under them by way of other users creating malicious symlinks.
1033 * It will permit symlinks to be followed only when outside a sticky
1034 * world-writable directory, or when the uid of the symlink and follower
1035 * match, or when the directory owner matches the symlink's owner.
1036 *
1037 * Returns 0 if following the symlink is allowed, -ve on error.
1038 */
Al Viroad6cc4c2020-01-14 14:41:39 -05001039static inline int may_follow_link(struct nameidata *nd, const struct inode *inode)
Kees Cook800179c2012-07-25 17:29:07 -07001040{
Kees Cook800179c2012-07-25 17:29:07 -07001041 if (!sysctl_protected_symlinks)
1042 return 0;
1043
1044 /* Allowed if owner and follower match. */
Eric W. Biederman81abe272012-08-03 09:38:08 -07001045 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001046 return 0;
1047
1048 /* Allowed if parent directory not sticky and world-writable. */
Al Viro0f705952020-03-05 11:34:48 -05001049 if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
Kees Cook800179c2012-07-25 17:29:07 -07001050 return 0;
1051
1052 /* Allowed if parent directory and link owner match. */
Al Viro0f705952020-03-05 11:34:48 -05001053 if (uid_valid(nd->dir_uid) && uid_eq(nd->dir_uid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -07001054 return 0;
1055
Al Viro319565022015-05-07 20:37:40 -04001056 if (nd->flags & LOOKUP_RCU)
1057 return -ECHILD;
1058
Richard Guy Briggsea841ba2018-03-21 04:42:21 -04001059 audit_inode(nd->name, nd->stack[0].link.dentry, 0);
Kees Cook245d7362019-10-02 16:41:58 -07001060 audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link");
Kees Cook800179c2012-07-25 17:29:07 -07001061 return -EACCES;
1062}
1063
1064/**
1065 * safe_hardlink_source - Check for safe hardlink conditions
1066 * @inode: the source inode to hardlink from
1067 *
1068 * Return false if at least one of the following conditions:
1069 * - inode is not a regular file
1070 * - inode is setuid
1071 * - inode is setgid and group-exec
1072 * - access failure for read and write
1073 *
1074 * Otherwise returns true.
1075 */
1076static bool safe_hardlink_source(struct inode *inode)
1077{
1078 umode_t mode = inode->i_mode;
1079
1080 /* Special files should not get pinned to the filesystem. */
1081 if (!S_ISREG(mode))
1082 return false;
1083
1084 /* Setuid files should not get pinned to the filesystem. */
1085 if (mode & S_ISUID)
1086 return false;
1087
1088 /* Executable setgid files should not get pinned to the filesystem. */
1089 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
1090 return false;
1091
1092 /* Hardlinking to unreadable or unwritable sources is dangerous. */
1093 if (inode_permission(inode, MAY_READ | MAY_WRITE))
1094 return false;
1095
1096 return true;
1097}
1098
1099/**
1100 * may_linkat - Check permissions for creating a hardlink
1101 * @link: the source to hardlink from
1102 *
1103 * Block hardlink when all of:
1104 * - sysctl_protected_hardlinks enabled
1105 * - fsuid does not match inode
1106 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001107 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -07001108 *
1109 * Returns 0 if successful, -ve on error.
1110 */
Christoph Hellwig812931d2020-07-22 11:14:19 +02001111int may_linkat(struct path *link)
Kees Cook800179c2012-07-25 17:29:07 -07001112{
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05001113 struct inode *inode = link->dentry->d_inode;
1114
1115 /* Inode writeback is not safe when the uid or gid are invalid. */
1116 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
1117 return -EOVERFLOW;
Kees Cook800179c2012-07-25 17:29:07 -07001118
1119 if (!sysctl_protected_hardlinks)
1120 return 0;
1121
Kees Cook800179c2012-07-25 17:29:07 -07001122 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1123 * otherwise, it must be a safe source.
1124 */
Kees Cookcc658db2017-06-21 09:53:06 -07001125 if (safe_hardlink_source(inode) || inode_owner_or_capable(inode))
Kees Cook800179c2012-07-25 17:29:07 -07001126 return 0;
1127
Kees Cook245d7362019-10-02 16:41:58 -07001128 audit_log_path_denied(AUDIT_ANOM_LINK, "linkat");
Kees Cook800179c2012-07-25 17:29:07 -07001129 return -EPERM;
1130}
1131
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001132/**
1133 * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory
1134 * should be allowed, or not, on files that already
1135 * exist.
Al Virod0cb5012020-01-26 09:29:34 -05001136 * @dir_mode: mode bits of directory
1137 * @dir_uid: owner of directory
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001138 * @inode: the inode of the file to open
1139 *
1140 * Block an O_CREAT open of a FIFO (or a regular file) when:
1141 * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled
1142 * - the file already exists
1143 * - we are in a sticky directory
1144 * - we don't own the file
1145 * - the owner of the directory doesn't own the file
1146 * - the directory is world writable
1147 * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2
1148 * the directory doesn't have to be world writable: being group writable will
1149 * be enough.
1150 *
1151 * Returns 0 if the open is allowed, -ve on error.
1152 */
Al Virod0cb5012020-01-26 09:29:34 -05001153static int may_create_in_sticky(umode_t dir_mode, kuid_t dir_uid,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001154 struct inode * const inode)
1155{
1156 if ((!sysctl_protected_fifos && S_ISFIFO(inode->i_mode)) ||
1157 (!sysctl_protected_regular && S_ISREG(inode->i_mode)) ||
Al Virod0cb5012020-01-26 09:29:34 -05001158 likely(!(dir_mode & S_ISVTX)) ||
1159 uid_eq(inode->i_uid, dir_uid) ||
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001160 uid_eq(current_fsuid(), inode->i_uid))
1161 return 0;
1162
Al Virod0cb5012020-01-26 09:29:34 -05001163 if (likely(dir_mode & 0002) ||
1164 (dir_mode & 0020 &&
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001165 ((sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) ||
1166 (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode))))) {
Kees Cook245d7362019-10-02 16:41:58 -07001167 const char *operation = S_ISFIFO(inode->i_mode) ?
1168 "sticky_create_fifo" :
1169 "sticky_create_regular";
1170 audit_log_path_denied(AUDIT_ANOM_CREAT, operation);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07001171 return -EACCES;
1172 }
1173 return 0;
1174}
1175
David Howellsf015f1262012-06-25 12:55:28 +01001176/*
1177 * follow_up - Find the mountpoint of path's vfsmount
1178 *
1179 * Given a path, find the mountpoint of its source file system.
1180 * Replace @path with the path of the mountpoint in the parent mount.
1181 * Up is towards /.
1182 *
1183 * Return 1 if we went up a level and 0 if we were already at the
1184 * root.
1185 */
Al Virobab77eb2009-04-18 03:26:48 -04001186int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
Al Viro0714a532011-11-24 22:19:58 -05001188 struct mount *mnt = real_mount(path->mnt);
1189 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001191
Al Viro48a066e2013-09-29 22:06:07 -04001192 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001193 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001194 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001195 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197 }
Al Viro0714a532011-11-24 22:19:58 -05001198 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001199 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001200 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001201 dput(path->dentry);
1202 path->dentry = mountpoint;
1203 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001204 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 return 1;
1206}
Al Viro4d359502014-03-14 12:20:17 -04001207EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Al Viro7ef482f2020-02-26 17:50:13 -05001209static bool choose_mountpoint_rcu(struct mount *m, const struct path *root,
1210 struct path *path, unsigned *seqp)
1211{
1212 while (mnt_has_parent(m)) {
1213 struct dentry *mountpoint = m->mnt_mountpoint;
1214
1215 m = m->mnt_parent;
1216 if (unlikely(root->dentry == mountpoint &&
1217 root->mnt == &m->mnt))
1218 break;
1219 if (mountpoint != m->mnt.mnt_root) {
1220 path->mnt = &m->mnt;
1221 path->dentry = mountpoint;
1222 *seqp = read_seqcount_begin(&mountpoint->d_seq);
1223 return true;
1224 }
1225 }
1226 return false;
1227}
1228
Al Viro2aa38472020-02-26 19:19:05 -05001229static bool choose_mountpoint(struct mount *m, const struct path *root,
1230 struct path *path)
1231{
1232 bool found;
1233
1234 rcu_read_lock();
1235 while (1) {
1236 unsigned seq, mseq = read_seqbegin(&mount_lock);
1237
1238 found = choose_mountpoint_rcu(m, root, path, &seq);
1239 if (unlikely(!found)) {
1240 if (!read_seqretry(&mount_lock, mseq))
1241 break;
1242 } else {
1243 if (likely(__legitimize_path(path, seq, mseq)))
1244 break;
1245 rcu_read_unlock();
1246 path_put(path);
1247 rcu_read_lock();
1248 }
1249 }
1250 rcu_read_unlock();
1251 return found;
1252}
1253
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001254/*
David Howells9875cf82011-01-14 18:45:21 +00001255 * Perform an automount
1256 * - return -EISDIR to tell follow_managed() to stop and return the path we
1257 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001259static int follow_automount(struct path *path, int *count, unsigned lookup_flags)
Nick Piggin31e6b012011-01-07 17:49:52 +11001260{
Al Viro25e195a2020-01-11 11:27:46 -05001261 struct dentry *dentry = path->dentry;
Al Viro463ffb22005-06-06 13:36:05 -07001262
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001263 /* We don't want to mount if someone's just doing a stat -
1264 * unless they're stat'ing a directory and appended a '/' to
1265 * the name.
1266 *
1267 * We do, however, want to mount if someone wants to open or
1268 * create a file of any type under the mountpoint, wants to
1269 * traverse through the mountpoint or wants to open the
1270 * mounted directory. Also, autofs may mark negative dentries
1271 * as being automount points. These will need the attentions
1272 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001273 */
Al Viro1c9f5e02020-01-16 22:05:18 -05001274 if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
Ian Kent5d38f042017-11-29 16:11:26 -08001275 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Al Viro25e195a2020-01-11 11:27:46 -05001276 dentry->d_inode)
Ian Kent5d38f042017-11-29 16:11:26 -08001277 return -EISDIR;
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001278
Al Viro1c9f5e02020-01-16 22:05:18 -05001279 if (count && (*count)++ >= MAXSYMLINKS)
David Howells9875cf82011-01-14 18:45:21 +00001280 return -ELOOP;
1281
Al Viro25e195a2020-01-11 11:27:46 -05001282 return finish_automount(dentry->d_op->d_automount(path), path);
David Howells9875cf82011-01-14 18:45:21 +00001283}
1284
1285/*
Al Viro9deed3e2020-01-17 08:45:08 -05001286 * mount traversal - out-of-line part. One note on ->d_flags accesses -
1287 * dentries are pinned but not locked here, so negative dentry can go
1288 * positive right under us. Use of smp_load_acquire() provides a barrier
1289 * sufficient for ->d_inode and ->d_flags consistency.
David Howells9875cf82011-01-14 18:45:21 +00001290 */
Al Viro9deed3e2020-01-17 08:45:08 -05001291static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped,
1292 int *count, unsigned lookup_flags)
David Howells9875cf82011-01-14 18:45:21 +00001293{
Al Viro9deed3e2020-01-17 08:45:08 -05001294 struct vfsmount *mnt = path->mnt;
David Howells9875cf82011-01-14 18:45:21 +00001295 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001296 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001297
Al Viro9deed3e2020-01-17 08:45:08 -05001298 while (flags & DCACHE_MANAGED_DENTRY) {
David Howellscc53ce52011-01-14 18:45:26 +00001299 /* Allow the filesystem to manage the transit without i_mutex
1300 * being held. */
Al Virod41efb52019-11-04 22:30:52 -05001301 if (flags & DCACHE_MANAGE_TRANSIT) {
Ian Kentfb5f51c2016-11-24 08:03:41 +11001302 ret = path->dentry->d_op->d_manage(path, false);
Al Viro508c8772020-01-14 22:09:57 -05001303 flags = smp_load_acquire(&path->dentry->d_flags);
David Howellscc53ce52011-01-14 18:45:26 +00001304 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001305 break;
David Howellscc53ce52011-01-14 18:45:26 +00001306 }
1307
Al Viro9deed3e2020-01-17 08:45:08 -05001308 if (flags & DCACHE_MOUNTED) { // something's mounted on it..
David Howells9875cf82011-01-14 18:45:21 +00001309 struct vfsmount *mounted = lookup_mnt(path);
Al Viro9deed3e2020-01-17 08:45:08 -05001310 if (mounted) { // ... in our namespace
David Howells9875cf82011-01-14 18:45:21 +00001311 dput(path->dentry);
1312 if (need_mntput)
1313 mntput(path->mnt);
1314 path->mnt = mounted;
1315 path->dentry = dget(mounted->mnt_root);
Al Viro9deed3e2020-01-17 08:45:08 -05001316 // here we know it's positive
1317 flags = path->dentry->d_flags;
David Howells9875cf82011-01-14 18:45:21 +00001318 need_mntput = true;
1319 continue;
1320 }
David Howells9875cf82011-01-14 18:45:21 +00001321 }
1322
Al Viro9deed3e2020-01-17 08:45:08 -05001323 if (!(flags & DCACHE_NEED_AUTOMOUNT))
1324 break;
David Howells9875cf82011-01-14 18:45:21 +00001325
Al Viro9deed3e2020-01-17 08:45:08 -05001326 // uncovered automount point
1327 ret = follow_automount(path, count, lookup_flags);
1328 flags = smp_load_acquire(&path->dentry->d_flags);
1329 if (ret < 0)
1330 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 }
Al Viro8aef1882011-06-16 15:10:06 +01001332
Al Viro9deed3e2020-01-17 08:45:08 -05001333 if (ret == -EISDIR)
1334 ret = 0;
1335 // possible if you race with several mount --move
1336 if (need_mntput && path->mnt == mnt)
1337 mntput(path->mnt);
1338 if (!ret && unlikely(d_flags_negative(flags)))
Al Virod41efb52019-11-04 22:30:52 -05001339 ret = -ENOENT;
Al Viro9deed3e2020-01-17 08:45:08 -05001340 *jumped = need_mntput;
Al Viro84027522015-04-22 10:30:08 -04001341 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Al Viro9deed3e2020-01-17 08:45:08 -05001344static inline int traverse_mounts(struct path *path, bool *jumped,
1345 int *count, unsigned lookup_flags)
1346{
1347 unsigned flags = smp_load_acquire(&path->dentry->d_flags);
1348
1349 /* fastpath */
1350 if (likely(!(flags & DCACHE_MANAGED_DENTRY))) {
1351 *jumped = false;
1352 if (unlikely(d_flags_negative(flags)))
1353 return -ENOENT;
1354 return 0;
1355 }
1356 return __traverse_mounts(path, flags, jumped, count, lookup_flags);
1357}
1358
David Howellscc53ce52011-01-14 18:45:26 +00001359int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
1361 struct vfsmount *mounted;
1362
Al Viro1c755af2009-04-18 14:06:57 -04001363 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001365 dput(path->dentry);
1366 mntput(path->mnt);
1367 path->mnt = mounted;
1368 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 1;
1370 }
1371 return 0;
1372}
Al Viro4d359502014-03-14 12:20:17 -04001373EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
David Howells9875cf82011-01-14 18:45:21 +00001375/*
Al Viro9deed3e2020-01-17 08:45:08 -05001376 * Follow down to the covering mount currently visible to userspace. At each
1377 * point, the filesystem owning that dentry may be queried as to whether the
1378 * caller is permitted to proceed or not.
1379 */
1380int follow_down(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Al Viro9deed3e2020-01-17 08:45:08 -05001382 struct vfsmount *mnt = path->mnt;
1383 bool jumped;
1384 int ret = traverse_mounts(path, &jumped, NULL, 0);
1385
1386 if (path->mnt != mnt)
1387 mntput(mnt);
1388 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
Al Viro9deed3e2020-01-17 08:45:08 -05001390EXPORT_SYMBOL(follow_down);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
David Howells9875cf82011-01-14 18:45:21 +00001392/*
Al Viro287548e2011-05-27 06:50:06 -04001393 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1394 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001395 */
1396static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001397 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001398{
Al Viroea936ae2020-01-16 09:52:04 -05001399 struct dentry *dentry = path->dentry;
1400 unsigned int flags = dentry->d_flags;
1401
1402 if (likely(!(flags & DCACHE_MANAGED_DENTRY)))
1403 return true;
1404
1405 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1406 return false;
1407
Ian Kent62a73752011-03-25 01:51:02 +08001408 for (;;) {
Ian Kent62a73752011-03-25 01:51:02 +08001409 /*
1410 * Don't forget we might have a non-mountpoint managed dentry
1411 * that wants to block transit.
1412 */
Al Viroea936ae2020-01-16 09:52:04 -05001413 if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) {
1414 int res = dentry->d_op->d_manage(path, true);
1415 if (res)
1416 return res == -EISDIR;
1417 flags = dentry->d_flags;
NeilBrownb8faf032014-08-04 17:06:29 +10001418 }
Ian Kent62a73752011-03-25 01:51:02 +08001419
Al Viroea936ae2020-01-16 09:52:04 -05001420 if (flags & DCACHE_MOUNTED) {
1421 struct mount *mounted = __lookup_mnt(path->mnt, dentry);
1422 if (mounted) {
1423 path->mnt = &mounted->mnt;
1424 dentry = path->dentry = mounted->mnt.mnt_root;
1425 nd->flags |= LOOKUP_JUMPED;
1426 *seqp = read_seqcount_begin(&dentry->d_seq);
1427 *inode = dentry->d_inode;
1428 /*
1429 * We don't need to re-check ->d_seq after this
1430 * ->d_inode read - there will be an RCU delay
1431 * between mount hash removal and ->mnt_root
1432 * becoming unpinned.
1433 */
1434 flags = dentry->d_flags;
1435 continue;
1436 }
1437 if (read_seqretry(&mount_lock, nd->m_seq))
1438 return false;
1439 }
1440 return !(flags & DCACHE_NEED_AUTOMOUNT);
David Howells9875cf82011-01-14 18:45:21 +00001441 }
Al Viro287548e2011-05-27 06:50:06 -04001442}
1443
Al Virodb3c9ad2020-01-09 14:41:00 -05001444static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry,
1445 struct path *path, struct inode **inode,
1446 unsigned int *seqp)
Nick Piggin31e6b012011-01-07 17:49:52 +11001447{
Al Viro9deed3e2020-01-17 08:45:08 -05001448 bool jumped;
David Howellscc53ce52011-01-14 18:45:26 +00001449 int ret;
1450
Al Virodb3c9ad2020-01-09 14:41:00 -05001451 path->mnt = nd->path.mnt;
1452 path->dentry = dentry;
Al Viroc1530072020-01-09 14:50:18 -05001453 if (nd->flags & LOOKUP_RCU) {
1454 unsigned int seq = *seqp;
1455 if (unlikely(!*inode))
1456 return -ENOENT;
1457 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viro9deed3e2020-01-17 08:45:08 -05001458 return 0;
Al Viroc1530072020-01-09 14:50:18 -05001459 if (unlazy_child(nd, dentry, seq))
1460 return -ECHILD;
1461 // *path might've been clobbered by __follow_mount_rcu()
1462 path->mnt = nd->path.mnt;
1463 path->dentry = dentry;
David Howellscc53ce52011-01-14 18:45:26 +00001464 }
Al Viro9deed3e2020-01-17 08:45:08 -05001465 ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags);
1466 if (jumped) {
Aleksa Sarai72ba2922019-12-07 01:13:32 +11001467 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
Al Viro9deed3e2020-01-17 08:45:08 -05001468 ret = -EXDEV;
1469 else
1470 nd->flags |= LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 }
Al Viro9deed3e2020-01-17 08:45:08 -05001472 if (unlikely(ret)) {
1473 dput(path->dentry);
1474 if (path->mnt != nd->path.mnt)
1475 mntput(path->mnt);
1476 } else {
Al Virobd7c4b52020-01-08 20:37:23 -05001477 *inode = d_backing_inode(path->dentry);
1478 *seqp = 0; /* out of RCU mode, so the value doesn't matter */
1479 }
1480 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001484 * This looks up the name in dcache and possibly revalidates the found dentry.
1485 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001486 */
Al Viroe3c13922016-03-06 14:03:27 -05001487static struct dentry *lookup_dcache(const struct qstr *name,
1488 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001489 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001490{
Al Viroa89f8332017-01-09 22:25:28 -05001491 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001492 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001493 int error = d_revalidate(dentry, flags);
1494 if (unlikely(error <= 0)) {
1495 if (!error)
1496 d_invalidate(dentry);
1497 dput(dentry);
1498 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001499 }
1500 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001501 return dentry;
1502}
1503
1504/*
Al Viroa03ece52018-03-08 11:00:45 -05001505 * Parent directory has inode locked exclusive. This is one
1506 * and only case when ->lookup() gets called on non in-lookup
1507 * dentries - as the matter of fact, this only gets called
1508 * when directory is guaranteed to have no in-lookup children
1509 * at all.
Josef Bacik44396f42011-05-31 11:58:49 -04001510 */
Al Viroa03ece52018-03-08 11:00:45 -05001511static struct dentry *__lookup_hash(const struct qstr *name,
1512 struct dentry *base, unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001513{
Al Viroa03ece52018-03-08 11:00:45 -05001514 struct dentry *dentry = lookup_dcache(name, base, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001515 struct dentry *old;
Al Viroa03ece52018-03-08 11:00:45 -05001516 struct inode *dir = base->d_inode;
1517
1518 if (dentry)
1519 return dentry;
Josef Bacik44396f42011-05-31 11:58:49 -04001520
1521 /* Don't create child dentry for a dead directory. */
Al Viroa03ece52018-03-08 11:00:45 -05001522 if (unlikely(IS_DEADDIR(dir)))
Josef Bacik44396f42011-05-31 11:58:49 -04001523 return ERR_PTR(-ENOENT);
Al Viroa03ece52018-03-08 11:00:45 -05001524
1525 dentry = d_alloc(base, name);
1526 if (unlikely(!dentry))
1527 return ERR_PTR(-ENOMEM);
Josef Bacik44396f42011-05-31 11:58:49 -04001528
Al Viro72bd8662012-06-10 17:17:17 -04001529 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001530 if (unlikely(old)) {
1531 dput(dentry);
1532 dentry = old;
1533 }
1534 return dentry;
1535}
1536
Al Viro20e34352020-01-09 14:58:31 -05001537static struct dentry *lookup_fast(struct nameidata *nd,
1538 struct inode **inode,
1539 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
Nick Piggin31e6b012011-01-07 17:49:52 +11001541 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001542 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001543
Al Viro3cac2602009-08-13 18:27:43 +04001544 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001545 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001546 * of a false negative due to a concurrent rename, the caller is
1547 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001548 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001549 if (nd->flags & LOOKUP_RCU) {
1550 unsigned seq;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001551 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001552 if (unlikely(!dentry)) {
Jens Axboe01fd84a2020-12-17 09:19:08 -07001553 if (!try_to_unlazy(nd))
Al Viro20e34352020-01-09 14:58:31 -05001554 return ERR_PTR(-ECHILD);
1555 return NULL;
Al Viro5d0f49c2016-03-05 21:32:53 -05001556 }
Al Viro5a18fff2011-03-11 04:44:53 -05001557
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001558 /*
1559 * This sequence count validates that the inode matches
1560 * the dentry name information from lookup.
1561 */
David Howells63afdfc2015-05-06 15:59:00 +01001562 *inode = d_backing_inode(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001563 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Al Viro20e34352020-01-09 14:58:31 -05001564 return ERR_PTR(-ECHILD);
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001565
1566 /*
1567 * This sequence count validates that the parent had no
1568 * changes while we did the lookup of the dentry above.
1569 *
1570 * The memory barrier in read_seqcount_begin of child is
1571 * enough, we can use __read_seqcount_retry here.
1572 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001573 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Al Viro20e34352020-01-09 14:58:31 -05001574 return ERR_PTR(-ECHILD);
Al Viro5a18fff2011-03-11 04:44:53 -05001575
Al Viro254cf582015-05-05 09:40:46 -04001576 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001577 status = d_revalidate(dentry, nd->flags);
Al Viroc1530072020-01-09 14:50:18 -05001578 if (likely(status > 0))
Al Viro20e34352020-01-09 14:58:31 -05001579 return dentry;
Al Viro4675ac32017-01-09 22:29:15 -05001580 if (unlazy_child(nd, dentry, seq))
Al Viro20e34352020-01-09 14:58:31 -05001581 return ERR_PTR(-ECHILD);
Al Viro209a7fb2017-01-09 01:35:39 -05001582 if (unlikely(status == -ECHILD))
1583 /* we'd been told to redo it in non-rcu mode */
1584 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001585 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001586 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001587 if (unlikely(!dentry))
Al Viro20e34352020-01-09 14:58:31 -05001588 return NULL;
Al Viroa89f8332017-01-09 22:25:28 -05001589 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001590 }
Al Viro5a18fff2011-03-11 04:44:53 -05001591 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001592 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001593 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001594 dput(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001595 return ERR_PTR(status);
Al Viro5a18fff2011-03-11 04:44:53 -05001596 }
Al Viro20e34352020-01-09 14:58:31 -05001597 return dentry;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001598}
1599
1600/* Fast lookup failed, do it the slow way */
Al Viro88d83312018-04-06 16:43:47 -04001601static struct dentry *__lookup_slow(const struct qstr *name,
1602 struct dentry *dir,
1603 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001604{
Al Viro88d83312018-04-06 16:43:47 -04001605 struct dentry *dentry, *old;
Al Viro19363862016-04-14 19:33:34 -04001606 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001607 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001608
Al Viro19363862016-04-14 19:33:34 -04001609 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001610 if (unlikely(IS_DEADDIR(inode)))
Al Viro88d83312018-04-06 16:43:47 -04001611 return ERR_PTR(-ENOENT);
Al Viro94bdd652016-04-15 02:42:04 -04001612again:
Al Virod9171b92016-04-15 03:33:13 -04001613 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001614 if (IS_ERR(dentry))
Al Viro88d83312018-04-06 16:43:47 -04001615 return dentry;
Al Viro94bdd652016-04-15 02:42:04 -04001616 if (unlikely(!d_in_lookup(dentry))) {
Al Viroc64cd6e2020-01-10 17:17:19 -05001617 int error = d_revalidate(dentry, flags);
1618 if (unlikely(error <= 0)) {
1619 if (!error) {
1620 d_invalidate(dentry);
Al Viro949a8522016-03-06 14:20:52 -05001621 dput(dentry);
Al Viroc64cd6e2020-01-10 17:17:19 -05001622 goto again;
Al Viro949a8522016-03-06 14:20:52 -05001623 }
Al Viroc64cd6e2020-01-10 17:17:19 -05001624 dput(dentry);
1625 dentry = ERR_PTR(error);
Al Viro949a8522016-03-06 14:20:52 -05001626 }
Al Viro94bdd652016-04-15 02:42:04 -04001627 } else {
1628 old = inode->i_op->lookup(inode, dentry, flags);
1629 d_lookup_done(dentry);
1630 if (unlikely(old)) {
1631 dput(dentry);
1632 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001633 }
1634 }
Al Viroe3c13922016-03-06 14:03:27 -05001635 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636}
1637
Al Viro88d83312018-04-06 16:43:47 -04001638static struct dentry *lookup_slow(const struct qstr *name,
1639 struct dentry *dir,
1640 unsigned int flags)
1641{
1642 struct inode *inode = dir->d_inode;
1643 struct dentry *res;
1644 inode_lock_shared(inode);
1645 res = __lookup_slow(name, dir, flags);
1646 inode_unlock_shared(inode);
1647 return res;
1648}
1649
Al Viro52094c82011-02-21 21:34:47 -05001650static inline int may_lookup(struct nameidata *nd)
1651{
1652 if (nd->flags & LOOKUP_RCU) {
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02001653 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Jens Axboe01fd84a2020-12-17 09:19:08 -07001654 if (err != -ECHILD || !try_to_unlazy(nd))
Al Viro52094c82011-02-21 21:34:47 -05001655 return err;
Al Viro52094c82011-02-21 21:34:47 -05001656 }
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02001657 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001658}
1659
Al Viro49055902020-03-03 11:22:34 -05001660static int reserve_stack(struct nameidata *nd, struct path *link, unsigned seq)
1661{
Al Viro49055902020-03-03 11:22:34 -05001662 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS))
1663 return -ELOOP;
Al Viro45425762020-03-03 11:25:31 -05001664
1665 if (likely(nd->depth != EMBEDDED_LEVELS))
1666 return 0;
1667 if (likely(nd->stack != nd->internal))
1668 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001669 if (likely(nd_alloc_stack(nd)))
Al Viro49055902020-03-03 11:22:34 -05001670 return 0;
Al Viro60ef60c2020-03-03 11:43:55 -05001671
1672 if (nd->flags & LOOKUP_RCU) {
1673 // we need to grab link before we do unlazy. And we can't skip
1674 // unlazy even if we fail to grab the link - cleanup needs it
Al Viro49055902020-03-03 11:22:34 -05001675 bool grabbed_link = legitimize_path(nd, link, seq);
Al Viro60ef60c2020-03-03 11:43:55 -05001676
Jens Axboe01fd84a2020-12-17 09:19:08 -07001677 if (!try_to_unlazy(nd) != 0 || !grabbed_link)
Al Viro60ef60c2020-03-03 11:43:55 -05001678 return -ECHILD;
1679
1680 if (nd_alloc_stack(nd))
1681 return 0;
Al Viro49055902020-03-03 11:22:34 -05001682 }
Al Viro60ef60c2020-03-03 11:43:55 -05001683 return -ENOMEM;
Al Viro49055902020-03-03 11:22:34 -05001684}
1685
Al Virob1a81972020-01-19 12:48:44 -05001686enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4};
1687
Al Viro06708ad2020-01-14 14:26:57 -05001688static const char *pick_link(struct nameidata *nd, struct path *link,
Al Virob1a81972020-01-19 12:48:44 -05001689 struct inode *inode, unsigned seq, int flags)
Al Virod63ff282015-05-04 18:13:23 -04001690{
Al Viro1cf26652015-05-06 16:01:56 -04001691 struct saved *last;
Al Viroad6cc4c2020-01-14 14:41:39 -05001692 const char *res;
Al Viro49055902020-03-03 11:22:34 -05001693 int error = reserve_stack(nd, link, seq);
Al Viroad6cc4c2020-01-14 14:41:39 -05001694
Al Viro49055902020-03-03 11:22:34 -05001695 if (unlikely(error)) {
Al Viro84f0cd92020-03-03 10:14:30 -05001696 if (!(nd->flags & LOOKUP_RCU))
1697 path_put(link);
Al Viro49055902020-03-03 11:22:34 -05001698 return ERR_PTR(error);
Al Viro626de992015-05-04 18:26:59 -04001699 }
Al Viroab104922015-05-10 11:50:01 -04001700 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001701 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001702 clear_delayed_call(&last->done);
Al Viro0450b2d2015-05-08 13:23:53 -04001703 last->seq = seq;
Al Viroad6cc4c2020-01-14 14:41:39 -05001704
Al Virob1a81972020-01-19 12:48:44 -05001705 if (flags & WALK_TRAILING) {
Al Viroad6cc4c2020-01-14 14:41:39 -05001706 error = may_follow_link(nd, inode);
1707 if (unlikely(error))
1708 return ERR_PTR(error);
1709 }
1710
Mattias Nisslerdab741e02020-08-27 11:09:46 -06001711 if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) ||
1712 unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW))
Al Viroad6cc4c2020-01-14 14:41:39 -05001713 return ERR_PTR(-ELOOP);
1714
1715 if (!(nd->flags & LOOKUP_RCU)) {
1716 touch_atime(&last->link);
1717 cond_resched();
1718 } else if (atime_needs_update(&last->link, inode)) {
Jens Axboe01fd84a2020-12-17 09:19:08 -07001719 if (!try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001720 return ERR_PTR(-ECHILD);
1721 touch_atime(&last->link);
1722 }
1723
1724 error = security_inode_follow_link(link->dentry, inode,
1725 nd->flags & LOOKUP_RCU);
1726 if (unlikely(error))
1727 return ERR_PTR(error);
1728
Al Viroad6cc4c2020-01-14 14:41:39 -05001729 res = READ_ONCE(inode->i_link);
1730 if (!res) {
1731 const char * (*get)(struct dentry *, struct inode *,
1732 struct delayed_call *);
1733 get = inode->i_op->get_link;
1734 if (nd->flags & LOOKUP_RCU) {
1735 res = get(NULL, inode, &last->done);
Jens Axboe01fd84a2020-12-17 09:19:08 -07001736 if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd))
Al Viroad6cc4c2020-01-14 14:41:39 -05001737 res = get(link->dentry, inode, &last->done);
Al Viroad6cc4c2020-01-14 14:41:39 -05001738 } else {
1739 res = get(link->dentry, inode, &last->done);
1740 }
1741 if (!res)
1742 goto all_done;
1743 if (IS_ERR(res))
1744 return res;
1745 }
1746 if (*res == '/') {
1747 error = nd_jump_root(nd);
1748 if (unlikely(error))
1749 return ERR_PTR(error);
1750 while (unlikely(*++res == '/'))
1751 ;
1752 }
1753 if (*res)
1754 return res;
1755all_done: // pure jump
1756 put_link(nd);
1757 return NULL;
Al Virod63ff282015-05-04 18:13:23 -04001758}
1759
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001760/*
1761 * Do we need to follow links? We _really_ want to be able
1762 * to do this check without having to look at inode->i_op,
1763 * so we keep a cache of "no, this doesn't need follow_link"
1764 * for the common case.
1765 */
Al Virob0417d22020-01-14 13:34:20 -05001766static const char *step_into(struct nameidata *nd, int flags,
Al Virocbae4d12020-01-12 13:40:02 -05001767 struct dentry *dentry, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001768{
Al Virocbae4d12020-01-12 13:40:02 -05001769 struct path path;
1770 int err = handle_mounts(nd, dentry, &path, &inode, &seq);
1771
1772 if (err < 0)
Al Virob0417d22020-01-14 13:34:20 -05001773 return ERR_PTR(err);
Al Virocbae4d12020-01-12 13:40:02 -05001774 if (likely(!d_is_symlink(path.dentry)) ||
Al Viro8c4efe22020-01-19 12:44:18 -05001775 ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) ||
Al Viroaca29032020-01-09 15:17:57 -05001776 (flags & WALK_NOFOLLOW)) {
Al Viro8f64fb12016-11-14 01:50:26 -05001777 /* not a symlink or should not follow */
Al Viroc99687a2020-03-03 10:56:17 -05001778 if (!(nd->flags & LOOKUP_RCU)) {
1779 dput(nd->path.dentry);
1780 if (nd->path.mnt != path.mnt)
1781 mntput(nd->path.mnt);
1782 }
1783 nd->path = path;
Al Viro8f64fb12016-11-14 01:50:26 -05001784 nd->inode = inode;
1785 nd->seq = seq;
Al Virob0417d22020-01-14 13:34:20 -05001786 return NULL;
Al Viro8f64fb12016-11-14 01:50:26 -05001787 }
Al Viroa7f77542016-02-27 19:31:01 -05001788 if (nd->flags & LOOKUP_RCU) {
Al Viro84f0cd92020-03-03 10:14:30 -05001789 /* make sure that d_is_symlink above matches inode */
Al Virocbae4d12020-01-12 13:40:02 -05001790 if (read_seqcount_retry(&path.dentry->d_seq, seq))
Al Virob0417d22020-01-14 13:34:20 -05001791 return ERR_PTR(-ECHILD);
Al Viro84f0cd92020-03-03 10:14:30 -05001792 } else {
1793 if (path.mnt == nd->path.mnt)
1794 mntget(path.mnt);
Al Viroa7f77542016-02-27 19:31:01 -05001795 }
Al Virob1a81972020-01-19 12:48:44 -05001796 return pick_link(nd, &path, inode, seq, flags);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001797}
1798
Al Viroc2df1962020-02-26 14:33:30 -05001799static struct dentry *follow_dotdot_rcu(struct nameidata *nd,
1800 struct inode **inodep,
1801 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001802{
Al Viro12487f32020-02-26 14:59:56 -05001803 struct dentry *parent, *old;
Al Viro957dd412020-02-26 01:40:04 -05001804
Al Viro12487f32020-02-26 14:59:56 -05001805 if (path_equal(&nd->path, &nd->root))
1806 goto in_root;
1807 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro7ef482f2020-02-26 17:50:13 -05001808 struct path path;
Al Viroefe772d2020-02-28 10:06:37 -05001809 unsigned seq;
Al Viro7ef482f2020-02-26 17:50:13 -05001810 if (!choose_mountpoint_rcu(real_mount(nd->path.mnt),
1811 &nd->root, &path, &seq))
1812 goto in_root;
Al Viroefe772d2020-02-28 10:06:37 -05001813 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1814 return ERR_PTR(-ECHILD);
1815 nd->path = path;
1816 nd->inode = path.dentry->d_inode;
1817 nd->seq = seq;
1818 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1819 return ERR_PTR(-ECHILD);
1820 /* we know that mountpoint was pinned */
Al Viro957dd412020-02-26 01:40:04 -05001821 }
Al Viro12487f32020-02-26 14:59:56 -05001822 old = nd->path.dentry;
1823 parent = old->d_parent;
1824 *inodep = parent->d_inode;
1825 *seqp = read_seqcount_begin(&parent->d_seq);
1826 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1827 return ERR_PTR(-ECHILD);
1828 if (unlikely(!path_connected(nd->path.mnt, parent)))
1829 return ERR_PTR(-ECHILD);
1830 return parent;
1831in_root:
Al Viroefe772d2020-02-28 10:06:37 -05001832 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1833 return ERR_PTR(-ECHILD);
Al Viroc2df1962020-02-26 14:33:30 -05001834 if (unlikely(nd->flags & LOOKUP_BENEATH))
1835 return ERR_PTR(-ECHILD);
1836 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001837}
1838
Al Viroc2df1962020-02-26 14:33:30 -05001839static struct dentry *follow_dotdot(struct nameidata *nd,
1840 struct inode **inodep,
1841 unsigned *seqp)
Al Viro957dd412020-02-26 01:40:04 -05001842{
Al Viro12487f32020-02-26 14:59:56 -05001843 struct dentry *parent;
1844
1845 if (path_equal(&nd->path, &nd->root))
1846 goto in_root;
1847 if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) {
Al Viro2aa38472020-02-26 19:19:05 -05001848 struct path path;
1849
1850 if (!choose_mountpoint(real_mount(nd->path.mnt),
1851 &nd->root, &path))
1852 goto in_root;
Al Viro165200d2020-02-28 10:17:52 -05001853 path_put(&nd->path);
1854 nd->path = path;
Al Viro2aa38472020-02-26 19:19:05 -05001855 nd->inode = path.dentry->d_inode;
Al Viro165200d2020-02-28 10:17:52 -05001856 if (unlikely(nd->flags & LOOKUP_NO_XDEV))
1857 return ERR_PTR(-EXDEV);
Al Viro957dd412020-02-26 01:40:04 -05001858 }
Al Viro12487f32020-02-26 14:59:56 -05001859 /* rare case of legitimate dget_parent()... */
1860 parent = dget_parent(nd->path.dentry);
1861 if (unlikely(!path_connected(nd->path.mnt, parent))) {
1862 dput(parent);
1863 return ERR_PTR(-ENOENT);
1864 }
1865 *seqp = 0;
1866 *inodep = parent->d_inode;
1867 return parent;
1868
1869in_root:
Al Viroc2df1962020-02-26 14:33:30 -05001870 if (unlikely(nd->flags & LOOKUP_BENEATH))
1871 return ERR_PTR(-EXDEV);
1872 dget(nd->path.dentry);
1873 return NULL;
Al Viro957dd412020-02-26 01:40:04 -05001874}
1875
Al Viro7521f222020-02-26 12:22:58 -05001876static const char *handle_dots(struct nameidata *nd, int type)
Al Virof0a9ba72015-05-04 07:59:30 -04001877{
Miklos Szeredi697f5142012-05-21 17:30:05 +02001878 if (type == LAST_DOTDOT) {
Al Viro7521f222020-02-26 12:22:58 -05001879 const char *error = NULL;
Al Viroc2df1962020-02-26 14:33:30 -05001880 struct dentry *parent;
1881 struct inode *inode;
1882 unsigned seq;
Al Viro9856fa12011-03-04 14:22:06 -05001883
1884 if (!nd->root.mnt) {
Al Viro7521f222020-02-26 12:22:58 -05001885 error = ERR_PTR(set_root(nd));
Al Viro9856fa12011-03-04 14:22:06 -05001886 if (error)
1887 return error;
1888 }
1889 if (nd->flags & LOOKUP_RCU)
Al Viroc2df1962020-02-26 14:33:30 -05001890 parent = follow_dotdot_rcu(nd, &inode, &seq);
Al Viro9856fa12011-03-04 14:22:06 -05001891 else
Al Viroc2df1962020-02-26 14:33:30 -05001892 parent = follow_dotdot(nd, &inode, &seq);
1893 if (IS_ERR(parent))
1894 return ERR_CAST(parent);
1895 if (unlikely(!parent))
1896 error = step_into(nd, WALK_NOFOLLOW,
1897 nd->path.dentry, nd->inode, nd->seq);
1898 else
1899 error = step_into(nd, WALK_NOFOLLOW,
1900 parent, inode, seq);
1901 if (unlikely(error))
Al Viro951361f2011-03-04 14:44:37 -05001902 return error;
1903
Al Viroce57dfc2011-03-13 19:58:58 -04001904 if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) {
1905 /*
1906 * If there was a racing rename or mount along our
1907 * path, then we can't be sure that ".." hasn't jumped
1908 * above nd->root (and so userspace should retry or use
1909 * some fallback).
1910 */
1911 smp_rmb();
1912 if (unlikely(__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001913 return ERR_PTR(-EAGAIN);
Al Viroce57dfc2011-03-13 19:58:58 -04001914 if (unlikely(__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)))
Al Viro7521f222020-02-26 12:22:58 -05001915 return ERR_PTR(-EAGAIN);
Al Viroce57dfc2011-03-13 19:58:58 -04001916 }
Al Viro766c4cb2015-05-07 19:24:57 -04001917 }
Al Viro7521f222020-02-26 12:22:58 -05001918 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919}
1920
Al Viro92d27012020-01-14 13:24:17 -05001921static const char *walk_component(struct nameidata *nd, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Al Virodb3c9ad2020-01-09 14:41:00 -05001923 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct inode *inode;
1925 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /*
Al Viroce57dfc2011-03-13 19:58:58 -04001927 * "." and ".." are special - ".." especially so because it has
1928 * to be able to know about the current root directory and
1929 * parent relationships.
1930 */
1931 if (unlikely(nd->last_type != LAST_NORM)) {
Al Viro1c4ff1a2016-11-14 01:39:36 -05001932 if (!(flags & WALK_MORE) && nd->depth)
Al Viroce57dfc2011-03-13 19:58:58 -04001933 put_link(nd);
Al Viro7521f222020-02-26 12:22:58 -05001934 return handle_dots(nd, nd->last_type);
Al Viroce57dfc2011-03-13 19:58:58 -04001935 }
Al Viro20e34352020-01-09 14:58:31 -05001936 dentry = lookup_fast(nd, &inode, &seq);
1937 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001938 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05001939 if (unlikely(!dentry)) {
Al Virodb3c9ad2020-01-09 14:41:00 -05001940 dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags);
1941 if (IS_ERR(dentry))
Al Viro92d27012020-01-14 13:24:17 -05001942 return ERR_CAST(dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001943 }
Al Viro56676ec2020-03-10 21:54:54 -04001944 if (!(flags & WALK_MORE) && nd->depth)
1945 put_link(nd);
Al Virob0417d22020-01-14 13:34:20 -05001946 return step_into(nd, flags, dentry, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001947}
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001950 * We can do the critical dentry name comparison and hashing
1951 * operations one word at a time, but we are limited to:
1952 *
1953 * - Architectures with fast unaligned word accesses. We could
1954 * do a "get_unaligned()" if this helps and is sufficiently
1955 * fast.
1956 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001957 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1958 * do not trap on the (extremely unlikely) case of a page
1959 * crossing operation.
1960 *
1961 * - Furthermore, we need an efficient 64-bit compile for the
1962 * 64-bit case in order to generate the "number of bytes in
1963 * the final mask". Again, that could be replaced with a
1964 * efficient population count instruction or similar.
1965 */
1966#ifdef CONFIG_DCACHE_WORD_ACCESS
1967
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001968#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001969
George Spelvin468a9422016-05-26 22:11:51 -04001970#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001971
George Spelvin468a9422016-05-26 22:11:51 -04001972/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1973
1974#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001975/*
1976 * Register pressure in the mixing function is an issue, particularly
1977 * on 32-bit x86, but almost any function requires one state value and
1978 * one temporary. Instead, use a function designed for two state values
1979 * and no temporaries.
1980 *
1981 * This function cannot create a collision in only two iterations, so
1982 * we have two iterations to achieve avalanche. In those two iterations,
1983 * we have six layers of mixing, which is enough to spread one bit's
1984 * influence out to 2^6 = 64 state bits.
1985 *
1986 * Rotate constants are scored by considering either 64 one-bit input
1987 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1988 * probability of that delta causing a change to each of the 128 output
1989 * bits, using a sample of random initial states.
1990 *
1991 * The Shannon entropy of the computed probabilities is then summed
1992 * to produce a score. Ideally, any input change has a 50% chance of
1993 * toggling any given output bit.
1994 *
1995 * Mixing scores (in bits) for (12,45):
1996 * Input delta: 1-bit 2-bit
1997 * 1 round: 713.3 42542.6
1998 * 2 rounds: 2753.7 140389.8
1999 * 3 rounds: 5954.1 233458.2
2000 * 4 rounds: 7862.6 256672.2
2001 * Perfect: 8192 258048
2002 * (64*128) (64*63/2 * 128)
2003 */
2004#define HASH_MIX(x, y, a) \
2005 ( x ^= (a), \
2006 y ^= x, x = rol64(x,12),\
2007 x += y, y = rol64(y,45),\
2008 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002009
George Spelvin0fed3ac2016-05-02 06:31:01 -04002010/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04002011 * Fold two longs into one 32-bit hash value. This must be fast, but
2012 * latency isn't quite as critical, as there is a fair bit of additional
2013 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04002014 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04002015static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002016{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002017 y ^= x * GOLDEN_RATIO_64;
2018 y *= GOLDEN_RATIO_64;
2019 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04002020}
2021
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002022#else /* 32-bit case */
2023
George Spelvin2a18da7a2016-05-23 07:43:58 -04002024/*
2025 * Mixing scores (in bits) for (7,20):
2026 * Input delta: 1-bit 2-bit
2027 * 1 round: 330.3 9201.6
2028 * 2 rounds: 1246.4 25475.4
2029 * 3 rounds: 1907.1 31295.1
2030 * 4 rounds: 2042.3 31718.6
2031 * Perfect: 2048 31744
2032 * (32*64) (32*31/2 * 64)
2033 */
2034#define HASH_MIX(x, y, a) \
2035 ( x ^= (a), \
2036 y ^= x, x = rol32(x, 7),\
2037 x += y, y = rol32(y,20),\
2038 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002039
George Spelvin2a18da7a2016-05-23 07:43:58 -04002040static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04002041{
George Spelvin2a18da7a2016-05-23 07:43:58 -04002042 /* Use arch-optimized multiply if one exists */
2043 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04002044}
2045
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002046#endif
2047
George Spelvin2a18da7a2016-05-23 07:43:58 -04002048/*
2049 * Return the hash of a string of known length. This is carfully
2050 * designed to match hash_name(), which is the more critical function.
2051 * In particular, we must end by hashing a final word containing 0..7
2052 * payload bytes, to match the way that hash_name() iterates until it
2053 * finds the delimiter after the name.
2054 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002055unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002056{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002057 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002058
2059 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002060 if (!len)
2061 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002062 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002063 if (len < sizeof(unsigned long))
2064 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04002065 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002066 name += sizeof(unsigned long);
2067 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002068 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04002069 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002070done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04002071 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002072}
2073EXPORT_SYMBOL(full_name_hash);
2074
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002075/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002076u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002077{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002078 unsigned long a = 0, x = 0, y = (unsigned long)salt;
2079 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002080 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
2081
Linus Torvalds8387ff22016-06-10 07:51:30 -07002082 len = 0;
2083 goto inside;
2084
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002085 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002086 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002087 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002088inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002089 a = load_unaligned_zeropad(name+len);
2090 } while (!has_zero(a, &adata, &constants));
2091
2092 adata = prep_zero_mask(a, adata, &constants);
2093 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002094 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002095
George Spelvin2a18da7a2016-05-23 07:43:58 -04002096 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002097}
2098EXPORT_SYMBOL(hashlen_string);
2099
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002100/*
2101 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002102 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002103 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002104static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002105{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002106 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
2107 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07002108 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002109
Linus Torvalds8387ff22016-06-10 07:51:30 -07002110 len = 0;
2111 goto inside;
2112
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002113 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04002114 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002115 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07002116inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07002117 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07002118 b = a ^ REPEAT_BYTE('/');
2119 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002120
Linus Torvalds36126f82012-05-26 10:43:17 -07002121 adata = prep_zero_mask(a, adata, &constants);
2122 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07002123 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002124 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07002125
George Spelvin2a18da7a2016-05-23 07:43:58 -04002126 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002127}
2128
George Spelvin2a18da7a2016-05-23 07:43:58 -04002129#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002130
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002131/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002132unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08002133{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002134 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002135 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002136 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002137 return end_name_hash(hash);
2138}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002139EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002140
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002141/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002142u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002143{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002144 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002145 unsigned long len = 0, c;
2146
2147 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002148 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002149 len++;
2150 hash = partial_name_hash(c, hash);
2151 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002152 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002153 return hashlen_create(end_name_hash(hash), len);
2154}
George Spelvinf2a031b2016-05-29 01:26:41 -04002155EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002156
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002157/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002158 * We know there's a real path component here of at least
2159 * one character.
2160 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002161static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002162{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002163 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002164 unsigned long len = 0, c;
2165
2166 c = (unsigned char)*name;
2167 do {
2168 len++;
2169 hash = partial_name_hash(c, hash);
2170 c = (unsigned char)name[len];
2171 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002172 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002173}
2174
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002175#endif
2176
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002179 * This is the basic name resolution function, turning a pathname into
2180 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002182 * Returns 0 and nd will have valid dentry and mnt on success.
2183 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 */
Al Viro6de88d72009-08-09 01:41:57 +04002185static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186{
Al Virod8d46112020-02-23 22:04:15 -05002187 int depth = 0; // depth <= nd->depth
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002189
Al Virob4c03532020-01-19 11:44:51 -05002190 nd->last_type = LAST_ROOT;
Al Viroc1088372020-03-05 15:48:44 -05002191 nd->flags |= LOOKUP_PARENT;
Al Viro9b5858e2018-07-09 16:33:23 -04002192 if (IS_ERR(name))
2193 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 while (*name=='/')
2195 name++;
2196 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002197 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 /* At this point we know we have a real path component. */
2200 for(;;) {
Al Viro92d27012020-01-14 13:24:17 -05002201 const char *link;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002202 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002203 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Al Viro52094c82011-02-21 21:34:47 -05002205 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002206 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002207 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Linus Torvalds8387ff22016-06-10 07:51:30 -07002209 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Al Virofe479a52011-02-22 15:10:03 -05002211 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002212 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002213 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002214 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002215 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002216 nd->flags |= LOOKUP_JUMPED;
2217 }
Al Virofe479a52011-02-22 15:10:03 -05002218 break;
2219 case 1:
2220 type = LAST_DOT;
2221 }
Al Viro5a202bc2011-03-08 14:17:44 -05002222 if (likely(type == LAST_NORM)) {
2223 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002224 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002225 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002226 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002227 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002228 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002229 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002230 hash_len = this.hash_len;
2231 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002232 }
2233 }
Al Virofe479a52011-02-22 15:10:03 -05002234
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002235 nd->last.hash_len = hash_len;
2236 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002237 nd->last_type = type;
2238
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002239 name += hashlen_len(hash_len);
2240 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002241 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002242 /*
2243 * If it wasn't NUL, we know it was '/'. Skip that
2244 * slash, and continue until no more slashes.
2245 */
2246 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002247 name++;
2248 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002249 if (unlikely(!*name)) {
2250OK:
Al Virod8d46112020-02-23 22:04:15 -05002251 /* pathname or trailing symlink, done */
Al Viroc1088372020-03-05 15:48:44 -05002252 if (!depth) {
Al Viro0f705952020-03-05 11:34:48 -05002253 nd->dir_uid = nd->inode->i_uid;
2254 nd->dir_mode = nd->inode->i_mode;
Al Viroc1088372020-03-05 15:48:44 -05002255 nd->flags &= ~LOOKUP_PARENT;
Al Viro8620c232015-05-04 08:58:35 -04002256 return 0;
Al Viroc1088372020-03-05 15:48:44 -05002257 }
Al Viro8620c232015-05-04 08:58:35 -04002258 /* last component of nested symlink */
Al Virod8d46112020-02-23 22:04:15 -05002259 name = nd->stack[--depth].name;
Al Viro8c4efe22020-01-19 12:44:18 -05002260 link = walk_component(nd, 0);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002261 } else {
2262 /* not the last component */
Al Viro8c4efe22020-01-19 12:44:18 -05002263 link = walk_component(nd, WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002264 }
Al Viro92d27012020-01-14 13:24:17 -05002265 if (unlikely(link)) {
2266 if (IS_ERR(link))
2267 return PTR_ERR(link);
2268 /* a symlink to follow */
Al Virod8d46112020-02-23 22:04:15 -05002269 nd->stack[depth++].name = name;
Al Viro92d27012020-01-14 13:24:17 -05002270 name = link;
2271 continue;
Nick Piggin31e6b012011-01-07 17:49:52 +11002272 }
Al Viro97242f92015-08-01 19:59:28 -04002273 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2274 if (nd->flags & LOOKUP_RCU) {
Jens Axboe01fd84a2020-12-17 09:19:08 -07002275 if (!try_to_unlazy(nd))
Al Viro97242f92015-08-01 19:59:28 -04002276 return -ECHILD;
2277 }
Al Viro3595e232015-05-09 16:54:45 -04002278 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002279 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281}
2282
Al Viroedc2b1d2018-07-09 16:27:23 -04002283/* must be paired with terminate_walk() */
Al Viroc8a53ee2015-05-12 18:43:07 -04002284static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Aleksa Sarai740a1672019-12-07 01:13:29 +11002286 int error;
Al Viroc8a53ee2015-05-12 18:43:07 -04002287 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002289 if (!*s)
2290 flags &= ~LOOKUP_RCU;
Al Viroedc2b1d2018-07-09 16:27:23 -04002291 if (flags & LOOKUP_RCU)
2292 rcu_read_lock();
Linus Torvaldsc0eb0272017-04-02 17:10:08 -07002293
Al Viroc1088372020-03-05 15:48:44 -05002294 nd->flags = flags | LOOKUP_JUMPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 nd->depth = 0;
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002296
2297 nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount);
2298 nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount);
2299 smp_rmb();
2300
Al Viro5b6ca022011-03-09 23:04:47 -05002301 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002302 struct dentry *root = nd->root.dentry;
2303 struct inode *inode = root->d_inode;
Al Viro93893862017-04-15 17:29:14 -04002304 if (*s && unlikely(!d_can_lookup(root)))
2305 return ERR_PTR(-ENOTDIR);
Al Viro5b6ca022011-03-09 23:04:47 -05002306 nd->path = nd->root;
2307 nd->inode = inode;
2308 if (flags & LOOKUP_RCU) {
Aleksa Saraiab87f9a2019-12-07 01:13:35 +11002309 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002310 nd->root_seq = nd->seq;
Al Viro5b6ca022011-03-09 23:04:47 -05002311 } else {
2312 path_get(&nd->path);
2313 }
Al Viro368ee9b2015-05-08 17:19:59 -04002314 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002315 }
2316
Al Viro2a737872009-04-07 11:49:53 -04002317 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002318 nd->path.mnt = NULL;
2319 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002321 /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */
2322 if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) {
Aleksa Sarai740a1672019-12-07 01:13:29 +11002323 error = nd_jump_root(nd);
2324 if (unlikely(error))
2325 return ERR_PTR(error);
2326 return s;
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002327 }
2328
2329 /* Relative pathname -- get the starting-point it is relative to. */
2330 if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002331 if (flags & LOOKUP_RCU) {
2332 struct fs_struct *fs = current->fs;
2333 unsigned seq;
2334
Al Viroe41f7d42011-02-22 14:02:58 -05002335 do {
2336 seq = read_seqcount_begin(&fs->seq);
2337 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002338 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002339 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2340 } while (read_seqcount_retry(&fs->seq, seq));
2341 } else {
2342 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002343 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002344 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002345 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002346 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002347 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002348 struct dentry *dentry;
2349
Al Viro2903ff02012-08-28 12:52:22 -04002350 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002351 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002352
Al Viro2903ff02012-08-28 12:52:22 -04002353 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002354
Al Viroedc2b1d2018-07-09 16:27:23 -04002355 if (*s && unlikely(!d_can_lookup(dentry))) {
2356 fdput(f);
2357 return ERR_PTR(-ENOTDIR);
Al Virof52e0c12011-03-14 18:56:51 -04002358 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002359
Al Viro2903ff02012-08-28 12:52:22 -04002360 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002361 if (flags & LOOKUP_RCU) {
Al Viro34a26b92015-05-11 08:05:05 -04002362 nd->inode = nd->path.dentry->d_inode;
2363 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002364 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002365 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002366 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002367 }
Al Viro34a26b92015-05-11 08:05:05 -04002368 fdput(f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369 }
Aleksa Sarai8db52c72019-12-07 01:13:34 +11002370
Aleksa Saraiadb21d22019-12-07 01:13:33 +11002371 /* For scoped-lookups we need to set the root to the dirfd as well. */
2372 if (flags & LOOKUP_IS_SCOPED) {
2373 nd->root = nd->path;
2374 if (flags & LOOKUP_RCU) {
2375 nd->root_seq = nd->seq;
2376 } else {
2377 path_get(&nd->root);
2378 nd->flags |= LOOKUP_ROOT_GRABBED;
2379 }
2380 }
2381 return s;
Al Viro9b4a9b12009-04-07 11:44:16 -04002382}
2383
Al Viro1ccac622020-01-14 10:13:40 -05002384static inline const char *lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002385{
2386 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2387 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2388
Al Viroc1088372020-03-05 15:48:44 -05002389 return walk_component(nd, WALK_TRAILING);
Al Virobd92d7f2011-03-14 19:54:59 -04002390}
2391
Al Viro4f757f32017-04-15 17:31:22 -04002392static int handle_lookup_down(struct nameidata *nd)
2393{
Al Viroc1530072020-01-09 14:50:18 -05002394 if (!(nd->flags & LOOKUP_RCU))
Al Virodb3c9ad2020-01-09 14:41:00 -05002395 dget(nd->path.dentry);
Al Virob0417d22020-01-14 13:34:20 -05002396 return PTR_ERR(step_into(nd, WALK_NOFOLLOW,
2397 nd->path.dentry, nd->inode, nd->seq));
Al Viro4f757f32017-04-15 17:31:22 -04002398}
2399
Al Viro9b4a9b12009-04-07 11:44:16 -04002400/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002401static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002402{
Al Viroc8a53ee2015-05-12 18:43:07 -04002403 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002404 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002405
Al Viro9b5858e2018-07-09 16:33:23 -04002406 if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) {
Al Viro4f757f32017-04-15 17:31:22 -04002407 err = handle_lookup_down(nd);
Al Viro5f336e72018-07-09 16:38:06 -04002408 if (unlikely(err < 0))
2409 s = ERR_PTR(err);
Al Viro4f757f32017-04-15 17:31:22 -04002410 }
2411
Al Viro1ccac622020-01-14 10:13:40 -05002412 while (!(err = link_path_walk(s, nd)) &&
2413 (s = lookup_last(nd)) != NULL)
2414 ;
Al Viro43908132021-04-06 19:46:51 -04002415 if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) {
2416 err = handle_lookup_down(nd);
2417 nd->flags &= ~LOOKUP_JUMPED; // no d_weak_revalidate(), please...
2418 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002419 if (!err)
2420 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002421
Al Virodeb106c2015-05-08 18:05:21 -04002422 if (!err && nd->flags & LOOKUP_DIRECTORY)
2423 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002424 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002425 if (!err) {
2426 *path = nd->path;
2427 nd->path.mnt = NULL;
2428 nd->path.dentry = NULL;
2429 }
2430 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002431 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002432}
Nick Piggin31e6b012011-01-07 17:49:52 +11002433
David Howells31d921c2018-11-01 23:07:24 +00002434int filename_lookup(int dfd, struct filename *name, unsigned flags,
2435 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002436{
Al Viro894bc8c2015-05-02 07:16:16 -04002437 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002438 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002439 if (IS_ERR(name))
2440 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002441 if (unlikely(root)) {
2442 nd.root = *root;
2443 flags |= LOOKUP_ROOT;
2444 }
Al Viro9883d182015-05-13 07:28:08 -04002445 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002446 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002447 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002448 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002449 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002450 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002451
2452 if (likely(!retval))
Al Viro161aff12020-01-11 22:52:26 -05002453 audit_inode(name, path->dentry,
2454 flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0);
Al Viro9883d182015-05-13 07:28:08 -04002455 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002456 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002457 return retval;
2458}
2459
Al Viro8bcb77f2015-05-08 16:59:20 -04002460/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002461static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002462 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002463{
Al Viroc8a53ee2015-05-12 18:43:07 -04002464 const char *s = path_init(nd, flags);
Al Viro9b5858e2018-07-09 16:33:23 -04002465 int err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002466 if (!err)
2467 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002468 if (!err) {
2469 *parent = nd->path;
2470 nd->path.mnt = NULL;
2471 nd->path.dentry = NULL;
2472 }
2473 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002474 return err;
2475}
2476
Al Viro5c31b6c2015-05-12 17:32:54 -04002477static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002478 unsigned int flags, struct path *parent,
2479 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002480{
2481 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002482 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002483
Al Viro5c31b6c2015-05-12 17:32:54 -04002484 if (IS_ERR(name))
2485 return name;
Al Viro9883d182015-05-13 07:28:08 -04002486 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002487 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002488 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002489 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002490 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002491 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002492 if (likely(!retval)) {
2493 *last = nd.last;
2494 *type = nd.last_type;
Al Viroc9b07ea2019-07-14 13:22:27 -04002495 audit_inode(name, parent->dentry, AUDIT_INODE_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002496 } else {
2497 putname(name);
2498 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002499 }
Al Viro9883d182015-05-13 07:28:08 -04002500 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002501 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002502}
2503
Al Viro79714f72012-06-15 03:01:42 +04002504/* does lookup, returns the object with parent locked */
2505struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002506{
Al Viro5c31b6c2015-05-12 17:32:54 -04002507 struct filename *filename;
2508 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002509 struct qstr last;
2510 int type;
Paul Moore51689102015-01-22 00:00:03 -05002511
Al Viro5c31b6c2015-05-12 17:32:54 -04002512 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2513 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002514 if (IS_ERR(filename))
2515 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002516 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002517 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002518 putname(filename);
2519 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002520 }
Al Viro59551022016-01-22 15:40:57 -05002521 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002522 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002523 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002524 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002525 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002526 }
Paul Moore51689102015-01-22 00:00:03 -05002527 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002528 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002529}
2530
Al Virod1811462008-08-02 00:49:18 -04002531int kern_path(const char *name, unsigned int flags, struct path *path)
2532{
Al Viroabc9f5b2015-05-12 16:53:42 -04002533 return filename_lookup(AT_FDCWD, getname_kernel(name),
2534 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002535}
Greg Kroah-Hartman91be4232022-03-07 20:45:25 +01002536EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002537
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002538/**
2539 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2540 * @dentry: pointer to dentry of the base directory
2541 * @mnt: pointer to vfs mount of the base directory
2542 * @name: pointer to file name
2543 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002544 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002545 */
2546int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2547 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002548 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002549{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002550 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002551 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002552 return filename_lookup(AT_FDCWD, getname_kernel(name),
2553 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002554}
Al Viro4d359502014-03-14 12:20:17 -04002555EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002556
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002557static int lookup_one_len_common(const char *name, struct dentry *base,
2558 int len, struct qstr *this)
Al Viro3c95f0d2018-04-06 16:32:38 -04002559{
2560 this->name = name;
2561 this->len = len;
2562 this->hash = full_name_hash(base, name, len);
2563 if (!len)
2564 return -EACCES;
2565
2566 if (unlikely(name[0] == '.')) {
2567 if (len < 2 || (len == 2 && name[1] == '.'))
2568 return -EACCES;
2569 }
2570
2571 while (len--) {
2572 unsigned int c = *(const unsigned char *)name++;
2573 if (c == '/' || c == '\0')
2574 return -EACCES;
2575 }
2576 /*
2577 * See if the low-level filesystem might want
2578 * to use its own hash..
2579 */
2580 if (base->d_flags & DCACHE_OP_HASH) {
2581 int err = base->d_op->d_hash(base, this);
2582 if (err < 0)
2583 return err;
2584 }
2585
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002586 return inode_permission(base->d_inode, MAY_EXEC);
Al Viro3c95f0d2018-04-06 16:32:38 -04002587}
2588
Christoph Hellwigeead1912007-10-16 23:25:38 -07002589/**
David Howells0da0b7f2018-06-15 15:19:22 +01002590 * try_lookup_one_len - filesystem helper to lookup single pathname component
2591 * @name: pathname component to lookup
2592 * @base: base directory to lookup from
2593 * @len: maximum length @len should be interpreted to
2594 *
2595 * Look up a dentry by name in the dcache, returning NULL if it does not
2596 * currently exist. The function does not try to create a dentry.
2597 *
2598 * Note that this routine is purely a helper for filesystem usage and should
2599 * not be called by generic code.
2600 *
2601 * The caller must hold base->i_mutex.
2602 */
2603struct dentry *try_lookup_one_len(const char *name, struct dentry *base, int len)
2604{
2605 struct qstr this;
2606 int err;
2607
2608 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
2609
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002610 err = lookup_one_len_common(name, base, len, &this);
David Howells0da0b7f2018-06-15 15:19:22 +01002611 if (err)
2612 return ERR_PTR(err);
2613
2614 return lookup_dcache(&this, base, 0);
2615}
2616EXPORT_SYMBOL(try_lookup_one_len);
2617
2618/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002619 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002620 * @name: pathname component to lookup
2621 * @base: base directory to lookup from
2622 * @len: maximum length @len should be interpreted to
2623 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002624 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002625 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002626 *
2627 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002628 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002629struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
James Morris057f6c02007-04-26 00:12:05 -07002630{
Al Viro8613a202018-04-06 16:45:33 -04002631 struct dentry *dentry;
James Morris057f6c02007-04-26 00:12:05 -07002632 struct qstr this;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002633 int err;
James Morris057f6c02007-04-26 00:12:05 -07002634
Al Viro59551022016-01-22 15:40:57 -05002635 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002636
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002637 err = lookup_one_len_common(name, base, len, &this);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002638 if (err)
2639 return ERR_PTR(err);
2640
Al Viro8613a202018-04-06 16:45:33 -04002641 dentry = lookup_dcache(&this, base, 0);
2642 return dentry ? dentry : __lookup_slow(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002643}
Al Viro4d359502014-03-14 12:20:17 -04002644EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002645
NeilBrownbbddca82016-01-07 16:08:20 -05002646/**
2647 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2648 * @name: pathname component to lookup
2649 * @base: base directory to lookup from
2650 * @len: maximum length @len should be interpreted to
2651 *
2652 * Note that this routine is purely a helper for filesystem usage and should
2653 * not be called by generic code.
2654 *
2655 * Unlike lookup_one_len, it should be called without the parent
2656 * i_mutex held, and will take the i_mutex itself if necessary.
2657 */
2658struct dentry *lookup_one_len_unlocked(const char *name,
2659 struct dentry *base, int len)
2660{
2661 struct qstr this;
NeilBrownbbddca82016-01-07 16:08:20 -05002662 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002663 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002664
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002665 err = lookup_one_len_common(name, base, len, &this);
NeilBrownbbddca82016-01-07 16:08:20 -05002666 if (err)
2667 return ERR_PTR(err);
2668
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002669 ret = lookup_dcache(&this, base, 0);
2670 if (!ret)
2671 ret = lookup_slow(&this, base, 0);
2672 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002673}
2674EXPORT_SYMBOL(lookup_one_len_unlocked);
2675
Al Viro6c2d47982019-10-31 01:21:58 -04002676/*
2677 * Like lookup_one_len_unlocked(), except that it yields ERR_PTR(-ENOENT)
2678 * on negatives. Returns known positive or ERR_PTR(); that's what
2679 * most of the users want. Note that pinned negative with unlocked parent
2680 * _can_ become positive at any time, so callers of lookup_one_len_unlocked()
2681 * need to be very careful; pinned positives have ->d_inode stable, so
2682 * this one avoids such problems.
2683 */
2684struct dentry *lookup_positive_unlocked(const char *name,
2685 struct dentry *base, int len)
2686{
2687 struct dentry *ret = lookup_one_len_unlocked(name, base, len);
Al Viro2fa6b1e2019-11-12 16:13:06 -05002688 if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) {
Al Viro6c2d47982019-10-31 01:21:58 -04002689 dput(ret);
2690 ret = ERR_PTR(-ENOENT);
2691 }
2692 return ret;
2693}
2694EXPORT_SYMBOL(lookup_positive_unlocked);
2695
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002696#ifdef CONFIG_UNIX98_PTYS
2697int path_pts(struct path *path)
2698{
2699 /* Find something mounted on "pts" in the same directory as
2700 * the input path.
2701 */
Al Viroa6a7eb72020-03-11 13:05:03 -04002702 struct dentry *parent = dget_parent(path->dentry);
2703 struct dentry *child;
Al Viro19f60282020-02-26 20:09:37 -05002704 struct qstr this = QSTR_INIT("pts", 3);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002705
Al Viroa6a7eb72020-03-11 13:05:03 -04002706 if (unlikely(!path_connected(path->mnt, parent))) {
2707 dput(parent);
Al Viro63b27722020-02-24 16:01:19 -05002708 return -ENOENT;
Al Viroa6a7eb72020-03-11 13:05:03 -04002709 }
Al Viro63b27722020-02-24 16:01:19 -05002710 dput(path->dentry);
2711 path->dentry = parent;
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002712 child = d_hash_and_lookup(parent, &this);
2713 if (!child)
2714 return -ENOENT;
2715
2716 path->dentry = child;
2717 dput(parent);
Al Viro19f60282020-02-26 20:09:37 -05002718 follow_down(path);
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002719 return 0;
2720}
2721#endif
2722
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002723int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2724 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725{
Al Viroabc9f5b2015-05-12 16:53:42 -04002726 return filename_lookup(dfd, getname_flags(name, flags, empty),
2727 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
Al Virob853a162015-05-13 09:12:02 -04002729EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002730
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002731int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002733 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002734
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002735 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002737 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002739 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002741EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
2743/*
2744 * Check whether we can remove a link victim from directory dir, check
2745 * whether the type of victim is right.
2746 * 1. We can't do it if dir is read-only (done in permission())
2747 * 2. We should have write and exec permissions on dir
2748 * 3. We can't remove anything from append-only dir
2749 * 4. We can't do anything with immutable dir (done in permission())
2750 * 5. If the sticky bit on dir is set we should either
2751 * a. be owner of dir, or
2752 * b. be owner of victim, or
2753 * c. have CAP_FOWNER capability
2754 * 6. If the victim is append-only or immutable we can't do antyhing with
2755 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002756 * 7. If the victim has an unknown uid or gid we can't change the inode.
2757 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2758 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2759 * 10. We can't remove a root or mountpoint.
2760 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 * nfs_async_unlink().
2762 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002763static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764{
David Howells63afdfc2015-05-06 15:59:00 +01002765 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 int error;
2767
David Howellsb18825a2013-09-12 19:22:53 +01002768 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002770 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 BUG_ON(victim->d_parent->d_inode != dir);
Eric W. Biederman593d1ce2017-09-14 12:07:32 -05002773
2774 /* Inode writeback is not safe when the uid or gid are invalid. */
2775 if (!uid_valid(inode->i_uid) || !gid_valid(inode->i_gid))
2776 return -EOVERFLOW;
2777
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002778 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002780 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 if (error)
2782 return error;
2783 if (IS_APPEND(dir))
2784 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002785
2786 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002787 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 return -EPERM;
2789 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002790 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return -ENOTDIR;
2792 if (IS_ROOT(victim))
2793 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002794 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 return -EISDIR;
2796 if (IS_DEADDIR(dir))
2797 return -ENOENT;
2798 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2799 return -EBUSY;
2800 return 0;
2801}
2802
2803/* Check whether we can create an object with dentry child in directory
2804 * dir.
2805 * 1. We can't do it if child already exists (open has special treatment for
2806 * this case, but since we are inlined it's OK)
2807 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002808 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2809 * 4. We should have write and exec permissions on dir
2810 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002812static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002814 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002815 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 if (child->d_inode)
2817 return -EEXIST;
2818 if (IS_DEADDIR(dir))
2819 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002820 s_user_ns = dir->i_sb->s_user_ns;
2821 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2822 !kgid_has_mapping(s_user_ns, current_fsgid()))
2823 return -EOVERFLOW;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002824 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/*
2828 * p1 and p2 should be directories on the same fs.
2829 */
2830struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2831{
2832 struct dentry *p;
2833
2834 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002835 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 return NULL;
2837 }
2838
Al Virofc640052016-04-10 01:33:30 -04002839 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002841 p = d_ancestor(p2, p1);
2842 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002843 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2844 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002845 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
2847
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002848 p = d_ancestor(p1, p2);
2849 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002850 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2851 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002852 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 }
2854
Al Viro59551022016-01-22 15:40:57 -05002855 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2856 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 return NULL;
2858}
Al Viro4d359502014-03-14 12:20:17 -04002859EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861void unlock_rename(struct dentry *p1, struct dentry *p2)
2862{
Al Viro59551022016-01-22 15:40:57 -05002863 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002865 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002866 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 }
2868}
Al Viro4d359502014-03-14 12:20:17 -04002869EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002871int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2872 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002874 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 if (error)
2876 return error;
2877
Al Viroacfa4382008-12-04 10:06:33 -05002878 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return -EACCES; /* shouldn't it be ENOSYS? */
2880 mode &= S_IALLUGO;
2881 mode |= S_IFREG;
2882 error = security_inode_create(dir, dentry, mode);
2883 if (error)
2884 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002885 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002886 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002887 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return error;
2889}
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +02002890EXPORT_SYMBOL_NS(vfs_create, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002892int vfs_mkobj(struct dentry *dentry, umode_t mode,
Al Viro8e6c8482017-12-01 17:12:45 -05002893 int (*f)(struct dentry *, umode_t, void *),
2894 void *arg)
2895{
2896 struct inode *dir = dentry->d_parent->d_inode;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002897 int error = may_create(dir, dentry);
Al Viro8e6c8482017-12-01 17:12:45 -05002898 if (error)
2899 return error;
2900
2901 mode &= S_IALLUGO;
2902 mode |= S_IFREG;
2903 error = security_inode_create(dir, dentry, mode);
2904 if (error)
2905 return error;
2906 error = f(dentry, mode, arg);
2907 if (!error)
2908 fsnotify_create(dir, dentry);
2909 return error;
2910}
2911EXPORT_SYMBOL(vfs_mkobj);
2912
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002913bool may_open_dev(const struct path *path)
2914{
2915 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2916 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2917}
2918
Al Virof0bb5aa2016-11-20 20:27:12 -05002919static int may_open(const struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002921 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 struct inode *inode = dentry->d_inode;
2923 int error;
2924
2925 if (!inode)
2926 return -ENOENT;
2927
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002928 switch (inode->i_mode & S_IFMT) {
2929 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002931 case S_IFDIR:
Kees Cook901e02c2020-08-13 16:17:22 -07002932 if (acc_mode & MAY_WRITE)
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002933 return -EISDIR;
Kees Cook901e02c2020-08-13 16:17:22 -07002934 if (acc_mode & MAY_EXEC)
2935 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002936 break;
2937 case S_IFBLK:
2938 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002939 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return -EACCES;
Greg Kroah-Hartmanaf494fd2020-08-14 09:11:29 +02002941 fallthrough;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002942 case S_IFIFO:
2943 case S_IFSOCK:
Greg Kroah-Hartmanaf494fd2020-08-14 09:11:29 +02002944 if (acc_mode & MAY_EXEC)
2945 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002947 break;
Kees Cook0fd338b2020-08-11 18:36:30 -07002948 case S_IFREG:
2949 if ((acc_mode & MAY_EXEC) && path_noexec(path))
2950 return -EACCES;
2951 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002952 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002953
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02002954 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002955 if (error)
2956 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 /*
2959 * An append-only file must be opened in append mode for writing.
2960 */
2961 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002962 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002963 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002965 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967
2968 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002969 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002970 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002972 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002973}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Jeff Laytone1181ee2010-12-07 16:19:50 -05002975static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002976{
Al Virof0bb5aa2016-11-20 20:27:12 -05002977 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002978 struct inode *inode = path->dentry->d_inode;
2979 int error = get_write_access(inode);
2980 if (error)
2981 return error;
2982 /*
2983 * Refuse to truncate files with mandatory locks held on them.
2984 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002985 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002986 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002987 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002988 if (!error) {
Greg Kroah-Hartman584c5472020-05-28 16:46:54 +02002989 error = do_truncate(path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05002990 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002991 filp);
Al Viro7715b522009-12-16 03:54:00 -05002992 }
2993 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002994 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Dave Hansend57999e2008-02-15 14:37:27 -08002997static inline int open_to_namei_flags(int flag)
2998{
Al Viro8a5e9292011-06-25 19:15:54 -04002999 if ((flag & O_ACCMODE) == 3)
3000 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003001 return flag;
3002}
3003
Al Virod3607752016-03-25 15:21:09 -04003004static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003005{
Seth Forshee1328c722017-01-26 14:33:46 -06003006 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003007 int error = security_path_mknod(dir, dentry, mode, 0);
3008 if (error)
3009 return error;
3010
Seth Forshee1328c722017-01-26 14:33:46 -06003011 s_user_ns = dir->dentry->d_sb->s_user_ns;
3012 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3013 !kgid_has_mapping(s_user_ns, current_fsgid()))
3014 return -EOVERFLOW;
3015
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003016 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003017 if (error)
3018 return error;
3019
3020 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3021}
3022
David Howells1acf0af2012-06-14 16:13:46 +01003023/*
3024 * Attempt to atomically look up, create and open a file from a negative
3025 * dentry.
3026 *
3027 * Returns 0 if successful. The file will have been created and attached to
3028 * @file by the filesystem calling finish_open().
3029 *
Al Viro00a07c12018-07-09 19:30:20 -04003030 * If the file was looked up only or didn't need creating, FMODE_OPENED won't
3031 * be set. The caller will need to perform the open themselves. @path will
3032 * have been updated to point to the new dentry. This may be negative.
David Howells1acf0af2012-06-14 16:13:46 +01003033 *
3034 * Returns an error code otherwise.
3035 */
Al Viro239eb982020-01-09 14:12:40 -05003036static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry,
3037 struct file *file,
Al Viro239eb982020-01-09 14:12:40 -05003038 int open_flag, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003039{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003040 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003041 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003042 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003043
Miklos Szeredid18e9002012-06-05 15:10:17 +02003044 if (nd->flags & LOOKUP_DIRECTORY)
3045 open_flag |= O_DIRECTORY;
3046
Al Viro30d90492012-06-22 12:40:19 +04003047 file->f_path.dentry = DENTRY_NOT_SET;
3048 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003049 error = dir->i_op->atomic_open(dir, dentry, file,
Al Viro44907d72018-06-08 13:32:02 -04003050 open_to_namei_flags(open_flag), mode);
Al Viro6fbd0712016-04-28 11:50:59 -04003051 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003052 if (!error) {
Al Viro64e1ac42018-07-09 19:17:52 -04003053 if (file->f_mode & FMODE_OPENED) {
Al Viro6fb968c2020-01-26 09:53:19 -05003054 if (unlikely(dentry != file->f_path.dentry)) {
3055 dput(dentry);
3056 dentry = dget(file->f_path.dentry);
Al Viro64e1ac42018-07-09 19:17:52 -04003057 }
Al Viro64e1ac42018-07-09 19:17:52 -04003058 } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003059 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003060 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003061 if (file->f_path.dentry) {
3062 dput(dentry);
3063 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003064 }
Al Viro239eb982020-01-09 14:12:40 -05003065 if (unlikely(d_is_negative(dentry)))
Al Viroa01e7182016-06-07 21:53:51 -04003066 error = -ENOENT;
Sage Weil62b2ce92012-08-15 13:30:12 -07003067 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003068 }
Al Viro239eb982020-01-09 14:12:40 -05003069 if (error) {
3070 dput(dentry);
3071 dentry = ERR_PTR(error);
3072 }
3073 return dentry;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003074}
3075
Nick Piggin31e6b012011-01-07 17:49:52 +11003076/*
David Howells1acf0af2012-06-14 16:13:46 +01003077 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003078 *
Al Viro00a07c12018-07-09 19:30:20 -04003079 * Must be called with parent locked (exclusive in O_CREAT case).
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003080 *
Al Viro00a07c12018-07-09 19:30:20 -04003081 * Returns 0 on success, that is, if
3082 * the file was successfully atomically created (if necessary) and opened, or
3083 * the file was not completely opened at this time, though lookups and
3084 * creations were performed.
3085 * These case are distinguished by presence of FMODE_OPENED on file->f_mode.
3086 * In the latter case dentry returned in @path might be negative if O_CREAT
3087 * hadn't been specified.
David Howells1acf0af2012-06-14 16:13:46 +01003088 *
Al Viro00a07c12018-07-09 19:30:20 -04003089 * An error code is returned on failure.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003090 */
Al Viroda5ebf52020-01-09 14:25:14 -05003091static struct dentry *lookup_open(struct nameidata *nd, struct file *file,
3092 const struct open_flags *op,
3093 bool got_write)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003094{
3095 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003096 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003097 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003098 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003099 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003100 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003101 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003102
Al Viroce8644f2016-04-26 14:17:56 -04003103 if (unlikely(IS_DEADDIR(dir_inode)))
Al Viroda5ebf52020-01-09 14:25:14 -05003104 return ERR_PTR(-ENOENT);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003105
Al Viro73a09dd2018-06-08 13:22:02 -04003106 file->f_mode &= ~FMODE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003107 dentry = d_lookup(dir, &nd->last);
3108 for (;;) {
3109 if (!dentry) {
3110 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3111 if (IS_ERR(dentry))
Al Viroda5ebf52020-01-09 14:25:14 -05003112 return dentry;
Al Viro6fbd0712016-04-28 11:50:59 -04003113 }
3114 if (d_in_lookup(dentry))
3115 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003116
Al Viro6fbd0712016-04-28 11:50:59 -04003117 error = d_revalidate(dentry, nd->flags);
3118 if (likely(error > 0))
3119 break;
3120 if (error)
3121 goto out_dput;
3122 d_invalidate(dentry);
3123 dput(dentry);
3124 dentry = NULL;
3125 }
3126 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003127 /* Cached positive dentry: will open in f_op->open */
Al Viroda5ebf52020-01-09 14:25:14 -05003128 return dentry;
Al Viro6c51e512016-03-05 20:09:32 -05003129 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003130
Al Viro1643b432016-04-27 19:14:10 -04003131 /*
3132 * Checking write permission is tricky, bacuse we don't know if we are
3133 * going to actually need it: O_CREAT opens should work as long as the
3134 * file exists. But checking existence breaks atomicity. The trick is
3135 * to check access and if not granted clear O_CREAT from the flags.
3136 *
3137 * Another problem is returing the "right" error value (e.g. for an
3138 * O_EXCL open we want to return EEXIST not EROFS).
3139 */
Al Viro99a4a902020-03-12 14:07:27 -04003140 if (unlikely(!got_write))
3141 open_flag &= ~O_TRUNC;
Al Viro1643b432016-04-27 19:14:10 -04003142 if (open_flag & O_CREAT) {
Al Viro99a4a902020-03-12 14:07:27 -04003143 if (open_flag & O_EXCL)
3144 open_flag &= ~O_TRUNC;
Al Viro1643b432016-04-27 19:14:10 -04003145 if (!IS_POSIXACL(dir->d_inode))
3146 mode &= ~current_umask();
Al Viro99a4a902020-03-12 14:07:27 -04003147 if (likely(got_write))
Al Viro1643b432016-04-27 19:14:10 -04003148 create_error = may_o_create(&nd->path, dentry, mode);
Al Viro99a4a902020-03-12 14:07:27 -04003149 else
3150 create_error = -EROFS;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003151 }
Al Viro99a4a902020-03-12 14:07:27 -04003152 if (create_error)
3153 open_flag &= ~O_CREAT;
Al Viro1643b432016-04-27 19:14:10 -04003154 if (dir_inode->i_op->atomic_open) {
Al Virod489cf92020-03-11 08:07:53 -04003155 dentry = atomic_open(nd, dentry, file, open_flag, mode);
Al Viroda5ebf52020-01-09 14:25:14 -05003156 if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT))
3157 dentry = ERR_PTR(create_error);
3158 return dentry;
Al Viro1643b432016-04-27 19:14:10 -04003159 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003160
Al Viro6fbd0712016-04-28 11:50:59 -04003161 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003162 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3163 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003164 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003165 if (unlikely(res)) {
3166 if (IS_ERR(res)) {
3167 error = PTR_ERR(res);
3168 goto out_dput;
3169 }
3170 dput(dentry);
3171 dentry = res;
3172 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003173 }
3174
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003175 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003176 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro73a09dd2018-06-08 13:22:02 -04003177 file->f_mode |= FMODE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003178 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003179 if (!dir_inode->i_op->create) {
3180 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003181 goto out_dput;
Al Viro64894cf2012-07-31 00:53:35 +04003182 }
Al Viroce8644f2016-04-26 14:17:56 -04003183 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003184 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003185 if (error)
3186 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003187 }
Al Viro1643b432016-04-27 19:14:10 -04003188 if (unlikely(create_error) && !dentry->d_inode) {
3189 error = create_error;
3190 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003191 }
Al Viroda5ebf52020-01-09 14:25:14 -05003192 return dentry;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003193
3194out_dput:
3195 dput(dentry);
Al Viroda5ebf52020-01-09 14:25:14 -05003196 return ERR_PTR(error);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003197}
3198
Al Viroc981a482020-01-26 11:06:21 -05003199static const char *open_last_lookups(struct nameidata *nd,
Al Viro3ec2eef2018-06-08 13:43:47 -04003200 struct file *file, const struct open_flags *op)
Al Virofb1cc552009-12-24 01:58:28 -05003201{
Al Viroa1e28032009-12-24 02:12:06 -05003202 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003203 int open_flag = op->open_flag;
Al Viro64894cf2012-07-31 00:53:35 +04003204 bool got_write = false;
Al Viro254cf582015-05-05 09:40:46 -04003205 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003206 struct inode *inode;
Al Viroda5ebf52020-01-09 14:25:14 -05003207 struct dentry *dentry;
Al Virob0417d22020-01-14 13:34:20 -05003208 const char *res;
Al Virofb1cc552009-12-24 01:58:28 -05003209
Al Viroc3e380b2011-02-23 13:39:45 -05003210 nd->flags |= op->intent;
3211
Al Virobc77daa2013-06-06 09:12:33 -04003212 if (nd->last_type != LAST_NORM) {
Al Viro56676ec2020-03-10 21:54:54 -04003213 if (nd->depth)
3214 put_link(nd);
Al Viroff326a32020-03-10 10:19:24 -04003215 return handle_dots(nd, nd->last_type);
Al Viro1f36f772009-12-26 10:56:19 -05003216 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003217
Al Viroca344a892011-03-09 00:36:45 -05003218 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003219 if (nd->last.name[nd->last.len])
3220 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3221 /* we _can_ be in RCU mode here */
Al Viro20e34352020-01-09 14:58:31 -05003222 dentry = lookup_fast(nd, &inode, &seq);
3223 if (IS_ERR(dentry))
Al Viro1ccac622020-01-14 10:13:40 -05003224 return ERR_CAST(dentry);
Al Viro20e34352020-01-09 14:58:31 -05003225 if (likely(dentry))
Miklos Szeredi71574862012-06-05 15:10:14 +02003226 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003227
Al Viro6583fe22016-03-05 18:14:03 -05003228 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003229 } else {
3230 /* create side of things */
Al Viro72287412020-03-10 10:09:26 -04003231 if (nd->flags & LOOKUP_RCU) {
Jens Axboe01fd84a2020-12-17 09:19:08 -07003232 if (!try_to_unlazy(nd))
3233 return ERR_PTR(-ECHILD);
Al Viro72287412020-03-10 10:09:26 -04003234 }
Al Viroc9b07ea2019-07-14 13:22:27 -04003235 audit_inode(nd->name, dir, AUDIT_INODE_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003236 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003237 if (unlikely(nd->last.name[nd->last.len]))
Al Viro1ccac622020-01-14 10:13:40 -05003238 return ERR_PTR(-EISDIR);
Al Virofe2d35f2011-03-05 22:58:25 -05003239 }
3240
Al Viro9cf843e2016-04-28 19:35:16 -04003241 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Jens Axboe01fd84a2020-12-17 09:19:08 -07003242 got_write = !mnt_want_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003243 /*
3244 * do _not_ fail yet - we might not need that or fail with
3245 * a different error; let lookup_open() decide; we'll be
3246 * dropping this one anyway.
3247 */
3248 }
Al Viro9cf843e2016-04-28 19:35:16 -04003249 if (open_flag & O_CREAT)
3250 inode_lock(dir->d_inode);
3251 else
3252 inode_lock_shared(dir->d_inode);
Al Viroda5ebf52020-01-09 14:25:14 -05003253 dentry = lookup_open(nd, file, op, got_write);
Al Virof7bb9592020-03-05 13:25:20 -05003254 if (!IS_ERR(dentry) && (file->f_mode & FMODE_CREATED))
3255 fsnotify_create(dir->d_inode, dentry);
Al Viro9cf843e2016-04-28 19:35:16 -04003256 if (open_flag & O_CREAT)
3257 inode_unlock(dir->d_inode);
3258 else
3259 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003260
Al Viroc981a482020-01-26 11:06:21 -05003261 if (got_write)
Al Viro59e96e62020-01-26 10:22:24 -05003262 mnt_drop_write(nd->path.mnt);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003263
Al Viro59e96e62020-01-26 10:22:24 -05003264 if (IS_ERR(dentry))
3265 return ERR_CAST(dentry);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003266
Al Viro973d4b72020-01-26 10:48:16 -05003267 if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) {
Al Viroe73cabf2020-01-09 14:30:08 -05003268 dput(nd->path.dentry);
3269 nd->path.dentry = dentry;
Al Viroc981a482020-01-26 11:06:21 -05003270 return NULL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003271 }
Al Virofb1cc552009-12-24 01:58:28 -05003272
Al Viro20e34352020-01-09 14:58:31 -05003273finish_lookup:
Al Viro56676ec2020-03-10 21:54:54 -04003274 if (nd->depth)
3275 put_link(nd);
Al Viro8c4efe22020-01-19 12:44:18 -05003276 res = step_into(nd, WALK_TRAILING, dentry, inode, seq);
Al Viroff326a32020-03-10 10:19:24 -04003277 if (unlikely(res))
Al Virob0417d22020-01-14 13:34:20 -05003278 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viroff326a32020-03-10 10:19:24 -04003279 return res;
Al Viroc981a482020-01-26 11:06:21 -05003280}
3281
3282/*
3283 * Handle the last step of open()
3284 */
Al Viroc5971b82020-03-05 11:41:29 -05003285static int do_open(struct nameidata *nd,
Al Viroc981a482020-01-26 11:06:21 -05003286 struct file *file, const struct open_flags *op)
3287{
Al Viroc981a482020-01-26 11:06:21 -05003288 int open_flag = op->open_flag;
3289 bool do_truncate;
3290 int acc_mode;
Al Viroc981a482020-01-26 11:06:21 -05003291 int error;
3292
Al Viroff326a32020-03-10 10:19:24 -04003293 if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) {
3294 error = complete_walk(nd);
3295 if (error)
3296 return error;
3297 }
Al Viro973d4b72020-01-26 10:48:16 -05003298 if (!(file->f_mode & FMODE_CREATED))
3299 audit_inode(nd->name, nd->path.dentry, 0);
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003300 if (open_flag & O_CREAT) {
Al Virob94e0b32020-03-10 10:13:53 -04003301 if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED))
3302 return -EEXIST;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003303 if (d_is_dir(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003304 return -EISDIR;
Al Viro0f705952020-03-05 11:34:48 -05003305 error = may_create_in_sticky(nd->dir_mode, nd->dir_uid,
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003306 d_backing_inode(nd->path.dentry));
3307 if (unlikely(error))
Al Viroc5971b82020-03-05 11:41:29 -05003308 return error;
Salvatore Mesoraca30aba662018-08-23 17:00:35 -07003309 }
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003310 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viroc5971b82020-03-05 11:41:29 -05003311 return -ENOTDIR;
Al Viro6c0d46c2011-03-09 00:59:59 -05003312
Al Viro8795e7d2020-01-26 10:38:17 -05003313 do_truncate = false;
3314 acc_mode = op->acc_mode;
Al Viro648fa862009-12-24 01:26:48 -05003315 if (file->f_mode & FMODE_CREATED) {
3316 /* Don't check for write permission, don't truncate */
3317 open_flag &= ~O_TRUNC;
Al Virofb1cc552009-12-24 01:58:28 -05003318 acc_mode = 0;
Al Viro8795e7d2020-01-26 10:38:17 -05003319 } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) {
Al Viro0f9d1a12011-03-09 00:13:14 -05003320 error = mnt_want_write(nd->path.mnt);
3321 if (error)
Al Viroc5971b82020-03-05 11:41:29 -05003322 return error;
Al Viro8795e7d2020-01-26 10:38:17 -05003323 do_truncate = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003324 }
Al Viro6ac08702016-04-26 00:02:50 -04003325 error = may_open(&nd->path, acc_mode, open_flag);
Al Viro8795e7d2020-01-26 10:38:17 -05003326 if (!error && !(file->f_mode & FMODE_OPENED))
Al Viro3ad56152020-01-26 10:06:13 -05003327 error = vfs_open(&nd->path, file);
Al Viro8795e7d2020-01-26 10:38:17 -05003328 if (!error)
3329 error = ima_file_check(file, op->acc_mode);
3330 if (!error && do_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003331 error = handle_truncate(file);
Al Viroc80567c2016-02-27 19:17:33 -05003332 if (unlikely(error > 0)) {
3333 WARN_ON(1);
3334 error = -EINVAL;
3335 }
Al Viro8795e7d2020-01-26 10:38:17 -05003336 if (do_truncate)
Al Viro0f9d1a12011-03-09 00:13:14 -05003337 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003338 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003339}
3340
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003341struct dentry *vfs_tmpfile(struct dentry *dentry, umode_t mode, int open_flag)
3342{
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003343 struct dentry *child = NULL;
3344 struct inode *dir = dentry->d_inode;
3345 struct inode *inode;
3346 int error;
3347
3348 /* we want directory to be writable */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003349 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003350 if (error)
3351 goto out_err;
3352 error = -EOPNOTSUPP;
3353 if (!dir->i_op->tmpfile)
3354 goto out_err;
3355 error = -ENOMEM;
David Howellscdf01222017-07-04 17:25:22 +01003356 child = d_alloc(dentry, &slash_name);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003357 if (unlikely(!child))
3358 goto out_err;
3359 error = dir->i_op->tmpfile(dir, child, mode);
3360 if (error)
3361 goto out_err;
3362 error = -ENOENT;
3363 inode = child->d_inode;
3364 if (unlikely(!inode))
3365 goto out_err;
3366 if (!(open_flag & O_EXCL)) {
3367 spin_lock(&inode->i_lock);
3368 inode->i_state |= I_LINKABLE;
3369 spin_unlock(&inode->i_lock);
3370 }
Mimi Zoharfdb24102019-01-22 14:06:49 -06003371 ima_post_create_tmpfile(inode);
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003372 return child;
3373
3374out_err:
3375 dput(child);
3376 return ERR_PTR(error);
3377}
3378EXPORT_SYMBOL(vfs_tmpfile);
3379
Al Viroc8a53ee2015-05-12 18:43:07 -04003380static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003381 const struct open_flags *op,
Al Viro3ec2eef2018-06-08 13:43:47 -04003382 struct file *file)
Al Viro60545d02013-06-07 01:20:27 -04003383{
Al Viro625b6d12015-05-12 16:36:12 -04003384 struct dentry *child;
Al Viro625b6d12015-05-12 16:36:12 -04003385 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003386 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003387 if (unlikely(error))
3388 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003389 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003390 if (unlikely(error))
3391 goto out;
Amir Goldsteinaf7bd4d2017-01-17 06:34:52 +02003392 child = vfs_tmpfile(path.dentry, op->mode, op->open_flag);
3393 error = PTR_ERR(child);
Hirofumi Nakagawa684e73b2017-09-26 03:21:26 +09003394 if (IS_ERR(child))
Al Viro60545d02013-06-07 01:20:27 -04003395 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003396 dput(path.dentry);
3397 path.dentry = child;
Al Viroc8a53ee2015-05-12 18:43:07 -04003398 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003399 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003400 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003401 if (error)
3402 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003403 file->f_path.mnt = path.mnt;
Al Virobe12af32018-06-08 11:44:56 -04003404 error = finish_open(file, child, NULL);
Al Viro60545d02013-06-07 01:20:27 -04003405out2:
Al Viro625b6d12015-05-12 16:36:12 -04003406 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003407out:
Al Viro625b6d12015-05-12 16:36:12 -04003408 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003409 return error;
3410}
3411
Al Viro6ac08702016-04-26 00:02:50 -04003412static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3413{
3414 struct path path;
3415 int error = path_lookupat(nd, flags, &path);
3416 if (!error) {
3417 audit_inode(nd->name, path.dentry, 0);
Al Viroae2bb292018-07-10 13:22:28 -04003418 error = vfs_open(&path, file);
Al Viro6ac08702016-04-26 00:02:50 -04003419 path_put(&path);
3420 }
3421 return error;
3422}
3423
Al Viroc8a53ee2015-05-12 18:43:07 -04003424static struct file *path_openat(struct nameidata *nd,
3425 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426{
Al Viro30d90492012-06-22 12:40:19 +04003427 struct file *file;
Al Viro13aab422011-02-23 17:54:08 -05003428 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003429
Al Viroea73ea72018-07-11 15:00:04 -04003430 file = alloc_empty_file(op->open_flag, current_cred());
Al Viro1afc99b2013-02-14 20:41:04 -05003431 if (IS_ERR(file))
3432 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003433
Al Virobb458c62013-07-13 13:26:37 +04003434 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viro3ec2eef2018-06-08 13:43:47 -04003435 error = do_tmpfile(nd, flags, op, file);
Al Viro5f336e72018-07-09 16:38:06 -04003436 } else if (unlikely(file->f_flags & O_PATH)) {
Al Viro6ac08702016-04-26 00:02:50 -04003437 error = do_o_path(nd, flags, file);
Al Viro5f336e72018-07-09 16:38:06 -04003438 } else {
3439 const char *s = path_init(nd, flags);
3440 while (!(error = link_path_walk(s, nd)) &&
Al Viroc5971b82020-03-05 11:41:29 -05003441 (s = open_last_lookups(nd, file, op)) != NULL)
Al Viro1ccac622020-01-14 10:13:40 -05003442 ;
Al Viroc5971b82020-03-05 11:41:29 -05003443 if (!error)
3444 error = do_open(nd, file, op);
Al Viro5f336e72018-07-09 16:38:06 -04003445 terminate_walk(nd);
Al Viro6ac08702016-04-26 00:02:50 -04003446 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003447 if (likely(!error)) {
Al Viroaad888f2018-06-08 12:58:04 -04003448 if (likely(file->f_mode & FMODE_OPENED))
Al Viro7c1c01e2018-06-08 12:56:55 -04003449 return file;
3450 WARN_ON(1);
3451 error = -EINVAL;
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003452 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003453 fput(file);
3454 if (error == -EOPENSTALE) {
3455 if (flags & LOOKUP_RCU)
3456 error = -ECHILD;
3457 else
3458 error = -ESTALE;
Al Viro2675a4e2012-06-22 12:41:10 +04003459 }
Al Viro7c1c01e2018-06-08 12:56:55 -04003460 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461}
3462
Jeff Layton669abf42012-10-10 16:43:10 -04003463struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003464 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003465{
Al Viro9883d182015-05-13 07:28:08 -04003466 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003467 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003468 struct file *filp;
3469
Al Viro9883d182015-05-13 07:28:08 -04003470 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003471 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003472 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003473 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003474 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003475 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003476 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003477 return filp;
3478}
3479
Al Viro73d049a2011-03-11 12:08:24 -05003480struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003481 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003482{
Al Viro9883d182015-05-13 07:28:08 -04003483 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003484 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003485 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003486 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003487
3488 nd.root.mnt = mnt;
3489 nd.root.dentry = dentry;
3490
David Howellsb18825a2013-09-12 19:22:53 +01003491 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003492 return ERR_PTR(-ELOOP);
3493
Paul Moore51689102015-01-22 00:00:03 -05003494 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303495 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003496 return ERR_CAST(filename);
3497
Al Viro9883d182015-05-13 07:28:08 -04003498 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003499 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003500 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003501 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003502 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003503 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003504 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003505 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003506 return file;
3507}
3508
Al Virofa14a0b2015-01-22 02:16:49 -05003509static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003510 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003512 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003513 struct qstr last;
3514 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003515 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003516 int error;
3517 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3518
3519 /*
3520 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3521 * other flags passed in are ignored!
3522 */
3523 lookup_flags &= LOOKUP_REVAL;
3524
Al Viro5c31b6c2015-05-12 17:32:54 -04003525 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3526 if (IS_ERR(name))
3527 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003529 /*
3530 * Yucky last component or no last component at all?
3531 * (foo/., foo/.., /////)
3532 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003533 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003534 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003535
Jan Karac30dabf2012-06-12 16:20:30 +02003536 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003537 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003538 /*
3539 * Do the final lookup.
3540 */
Al Viro391172c2015-05-09 11:19:16 -04003541 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003542 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003543 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003545 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003546
Al Viroa8104a92012-07-20 02:25:00 +04003547 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003548 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003549 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003550
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003551 /*
3552 * Special case - lookup gave negative, but... we had foo/bar/
3553 * From the vfs_mknod() POV we just have a negative dentry -
3554 * all is fine. Let's be bastards - you had / on the end, you've
3555 * been asking for (non-existent) directory. -ENOENT for you.
3556 */
Al Viro391172c2015-05-09 11:19:16 -04003557 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003558 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003559 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003560 }
Jan Karac30dabf2012-06-12 16:20:30 +02003561 if (unlikely(err2)) {
3562 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003563 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003564 }
Al Viro181c37b2015-05-12 17:21:25 -04003565 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567fail:
Al Viroa8104a92012-07-20 02:25:00 +04003568 dput(dentry);
3569 dentry = ERR_PTR(error);
3570unlock:
Al Viro59551022016-01-22 15:40:57 -05003571 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003572 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003573 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003574out:
Al Viro391172c2015-05-09 11:19:16 -04003575 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003576 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 return dentry;
3578}
Al Virofa14a0b2015-01-22 02:16:49 -05003579
3580struct dentry *kern_path_create(int dfd, const char *pathname,
3581 struct path *path, unsigned int lookup_flags)
3582{
Al Viro181c37b2015-05-12 17:21:25 -04003583 return filename_create(dfd, getname_kernel(pathname),
3584 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003585}
Al Virodae6ad82011-06-26 11:50:15 -04003586EXPORT_SYMBOL(kern_path_create);
3587
Al Viro921a1652012-07-20 01:15:31 +04003588void done_path_create(struct path *path, struct dentry *dentry)
3589{
3590 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003591 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003592 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003593 path_put(path);
3594}
3595EXPORT_SYMBOL(done_path_create);
3596
Al Viro520ae682015-05-13 07:00:28 -04003597inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003598 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003599{
Al Viro181c37b2015-05-12 17:21:25 -04003600 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003601}
3602EXPORT_SYMBOL(user_path_create);
3603
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003604int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003606 bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003607 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609 if (error)
3610 return error;
3611
Miklos Szeredia3c751a2020-05-14 16:44:23 +02003612 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout &&
3613 !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 return -EPERM;
3615
Al Viroacfa4382008-12-04 10:06:33 -05003616 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return -EPERM;
3618
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003619 error = devcgroup_inode_mknod(mode, dev);
3620 if (error)
3621 return error;
3622
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623 error = security_inode_mknod(dir, dentry, mode, dev);
3624 if (error)
3625 return error;
3626
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003628 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003629 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 return error;
3631}
Al Viro4d359502014-03-14 12:20:17 -04003632EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Al Virof69aac02011-07-26 04:31:40 -04003634static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003635{
3636 switch (mode & S_IFMT) {
3637 case S_IFREG:
3638 case S_IFCHR:
3639 case S_IFBLK:
3640 case S_IFIFO:
3641 case S_IFSOCK:
3642 case 0: /* zero mode translates to S_IFREG */
3643 return 0;
3644 case S_IFDIR:
3645 return -EPERM;
3646 default:
3647 return -EINVAL;
3648 }
3649}
3650
Christoph Hellwig5fee64f2020-07-22 11:41:20 +02003651static long do_mknodat(int dfd, const char __user *filename, umode_t mode,
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003652 unsigned int dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Al Viro2ad94ae2008-07-21 09:32:51 -04003654 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003655 struct path path;
3656 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003657 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Al Viro8e4bfca2012-07-20 01:17:26 +04003659 error = may_mknod(mode);
3660 if (error)
3661 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003662retry:
3663 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003664 if (IS_ERR(dentry))
3665 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003666
Al Virodae6ad82011-06-26 11:50:15 -04003667 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003668 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003669 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003670 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003671 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003672 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 case 0: case S_IFREG:
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003674 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003675 if (!error)
3676 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 break;
3678 case S_IFCHR: case S_IFBLK:
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003679 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 new_decode_dev(dev));
3681 break;
3682 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003683 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 }
Al Viroa8104a92012-07-20 02:25:00 +04003686out:
Al Viro921a1652012-07-20 01:15:31 +04003687 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003688 if (retry_estale(error, lookup_flags)) {
3689 lookup_flags |= LOOKUP_REVAL;
3690 goto retry;
3691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 return error;
3693}
3694
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003695SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
3696 unsigned int, dev)
3697{
3698 return do_mknodat(dfd, filename, mode, dev);
3699}
3700
Al Viro8208a222011-07-25 17:32:17 -04003701SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003702{
Dominik Brodowski87c4e192018-03-11 11:34:50 +01003703 return do_mknodat(AT_FDCWD, filename, mode, dev);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003704}
3705
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003706int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707{
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003708 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003709 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710
3711 if (error)
3712 return error;
3713
Al Viroacfa4382008-12-04 10:06:33 -05003714 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 return -EPERM;
3716
3717 mode &= (S_IRWXUGO|S_ISVTX);
3718 error = security_inode_mkdir(dir, dentry, mode);
3719 if (error)
3720 return error;
3721
Al Viro8de52772012-02-06 12:45:27 -05003722 if (max_links && dir->i_nlink >= max_links)
3723 return -EMLINK;
3724
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003726 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003727 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728 return error;
3729}
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +02003730EXPORT_SYMBOL_NS(vfs_mkdir, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Christoph Hellwig83ff98c2020-07-22 11:14:59 +02003732static long do_mkdirat(int dfd, const char __user *pathname, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733{
Dave Hansen6902d922006-09-30 23:29:01 -07003734 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003735 struct path path;
3736 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003737 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003739retry:
3740 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003741 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003742 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003743
Al Virodae6ad82011-06-26 11:50:15 -04003744 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003745 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003746 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003747 if (!error)
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003748 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003749 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003750 if (retry_estale(error, lookup_flags)) {
3751 lookup_flags |= LOOKUP_REVAL;
3752 goto retry;
3753 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 return error;
3755}
3756
Dominik Brodowski0101db72018-03-11 11:34:49 +01003757SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
3758{
3759 return do_mkdirat(dfd, pathname, mode);
3760}
3761
Al Viroa218d0f2011-11-21 14:59:34 -05003762SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003763{
Dominik Brodowski0101db72018-03-11 11:34:49 +01003764 return do_mkdirat(AT_FDCWD, pathname, mode);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003765}
3766
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003767int vfs_rmdir(struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768{
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003769 int error = may_delete(dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
3771 if (error)
3772 return error;
3773
Al Viroacfa4382008-12-04 10:06:33 -05003774 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 return -EPERM;
3776
Al Viro1d2ef592011-09-14 18:55:41 +01003777 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003778 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779
Sage Weil912dbc12011-05-24 13:06:11 -07003780 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003781 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003782 goto out;
3783
3784 error = security_inode_rmdir(dir, dentry);
3785 if (error)
3786 goto out;
3787
3788 error = dir->i_op->rmdir(dir, dentry);
3789 if (error)
3790 goto out;
3791
Al Viro87677122018-05-27 16:23:51 -04003792 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003793 dentry->d_inode->i_flags |= S_DEAD;
3794 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003795 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003796
3797out:
Al Viro59551022016-01-22 15:40:57 -05003798 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003799 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003800 if (!error)
Amir Goldstein0b4e8242022-01-20 23:53:04 +02003801 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 return error;
3803}
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +02003804EXPORT_SYMBOL_NS(vfs_rmdir, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02003806long do_rmdir(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
3808 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003810 struct path path;
3811 struct qstr last;
3812 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003813 unsigned int lookup_flags = 0;
3814retry:
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02003815 name = filename_parentat(dfd, name, lookup_flags,
Al Viroc1d4dd22016-06-05 16:38:18 -04003816 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003817 if (IS_ERR(name))
3818 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Al Virof5beed72015-04-30 16:09:11 -04003820 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003821 case LAST_DOTDOT:
3822 error = -ENOTEMPTY;
3823 goto exit1;
3824 case LAST_DOT:
3825 error = -EINVAL;
3826 goto exit1;
3827 case LAST_ROOT:
3828 error = -EBUSY;
3829 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003831
Al Virof5beed72015-04-30 16:09:11 -04003832 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003833 if (error)
3834 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003835
Al Viro59551022016-01-22 15:40:57 -05003836 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003837 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003839 if (IS_ERR(dentry))
3840 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003841 if (!dentry->d_inode) {
3842 error = -ENOENT;
3843 goto exit3;
3844 }
Al Virof5beed72015-04-30 16:09:11 -04003845 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003846 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003847 goto exit3;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003848 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003849exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003850 dput(dentry);
3851exit2:
Al Viro59551022016-01-22 15:40:57 -05003852 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003853 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854exit1:
Al Virof5beed72015-04-30 16:09:11 -04003855 path_put(&path);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003856 if (retry_estale(error, lookup_flags)) {
3857 lookup_flags |= LOOKUP_REVAL;
3858 goto retry;
3859 }
Al Viro24fb33d2020-08-12 05:15:18 +01003860 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return error;
3862}
3863
Heiko Carstens3cdad422009-01-14 14:14:22 +01003864SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003865{
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02003866 return do_rmdir(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003867}
3868
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003869/**
3870 * vfs_unlink - unlink a filesystem object
3871 * @dir: parent directory
3872 * @dentry: victim
3873 * @delegated_inode: returns victim inode, if the inode is delegated.
3874 *
3875 * The caller must hold dir->i_mutex.
3876 *
3877 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3878 * return a reference to the inode in delegated_inode. The caller
3879 * should then break the delegation on that inode and retry. Because
3880 * breaking a delegation may take a long time, the caller should drop
3881 * dir->i_mutex before doing so.
3882 *
3883 * Alternatively, a caller may pass NULL for delegated_inode. This may
3884 * be appropriate for callers that expect the underlying filesystem not
3885 * to be NFS exported.
3886 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003887int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003889 struct inode *target = dentry->d_inode;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003890 int error = may_delete(dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891
3892 if (error)
3893 return error;
3894
Al Viroacfa4382008-12-04 10:06:33 -05003895 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 return -EPERM;
3897
Al Viro59551022016-01-22 15:40:57 -05003898 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003899 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 error = -EBUSY;
3901 else {
3902 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003903 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003904 error = try_break_deleg(target, delegated_inode);
3905 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003906 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003908 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003909 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003910 detach_mounts(dentry);
3911 }
Al Virobec10522010-03-03 14:12:08 -05003912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003914out:
Al Viro59551022016-01-22 15:40:57 -05003915 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916
3917 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
Amir Goldstein0b4e8242022-01-20 23:53:04 +02003918 if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) {
3919 fsnotify_unlink(dir, dentry);
3920 } else if (!error) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003921 fsnotify_link_count(target);
Amir Goldstein0b4e8242022-01-20 23:53:04 +02003922 d_delete_notify(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 }
Robert Love0eeca282005-07-12 17:06:03 -04003924
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return error;
3926}
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +02003927EXPORT_SYMBOL_NS(vfs_unlink, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928
3929/*
3930 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003931 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 * writeout happening, and we don't want to prevent access to the directory
3933 * while waiting on the I/O.
3934 */
Christoph Hellwigda2f1362017-11-04 13:44:45 +03003935long do_unlinkat(int dfd, struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
Al Viro2ad94ae2008-07-21 09:32:51 -04003937 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003939 struct path path;
3940 struct qstr last;
3941 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003943 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003944 unsigned int lookup_flags = 0;
3945retry:
Christoph Hellwigda2f1362017-11-04 13:44:45 +03003946 name = filename_parentat(dfd, name, lookup_flags, &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003947 if (IS_ERR(name))
3948 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003949
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04003951 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003953
Al Virof5beed72015-04-30 16:09:11 -04003954 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003955 if (error)
3956 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003957retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05003958 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003959 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 error = PTR_ERR(dentry);
3961 if (!IS_ERR(dentry)) {
3962 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04003963 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03003964 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003966 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003967 goto slashes;
3968 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04003969 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003970 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003971 goto exit2;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02003972 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003973exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 dput(dentry);
3975 }
Al Viro59551022016-01-22 15:40:57 -05003976 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 if (inode)
3978 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003979 inode = NULL;
3980 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003981 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003982 if (!error)
3983 goto retry_deleg;
3984 }
Al Virof5beed72015-04-30 16:09:11 -04003985 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986exit1:
Al Virof5beed72015-04-30 16:09:11 -04003987 path_put(&path);
Jeff Layton5d18f812012-12-20 16:38:04 -05003988 if (retry_estale(error, lookup_flags)) {
3989 lookup_flags |= LOOKUP_REVAL;
3990 inode = NULL;
3991 goto retry;
3992 }
Christoph Hellwigda2f1362017-11-04 13:44:45 +03003993 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 return error;
3995
3996slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003997 if (d_is_negative(dentry))
3998 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003999 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004000 error = -EISDIR;
4001 else
4002 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 goto exit2;
4004}
4005
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004006SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004007{
4008 if ((flag & ~AT_REMOVEDIR) != 0)
4009 return -EINVAL;
4010
4011 if (flag & AT_REMOVEDIR)
Christoph Hellwige24ab0e2020-07-21 10:48:15 +02004012 return do_rmdir(dfd, getname(pathname));
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004013 return do_unlinkat(dfd, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004014}
4015
Heiko Carstens3480b252009-01-14 14:14:16 +01004016SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004017{
Christoph Hellwigda2f1362017-11-04 13:44:45 +03004018 return do_unlinkat(AT_FDCWD, getname(pathname));
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004019}
4020
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004021int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022{
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004023 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024
4025 if (error)
4026 return error;
4027
Al Viroacfa4382008-12-04 10:06:33 -05004028 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 return -EPERM;
4030
4031 error = security_inode_symlink(dir, dentry, oldname);
4032 if (error)
4033 return error;
4034
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004036 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004037 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 return error;
4039}
Al Viro4d359502014-03-14 12:20:17 -04004040EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041
Christoph Hellwigcd3acb62020-07-22 11:14:36 +02004042static long do_symlinkat(const char __user *oldname, int newdfd,
Dominik Brodowskib724e842018-03-11 11:34:49 +01004043 const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Al Viro2ad94ae2008-07-21 09:32:51 -04004045 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004046 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004047 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004048 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004049 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
4051 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004052 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004054retry:
4055 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004056 error = PTR_ERR(dentry);
4057 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004058 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004059
Jeff Layton91a27b22012-10-10 15:25:28 -04004060 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004061 if (!error)
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004062 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004063 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004064 if (retry_estale(error, lookup_flags)) {
4065 lookup_flags |= LOOKUP_REVAL;
4066 goto retry;
4067 }
Dave Hansen6902d922006-09-30 23:29:01 -07004068out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 putname(from);
4070 return error;
4071}
4072
Dominik Brodowskib724e842018-03-11 11:34:49 +01004073SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4074 int, newdfd, const char __user *, newname)
4075{
4076 return do_symlinkat(oldname, newdfd, newname);
4077}
4078
Heiko Carstens3480b252009-01-14 14:14:16 +01004079SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004080{
Dominik Brodowskib724e842018-03-11 11:34:49 +01004081 return do_symlinkat(oldname, AT_FDCWD, newname);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004082}
4083
J. Bruce Fields146a8592011-09-20 17:14:31 -04004084/**
4085 * vfs_link - create a new link
4086 * @old_dentry: object to be linked
4087 * @dir: new parent
4088 * @new_dentry: where to create the new link
4089 * @delegated_inode: returns inode needing a delegation break
4090 *
4091 * The caller must hold dir->i_mutex
4092 *
4093 * If vfs_link discovers a delegation on the to-be-linked file in need
4094 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4095 * inode in delegated_inode. The caller should then break the delegation
4096 * and retry. Because breaking a delegation may take a long time, the
4097 * caller should drop the i_mutex before doing so.
4098 *
4099 * Alternatively, a caller may pass NULL for delegated_inode. This may
4100 * be appropriate for callers that expect the underlying filesystem not
4101 * to be NFS exported.
4102 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004103int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004106 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 int error;
4108
4109 if (!inode)
4110 return -ENOENT;
4111
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004112 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 if (error)
4114 return error;
4115
4116 if (dir->i_sb != inode->i_sb)
4117 return -EXDEV;
4118
4119 /*
4120 * A link to an append-only or immutable file cannot be created.
4121 */
4122 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4123 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004124 /*
4125 * Updating the link count will likely cause i_uid and i_gid to
4126 * be writen back improperly if their true value is unknown to
4127 * the vfs.
4128 */
4129 if (HAS_UNMAPPED_ID(inode))
4130 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004131 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004133 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 return -EPERM;
4135
4136 error = security_inode_link(old_dentry, dir, new_dentry);
4137 if (error)
4138 return error;
4139
Al Viro59551022016-01-22 15:40:57 -05004140 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304141 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004142 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304143 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004144 else if (max_links && inode->i_nlink >= max_links)
4145 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004146 else {
4147 error = try_break_deleg(inode, delegated_inode);
4148 if (!error)
4149 error = dir->i_op->link(old_dentry, dir, new_dentry);
4150 }
Al Virof4e0c302013-06-11 08:34:36 +04004151
4152 if (!error && (inode->i_state & I_LINKABLE)) {
4153 spin_lock(&inode->i_lock);
4154 inode->i_state &= ~I_LINKABLE;
4155 spin_unlock(&inode->i_lock);
4156 }
Al Viro59551022016-01-22 15:40:57 -05004157 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004158 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004159 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 return error;
4161}
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +02004162EXPORT_SYMBOL_NS(vfs_link, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164/*
4165 * Hardlinks are often used in delicate situations. We avoid
4166 * security-related surprises by not following symlinks on the
4167 * newname. --KAB
4168 *
4169 * We don't follow them on the oldname either to be compatible
4170 * with linux 2.0, and to avoid hard-linking to directories
4171 * and other special files. --ADM
4172 */
Christoph Hellwig812931d2020-07-22 11:14:19 +02004173static int do_linkat(int olddfd, const char __user *oldname, int newdfd,
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004174 const char __user *newname, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004177 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004178 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304179 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304182 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004183 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304184 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004185 * To use null names we require CAP_DAC_READ_SEARCH
4186 * This ensures that not everyone will be able to create
4187 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304188 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004189 if (flags & AT_EMPTY_PATH) {
4190 if (!capable(CAP_DAC_READ_SEARCH))
4191 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304192 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004193 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004194
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304195 if (flags & AT_SYMLINK_FOLLOW)
4196 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004197retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304198 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004200 return error;
4201
Jeff Layton442e31c2012-12-20 16:15:38 -05004202 new_dentry = user_path_create(newdfd, newname, &new_path,
4203 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004205 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004206 goto out;
4207
4208 error = -EXDEV;
4209 if (old_path.mnt != new_path.mnt)
4210 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004211 error = may_linkat(&old_path);
4212 if (unlikely(error))
4213 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004214 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004215 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004216 goto out_dput;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004217 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004218out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004219 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004220 if (delegated_inode) {
4221 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004222 if (!error) {
4223 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004224 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004225 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004226 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004227 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004228 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004229 how |= LOOKUP_REVAL;
4230 goto retry;
4231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232out:
Al Viro2d8f3032008-07-22 09:59:21 -04004233 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
4235 return error;
4236}
4237
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004238SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4239 int, newdfd, const char __user *, newname, int, flags)
4240{
4241 return do_linkat(olddfd, oldname, newdfd, newname, flags);
4242}
4243
Heiko Carstens3480b252009-01-14 14:14:16 +01004244SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004245{
Dominik Brodowski46ea89e2018-03-11 11:34:53 +01004246 return do_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004247}
4248
Miklos Szeredibc270272014-04-01 17:08:42 +02004249/**
4250 * vfs_rename - rename a filesystem object
4251 * @old_dir: parent of source
4252 * @old_dentry: source
4253 * @new_dir: parent of destination
4254 * @new_dentry: destination
4255 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004256 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004257 *
4258 * The caller must hold multiple mutexes--see lock_rename()).
4259 *
4260 * If vfs_rename discovers a delegation in need of breaking at either
4261 * the source or destination, it will return -EWOULDBLOCK and return a
4262 * reference to the inode in delegated_inode. The caller should then
4263 * break the delegation and retry. Because breaking a delegation may
4264 * take a long time, the caller should drop all locks before doing
4265 * so.
4266 *
4267 * Alternatively, a caller may pass NULL for delegated_inode. This may
4268 * be appropriate for callers that expect the underlying filesystem not
4269 * to be NFS exported.
4270 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 * The worst of all namespace operations - renaming directory. "Perverted"
4272 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4273 * Problems:
Mauro Carvalho Chehab0117d422017-05-12 07:45:42 -03004274 *
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004275 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 * b) race potential - two innocent renames can create a loop together.
4277 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004278 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004280 * c) we have to lock _four_ objects - parents and victim (if it exists),
4281 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004282 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 * whether the target exists). Solution: try to be smart with locking
4284 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004285 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 * move will be locked. Thus we can rank directories by the tree
4287 * (ancestors first) and rank all non-directories after them.
4288 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004289 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 * HOWEVER, it relies on the assumption that any object with ->lookup()
4291 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4292 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004293 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004294 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004296 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 * locking].
4298 */
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004299int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004300 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004301 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302{
4303 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004304 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004305 struct inode *source = old_dentry->d_inode;
4306 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004307 bool new_is_dir = false;
4308 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viro49d31c22017-07-07 14:51:19 -04004309 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004311 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004312 return 0;
4313
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004314 error = may_delete(old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 if (error)
4316 return error;
4317
Miklos Szeredida1ce062014-04-01 17:08:43 +02004318 if (!target) {
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004319 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004320 } else {
4321 new_is_dir = d_is_dir(new_dentry);
4322
4323 if (!(flags & RENAME_EXCHANGE))
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004324 error = may_delete(new_dir, new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004325 else
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004326 error = may_delete(new_dir, new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 if (error)
4329 return error;
4330
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004331 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 return -EPERM;
4333
Miklos Szeredibc270272014-04-01 17:08:42 +02004334 /*
4335 * If we are going to change the parent - check write permissions,
4336 * we'll need to flip '..'.
4337 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004338 if (new_dir != old_dir) {
4339 if (is_dir) {
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004340 error = inode_permission(source, MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004341 if (error)
4342 return error;
4343 }
4344 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004345 error = inode_permission(target, MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004346 if (error)
4347 return error;
4348 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004349 }
Robert Love0eeca282005-07-12 17:06:03 -04004350
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004351 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4352 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004353 if (error)
4354 return error;
4355
Al Viro49d31c22017-07-07 14:51:19 -04004356 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004357 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004358 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004359 lock_two_nondirectories(source, target);
4360 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004361 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004362
4363 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004364 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004365 goto out;
4366
Miklos Szeredida1ce062014-04-01 17:08:43 +02004367 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004368 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004369 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004370 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004371 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4372 old_dir->i_nlink >= max_links)
4373 goto out;
4374 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004375 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004376 error = try_break_deleg(source, delegated_inode);
4377 if (error)
4378 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004379 }
4380 if (target && !new_is_dir) {
4381 error = try_break_deleg(target, delegated_inode);
4382 if (error)
4383 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004384 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004385 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004386 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004387 if (error)
4388 goto out;
4389
Miklos Szeredida1ce062014-04-01 17:08:43 +02004390 if (!(flags & RENAME_EXCHANGE) && target) {
Al Viro87677122018-05-27 16:23:51 -04004391 if (is_dir) {
4392 shrink_dcache_parent(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004393 target->i_flags |= S_DEAD;
Al Viro87677122018-05-27 16:23:51 -04004394 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004395 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004396 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004397 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004398 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4399 if (!(flags & RENAME_EXCHANGE))
4400 d_move(old_dentry, new_dentry);
4401 else
4402 d_exchange(old_dentry, new_dentry);
4403 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004404out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004405 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004406 unlock_two_nondirectories(source, target);
4407 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004408 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004409 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004410 if (!error) {
Al Virof4ec3a32019-04-26 13:21:24 -04004411 fsnotify_move(old_dir, new_dir, &old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004412 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4413 if (flags & RENAME_EXCHANGE) {
Al Virof4ec3a32019-04-26 13:21:24 -04004414 fsnotify_move(new_dir, old_dir, &old_dentry->d_name,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004415 new_is_dir, NULL, new_dentry);
4416 }
4417 }
Al Viro49d31c22017-07-07 14:51:19 -04004418 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 return error;
4421}
Greg Kroah-Hartmand483eed2020-07-02 12:51:03 +02004422EXPORT_SYMBOL_NS(vfs_rename, ANDROID_GKI_VFS_EXPORT_ONLY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004424static int do_renameat2(int olddfd, const char __user *oldname, int newdfd,
4425 const char __user *newname, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426{
Al Viro2ad94ae2008-07-21 09:32:51 -04004427 struct dentry *old_dentry, *new_dentry;
4428 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004429 struct path old_path, new_path;
4430 struct qstr old_last, new_last;
4431 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004432 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004433 struct filename *from;
4434 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004435 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004436 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004437 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004438
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004439 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004440 return -EINVAL;
4441
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004442 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4443 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004444 return -EINVAL;
4445
Al Virof5beed72015-04-30 16:09:11 -04004446 if (flags & RENAME_EXCHANGE)
4447 target_flags = 0;
4448
Jeff Laytonc6a94282012-12-11 12:10:10 -05004449retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04004450 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4451 &old_path, &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004452 if (IS_ERR(from)) {
4453 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456
Al Viroc1d4dd22016-06-05 16:38:18 -04004457 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4458 &new_path, &new_last, &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004459 if (IS_ERR(to)) {
4460 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
4464 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004465 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466 goto exit2;
4467
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004469 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470 goto exit2;
4471
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004472 if (flags & RENAME_NOREPLACE)
4473 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004474 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 goto exit2;
4476
Al Virof5beed72015-04-30 16:09:11 -04004477 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004478 if (error)
4479 goto exit2;
4480
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004481retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004482 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Al Virof5beed72015-04-30 16:09:11 -04004484 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 error = PTR_ERR(old_dentry);
4486 if (IS_ERR(old_dentry))
4487 goto exit3;
4488 /* source must exist */
4489 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004490 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004492 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 error = PTR_ERR(new_dentry);
4494 if (IS_ERR(new_dentry))
4495 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004496 error = -EEXIST;
4497 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4498 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004499 if (flags & RENAME_EXCHANGE) {
4500 error = -ENOENT;
4501 if (d_is_negative(new_dentry))
4502 goto exit5;
4503
4504 if (!d_is_dir(new_dentry)) {
4505 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004506 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004507 goto exit5;
4508 }
4509 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004510 /* unless the source is a directory trailing slashes give -ENOTDIR */
4511 if (!d_is_dir(old_dentry)) {
4512 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004513 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004514 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004515 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004516 goto exit5;
4517 }
4518 /* source should not be ancestor of target */
4519 error = -EINVAL;
4520 if (old_dentry == trap)
4521 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004523 if (!(flags & RENAME_EXCHANGE))
4524 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 if (new_dentry == trap)
4526 goto exit5;
4527
Al Virof5beed72015-04-30 16:09:11 -04004528 error = security_path_rename(&old_path, old_dentry,
4529 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004530 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004531 goto exit5;
Greg Kroah-Hartmanc3271fe2020-05-28 16:44:45 +02004532 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
Al Virof5beed72015-04-30 16:09:11 -04004533 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004534 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535exit5:
4536 dput(new_dentry);
4537exit4:
4538 dput(old_dentry);
4539exit3:
Al Virof5beed72015-04-30 16:09:11 -04004540 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004541 if (delegated_inode) {
4542 error = break_deleg_wait(&delegated_inode);
4543 if (!error)
4544 goto retry_deleg;
4545 }
Al Virof5beed72015-04-30 16:09:11 -04004546 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004548 if (retry_estale(error, lookup_flags))
4549 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004550 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004551 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552exit1:
Al Virof5beed72015-04-30 16:09:11 -04004553 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004555 if (should_retry) {
4556 should_retry = false;
4557 lookup_flags |= LOOKUP_REVAL;
4558 goto retry;
4559 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004560exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 return error;
4562}
4563
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004564SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4565 int, newdfd, const char __user *, newname, unsigned int, flags)
4566{
4567 return do_renameat2(olddfd, oldname, newdfd, newname, flags);
4568}
4569
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004570SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4571 int, newdfd, const char __user *, newname)
4572{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004573 return do_renameat2(olddfd, oldname, newdfd, newname, 0);
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004574}
4575
Heiko Carstensa26eab22009-01-14 14:14:17 +01004576SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004577{
Dominik Brodowskiee81feb2018-03-11 11:34:28 +01004578 return do_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004579}
4580
Al Viro5d826c82014-03-14 13:42:45 -04004581int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
Al Viro5d826c82014-03-14 13:42:45 -04004583 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 if (IS_ERR(link))
4585 goto out;
4586
4587 len = strlen(link);
4588 if (len > (unsigned) buflen)
4589 len = buflen;
4590 if (copy_to_user(buffer, link, len))
4591 len = -EFAULT;
4592out:
4593 return len;
4594}
4595
Miklos Szeredid60874c2016-10-04 14:40:45 +02004596/**
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004597 * vfs_readlink - copy symlink body into userspace buffer
4598 * @dentry: dentry on which to get symbolic link
4599 * @buffer: user memory pointer
4600 * @buflen: size of buffer
4601 *
4602 * Does not touch atime. That's up to the caller if necessary
4603 *
4604 * Does not call security hook.
4605 */
4606int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4607{
4608 struct inode *inode = d_inode(dentry);
Al Virof2df5da2018-07-19 17:35:51 -04004609 DEFINE_DELAYED_CALL(done);
4610 const char *link;
4611 int res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004612
Miklos Szeredi76fca902016-12-09 16:45:04 +01004613 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4614 if (unlikely(inode->i_op->readlink))
4615 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004616
Miklos Szeredi76fca902016-12-09 16:45:04 +01004617 if (!d_is_symlink(dentry))
4618 return -EINVAL;
4619
4620 spin_lock(&inode->i_lock);
4621 inode->i_opflags |= IOP_DEFAULT_READLINK;
4622 spin_unlock(&inode->i_lock);
4623 }
4624
Eric Biggers4c4f7c12019-04-10 13:21:14 -07004625 link = READ_ONCE(inode->i_link);
Al Virof2df5da2018-07-19 17:35:51 -04004626 if (!link) {
4627 link = inode->i_op->get_link(dentry, inode, &done);
4628 if (IS_ERR(link))
4629 return PTR_ERR(link);
4630 }
4631 res = readlink_copy(buffer, buflen, link);
4632 do_delayed_call(&done);
4633 return res;
Miklos Szeredifd4a0edf2016-12-09 16:45:04 +01004634}
4635EXPORT_SYMBOL(vfs_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636
Miklos Szeredid60874c2016-10-04 14:40:45 +02004637/**
4638 * vfs_get_link - get symlink body
4639 * @dentry: dentry on which to get symbolic link
4640 * @done: caller needs to free returned data with this
4641 *
4642 * Calls security hook and i_op->get_link() on the supplied inode.
4643 *
4644 * It does not touch atime. That's up to the caller if necessary.
4645 *
4646 * Does not work on "special" symlinks like /proc/$$/fd/N
4647 */
4648const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4649{
4650 const char *res = ERR_PTR(-EINVAL);
4651 struct inode *inode = d_inode(dentry);
4652
4653 if (d_is_symlink(dentry)) {
4654 res = ERR_PTR(security_inode_readlink(dentry));
4655 if (!res)
4656 res = inode->i_op->get_link(dentry, inode, done);
4657 }
4658 return res;
4659}
4660EXPORT_SYMBOL(vfs_get_link);
4661
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004663const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004664 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004666 char *kaddr;
4667 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004668 struct address_space *mapping = inode->i_mapping;
4669
Al Virod3883d42015-11-17 10:41:04 -05004670 if (!dentry) {
4671 page = find_get_page(mapping, 0);
4672 if (!page)
4673 return ERR_PTR(-ECHILD);
4674 if (!PageUptodate(page)) {
4675 put_page(page);
4676 return ERR_PTR(-ECHILD);
4677 }
4678 } else {
4679 page = read_mapping_page(mapping, 0, NULL);
4680 if (IS_ERR(page))
4681 return (char*)page;
4682 }
Al Virofceef392015-12-29 15:58:39 -05004683 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004684 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4685 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004686 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004687 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688}
4689
Al Viro6b255392015-11-17 10:20:54 -05004690EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691
Al Virofceef392015-12-29 15:58:39 -05004692void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693{
Al Virofceef392015-12-29 15:58:39 -05004694 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
Al Viro4d359502014-03-14 12:20:17 -04004696EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697
Al Viroaa80dea2015-11-16 18:26:34 -05004698int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4699{
Al Virofceef392015-12-29 15:58:39 -05004700 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004701 int res = readlink_copy(buffer, buflen,
4702 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004703 &done));
4704 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004705 return res;
4706}
4707EXPORT_SYMBOL(page_readlink);
4708
Nick Piggin54566b22009-01-04 12:00:53 -08004709/*
4710 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4711 */
4712int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713{
4714 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004715 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004716 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004717 int err;
Tetsuo Handac718a972017-05-08 15:58:59 -07004718 unsigned int flags = 0;
Nick Piggin54566b22009-01-04 12:00:53 -08004719 if (nofs)
4720 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
NeilBrown7e53cac2006-03-25 03:07:57 -08004722retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004723 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004724 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004726 goto fail;
4727
Al Viro21fc61c2015-11-17 01:07:57 -05004728 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004729
4730 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4731 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 if (err < 0)
4733 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004734 if (err < len-1)
4735 goto retry;
4736
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 mark_inode_dirty(inode);
4738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739fail:
4740 return err;
4741}
Al Viro4d359502014-03-14 12:20:17 -04004742EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004743
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004744int page_symlink(struct inode *inode, const char *symname, int len)
4745{
4746 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004747 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004748}
Al Viro4d359502014-03-14 12:20:17 -04004749EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004750
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004751const struct inode_operations page_symlink_inode_operations = {
Al Viro6b255392015-11-17 10:20:54 -05004752 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754EXPORT_SYMBOL(page_symlink_inode_operations);