blob: 2ed2701a74b563995289e54d912133f081ebd980 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/namei.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Some corrections by tytso.
9 */
10
11/* [Feb 1997 T. Schoebel-Theuer] Complete rewrite of the pathname
12 * lookup logic.
13 */
14/* [Feb-Apr 2000, AV] Rewrite to the new namespace architecture.
15 */
16
17#include <linux/init.h>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
David S. Miller44696902012-05-23 20:12:50 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/fs.h>
22#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/pagemap.h>
Robert Love0eeca282005-07-12 17:06:03 -040024#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/personality.h>
26#include <linux/security.h>
Mimi Zohar6146f0d2009-02-04 09:06:57 -050027#include <linux/ima.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/syscalls.h>
29#include <linux/mount.h>
30#include <linux/audit.h>
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080031#include <linux/capability.h>
Trond Myklebust834f2a42005-10-18 14:20:16 -070032#include <linux/file.h>
Ulrich Drepper5590ff02006-01-18 17:43:53 -080033#include <linux/fcntl.h>
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -070034#include <linux/device_cgroup.h>
Al Viro5ad4e532009-03-29 19:50:06 -040035#include <linux/fs_struct.h>
Linus Torvaldse77819e2011-07-22 19:30:19 -070036#include <linux/posix_acl.h>
Linus Torvalds99d263d2014-09-13 11:30:10 -070037#include <linux/hash.h>
George Spelvin2a18da7a2016-05-23 07:43:58 -040038#include <linux/bitops.h>
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -050039#include <linux/init_task.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080040#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Eric Parise81e3f42009-12-04 15:47:36 -050042#include "internal.h"
Al Viroc7105362011-11-24 18:22:03 -050043#include "mount.h"
Eric Parise81e3f42009-12-04 15:47:36 -050044
Linus Torvalds1da177e2005-04-16 15:20:36 -070045/* [Feb-1997 T. Schoebel-Theuer]
46 * Fundamental changes in the pathname lookup mechanisms (namei)
47 * were necessary because of omirr. The reason is that omirr needs
48 * to know the _real_ pathname, not the user-supplied one, in case
49 * of symlinks (and also when transname replacements occur).
50 *
51 * The new code replaces the old recursive symlink resolution with
52 * an iterative one (in case of non-nested symlink chains). It does
53 * this with calls to <fs>_follow_link().
54 * As a side effect, dir_namei(), _namei() and follow_link() are now
55 * replaced with a single function lookup_dentry() that can handle all
56 * the special cases of the former code.
57 *
58 * With the new dcache, the pathname is stored at each inode, at least as
59 * long as the refcount of the inode is positive. As a side effect, the
60 * size of the dcache depends on the inode cache and thus is dynamic.
61 *
62 * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink
63 * resolution to correspond with current state of the code.
64 *
65 * Note that the symlink resolution is not *completely* iterative.
66 * There is still a significant amount of tail- and mid- recursion in
67 * the algorithm. Also, note that <fs>_readlink() is not used in
68 * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink()
69 * may return different results than <fs>_follow_link(). Many virtual
70 * filesystems (including /proc) exhibit this behavior.
71 */
72
73/* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation:
74 * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL
75 * and the name already exists in form of a symlink, try to create the new
76 * name indicated by the symlink. The old code always complained that the
77 * name already exists, due to not following the symlink even if its target
78 * is nonexistent. The new semantics affects also mknod() and link() when
Lucas De Marchi25985ed2011-03-30 22:57:33 -030079 * the name is a symlink pointing to a non-existent name.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
81 * I don't know which semantics is the right one, since I have no access
82 * to standards. But I found by trial that HP-UX 9.0 has the full "new"
83 * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the
84 * "old" one. Personally, I think the new semantics is much more logical.
85 * Note that "ln old new" where "new" is a symlink pointing to a non-existing
86 * file does succeed in both HP-UX and SunOs, but not in Solaris
87 * and in the old Linux semantics.
88 */
89
90/* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink
91 * semantics. See the comments in "open_namei" and "do_link" below.
92 *
93 * [10-Sep-98 Alan Modra] Another symlink change.
94 */
95
96/* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks:
97 * inside the path - always follow.
98 * in the last component in creation/removal/renaming - never follow.
99 * if LOOKUP_FOLLOW passed - follow.
100 * if the pathname has trailing slashes - follow.
101 * otherwise - don't follow.
102 * (applied in that order).
103 *
104 * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT
105 * restored for 2.4. This is the last surviving part of old 4.2BSD bug.
106 * During the 2.4 we need to fix the userland stuff depending on it -
107 * hopefully we will be able to get rid of that wart in 2.5. So far only
108 * XEmacs seems to be relying on it...
109 */
110/*
111 * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland)
Arjan van de Vena11f3a02006-03-23 03:00:33 -0800112 * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 * any extra contention...
114 */
115
116/* In order to reduce some races, while at the same time doing additional
117 * checking and hopefully speeding things up, we copy filenames to the
118 * kernel data space before using them..
119 *
120 * POSIX.1 2.4: an empty pathname is invalid (ENOENT).
121 * PATH_MAX includes the nul terminator --RR.
122 */
Jeff Layton91a27b22012-10-10 15:25:28 -0400123
Al Virofd2f7cb2015-02-22 20:07:13 -0500124#define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname))
Jeff Layton7950e382012-10-10 16:43:13 -0400125
David Drysdale51f39a12014-12-12 16:57:29 -0800126struct filename *
Jeff Layton91a27b22012-10-10 15:25:28 -0400127getname_flags(const char __user *filename, int flags, int *empty)
128{
Al Viro94b5d262015-02-22 19:38:03 -0500129 struct filename *result;
Jeff Layton7950e382012-10-10 16:43:13 -0400130 char *kname;
Al Viro94b5d262015-02-22 19:38:03 -0500131 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Jeff Layton7ac86262012-10-10 15:25:28 -0400133 result = audit_reusename(filename);
134 if (result)
135 return result;
136
Jeff Layton7950e382012-10-10 16:43:13 -0400137 result = __getname();
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700138 if (unlikely(!result))
Eric Paris4043cde2012-01-03 14:23:08 -0500139 return ERR_PTR(-ENOMEM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Jeff Layton7950e382012-10-10 16:43:13 -0400141 /*
142 * First, try to embed the struct filename inside the names_cache
143 * allocation
144 */
Al Virofd2f7cb2015-02-22 20:07:13 -0500145 kname = (char *)result->iname;
Jeff Layton91a27b22012-10-10 15:25:28 -0400146 result->name = kname;
Jeff Layton7950e382012-10-10 16:43:13 -0400147
Al Viro94b5d262015-02-22 19:38:03 -0500148 len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX);
Jeff Layton91a27b22012-10-10 15:25:28 -0400149 if (unlikely(len < 0)) {
Al Viro94b5d262015-02-22 19:38:03 -0500150 __putname(result);
151 return ERR_PTR(len);
Jeff Layton91a27b22012-10-10 15:25:28 -0400152 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700153
Jeff Layton7950e382012-10-10 16:43:13 -0400154 /*
155 * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a
156 * separate struct filename so we can dedicate the entire
157 * names_cache allocation for the pathname, and re-do the copy from
158 * userland.
159 */
Al Viro94b5d262015-02-22 19:38:03 -0500160 if (unlikely(len == EMBEDDED_NAME_MAX)) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500161 const size_t size = offsetof(struct filename, iname[1]);
Jeff Layton7950e382012-10-10 16:43:13 -0400162 kname = (char *)result;
163
Al Virofd2f7cb2015-02-22 20:07:13 -0500164 /*
165 * size is chosen that way we to guarantee that
166 * result->iname[0] is within the same object and that
167 * kname can't be equal to result->iname, no matter what.
168 */
169 result = kzalloc(size, GFP_KERNEL);
Al Viro94b5d262015-02-22 19:38:03 -0500170 if (unlikely(!result)) {
171 __putname(kname);
172 return ERR_PTR(-ENOMEM);
Jeff Layton7950e382012-10-10 16:43:13 -0400173 }
174 result->name = kname;
Al Viro94b5d262015-02-22 19:38:03 -0500175 len = strncpy_from_user(kname, filename, PATH_MAX);
176 if (unlikely(len < 0)) {
177 __putname(kname);
178 kfree(result);
179 return ERR_PTR(len);
180 }
181 if (unlikely(len == PATH_MAX)) {
182 __putname(kname);
183 kfree(result);
184 return ERR_PTR(-ENAMETOOLONG);
185 }
Jeff Layton7950e382012-10-10 16:43:13 -0400186 }
187
Al Viro94b5d262015-02-22 19:38:03 -0500188 result->refcnt = 1;
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700189 /* The empty path is special. */
190 if (unlikely(!len)) {
191 if (empty)
Eric Paris4043cde2012-01-03 14:23:08 -0500192 *empty = 1;
Al Viro94b5d262015-02-22 19:38:03 -0500193 if (!(flags & LOOKUP_EMPTY)) {
194 putname(result);
195 return ERR_PTR(-ENOENT);
196 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 }
Linus Torvalds3f9f0aa2012-04-28 14:38:32 -0700198
Jeff Layton7950e382012-10-10 16:43:13 -0400199 result->uptr = filename;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800200 result->aname = NULL;
Jeff Layton7950e382012-10-10 16:43:13 -0400201 audit_getname(result);
202 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Jeff Layton91a27b22012-10-10 15:25:28 -0400205struct filename *
206getname(const char __user * filename)
Al Virof52e0c12011-03-14 18:56:51 -0400207{
Linus Torvaldsf7493e52012-03-22 16:10:40 -0700208 return getname_flags(filename, 0, NULL);
Al Virof52e0c12011-03-14 18:56:51 -0400209}
210
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800211struct filename *
212getname_kernel(const char * filename)
213{
214 struct filename *result;
Paul Moore08518542015-01-21 23:59:56 -0500215 int len = strlen(filename) + 1;
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800216
217 result = __getname();
218 if (unlikely(!result))
219 return ERR_PTR(-ENOMEM);
220
Paul Moore08518542015-01-21 23:59:56 -0500221 if (len <= EMBEDDED_NAME_MAX) {
Al Virofd2f7cb2015-02-22 20:07:13 -0500222 result->name = (char *)result->iname;
Paul Moore08518542015-01-21 23:59:56 -0500223 } else if (len <= PATH_MAX) {
224 struct filename *tmp;
225
226 tmp = kmalloc(sizeof(*tmp), GFP_KERNEL);
227 if (unlikely(!tmp)) {
228 __putname(result);
229 return ERR_PTR(-ENOMEM);
230 }
231 tmp->name = (char *)result;
Paul Moore08518542015-01-21 23:59:56 -0500232 result = tmp;
233 } else {
234 __putname(result);
235 return ERR_PTR(-ENAMETOOLONG);
236 }
237 memcpy((char *)result->name, filename, len);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800238 result->uptr = NULL;
239 result->aname = NULL;
Paul Moore55422d02015-01-22 00:00:23 -0500240 result->refcnt = 1;
Paul Moorefd3522f2015-01-22 00:00:10 -0500241 audit_getname(result);
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800242
Linus Torvaldsc4ad8f92014-02-05 12:54:53 -0800243 return result;
244}
245
Jeff Layton91a27b22012-10-10 15:25:28 -0400246void putname(struct filename *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
Paul Moore55422d02015-01-22 00:00:23 -0500248 BUG_ON(name->refcnt <= 0);
249
250 if (--name->refcnt > 0)
251 return;
252
Al Virofd2f7cb2015-02-22 20:07:13 -0500253 if (name->name != name->iname) {
Paul Moore55422d02015-01-22 00:00:23 -0500254 __putname(name->name);
255 kfree(name);
256 } else
257 __putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Linus Torvaldse77819e2011-07-22 19:30:19 -0700260static int check_acl(struct inode *inode, int mask)
261{
Linus Torvalds84635d62011-07-25 22:47:03 -0700262#ifdef CONFIG_FS_POSIX_ACL
Linus Torvaldse77819e2011-07-22 19:30:19 -0700263 struct posix_acl *acl;
264
Linus Torvaldse77819e2011-07-22 19:30:19 -0700265 if (mask & MAY_NOT_BLOCK) {
Al Viro35678662011-08-02 21:32:13 -0400266 acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS);
267 if (!acl)
Linus Torvaldse77819e2011-07-22 19:30:19 -0700268 return -EAGAIN;
Al Viro35678662011-08-02 21:32:13 -0400269 /* no ->get_acl() calls in RCU mode... */
Andreas Gruenbacherb8a7a3a2016-03-24 14:38:37 +0100270 if (is_uncached_acl(acl))
Al Viro35678662011-08-02 21:32:13 -0400271 return -ECHILD;
Ari Savolainen206b1d02011-08-06 19:43:07 +0300272 return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700273 }
274
Christoph Hellwig2982baa2013-12-20 05:16:38 -0800275 acl = get_acl(inode, ACL_TYPE_ACCESS);
276 if (IS_ERR(acl))
277 return PTR_ERR(acl);
Linus Torvaldse77819e2011-07-22 19:30:19 -0700278 if (acl) {
279 int error = posix_acl_permission(inode, acl, mask);
280 posix_acl_release(acl);
281 return error;
282 }
Linus Torvalds84635d62011-07-25 22:47:03 -0700283#endif
Linus Torvaldse77819e2011-07-22 19:30:19 -0700284
285 return -EAGAIN;
286}
287
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700288/*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530289 * This does the basic permission checking
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700290 */
Al Viro7e401452011-06-20 19:12:17 -0400291static int acl_permission_check(struct inode *inode, int mask)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700292{
Linus Torvalds26cf46b2011-05-13 11:51:01 -0700293 unsigned int mode = inode->i_mode;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700294
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -0800295 if (likely(uid_eq(current_fsuid(), inode->i_uid)))
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700296 mode >>= 6;
297 else {
Linus Torvaldse77819e2011-07-22 19:30:19 -0700298 if (IS_POSIXACL(inode) && (mode & S_IRWXG)) {
Al Viro7e401452011-06-20 19:12:17 -0400299 int error = check_acl(inode, mask);
Nick Pigginb74c79e2011-01-07 17:49:58 +1100300 if (error != -EAGAIN)
301 return error;
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700302 }
303
304 if (in_group_p(inode->i_gid))
305 mode >>= 3;
306 }
307
308 /*
309 * If the DACs are ok we don't need any capability check.
310 */
Al Viro9c2c7032011-06-20 19:06:22 -0400311 if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0)
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700312 return 0;
313 return -EACCES;
314}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316/**
Nick Pigginb74c79e2011-01-07 17:49:58 +1100317 * generic_permission - check for access rights on a Posix-like filesystem
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * @inode: inode to check access rights for
Andreas Gruenbacher8fd90c82011-10-23 23:13:30 +0530319 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 *
321 * Used to check for read/write/execute permissions on a file.
322 * We use "fsuid" for this, letting us set arbitrary permissions
323 * for filesystem access without changing the "normal" uids which
Nick Pigginb74c79e2011-01-07 17:49:58 +1100324 * are used for other things.
325 *
326 * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk
327 * request cannot be satisfied (eg. requires blocking or too much complexity).
328 * It would then be called again in ref-walk mode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 */
Al Viro2830ba72011-06-20 19:16:29 -0400330int generic_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700332 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /*
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530335 * Do the basic permission checks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 */
Al Viro7e401452011-06-20 19:12:17 -0400337 ret = acl_permission_check(inode, mask);
Linus Torvalds5909ccaa2009-08-28 11:51:25 -0700338 if (ret != -EACCES)
339 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Al Virod594e7e2011-06-20 19:55:42 -0400341 if (S_ISDIR(inode->i_mode)) {
342 /* DACs are overridable for directories */
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700343 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Al Virod594e7e2011-06-20 19:55:42 -0400344 return 0;
345 if (!(mask & MAY_WRITE))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700346 if (capable_wrt_inode_uidgid(inode,
347 CAP_DAC_READ_SEARCH))
Al Virod594e7e2011-06-20 19:55:42 -0400348 return 0;
349 return -EACCES;
350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 /*
352 * Read/write DACs are always overridable.
Al Virod594e7e2011-06-20 19:55:42 -0400353 * Executable DACs are overridable when there is
354 * at least one exec bit set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Al Virod594e7e2011-06-20 19:55:42 -0400356 if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO))
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700357 if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return 0;
359
360 /*
361 * Searching includes executable on directories, else just read.
362 */
Serge E. Hallyn7ea66002009-12-29 14:50:19 -0600363 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
Al Virod594e7e2011-06-20 19:55:42 -0400364 if (mask == MAY_READ)
Andy Lutomirski23adbe12014-06-10 12:45:42 -0700365 if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return 0;
367
368 return -EACCES;
369}
Al Viro4d359502014-03-14 12:20:17 -0400370EXPORT_SYMBOL(generic_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700372/*
373 * We _really_ want to just do "generic_permission()" without
374 * even looking at the inode->i_op values. So we keep a cache
375 * flag in inode->i_opflags, that says "this has not special
376 * permission function, use the fast case".
377 */
378static inline int do_inode_permission(struct inode *inode, int mask)
379{
380 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
381 if (likely(inode->i_op->permission))
382 return inode->i_op->permission(inode, mask);
383
384 /* This gets set once for the inode lifetime */
385 spin_lock(&inode->i_lock);
386 inode->i_opflags |= IOP_FASTPERM;
387 spin_unlock(&inode->i_lock);
388 }
389 return generic_permission(inode, mask);
390}
391
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200392/**
David Howells0bdaea92012-06-25 12:55:46 +0100393 * __inode_permission - Check for access rights to a given inode
394 * @inode: Inode to check permission on
395 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200396 *
David Howells0bdaea92012-06-25 12:55:46 +0100397 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530398 *
399 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100400 *
401 * This does not check for a read-only file system. You probably want
402 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200403 */
David Howells0bdaea92012-06-25 12:55:46 +0100404int __inode_permission(struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
Al Viroe6305c42008-07-15 21:03:57 -0400406 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700408 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 /*
410 * Nobody gets write access to an immutable file.
411 */
412 if (IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +0800413 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -0500414
415 /*
416 * Updating mtime will likely cause i_uid and i_gid to be
417 * written back improperly if their true value is unknown
418 * to the vfs.
419 */
420 if (HAS_UNMAPPED_ID(inode))
421 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 }
423
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700424 retval = do_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (retval)
426 return retval;
427
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700428 retval = devcgroup_inode_permission(inode, mask);
429 if (retval)
430 return retval;
431
Eric Parisd09ca732010-07-23 11:43:57 -0400432 return security_inode_permission(inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
Miklos Szeredibd5d0852014-10-24 00:14:35 +0200434EXPORT_SYMBOL(__inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Al Virof4d6ff82011-06-19 13:14:21 -0400436/**
David Howells0bdaea92012-06-25 12:55:46 +0100437 * sb_permission - Check superblock-level permissions
438 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700439 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100440 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
441 *
442 * Separate out file-system wide checks from inode-specific permission checks.
443 */
444static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
445{
446 if (unlikely(mask & MAY_WRITE)) {
447 umode_t mode = inode->i_mode;
448
449 /* Nobody gets write access to a read-only fs. */
450 if ((sb->s_flags & MS_RDONLY) &&
451 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
452 return -EROFS;
453 }
454 return 0;
455}
456
457/**
458 * inode_permission - Check for access rights to a given inode
459 * @inode: Inode to check permission on
460 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
461 *
462 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
463 * this, letting us set arbitrary permissions for filesystem access without
464 * changing the "normal" UIDs which are used for other things.
465 *
466 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
467 */
468int inode_permission(struct inode *inode, int mask)
469{
470 int retval;
471
472 retval = sb_permission(inode->i_sb, inode, mask);
473 if (retval)
474 return retval;
475 return __inode_permission(inode, mask);
476}
Al Viro4d359502014-03-14 12:20:17 -0400477EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100478
479/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800480 * path_get - get a reference to a path
481 * @path: path to get the reference to
482 *
483 * Given a path increment the reference count to the dentry and the vfsmount.
484 */
Al Virodcf787f2013-03-01 23:51:07 -0500485void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800486{
487 mntget(path->mnt);
488 dget(path->dentry);
489}
490EXPORT_SYMBOL(path_get);
491
492/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800493 * path_put - put a reference to a path
494 * @path: path to put the reference to
495 *
496 * Given a path decrement the reference count to the dentry and the vfsmount.
497 */
Al Virodcf787f2013-03-01 23:51:07 -0500498void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Jan Blunck1d957f92008-02-14 19:34:35 -0800500 dput(path->dentry);
501 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
Jan Blunck1d957f92008-02-14 19:34:35 -0800503EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Al Viro894bc8c2015-05-02 07:16:16 -0400505#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400506struct nameidata {
507 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400508 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400509 struct path root;
510 struct inode *inode; /* path.dentry.d_inode */
511 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400512 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400513 int last_type;
514 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100515 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400516 struct saved {
517 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500518 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400519 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400520 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400521 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400522 struct filename *name;
523 struct nameidata *saved;
Al Virofceef392015-12-29 15:58:39 -0500524 struct inode *link_inode;
Al Viro9883d182015-05-13 07:28:08 -0400525 unsigned root_seq;
526 int dfd;
Al Viro1f55a6e2014-11-01 19:30:41 -0400527};
528
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
551static int __nd_alloc_stack(struct nameidata *nd)
552{
Al Virobc40aee2015-05-09 13:04:24 -0400553 struct saved *p;
554
555 if (nd->flags & LOOKUP_RCU) {
556 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
557 GFP_ATOMIC);
558 if (unlikely(!p))
559 return -ECHILD;
560 } else {
561 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400562 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400563 if (unlikely(!p))
564 return -ENOMEM;
565 }
Al Viro894bc8c2015-05-02 07:16:16 -0400566 memcpy(p, nd->internal, sizeof(nd->internal));
567 nd->stack = p;
568 return 0;
569}
570
Eric W. Biederman397d4252015-08-15 20:27:13 -0500571/**
572 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
573 * @path: nameidate to verify
574 *
575 * Rename can sometimes move a file or directory outside of a bind
576 * mount, path_connected allows those cases to be detected.
577 */
578static bool path_connected(const struct path *path)
579{
580 struct vfsmount *mnt = path->mnt;
581
582 /* Only bind mounts can have disconnected paths */
583 if (mnt->mnt_root == mnt->mnt_sb->s_root)
584 return true;
585
586 return is_subdir(path->dentry, mnt->mnt_root);
587}
588
Al Viro894bc8c2015-05-02 07:16:16 -0400589static inline int nd_alloc_stack(struct nameidata *nd)
590{
Al Viroda4e0be2015-05-03 20:52:15 -0400591 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400592 return 0;
593 if (likely(nd->stack != nd->internal))
594 return 0;
595 return __nd_alloc_stack(nd);
596}
597
Al Viro79733872015-05-09 12:55:43 -0400598static void drop_links(struct nameidata *nd)
599{
600 int i = nd->depth;
601 while (i--) {
602 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500603 do_delayed_call(&last->done);
604 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400605 }
606}
607
608static void terminate_walk(struct nameidata *nd)
609{
610 drop_links(nd);
611 if (!(nd->flags & LOOKUP_RCU)) {
612 int i;
613 path_put(&nd->path);
614 for (i = 0; i < nd->depth; i++)
615 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400616 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
617 path_put(&nd->root);
618 nd->root.mnt = NULL;
619 }
Al Viro79733872015-05-09 12:55:43 -0400620 } else {
621 nd->flags &= ~LOOKUP_RCU;
622 if (!(nd->flags & LOOKUP_ROOT))
623 nd->root.mnt = NULL;
624 rcu_read_unlock();
625 }
626 nd->depth = 0;
627}
628
629/* path_put is needed afterwards regardless of success or failure */
630static bool legitimize_path(struct nameidata *nd,
631 struct path *path, unsigned seq)
632{
633 int res = __legitimize_mnt(path->mnt, nd->m_seq);
634 if (unlikely(res)) {
635 if (res > 0)
636 path->mnt = NULL;
637 path->dentry = NULL;
638 return false;
639 }
640 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
641 path->dentry = NULL;
642 return false;
643 }
644 return !read_seqcount_retry(&path->dentry->d_seq, seq);
645}
646
647static bool legitimize_links(struct nameidata *nd)
648{
649 int i;
650 for (i = 0; i < nd->depth; i++) {
651 struct saved *last = nd->stack + i;
652 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
653 drop_links(nd);
654 nd->depth = i + 1;
655 return false;
656 }
657 }
658 return true;
659}
660
Al Viro19660af2011-03-25 10:32:48 -0400661/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100662 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400663 * Documentation/filesystems/path-lookup.txt). In situations when we can't
664 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500665 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400666 * mode. Refcounts are grabbed at the last known good point before rcu-walk
667 * got stuck, so ref-walk may continue from there. If this is not successful
668 * (eg. a seqcount has changed), then failure is returned and it's up to caller
669 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100670 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100671
672/**
Al Viro19660af2011-03-25 10:32:48 -0400673 * unlazy_walk - try to switch to ref-walk mode.
674 * @nd: nameidata pathwalk data
675 * @dentry: child of nd->path.dentry or NULL
Al Viro6e9918b2015-05-05 09:26:05 -0400676 * @seq: seq number to check dentry against
Randy Dunlap39191622011-01-08 19:36:21 -0800677 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100678 *
Al Viro19660af2011-03-25 10:32:48 -0400679 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
680 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
681 * @nd or NULL. Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400682 * Nothing should touch nameidata between unlazy_walk() failure and
683 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100684 */
Al Viro6e9918b2015-05-05 09:26:05 -0400685static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
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)))
693 goto out2;
694 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
695 goto out2;
696 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
697 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400698
Linus Torvalds15570082013-09-02 11:38:06 -0700699 /*
700 * For a negative lookup, the lookup sequence point is the parents
701 * sequence point, and it only needs to revalidate the parent dentry.
702 *
703 * For a positive lookup, we need to move both the parent and the
704 * dentry from the RCU domain to be properly refcounted. And the
705 * sequence number in the dentry validates *both* dentry counters,
706 * since we checked the sequence number of the parent after we got
707 * the child sequence number. So we know the parent must still
708 * be valid if the child sequence number is still valid.
709 */
Al Viro19660af2011-03-25 10:32:48 -0400710 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700711 if (read_seqcount_retry(&parent->d_seq, nd->seq))
712 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400713 BUG_ON(nd->inode != parent->d_inode);
714 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700715 if (!lockref_get_not_dead(&dentry->d_lockref))
716 goto out;
Al Viro6e9918b2015-05-05 09:26:05 -0400717 if (read_seqcount_retry(&dentry->d_seq, seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700718 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400719 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700720
721 /*
722 * Sequence counts matched. Now make sure that the root is
723 * still valid and get it if required.
724 */
725 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400726 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
727 rcu_read_unlock();
728 dput(dentry);
729 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400730 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100731 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100732
Al Viro8b61e742013-11-08 12:45:01 -0500733 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100734 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400735
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700736drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500737 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700738 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700739 goto drop_root_mnt;
Al Viro79733872015-05-09 12:55:43 -0400740out2:
741 nd->path.mnt = NULL;
742out1:
743 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700744out:
Al Viro8b61e742013-11-08 12:45:01 -0500745 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700746drop_root_mnt:
747 if (!(nd->flags & LOOKUP_ROOT))
748 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100749 return -ECHILD;
750}
751
Al Viro4ce16ef32012-06-10 16:10:59 -0400752static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100753{
Al Viroa89f8332017-01-09 22:25:28 -0500754 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
755 return dentry->d_op->d_revalidate(dentry, flags);
756 else
757 return 1;
Nick Piggin34286d62011-01-07 17:49:57 +1100758}
759
Al Viro9f1fafe2011-03-25 11:00:12 -0400760/**
761 * complete_walk - successful completion of path walk
762 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500763 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400764 * If we had been in RCU mode, drop out of it and legitimize nd->path.
765 * Revalidate the final result, unless we'd already done that during
766 * the path walk or the filesystem doesn't ask for it. Return 0 on
767 * success, -error on failure. In case of failure caller does not
768 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500769 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400770static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500771{
Al Viro16c2cd72011-02-22 15:50:10 -0500772 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500773 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500774
Al Viro9f1fafe2011-03-25 11:00:12 -0400775 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400776 if (!(nd->flags & LOOKUP_ROOT))
777 nd->root.mnt = NULL;
Al Viro6e9918b2015-05-05 09:26:05 -0400778 if (unlikely(unlazy_walk(nd, NULL, 0)))
Al Viro48a066e2013-09-29 22:06:07 -0400779 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400780 }
781
Al Viro16c2cd72011-02-22 15:50:10 -0500782 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500783 return 0;
784
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500785 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500786 return 0;
787
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500788 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500789 if (status > 0)
790 return 0;
791
Al Viro16c2cd72011-02-22 15:50:10 -0500792 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500793 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500794
Jeff Layton39159de2009-12-07 12:01:50 -0500795 return status;
796}
797
Al Viro18d8c862015-05-12 16:32:34 -0400798static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400799{
Al Viro7bd88372014-09-13 21:55:46 -0400800 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100801
Al Viro9e6697e2015-12-05 20:07:21 -0500802 if (nd->flags & LOOKUP_RCU) {
803 unsigned seq;
804
805 do {
806 seq = read_seqcount_begin(&fs->seq);
807 nd->root = fs->root;
808 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
809 } while (read_seqcount_retry(&fs->seq, seq));
810 } else {
811 get_fs_root(fs, &nd->root);
812 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100813}
814
Jan Blunck1d957f92008-02-14 19:34:35 -0800815static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700816{
817 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800818 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700819 mntput(path->mnt);
820}
821
Nick Piggin7b9337a2011-01-14 08:42:43 +0000822static inline void path_to_nameidata(const struct path *path,
823 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700824{
Nick Piggin31e6b012011-01-07 17:49:52 +1100825 if (!(nd->flags & LOOKUP_RCU)) {
826 dput(nd->path.dentry);
827 if (nd->path.mnt != path->mnt)
828 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800829 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100830 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800831 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700832}
833
Al Viro248fb5b2015-12-05 20:51:58 -0500834static int nd_jump_root(struct nameidata *nd)
835{
836 if (nd->flags & LOOKUP_RCU) {
837 struct dentry *d;
838 nd->path = nd->root;
839 d = nd->path.dentry;
840 nd->inode = d->d_inode;
841 nd->seq = nd->root_seq;
842 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
843 return -ECHILD;
844 } else {
845 path_put(&nd->path);
846 nd->path = nd->root;
847 path_get(&nd->path);
848 nd->inode = nd->path.dentry->d_inode;
849 }
850 nd->flags |= LOOKUP_JUMPED;
851 return 0;
852}
853
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400854/*
Al Viro6b255392015-11-17 10:20:54 -0500855 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400856 * caller must have taken a reference to path beforehand.
857 */
Al Viro6e77137b2015-05-02 13:37:52 -0400858void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400859{
Al Viro6e77137b2015-05-02 13:37:52 -0400860 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400861 path_put(&nd->path);
862
863 nd->path = *path;
864 nd->inode = nd->path.dentry->d_inode;
865 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400866}
867
Al Virob9ff4422015-05-02 20:19:23 -0400868static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400869{
Al Viro21c30032015-05-03 21:06:24 -0400870 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -0500871 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -0400872 if (!(nd->flags & LOOKUP_RCU))
873 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400874}
875
Linus Torvalds561ec642012-10-26 10:05:07 -0700876int sysctl_protected_symlinks __read_mostly = 0;
877int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700878
879/**
880 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700881 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700882 *
883 * In the case of the sysctl_protected_symlinks sysctl being enabled,
884 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
885 * in a sticky world-writable directory. This is to protect privileged
886 * processes from failing races against path names that may change out
887 * from under them by way of other users creating malicious symlinks.
888 * It will permit symlinks to be followed only when outside a sticky
889 * world-writable directory, or when the uid of the symlink and follower
890 * match, or when the directory owner matches the symlink's owner.
891 *
892 * Returns 0 if following the symlink is allowed, -ve on error.
893 */
Al Virofec2fa22015-05-06 15:58:18 -0400894static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700895{
896 const struct inode *inode;
897 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500898 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700899
900 if (!sysctl_protected_symlinks)
901 return 0;
902
903 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500904 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700905 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700906 return 0;
907
908 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400909 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700910 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
911 return 0;
912
913 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500914 puid = parent->i_uid;
915 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700916 return 0;
917
Al Viro319565022015-05-07 20:37:40 -0400918 if (nd->flags & LOOKUP_RCU)
919 return -ECHILD;
920
Al Viro1cf26652015-05-06 16:01:56 -0400921 audit_log_link_denied("follow_link", &nd->stack[0].link);
Kees Cook800179c2012-07-25 17:29:07 -0700922 return -EACCES;
923}
924
925/**
926 * safe_hardlink_source - Check for safe hardlink conditions
927 * @inode: the source inode to hardlink from
928 *
929 * Return false if at least one of the following conditions:
930 * - inode is not a regular file
931 * - inode is setuid
932 * - inode is setgid and group-exec
933 * - access failure for read and write
934 *
935 * Otherwise returns true.
936 */
937static bool safe_hardlink_source(struct inode *inode)
938{
939 umode_t mode = inode->i_mode;
940
941 /* Special files should not get pinned to the filesystem. */
942 if (!S_ISREG(mode))
943 return false;
944
945 /* Setuid files should not get pinned to the filesystem. */
946 if (mode & S_ISUID)
947 return false;
948
949 /* Executable setgid files should not get pinned to the filesystem. */
950 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
951 return false;
952
953 /* Hardlinking to unreadable or unwritable sources is dangerous. */
954 if (inode_permission(inode, MAY_READ | MAY_WRITE))
955 return false;
956
957 return true;
958}
959
960/**
961 * may_linkat - Check permissions for creating a hardlink
962 * @link: the source to hardlink from
963 *
964 * Block hardlink when all of:
965 * - sysctl_protected_hardlinks enabled
966 * - fsuid does not match inode
967 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200968 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700969 *
970 * Returns 0 if successful, -ve on error.
971 */
972static int may_linkat(struct path *link)
973{
Kees Cook800179c2012-07-25 17:29:07 -0700974 struct inode *inode;
975
976 if (!sysctl_protected_hardlinks)
977 return 0;
978
Kees Cook800179c2012-07-25 17:29:07 -0700979 inode = link->dentry->d_inode;
980
981 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
982 * otherwise, it must be a safe source.
983 */
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200984 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
Kees Cook800179c2012-07-25 17:29:07 -0700985 return 0;
986
Kees Cooka51d9ea2012-07-25 17:29:08 -0700987 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -0700988 return -EPERM;
989}
990
Al Viro3b2e7f72015-04-19 00:53:50 -0400991static __always_inline
992const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -0800993{
Al Viroab104922015-05-10 11:50:01 -0400994 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -0400995 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -0500996 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -0400997 int error;
Al Viro0a959df2015-04-18 18:23:41 -0400998 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -0800999
NeilBrown8fa9dd22015-03-23 13:37:40 +11001000 if (!(nd->flags & LOOKUP_RCU)) {
1001 touch_atime(&last->link);
1002 cond_resched();
Miklos Szeredi598e3c82016-09-16 12:44:20 +02001003 } else if (atime_needs_update_rcu(&last->link, inode)) {
NeilBrown8fa9dd22015-03-23 13:37:40 +11001004 if (unlikely(unlazy_walk(nd, NULL, 0)))
1005 return ERR_PTR(-ECHILD);
1006 touch_atime(&last->link);
1007 }
1008
NeilBrownbda0be72015-03-23 13:37:39 +11001009 error = security_inode_follow_link(dentry, inode,
1010 nd->flags & LOOKUP_RCU);
1011 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001012 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001013
Al Viro86acdca12009-12-22 23:45:11 -05001014 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001015 res = inode->i_link;
1016 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001017 const char * (*get)(struct dentry *, struct inode *,
1018 struct delayed_call *);
1019 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001020 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001021 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001022 if (res == ERR_PTR(-ECHILD)) {
1023 if (unlikely(unlazy_walk(nd, NULL, 0)))
1024 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001025 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001026 }
1027 } else {
Al Virofceef392015-12-29 15:58:39 -05001028 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001029 }
Al Virofceef392015-12-29 15:58:39 -05001030 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001031 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001032 }
Al Virofab51e82015-05-10 11:01:00 -04001033 if (*res == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05001034 if (!nd->root.mnt)
1035 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05001036 if (unlikely(nd_jump_root(nd)))
1037 return ERR_PTR(-ECHILD);
Al Virofab51e82015-05-10 11:01:00 -04001038 while (unlikely(*++res == '/'))
1039 ;
1040 }
1041 if (!*res)
1042 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001043 return res;
1044}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001045
David Howellsf015f1262012-06-25 12:55:28 +01001046/*
1047 * follow_up - Find the mountpoint of path's vfsmount
1048 *
1049 * Given a path, find the mountpoint of its source file system.
1050 * Replace @path with the path of the mountpoint in the parent mount.
1051 * Up is towards /.
1052 *
1053 * Return 1 if we went up a level and 0 if we were already at the
1054 * root.
1055 */
Al Virobab77eb2009-04-18 03:26:48 -04001056int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Al Viro0714a532011-11-24 22:19:58 -05001058 struct mount *mnt = real_mount(path->mnt);
1059 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001061
Al Viro48a066e2013-09-29 22:06:07 -04001062 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001063 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001064 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001065 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return 0;
1067 }
Al Viro0714a532011-11-24 22:19:58 -05001068 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001069 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001070 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001071 dput(path->dentry);
1072 path->dentry = mountpoint;
1073 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001074 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 1;
1076}
Al Viro4d359502014-03-14 12:20:17 -04001077EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001079/*
David Howells9875cf82011-01-14 18:45:21 +00001080 * Perform an automount
1081 * - return -EISDIR to tell follow_managed() to stop and return the path we
1082 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 */
NeilBrown756daf22015-03-23 13:37:38 +11001084static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001085 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001086{
David Howells9875cf82011-01-14 18:45:21 +00001087 struct vfsmount *mnt;
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -05001088 const struct cred *old_cred;
David Howellsea5b7782011-01-14 19:10:03 +00001089 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001090
David Howells9875cf82011-01-14 18:45:21 +00001091 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1092 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001093
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001094 /* We don't want to mount if someone's just doing a stat -
1095 * unless they're stat'ing a directory and appended a '/' to
1096 * the name.
1097 *
1098 * We do, however, want to mount if someone wants to open or
1099 * create a file of any type under the mountpoint, wants to
1100 * traverse through the mountpoint or wants to open the
1101 * mounted directory. Also, autofs may mark negative dentries
1102 * as being automount points. These will need the attentions
1103 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001104 */
NeilBrown756daf22015-03-23 13:37:38 +11001105 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1106 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001107 path->dentry->d_inode)
1108 return -EISDIR;
1109
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -05001110 if (path->dentry->d_sb->s_user_ns != &init_user_ns)
1111 return -EACCES;
1112
NeilBrown756daf22015-03-23 13:37:38 +11001113 nd->total_link_count++;
1114 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001115 return -ELOOP;
1116
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -05001117 old_cred = override_creds(&init_cred);
David Howells9875cf82011-01-14 18:45:21 +00001118 mnt = path->dentry->d_op->d_automount(path);
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -05001119 revert_creds(old_cred);
David Howells9875cf82011-01-14 18:45:21 +00001120 if (IS_ERR(mnt)) {
1121 /*
1122 * The filesystem is allowed to return -EISDIR here to indicate
1123 * it doesn't want to automount. For instance, autofs would do
1124 * this so that its userspace daemon can mount on this dentry.
1125 *
1126 * However, we can only permit this if it's a terminal point in
1127 * the path being looked up; if it wasn't then the remainder of
1128 * the path is inaccessible and we should say so.
1129 */
NeilBrown756daf22015-03-23 13:37:38 +11001130 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001131 return -EREMOTE;
1132 return PTR_ERR(mnt);
1133 }
David Howellsea5b7782011-01-14 19:10:03 +00001134
David Howells9875cf82011-01-14 18:45:21 +00001135 if (!mnt) /* mount collision */
1136 return 0;
1137
Al Viro8aef1882011-06-16 15:10:06 +01001138 if (!*need_mntput) {
1139 /* lock_mount() may release path->mnt on error */
1140 mntget(path->mnt);
1141 *need_mntput = true;
1142 }
Al Viro19a167a2011-01-17 01:35:23 -05001143 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001144
David Howellsea5b7782011-01-14 19:10:03 +00001145 switch (err) {
1146 case -EBUSY:
1147 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001148 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001149 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001150 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001151 path->mnt = mnt;
1152 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001153 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001154 default:
1155 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001156 }
Al Viro19a167a2011-01-17 01:35:23 -05001157
David Howells9875cf82011-01-14 18:45:21 +00001158}
1159
1160/*
1161 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001162 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001163 * - Flagged as mountpoint
1164 * - Flagged as automount point
1165 *
1166 * This may only be called in refwalk mode.
1167 *
1168 * Serialization is taken care of in namespace.c
1169 */
NeilBrown756daf22015-03-23 13:37:38 +11001170static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001171{
Al Viro8aef1882011-06-16 15:10:06 +01001172 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001173 unsigned managed;
1174 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001175 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001176
1177 /* Given that we're not holding a lock here, we retain the value in a
1178 * local variable for each dentry as we look at it so that we don't see
1179 * the components of that value change under us */
1180 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1181 managed &= DCACHE_MANAGED_DENTRY,
1182 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001183 /* Allow the filesystem to manage the transit without i_mutex
1184 * being held. */
1185 if (managed & DCACHE_MANAGE_TRANSIT) {
1186 BUG_ON(!path->dentry->d_op);
1187 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001188 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001189 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001190 break;
David Howellscc53ce52011-01-14 18:45:26 +00001191 }
1192
David Howells9875cf82011-01-14 18:45:21 +00001193 /* Transit to a mounted filesystem. */
1194 if (managed & DCACHE_MOUNTED) {
1195 struct vfsmount *mounted = lookup_mnt(path);
1196 if (mounted) {
1197 dput(path->dentry);
1198 if (need_mntput)
1199 mntput(path->mnt);
1200 path->mnt = mounted;
1201 path->dentry = dget(mounted->mnt_root);
1202 need_mntput = true;
1203 continue;
1204 }
1205
1206 /* Something is mounted on this dentry in another
1207 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001208 * namespace got unmounted before lookup_mnt() could
1209 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001210 }
1211
1212 /* Handle an automount point */
1213 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001214 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001215 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001216 break;
David Howells9875cf82011-01-14 18:45:21 +00001217 continue;
1218 }
1219
1220 /* We didn't change the current path point */
1221 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
Al Viro8aef1882011-06-16 15:10:06 +01001223
1224 if (need_mntput && path->mnt == mnt)
1225 mntput(path->mnt);
Al Viroe9742b52016-03-05 22:04:59 -05001226 if (ret == -EISDIR || !ret)
1227 ret = 1;
Al Viro84027522015-04-22 10:30:08 -04001228 if (need_mntput)
1229 nd->flags |= LOOKUP_JUMPED;
1230 if (unlikely(ret < 0))
1231 path_put_conditional(path, nd);
1232 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233}
1234
David Howellscc53ce52011-01-14 18:45:26 +00001235int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
1237 struct vfsmount *mounted;
1238
Al Viro1c755af2009-04-18 14:06:57 -04001239 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001241 dput(path->dentry);
1242 mntput(path->mnt);
1243 path->mnt = mounted;
1244 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 1;
1246 }
1247 return 0;
1248}
Al Viro4d359502014-03-14 12:20:17 -04001249EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Ian Kentfb5f51c2016-11-24 08:03:41 +11001251static inline int managed_dentry_rcu(const struct path *path)
Ian Kent62a73752011-03-25 01:51:02 +08001252{
Ian Kentfb5f51c2016-11-24 08:03:41 +11001253 return (path->dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1254 path->dentry->d_op->d_manage(path, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001255}
1256
David Howells9875cf82011-01-14 18:45:21 +00001257/*
Al Viro287548e2011-05-27 06:50:06 -04001258 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1259 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001260 */
1261static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001262 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001263{
Ian Kent62a73752011-03-25 01:51:02 +08001264 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001265 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001266 /*
1267 * Don't forget we might have a non-mountpoint managed dentry
1268 * that wants to block transit.
1269 */
Ian Kentfb5f51c2016-11-24 08:03:41 +11001270 switch (managed_dentry_rcu(path)) {
NeilBrownb8faf032014-08-04 17:06:29 +10001271 case -ECHILD:
1272 default:
David Howellsab909112011-01-14 18:46:51 +00001273 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001274 case -EISDIR:
1275 return true;
1276 case 0:
1277 break;
1278 }
Ian Kent62a73752011-03-25 01:51:02 +08001279
1280 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001281 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001282
Al Viro474279d2013-10-01 16:11:26 -04001283 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001284 if (!mounted)
1285 break;
Al Viroc7105362011-11-24 18:22:03 -05001286 path->mnt = &mounted->mnt;
1287 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001288 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001289 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001290 /*
1291 * Update the inode too. We don't need to re-check the
1292 * dentry sequence number here after this d_inode read,
1293 * because a mount-point is always pinned.
1294 */
1295 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001296 }
Al Virof5be3e29122014-09-13 21:50:45 -04001297 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001298 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001299}
1300
Nick Piggin31e6b012011-01-07 17:49:52 +11001301static int follow_dotdot_rcu(struct nameidata *nd)
1302{
Al Viro4023bfc2014-09-13 21:59:43 -04001303 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001304
David Howells9875cf82011-01-14 18:45:21 +00001305 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001306 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001307 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001308 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1309 struct dentry *old = nd->path.dentry;
1310 struct dentry *parent = old->d_parent;
1311 unsigned seq;
1312
Al Viro4023bfc2014-09-13 21:59:43 -04001313 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001314 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001315 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1316 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001317 nd->path.dentry = parent;
1318 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001319 if (unlikely(!path_connected(&nd->path)))
1320 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001321 break;
Al Viroaed434a2015-05-12 12:22:47 -04001322 } else {
1323 struct mount *mnt = real_mount(nd->path.mnt);
1324 struct mount *mparent = mnt->mnt_parent;
1325 struct dentry *mountpoint = mnt->mnt_mountpoint;
1326 struct inode *inode2 = mountpoint->d_inode;
1327 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1328 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1329 return -ECHILD;
1330 if (&mparent->mnt == nd->path.mnt)
1331 break;
1332 /* we know that mountpoint was pinned */
1333 nd->path.dentry = mountpoint;
1334 nd->path.mnt = &mparent->mnt;
1335 inode = inode2;
1336 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001337 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001338 }
Al Viroaed434a2015-05-12 12:22:47 -04001339 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001340 struct mount *mounted;
1341 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001342 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1343 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001344 if (!mounted)
1345 break;
1346 nd->path.mnt = &mounted->mnt;
1347 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001348 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001349 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001350 }
Al Viro4023bfc2014-09-13 21:59:43 -04001351 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001352 return 0;
1353}
1354
David Howells9875cf82011-01-14 18:45:21 +00001355/*
David Howellscc53ce52011-01-14 18:45:26 +00001356 * Follow down to the covering mount currently visible to userspace. At each
1357 * point, the filesystem owning that dentry may be queried as to whether the
1358 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001359 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001360int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001361{
1362 unsigned managed;
1363 int ret;
1364
1365 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1366 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1367 /* Allow the filesystem to manage the transit without i_mutex
1368 * being held.
1369 *
1370 * We indicate to the filesystem if someone is trying to mount
1371 * something here. This gives autofs the chance to deny anyone
1372 * other than its daemon the right to mount on its
1373 * superstructure.
1374 *
1375 * The filesystem may sleep at this point.
1376 */
1377 if (managed & DCACHE_MANAGE_TRANSIT) {
1378 BUG_ON(!path->dentry->d_op);
1379 BUG_ON(!path->dentry->d_op->d_manage);
Ian Kentfb5f51c2016-11-24 08:03:41 +11001380 ret = path->dentry->d_op->d_manage(path, false);
David Howellscc53ce52011-01-14 18:45:26 +00001381 if (ret < 0)
1382 return ret == -EISDIR ? 0 : ret;
1383 }
1384
1385 /* Transit to a mounted filesystem. */
1386 if (managed & DCACHE_MOUNTED) {
1387 struct vfsmount *mounted = lookup_mnt(path);
1388 if (!mounted)
1389 break;
1390 dput(path->dentry);
1391 mntput(path->mnt);
1392 path->mnt = mounted;
1393 path->dentry = dget(mounted->mnt_root);
1394 continue;
1395 }
1396
1397 /* Don't handle automount points here */
1398 break;
1399 }
1400 return 0;
1401}
Al Viro4d359502014-03-14 12:20:17 -04001402EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001403
1404/*
David Howells9875cf82011-01-14 18:45:21 +00001405 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1406 */
1407static void follow_mount(struct path *path)
1408{
1409 while (d_mountpoint(path->dentry)) {
1410 struct vfsmount *mounted = lookup_mnt(path);
1411 if (!mounted)
1412 break;
1413 dput(path->dentry);
1414 mntput(path->mnt);
1415 path->mnt = mounted;
1416 path->dentry = dget(mounted->mnt_root);
1417 }
1418}
1419
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001420static int path_parent_directory(struct path *path)
1421{
1422 struct dentry *old = path->dentry;
1423 /* rare case of legitimate dget_parent()... */
1424 path->dentry = dget_parent(path->dentry);
1425 dput(old);
1426 if (unlikely(!path_connected(path)))
1427 return -ENOENT;
1428 return 0;
1429}
1430
Eric W. Biederman397d4252015-08-15 20:27:13 -05001431static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
1433 while(1) {
Al Viro2a737872009-04-07 11:49:53 -04001434 if (nd->path.dentry == nd->root.dentry &&
1435 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 break;
1437 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001438 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001439 int ret = path_parent_directory(&nd->path);
1440 if (ret)
1441 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 break;
1443 }
Al Viro3088dd72010-01-30 15:47:29 -05001444 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Al Viro79ed0222009-04-18 13:59:41 -04001447 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001448 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001453 * This looks up the name in dcache and possibly revalidates the found dentry.
1454 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001455 */
Al Viroe3c13922016-03-06 14:03:27 -05001456static struct dentry *lookup_dcache(const struct qstr *name,
1457 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001458 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001459{
Al Viroa89f8332017-01-09 22:25:28 -05001460 struct dentry *dentry = d_lookup(dir, name);
Miklos Szeredibad61182012-03-26 12:54:24 +02001461 if (dentry) {
Al Viroa89f8332017-01-09 22:25:28 -05001462 int error = d_revalidate(dentry, flags);
1463 if (unlikely(error <= 0)) {
1464 if (!error)
1465 d_invalidate(dentry);
1466 dput(dentry);
1467 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001468 }
1469 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001470 return dentry;
1471}
1472
1473/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001474 * Call i_op->lookup on the dentry. The dentry must be negative and
1475 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001476 *
1477 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001478 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001479static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001480 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001481{
Josef Bacik44396f42011-05-31 11:58:49 -04001482 struct dentry *old;
1483
1484 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001485 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001486 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001487 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001488 }
Josef Bacik44396f42011-05-31 11:58:49 -04001489
Al Viro72bd8662012-06-10 17:17:17 -04001490 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001491 if (unlikely(old)) {
1492 dput(dentry);
1493 dentry = old;
1494 }
1495 return dentry;
1496}
1497
Al Viroe3c13922016-03-06 14:03:27 -05001498static struct dentry *__lookup_hash(const struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001499 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001500{
Al Viro6c51e512016-03-05 20:09:32 -05001501 struct dentry *dentry = lookup_dcache(name, base, flags);
Al Viroa3255542012-03-30 14:41:51 -04001502
Al Viro6c51e512016-03-05 20:09:32 -05001503 if (dentry)
Miklos Szeredibad61182012-03-26 12:54:24 +02001504 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001505
Al Viro6c51e512016-03-05 20:09:32 -05001506 dentry = d_alloc(base, name);
1507 if (unlikely(!dentry))
1508 return ERR_PTR(-ENOMEM);
1509
Al Viro72bd8662012-06-10 17:17:17 -04001510 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001511}
1512
Al Viroe97cdc82013-01-24 18:16:00 -05001513static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001514 struct path *path, struct inode **inode,
1515 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516{
Jan Blunck4ac91372008-02-14 19:34:32 -08001517 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001518 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001519 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001520 int err;
1521
Al Viro3cac2602009-08-13 18:27:43 +04001522 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001523 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001524 * of a false negative due to a concurrent rename, the caller is
1525 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001526 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001527 if (nd->flags & LOOKUP_RCU) {
1528 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001529 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001530 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001531 if (unlikely(!dentry)) {
1532 if (unlazy_walk(nd, NULL, 0))
1533 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001534 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001535 }
Al Viro5a18fff2011-03-11 04:44:53 -05001536
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001537 /*
1538 * This sequence count validates that the inode matches
1539 * the dentry name information from lookup.
1540 */
David Howells63afdfc2015-05-06 15:59:00 +01001541 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001542 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001543 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001544 return -ECHILD;
1545
1546 /*
1547 * This sequence count validates that the parent had no
1548 * changes while we did the lookup of the dentry above.
1549 *
1550 * The memory barrier in read_seqcount_begin of child is
1551 * enough, we can use __read_seqcount_retry here.
1552 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001553 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001554 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001555
Al Viro254cf582015-05-05 09:40:46 -04001556 *seqp = seq;
Al Viroa89f8332017-01-09 22:25:28 -05001557 status = d_revalidate(dentry, nd->flags);
Al Viro209a7fb2017-01-09 01:35:39 -05001558 if (likely(status > 0)) {
Al Viro5d0f49c2016-03-05 21:32:53 -05001559 /*
1560 * Note: do negative dentry check after revalidation in
1561 * case that drops it.
1562 */
1563 if (unlikely(negative))
1564 return -ENOENT;
1565 path->mnt = mnt;
1566 path->dentry = dentry;
1567 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001568 return 1;
Al Viro24643082011-02-15 01:26:22 -05001569 }
Al Viro209a7fb2017-01-09 01:35:39 -05001570 if (unlazy_walk(nd, dentry, seq))
1571 return -ECHILD;
1572 if (unlikely(status == -ECHILD))
1573 /* we'd been told to redo it in non-rcu mode */
1574 status = d_revalidate(dentry, nd->flags);
Al Viro5a18fff2011-03-11 04:44:53 -05001575 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001576 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001577 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001578 return 0;
Al Viroa89f8332017-01-09 22:25:28 -05001579 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001580 }
Al Viro5a18fff2011-03-11 04:44:53 -05001581 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001582 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001583 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001584 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001585 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001586 }
Al Viro766c4cb2015-05-07 19:24:57 -04001587 if (unlikely(d_is_negative(dentry))) {
1588 dput(dentry);
1589 return -ENOENT;
1590 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001591
David Howells9875cf82011-01-14 18:45:21 +00001592 path->mnt = mnt;
1593 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001594 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001595 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001596 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001597 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001598}
1599
1600/* Fast lookup failed, do it the slow way */
Al Viroe3c13922016-03-06 14:03:27 -05001601static 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 Viro94bdd652016-04-15 02:42:04 -04001605 struct dentry *dentry = ERR_PTR(-ENOENT), *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 Viro9902af72016-04-15 15:08:36 -04001609 inode_lock_shared(inode);
Al Viro19363862016-04-14 19:33:34 -04001610 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001611 if (unlikely(IS_DEADDIR(inode)))
1612 goto out;
1613again:
Al Virod9171b92016-04-15 03:33:13 -04001614 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001615 if (IS_ERR(dentry))
1616 goto out;
1617 if (unlikely(!d_in_lookup(dentry))) {
Al Viroa89f8332017-01-09 22:25:28 -05001618 if (!(flags & LOOKUP_NO_REVAL)) {
Al Viro949a8522016-03-06 14:20:52 -05001619 int error = d_revalidate(dentry, flags);
1620 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001621 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001622 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001623 dput(dentry);
1624 goto again;
1625 }
Al Viro949a8522016-03-06 14:20:52 -05001626 dput(dentry);
1627 dentry = ERR_PTR(error);
1628 }
1629 }
Al Viro94bdd652016-04-15 02:42:04 -04001630 } else {
1631 old = inode->i_op->lookup(inode, dentry, flags);
1632 d_lookup_done(dentry);
1633 if (unlikely(old)) {
1634 dput(dentry);
1635 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001636 }
1637 }
Al Viro94bdd652016-04-15 02:42:04 -04001638out:
Al Viro9902af72016-04-15 15:08:36 -04001639 inode_unlock_shared(inode);
Al Viroe3c13922016-03-06 14:03:27 -05001640 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641}
1642
Al Viro52094c82011-02-21 21:34:47 -05001643static inline int may_lookup(struct nameidata *nd)
1644{
1645 if (nd->flags & LOOKUP_RCU) {
Al Viro4ad5abb2011-06-20 19:57:03 -04001646 int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001647 if (err != -ECHILD)
1648 return err;
Al Viro6e9918b2015-05-05 09:26:05 -04001649 if (unlazy_walk(nd, NULL, 0))
Al Viro52094c82011-02-21 21:34:47 -05001650 return -ECHILD;
1651 }
Al Viro4ad5abb2011-06-20 19:57:03 -04001652 return inode_permission(nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001653}
1654
Al Viro9856fa12011-03-04 14:22:06 -05001655static inline int handle_dots(struct nameidata *nd, int type)
1656{
1657 if (type == LAST_DOTDOT) {
Al Viro9e6697e2015-12-05 20:07:21 -05001658 if (!nd->root.mnt)
1659 set_root(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001660 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001661 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001662 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001663 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001664 }
1665 return 0;
1666}
1667
Al Viro181548c2015-05-07 19:54:34 -04001668static int pick_link(struct nameidata *nd, struct path *link,
1669 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001670{
Al Viro626de992015-05-04 18:26:59 -04001671 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001672 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001673 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001674 path_to_nameidata(link, nd);
1675 return -ELOOP;
1676 }
Al Virobc40aee2015-05-09 13:04:24 -04001677 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001678 if (link->mnt == nd->path.mnt)
1679 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001680 }
Al Viro626de992015-05-04 18:26:59 -04001681 error = nd_alloc_stack(nd);
1682 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001683 if (error == -ECHILD) {
Al Viroad1633a2017-01-08 22:35:31 -05001684 if (unlikely(!legitimize_path(nd, link, seq))) {
1685 drop_links(nd);
1686 nd->depth = 0;
1687 nd->flags &= ~LOOKUP_RCU;
1688 nd->path.mnt = NULL;
1689 nd->path.dentry = NULL;
1690 if (!(nd->flags & LOOKUP_ROOT))
1691 nd->root.mnt = NULL;
1692 rcu_read_unlock();
1693 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0)
1694 error = nd_alloc_stack(nd);
Al Virobc40aee2015-05-09 13:04:24 -04001695 }
1696 if (error) {
1697 path_put(link);
1698 return error;
1699 }
Al Viro626de992015-05-04 18:26:59 -04001700 }
1701
Al Viroab104922015-05-10 11:50:01 -04001702 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001703 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001704 clear_delayed_call(&last->done);
1705 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001706 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001707 return 1;
1708}
1709
Al Viro8f64fb12016-11-14 01:50:26 -05001710enum {WALK_FOLLOW = 1, WALK_MORE = 2};
Al Viro31d66bc2016-11-14 01:43:34 -05001711
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001712/*
1713 * Do we need to follow links? We _really_ want to be able
1714 * to do this check without having to look at inode->i_op,
1715 * so we keep a cache of "no, this doesn't need follow_link"
1716 * for the common case.
1717 */
Al Viro8f64fb12016-11-14 01:50:26 -05001718static inline int step_into(struct nameidata *nd, struct path *path,
1719 int flags, struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001720{
Al Viro31d66bc2016-11-14 01:43:34 -05001721 if (!(flags & WALK_MORE) && nd->depth)
1722 put_link(nd);
Al Viro8f64fb12016-11-14 01:50:26 -05001723 if (likely(!d_is_symlink(path->dentry)) ||
1724 !(flags & WALK_FOLLOW || nd->flags & LOOKUP_FOLLOW)) {
1725 /* not a symlink or should not follow */
1726 path_to_nameidata(path, nd);
1727 nd->inode = inode;
1728 nd->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001729 return 0;
Al Viro8f64fb12016-11-14 01:50:26 -05001730 }
Al Viroa7f77542016-02-27 19:31:01 -05001731 /* make sure that d_is_symlink above matches inode */
1732 if (nd->flags & LOOKUP_RCU) {
Al Viro8f64fb12016-11-14 01:50:26 -05001733 if (read_seqcount_retry(&path->dentry->d_seq, seq))
Al Viroa7f77542016-02-27 19:31:01 -05001734 return -ECHILD;
1735 }
Al Viro8f64fb12016-11-14 01:50:26 -05001736 return pick_link(nd, path, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001737}
1738
Al Viro4693a542015-05-04 17:47:11 -04001739static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001740{
Al Virocaa856342015-04-22 17:52:47 -04001741 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001742 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001743 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001744 int err;
1745 /*
1746 * "." and ".." are special - ".." especially so because it has
1747 * to be able to know about the current root directory and
1748 * parent relationships.
1749 */
Al Viro4693a542015-05-04 17:47:11 -04001750 if (unlikely(nd->last_type != LAST_NORM)) {
1751 err = handle_dots(nd, nd->last_type);
Al Viro1c4ff1a2016-11-14 01:39:36 -05001752 if (!(flags & WALK_MORE) && nd->depth)
Al Viro4693a542015-05-04 17:47:11 -04001753 put_link(nd);
1754 return err;
1755 }
Al Viro254cf582015-05-05 09:40:46 -04001756 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001757 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001758 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001759 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001760 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1761 nd->flags);
1762 if (IS_ERR(path.dentry))
1763 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001764
Al Viroe3c13922016-03-06 14:03:27 -05001765 path.mnt = nd->path.mnt;
1766 err = follow_managed(&path, nd);
1767 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001768 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001769
Al Viro7500c382016-03-31 00:23:05 -04001770 if (unlikely(d_is_negative(path.dentry))) {
1771 path_to_nameidata(&path, nd);
1772 return -ENOENT;
1773 }
1774
Al Viro254cf582015-05-05 09:40:46 -04001775 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001776 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001777 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001778
Al Viro8f64fb12016-11-14 01:50:26 -05001779 return step_into(nd, &path, flags, inode, seq);
Al Viroce57dfc2011-03-13 19:58:58 -04001780}
1781
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001783 * We can do the critical dentry name comparison and hashing
1784 * operations one word at a time, but we are limited to:
1785 *
1786 * - Architectures with fast unaligned word accesses. We could
1787 * do a "get_unaligned()" if this helps and is sufficiently
1788 * fast.
1789 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001790 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1791 * do not trap on the (extremely unlikely) case of a page
1792 * crossing operation.
1793 *
1794 * - Furthermore, we need an efficient 64-bit compile for the
1795 * 64-bit case in order to generate the "number of bytes in
1796 * the final mask". Again, that could be replaced with a
1797 * efficient population count instruction or similar.
1798 */
1799#ifdef CONFIG_DCACHE_WORD_ACCESS
1800
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001801#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001802
George Spelvin468a9422016-05-26 22:11:51 -04001803#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001804
George Spelvin468a9422016-05-26 22:11:51 -04001805/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1806
1807#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001808/*
1809 * Register pressure in the mixing function is an issue, particularly
1810 * on 32-bit x86, but almost any function requires one state value and
1811 * one temporary. Instead, use a function designed for two state values
1812 * and no temporaries.
1813 *
1814 * This function cannot create a collision in only two iterations, so
1815 * we have two iterations to achieve avalanche. In those two iterations,
1816 * we have six layers of mixing, which is enough to spread one bit's
1817 * influence out to 2^6 = 64 state bits.
1818 *
1819 * Rotate constants are scored by considering either 64 one-bit input
1820 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1821 * probability of that delta causing a change to each of the 128 output
1822 * bits, using a sample of random initial states.
1823 *
1824 * The Shannon entropy of the computed probabilities is then summed
1825 * to produce a score. Ideally, any input change has a 50% chance of
1826 * toggling any given output bit.
1827 *
1828 * Mixing scores (in bits) for (12,45):
1829 * Input delta: 1-bit 2-bit
1830 * 1 round: 713.3 42542.6
1831 * 2 rounds: 2753.7 140389.8
1832 * 3 rounds: 5954.1 233458.2
1833 * 4 rounds: 7862.6 256672.2
1834 * Perfect: 8192 258048
1835 * (64*128) (64*63/2 * 128)
1836 */
1837#define HASH_MIX(x, y, a) \
1838 ( x ^= (a), \
1839 y ^= x, x = rol64(x,12),\
1840 x += y, y = rol64(y,45),\
1841 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001842
George Spelvin0fed3ac2016-05-02 06:31:01 -04001843/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001844 * Fold two longs into one 32-bit hash value. This must be fast, but
1845 * latency isn't quite as critical, as there is a fair bit of additional
1846 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001847 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001848static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001849{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001850 y ^= x * GOLDEN_RATIO_64;
1851 y *= GOLDEN_RATIO_64;
1852 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001853}
1854
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001855#else /* 32-bit case */
1856
George Spelvin2a18da7a2016-05-23 07:43:58 -04001857/*
1858 * Mixing scores (in bits) for (7,20):
1859 * Input delta: 1-bit 2-bit
1860 * 1 round: 330.3 9201.6
1861 * 2 rounds: 1246.4 25475.4
1862 * 3 rounds: 1907.1 31295.1
1863 * 4 rounds: 2042.3 31718.6
1864 * Perfect: 2048 31744
1865 * (32*64) (32*31/2 * 64)
1866 */
1867#define HASH_MIX(x, y, a) \
1868 ( x ^= (a), \
1869 y ^= x, x = rol32(x, 7),\
1870 x += y, y = rol32(y,20),\
1871 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001872
George Spelvin2a18da7a2016-05-23 07:43:58 -04001873static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001874{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001875 /* Use arch-optimized multiply if one exists */
1876 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001877}
1878
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001879#endif
1880
George Spelvin2a18da7a2016-05-23 07:43:58 -04001881/*
1882 * Return the hash of a string of known length. This is carfully
1883 * designed to match hash_name(), which is the more critical function.
1884 * In particular, we must end by hashing a final word containing 0..7
1885 * payload bytes, to match the way that hash_name() iterates until it
1886 * finds the delimiter after the name.
1887 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001888unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001889{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001890 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001891
1892 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001893 if (!len)
1894 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001895 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001896 if (len < sizeof(unsigned long))
1897 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001898 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001899 name += sizeof(unsigned long);
1900 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001901 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001902 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001903done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001904 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001905}
1906EXPORT_SYMBOL(full_name_hash);
1907
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001908/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001909u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001910{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001911 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1912 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001913 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1914
Linus Torvalds8387ff22016-06-10 07:51:30 -07001915 len = 0;
1916 goto inside;
1917
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001918 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001919 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001920 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001921inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001922 a = load_unaligned_zeropad(name+len);
1923 } while (!has_zero(a, &adata, &constants));
1924
1925 adata = prep_zero_mask(a, adata, &constants);
1926 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001927 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001928
George Spelvin2a18da7a2016-05-23 07:43:58 -04001929 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001930}
1931EXPORT_SYMBOL(hashlen_string);
1932
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001933/*
1934 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001935 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001936 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001937static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001938{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001939 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1940 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07001941 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001942
Linus Torvalds8387ff22016-06-10 07:51:30 -07001943 len = 0;
1944 goto inside;
1945
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001946 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001947 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001948 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001949inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001950 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001951 b = a ^ REPEAT_BYTE('/');
1952 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001953
Linus Torvalds36126f82012-05-26 10:43:17 -07001954 adata = prep_zero_mask(a, adata, &constants);
1955 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07001956 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001957 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07001958
George Spelvin2a18da7a2016-05-23 07:43:58 -04001959 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001960}
1961
George Spelvin2a18da7a2016-05-23 07:43:58 -04001962#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001963
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001964/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001965unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08001966{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001967 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001968 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001969 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001970 return end_name_hash(hash);
1971}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08001972EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08001973
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001974/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001975u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001976{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001977 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001978 unsigned long len = 0, c;
1979
1980 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04001981 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001982 len++;
1983 hash = partial_name_hash(c, hash);
1984 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04001985 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001986 return hashlen_create(end_name_hash(hash), len);
1987}
George Spelvinf2a031b2016-05-29 01:26:41 -04001988EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001989
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001990/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001991 * We know there's a real path component here of at least
1992 * one character.
1993 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001994static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001995{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001996 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08001997 unsigned long len = 0, c;
1998
1999 c = (unsigned char)*name;
2000 do {
2001 len++;
2002 hash = partial_name_hash(c, hash);
2003 c = (unsigned char)name[len];
2004 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002005 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002006}
2007
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002008#endif
2009
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002010/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002012 * This is the basic name resolution function, turning a pathname into
2013 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002015 * Returns 0 and nd will have valid dentry and mnt on success.
2016 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Al Viro6de88d72009-08-09 01:41:57 +04002018static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002021
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 while (*name=='/')
2023 name++;
2024 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002025 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* At this point we know we have a real path component. */
2028 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002029 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002030 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
Al Viro52094c82011-02-21 21:34:47 -05002032 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002033 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002034 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035
Linus Torvalds8387ff22016-06-10 07:51:30 -07002036 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Al Virofe479a52011-02-22 15:10:03 -05002038 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002039 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002040 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002041 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002042 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002043 nd->flags |= LOOKUP_JUMPED;
2044 }
Al Virofe479a52011-02-22 15:10:03 -05002045 break;
2046 case 1:
2047 type = LAST_DOT;
2048 }
Al Viro5a202bc2011-03-08 14:17:44 -05002049 if (likely(type == LAST_NORM)) {
2050 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002051 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002052 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002053 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002054 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002055 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002056 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002057 hash_len = this.hash_len;
2058 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002059 }
2060 }
Al Virofe479a52011-02-22 15:10:03 -05002061
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002062 nd->last.hash_len = hash_len;
2063 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002064 nd->last_type = type;
2065
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002066 name += hashlen_len(hash_len);
2067 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002068 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002069 /*
2070 * If it wasn't NUL, we know it was '/'. Skip that
2071 * slash, and continue until no more slashes.
2072 */
2073 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002074 name++;
2075 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002076 if (unlikely(!*name)) {
2077OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002078 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002079 if (!nd->depth)
2080 return 0;
2081 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002082 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002083 if (!name)
2084 return 0;
2085 /* last component of nested symlink */
Al Viro8f64fb12016-11-14 01:50:26 -05002086 err = walk_component(nd, WALK_FOLLOW);
Al Viro1c4ff1a2016-11-14 01:39:36 -05002087 } else {
2088 /* not the last component */
Al Viro8f64fb12016-11-14 01:50:26 -05002089 err = walk_component(nd, WALK_FOLLOW | WALK_MORE);
Al Viro8620c232015-05-04 08:58:35 -04002090 }
Al Viroce57dfc2011-03-13 19:58:58 -04002091 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002092 return err;
Al Virofe479a52011-02-22 15:10:03 -05002093
Al Viroce57dfc2011-03-13 19:58:58 -04002094 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002095 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002096
Viresh Kumara1c83682015-08-12 15:59:44 +05302097 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002098 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002099 err = 0;
2100 if (unlikely(!s)) {
2101 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002102 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002103 } else {
Al Virofab51e82015-05-10 11:01:00 -04002104 nd->stack[nd->depth - 1].name = name;
2105 name = s;
2106 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002107 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002108 }
Al Viro97242f92015-08-01 19:59:28 -04002109 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2110 if (nd->flags & LOOKUP_RCU) {
2111 if (unlazy_walk(nd, NULL, 0))
2112 return -ECHILD;
2113 }
Al Viro3595e232015-05-09 16:54:45 -04002114 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117}
2118
Al Viroc8a53ee2015-05-12 18:43:07 -04002119static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
Prasanna Medaea3834d2005-04-29 16:00:17 +01002121 int retval = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -04002122 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002125 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002127 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002128 struct dentry *root = nd->root.dentry;
2129 struct inode *inode = root->d_inode;
Al Virofd2f7cb2015-02-22 20:07:13 -05002130 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002131 if (!d_can_lookup(root))
Al Viro368ee9b2015-05-08 17:19:59 -04002132 return ERR_PTR(-ENOTDIR);
Al Viro73d049a2011-03-11 12:08:24 -05002133 retval = inode_permission(inode, MAY_EXEC);
2134 if (retval)
Al Viro368ee9b2015-05-08 17:19:59 -04002135 return ERR_PTR(retval);
Al Viro73d049a2011-03-11 12:08:24 -05002136 }
Al Viro5b6ca022011-03-09 23:04:47 -05002137 nd->path = nd->root;
2138 nd->inode = inode;
2139 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002140 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05002141 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002142 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002143 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002144 } else {
2145 path_get(&nd->path);
2146 }
Al Viro368ee9b2015-05-08 17:19:59 -04002147 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002148 }
2149
Al Viro2a737872009-04-07 11:49:53 -04002150 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002151 nd->path.mnt = NULL;
2152 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Al Viro48a066e2013-09-29 22:06:07 -04002154 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002155 if (*s == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05002156 if (flags & LOOKUP_RCU)
Al Viro8b61e742013-11-08 12:45:01 -05002157 rcu_read_lock();
Al Viro9e6697e2015-12-05 20:07:21 -05002158 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05002159 if (likely(!nd_jump_root(nd)))
Al Viroef55d912015-12-05 20:25:06 -05002160 return s;
Al Viro248fb5b2015-12-05 20:51:58 -05002161 nd->root.mnt = NULL;
Al Viroef55d912015-12-05 20:25:06 -05002162 rcu_read_unlock();
2163 return ERR_PTR(-ECHILD);
Al Viroc8a53ee2015-05-12 18:43:07 -04002164 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002165 if (flags & LOOKUP_RCU) {
2166 struct fs_struct *fs = current->fs;
2167 unsigned seq;
2168
Al Viro8b61e742013-11-08 12:45:01 -05002169 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002170
2171 do {
2172 seq = read_seqcount_begin(&fs->seq);
2173 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002174 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002175 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2176 } while (read_seqcount_retry(&fs->seq, seq));
2177 } else {
2178 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002179 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002180 }
Al Viroef55d912015-12-05 20:25:06 -05002181 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002182 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002183 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002184 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002185 struct dentry *dentry;
2186
Al Viro2903ff02012-08-28 12:52:22 -04002187 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002188 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002189
Al Viro2903ff02012-08-28 12:52:22 -04002190 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002191
Al Virofd2f7cb2015-02-22 20:07:13 -05002192 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002193 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002194 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002195 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002196 }
Al Virof52e0c12011-03-14 18:56:51 -04002197 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002198
Al Viro2903ff02012-08-28 12:52:22 -04002199 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002200 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002201 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002202 nd->inode = nd->path.dentry->d_inode;
2203 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002204 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002205 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002206 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002207 }
Al Viro34a26b92015-05-11 08:05:05 -04002208 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002209 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002211}
2212
Al Viro3bdba282015-05-08 17:37:07 -04002213static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002214{
2215 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002216 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002217 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002218 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002219 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002220 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002221 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002222 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002223}
2224
Al Virocaa856342015-04-22 17:52:47 -04002225static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002226{
2227 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2228 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2229
2230 nd->flags &= ~LOOKUP_PARENT;
Al Viro1c4ff1a2016-11-14 01:39:36 -05002231 return walk_component(nd, 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002232}
2233
Al Viro9b4a9b12009-04-07 11:44:16 -04002234/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002235static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002236{
Al Viroc8a53ee2015-05-12 18:43:07 -04002237 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002238 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002239
Al Viro368ee9b2015-05-08 17:19:59 -04002240 if (IS_ERR(s))
2241 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002242 while (!(err = link_path_walk(s, nd))
2243 && ((err = lookup_last(nd)) > 0)) {
2244 s = trailing_symlink(nd);
2245 if (IS_ERR(s)) {
2246 err = PTR_ERR(s);
2247 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002248 }
2249 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002250 if (!err)
2251 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002252
Al Virodeb106c2015-05-08 18:05:21 -04002253 if (!err && nd->flags & LOOKUP_DIRECTORY)
2254 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002255 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002256 if (!err) {
2257 *path = nd->path;
2258 nd->path.mnt = NULL;
2259 nd->path.dentry = NULL;
2260 }
2261 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002262 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002263}
Nick Piggin31e6b012011-01-07 17:49:52 +11002264
Al Viro625b6d12015-05-12 16:36:12 -04002265static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002266 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002267{
Al Viro894bc8c2015-05-02 07:16:16 -04002268 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002269 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002270 if (IS_ERR(name))
2271 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002272 if (unlikely(root)) {
2273 nd.root = *root;
2274 flags |= LOOKUP_ROOT;
2275 }
Al Viro9883d182015-05-13 07:28:08 -04002276 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002277 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002278 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002279 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002280 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002281 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002282
2283 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002284 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002285 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002286 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002287 return retval;
2288}
2289
Al Viro8bcb77f2015-05-08 16:59:20 -04002290/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002291static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002292 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002293{
Al Viroc8a53ee2015-05-12 18:43:07 -04002294 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002295 int err;
2296 if (IS_ERR(s))
2297 return PTR_ERR(s);
2298 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002299 if (!err)
2300 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002301 if (!err) {
2302 *parent = nd->path;
2303 nd->path.mnt = NULL;
2304 nd->path.dentry = NULL;
2305 }
2306 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002307 return err;
2308}
2309
Al Viro5c31b6c2015-05-12 17:32:54 -04002310static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002311 unsigned int flags, struct path *parent,
2312 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002313{
2314 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002315 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002316
Al Viro5c31b6c2015-05-12 17:32:54 -04002317 if (IS_ERR(name))
2318 return name;
Al Viro9883d182015-05-13 07:28:08 -04002319 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002320 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002321 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002322 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002323 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002324 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002325 if (likely(!retval)) {
2326 *last = nd.last;
2327 *type = nd.last_type;
2328 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002329 } else {
2330 putname(name);
2331 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002332 }
Al Viro9883d182015-05-13 07:28:08 -04002333 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002334 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002335}
2336
Al Viro79714f72012-06-15 03:01:42 +04002337/* does lookup, returns the object with parent locked */
2338struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002339{
Al Viro5c31b6c2015-05-12 17:32:54 -04002340 struct filename *filename;
2341 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002342 struct qstr last;
2343 int type;
Paul Moore51689102015-01-22 00:00:03 -05002344
Al Viro5c31b6c2015-05-12 17:32:54 -04002345 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2346 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002347 if (IS_ERR(filename))
2348 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002349 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002350 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002351 putname(filename);
2352 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002353 }
Al Viro59551022016-01-22 15:40:57 -05002354 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002355 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002356 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002357 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002358 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002359 }
Paul Moore51689102015-01-22 00:00:03 -05002360 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002361 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002362}
2363
Al Virod1811462008-08-02 00:49:18 -04002364int kern_path(const char *name, unsigned int flags, struct path *path)
2365{
Al Viroabc9f5b2015-05-12 16:53:42 -04002366 return filename_lookup(AT_FDCWD, getname_kernel(name),
2367 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002368}
Al Viro4d359502014-03-14 12:20:17 -04002369EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002370
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002371/**
2372 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2373 * @dentry: pointer to dentry of the base directory
2374 * @mnt: pointer to vfs mount of the base directory
2375 * @name: pointer to file name
2376 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002377 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002378 */
2379int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2380 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002381 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002382{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002383 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002384 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002385 return filename_lookup(AT_FDCWD, getname_kernel(name),
2386 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002387}
Al Viro4d359502014-03-14 12:20:17 -04002388EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002389
Christoph Hellwigeead1912007-10-16 23:25:38 -07002390/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002391 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002392 * @name: pathname component to lookup
2393 * @base: base directory to lookup from
2394 * @len: maximum length @len should be interpreted to
2395 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002396 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002397 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002398 *
2399 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002400 */
James Morris057f6c02007-04-26 00:12:05 -07002401struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2402{
James Morris057f6c02007-04-26 00:12:05 -07002403 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002404 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002405 int err;
James Morris057f6c02007-04-26 00:12:05 -07002406
Al Viro59551022016-01-22 15:40:57 -05002407 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002408
Al Viro6a96ba52011-03-07 23:49:20 -05002409 this.name = name;
2410 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002411 this.hash = full_name_hash(base, name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002412 if (!len)
2413 return ERR_PTR(-EACCES);
2414
Al Viro21d8a152012-11-29 22:17:21 -05002415 if (unlikely(name[0] == '.')) {
2416 if (len < 2 || (len == 2 && name[1] == '.'))
2417 return ERR_PTR(-EACCES);
2418 }
2419
Al Viro6a96ba52011-03-07 23:49:20 -05002420 while (len--) {
2421 c = *(const unsigned char *)name++;
2422 if (c == '/' || c == '\0')
2423 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002424 }
Al Viro5a202bc2011-03-08 14:17:44 -05002425 /*
2426 * See if the low-level filesystem might want
2427 * to use its own hash..
2428 */
2429 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002430 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002431 if (err < 0)
2432 return ERR_PTR(err);
2433 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002434
Miklos Szeredicda309d2012-03-26 12:54:21 +02002435 err = inode_permission(base->d_inode, MAY_EXEC);
2436 if (err)
2437 return ERR_PTR(err);
2438
Al Viro72bd8662012-06-10 17:17:17 -04002439 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002440}
Al Viro4d359502014-03-14 12:20:17 -04002441EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002442
NeilBrownbbddca82016-01-07 16:08:20 -05002443/**
2444 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2445 * @name: pathname component to lookup
2446 * @base: base directory to lookup from
2447 * @len: maximum length @len should be interpreted to
2448 *
2449 * Note that this routine is purely a helper for filesystem usage and should
2450 * not be called by generic code.
2451 *
2452 * Unlike lookup_one_len, it should be called without the parent
2453 * i_mutex held, and will take the i_mutex itself if necessary.
2454 */
2455struct dentry *lookup_one_len_unlocked(const char *name,
2456 struct dentry *base, int len)
2457{
2458 struct qstr this;
2459 unsigned int c;
2460 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002461 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002462
2463 this.name = name;
2464 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002465 this.hash = full_name_hash(base, name, len);
NeilBrownbbddca82016-01-07 16:08:20 -05002466 if (!len)
2467 return ERR_PTR(-EACCES);
2468
2469 if (unlikely(name[0] == '.')) {
2470 if (len < 2 || (len == 2 && name[1] == '.'))
2471 return ERR_PTR(-EACCES);
2472 }
2473
2474 while (len--) {
2475 c = *(const unsigned char *)name++;
2476 if (c == '/' || c == '\0')
2477 return ERR_PTR(-EACCES);
2478 }
2479 /*
2480 * See if the low-level filesystem might want
2481 * to use its own hash..
2482 */
2483 if (base->d_flags & DCACHE_OP_HASH) {
2484 int err = base->d_op->d_hash(base, &this);
2485 if (err < 0)
2486 return ERR_PTR(err);
2487 }
2488
2489 err = inode_permission(base->d_inode, MAY_EXEC);
2490 if (err)
2491 return ERR_PTR(err);
2492
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002493 ret = lookup_dcache(&this, base, 0);
2494 if (!ret)
2495 ret = lookup_slow(&this, base, 0);
2496 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002497}
2498EXPORT_SYMBOL(lookup_one_len_unlocked);
2499
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002500#ifdef CONFIG_UNIX98_PTYS
2501int path_pts(struct path *path)
2502{
2503 /* Find something mounted on "pts" in the same directory as
2504 * the input path.
2505 */
2506 struct dentry *child, *parent;
2507 struct qstr this;
2508 int ret;
2509
2510 ret = path_parent_directory(path);
2511 if (ret)
2512 return ret;
2513
2514 parent = path->dentry;
2515 this.name = "pts";
2516 this.len = 3;
2517 child = d_hash_and_lookup(parent, &this);
2518 if (!child)
2519 return -ENOENT;
2520
2521 path->dentry = child;
2522 dput(parent);
2523 follow_mount(path);
2524 return 0;
2525}
2526#endif
2527
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002528int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2529 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Al Viroabc9f5b2015-05-12 16:53:42 -04002531 return filename_lookup(dfd, getname_flags(name, flags, empty),
2532 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533}
Al Virob853a162015-05-13 09:12:02 -04002534EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002535
Jeff Layton80334262013-07-26 06:23:25 -04002536/**
Al Viro197df042013-09-08 14:03:27 -04002537 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002538 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
Jeff Layton80334262013-07-26 06:23:25 -04002539 *
2540 * This is a special lookup_last function just for umount. In this case, we
2541 * need to resolve the path without doing any revalidation.
2542 *
2543 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2544 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2545 * in almost all cases, this lookup will be served out of the dcache. The only
2546 * cases where it won't are if nd->last refers to a symlink or the path is
2547 * bogus and it doesn't exist.
2548 *
2549 * Returns:
2550 * -error: if there was an error during lookup. This includes -ENOENT if the
Al Viroba8f4612016-11-14 00:40:33 -05002551 * lookup found a negative dentry.
Jeff Layton80334262013-07-26 06:23:25 -04002552 *
Al Viroba8f4612016-11-14 00:40:33 -05002553 * 0: if we successfully resolved nd->last and found it to not to be a
2554 * symlink that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002555 *
2556 * 1: if we successfully resolved nd->last and found it to be a symlink
Al Viroba8f4612016-11-14 00:40:33 -05002557 * that needs to be followed.
Jeff Layton80334262013-07-26 06:23:25 -04002558 */
2559static int
Al Viroba8f4612016-11-14 00:40:33 -05002560mountpoint_last(struct nameidata *nd)
Jeff Layton80334262013-07-26 06:23:25 -04002561{
2562 int error = 0;
Jeff Layton80334262013-07-26 06:23:25 -04002563 struct dentry *dir = nd->path.dentry;
Al Viroba8f4612016-11-14 00:40:33 -05002564 struct path path;
Jeff Layton80334262013-07-26 06:23:25 -04002565
Al Viro35759522013-09-08 13:41:33 -04002566 /* If we're in rcuwalk, drop out of it to handle last component */
2567 if (nd->flags & LOOKUP_RCU) {
Al Viro6e9918b2015-05-05 09:26:05 -04002568 if (unlazy_walk(nd, NULL, 0))
Al Virodeb106c2015-05-08 18:05:21 -04002569 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002570 }
2571
2572 nd->flags &= ~LOOKUP_PARENT;
2573
2574 if (unlikely(nd->last_type != LAST_NORM)) {
2575 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002576 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002577 return error;
Al Viroba8f4612016-11-14 00:40:33 -05002578 path.dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002579 } else {
Al Viroba8f4612016-11-14 00:40:33 -05002580 path.dentry = d_lookup(dir, &nd->last);
2581 if (!path.dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002582 /*
2583 * No cached dentry. Mounted dentries are pinned in the
2584 * cache, so that means that this dentry is probably
2585 * a symlink or the path doesn't actually point
2586 * to a mounted dentry.
2587 */
Al Viroba8f4612016-11-14 00:40:33 -05002588 path.dentry = lookup_slow(&nd->last, dir,
Al Viro949a8522016-03-06 14:20:52 -05002589 nd->flags | LOOKUP_NO_REVAL);
Al Viroba8f4612016-11-14 00:40:33 -05002590 if (IS_ERR(path.dentry))
2591 return PTR_ERR(path.dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002592 }
Jeff Layton80334262013-07-26 06:23:25 -04002593 }
Al Viroba8f4612016-11-14 00:40:33 -05002594 if (d_is_negative(path.dentry)) {
2595 dput(path.dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002596 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002597 }
Al Viroba8f4612016-11-14 00:40:33 -05002598 path.mnt = nd->path.mnt;
Al Viro8f64fb12016-11-14 01:50:26 -05002599 return step_into(nd, &path, 0, d_backing_inode(path.dentry), 0);
Jeff Layton80334262013-07-26 06:23:25 -04002600}
2601
2602/**
Al Viro197df042013-09-08 14:03:27 -04002603 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b8572015-09-09 15:39:23 -07002604 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002605 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002606 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002607 *
2608 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002609 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002610 */
2611static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002612path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002613{
Al Viroc8a53ee2015-05-12 18:43:07 -04002614 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002615 int err;
2616 if (IS_ERR(s))
2617 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002618 while (!(err = link_path_walk(s, nd)) &&
Al Viroba8f4612016-11-14 00:40:33 -05002619 (err = mountpoint_last(nd)) > 0) {
Al Viro3bdba282015-05-08 17:37:07 -04002620 s = trailing_symlink(nd);
2621 if (IS_ERR(s)) {
2622 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002623 break;
Al Viro3bdba282015-05-08 17:37:07 -04002624 }
Jeff Layton80334262013-07-26 06:23:25 -04002625 }
Al Viroba8f4612016-11-14 00:40:33 -05002626 if (!err) {
2627 *path = nd->path;
2628 nd->path.mnt = NULL;
2629 nd->path.dentry = NULL;
2630 follow_mount(path);
2631 }
Al Virodeb106c2015-05-08 18:05:21 -04002632 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002633 return err;
2634}
2635
Al Viro2d864652013-09-08 20:18:44 -04002636static int
Al Viro668696d2015-02-22 19:44:00 -05002637filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002638 unsigned int flags)
2639{
Al Viro9883d182015-05-13 07:28:08 -04002640 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002641 int error;
Al Viro668696d2015-02-22 19:44:00 -05002642 if (IS_ERR(name))
2643 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002644 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002645 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002646 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002647 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002648 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002649 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002650 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002651 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002652 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002653 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002654 return error;
2655}
2656
Jeff Layton80334262013-07-26 06:23:25 -04002657/**
Al Viro197df042013-09-08 14:03:27 -04002658 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002659 * @dfd: directory file descriptor
2660 * @name: pathname from userland
2661 * @flags: lookup flags
2662 * @path: pointer to container to hold result
2663 *
2664 * A umount is a special case for path walking. We're not actually interested
2665 * in the inode in this situation, and ESTALE errors can be a problem. We
2666 * simply want track down the dentry and vfsmount attached at the mountpoint
2667 * and avoid revalidating the last component.
2668 *
2669 * Returns 0 and populates "path" on success.
2670 */
2671int
Al Viro197df042013-09-08 14:03:27 -04002672user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002673 struct path *path)
2674{
Al Virocbaab2d2015-01-22 02:49:00 -05002675 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002676}
2677
Al Viro2d864652013-09-08 20:18:44 -04002678int
2679kern_path_mountpoint(int dfd, const char *name, struct path *path,
2680 unsigned int flags)
2681{
Al Virocbaab2d2015-01-22 02:49:00 -05002682 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002683}
2684EXPORT_SYMBOL(kern_path_mountpoint);
2685
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002686int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002688 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002689
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002690 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002692 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002694 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002696EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698/*
2699 * Check whether we can remove a link victim from directory dir, check
2700 * whether the type of victim is right.
2701 * 1. We can't do it if dir is read-only (done in permission())
2702 * 2. We should have write and exec permissions on dir
2703 * 3. We can't remove anything from append-only dir
2704 * 4. We can't do anything with immutable dir (done in permission())
2705 * 5. If the sticky bit on dir is set we should either
2706 * a. be owner of dir, or
2707 * b. be owner of victim, or
2708 * c. have CAP_FOWNER capability
2709 * 6. If the victim is append-only or immutable we can't do antyhing with
2710 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002711 * 7. If the victim has an unknown uid or gid we can't change the inode.
2712 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2713 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2714 * 10. We can't remove a root or mountpoint.
2715 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 * nfs_async_unlink().
2717 */
David Howellsb18825a2013-09-12 19:22:53 +01002718static int may_delete(struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719{
David Howells63afdfc2015-05-06 15:59:00 +01002720 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 int error;
2722
David Howellsb18825a2013-09-12 19:22:53 +01002723 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002725 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
2727 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002728 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Al Virof419a2e2008-07-22 00:07:17 -04002730 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 if (error)
2732 return error;
2733 if (IS_APPEND(dir))
2734 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002735
2736 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002737 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return -EPERM;
2739 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002740 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 return -ENOTDIR;
2742 if (IS_ROOT(victim))
2743 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002744 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 return -EISDIR;
2746 if (IS_DEADDIR(dir))
2747 return -ENOENT;
2748 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2749 return -EBUSY;
2750 return 0;
2751}
2752
2753/* Check whether we can create an object with dentry child in directory
2754 * dir.
2755 * 1. We can't do it if child already exists (open has special treatment for
2756 * this case, but since we are inlined it's OK)
2757 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002758 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2759 * 4. We should have write and exec permissions on dir
2760 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 */
Miklos Szeredia95164d2008-07-30 15:08:48 +02002762static inline int may_create(struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002764 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002765 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (child->d_inode)
2767 return -EEXIST;
2768 if (IS_DEADDIR(dir))
2769 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002770 s_user_ns = dir->i_sb->s_user_ns;
2771 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2772 !kgid_has_mapping(s_user_ns, current_fsgid()))
2773 return -EOVERFLOW;
Al Virof419a2e2008-07-22 00:07:17 -04002774 return inode_permission(dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775}
2776
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777/*
2778 * p1 and p2 should be directories on the same fs.
2779 */
2780struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2781{
2782 struct dentry *p;
2783
2784 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002785 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return NULL;
2787 }
2788
Al Virofc640052016-04-10 01:33:30 -04002789 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002791 p = d_ancestor(p2, p1);
2792 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002793 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2794 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002795 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
2797
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002798 p = d_ancestor(p1, p2);
2799 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002800 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2801 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002802 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 }
2804
Al Viro59551022016-01-22 15:40:57 -05002805 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2806 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 return NULL;
2808}
Al Viro4d359502014-03-14 12:20:17 -04002809EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811void unlock_rename(struct dentry *p1, struct dentry *p2)
2812{
Al Viro59551022016-01-22 15:40:57 -05002813 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002815 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002816 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 }
2818}
Al Viro4d359502014-03-14 12:20:17 -04002819EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
Al Viro4acdaf22011-07-26 01:42:34 -04002821int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
Al Viro312b63f2012-06-10 18:09:36 -04002822 bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
Miklos Szeredia95164d2008-07-30 15:08:48 +02002824 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (error)
2826 return error;
2827
Al Viroacfa4382008-12-04 10:06:33 -05002828 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 return -EACCES; /* shouldn't it be ENOSYS? */
2830 mode &= S_IALLUGO;
2831 mode |= S_IFREG;
2832 error = security_inode_create(dir, dentry, mode);
2833 if (error)
2834 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002835 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002836 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002837 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return error;
2839}
Al Viro4d359502014-03-14 12:20:17 -04002840EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002842bool may_open_dev(const struct path *path)
2843{
2844 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2845 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2846}
2847
Al Virof0bb5aa2016-11-20 20:27:12 -05002848static int may_open(const struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002850 struct dentry *dentry = path->dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 struct inode *inode = dentry->d_inode;
2852 int error;
2853
2854 if (!inode)
2855 return -ENOENT;
2856
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002857 switch (inode->i_mode & S_IFMT) {
2858 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002860 case S_IFDIR:
2861 if (acc_mode & MAY_WRITE)
2862 return -EISDIR;
2863 break;
2864 case S_IFBLK:
2865 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002866 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002868 /*FALLTHRU*/
2869 case S_IFIFO:
2870 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002872 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002873 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002874
Al Viro62fb4a12015-12-26 22:33:24 -05002875 error = inode_permission(inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002876 if (error)
2877 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 /*
2880 * An append-only file must be opened in append mode for writing.
2881 */
2882 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002883 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002884 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002886 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888
2889 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002890 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002891 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002893 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
Jeff Laytone1181ee2010-12-07 16:19:50 -05002896static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002897{
Al Virof0bb5aa2016-11-20 20:27:12 -05002898 const struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002899 struct inode *inode = path->dentry->d_inode;
2900 int error = get_write_access(inode);
2901 if (error)
2902 return error;
2903 /*
2904 * Refuse to truncate files with mandatory locks held on them.
2905 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002906 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002907 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002908 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002909 if (!error) {
2910 error = do_truncate(path->dentry, 0,
2911 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002912 filp);
Al Viro7715b522009-12-16 03:54:00 -05002913 }
2914 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002915 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916}
2917
Dave Hansend57999e2008-02-15 14:37:27 -08002918static inline int open_to_namei_flags(int flag)
2919{
Al Viro8a5e9292011-06-25 19:15:54 -04002920 if ((flag & O_ACCMODE) == 3)
2921 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08002922 return flag;
2923}
2924
Al Virod3607752016-03-25 15:21:09 -04002925static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002926{
2927 int error = security_path_mknod(dir, dentry, mode, 0);
2928 if (error)
2929 return error;
2930
2931 error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
2932 if (error)
2933 return error;
2934
2935 return security_inode_create(dir->dentry->d_inode, dentry, mode);
2936}
2937
David Howells1acf0af2012-06-14 16:13:46 +01002938/*
2939 * Attempt to atomically look up, create and open a file from a negative
2940 * dentry.
2941 *
2942 * Returns 0 if successful. The file will have been created and attached to
2943 * @file by the filesystem calling finish_open().
2944 *
2945 * Returns 1 if the file was looked up only or didn't need creating. The
2946 * caller will need to perform the open themselves. @path will have been
2947 * updated to point to the new dentry. This may be negative.
2948 *
2949 * Returns an error code otherwise.
2950 */
Al Viro2675a4e2012-06-22 12:41:10 +04002951static int atomic_open(struct nameidata *nd, struct dentry *dentry,
2952 struct path *path, struct file *file,
2953 const struct open_flags *op,
Al Viro1643b432016-04-27 19:14:10 -04002954 int open_flag, umode_t mode,
Al Viro2675a4e2012-06-22 12:41:10 +04002955 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02002956{
Miklos Szeredid18e9002012-06-05 15:10:17 +02002957 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002958 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002959 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002960
Al Viro384f26e2016-04-28 02:03:55 -04002961 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02002962 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02002963
Miklos Szeredid18e9002012-06-05 15:10:17 +02002964 if (nd->flags & LOOKUP_DIRECTORY)
2965 open_flag |= O_DIRECTORY;
2966
Al Viro30d90492012-06-22 12:40:19 +04002967 file->f_path.dentry = DENTRY_NOT_SET;
2968 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04002969 error = dir->i_op->atomic_open(dir, dentry, file,
2970 open_to_namei_flags(open_flag),
2971 mode, opened);
Al Viro6fbd0712016-04-28 11:50:59 -04002972 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04002973 if (!error) {
2974 /*
2975 * We didn't have the inode before the open, so check open
2976 * permission here.
2977 */
2978 int acc_mode = op->acc_mode;
2979 if (*opened & FILE_CREATED) {
2980 WARN_ON(!(open_flag & O_CREAT));
2981 fsnotify_create(dir, dentry);
2982 acc_mode = 0;
2983 }
2984 error = may_open(&file->f_path, acc_mode, open_flag);
2985 if (WARN_ON(error > 0))
2986 error = -EINVAL;
2987 } else if (error > 0) {
Al Viro30d90492012-06-22 12:40:19 +04002988 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04002989 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04002990 } else {
Al Viro384f26e2016-04-28 02:03:55 -04002991 if (file->f_path.dentry) {
2992 dput(dentry);
2993 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04002994 }
Al Viro384f26e2016-04-28 02:03:55 -04002995 if (*opened & FILE_CREATED)
2996 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04002997 if (unlikely(d_is_negative(dentry))) {
2998 error = -ENOENT;
2999 } else {
3000 path->dentry = dentry;
3001 path->mnt = nd->path.mnt;
3002 return 1;
3003 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003004 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003005 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003006 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003007 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003008}
3009
Nick Piggin31e6b012011-01-07 17:49:52 +11003010/*
David Howells1acf0af2012-06-14 16:13:46 +01003011 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003012 *
3013 * Must be called with i_mutex held on parent.
3014 *
David Howells1acf0af2012-06-14 16:13:46 +01003015 * Returns 0 if the file was successfully atomically created (if necessary) and
3016 * opened. In this case the file will be returned attached to @file.
3017 *
3018 * Returns 1 if the file was not completely opened at this time, though lookups
3019 * and creations will have been performed and the dentry returned in @path will
3020 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3021 * specified then a negative dentry may be returned.
3022 *
3023 * An error code is returned otherwise.
3024 *
3025 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3026 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003027 */
Al Viro2675a4e2012-06-22 12:41:10 +04003028static int lookup_open(struct nameidata *nd, struct path *path,
3029 struct file *file,
3030 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04003031 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003032{
3033 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003034 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003035 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003036 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003037 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003038 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003039 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003040
Al Viroce8644f2016-04-26 14:17:56 -04003041 if (unlikely(IS_DEADDIR(dir_inode)))
3042 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003043
Al Viro47237682012-06-10 05:01:45 -04003044 *opened &= ~FILE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003045 dentry = d_lookup(dir, &nd->last);
3046 for (;;) {
3047 if (!dentry) {
3048 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3049 if (IS_ERR(dentry))
3050 return PTR_ERR(dentry);
3051 }
3052 if (d_in_lookup(dentry))
3053 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003054
Al Viro6fbd0712016-04-28 11:50:59 -04003055 error = d_revalidate(dentry, nd->flags);
3056 if (likely(error > 0))
3057 break;
3058 if (error)
3059 goto out_dput;
3060 d_invalidate(dentry);
3061 dput(dentry);
3062 dentry = NULL;
3063 }
3064 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003065 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003066 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003067 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003068
Al Viro1643b432016-04-27 19:14:10 -04003069 /*
3070 * Checking write permission is tricky, bacuse we don't know if we are
3071 * going to actually need it: O_CREAT opens should work as long as the
3072 * file exists. But checking existence breaks atomicity. The trick is
3073 * to check access and if not granted clear O_CREAT from the flags.
3074 *
3075 * Another problem is returing the "right" error value (e.g. for an
3076 * O_EXCL open we want to return EEXIST not EROFS).
3077 */
3078 if (open_flag & O_CREAT) {
3079 if (!IS_POSIXACL(dir->d_inode))
3080 mode &= ~current_umask();
3081 if (unlikely(!got_write)) {
3082 create_error = -EROFS;
3083 open_flag &= ~O_CREAT;
3084 if (open_flag & (O_EXCL | O_TRUNC))
3085 goto no_open;
3086 /* No side effects, safe to clear O_CREAT */
3087 } else {
3088 create_error = may_o_create(&nd->path, dentry, mode);
3089 if (create_error) {
3090 open_flag &= ~O_CREAT;
3091 if (open_flag & O_EXCL)
3092 goto no_open;
3093 }
3094 }
3095 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3096 unlikely(!got_write)) {
3097 /*
3098 * No O_CREATE -> atomicity not a requirement -> fall
3099 * back to lookup + open
3100 */
3101 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003102 }
3103
Al Viro1643b432016-04-27 19:14:10 -04003104 if (dir_inode->i_op->atomic_open) {
3105 error = atomic_open(nd, dentry, path, file, op, open_flag,
3106 mode, opened);
3107 if (unlikely(error == -ENOENT) && create_error)
3108 error = create_error;
3109 return error;
3110 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003111
Al Viro1643b432016-04-27 19:14:10 -04003112no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003113 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003114 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3115 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003116 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003117 if (unlikely(res)) {
3118 if (IS_ERR(res)) {
3119 error = PTR_ERR(res);
3120 goto out_dput;
3121 }
3122 dput(dentry);
3123 dentry = res;
3124 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003125 }
3126
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003127 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003128 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro47237682012-06-10 05:01:45 -04003129 *opened |= FILE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003130 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003131 if (!dir_inode->i_op->create) {
3132 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003133 goto out_dput;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003134 }
Al Viroce8644f2016-04-26 14:17:56 -04003135 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003136 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003137 if (error)
3138 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003139 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003140 }
Al Viro1643b432016-04-27 19:14:10 -04003141 if (unlikely(create_error) && !dentry->d_inode) {
3142 error = create_error;
3143 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003144 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003145out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003146 path->dentry = dentry;
3147 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04003148 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003149
3150out_dput:
3151 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003152 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003153}
3154
3155/*
Al Virofe2d35f2011-03-05 22:58:25 -05003156 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003157 */
Al Viro896475d2015-04-22 18:02:17 -04003158static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04003159 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04003160 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05003161{
Al Viroa1e28032009-12-24 02:12:06 -05003162 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003163 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003164 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003165 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003166 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003167 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003168 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003169 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003170 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003171
Al Viroc3e380b2011-02-23 13:39:45 -05003172 nd->flags &= ~LOOKUP_PARENT;
3173 nd->flags |= op->intent;
3174
Al Virobc77daa2013-06-06 09:12:33 -04003175 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003176 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003177 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003178 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003179 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003180 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003181
Al Viroca344a892011-03-09 00:36:45 -05003182 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003183 if (nd->last.name[nd->last.len])
3184 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3185 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003186 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003187 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003188 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003189
Miklos Szeredi71574862012-06-05 15:10:14 +02003190 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003191 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003192
Miklos Szeredi71574862012-06-05 15:10:14 +02003193 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003194 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003195 } else {
3196 /* create side of things */
3197 /*
3198 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3199 * has been cleared when we got to the last component we are
3200 * about to look up
3201 */
3202 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003203 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003204 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003205
Al Viro76ae2a52015-05-12 18:44:32 -04003206 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003207 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003208 if (unlikely(nd->last.name[nd->last.len]))
3209 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003210 }
3211
Al Viro9cf843e2016-04-28 19:35:16 -04003212 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003213 error = mnt_want_write(nd->path.mnt);
3214 if (!error)
3215 got_write = true;
3216 /*
3217 * do _not_ fail yet - we might not need that or fail with
3218 * a different error; let lookup_open() decide; we'll be
3219 * dropping this one anyway.
3220 */
3221 }
Al Viro9cf843e2016-04-28 19:35:16 -04003222 if (open_flag & O_CREAT)
3223 inode_lock(dir->d_inode);
3224 else
3225 inode_lock_shared(dir->d_inode);
Al Viro896475d2015-04-22 18:02:17 -04003226 error = lookup_open(nd, &path, file, op, got_write, opened);
Al Viro9cf843e2016-04-28 19:35:16 -04003227 if (open_flag & O_CREAT)
3228 inode_unlock(dir->d_inode);
3229 else
3230 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003231
Al Viro2675a4e2012-06-22 12:41:10 +04003232 if (error <= 0) {
3233 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003234 goto out;
3235
Al Viro47237682012-06-10 05:01:45 -04003236 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003237 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003238 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003239
Al Viro76ae2a52015-05-12 18:44:32 -04003240 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003241 goto opened;
3242 }
Al Virofb1cc552009-12-24 01:58:28 -05003243
Al Viro47237682012-06-10 05:01:45 -04003244 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003245 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003246 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003247 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003248 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003249 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003250 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003251 }
3252
3253 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003254 * If atomic_open() acquired write access it is dropped now due to
3255 * possible mount and symlink following (this might be optimized away if
3256 * necessary...)
3257 */
Al Viro64894cf2012-07-31 00:53:35 +04003258 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003259 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003260 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003261 }
3262
Al Viroe6ec03a2016-06-05 00:23:09 -04003263 error = follow_managed(&path, nd);
3264 if (unlikely(error < 0))
3265 return error;
3266
Al Viro6583fe22016-03-05 18:14:03 -05003267 if (unlikely(d_is_negative(path.dentry))) {
3268 path_to_nameidata(&path, nd);
3269 return -ENOENT;
3270 }
3271
3272 /*
3273 * create/update audit record if it already exists.
3274 */
3275 audit_inode(nd->name, path.dentry, 0);
3276
Al Virodeb106c2015-05-08 18:05:21 -04003277 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3278 path_to_nameidata(&path, nd);
3279 return -EEXIST;
3280 }
Al Virofb1cc552009-12-24 01:58:28 -05003281
Al Viro254cf582015-05-05 09:40:46 -04003282 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003283 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003284finish_lookup:
Al Viro8f64fb12016-11-14 01:50:26 -05003285 error = step_into(nd, &path, 0, inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003286 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003287 return error;
Al Virobc77daa2013-06-06 09:12:33 -04003288finish_open:
Al Viro8f64fb12016-11-14 01:50:26 -05003289 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Viroa3fbbde2011-11-07 21:21:26 +00003290 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003291 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003292 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003293 audit_inode(nd->name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003294 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003295 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003296 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003297 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003298 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003299 goto out;
David Howells4bbcbd3b2015-03-17 22:16:40 +00003300 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003301 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003302
Al Viro0f9d1a12011-03-09 00:13:14 -05003303 if (will_truncate) {
3304 error = mnt_want_write(nd->path.mnt);
3305 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003306 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003307 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003308 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003309finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003310 error = may_open(&nd->path, acc_mode, open_flag);
3311 if (error)
3312 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003313 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3314 error = vfs_open(&nd->path, file, current_cred());
Al Virofac7d192016-06-04 11:41:49 -04003315 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003316 goto out;
Al Virofac7d192016-06-04 11:41:49 -04003317 *opened |= FILE_OPENED;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003318opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003319 error = open_check_o_direct(file);
Al Virofe9ec822016-04-27 03:14:20 -04003320 if (!error)
3321 error = ima_file_check(file, op->acc_mode, *opened);
3322 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003323 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003324out:
Al Virofe9ec822016-04-27 03:14:20 -04003325 if (unlikely(error) && (*opened & FILE_OPENED))
3326 fput(file);
Al Viroc80567c2016-02-27 19:17:33 -05003327 if (unlikely(error > 0)) {
3328 WARN_ON(1);
3329 error = -EINVAL;
3330 }
Al Viro64894cf2012-07-31 00:53:35 +04003331 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003332 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003333 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003334}
3335
Al Viroc8a53ee2015-05-12 18:43:07 -04003336static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003337 const struct open_flags *op,
3338 struct file *file, int *opened)
3339{
3340 static const struct qstr name = QSTR_INIT("/", 1);
Al Viro625b6d12015-05-12 16:36:12 -04003341 struct dentry *child;
Al Viro60545d02013-06-07 01:20:27 -04003342 struct inode *dir;
Al Viro625b6d12015-05-12 16:36:12 -04003343 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003344 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003345 if (unlikely(error))
3346 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003347 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003348 if (unlikely(error))
3349 goto out;
Al Viro625b6d12015-05-12 16:36:12 -04003350 dir = path.dentry->d_inode;
Al Viro60545d02013-06-07 01:20:27 -04003351 /* we want directory to be writable */
Al Viro625b6d12015-05-12 16:36:12 -04003352 error = inode_permission(dir, MAY_WRITE | MAY_EXEC);
Al Viro60545d02013-06-07 01:20:27 -04003353 if (error)
3354 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003355 if (!dir->i_op->tmpfile) {
3356 error = -EOPNOTSUPP;
3357 goto out2;
3358 }
Al Viro625b6d12015-05-12 16:36:12 -04003359 child = d_alloc(path.dentry, &name);
Al Viro60545d02013-06-07 01:20:27 -04003360 if (unlikely(!child)) {
3361 error = -ENOMEM;
3362 goto out2;
3363 }
Al Viro625b6d12015-05-12 16:36:12 -04003364 dput(path.dentry);
3365 path.dentry = child;
3366 error = dir->i_op->tmpfile(dir, child, op->mode);
Al Viro60545d02013-06-07 01:20:27 -04003367 if (error)
3368 goto out2;
Al Viroc8a53ee2015-05-12 18:43:07 -04003369 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003370 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003371 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003372 if (error)
3373 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003374 file->f_path.mnt = path.mnt;
3375 error = finish_open(file, child, NULL, opened);
Al Viro60545d02013-06-07 01:20:27 -04003376 if (error)
3377 goto out2;
3378 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003379 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003380 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003381 } else if (!(op->open_flag & O_EXCL)) {
3382 struct inode *inode = file_inode(file);
3383 spin_lock(&inode->i_lock);
3384 inode->i_state |= I_LINKABLE;
3385 spin_unlock(&inode->i_lock);
3386 }
Al Viro60545d02013-06-07 01:20:27 -04003387out2:
Al Viro625b6d12015-05-12 16:36:12 -04003388 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003389out:
Al Viro625b6d12015-05-12 16:36:12 -04003390 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003391 return error;
3392}
3393
Al Viro6ac08702016-04-26 00:02:50 -04003394static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3395{
3396 struct path path;
3397 int error = path_lookupat(nd, flags, &path);
3398 if (!error) {
3399 audit_inode(nd->name, path.dentry, 0);
3400 error = vfs_open(&path, file, current_cred());
3401 path_put(&path);
3402 }
3403 return error;
3404}
3405
Al Viroc8a53ee2015-05-12 18:43:07 -04003406static struct file *path_openat(struct nameidata *nd,
3407 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
Al Viro368ee9b2015-05-08 17:19:59 -04003409 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003410 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003411 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003412 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003413
Al Viro30d90492012-06-22 12:40:19 +04003414 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003415 if (IS_ERR(file))
3416 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003417
Al Viro30d90492012-06-22 12:40:19 +04003418 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003419
Al Virobb458c62013-07-13 13:26:37 +04003420 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003421 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003422 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003423 }
3424
Al Viro6ac08702016-04-26 00:02:50 -04003425 if (unlikely(file->f_flags & O_PATH)) {
3426 error = do_o_path(nd, flags, file);
3427 if (!error)
3428 opened |= FILE_OPENED;
3429 goto out2;
3430 }
3431
Al Viroc8a53ee2015-05-12 18:43:07 -04003432 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003433 if (IS_ERR(s)) {
3434 put_filp(file);
3435 return ERR_CAST(s);
3436 }
Al Viro3bdba282015-05-08 17:37:07 -04003437 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003438 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003439 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003440 s = trailing_symlink(nd);
3441 if (IS_ERR(s)) {
3442 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003443 break;
Al Viro3bdba282015-05-08 17:37:07 -04003444 }
Al Viro806b6812009-12-26 07:16:40 -05003445 }
Al Virodeb106c2015-05-08 18:05:21 -04003446 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003447out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003448 if (!(opened & FILE_OPENED)) {
3449 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003450 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003451 }
Al Viro2675a4e2012-06-22 12:41:10 +04003452 if (unlikely(error)) {
3453 if (error == -EOPENSTALE) {
3454 if (flags & LOOKUP_RCU)
3455 error = -ECHILD;
3456 else
3457 error = -ESTALE;
3458 }
3459 file = ERR_PTR(error);
3460 }
3461 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462}
3463
Jeff Layton669abf42012-10-10 16:43:10 -04003464struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003465 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003466{
Al Viro9883d182015-05-13 07:28:08 -04003467 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003468 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003469 struct file *filp;
3470
Al Viro9883d182015-05-13 07:28:08 -04003471 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003472 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003473 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003474 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003475 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003476 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003477 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003478 return filp;
3479}
3480
Al Viro73d049a2011-03-11 12:08:24 -05003481struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003482 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003483{
Al Viro9883d182015-05-13 07:28:08 -04003484 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003485 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003486 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003487 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003488
3489 nd.root.mnt = mnt;
3490 nd.root.dentry = dentry;
3491
David Howellsb18825a2013-09-12 19:22:53 +01003492 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003493 return ERR_PTR(-ELOOP);
3494
Paul Moore51689102015-01-22 00:00:03 -05003495 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303496 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003497 return ERR_CAST(filename);
3498
Al Viro9883d182015-05-13 07:28:08 -04003499 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003500 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003501 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003502 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003503 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003504 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003505 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003506 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003507 return file;
3508}
3509
Al Virofa14a0b2015-01-22 02:16:49 -05003510static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003511 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003513 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003514 struct qstr last;
3515 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003516 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003517 int error;
3518 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3519
3520 /*
3521 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3522 * other flags passed in are ignored!
3523 */
3524 lookup_flags &= LOOKUP_REVAL;
3525
Al Viro5c31b6c2015-05-12 17:32:54 -04003526 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3527 if (IS_ERR(name))
3528 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003529
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003530 /*
3531 * Yucky last component or no last component at all?
3532 * (foo/., foo/.., /////)
3533 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003534 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003535 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003536
Jan Karac30dabf2012-06-12 16:20:30 +02003537 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003538 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003539 /*
3540 * Do the final lookup.
3541 */
Al Viro391172c2015-05-09 11:19:16 -04003542 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003543 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003544 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003546 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003547
Al Viroa8104a92012-07-20 02:25:00 +04003548 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003549 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003550 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003551
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003552 /*
3553 * Special case - lookup gave negative, but... we had foo/bar/
3554 * From the vfs_mknod() POV we just have a negative dentry -
3555 * all is fine. Let's be bastards - you had / on the end, you've
3556 * been asking for (non-existent) directory. -ENOENT for you.
3557 */
Al Viro391172c2015-05-09 11:19:16 -04003558 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003559 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003560 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003561 }
Jan Karac30dabf2012-06-12 16:20:30 +02003562 if (unlikely(err2)) {
3563 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003564 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003565 }
Al Viro181c37b2015-05-12 17:21:25 -04003566 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568fail:
Al Viroa8104a92012-07-20 02:25:00 +04003569 dput(dentry);
3570 dentry = ERR_PTR(error);
3571unlock:
Al Viro59551022016-01-22 15:40:57 -05003572 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003573 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003574 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003575out:
Al Viro391172c2015-05-09 11:19:16 -04003576 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003577 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 return dentry;
3579}
Al Virofa14a0b2015-01-22 02:16:49 -05003580
3581struct dentry *kern_path_create(int dfd, const char *pathname,
3582 struct path *path, unsigned int lookup_flags)
3583{
Al Viro181c37b2015-05-12 17:21:25 -04003584 return filename_create(dfd, getname_kernel(pathname),
3585 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003586}
Al Virodae6ad82011-06-26 11:50:15 -04003587EXPORT_SYMBOL(kern_path_create);
3588
Al Viro921a1652012-07-20 01:15:31 +04003589void done_path_create(struct path *path, struct dentry *dentry)
3590{
3591 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003592 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003593 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003594 path_put(path);
3595}
3596EXPORT_SYMBOL(done_path_create);
3597
Al Viro520ae682015-05-13 07:00:28 -04003598inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003599 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003600{
Al Viro181c37b2015-05-12 17:21:25 -04003601 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003602}
3603EXPORT_SYMBOL(user_path_create);
3604
Al Viro1a67aaf2011-07-26 01:52:52 -04003605int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003607 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
3609 if (error)
3610 return error;
3611
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003612 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 return -EPERM;
3614
Al Viroacfa4382008-12-04 10:06:33 -05003615 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 return -EPERM;
3617
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003618 error = devcgroup_inode_mknod(mode, dev);
3619 if (error)
3620 return error;
3621
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 error = security_inode_mknod(dir, dentry, mode, dev);
3623 if (error)
3624 return error;
3625
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003627 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003628 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 return error;
3630}
Al Viro4d359502014-03-14 12:20:17 -04003631EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Al Virof69aac02011-07-26 04:31:40 -04003633static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003634{
3635 switch (mode & S_IFMT) {
3636 case S_IFREG:
3637 case S_IFCHR:
3638 case S_IFBLK:
3639 case S_IFIFO:
3640 case S_IFSOCK:
3641 case 0: /* zero mode translates to S_IFREG */
3642 return 0;
3643 case S_IFDIR:
3644 return -EPERM;
3645 default:
3646 return -EINVAL;
3647 }
3648}
3649
Al Viro8208a222011-07-25 17:32:17 -04003650SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003651 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Al Viro2ad94ae2008-07-21 09:32:51 -04003653 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003654 struct path path;
3655 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003656 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
Al Viro8e4bfca2012-07-20 01:17:26 +04003658 error = may_mknod(mode);
3659 if (error)
3660 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003661retry:
3662 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003663 if (IS_ERR(dentry))
3664 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003665
Al Virodae6ad82011-06-26 11:50:15 -04003666 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003667 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003668 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003669 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003670 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003671 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 case 0: case S_IFREG:
Al Viro312b63f2012-06-10 18:09:36 -04003673 error = vfs_create(path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003674 if (!error)
3675 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 break;
3677 case S_IFCHR: case S_IFBLK:
Al Virodae6ad82011-06-26 11:50:15 -04003678 error = vfs_mknod(path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 new_decode_dev(dev));
3680 break;
3681 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003682 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684 }
Al Viroa8104a92012-07-20 02:25:00 +04003685out:
Al Viro921a1652012-07-20 01:15:31 +04003686 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003687 if (retry_estale(error, lookup_flags)) {
3688 lookup_flags |= LOOKUP_REVAL;
3689 goto retry;
3690 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 return error;
3692}
3693
Al Viro8208a222011-07-25 17:32:17 -04003694SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003695{
3696 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3697}
3698
Al Viro18bb1db2011-07-26 01:41:39 -04003699int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700{
Miklos Szeredia95164d2008-07-30 15:08:48 +02003701 int error = may_create(dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003702 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703
3704 if (error)
3705 return error;
3706
Al Viroacfa4382008-12-04 10:06:33 -05003707 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 return -EPERM;
3709
3710 mode &= (S_IRWXUGO|S_ISVTX);
3711 error = security_inode_mkdir(dir, dentry, mode);
3712 if (error)
3713 return error;
3714
Al Viro8de52772012-02-06 12:45:27 -05003715 if (max_links && dir->i_nlink >= max_links)
3716 return -EMLINK;
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003719 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003720 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return error;
3722}
Al Viro4d359502014-03-14 12:20:17 -04003723EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724
Al Viroa218d0f2011-11-21 14:59:34 -05003725SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726{
Dave Hansen6902d922006-09-30 23:29:01 -07003727 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003728 struct path path;
3729 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003730 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003732retry:
3733 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003734 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003735 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003736
Al Virodae6ad82011-06-26 11:50:15 -04003737 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003738 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003739 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003740 if (!error)
3741 error = vfs_mkdir(path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003742 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003743 if (retry_estale(error, lookup_flags)) {
3744 lookup_flags |= LOOKUP_REVAL;
3745 goto retry;
3746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 return error;
3748}
3749
Al Viroa218d0f2011-11-21 14:59:34 -05003750SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003751{
3752 return sys_mkdirat(AT_FDCWD, pathname, mode);
3753}
3754
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3756{
3757 int error = may_delete(dir, dentry, 1);
3758
3759 if (error)
3760 return error;
3761
Al Viroacfa4382008-12-04 10:06:33 -05003762 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 return -EPERM;
3764
Al Viro1d2ef592011-09-14 18:55:41 +01003765 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003766 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Sage Weil912dbc12011-05-24 13:06:11 -07003768 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003769 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003770 goto out;
3771
3772 error = security_inode_rmdir(dir, dentry);
3773 if (error)
3774 goto out;
3775
Sage Weil3cebde22011-05-29 21:20:59 -07003776 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003777 error = dir->i_op->rmdir(dir, dentry);
3778 if (error)
3779 goto out;
3780
3781 dentry->d_inode->i_flags |= S_DEAD;
3782 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003783 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003784
3785out:
Al Viro59551022016-01-22 15:40:57 -05003786 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003787 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003788 if (!error)
3789 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 return error;
3791}
Al Viro4d359502014-03-14 12:20:17 -04003792EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003794static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795{
3796 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003797 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003799 struct path path;
3800 struct qstr last;
3801 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003802 unsigned int lookup_flags = 0;
3803retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04003804 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3805 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003806 if (IS_ERR(name))
3807 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808
Al Virof5beed72015-04-30 16:09:11 -04003809 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003810 case LAST_DOTDOT:
3811 error = -ENOTEMPTY;
3812 goto exit1;
3813 case LAST_DOT:
3814 error = -EINVAL;
3815 goto exit1;
3816 case LAST_ROOT:
3817 error = -EBUSY;
3818 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003820
Al Virof5beed72015-04-30 16:09:11 -04003821 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003822 if (error)
3823 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003824
Al Viro59551022016-01-22 15:40:57 -05003825 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003826 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003828 if (IS_ERR(dentry))
3829 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003830 if (!dentry->d_inode) {
3831 error = -ENOENT;
3832 goto exit3;
3833 }
Al Virof5beed72015-04-30 16:09:11 -04003834 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003835 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003836 goto exit3;
Al Virof5beed72015-04-30 16:09:11 -04003837 error = vfs_rmdir(path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003838exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003839 dput(dentry);
3840exit2:
Al Viro59551022016-01-22 15:40:57 -05003841 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003842 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843exit1:
Al Virof5beed72015-04-30 16:09:11 -04003844 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003846 if (retry_estale(error, lookup_flags)) {
3847 lookup_flags |= LOOKUP_REVAL;
3848 goto retry;
3849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 return error;
3851}
3852
Heiko Carstens3cdad422009-01-14 14:14:22 +01003853SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003854{
3855 return do_rmdir(AT_FDCWD, pathname);
3856}
3857
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003858/**
3859 * vfs_unlink - unlink a filesystem object
3860 * @dir: parent directory
3861 * @dentry: victim
3862 * @delegated_inode: returns victim inode, if the inode is delegated.
3863 *
3864 * The caller must hold dir->i_mutex.
3865 *
3866 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3867 * return a reference to the inode in delegated_inode. The caller
3868 * should then break the delegation on that inode and retry. Because
3869 * breaking a delegation may take a long time, the caller should drop
3870 * dir->i_mutex before doing so.
3871 *
3872 * Alternatively, a caller may pass NULL for delegated_inode. This may
3873 * be appropriate for callers that expect the underlying filesystem not
3874 * to be NFS exported.
3875 */
3876int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003878 struct inode *target = dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 int error = may_delete(dir, dentry, 0);
3880
3881 if (error)
3882 return error;
3883
Al Viroacfa4382008-12-04 10:06:33 -05003884 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 return -EPERM;
3886
Al Viro59551022016-01-22 15:40:57 -05003887 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003888 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 error = -EBUSY;
3890 else {
3891 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05003892 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003893 error = try_break_deleg(target, delegated_inode);
3894 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003895 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003897 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04003898 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003899 detach_mounts(dentry);
3900 }
Al Virobec10522010-03-03 14:12:08 -05003901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003903out:
Al Viro59551022016-01-22 15:40:57 -05003904 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905
3906 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
3907 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003908 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 }
Robert Love0eeca282005-07-12 17:06:03 -04003911
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 return error;
3913}
Al Viro4d359502014-03-14 12:20:17 -04003914EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
3916/*
3917 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08003918 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 * writeout happening, and we don't want to prevent access to the directory
3920 * while waiting on the I/O.
3921 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003922static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923{
Al Viro2ad94ae2008-07-21 09:32:51 -04003924 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04003925 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003927 struct path path;
3928 struct qstr last;
3929 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003931 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05003932 unsigned int lookup_flags = 0;
3933retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04003934 name = filename_parentat(dfd, getname(pathname), lookup_flags,
3935 &path, &last, &type);
Jeff Layton91a27b22012-10-10 15:25:28 -04003936 if (IS_ERR(name))
3937 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04003938
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04003940 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003942
Al Virof5beed72015-04-30 16:09:11 -04003943 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003944 if (error)
3945 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003946retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05003947 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003948 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 error = PTR_ERR(dentry);
3950 if (!IS_ERR(dentry)) {
3951 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04003952 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03003953 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01003955 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003956 goto slashes;
3957 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04003958 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003959 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003960 goto exit2;
Al Virof5beed72015-04-30 16:09:11 -04003961 error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003962exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 dput(dentry);
3964 }
Al Viro59551022016-01-22 15:40:57 -05003965 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 if (inode)
3967 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003968 inode = NULL;
3969 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07003970 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003971 if (!error)
3972 goto retry_deleg;
3973 }
Al Virof5beed72015-04-30 16:09:11 -04003974 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975exit1:
Al Virof5beed72015-04-30 16:09:11 -04003976 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05003978 if (retry_estale(error, lookup_flags)) {
3979 lookup_flags |= LOOKUP_REVAL;
3980 inode = NULL;
3981 goto retry;
3982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 return error;
3984
3985slashes:
David Howellsb18825a2013-09-12 19:22:53 +01003986 if (d_is_negative(dentry))
3987 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003988 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01003989 error = -EISDIR;
3990 else
3991 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 goto exit2;
3993}
3994
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003995SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003996{
3997 if ((flag & ~AT_REMOVEDIR) != 0)
3998 return -EINVAL;
3999
4000 if (flag & AT_REMOVEDIR)
4001 return do_rmdir(dfd, pathname);
4002
4003 return do_unlinkat(dfd, pathname);
4004}
4005
Heiko Carstens3480b252009-01-14 14:14:16 +01004006SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004007{
4008 return do_unlinkat(AT_FDCWD, pathname);
4009}
4010
Miklos Szeredidb2e7472008-06-24 16:50:16 +02004011int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
Miklos Szeredia95164d2008-07-30 15:08:48 +02004013 int error = may_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014
4015 if (error)
4016 return error;
4017
Al Viroacfa4382008-12-04 10:06:33 -05004018 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 return -EPERM;
4020
4021 error = security_inode_symlink(dir, dentry, oldname);
4022 if (error)
4023 return error;
4024
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004026 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004027 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 return error;
4029}
Al Viro4d359502014-03-14 12:20:17 -04004030EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004032SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4033 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034{
Al Viro2ad94ae2008-07-21 09:32:51 -04004035 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004036 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004037 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004038 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004039 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
4041 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004042 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004044retry:
4045 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004046 error = PTR_ERR(dentry);
4047 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004048 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004049
Jeff Layton91a27b22012-10-10 15:25:28 -04004050 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004051 if (!error)
Jeff Layton91a27b22012-10-10 15:25:28 -04004052 error = vfs_symlink(path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004053 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004054 if (retry_estale(error, lookup_flags)) {
4055 lookup_flags |= LOOKUP_REVAL;
4056 goto retry;
4057 }
Dave Hansen6902d922006-09-30 23:29:01 -07004058out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 putname(from);
4060 return error;
4061}
4062
Heiko Carstens3480b252009-01-14 14:14:16 +01004063SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004064{
4065 return sys_symlinkat(oldname, AT_FDCWD, newname);
4066}
4067
J. Bruce Fields146a8592011-09-20 17:14:31 -04004068/**
4069 * vfs_link - create a new link
4070 * @old_dentry: object to be linked
4071 * @dir: new parent
4072 * @new_dentry: where to create the new link
4073 * @delegated_inode: returns inode needing a delegation break
4074 *
4075 * The caller must hold dir->i_mutex
4076 *
4077 * If vfs_link discovers a delegation on the to-be-linked file in need
4078 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4079 * inode in delegated_inode. The caller should then break the delegation
4080 * and retry. Because breaking a delegation may take a long time, the
4081 * caller should drop the i_mutex before doing so.
4082 *
4083 * Alternatively, a caller may pass NULL for delegated_inode. This may
4084 * be appropriate for callers that expect the underlying filesystem not
4085 * to be NFS exported.
4086 */
4087int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
4089 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004090 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 int error;
4092
4093 if (!inode)
4094 return -ENOENT;
4095
Miklos Szeredia95164d2008-07-30 15:08:48 +02004096 error = may_create(dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 if (error)
4098 return error;
4099
4100 if (dir->i_sb != inode->i_sb)
4101 return -EXDEV;
4102
4103 /*
4104 * A link to an append-only or immutable file cannot be created.
4105 */
4106 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4107 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004108 /*
4109 * Updating the link count will likely cause i_uid and i_gid to
4110 * be writen back improperly if their true value is unknown to
4111 * the vfs.
4112 */
4113 if (HAS_UNMAPPED_ID(inode))
4114 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004115 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004117 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 return -EPERM;
4119
4120 error = security_inode_link(old_dentry, dir, new_dentry);
4121 if (error)
4122 return error;
4123
Al Viro59551022016-01-22 15:40:57 -05004124 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304125 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004126 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304127 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004128 else if (max_links && inode->i_nlink >= max_links)
4129 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004130 else {
4131 error = try_break_deleg(inode, delegated_inode);
4132 if (!error)
4133 error = dir->i_op->link(old_dentry, dir, new_dentry);
4134 }
Al Virof4e0c302013-06-11 08:34:36 +04004135
4136 if (!error && (inode->i_state & I_LINKABLE)) {
4137 spin_lock(&inode->i_lock);
4138 inode->i_state &= ~I_LINKABLE;
4139 spin_unlock(&inode->i_lock);
4140 }
Al Viro59551022016-01-22 15:40:57 -05004141 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004142 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004143 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 return error;
4145}
Al Viro4d359502014-03-14 12:20:17 -04004146EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148/*
4149 * Hardlinks are often used in delicate situations. We avoid
4150 * security-related surprises by not following symlinks on the
4151 * newname. --KAB
4152 *
4153 * We don't follow them on the oldname either to be compatible
4154 * with linux 2.0, and to avoid hard-linking to directories
4155 * and other special files. --ADM
4156 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004157SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4158 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
4160 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004161 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004162 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304163 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304166 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004167 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304168 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004169 * To use null names we require CAP_DAC_READ_SEARCH
4170 * This ensures that not everyone will be able to create
4171 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304172 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004173 if (flags & AT_EMPTY_PATH) {
4174 if (!capable(CAP_DAC_READ_SEARCH))
4175 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304176 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004177 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004178
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304179 if (flags & AT_SYMLINK_FOLLOW)
4180 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004181retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304182 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004184 return error;
4185
Jeff Layton442e31c2012-12-20 16:15:38 -05004186 new_dentry = user_path_create(newdfd, newname, &new_path,
4187 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004189 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004190 goto out;
4191
4192 error = -EXDEV;
4193 if (old_path.mnt != new_path.mnt)
4194 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004195 error = may_linkat(&old_path);
4196 if (unlikely(error))
4197 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004198 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004199 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004200 goto out_dput;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004201 error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004202out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004203 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004204 if (delegated_inode) {
4205 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004206 if (!error) {
4207 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004208 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004209 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004210 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004211 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004212 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004213 how |= LOOKUP_REVAL;
4214 goto retry;
4215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216out:
Al Viro2d8f3032008-07-22 09:59:21 -04004217 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 return error;
4220}
4221
Heiko Carstens3480b252009-01-14 14:14:16 +01004222SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004223{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004224 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004225}
4226
Miklos Szeredibc270272014-04-01 17:08:42 +02004227/**
4228 * vfs_rename - rename a filesystem object
4229 * @old_dir: parent of source
4230 * @old_dentry: source
4231 * @new_dir: parent of destination
4232 * @new_dentry: destination
4233 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004234 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004235 *
4236 * The caller must hold multiple mutexes--see lock_rename()).
4237 *
4238 * If vfs_rename discovers a delegation in need of breaking at either
4239 * the source or destination, it will return -EWOULDBLOCK and return a
4240 * reference to the inode in delegated_inode. The caller should then
4241 * break the delegation and retry. Because breaking a delegation may
4242 * take a long time, the caller should drop all locks before doing
4243 * so.
4244 *
4245 * Alternatively, a caller may pass NULL for delegated_inode. This may
4246 * be appropriate for callers that expect the underlying filesystem not
4247 * to be NFS exported.
4248 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 * The worst of all namespace operations - renaming directory. "Perverted"
4250 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4251 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004252 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 * b) race potential - two innocent renames can create a loop together.
4254 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004255 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004257 * c) we have to lock _four_ objects - parents and victim (if it exists),
4258 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004259 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 * whether the target exists). Solution: try to be smart with locking
4261 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004262 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 * move will be locked. Thus we can rank directories by the tree
4264 * (ancestors first) and rank all non-directories after them.
4265 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004266 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 * HOWEVER, it relies on the assumption that any object with ->lookup()
4268 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4269 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004270 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004271 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004273 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 * locking].
4275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004277 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004278 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
4280 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004281 bool is_dir = d_is_dir(old_dentry);
Eric Paris59b0df22010-02-08 12:53:52 -05004282 const unsigned char *old_name;
Miklos Szeredibc270272014-04-01 17:08:42 +02004283 struct inode *source = old_dentry->d_inode;
4284 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004285 bool new_is_dir = false;
4286 unsigned max_links = new_dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287
Miklos Szeredi8d3e2932016-12-16 11:02:54 +01004288 if (source == target)
Miklos Szeredibc270272014-04-01 17:08:42 +02004289 return 0;
4290
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 error = may_delete(old_dir, old_dentry, is_dir);
4292 if (error)
4293 return error;
4294
Miklos Szeredida1ce062014-04-01 17:08:43 +02004295 if (!target) {
Miklos Szeredia95164d2008-07-30 15:08:48 +02004296 error = may_create(new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004297 } else {
4298 new_is_dir = d_is_dir(new_dentry);
4299
4300 if (!(flags & RENAME_EXCHANGE))
4301 error = may_delete(new_dir, new_dentry, is_dir);
4302 else
4303 error = may_delete(new_dir, new_dentry, new_is_dir);
4304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (error)
4306 return error;
4307
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004308 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309 return -EPERM;
4310
Miklos Szeredibc270272014-04-01 17:08:42 +02004311 /*
4312 * If we are going to change the parent - check write permissions,
4313 * we'll need to flip '..'.
4314 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004315 if (new_dir != old_dir) {
4316 if (is_dir) {
4317 error = inode_permission(source, MAY_WRITE);
4318 if (error)
4319 return error;
4320 }
4321 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
4322 error = inode_permission(target, MAY_WRITE);
4323 if (error)
4324 return error;
4325 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004326 }
Robert Love0eeca282005-07-12 17:06:03 -04004327
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004328 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4329 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004330 if (error)
4331 return error;
4332
4333 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
4334 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004335 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004336 lock_two_nondirectories(source, target);
4337 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004338 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004339
4340 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004341 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004342 goto out;
4343
Miklos Szeredida1ce062014-04-01 17:08:43 +02004344 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004345 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004346 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004347 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004348 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4349 old_dir->i_nlink >= max_links)
4350 goto out;
4351 }
4352 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4353 shrink_dcache_parent(new_dentry);
4354 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004355 error = try_break_deleg(source, delegated_inode);
4356 if (error)
4357 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004358 }
4359 if (target && !new_is_dir) {
4360 error = try_break_deleg(target, delegated_inode);
4361 if (error)
4362 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004363 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004364 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004365 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004366 if (error)
4367 goto out;
4368
Miklos Szeredida1ce062014-04-01 17:08:43 +02004369 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004370 if (is_dir)
4371 target->i_flags |= S_DEAD;
4372 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004373 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004374 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004375 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4376 if (!(flags & RENAME_EXCHANGE))
4377 d_move(old_dentry, new_dentry);
4378 else
4379 d_exchange(old_dentry, new_dentry);
4380 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004381out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004382 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004383 unlock_two_nondirectories(source, target);
4384 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004385 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004386 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004387 if (!error) {
Al Viro123df292009-12-25 04:57:57 -05004388 fsnotify_move(old_dir, new_dir, old_name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004389 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4390 if (flags & RENAME_EXCHANGE) {
4391 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4392 new_is_dir, NULL, new_dentry);
4393 }
4394 }
Robert Love0eeca282005-07-12 17:06:03 -04004395 fsnotify_oldname_free(old_name);
4396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return error;
4398}
Al Viro4d359502014-03-14 12:20:17 -04004399EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004401SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4402 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403{
Al Viro2ad94ae2008-07-21 09:32:51 -04004404 struct dentry *old_dentry, *new_dentry;
4405 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004406 struct path old_path, new_path;
4407 struct qstr old_last, new_last;
4408 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004409 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004410 struct filename *from;
4411 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004412 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004413 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004414 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004415
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004416 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004417 return -EINVAL;
4418
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004419 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4420 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004421 return -EINVAL;
4422
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004423 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4424 return -EPERM;
4425
Al Virof5beed72015-04-30 16:09:11 -04004426 if (flags & RENAME_EXCHANGE)
4427 target_flags = 0;
4428
Jeff Laytonc6a94282012-12-11 12:10:10 -05004429retry:
Al Viroc1d4dd22016-06-05 16:38:18 -04004430 from = filename_parentat(olddfd, getname(oldname), lookup_flags,
4431 &old_path, &old_last, &old_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004432 if (IS_ERR(from)) {
4433 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Al Viroc1d4dd22016-06-05 16:38:18 -04004437 to = filename_parentat(newdfd, getname(newname), lookup_flags,
4438 &new_path, &new_last, &new_type);
Jeff Layton91a27b22012-10-10 15:25:28 -04004439 if (IS_ERR(to)) {
4440 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443
4444 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004445 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 goto exit2;
4447
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004449 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 goto exit2;
4451
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004452 if (flags & RENAME_NOREPLACE)
4453 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004454 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 goto exit2;
4456
Al Virof5beed72015-04-30 16:09:11 -04004457 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004458 if (error)
4459 goto exit2;
4460
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004461retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004462 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Al Virof5beed72015-04-30 16:09:11 -04004464 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 error = PTR_ERR(old_dentry);
4466 if (IS_ERR(old_dentry))
4467 goto exit3;
4468 /* source must exist */
4469 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004470 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004472 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 error = PTR_ERR(new_dentry);
4474 if (IS_ERR(new_dentry))
4475 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004476 error = -EEXIST;
4477 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4478 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004479 if (flags & RENAME_EXCHANGE) {
4480 error = -ENOENT;
4481 if (d_is_negative(new_dentry))
4482 goto exit5;
4483
4484 if (!d_is_dir(new_dentry)) {
4485 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004486 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004487 goto exit5;
4488 }
4489 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004490 /* unless the source is a directory trailing slashes give -ENOTDIR */
4491 if (!d_is_dir(old_dentry)) {
4492 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004493 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004494 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004495 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004496 goto exit5;
4497 }
4498 /* source should not be ancestor of target */
4499 error = -EINVAL;
4500 if (old_dentry == trap)
4501 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004503 if (!(flags & RENAME_EXCHANGE))
4504 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 if (new_dentry == trap)
4506 goto exit5;
4507
Al Virof5beed72015-04-30 16:09:11 -04004508 error = security_path_rename(&old_path, old_dentry,
4509 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004510 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004511 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004512 error = vfs_rename(old_path.dentry->d_inode, old_dentry,
4513 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004514 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515exit5:
4516 dput(new_dentry);
4517exit4:
4518 dput(old_dentry);
4519exit3:
Al Virof5beed72015-04-30 16:09:11 -04004520 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004521 if (delegated_inode) {
4522 error = break_deleg_wait(&delegated_inode);
4523 if (!error)
4524 goto retry_deleg;
4525 }
Al Virof5beed72015-04-30 16:09:11 -04004526 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004528 if (retry_estale(error, lookup_flags))
4529 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004530 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004531 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532exit1:
Al Virof5beed72015-04-30 16:09:11 -04004533 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004535 if (should_retry) {
4536 should_retry = false;
4537 lookup_flags |= LOOKUP_REVAL;
4538 goto retry;
4539 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004540exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 return error;
4542}
4543
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004544SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4545 int, newdfd, const char __user *, newname)
4546{
4547 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4548}
4549
Heiko Carstensa26eab22009-01-14 14:14:17 +01004550SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004551{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004552 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004553}
4554
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004555int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4556{
4557 int error = may_create(dir, dentry);
4558 if (error)
4559 return error;
4560
4561 if (!dir->i_op->mknod)
4562 return -EPERM;
4563
4564 return dir->i_op->mknod(dir, dentry,
4565 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4566}
4567EXPORT_SYMBOL(vfs_whiteout);
4568
Al Viro5d826c82014-03-14 13:42:45 -04004569int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
Al Viro5d826c82014-03-14 13:42:45 -04004571 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572 if (IS_ERR(link))
4573 goto out;
4574
4575 len = strlen(link);
4576 if (len > (unsigned) buflen)
4577 len = buflen;
4578 if (copy_to_user(buffer, link, len))
4579 len = -EFAULT;
4580out:
4581 return len;
4582}
4583
4584/*
4585 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
Al Viro6b255392015-11-17 10:20:54 -05004586 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4587 * for any given inode is up to filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 */
Miklos Szeredid16744e2016-12-09 16:45:04 +01004589static int generic_readlink(struct dentry *dentry, char __user *buffer,
4590 int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591{
Al Virofceef392015-12-29 15:58:39 -05004592 DEFINE_DELAYED_CALL(done);
Al Viro5f2c4172015-05-07 11:14:26 -04004593 struct inode *inode = d_inode(dentry);
4594 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004595 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004596
Al Virod4dee482015-04-30 20:08:02 -04004597 if (!link) {
Al Virofceef392015-12-29 15:58:39 -05004598 link = inode->i_op->get_link(dentry, inode, &done);
Al Virod4dee482015-04-30 20:08:02 -04004599 if (IS_ERR(link))
4600 return PTR_ERR(link);
4601 }
Al Viro680baac2015-05-02 13:32:22 -04004602 res = readlink_copy(buffer, buflen, link);
Al Virofceef392015-12-29 15:58:39 -05004603 do_delayed_call(&done);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004604 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605}
4606
Miklos Szeredid60874c2016-10-04 14:40:45 +02004607/**
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004608 * vfs_readlink - copy symlink body into userspace buffer
4609 * @dentry: dentry on which to get symbolic link
4610 * @buffer: user memory pointer
4611 * @buflen: size of buffer
4612 *
4613 * Does not touch atime. That's up to the caller if necessary
4614 *
4615 * Does not call security hook.
4616 */
4617int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4618{
4619 struct inode *inode = d_inode(dentry);
4620
Miklos Szeredi76fca902016-12-09 16:45:04 +01004621 if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) {
4622 if (unlikely(inode->i_op->readlink))
4623 return inode->i_op->readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0ed2016-12-09 16:45:04 +01004624
Miklos Szeredi76fca902016-12-09 16:45:04 +01004625 if (!d_is_symlink(dentry))
4626 return -EINVAL;
4627
4628 spin_lock(&inode->i_lock);
4629 inode->i_opflags |= IOP_DEFAULT_READLINK;
4630 spin_unlock(&inode->i_lock);
4631 }
4632
4633 return generic_readlink(dentry, buffer, buflen);
Miklos Szeredifd4a0ed2016-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;
Nick Piggin54566b22009-01-04 12:00:53 -08004718 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4719 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);