blob: 339f7c741ed11b4dd1054a90146107fdb296f74f [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 Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/uaccess.h>
41
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 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700378static inline int do_inode_permission(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700379{
380 if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) {
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700381 if (likely(mnt && inode->i_op->permission2))
382 return inode->i_op->permission2(mnt, inode, mask);
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700383 if (likely(inode->i_op->permission))
384 return inode->i_op->permission(inode, mask);
385
386 /* This gets set once for the inode lifetime */
387 spin_lock(&inode->i_lock);
388 inode->i_opflags |= IOP_FASTPERM;
389 spin_unlock(&inode->i_lock);
390 }
391 return generic_permission(inode, mask);
392}
393
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200394/**
David Howells0bdaea92012-06-25 12:55:46 +0100395 * __inode_permission - Check for access rights to a given inode
396 * @inode: Inode to check permission on
397 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200398 *
David Howells0bdaea92012-06-25 12:55:46 +0100399 * Check for read/write/execute permissions on an inode.
Andreas Gruenbacher948409c2011-10-23 23:13:33 +0530400 *
401 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
David Howells0bdaea92012-06-25 12:55:46 +0100402 *
403 * This does not check for a read-only file system. You probably want
404 * inode_permission().
Christoph Hellwigcb23beb2008-10-24 09:59:29 +0200405 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700406int __inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407{
Al Viroe6305c42008-07-15 21:03:57 -0400408 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Linus Torvalds3ddcd052011-08-06 22:45:50 -0700410 if (unlikely(mask & MAY_WRITE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * Nobody gets write access to an immutable file.
413 */
414 if (IS_IMMUTABLE(inode))
Eryu Guan337684a2016-08-02 19:58:28 +0800415 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -0500416
417 /*
418 * Updating mtime will likely cause i_uid and i_gid to be
419 * written back improperly if their true value is unknown
420 * to the vfs.
421 */
422 if (HAS_UNMAPPED_ID(inode))
423 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 }
425
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700426 retval = do_inode_permission(mnt, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (retval)
428 return retval;
429
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700430 retval = devcgroup_inode_permission(inode, mask);
431 if (retval)
432 return retval;
433
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700434 retval = security_inode_permission(inode, mask);
435 return retval;
436}
437EXPORT_SYMBOL(__inode_permission2);
438
439int __inode_permission(struct inode *inode, int mask)
440{
441 return __inode_permission2(NULL, inode, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
Miklos Szeredibd5d0852014-10-24 00:14:35 +0200443EXPORT_SYMBOL(__inode_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Al Virof4d6ff82011-06-19 13:14:21 -0400445/**
David Howells0bdaea92012-06-25 12:55:46 +0100446 * sb_permission - Check superblock-level permissions
447 * @sb: Superblock of inode to check permission on
Randy Dunlap55852632012-08-18 17:39:25 -0700448 * @inode: Inode to check permission on
David Howells0bdaea92012-06-25 12:55:46 +0100449 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
450 *
451 * Separate out file-system wide checks from inode-specific permission checks.
452 */
453static int sb_permission(struct super_block *sb, struct inode *inode, int mask)
454{
455 if (unlikely(mask & MAY_WRITE)) {
456 umode_t mode = inode->i_mode;
457
458 /* Nobody gets write access to a read-only fs. */
459 if ((sb->s_flags & MS_RDONLY) &&
460 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
461 return -EROFS;
462 }
463 return 0;
464}
465
466/**
467 * inode_permission - Check for access rights to a given inode
468 * @inode: Inode to check permission on
469 * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
470 *
471 * Check for read/write/execute permissions on an inode. We use fs[ug]id for
472 * this, letting us set arbitrary permissions for filesystem access without
473 * changing the "normal" UIDs which are used for other things.
474 *
475 * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask.
476 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700477int inode_permission2(struct vfsmount *mnt, struct inode *inode, int mask)
David Howells0bdaea92012-06-25 12:55:46 +0100478{
479 int retval;
480
481 retval = sb_permission(inode->i_sb, inode, mask);
482 if (retval)
483 return retval;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -0700484 return __inode_permission2(mnt, inode, mask);
485}
486EXPORT_SYMBOL(inode_permission2);
487
488int inode_permission(struct inode *inode, int mask)
489{
490 return inode_permission2(NULL, inode, mask);
David Howells0bdaea92012-06-25 12:55:46 +0100491}
Al Viro4d359502014-03-14 12:20:17 -0400492EXPORT_SYMBOL(inode_permission);
David Howells0bdaea92012-06-25 12:55:46 +0100493
494/**
Jan Blunck5dd784d02008-02-14 19:34:38 -0800495 * path_get - get a reference to a path
496 * @path: path to get the reference to
497 *
498 * Given a path increment the reference count to the dentry and the vfsmount.
499 */
Al Virodcf787f2013-03-01 23:51:07 -0500500void path_get(const struct path *path)
Jan Blunck5dd784d02008-02-14 19:34:38 -0800501{
502 mntget(path->mnt);
503 dget(path->dentry);
504}
505EXPORT_SYMBOL(path_get);
506
507/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800508 * path_put - put a reference to a path
509 * @path: path to put the reference to
510 *
511 * Given a path decrement the reference count to the dentry and the vfsmount.
512 */
Al Virodcf787f2013-03-01 23:51:07 -0500513void path_put(const struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
Jan Blunck1d957f92008-02-14 19:34:35 -0800515 dput(path->dentry);
516 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
Jan Blunck1d957f92008-02-14 19:34:35 -0800518EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Al Viro894bc8c2015-05-02 07:16:16 -0400520#define EMBEDDED_LEVELS 2
Al Viro1f55a6e2014-11-01 19:30:41 -0400521struct nameidata {
522 struct path path;
Al Viro1cf26652015-05-06 16:01:56 -0400523 struct qstr last;
Al Viro1f55a6e2014-11-01 19:30:41 -0400524 struct path root;
525 struct inode *inode; /* path.dentry.d_inode */
526 unsigned int flags;
Al Viro9883d182015-05-13 07:28:08 -0400527 unsigned seq, m_seq;
Al Viro1f55a6e2014-11-01 19:30:41 -0400528 int last_type;
529 unsigned depth;
NeilBrown756daf22015-03-23 13:37:38 +1100530 int total_link_count;
Al Viro697fc6c2015-05-02 19:38:35 -0400531 struct saved {
532 struct path link;
Al Virofceef392015-12-29 15:58:39 -0500533 struct delayed_call done;
Al Viro697fc6c2015-05-02 19:38:35 -0400534 const char *name;
Al Viro0450b2d2015-05-08 13:23:53 -0400535 unsigned seq;
Al Viro894bc8c2015-05-02 07:16:16 -0400536 } *stack, internal[EMBEDDED_LEVELS];
Al Viro9883d182015-05-13 07:28:08 -0400537 struct filename *name;
538 struct nameidata *saved;
Al Virofceef392015-12-29 15:58:39 -0500539 struct inode *link_inode;
Al Viro9883d182015-05-13 07:28:08 -0400540 unsigned root_seq;
541 int dfd;
Al Viro1f55a6e2014-11-01 19:30:41 -0400542};
543
Al Viro9883d182015-05-13 07:28:08 -0400544static void set_nameidata(struct nameidata *p, int dfd, struct filename *name)
Al Viro894bc8c2015-05-02 07:16:16 -0400545{
NeilBrown756daf22015-03-23 13:37:38 +1100546 struct nameidata *old = current->nameidata;
547 p->stack = p->internal;
Al Viroc8a53ee2015-05-12 18:43:07 -0400548 p->dfd = dfd;
549 p->name = name;
NeilBrown756daf22015-03-23 13:37:38 +1100550 p->total_link_count = old ? old->total_link_count : 0;
Al Viro9883d182015-05-13 07:28:08 -0400551 p->saved = old;
NeilBrown756daf22015-03-23 13:37:38 +1100552 current->nameidata = p;
Al Viro894bc8c2015-05-02 07:16:16 -0400553}
554
Al Viro9883d182015-05-13 07:28:08 -0400555static void restore_nameidata(void)
Al Viro894bc8c2015-05-02 07:16:16 -0400556{
Al Viro9883d182015-05-13 07:28:08 -0400557 struct nameidata *now = current->nameidata, *old = now->saved;
NeilBrown756daf22015-03-23 13:37:38 +1100558
559 current->nameidata = old;
560 if (old)
561 old->total_link_count = now->total_link_count;
Al Viroe1a63bb2015-12-05 21:06:33 -0500562 if (now->stack != now->internal)
NeilBrown756daf22015-03-23 13:37:38 +1100563 kfree(now->stack);
Al Viro894bc8c2015-05-02 07:16:16 -0400564}
565
566static int __nd_alloc_stack(struct nameidata *nd)
567{
Al Virobc40aee2015-05-09 13:04:24 -0400568 struct saved *p;
569
570 if (nd->flags & LOOKUP_RCU) {
571 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
572 GFP_ATOMIC);
573 if (unlikely(!p))
574 return -ECHILD;
575 } else {
576 p= kmalloc(MAXSYMLINKS * sizeof(struct saved),
Al Viro894bc8c2015-05-02 07:16:16 -0400577 GFP_KERNEL);
Al Virobc40aee2015-05-09 13:04:24 -0400578 if (unlikely(!p))
579 return -ENOMEM;
580 }
Al Viro894bc8c2015-05-02 07:16:16 -0400581 memcpy(p, nd->internal, sizeof(nd->internal));
582 nd->stack = p;
583 return 0;
584}
585
Eric W. Biederman397d4252015-08-15 20:27:13 -0500586/**
587 * path_connected - Verify that a path->dentry is below path->mnt.mnt_root
588 * @path: nameidate to verify
589 *
590 * Rename can sometimes move a file or directory outside of a bind
591 * mount, path_connected allows those cases to be detected.
592 */
593static bool path_connected(const struct path *path)
594{
595 struct vfsmount *mnt = path->mnt;
596
597 /* Only bind mounts can have disconnected paths */
598 if (mnt->mnt_root == mnt->mnt_sb->s_root)
599 return true;
600
601 return is_subdir(path->dentry, mnt->mnt_root);
602}
603
Al Viro894bc8c2015-05-02 07:16:16 -0400604static inline int nd_alloc_stack(struct nameidata *nd)
605{
Al Viroda4e0be2015-05-03 20:52:15 -0400606 if (likely(nd->depth != EMBEDDED_LEVELS))
Al Viro894bc8c2015-05-02 07:16:16 -0400607 return 0;
608 if (likely(nd->stack != nd->internal))
609 return 0;
610 return __nd_alloc_stack(nd);
611}
612
Al Viro79733872015-05-09 12:55:43 -0400613static void drop_links(struct nameidata *nd)
614{
615 int i = nd->depth;
616 while (i--) {
617 struct saved *last = nd->stack + i;
Al Virofceef392015-12-29 15:58:39 -0500618 do_delayed_call(&last->done);
619 clear_delayed_call(&last->done);
Al Viro79733872015-05-09 12:55:43 -0400620 }
621}
622
623static void terminate_walk(struct nameidata *nd)
624{
625 drop_links(nd);
626 if (!(nd->flags & LOOKUP_RCU)) {
627 int i;
628 path_put(&nd->path);
629 for (i = 0; i < nd->depth; i++)
630 path_put(&nd->stack[i].link);
Al Viro102b8af2015-05-12 17:35:52 -0400631 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
632 path_put(&nd->root);
633 nd->root.mnt = NULL;
634 }
Al Viro79733872015-05-09 12:55:43 -0400635 } else {
636 nd->flags &= ~LOOKUP_RCU;
637 if (!(nd->flags & LOOKUP_ROOT))
638 nd->root.mnt = NULL;
639 rcu_read_unlock();
640 }
641 nd->depth = 0;
642}
643
644/* path_put is needed afterwards regardless of success or failure */
645static bool legitimize_path(struct nameidata *nd,
646 struct path *path, unsigned seq)
647{
648 int res = __legitimize_mnt(path->mnt, nd->m_seq);
649 if (unlikely(res)) {
650 if (res > 0)
651 path->mnt = NULL;
652 path->dentry = NULL;
653 return false;
654 }
655 if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) {
656 path->dentry = NULL;
657 return false;
658 }
659 return !read_seqcount_retry(&path->dentry->d_seq, seq);
660}
661
662static bool legitimize_links(struct nameidata *nd)
663{
664 int i;
665 for (i = 0; i < nd->depth; i++) {
666 struct saved *last = nd->stack + i;
667 if (unlikely(!legitimize_path(nd, &last->link, last->seq))) {
668 drop_links(nd);
669 nd->depth = i + 1;
670 return false;
671 }
672 }
673 return true;
674}
675
Al Viro19660af2011-03-25 10:32:48 -0400676/*
Nick Piggin31e6b012011-01-07 17:49:52 +1100677 * Path walking has 2 modes, rcu-walk and ref-walk (see
Al Viro19660af2011-03-25 10:32:48 -0400678 * Documentation/filesystems/path-lookup.txt). In situations when we can't
679 * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab
Mike Marshall57e37152015-11-30 11:11:59 -0500680 * normal reference counts on dentries and vfsmounts to transition to ref-walk
Al Viro19660af2011-03-25 10:32:48 -0400681 * mode. Refcounts are grabbed at the last known good point before rcu-walk
682 * got stuck, so ref-walk may continue from there. If this is not successful
683 * (eg. a seqcount has changed), then failure is returned and it's up to caller
684 * to restart the path walk from the beginning in ref-walk mode.
Nick Piggin31e6b012011-01-07 17:49:52 +1100685 */
Nick Piggin31e6b012011-01-07 17:49:52 +1100686
687/**
Al Viro19660af2011-03-25 10:32:48 -0400688 * unlazy_walk - try to switch to ref-walk mode.
689 * @nd: nameidata pathwalk data
690 * @dentry: child of nd->path.dentry or NULL
Al Viro6e9918b2015-05-05 09:26:05 -0400691 * @seq: seq number to check dentry against
Randy Dunlap39191622011-01-08 19:36:21 -0800692 * Returns: 0 on success, -ECHILD on failure
Nick Piggin31e6b012011-01-07 17:49:52 +1100693 *
Al Viro19660af2011-03-25 10:32:48 -0400694 * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry
695 * for ref-walk mode. @dentry must be a path found by a do_lookup call on
696 * @nd or NULL. Must be called from rcu-walk context.
Al Viro79733872015-05-09 12:55:43 -0400697 * Nothing should touch nameidata between unlazy_walk() failure and
698 * terminate_walk().
Nick Piggin31e6b012011-01-07 17:49:52 +1100699 */
Al Viro6e9918b2015-05-05 09:26:05 -0400700static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq)
Nick Piggin31e6b012011-01-07 17:49:52 +1100701{
Nick Piggin31e6b012011-01-07 17:49:52 +1100702 struct dentry *parent = nd->path.dentry;
703
704 BUG_ON(!(nd->flags & LOOKUP_RCU));
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700705
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700706 nd->flags &= ~LOOKUP_RCU;
Al Viro79733872015-05-09 12:55:43 -0400707 if (unlikely(!legitimize_links(nd)))
708 goto out2;
709 if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq)))
710 goto out2;
711 if (unlikely(!lockref_get_not_dead(&parent->d_lockref)))
712 goto out1;
Al Viro48a066e2013-09-29 22:06:07 -0400713
Linus Torvalds15570082013-09-02 11:38:06 -0700714 /*
715 * For a negative lookup, the lookup sequence point is the parents
716 * sequence point, and it only needs to revalidate the parent dentry.
717 *
718 * For a positive lookup, we need to move both the parent and the
719 * dentry from the RCU domain to be properly refcounted. And the
720 * sequence number in the dentry validates *both* dentry counters,
721 * since we checked the sequence number of the parent after we got
722 * the child sequence number. So we know the parent must still
723 * be valid if the child sequence number is still valid.
724 */
Al Viro19660af2011-03-25 10:32:48 -0400725 if (!dentry) {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700726 if (read_seqcount_retry(&parent->d_seq, nd->seq))
727 goto out;
Al Viro19660af2011-03-25 10:32:48 -0400728 BUG_ON(nd->inode != parent->d_inode);
729 } else {
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700730 if (!lockref_get_not_dead(&dentry->d_lockref))
731 goto out;
Al Viro6e9918b2015-05-05 09:26:05 -0400732 if (read_seqcount_retry(&dentry->d_seq, seq))
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700733 goto drop_dentry;
Al Viro19660af2011-03-25 10:32:48 -0400734 }
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700735
736 /*
737 * Sequence counts matched. Now make sure that the root is
738 * still valid and get it if required.
739 */
740 if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) {
Al Viro5a8d87e2015-05-12 00:10:18 -0400741 if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) {
742 rcu_read_unlock();
743 dput(dentry);
744 return -ECHILD;
Al Viro79733872015-05-09 12:55:43 -0400745 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100746 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100747
Al Viro8b61e742013-11-08 12:45:01 -0500748 rcu_read_unlock();
Nick Piggin31e6b012011-01-07 17:49:52 +1100749 return 0;
Al Viro19660af2011-03-25 10:32:48 -0400750
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700751drop_dentry:
Al Viro8b61e742013-11-08 12:45:01 -0500752 rcu_read_unlock();
Linus Torvalds15570082013-09-02 11:38:06 -0700753 dput(dentry);
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700754 goto drop_root_mnt;
Al Viro79733872015-05-09 12:55:43 -0400755out2:
756 nd->path.mnt = NULL;
757out1:
758 nd->path.dentry = NULL;
Linus Torvaldse5c832d2013-09-08 18:13:49 -0700759out:
Al Viro8b61e742013-11-08 12:45:01 -0500760 rcu_read_unlock();
Linus Torvaldsd0d27272013-09-10 12:17:49 -0700761drop_root_mnt:
762 if (!(nd->flags & LOOKUP_ROOT))
763 nd->root.mnt = NULL;
Nick Piggin31e6b012011-01-07 17:49:52 +1100764 return -ECHILD;
765}
766
Al Viro79733872015-05-09 12:55:43 -0400767static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq)
768{
769 if (unlikely(!legitimize_path(nd, link, seq))) {
770 drop_links(nd);
771 nd->depth = 0;
772 nd->flags &= ~LOOKUP_RCU;
773 nd->path.mnt = NULL;
774 nd->path.dentry = NULL;
775 if (!(nd->flags & LOOKUP_ROOT))
776 nd->root.mnt = NULL;
777 rcu_read_unlock();
778 } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) {
779 return 0;
780 }
781 path_put(link);
782 return -ECHILD;
783}
784
Al Viro4ce16ef32012-06-10 16:10:59 -0400785static inline int d_revalidate(struct dentry *dentry, unsigned int flags)
Nick Piggin34286d62011-01-07 17:49:57 +1100786{
Al Viro4ce16ef32012-06-10 16:10:59 -0400787 return dentry->d_op->d_revalidate(dentry, flags);
Nick Piggin34286d62011-01-07 17:49:57 +1100788}
789
Al Viro9f1fafe2011-03-25 11:00:12 -0400790/**
791 * complete_walk - successful completion of path walk
792 * @nd: pointer nameidata
Jeff Layton39159de2009-12-07 12:01:50 -0500793 *
Al Viro9f1fafe2011-03-25 11:00:12 -0400794 * If we had been in RCU mode, drop out of it and legitimize nd->path.
795 * Revalidate the final result, unless we'd already done that during
796 * the path walk or the filesystem doesn't ask for it. Return 0 on
797 * success, -error on failure. In case of failure caller does not
798 * need to drop nd->path.
Jeff Layton39159de2009-12-07 12:01:50 -0500799 */
Al Viro9f1fafe2011-03-25 11:00:12 -0400800static int complete_walk(struct nameidata *nd)
Jeff Layton39159de2009-12-07 12:01:50 -0500801{
Al Viro16c2cd72011-02-22 15:50:10 -0500802 struct dentry *dentry = nd->path.dentry;
Jeff Layton39159de2009-12-07 12:01:50 -0500803 int status;
Jeff Layton39159de2009-12-07 12:01:50 -0500804
Al Viro9f1fafe2011-03-25 11:00:12 -0400805 if (nd->flags & LOOKUP_RCU) {
Al Viro9f1fafe2011-03-25 11:00:12 -0400806 if (!(nd->flags & LOOKUP_ROOT))
807 nd->root.mnt = NULL;
Al Viro6e9918b2015-05-05 09:26:05 -0400808 if (unlikely(unlazy_walk(nd, NULL, 0)))
Al Viro48a066e2013-09-29 22:06:07 -0400809 return -ECHILD;
Al Viro9f1fafe2011-03-25 11:00:12 -0400810 }
811
Al Viro16c2cd72011-02-22 15:50:10 -0500812 if (likely(!(nd->flags & LOOKUP_JUMPED)))
Jeff Layton39159de2009-12-07 12:01:50 -0500813 return 0;
814
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500815 if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE)))
Al Viro16c2cd72011-02-22 15:50:10 -0500816 return 0;
817
Jeff Laytonecf3d1f2013-02-20 11:19:05 -0500818 status = dentry->d_op->d_weak_revalidate(dentry, nd->flags);
Jeff Layton39159de2009-12-07 12:01:50 -0500819 if (status > 0)
820 return 0;
821
Al Viro16c2cd72011-02-22 15:50:10 -0500822 if (!status)
Jeff Layton39159de2009-12-07 12:01:50 -0500823 status = -ESTALE;
Al Viro16c2cd72011-02-22 15:50:10 -0500824
Jeff Layton39159de2009-12-07 12:01:50 -0500825 return status;
826}
827
Al Viro18d8c862015-05-12 16:32:34 -0400828static void set_root(struct nameidata *nd)
Al Viro2a737872009-04-07 11:49:53 -0400829{
Al Viro7bd88372014-09-13 21:55:46 -0400830 struct fs_struct *fs = current->fs;
Nick Pigginc28cc362011-01-07 17:49:53 +1100831
Al Viro9e6697e2015-12-05 20:07:21 -0500832 if (nd->flags & LOOKUP_RCU) {
833 unsigned seq;
834
835 do {
836 seq = read_seqcount_begin(&fs->seq);
837 nd->root = fs->root;
838 nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq);
839 } while (read_seqcount_retry(&fs->seq, seq));
840 } else {
841 get_fs_root(fs, &nd->root);
842 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100843}
844
Jan Blunck1d957f92008-02-14 19:34:35 -0800845static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700846{
847 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800848 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700849 mntput(path->mnt);
850}
851
Nick Piggin7b9337a2011-01-14 08:42:43 +0000852static inline void path_to_nameidata(const struct path *path,
853 struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700854{
Nick Piggin31e6b012011-01-07 17:49:52 +1100855 if (!(nd->flags & LOOKUP_RCU)) {
856 dput(nd->path.dentry);
857 if (nd->path.mnt != path->mnt)
858 mntput(nd->path.mnt);
Huang Shijie9a229682010-04-02 17:37:13 +0800859 }
Nick Piggin31e6b012011-01-07 17:49:52 +1100860 nd->path.mnt = path->mnt;
Jan Blunck4ac91372008-02-14 19:34:32 -0800861 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700862}
863
Al Viro248fb5b2015-12-05 20:51:58 -0500864static int nd_jump_root(struct nameidata *nd)
865{
866 if (nd->flags & LOOKUP_RCU) {
867 struct dentry *d;
868 nd->path = nd->root;
869 d = nd->path.dentry;
870 nd->inode = d->d_inode;
871 nd->seq = nd->root_seq;
872 if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq)))
873 return -ECHILD;
874 } else {
875 path_put(&nd->path);
876 nd->path = nd->root;
877 path_get(&nd->path);
878 nd->inode = nd->path.dentry->d_inode;
879 }
880 nd->flags |= LOOKUP_JUMPED;
881 return 0;
882}
883
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400884/*
Al Viro6b255392015-11-17 10:20:54 -0500885 * Helper to directly jump to a known parsed path from ->get_link,
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400886 * caller must have taken a reference to path beforehand.
887 */
Al Viro6e771372015-05-02 13:37:52 -0400888void nd_jump_link(struct path *path)
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400889{
Al Viro6e771372015-05-02 13:37:52 -0400890 struct nameidata *nd = current->nameidata;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400891 path_put(&nd->path);
892
893 nd->path = *path;
894 nd->inode = nd->path.dentry->d_inode;
895 nd->flags |= LOOKUP_JUMPED;
Christoph Hellwigb5fb63c12012-06-18 10:47:04 -0400896}
897
Al Virob9ff4422015-05-02 20:19:23 -0400898static inline void put_link(struct nameidata *nd)
Al Viro574197e2011-03-14 22:20:34 -0400899{
Al Viro21c30032015-05-03 21:06:24 -0400900 struct saved *last = nd->stack + --nd->depth;
Al Virofceef392015-12-29 15:58:39 -0500901 do_delayed_call(&last->done);
Al Viro6548fae2015-05-07 20:32:22 -0400902 if (!(nd->flags & LOOKUP_RCU))
903 path_put(&last->link);
Al Viro574197e2011-03-14 22:20:34 -0400904}
905
Linus Torvalds561ec642012-10-26 10:05:07 -0700906int sysctl_protected_symlinks __read_mostly = 0;
907int sysctl_protected_hardlinks __read_mostly = 0;
Kees Cook800179c2012-07-25 17:29:07 -0700908
909/**
910 * may_follow_link - Check symlink following for unsafe situations
Randy Dunlap55852632012-08-18 17:39:25 -0700911 * @nd: nameidata pathwalk data
Kees Cook800179c2012-07-25 17:29:07 -0700912 *
913 * In the case of the sysctl_protected_symlinks sysctl being enabled,
914 * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is
915 * in a sticky world-writable directory. This is to protect privileged
916 * processes from failing races against path names that may change out
917 * from under them by way of other users creating malicious symlinks.
918 * It will permit symlinks to be followed only when outside a sticky
919 * world-writable directory, or when the uid of the symlink and follower
920 * match, or when the directory owner matches the symlink's owner.
921 *
922 * Returns 0 if following the symlink is allowed, -ve on error.
923 */
Al Virofec2fa22015-05-06 15:58:18 -0400924static inline int may_follow_link(struct nameidata *nd)
Kees Cook800179c2012-07-25 17:29:07 -0700925{
926 const struct inode *inode;
927 const struct inode *parent;
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500928 kuid_t puid;
Kees Cook800179c2012-07-25 17:29:07 -0700929
930 if (!sysctl_protected_symlinks)
931 return 0;
932
933 /* Allowed if owner and follower match. */
Al Virofceef392015-12-29 15:58:39 -0500934 inode = nd->link_inode;
Eric W. Biederman81abe272012-08-03 09:38:08 -0700935 if (uid_eq(current_cred()->fsuid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700936 return 0;
937
938 /* Allowed if parent directory not sticky and world-writable. */
Al Viroaa65fa32015-08-04 23:23:50 -0400939 parent = nd->inode;
Kees Cook800179c2012-07-25 17:29:07 -0700940 if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH))
941 return 0;
942
943 /* Allowed if parent directory and link owner match. */
Seth Forshee2d7f9e22016-04-26 14:36:23 -0500944 puid = parent->i_uid;
945 if (uid_valid(puid) && uid_eq(puid, inode->i_uid))
Kees Cook800179c2012-07-25 17:29:07 -0700946 return 0;
947
Al Viro319565022015-05-07 20:37:40 -0400948 if (nd->flags & LOOKUP_RCU)
949 return -ECHILD;
950
Al Viro1cf26652015-05-06 16:01:56 -0400951 audit_log_link_denied("follow_link", &nd->stack[0].link);
Kees Cook800179c2012-07-25 17:29:07 -0700952 return -EACCES;
953}
954
955/**
956 * safe_hardlink_source - Check for safe hardlink conditions
957 * @inode: the source inode to hardlink from
958 *
959 * Return false if at least one of the following conditions:
960 * - inode is not a regular file
961 * - inode is setuid
962 * - inode is setgid and group-exec
963 * - access failure for read and write
964 *
965 * Otherwise returns true.
966 */
967static bool safe_hardlink_source(struct inode *inode)
968{
969 umode_t mode = inode->i_mode;
970
971 /* Special files should not get pinned to the filesystem. */
972 if (!S_ISREG(mode))
973 return false;
974
975 /* Setuid files should not get pinned to the filesystem. */
976 if (mode & S_ISUID)
977 return false;
978
979 /* Executable setgid files should not get pinned to the filesystem. */
980 if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP))
981 return false;
982
983 /* Hardlinking to unreadable or unwritable sources is dangerous. */
984 if (inode_permission(inode, MAY_READ | MAY_WRITE))
985 return false;
986
987 return true;
988}
989
990/**
991 * may_linkat - Check permissions for creating a hardlink
992 * @link: the source to hardlink from
993 *
994 * Block hardlink when all of:
995 * - sysctl_protected_hardlinks enabled
996 * - fsuid does not match inode
997 * - hardlink source is unsafe (see safe_hardlink_source() above)
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +0200998 * - not CAP_FOWNER in a namespace with the inode owner uid mapped
Kees Cook800179c2012-07-25 17:29:07 -0700999 *
1000 * Returns 0 if successful, -ve on error.
1001 */
1002static int may_linkat(struct path *link)
1003{
Kees Cook800179c2012-07-25 17:29:07 -07001004 struct inode *inode;
1005
1006 if (!sysctl_protected_hardlinks)
1007 return 0;
1008
Kees Cook800179c2012-07-25 17:29:07 -07001009 inode = link->dentry->d_inode;
1010
1011 /* Source inode owner (or CAP_FOWNER) can hardlink all they like,
1012 * otherwise, it must be a safe source.
1013 */
Dirk Steinmetzf2ca3792015-10-20 16:09:19 +02001014 if (inode_owner_or_capable(inode) || safe_hardlink_source(inode))
Kees Cook800179c2012-07-25 17:29:07 -07001015 return 0;
1016
Kees Cooka51d9ea2012-07-25 17:29:08 -07001017 audit_log_link_denied("linkat", link);
Kees Cook800179c2012-07-25 17:29:07 -07001018 return -EPERM;
1019}
1020
Al Viro3b2e7f72015-04-19 00:53:50 -04001021static __always_inline
1022const char *get_link(struct nameidata *nd)
Ian Kent051d3812006-03-27 01:14:53 -08001023{
Al Viroab104922015-05-10 11:50:01 -04001024 struct saved *last = nd->stack + nd->depth - 1;
Al Viro1cf26652015-05-06 16:01:56 -04001025 struct dentry *dentry = last->link.dentry;
Al Virofceef392015-12-29 15:58:39 -05001026 struct inode *inode = nd->link_inode;
Al Viro6d7b5aa2012-06-10 04:15:17 -04001027 int error;
Al Viro0a959df2015-04-18 18:23:41 -04001028 const char *res;
Ian Kent051d3812006-03-27 01:14:53 -08001029
NeilBrown8fa9dd22015-03-23 13:37:40 +11001030 if (!(nd->flags & LOOKUP_RCU)) {
1031 touch_atime(&last->link);
1032 cond_resched();
Miklos Szeredi598e3c82016-09-16 12:44:20 +02001033 } else if (atime_needs_update_rcu(&last->link, inode)) {
NeilBrown8fa9dd22015-03-23 13:37:40 +11001034 if (unlikely(unlazy_walk(nd, NULL, 0)))
1035 return ERR_PTR(-ECHILD);
1036 touch_atime(&last->link);
1037 }
1038
NeilBrownbda0be72015-03-23 13:37:39 +11001039 error = security_inode_follow_link(dentry, inode,
1040 nd->flags & LOOKUP_RCU);
1041 if (unlikely(error))
Al Viro6920a442015-05-10 10:43:46 -04001042 return ERR_PTR(error);
Al Viro36f3b4f2011-02-22 21:24:38 -05001043
Al Viro86acdca12009-12-22 23:45:11 -05001044 nd->last_type = LAST_BIND;
Al Virod4dee482015-04-30 20:08:02 -04001045 res = inode->i_link;
1046 if (!res) {
Al Virofceef392015-12-29 15:58:39 -05001047 const char * (*get)(struct dentry *, struct inode *,
1048 struct delayed_call *);
1049 get = inode->i_op->get_link;
Al Viro8c1b4562015-05-09 18:15:21 -04001050 if (nd->flags & LOOKUP_RCU) {
Al Virofceef392015-12-29 15:58:39 -05001051 res = get(NULL, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001052 if (res == ERR_PTR(-ECHILD)) {
1053 if (unlikely(unlazy_walk(nd, NULL, 0)))
1054 return ERR_PTR(-ECHILD);
Al Virofceef392015-12-29 15:58:39 -05001055 res = get(dentry, inode, &last->done);
Al Viro6b255392015-11-17 10:20:54 -05001056 }
1057 } else {
Al Virofceef392015-12-29 15:58:39 -05001058 res = get(dentry, inode, &last->done);
Al Viro8c1b4562015-05-09 18:15:21 -04001059 }
Al Virofceef392015-12-29 15:58:39 -05001060 if (IS_ERR_OR_NULL(res))
Al Virofab51e82015-05-10 11:01:00 -04001061 return res;
Ian Kent051d3812006-03-27 01:14:53 -08001062 }
Al Virofab51e82015-05-10 11:01:00 -04001063 if (*res == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05001064 if (!nd->root.mnt)
1065 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05001066 if (unlikely(nd_jump_root(nd)))
1067 return ERR_PTR(-ECHILD);
Al Virofab51e82015-05-10 11:01:00 -04001068 while (unlikely(*++res == '/'))
1069 ;
1070 }
1071 if (!*res)
1072 res = NULL;
Al Viro0a959df2015-04-18 18:23:41 -04001073 return res;
1074}
Al Viro6d7b5aa2012-06-10 04:15:17 -04001075
David Howellsf015f1262012-06-25 12:55:28 +01001076/*
1077 * follow_up - Find the mountpoint of path's vfsmount
1078 *
1079 * Given a path, find the mountpoint of its source file system.
1080 * Replace @path with the path of the mountpoint in the parent mount.
1081 * Up is towards /.
1082 *
1083 * Return 1 if we went up a level and 0 if we were already at the
1084 * root.
1085 */
Al Virobab77eb2009-04-18 03:26:48 -04001086int follow_up(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Al Viro0714a532011-11-24 22:19:58 -05001088 struct mount *mnt = real_mount(path->mnt);
1089 struct mount *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 struct dentry *mountpoint;
Nick Piggin99b7db72010-08-18 04:37:39 +10001091
Al Viro48a066e2013-09-29 22:06:07 -04001092 read_seqlock_excl(&mount_lock);
Al Viro0714a532011-11-24 22:19:58 -05001093 parent = mnt->mnt_parent;
Al Viro3c0a6162012-07-18 17:32:50 +04001094 if (parent == mnt) {
Al Viro48a066e2013-09-29 22:06:07 -04001095 read_sequnlock_excl(&mount_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
1097 }
Al Viro0714a532011-11-24 22:19:58 -05001098 mntget(&parent->mnt);
Al Viroa73324d2011-11-24 22:25:07 -05001099 mountpoint = dget(mnt->mnt_mountpoint);
Al Viro48a066e2013-09-29 22:06:07 -04001100 read_sequnlock_excl(&mount_lock);
Al Virobab77eb2009-04-18 03:26:48 -04001101 dput(path->dentry);
1102 path->dentry = mountpoint;
1103 mntput(path->mnt);
Al Viro0714a532011-11-24 22:19:58 -05001104 path->mnt = &parent->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return 1;
1106}
Al Viro4d359502014-03-14 12:20:17 -04001107EXPORT_SYMBOL(follow_up);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001109/*
David Howells9875cf82011-01-14 18:45:21 +00001110 * Perform an automount
1111 * - return -EISDIR to tell follow_managed() to stop and return the path we
1112 * were called with.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 */
NeilBrown756daf22015-03-23 13:37:38 +11001114static int follow_automount(struct path *path, struct nameidata *nd,
David Howells9875cf82011-01-14 18:45:21 +00001115 bool *need_mntput)
Nick Piggin31e6b012011-01-07 17:49:52 +11001116{
David Howells9875cf82011-01-14 18:45:21 +00001117 struct vfsmount *mnt;
David Howellsea5b7782011-01-14 19:10:03 +00001118 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11001119
David Howells9875cf82011-01-14 18:45:21 +00001120 if (!path->dentry->d_op || !path->dentry->d_op->d_automount)
1121 return -EREMOTE;
Al Viro463ffb22005-06-06 13:36:05 -07001122
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001123 /* We don't want to mount if someone's just doing a stat -
1124 * unless they're stat'ing a directory and appended a '/' to
1125 * the name.
1126 *
1127 * We do, however, want to mount if someone wants to open or
1128 * create a file of any type under the mountpoint, wants to
1129 * traverse through the mountpoint or wants to open the
1130 * mounted directory. Also, autofs may mark negative dentries
1131 * as being automount points. These will need the attentions
1132 * of the daemon to instantiate them before they can be used.
David Howells9875cf82011-01-14 18:45:21 +00001133 */
NeilBrown756daf22015-03-23 13:37:38 +11001134 if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY |
1135 LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) &&
Miklos Szeredi0ec26fd2011-09-05 18:06:26 +02001136 path->dentry->d_inode)
1137 return -EISDIR;
1138
Eric W. Biedermanaeaa4a72016-07-23 11:20:44 -05001139 if (path->dentry->d_sb->s_user_ns != &init_user_ns)
1140 return -EACCES;
1141
NeilBrown756daf22015-03-23 13:37:38 +11001142 nd->total_link_count++;
1143 if (nd->total_link_count >= 40)
David Howells9875cf82011-01-14 18:45:21 +00001144 return -ELOOP;
1145
1146 mnt = path->dentry->d_op->d_automount(path);
1147 if (IS_ERR(mnt)) {
1148 /*
1149 * The filesystem is allowed to return -EISDIR here to indicate
1150 * it doesn't want to automount. For instance, autofs would do
1151 * this so that its userspace daemon can mount on this dentry.
1152 *
1153 * However, we can only permit this if it's a terminal point in
1154 * the path being looked up; if it wasn't then the remainder of
1155 * the path is inaccessible and we should say so.
1156 */
NeilBrown756daf22015-03-23 13:37:38 +11001157 if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT))
David Howells9875cf82011-01-14 18:45:21 +00001158 return -EREMOTE;
1159 return PTR_ERR(mnt);
1160 }
David Howellsea5b7782011-01-14 19:10:03 +00001161
David Howells9875cf82011-01-14 18:45:21 +00001162 if (!mnt) /* mount collision */
1163 return 0;
1164
Al Viro8aef1882011-06-16 15:10:06 +01001165 if (!*need_mntput) {
1166 /* lock_mount() may release path->mnt on error */
1167 mntget(path->mnt);
1168 *need_mntput = true;
1169 }
Al Viro19a167a2011-01-17 01:35:23 -05001170 err = finish_automount(mnt, path);
David Howellsea5b7782011-01-14 19:10:03 +00001171
David Howellsea5b7782011-01-14 19:10:03 +00001172 switch (err) {
1173 case -EBUSY:
1174 /* Someone else made a mount here whilst we were busy */
Al Viro19a167a2011-01-17 01:35:23 -05001175 return 0;
David Howellsea5b7782011-01-14 19:10:03 +00001176 case 0:
Al Viro8aef1882011-06-16 15:10:06 +01001177 path_put(path);
David Howellsea5b7782011-01-14 19:10:03 +00001178 path->mnt = mnt;
1179 path->dentry = dget(mnt->mnt_root);
David Howellsea5b7782011-01-14 19:10:03 +00001180 return 0;
Al Viro19a167a2011-01-17 01:35:23 -05001181 default:
1182 return err;
David Howellsea5b7782011-01-14 19:10:03 +00001183 }
Al Viro19a167a2011-01-17 01:35:23 -05001184
David Howells9875cf82011-01-14 18:45:21 +00001185}
1186
1187/*
1188 * Handle a dentry that is managed in some way.
David Howellscc53ce52011-01-14 18:45:26 +00001189 * - Flagged for transit management (autofs)
David Howells9875cf82011-01-14 18:45:21 +00001190 * - Flagged as mountpoint
1191 * - Flagged as automount point
1192 *
1193 * This may only be called in refwalk mode.
1194 *
1195 * Serialization is taken care of in namespace.c
1196 */
NeilBrown756daf22015-03-23 13:37:38 +11001197static int follow_managed(struct path *path, struct nameidata *nd)
David Howells9875cf82011-01-14 18:45:21 +00001198{
Al Viro8aef1882011-06-16 15:10:06 +01001199 struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */
David Howells9875cf82011-01-14 18:45:21 +00001200 unsigned managed;
1201 bool need_mntput = false;
Al Viro8aef1882011-06-16 15:10:06 +01001202 int ret = 0;
David Howells9875cf82011-01-14 18:45:21 +00001203
1204 /* Given that we're not holding a lock here, we retain the value in a
1205 * local variable for each dentry as we look at it so that we don't see
1206 * the components of that value change under us */
1207 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1208 managed &= DCACHE_MANAGED_DENTRY,
1209 unlikely(managed != 0)) {
David Howellscc53ce52011-01-14 18:45:26 +00001210 /* Allow the filesystem to manage the transit without i_mutex
1211 * being held. */
1212 if (managed & DCACHE_MANAGE_TRANSIT) {
1213 BUG_ON(!path->dentry->d_op);
1214 BUG_ON(!path->dentry->d_op->d_manage);
Al Viro1aed3e42011-03-18 09:09:02 -04001215 ret = path->dentry->d_op->d_manage(path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001216 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001217 break;
David Howellscc53ce52011-01-14 18:45:26 +00001218 }
1219
David Howells9875cf82011-01-14 18:45:21 +00001220 /* Transit to a mounted filesystem. */
1221 if (managed & DCACHE_MOUNTED) {
1222 struct vfsmount *mounted = lookup_mnt(path);
1223 if (mounted) {
1224 dput(path->dentry);
1225 if (need_mntput)
1226 mntput(path->mnt);
1227 path->mnt = mounted;
1228 path->dentry = dget(mounted->mnt_root);
1229 need_mntput = true;
1230 continue;
1231 }
1232
1233 /* Something is mounted on this dentry in another
1234 * namespace and/or whatever was mounted there in this
Al Viro48a066e2013-09-29 22:06:07 -04001235 * namespace got unmounted before lookup_mnt() could
1236 * get it */
David Howells9875cf82011-01-14 18:45:21 +00001237 }
1238
1239 /* Handle an automount point */
1240 if (managed & DCACHE_NEED_AUTOMOUNT) {
NeilBrown756daf22015-03-23 13:37:38 +11001241 ret = follow_automount(path, nd, &need_mntput);
David Howells9875cf82011-01-14 18:45:21 +00001242 if (ret < 0)
Al Viro8aef1882011-06-16 15:10:06 +01001243 break;
David Howells9875cf82011-01-14 18:45:21 +00001244 continue;
1245 }
1246
1247 /* We didn't change the current path point */
1248 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Al Viro8aef1882011-06-16 15:10:06 +01001250
1251 if (need_mntput && path->mnt == mnt)
1252 mntput(path->mnt);
Al Viroe9742b52016-03-05 22:04:59 -05001253 if (ret == -EISDIR || !ret)
1254 ret = 1;
Al Viro84027522015-04-22 10:30:08 -04001255 if (need_mntput)
1256 nd->flags |= LOOKUP_JUMPED;
1257 if (unlikely(ret < 0))
1258 path_put_conditional(path, nd);
1259 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260}
1261
David Howellscc53ce52011-01-14 18:45:26 +00001262int follow_down_one(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 struct vfsmount *mounted;
1265
Al Viro1c755af2009-04-18 14:06:57 -04001266 mounted = lookup_mnt(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (mounted) {
Al Viro9393bd02009-04-18 13:58:15 -04001268 dput(path->dentry);
1269 mntput(path->mnt);
1270 path->mnt = mounted;
1271 path->dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return 1;
1273 }
1274 return 0;
1275}
Al Viro4d359502014-03-14 12:20:17 -04001276EXPORT_SYMBOL(follow_down_one);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
NeilBrownb8faf032014-08-04 17:06:29 +10001278static inline int managed_dentry_rcu(struct dentry *dentry)
Ian Kent62a73752011-03-25 01:51:02 +08001279{
NeilBrownb8faf032014-08-04 17:06:29 +10001280 return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ?
1281 dentry->d_op->d_manage(dentry, true) : 0;
Ian Kent62a73752011-03-25 01:51:02 +08001282}
1283
David Howells9875cf82011-01-14 18:45:21 +00001284/*
Al Viro287548e2011-05-27 06:50:06 -04001285 * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if
1286 * we meet a managed dentry that would need blocking.
David Howells9875cf82011-01-14 18:45:21 +00001287 */
1288static bool __follow_mount_rcu(struct nameidata *nd, struct path *path,
Al Viro254cf582015-05-05 09:40:46 -04001289 struct inode **inode, unsigned *seqp)
David Howells9875cf82011-01-14 18:45:21 +00001290{
Ian Kent62a73752011-03-25 01:51:02 +08001291 for (;;) {
Al Viroc7105362011-11-24 18:22:03 -05001292 struct mount *mounted;
Ian Kent62a73752011-03-25 01:51:02 +08001293 /*
1294 * Don't forget we might have a non-mountpoint managed dentry
1295 * that wants to block transit.
1296 */
NeilBrownb8faf032014-08-04 17:06:29 +10001297 switch (managed_dentry_rcu(path->dentry)) {
1298 case -ECHILD:
1299 default:
David Howellsab909112011-01-14 18:46:51 +00001300 return false;
NeilBrownb8faf032014-08-04 17:06:29 +10001301 case -EISDIR:
1302 return true;
1303 case 0:
1304 break;
1305 }
Ian Kent62a73752011-03-25 01:51:02 +08001306
1307 if (!d_mountpoint(path->dentry))
NeilBrownb8faf032014-08-04 17:06:29 +10001308 return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Ian Kent62a73752011-03-25 01:51:02 +08001309
Al Viro474279d2013-10-01 16:11:26 -04001310 mounted = __lookup_mnt(path->mnt, path->dentry);
David Howells9875cf82011-01-14 18:45:21 +00001311 if (!mounted)
1312 break;
Al Viroc7105362011-11-24 18:22:03 -05001313 path->mnt = &mounted->mnt;
1314 path->dentry = mounted->mnt.mnt_root;
Al Viroa3fbbde2011-11-07 21:21:26 +00001315 nd->flags |= LOOKUP_JUMPED;
Al Viro254cf582015-05-05 09:40:46 -04001316 *seqp = read_seqcount_begin(&path->dentry->d_seq);
Linus Torvalds59430262011-07-18 15:43:29 -07001317 /*
1318 * Update the inode too. We don't need to re-check the
1319 * dentry sequence number here after this d_inode read,
1320 * because a mount-point is always pinned.
1321 */
1322 *inode = path->dentry->d_inode;
David Howells9875cf82011-01-14 18:45:21 +00001323 }
Al Virof5be3e29122014-09-13 21:50:45 -04001324 return !read_seqretry(&mount_lock, nd->m_seq) &&
NeilBrownb8faf032014-08-04 17:06:29 +10001325 !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT);
Al Viro287548e2011-05-27 06:50:06 -04001326}
1327
Nick Piggin31e6b012011-01-07 17:49:52 +11001328static int follow_dotdot_rcu(struct nameidata *nd)
1329{
Al Viro4023bfc2014-09-13 21:59:43 -04001330 struct inode *inode = nd->inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001331
David Howells9875cf82011-01-14 18:45:21 +00001332 while (1) {
Al Viroaed434a2015-05-12 12:22:47 -04001333 if (path_equal(&nd->path, &nd->root))
Nick Piggin31e6b012011-01-07 17:49:52 +11001334 break;
Nick Piggin31e6b012011-01-07 17:49:52 +11001335 if (nd->path.dentry != nd->path.mnt->mnt_root) {
1336 struct dentry *old = nd->path.dentry;
1337 struct dentry *parent = old->d_parent;
1338 unsigned seq;
1339
Al Viro4023bfc2014-09-13 21:59:43 -04001340 inode = parent->d_inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001341 seq = read_seqcount_begin(&parent->d_seq);
Al Viroaed434a2015-05-12 12:22:47 -04001342 if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq)))
1343 return -ECHILD;
Nick Piggin31e6b012011-01-07 17:49:52 +11001344 nd->path.dentry = parent;
1345 nd->seq = seq;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001346 if (unlikely(!path_connected(&nd->path)))
1347 return -ENOENT;
Nick Piggin31e6b012011-01-07 17:49:52 +11001348 break;
Al Viroaed434a2015-05-12 12:22:47 -04001349 } else {
1350 struct mount *mnt = real_mount(nd->path.mnt);
1351 struct mount *mparent = mnt->mnt_parent;
1352 struct dentry *mountpoint = mnt->mnt_mountpoint;
1353 struct inode *inode2 = mountpoint->d_inode;
1354 unsigned seq = read_seqcount_begin(&mountpoint->d_seq);
1355 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1356 return -ECHILD;
1357 if (&mparent->mnt == nd->path.mnt)
1358 break;
1359 /* we know that mountpoint was pinned */
1360 nd->path.dentry = mountpoint;
1361 nd->path.mnt = &mparent->mnt;
1362 inode = inode2;
1363 nd->seq = seq;
Nick Piggin31e6b012011-01-07 17:49:52 +11001364 }
Nick Piggin31e6b012011-01-07 17:49:52 +11001365 }
Al Viroaed434a2015-05-12 12:22:47 -04001366 while (unlikely(d_mountpoint(nd->path.dentry))) {
Al Virob37199e2014-03-20 15:18:22 -04001367 struct mount *mounted;
1368 mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry);
Al Viroaed434a2015-05-12 12:22:47 -04001369 if (unlikely(read_seqretry(&mount_lock, nd->m_seq)))
1370 return -ECHILD;
Al Virob37199e2014-03-20 15:18:22 -04001371 if (!mounted)
1372 break;
1373 nd->path.mnt = &mounted->mnt;
1374 nd->path.dentry = mounted->mnt.mnt_root;
Al Viro4023bfc2014-09-13 21:59:43 -04001375 inode = nd->path.dentry->d_inode;
Al Virob37199e2014-03-20 15:18:22 -04001376 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Virob37199e2014-03-20 15:18:22 -04001377 }
Al Viro4023bfc2014-09-13 21:59:43 -04001378 nd->inode = inode;
Nick Piggin31e6b012011-01-07 17:49:52 +11001379 return 0;
1380}
1381
David Howells9875cf82011-01-14 18:45:21 +00001382/*
David Howellscc53ce52011-01-14 18:45:26 +00001383 * Follow down to the covering mount currently visible to userspace. At each
1384 * point, the filesystem owning that dentry may be queried as to whether the
1385 * caller is permitted to proceed or not.
David Howellscc53ce52011-01-14 18:45:26 +00001386 */
Al Viro7cc90cc2011-03-18 09:04:20 -04001387int follow_down(struct path *path)
David Howellscc53ce52011-01-14 18:45:26 +00001388{
1389 unsigned managed;
1390 int ret;
1391
1392 while (managed = ACCESS_ONCE(path->dentry->d_flags),
1393 unlikely(managed & DCACHE_MANAGED_DENTRY)) {
1394 /* Allow the filesystem to manage the transit without i_mutex
1395 * being held.
1396 *
1397 * We indicate to the filesystem if someone is trying to mount
1398 * something here. This gives autofs the chance to deny anyone
1399 * other than its daemon the right to mount on its
1400 * superstructure.
1401 *
1402 * The filesystem may sleep at this point.
1403 */
1404 if (managed & DCACHE_MANAGE_TRANSIT) {
1405 BUG_ON(!path->dentry->d_op);
1406 BUG_ON(!path->dentry->d_op->d_manage);
David Howellsab909112011-01-14 18:46:51 +00001407 ret = path->dentry->d_op->d_manage(
Al Viro1aed3e42011-03-18 09:09:02 -04001408 path->dentry, false);
David Howellscc53ce52011-01-14 18:45:26 +00001409 if (ret < 0)
1410 return ret == -EISDIR ? 0 : ret;
1411 }
1412
1413 /* Transit to a mounted filesystem. */
1414 if (managed & DCACHE_MOUNTED) {
1415 struct vfsmount *mounted = lookup_mnt(path);
1416 if (!mounted)
1417 break;
1418 dput(path->dentry);
1419 mntput(path->mnt);
1420 path->mnt = mounted;
1421 path->dentry = dget(mounted->mnt_root);
1422 continue;
1423 }
1424
1425 /* Don't handle automount points here */
1426 break;
1427 }
1428 return 0;
1429}
Al Viro4d359502014-03-14 12:20:17 -04001430EXPORT_SYMBOL(follow_down);
David Howellscc53ce52011-01-14 18:45:26 +00001431
1432/*
David Howells9875cf82011-01-14 18:45:21 +00001433 * Skip to top of mountpoint pile in refwalk mode for follow_dotdot()
1434 */
1435static void follow_mount(struct path *path)
1436{
1437 while (d_mountpoint(path->dentry)) {
1438 struct vfsmount *mounted = lookup_mnt(path);
1439 if (!mounted)
1440 break;
1441 dput(path->dentry);
1442 mntput(path->mnt);
1443 path->mnt = mounted;
1444 path->dentry = dget(mounted->mnt_root);
1445 }
1446}
1447
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001448static int path_parent_directory(struct path *path)
1449{
1450 struct dentry *old = path->dentry;
1451 /* rare case of legitimate dget_parent()... */
1452 path->dentry = dget_parent(path->dentry);
1453 dput(old);
1454 if (unlikely(!path_connected(path)))
1455 return -ENOENT;
1456 return 0;
1457}
1458
Eric W. Biederman397d4252015-08-15 20:27:13 -05001459static int follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460{
1461 while(1) {
Al Viro2a737872009-04-07 11:49:53 -04001462 if (nd->path.dentry == nd->root.dentry &&
1463 nd->path.mnt == nd->root.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 break;
1465 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001466 if (nd->path.dentry != nd->path.mnt->mnt_root) {
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05001467 int ret = path_parent_directory(&nd->path);
1468 if (ret)
1469 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 break;
1471 }
Al Viro3088dd72010-01-30 15:47:29 -05001472 if (!follow_up(&nd->path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
Al Viro79ed0222009-04-18 13:59:41 -04001475 follow_mount(&nd->path);
Nick Piggin31e6b012011-01-07 17:49:52 +11001476 nd->inode = nd->path.dentry->d_inode;
Eric W. Biederman397d4252015-08-15 20:27:13 -05001477 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480/*
Oleg Drokinf4fdace2016-07-07 22:04:04 -04001481 * This looks up the name in dcache and possibly revalidates the found dentry.
1482 * NULL is returned if the dentry does not exist in the cache.
Nick Pigginbaa03892010-08-18 04:37:31 +10001483 */
Al Viroe3c13922016-03-06 14:03:27 -05001484static struct dentry *lookup_dcache(const struct qstr *name,
1485 struct dentry *dir,
Al Viro6c51e512016-03-05 20:09:32 -05001486 unsigned int flags)
Nick Pigginbaa03892010-08-18 04:37:31 +10001487{
Nick Pigginbaa03892010-08-18 04:37:31 +10001488 struct dentry *dentry;
Miklos Szeredibad61182012-03-26 12:54:24 +02001489 int error;
Nick Pigginbaa03892010-08-18 04:37:31 +10001490
Miklos Szeredibad61182012-03-26 12:54:24 +02001491 dentry = d_lookup(dir, name);
1492 if (dentry) {
Jeff Layton39e3c952012-11-28 11:30:53 -05001493 if (dentry->d_flags & DCACHE_OP_REVALIDATE) {
Al Viro201f9562012-06-22 12:42:10 +04001494 error = d_revalidate(dentry, flags);
Miklos Szeredibad61182012-03-26 12:54:24 +02001495 if (unlikely(error <= 0)) {
Al Viro74ff0ff2016-03-05 22:37:46 -05001496 if (!error)
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001497 d_invalidate(dentry);
Al Viro74ff0ff2016-03-05 22:37:46 -05001498 dput(dentry);
1499 return ERR_PTR(error);
Miklos Szeredibad61182012-03-26 12:54:24 +02001500 }
1501 }
1502 }
Nick Pigginbaa03892010-08-18 04:37:31 +10001503 return dentry;
1504}
1505
1506/*
J. Bruce Fields13a2c3b2013-10-23 16:09:16 -04001507 * Call i_op->lookup on the dentry. The dentry must be negative and
1508 * unhashed.
Miklos Szeredibad61182012-03-26 12:54:24 +02001509 *
1510 * dir->d_inode->i_mutex must be held
Josef Bacik44396f42011-05-31 11:58:49 -04001511 */
Miklos Szeredibad61182012-03-26 12:54:24 +02001512static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry,
Al Viro72bd8662012-06-10 17:17:17 -04001513 unsigned int flags)
Josef Bacik44396f42011-05-31 11:58:49 -04001514{
Josef Bacik44396f42011-05-31 11:58:49 -04001515 struct dentry *old;
1516
1517 /* Don't create child dentry for a dead directory. */
Miklos Szeredibad61182012-03-26 12:54:24 +02001518 if (unlikely(IS_DEADDIR(dir))) {
Miklos Szeredie188dc02012-02-03 14:25:18 +01001519 dput(dentry);
Josef Bacik44396f42011-05-31 11:58:49 -04001520 return ERR_PTR(-ENOENT);
Miklos Szeredie188dc02012-02-03 14:25:18 +01001521 }
Josef Bacik44396f42011-05-31 11:58:49 -04001522
Al Viro72bd8662012-06-10 17:17:17 -04001523 old = dir->i_op->lookup(dir, dentry, flags);
Josef Bacik44396f42011-05-31 11:58:49 -04001524 if (unlikely(old)) {
1525 dput(dentry);
1526 dentry = old;
1527 }
1528 return dentry;
1529}
1530
Al Viroe3c13922016-03-06 14:03:27 -05001531static struct dentry *__lookup_hash(const struct qstr *name,
Al Viro72bd8662012-06-10 17:17:17 -04001532 struct dentry *base, unsigned int flags)
Al Viroa3255542012-03-30 14:41:51 -04001533{
Al Viro6c51e512016-03-05 20:09:32 -05001534 struct dentry *dentry = lookup_dcache(name, base, flags);
Al Viroa3255542012-03-30 14:41:51 -04001535
Al Viro6c51e512016-03-05 20:09:32 -05001536 if (dentry)
Miklos Szeredibad61182012-03-26 12:54:24 +02001537 return dentry;
Al Viroa3255542012-03-30 14:41:51 -04001538
Al Viro6c51e512016-03-05 20:09:32 -05001539 dentry = d_alloc(base, name);
1540 if (unlikely(!dentry))
1541 return ERR_PTR(-ENOMEM);
1542
Al Viro72bd8662012-06-10 17:17:17 -04001543 return lookup_real(base->d_inode, dentry, flags);
Al Viroa3255542012-03-30 14:41:51 -04001544}
1545
Al Viroe97cdc82013-01-24 18:16:00 -05001546static int lookup_fast(struct nameidata *nd,
Al Viro254cf582015-05-05 09:40:46 -04001547 struct path *path, struct inode **inode,
1548 unsigned *seqp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
Jan Blunck4ac91372008-02-14 19:34:32 -08001550 struct vfsmount *mnt = nd->path.mnt;
Nick Piggin31e6b012011-01-07 17:49:52 +11001551 struct dentry *dentry, *parent = nd->path.dentry;
Al Viro5a18fff2011-03-11 04:44:53 -05001552 int status = 1;
David Howells9875cf82011-01-14 18:45:21 +00001553 int err;
1554
Al Viro3cac2602009-08-13 18:27:43 +04001555 /*
Nick Pigginb04f7842010-08-18 04:37:34 +10001556 * Rename seqlock is not required here because in the off chance
Al Viro5d0f49c2016-03-05 21:32:53 -05001557 * of a false negative due to a concurrent rename, the caller is
1558 * going to fall back to non-racy lookup.
Nick Pigginb04f7842010-08-18 04:37:34 +10001559 */
Nick Piggin31e6b012011-01-07 17:49:52 +11001560 if (nd->flags & LOOKUP_RCU) {
1561 unsigned seq;
Al Viro766c4cb2015-05-07 19:24:57 -04001562 bool negative;
Linus Torvaldsda53be12013-05-21 15:22:44 -07001563 dentry = __d_lookup_rcu(parent, &nd->last, &seq);
Al Viro5d0f49c2016-03-05 21:32:53 -05001564 if (unlikely(!dentry)) {
1565 if (unlazy_walk(nd, NULL, 0))
1566 return -ECHILD;
Al Viroe9742b52016-03-05 22:04:59 -05001567 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001568 }
Al Viro5a18fff2011-03-11 04:44:53 -05001569
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001570 /*
1571 * This sequence count validates that the inode matches
1572 * the dentry name information from lookup.
1573 */
David Howells63afdfc2015-05-06 15:59:00 +01001574 *inode = d_backing_inode(dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04001575 negative = d_is_negative(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001576 if (unlikely(read_seqcount_retry(&dentry->d_seq, seq)))
Linus Torvalds12f8ad42012-05-04 14:59:14 -07001577 return -ECHILD;
1578
1579 /*
1580 * This sequence count validates that the parent had no
1581 * changes while we did the lookup of the dentry above.
1582 *
1583 * The memory barrier in read_seqcount_begin of child is
1584 * enough, we can use __read_seqcount_retry here.
1585 */
Al Viro5d0f49c2016-03-05 21:32:53 -05001586 if (unlikely(__read_seqcount_retry(&parent->d_seq, nd->seq)))
Nick Piggin31e6b012011-01-07 17:49:52 +11001587 return -ECHILD;
Al Viro5a18fff2011-03-11 04:44:53 -05001588
Al Viro254cf582015-05-05 09:40:46 -04001589 *seqp = seq;
Al Viro5d0f49c2016-03-05 21:32:53 -05001590 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
Al Viro4ce16ef32012-06-10 16:10:59 -04001591 status = d_revalidate(dentry, nd->flags);
Al Viro5d0f49c2016-03-05 21:32:53 -05001592 if (unlikely(status <= 0)) {
1593 if (unlazy_walk(nd, dentry, seq))
1594 return -ECHILD;
1595 if (status == -ECHILD)
1596 status = d_revalidate(dentry, nd->flags);
1597 } else {
1598 /*
1599 * Note: do negative dentry check after revalidation in
1600 * case that drops it.
1601 */
1602 if (unlikely(negative))
1603 return -ENOENT;
1604 path->mnt = mnt;
1605 path->dentry = dentry;
1606 if (likely(__follow_mount_rcu(nd, path, inode, seqp)))
Al Viroe9742b52016-03-05 22:04:59 -05001607 return 1;
Al Viro5d0f49c2016-03-05 21:32:53 -05001608 if (unlazy_walk(nd, dentry, seq))
1609 return -ECHILD;
Al Viro24643082011-02-15 01:26:22 -05001610 }
Al Viro5a18fff2011-03-11 04:44:53 -05001611 } else {
Al Viroe97cdc82013-01-24 18:16:00 -05001612 dentry = __d_lookup(parent, &nd->last);
Al Viro5d0f49c2016-03-05 21:32:53 -05001613 if (unlikely(!dentry))
Al Viroe9742b52016-03-05 22:04:59 -05001614 return 0;
Al Viro5d0f49c2016-03-05 21:32:53 -05001615 if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE))
1616 status = d_revalidate(dentry, nd->flags);
Nick Piggin31e6b012011-01-07 17:49:52 +11001617 }
Al Viro5a18fff2011-03-11 04:44:53 -05001618 if (unlikely(status <= 0)) {
Al Viroe9742b52016-03-05 22:04:59 -05001619 if (!status)
Al Viro5d0f49c2016-03-05 21:32:53 -05001620 d_invalidate(dentry);
Eric W. Biederman5542aa22014-02-13 09:46:25 -08001621 dput(dentry);
Al Viro5d0f49c2016-03-05 21:32:53 -05001622 return status;
Al Viro5a18fff2011-03-11 04:44:53 -05001623 }
Al Viro766c4cb2015-05-07 19:24:57 -04001624 if (unlikely(d_is_negative(dentry))) {
1625 dput(dentry);
1626 return -ENOENT;
1627 }
Al Viro5d0f49c2016-03-05 21:32:53 -05001628
David Howells9875cf82011-01-14 18:45:21 +00001629 path->mnt = mnt;
1630 path->dentry = dentry;
NeilBrown756daf22015-03-23 13:37:38 +11001631 err = follow_managed(path, nd);
Al Viroe9742b52016-03-05 22:04:59 -05001632 if (likely(err > 0))
David Howells63afdfc2015-05-06 15:59:00 +01001633 *inode = d_backing_inode(path->dentry);
Al Viro84027522015-04-22 10:30:08 -04001634 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001635}
1636
1637/* Fast lookup failed, do it the slow way */
Al Viroe3c13922016-03-06 14:03:27 -05001638static struct dentry *lookup_slow(const struct qstr *name,
1639 struct dentry *dir,
1640 unsigned int flags)
Miklos Szeredi697f5142012-05-21 17:30:05 +02001641{
Al Viro94bdd652016-04-15 02:42:04 -04001642 struct dentry *dentry = ERR_PTR(-ENOENT), *old;
Al Viro19363862016-04-14 19:33:34 -04001643 struct inode *inode = dir->d_inode;
Al Virod9171b92016-04-15 03:33:13 -04001644 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Al Viro19363862016-04-14 19:33:34 -04001645
Al Viro9902af72016-04-15 15:08:36 -04001646 inode_lock_shared(inode);
Al Viro19363862016-04-14 19:33:34 -04001647 /* Don't go there if it's already dead */
Al Viro94bdd652016-04-15 02:42:04 -04001648 if (unlikely(IS_DEADDIR(inode)))
1649 goto out;
1650again:
Al Virod9171b92016-04-15 03:33:13 -04001651 dentry = d_alloc_parallel(dir, name, &wq);
Al Viro94bdd652016-04-15 02:42:04 -04001652 if (IS_ERR(dentry))
1653 goto out;
1654 if (unlikely(!d_in_lookup(dentry))) {
Al Viro949a8522016-03-06 14:20:52 -05001655 if ((dentry->d_flags & DCACHE_OP_REVALIDATE) &&
1656 !(flags & LOOKUP_NO_REVAL)) {
1657 int error = d_revalidate(dentry, flags);
1658 if (unlikely(error <= 0)) {
Al Viro94bdd652016-04-15 02:42:04 -04001659 if (!error) {
Al Viro949a8522016-03-06 14:20:52 -05001660 d_invalidate(dentry);
Al Viro94bdd652016-04-15 02:42:04 -04001661 dput(dentry);
1662 goto again;
1663 }
Al Viro949a8522016-03-06 14:20:52 -05001664 dput(dentry);
1665 dentry = ERR_PTR(error);
1666 }
1667 }
Al Viro94bdd652016-04-15 02:42:04 -04001668 } else {
1669 old = inode->i_op->lookup(inode, dentry, flags);
1670 d_lookup_done(dentry);
1671 if (unlikely(old)) {
1672 dput(dentry);
1673 dentry = old;
Al Viro949a8522016-03-06 14:20:52 -05001674 }
1675 }
Al Viro94bdd652016-04-15 02:42:04 -04001676out:
Al Viro9902af72016-04-15 15:08:36 -04001677 inode_unlock_shared(inode);
Al Viroe3c13922016-03-06 14:03:27 -05001678 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
Al Viro52094c82011-02-21 21:34:47 -05001681static inline int may_lookup(struct nameidata *nd)
1682{
1683 if (nd->flags & LOOKUP_RCU) {
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07001684 int err = inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC|MAY_NOT_BLOCK);
Al Viro52094c82011-02-21 21:34:47 -05001685 if (err != -ECHILD)
1686 return err;
Al Viro6e9918b2015-05-05 09:26:05 -04001687 if (unlazy_walk(nd, NULL, 0))
Al Viro52094c82011-02-21 21:34:47 -05001688 return -ECHILD;
1689 }
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07001690 return inode_permission2(nd->path.mnt, nd->inode, MAY_EXEC);
Al Viro52094c82011-02-21 21:34:47 -05001691}
1692
Al Viro9856fa12011-03-04 14:22:06 -05001693static inline int handle_dots(struct nameidata *nd, int type)
1694{
1695 if (type == LAST_DOTDOT) {
Al Viro9e6697e2015-12-05 20:07:21 -05001696 if (!nd->root.mnt)
1697 set_root(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001698 if (nd->flags & LOOKUP_RCU) {
Al Viro70291ae2015-05-04 07:53:00 -04001699 return follow_dotdot_rcu(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001700 } else
Eric W. Biederman397d4252015-08-15 20:27:13 -05001701 return follow_dotdot(nd);
Al Viro9856fa12011-03-04 14:22:06 -05001702 }
1703 return 0;
1704}
1705
Al Viro181548c2015-05-07 19:54:34 -04001706static int pick_link(struct nameidata *nd, struct path *link,
1707 struct inode *inode, unsigned seq)
Al Virod63ff282015-05-04 18:13:23 -04001708{
Al Viro626de992015-05-04 18:26:59 -04001709 int error;
Al Viro1cf26652015-05-06 16:01:56 -04001710 struct saved *last;
NeilBrown756daf22015-03-23 13:37:38 +11001711 if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) {
Al Viro626de992015-05-04 18:26:59 -04001712 path_to_nameidata(link, nd);
1713 return -ELOOP;
1714 }
Al Virobc40aee2015-05-09 13:04:24 -04001715 if (!(nd->flags & LOOKUP_RCU)) {
Al Viro79733872015-05-09 12:55:43 -04001716 if (link->mnt == nd->path.mnt)
1717 mntget(link->mnt);
Al Virod63ff282015-05-04 18:13:23 -04001718 }
Al Viro626de992015-05-04 18:26:59 -04001719 error = nd_alloc_stack(nd);
1720 if (unlikely(error)) {
Al Virobc40aee2015-05-09 13:04:24 -04001721 if (error == -ECHILD) {
1722 if (unlikely(unlazy_link(nd, link, seq)))
1723 return -ECHILD;
1724 error = nd_alloc_stack(nd);
1725 }
1726 if (error) {
1727 path_put(link);
1728 return error;
1729 }
Al Viro626de992015-05-04 18:26:59 -04001730 }
1731
Al Viroab104922015-05-10 11:50:01 -04001732 last = nd->stack + nd->depth++;
Al Viro1cf26652015-05-06 16:01:56 -04001733 last->link = *link;
Al Virofceef392015-12-29 15:58:39 -05001734 clear_delayed_call(&last->done);
1735 nd->link_inode = inode;
Al Viro0450b2d2015-05-08 13:23:53 -04001736 last->seq = seq;
Al Virod63ff282015-05-04 18:13:23 -04001737 return 1;
1738}
1739
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001740/*
1741 * Do we need to follow links? We _really_ want to be able
1742 * to do this check without having to look at inode->i_op,
1743 * so we keep a cache of "no, this doesn't need follow_link"
1744 * for the common case.
1745 */
Al Viro254cf582015-05-05 09:40:46 -04001746static inline int should_follow_link(struct nameidata *nd, struct path *link,
Al Viro181548c2015-05-07 19:54:34 -04001747 int follow,
1748 struct inode *inode, unsigned seq)
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001749{
Al Virod63ff282015-05-04 18:13:23 -04001750 if (likely(!d_is_symlink(link->dentry)))
1751 return 0;
1752 if (!follow)
1753 return 0;
Al Viroa7f77542016-02-27 19:31:01 -05001754 /* make sure that d_is_symlink above matches inode */
1755 if (nd->flags & LOOKUP_RCU) {
1756 if (read_seqcount_retry(&link->dentry->d_seq, seq))
1757 return -ECHILD;
1758 }
Al Viro181548c2015-05-07 19:54:34 -04001759 return pick_link(nd, link, inode, seq);
Linus Torvalds3ddcd052011-08-06 22:45:50 -07001760}
1761
Al Viro4693a542015-05-04 17:47:11 -04001762enum {WALK_GET = 1, WALK_PUT = 2};
1763
1764static int walk_component(struct nameidata *nd, int flags)
Al Viroce57dfc2011-03-13 19:58:58 -04001765{
Al Virocaa85632015-04-22 17:52:47 -04001766 struct path path;
Al Viroce57dfc2011-03-13 19:58:58 -04001767 struct inode *inode;
Al Viro254cf582015-05-05 09:40:46 -04001768 unsigned seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001769 int err;
1770 /*
1771 * "." and ".." are special - ".." especially so because it has
1772 * to be able to know about the current root directory and
1773 * parent relationships.
1774 */
Al Viro4693a542015-05-04 17:47:11 -04001775 if (unlikely(nd->last_type != LAST_NORM)) {
1776 err = handle_dots(nd, nd->last_type);
1777 if (flags & WALK_PUT)
1778 put_link(nd);
1779 return err;
1780 }
Al Viro254cf582015-05-05 09:40:46 -04001781 err = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05001782 if (unlikely(err <= 0)) {
Miklos Szeredi697f5142012-05-21 17:30:05 +02001783 if (err < 0)
Al Virof0a9ba72015-05-04 07:59:30 -04001784 return err;
Al Viroe3c13922016-03-06 14:03:27 -05001785 path.dentry = lookup_slow(&nd->last, nd->path.dentry,
1786 nd->flags);
1787 if (IS_ERR(path.dentry))
1788 return PTR_ERR(path.dentry);
Al Viro7500c382016-03-31 00:23:05 -04001789
Al Viroe3c13922016-03-06 14:03:27 -05001790 path.mnt = nd->path.mnt;
1791 err = follow_managed(&path, nd);
1792 if (unlikely(err < 0))
Al Virof0a9ba72015-05-04 07:59:30 -04001793 return err;
Miklos Szeredi697f5142012-05-21 17:30:05 +02001794
Al Viro7500c382016-03-31 00:23:05 -04001795 if (unlikely(d_is_negative(path.dentry))) {
1796 path_to_nameidata(&path, nd);
1797 return -ENOENT;
1798 }
1799
Al Viro254cf582015-05-05 09:40:46 -04001800 seq = 0; /* we are already out of RCU mode */
Al Virod4565642016-02-27 19:23:16 -05001801 inode = d_backing_inode(path.dentry);
Al Viroce57dfc2011-03-13 19:58:58 -04001802 }
Miklos Szeredi697f5142012-05-21 17:30:05 +02001803
Al Viro4693a542015-05-04 17:47:11 -04001804 if (flags & WALK_PUT)
1805 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04001806 err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq);
Al Virod63ff282015-05-04 18:13:23 -04001807 if (unlikely(err))
1808 return err;
Al Virocaa85632015-04-22 17:52:47 -04001809 path_to_nameidata(&path, nd);
Al Viroce57dfc2011-03-13 19:58:58 -04001810 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04001811 nd->seq = seq;
Al Viroce57dfc2011-03-13 19:58:58 -04001812 return 0;
1813}
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815/*
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001816 * We can do the critical dentry name comparison and hashing
1817 * operations one word at a time, but we are limited to:
1818 *
1819 * - Architectures with fast unaligned word accesses. We could
1820 * do a "get_unaligned()" if this helps and is sufficiently
1821 * fast.
1822 *
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001823 * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we
1824 * do not trap on the (extremely unlikely) case of a page
1825 * crossing operation.
1826 *
1827 * - Furthermore, we need an efficient 64-bit compile for the
1828 * 64-bit case in order to generate the "number of bytes in
1829 * the final mask". Again, that could be replaced with a
1830 * efficient population count instruction or similar.
1831 */
1832#ifdef CONFIG_DCACHE_WORD_ACCESS
1833
Linus Torvaldsf68e5562012-04-06 13:54:56 -07001834#include <asm/word-at-a-time.h>
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001835
George Spelvin468a9422016-05-26 22:11:51 -04001836#ifdef HASH_MIX
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001837
George Spelvin468a9422016-05-26 22:11:51 -04001838/* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */
1839
1840#elif defined(CONFIG_64BIT)
George Spelvin2a18da7a2016-05-23 07:43:58 -04001841/*
1842 * Register pressure in the mixing function is an issue, particularly
1843 * on 32-bit x86, but almost any function requires one state value and
1844 * one temporary. Instead, use a function designed for two state values
1845 * and no temporaries.
1846 *
1847 * This function cannot create a collision in only two iterations, so
1848 * we have two iterations to achieve avalanche. In those two iterations,
1849 * we have six layers of mixing, which is enough to spread one bit's
1850 * influence out to 2^6 = 64 state bits.
1851 *
1852 * Rotate constants are scored by considering either 64 one-bit input
1853 * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the
1854 * probability of that delta causing a change to each of the 128 output
1855 * bits, using a sample of random initial states.
1856 *
1857 * The Shannon entropy of the computed probabilities is then summed
1858 * to produce a score. Ideally, any input change has a 50% chance of
1859 * toggling any given output bit.
1860 *
1861 * Mixing scores (in bits) for (12,45):
1862 * Input delta: 1-bit 2-bit
1863 * 1 round: 713.3 42542.6
1864 * 2 rounds: 2753.7 140389.8
1865 * 3 rounds: 5954.1 233458.2
1866 * 4 rounds: 7862.6 256672.2
1867 * Perfect: 8192 258048
1868 * (64*128) (64*63/2 * 128)
1869 */
1870#define HASH_MIX(x, y, a) \
1871 ( x ^= (a), \
1872 y ^= x, x = rol64(x,12),\
1873 x += y, y = rol64(y,45),\
1874 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001875
George Spelvin0fed3ac2016-05-02 06:31:01 -04001876/*
George Spelvin2a18da7a2016-05-23 07:43:58 -04001877 * Fold two longs into one 32-bit hash value. This must be fast, but
1878 * latency isn't quite as critical, as there is a fair bit of additional
1879 * work done before the hash value is used.
George Spelvin0fed3ac2016-05-02 06:31:01 -04001880 */
George Spelvin2a18da7a2016-05-23 07:43:58 -04001881static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001882{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001883 y ^= x * GOLDEN_RATIO_64;
1884 y *= GOLDEN_RATIO_64;
1885 return y >> 32;
George Spelvin0fed3ac2016-05-02 06:31:01 -04001886}
1887
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001888#else /* 32-bit case */
1889
George Spelvin2a18da7a2016-05-23 07:43:58 -04001890/*
1891 * Mixing scores (in bits) for (7,20):
1892 * Input delta: 1-bit 2-bit
1893 * 1 round: 330.3 9201.6
1894 * 2 rounds: 1246.4 25475.4
1895 * 3 rounds: 1907.1 31295.1
1896 * 4 rounds: 2042.3 31718.6
1897 * Perfect: 2048 31744
1898 * (32*64) (32*31/2 * 64)
1899 */
1900#define HASH_MIX(x, y, a) \
1901 ( x ^= (a), \
1902 y ^= x, x = rol32(x, 7),\
1903 x += y, y = rol32(y,20),\
1904 y *= 9 )
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001905
George Spelvin2a18da7a2016-05-23 07:43:58 -04001906static inline unsigned int fold_hash(unsigned long x, unsigned long y)
George Spelvin0fed3ac2016-05-02 06:31:01 -04001907{
George Spelvin2a18da7a2016-05-23 07:43:58 -04001908 /* Use arch-optimized multiply if one exists */
1909 return __hash_32(y ^ __hash_32(x));
George Spelvin0fed3ac2016-05-02 06:31:01 -04001910}
1911
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001912#endif
1913
George Spelvin2a18da7a2016-05-23 07:43:58 -04001914/*
1915 * Return the hash of a string of known length. This is carfully
1916 * designed to match hash_name(), which is the more critical function.
1917 * In particular, we must end by hashing a final word containing 0..7
1918 * payload bytes, to match the way that hash_name() iterates until it
1919 * finds the delimiter after the name.
1920 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001921unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001922{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001923 unsigned long a, x = 0, y = (unsigned long)salt;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001924
1925 for (;;) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001926 if (!len)
1927 goto done;
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001928 a = load_unaligned_zeropad(name);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001929 if (len < sizeof(unsigned long))
1930 break;
George Spelvin2a18da7a2016-05-23 07:43:58 -04001931 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001932 name += sizeof(unsigned long);
1933 len -= sizeof(unsigned long);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001934 }
George Spelvin2a18da7a2016-05-23 07:43:58 -04001935 x ^= a & bytemask_from_count(len);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001936done:
George Spelvin2a18da7a2016-05-23 07:43:58 -04001937 return fold_hash(x, y);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001938}
1939EXPORT_SYMBOL(full_name_hash);
1940
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001941/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001942u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001943{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001944 unsigned long a = 0, x = 0, y = (unsigned long)salt;
1945 unsigned long adata, mask, len;
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001946 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
1947
Linus Torvalds8387ff22016-06-10 07:51:30 -07001948 len = 0;
1949 goto inside;
1950
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001951 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001952 HASH_MIX(x, y, a);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001953 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001954inside:
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001955 a = load_unaligned_zeropad(name+len);
1956 } while (!has_zero(a, &adata, &constants));
1957
1958 adata = prep_zero_mask(a, adata, &constants);
1959 mask = create_zero_mask(adata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001960 x ^= a & zero_bytemask(mask);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001961
George Spelvin2a18da7a2016-05-23 07:43:58 -04001962 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001963}
1964EXPORT_SYMBOL(hashlen_string);
1965
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001966/*
1967 * Calculate the length and hash of the path component, and
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07001968 * return the "hash_len" as the result.
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001969 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001970static inline u64 hash_name(const void *salt, const char *name)
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001971{
Linus Torvalds8387ff22016-06-10 07:51:30 -07001972 unsigned long a = 0, b, x = 0, y = (unsigned long)salt;
1973 unsigned long adata, bdata, mask, len;
Linus Torvalds36126f82012-05-26 10:43:17 -07001974 const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS;
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001975
Linus Torvalds8387ff22016-06-10 07:51:30 -07001976 len = 0;
1977 goto inside;
1978
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001979 do {
George Spelvin2a18da7a2016-05-23 07:43:58 -04001980 HASH_MIX(x, y, a);
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001981 len += sizeof(unsigned long);
Linus Torvalds8387ff22016-06-10 07:51:30 -07001982inside:
Linus Torvaldse419b4c2012-05-03 10:16:43 -07001983 a = load_unaligned_zeropad(name+len);
Linus Torvalds36126f82012-05-26 10:43:17 -07001984 b = a ^ REPEAT_BYTE('/');
1985 } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001986
Linus Torvalds36126f82012-05-26 10:43:17 -07001987 adata = prep_zero_mask(a, adata, &constants);
1988 bdata = prep_zero_mask(b, bdata, &constants);
Linus Torvalds36126f82012-05-26 10:43:17 -07001989 mask = create_zero_mask(adata | bdata);
George Spelvin2a18da7a2016-05-23 07:43:58 -04001990 x ^= a & zero_bytemask(mask);
Linus Torvalds36126f82012-05-26 10:43:17 -07001991
George Spelvin2a18da7a2016-05-23 07:43:58 -04001992 return hashlen_create(fold_hash(x, y), len + find_zero(mask));
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001993}
1994
George Spelvin2a18da7a2016-05-23 07:43:58 -04001995#else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08001996
George Spelvinfcfd2fb2016-05-20 08:41:37 -04001997/* Return the hash of a string of known length */
Linus Torvalds8387ff22016-06-10 07:51:30 -07001998unsigned int full_name_hash(const void *salt, const char *name, unsigned int len)
Linus Torvalds0145acc2012-03-02 14:32:59 -08001999{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002000 unsigned long hash = init_name_hash(salt);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002001 while (len--)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002002 hash = partial_name_hash((unsigned char)*name++, hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002003 return end_name_hash(hash);
2004}
Linus Torvaldsae942ae2012-03-02 19:40:57 -08002005EXPORT_SYMBOL(full_name_hash);
Linus Torvalds0145acc2012-03-02 14:32:59 -08002006
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002007/* Return the "hash_len" (hash and length) of a null-terminated string */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002008u64 hashlen_string(const void *salt, const char *name)
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002009{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002010 unsigned long hash = init_name_hash(salt);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002011 unsigned long len = 0, c;
2012
2013 c = (unsigned char)*name;
George Spelvine0ab7af2016-05-29 08:05:56 -04002014 while (c) {
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002015 len++;
2016 hash = partial_name_hash(c, hash);
2017 c = (unsigned char)name[len];
George Spelvine0ab7af2016-05-29 08:05:56 -04002018 }
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002019 return hashlen_create(end_name_hash(hash), len);
2020}
George Spelvinf2a031b2016-05-29 01:26:41 -04002021EXPORT_SYMBOL(hashlen_string);
George Spelvinfcfd2fb2016-05-20 08:41:37 -04002022
Linus Torvalds3ddcd052011-08-06 22:45:50 -07002023/*
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002024 * We know there's a real path component here of at least
2025 * one character.
2026 */
Linus Torvalds8387ff22016-06-10 07:51:30 -07002027static inline u64 hash_name(const void *salt, const char *name)
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002028{
Linus Torvalds8387ff22016-06-10 07:51:30 -07002029 unsigned long hash = init_name_hash(salt);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002030 unsigned long len = 0, c;
2031
2032 c = (unsigned char)*name;
2033 do {
2034 len++;
2035 hash = partial_name_hash(c, hash);
2036 c = (unsigned char)name[len];
2037 } while (c && c != '/');
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002038 return hashlen_create(end_name_hash(hash), len);
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002039}
2040
Linus Torvaldsbfcfaa72012-03-06 11:16:17 -08002041#endif
2042
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002043/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +01002045 * This is the basic name resolution function, turning a pathname into
2046 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 *
Prasanna Medaea3834d2005-04-29 16:00:17 +01002048 * Returns 0 and nd will have valid dentry and mnt on success.
2049 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 */
Al Viro6de88d72009-08-09 01:41:57 +04002051static int link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 int err;
Al Viro32cd7462015-04-18 20:30:49 -04002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 while (*name=='/')
2056 name++;
2057 if (!*name)
Al Viro9e18f102015-04-18 20:44:34 -04002058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 /* At this point we know we have a real path component. */
2061 for(;;) {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002062 u64 hash_len;
Al Virofe479a52011-02-22 15:10:03 -05002063 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Al Viro52094c82011-02-21 21:34:47 -05002065 err = may_lookup(nd);
George Spelvin2a18da7a2016-05-23 07:43:58 -04002066 if (err)
Al Viro3595e232015-05-09 16:54:45 -04002067 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Linus Torvalds8387ff22016-06-10 07:51:30 -07002069 hash_len = hash_name(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Al Virofe479a52011-02-22 15:10:03 -05002071 type = LAST_NORM;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002072 if (name[0] == '.') switch (hashlen_len(hash_len)) {
Al Virofe479a52011-02-22 15:10:03 -05002073 case 2:
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002074 if (name[1] == '.') {
Al Virofe479a52011-02-22 15:10:03 -05002075 type = LAST_DOTDOT;
Al Viro16c2cd72011-02-22 15:50:10 -05002076 nd->flags |= LOOKUP_JUMPED;
2077 }
Al Virofe479a52011-02-22 15:10:03 -05002078 break;
2079 case 1:
2080 type = LAST_DOT;
2081 }
Al Viro5a202bc2011-03-08 14:17:44 -05002082 if (likely(type == LAST_NORM)) {
2083 struct dentry *parent = nd->path.dentry;
Al Viro16c2cd72011-02-22 15:50:10 -05002084 nd->flags &= ~LOOKUP_JUMPED;
Al Viro5a202bc2011-03-08 14:17:44 -05002085 if (unlikely(parent->d_flags & DCACHE_OP_HASH)) {
James Hogana060dc52014-09-16 13:07:35 +01002086 struct qstr this = { { .hash_len = hash_len }, .name = name };
Linus Torvaldsda53be12013-05-21 15:22:44 -07002087 err = parent->d_op->d_hash(parent, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002088 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002089 return err;
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002090 hash_len = this.hash_len;
2091 name = this.name;
Al Viro5a202bc2011-03-08 14:17:44 -05002092 }
2093 }
Al Virofe479a52011-02-22 15:10:03 -05002094
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002095 nd->last.hash_len = hash_len;
2096 nd->last.name = name;
Al Viro5f4a6a62013-01-24 18:04:22 -05002097 nd->last_type = type;
2098
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002099 name += hashlen_len(hash_len);
2100 if (!*name)
Al Virobdf6cbf2015-04-18 20:21:40 -04002101 goto OK;
Linus Torvalds200e9ef2012-03-02 14:49:24 -08002102 /*
2103 * If it wasn't NUL, we know it was '/'. Skip that
2104 * slash, and continue until no more slashes.
2105 */
2106 do {
Linus Torvaldsd6bb3e92014-09-15 10:51:07 -07002107 name++;
2108 } while (unlikely(*name == '/'));
Al Viro8620c232015-05-04 08:58:35 -04002109 if (unlikely(!*name)) {
2110OK:
Al Viro368ee9b2015-05-08 17:19:59 -04002111 /* pathname body, done */
Al Viro8620c232015-05-04 08:58:35 -04002112 if (!nd->depth)
2113 return 0;
2114 name = nd->stack[nd->depth - 1].name;
Al Viro368ee9b2015-05-08 17:19:59 -04002115 /* trailing symlink, done */
Al Viro8620c232015-05-04 08:58:35 -04002116 if (!name)
2117 return 0;
2118 /* last component of nested symlink */
Al Viro4693a542015-05-04 17:47:11 -04002119 err = walk_component(nd, WALK_GET | WALK_PUT);
Al Viro8620c232015-05-04 08:58:35 -04002120 } else {
Al Viro4693a542015-05-04 17:47:11 -04002121 err = walk_component(nd, WALK_GET);
Al Viro8620c232015-05-04 08:58:35 -04002122 }
Al Viroce57dfc2011-03-13 19:58:58 -04002123 if (err < 0)
Al Viro3595e232015-05-09 16:54:45 -04002124 return err;
Al Virofe479a52011-02-22 15:10:03 -05002125
Al Viroce57dfc2011-03-13 19:58:58 -04002126 if (err) {
Al Viro626de992015-05-04 18:26:59 -04002127 const char *s = get_link(nd);
Al Viro5a460272015-04-17 23:44:45 -04002128
Viresh Kumara1c83682015-08-12 15:59:44 +05302129 if (IS_ERR(s))
Al Viro3595e232015-05-09 16:54:45 -04002130 return PTR_ERR(s);
Al Virod40bcc02015-04-18 20:03:03 -04002131 err = 0;
2132 if (unlikely(!s)) {
2133 /* jumped */
Al Virob9ff4422015-05-02 20:19:23 -04002134 put_link(nd);
Al Virod40bcc02015-04-18 20:03:03 -04002135 } else {
Al Virofab51e82015-05-10 11:01:00 -04002136 nd->stack[nd->depth - 1].name = name;
2137 name = s;
2138 continue;
Al Virod40bcc02015-04-18 20:03:03 -04002139 }
Nick Piggin31e6b012011-01-07 17:49:52 +11002140 }
Al Viro97242f92015-08-01 19:59:28 -04002141 if (unlikely(!d_can_lookup(nd->path.dentry))) {
2142 if (nd->flags & LOOKUP_RCU) {
2143 if (unlazy_walk(nd, NULL, 0))
2144 return -ECHILD;
2145 }
Al Viro3595e232015-05-09 16:54:45 -04002146 return -ENOTDIR;
Al Viro97242f92015-08-01 19:59:28 -04002147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149}
2150
Al Viroc8a53ee2015-05-12 18:43:07 -04002151static const char *path_init(struct nameidata *nd, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
Prasanna Medaea3834d2005-04-29 16:00:17 +01002153 int retval = 0;
Al Viroc8a53ee2015-05-12 18:43:07 -04002154 const char *s = nd->name->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Linus Torvalds012e79b2017-04-02 17:10:08 -07002156 if (!*s)
2157 flags &= ~LOOKUP_RCU;
2158
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 nd->last_type = LAST_ROOT; /* if there are only slashes... */
Al Viro980f3ea2014-11-20 14:20:24 -05002160 nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 nd->depth = 0;
Al Viro5b6ca022011-03-09 23:04:47 -05002162 if (flags & LOOKUP_ROOT) {
David Howellsb18825a2013-09-12 19:22:53 +01002163 struct dentry *root = nd->root.dentry;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002164 struct vfsmount *mnt = nd->root.mnt;
David Howellsb18825a2013-09-12 19:22:53 +01002165 struct inode *inode = root->d_inode;
Al Virofd2f7cb2015-02-22 20:07:13 -05002166 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002167 if (!d_can_lookup(root))
Al Viro368ee9b2015-05-08 17:19:59 -04002168 return ERR_PTR(-ENOTDIR);
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002169 retval = inode_permission2(mnt, inode, MAY_EXEC);
Al Viro73d049a2011-03-11 12:08:24 -05002170 if (retval)
Al Viro368ee9b2015-05-08 17:19:59 -04002171 return ERR_PTR(retval);
Al Viro73d049a2011-03-11 12:08:24 -05002172 }
Al Viro5b6ca022011-03-09 23:04:47 -05002173 nd->path = nd->root;
2174 nd->inode = inode;
2175 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002176 rcu_read_lock();
Al Viro5b6ca022011-03-09 23:04:47 -05002177 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viro8f47a0162015-05-09 19:02:01 -04002178 nd->root_seq = nd->seq;
Al Viro48a066e2013-09-29 22:06:07 -04002179 nd->m_seq = read_seqbegin(&mount_lock);
Al Viro5b6ca022011-03-09 23:04:47 -05002180 } else {
2181 path_get(&nd->path);
2182 }
Al Viro368ee9b2015-05-08 17:19:59 -04002183 return s;
Al Viro5b6ca022011-03-09 23:04:47 -05002184 }
2185
Al Viro2a737872009-04-07 11:49:53 -04002186 nd->root.mnt = NULL;
Al Viro248fb5b2015-12-05 20:51:58 -05002187 nd->path.mnt = NULL;
2188 nd->path.dentry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Al Viro48a066e2013-09-29 22:06:07 -04002190 nd->m_seq = read_seqbegin(&mount_lock);
Al Virofd2f7cb2015-02-22 20:07:13 -05002191 if (*s == '/') {
Al Viro9e6697e2015-12-05 20:07:21 -05002192 if (flags & LOOKUP_RCU)
Al Viro8b61e742013-11-08 12:45:01 -05002193 rcu_read_lock();
Al Viro9e6697e2015-12-05 20:07:21 -05002194 set_root(nd);
Al Viro248fb5b2015-12-05 20:51:58 -05002195 if (likely(!nd_jump_root(nd)))
Al Viroef55d912015-12-05 20:25:06 -05002196 return s;
Al Viro248fb5b2015-12-05 20:51:58 -05002197 nd->root.mnt = NULL;
Al Viroef55d912015-12-05 20:25:06 -05002198 rcu_read_unlock();
2199 return ERR_PTR(-ECHILD);
Al Viroc8a53ee2015-05-12 18:43:07 -04002200 } else if (nd->dfd == AT_FDCWD) {
Al Viroe41f7d42011-02-22 14:02:58 -05002201 if (flags & LOOKUP_RCU) {
2202 struct fs_struct *fs = current->fs;
2203 unsigned seq;
2204
Al Viro8b61e742013-11-08 12:45:01 -05002205 rcu_read_lock();
Al Viroe41f7d42011-02-22 14:02:58 -05002206
2207 do {
2208 seq = read_seqcount_begin(&fs->seq);
2209 nd->path = fs->pwd;
Al Viroef55d912015-12-05 20:25:06 -05002210 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002211 nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq);
2212 } while (read_seqcount_retry(&fs->seq, seq));
2213 } else {
2214 get_fs_pwd(current->fs, &nd->path);
Al Viroef55d912015-12-05 20:25:06 -05002215 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002216 }
Al Viroef55d912015-12-05 20:25:06 -05002217 return s;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002218 } else {
Jeff Layton582aa642012-12-11 08:56:16 -05002219 /* Caller must check execute permissions on the starting path component */
Al Viroc8a53ee2015-05-12 18:43:07 -04002220 struct fd f = fdget_raw(nd->dfd);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002221 struct dentry *dentry;
2222
Al Viro2903ff02012-08-28 12:52:22 -04002223 if (!f.file)
Al Viro368ee9b2015-05-08 17:19:59 -04002224 return ERR_PTR(-EBADF);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002225
Al Viro2903ff02012-08-28 12:52:22 -04002226 dentry = f.file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002227
Al Virofd2f7cb2015-02-22 20:07:13 -05002228 if (*s) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002229 if (!d_can_lookup(dentry)) {
Al Viro2903ff02012-08-28 12:52:22 -04002230 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002231 return ERR_PTR(-ENOTDIR);
Al Viro2903ff02012-08-28 12:52:22 -04002232 }
Al Virof52e0c12011-03-14 18:56:51 -04002233 }
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002234
Al Viro2903ff02012-08-28 12:52:22 -04002235 nd->path = f.file->f_path;
Al Viroe41f7d42011-02-22 14:02:58 -05002236 if (flags & LOOKUP_RCU) {
Al Viro8b61e742013-11-08 12:45:01 -05002237 rcu_read_lock();
Al Viro34a26b92015-05-11 08:05:05 -04002238 nd->inode = nd->path.dentry->d_inode;
2239 nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq);
Al Viroe41f7d42011-02-22 14:02:58 -05002240 } else {
Al Viro2903ff02012-08-28 12:52:22 -04002241 path_get(&nd->path);
Al Viro34a26b92015-05-11 08:05:05 -04002242 nd->inode = nd->path.dentry->d_inode;
Al Viroe41f7d42011-02-22 14:02:58 -05002243 }
Al Viro34a26b92015-05-11 08:05:05 -04002244 fdput(f);
Al Viro368ee9b2015-05-08 17:19:59 -04002245 return s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Al Viro9b4a9b12009-04-07 11:44:16 -04002247}
2248
Al Viro3bdba282015-05-08 17:37:07 -04002249static const char *trailing_symlink(struct nameidata *nd)
Al Viro95fa25d2015-04-22 13:46:57 -04002250{
2251 const char *s;
Al Virofec2fa22015-05-06 15:58:18 -04002252 int error = may_follow_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002253 if (unlikely(error))
Al Viro3bdba282015-05-08 17:37:07 -04002254 return ERR_PTR(error);
Al Viro95fa25d2015-04-22 13:46:57 -04002255 nd->flags |= LOOKUP_PARENT;
Al Virofab51e82015-05-10 11:01:00 -04002256 nd->stack[0].name = NULL;
Al Viro3b2e7f72015-04-19 00:53:50 -04002257 s = get_link(nd);
Al Virodeb106c2015-05-08 18:05:21 -04002258 return s ? s : "";
Al Viro95fa25d2015-04-22 13:46:57 -04002259}
2260
Al Virocaa85632015-04-22 17:52:47 -04002261static inline int lookup_last(struct nameidata *nd)
Al Virobd92d7f2011-03-14 19:54:59 -04002262{
2263 if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len])
2264 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
2265
2266 nd->flags &= ~LOOKUP_PARENT;
Al Virodeb106c2015-05-08 18:05:21 -04002267 return walk_component(nd,
Al Viro4693a542015-05-04 17:47:11 -04002268 nd->flags & LOOKUP_FOLLOW
2269 ? nd->depth
2270 ? WALK_PUT | WALK_GET
2271 : WALK_GET
2272 : 0);
Al Virobd92d7f2011-03-14 19:54:59 -04002273}
2274
Al Viro9b4a9b12009-04-07 11:44:16 -04002275/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002276static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path)
Al Viro9b4a9b12009-04-07 11:44:16 -04002277{
Al Viroc8a53ee2015-05-12 18:43:07 -04002278 const char *s = path_init(nd, flags);
Al Virobd92d7f2011-03-14 19:54:59 -04002279 int err;
Nick Piggin31e6b012011-01-07 17:49:52 +11002280
Al Viro368ee9b2015-05-08 17:19:59 -04002281 if (IS_ERR(s))
2282 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002283 while (!(err = link_path_walk(s, nd))
2284 && ((err = lookup_last(nd)) > 0)) {
2285 s = trailing_symlink(nd);
2286 if (IS_ERR(s)) {
2287 err = PTR_ERR(s);
2288 break;
Al Virobd92d7f2011-03-14 19:54:59 -04002289 }
2290 }
Al Viro9f1fafe2011-03-25 11:00:12 -04002291 if (!err)
2292 err = complete_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002293
Al Virodeb106c2015-05-08 18:05:21 -04002294 if (!err && nd->flags & LOOKUP_DIRECTORY)
2295 if (!d_can_lookup(nd->path.dentry))
Al Virobd23a532011-03-23 09:56:30 -04002296 err = -ENOTDIR;
Al Viro625b6d12015-05-12 16:36:12 -04002297 if (!err) {
2298 *path = nd->path;
2299 nd->path.mnt = NULL;
2300 nd->path.dentry = NULL;
2301 }
2302 terminate_walk(nd);
Al Virobd92d7f2011-03-14 19:54:59 -04002303 return err;
Al Viroee0827c2011-02-21 23:38:09 -05002304}
Nick Piggin31e6b012011-01-07 17:49:52 +11002305
Al Viro625b6d12015-05-12 16:36:12 -04002306static int filename_lookup(int dfd, struct filename *name, unsigned flags,
Al Viro9ad1aaa2015-05-12 16:44:39 -04002307 struct path *path, struct path *root)
Jeff Layton873f1ee2012-10-10 15:25:29 -04002308{
Al Viro894bc8c2015-05-02 07:16:16 -04002309 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002310 struct nameidata nd;
Al Viroabc9f5b2015-05-12 16:53:42 -04002311 if (IS_ERR(name))
2312 return PTR_ERR(name);
Al Viro9ad1aaa2015-05-12 16:44:39 -04002313 if (unlikely(root)) {
2314 nd.root = *root;
2315 flags |= LOOKUP_ROOT;
2316 }
Al Viro9883d182015-05-13 07:28:08 -04002317 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002318 retval = path_lookupat(&nd, flags | LOOKUP_RCU, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002319 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002320 retval = path_lookupat(&nd, flags, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002321 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002322 retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002323
2324 if (likely(!retval))
Al Viro625b6d12015-05-12 16:36:12 -04002325 audit_inode(name, path->dentry, flags & LOOKUP_PARENT);
Al Viro9883d182015-05-13 07:28:08 -04002326 restore_nameidata();
Al Viroe4bd1c12015-05-12 16:40:39 -04002327 putname(name);
Jeff Layton873f1ee2012-10-10 15:25:29 -04002328 return retval;
2329}
2330
Al Viro8bcb77f2015-05-08 16:59:20 -04002331/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Al Viroc8a53ee2015-05-12 18:43:07 -04002332static int path_parentat(struct nameidata *nd, unsigned flags,
Al Viro391172c2015-05-09 11:19:16 -04002333 struct path *parent)
Al Viro8bcb77f2015-05-08 16:59:20 -04002334{
Al Viroc8a53ee2015-05-12 18:43:07 -04002335 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002336 int err;
2337 if (IS_ERR(s))
2338 return PTR_ERR(s);
2339 err = link_path_walk(s, nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002340 if (!err)
2341 err = complete_walk(nd);
Al Viro391172c2015-05-09 11:19:16 -04002342 if (!err) {
2343 *parent = nd->path;
2344 nd->path.mnt = NULL;
2345 nd->path.dentry = NULL;
2346 }
2347 terminate_walk(nd);
Al Viro8bcb77f2015-05-08 16:59:20 -04002348 return err;
2349}
2350
Al Viro5c31b6c2015-05-12 17:32:54 -04002351static struct filename *filename_parentat(int dfd, struct filename *name,
Al Viro391172c2015-05-09 11:19:16 -04002352 unsigned int flags, struct path *parent,
2353 struct qstr *last, int *type)
Al Viro8bcb77f2015-05-08 16:59:20 -04002354{
2355 int retval;
Al Viro9883d182015-05-13 07:28:08 -04002356 struct nameidata nd;
Al Viro8bcb77f2015-05-08 16:59:20 -04002357
Al Viro5c31b6c2015-05-12 17:32:54 -04002358 if (IS_ERR(name))
2359 return name;
Al Viro9883d182015-05-13 07:28:08 -04002360 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002361 retval = path_parentat(&nd, flags | LOOKUP_RCU, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002362 if (unlikely(retval == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002363 retval = path_parentat(&nd, flags, parent);
Al Viro8bcb77f2015-05-08 16:59:20 -04002364 if (unlikely(retval == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002365 retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent);
Al Viro391172c2015-05-09 11:19:16 -04002366 if (likely(!retval)) {
2367 *last = nd.last;
2368 *type = nd.last_type;
2369 audit_inode(name, parent->dentry, LOOKUP_PARENT);
Al Viro5c31b6c2015-05-12 17:32:54 -04002370 } else {
2371 putname(name);
2372 name = ERR_PTR(retval);
Al Viro391172c2015-05-09 11:19:16 -04002373 }
Al Viro9883d182015-05-13 07:28:08 -04002374 restore_nameidata();
Al Viro5c31b6c2015-05-12 17:32:54 -04002375 return name;
Al Viro8bcb77f2015-05-08 16:59:20 -04002376}
2377
Al Viro79714f72012-06-15 03:01:42 +04002378/* does lookup, returns the object with parent locked */
2379struct dentry *kern_path_locked(const char *name, struct path *path)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002380{
Al Viro5c31b6c2015-05-12 17:32:54 -04002381 struct filename *filename;
2382 struct dentry *d;
Al Viro391172c2015-05-09 11:19:16 -04002383 struct qstr last;
2384 int type;
Paul Moore51689102015-01-22 00:00:03 -05002385
Al Viro5c31b6c2015-05-12 17:32:54 -04002386 filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path,
2387 &last, &type);
Paul Moore51689102015-01-22 00:00:03 -05002388 if (IS_ERR(filename))
2389 return ERR_CAST(filename);
Al Viro5c31b6c2015-05-12 17:32:54 -04002390 if (unlikely(type != LAST_NORM)) {
Al Viro391172c2015-05-09 11:19:16 -04002391 path_put(path);
Al Viro5c31b6c2015-05-12 17:32:54 -04002392 putname(filename);
2393 return ERR_PTR(-EINVAL);
Al Viro79714f72012-06-15 03:01:42 +04002394 }
Al Viro59551022016-01-22 15:40:57 -05002395 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04002396 d = __lookup_hash(&last, path->dentry, 0);
Al Viro79714f72012-06-15 03:01:42 +04002397 if (IS_ERR(d)) {
Al Viro59551022016-01-22 15:40:57 -05002398 inode_unlock(path->dentry->d_inode);
Al Viro391172c2015-05-09 11:19:16 -04002399 path_put(path);
Al Viro79714f72012-06-15 03:01:42 +04002400 }
Paul Moore51689102015-01-22 00:00:03 -05002401 putname(filename);
Al Viro79714f72012-06-15 03:01:42 +04002402 return d;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002403}
2404
Al Virod1811462008-08-02 00:49:18 -04002405int kern_path(const char *name, unsigned int flags, struct path *path)
2406{
Al Viroabc9f5b2015-05-12 16:53:42 -04002407 return filename_lookup(AT_FDCWD, getname_kernel(name),
2408 flags, path, NULL);
Al Virod1811462008-08-02 00:49:18 -04002409}
Al Viro4d359502014-03-14 12:20:17 -04002410EXPORT_SYMBOL(kern_path);
Al Virod1811462008-08-02 00:49:18 -04002411
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002412/**
2413 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
2414 * @dentry: pointer to dentry of the base directory
2415 * @mnt: pointer to vfs mount of the base directory
2416 * @name: pointer to file name
2417 * @flags: lookup flags
Al Viroe0a01242011-06-27 17:00:37 -04002418 * @path: pointer to struct path to fill
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002419 */
2420int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
2421 const char *name, unsigned int flags,
Al Viroe0a01242011-06-27 17:00:37 -04002422 struct path *path)
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002423{
Al Viro9ad1aaa2015-05-12 16:44:39 -04002424 struct path root = {.mnt = mnt, .dentry = dentry};
Al Viro9ad1aaa2015-05-12 16:44:39 -04002425 /* the first argument of filename_lookup() is ignored with root */
Al Viroabc9f5b2015-05-12 16:53:42 -04002426 return filename_lookup(AT_FDCWD, getname_kernel(name),
2427 flags , path, &root);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002428}
Al Viro4d359502014-03-14 12:20:17 -04002429EXPORT_SYMBOL(vfs_path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07002430
Christoph Hellwigeead1912007-10-16 23:25:38 -07002431/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07002432 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07002433 * @name: pathname component to lookup
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002434 * @mnt: mount we are looking up on
Christoph Hellwigeead1912007-10-16 23:25:38 -07002435 * @base: base directory to lookup from
2436 * @len: maximum length @len should be interpreted to
2437 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07002438 * Note that this routine is purely a helper for filesystem usage and should
Al Viro9e7543e2015-02-23 02:49:48 -05002439 * not be called by generic code.
NeilBrownbbddca82016-01-07 16:08:20 -05002440 *
2441 * The caller must hold base->i_mutex.
Christoph Hellwigeead1912007-10-16 23:25:38 -07002442 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002443struct dentry *lookup_one_len2(const char *name, struct vfsmount *mnt, struct dentry *base, int len)
James Morris057f6c02007-04-26 00:12:05 -07002444{
James Morris057f6c02007-04-26 00:12:05 -07002445 struct qstr this;
Al Viro6a96ba52011-03-07 23:49:20 -05002446 unsigned int c;
Miklos Szeredicda309d2012-03-26 12:54:21 +02002447 int err;
James Morris057f6c02007-04-26 00:12:05 -07002448
Al Viro59551022016-01-22 15:40:57 -05002449 WARN_ON_ONCE(!inode_is_locked(base->d_inode));
David Woodhouse2f9092e2009-04-20 23:18:37 +01002450
Al Viro6a96ba52011-03-07 23:49:20 -05002451 this.name = name;
2452 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002453 this.hash = full_name_hash(base, name, len);
Al Viro6a96ba52011-03-07 23:49:20 -05002454 if (!len)
2455 return ERR_PTR(-EACCES);
2456
Al Viro21d8a152012-11-29 22:17:21 -05002457 if (unlikely(name[0] == '.')) {
2458 if (len < 2 || (len == 2 && name[1] == '.'))
2459 return ERR_PTR(-EACCES);
2460 }
2461
Al Viro6a96ba52011-03-07 23:49:20 -05002462 while (len--) {
2463 c = *(const unsigned char *)name++;
2464 if (c == '/' || c == '\0')
2465 return ERR_PTR(-EACCES);
Al Viro6a96ba52011-03-07 23:49:20 -05002466 }
Al Viro5a202bc2011-03-08 14:17:44 -05002467 /*
2468 * See if the low-level filesystem might want
2469 * to use its own hash..
2470 */
2471 if (base->d_flags & DCACHE_OP_HASH) {
Linus Torvaldsda53be12013-05-21 15:22:44 -07002472 int err = base->d_op->d_hash(base, &this);
Al Viro5a202bc2011-03-08 14:17:44 -05002473 if (err < 0)
2474 return ERR_PTR(err);
2475 }
Christoph Hellwigeead1912007-10-16 23:25:38 -07002476
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002477 err = inode_permission2(mnt, base->d_inode, MAY_EXEC);
Miklos Szeredicda309d2012-03-26 12:54:21 +02002478 if (err)
2479 return ERR_PTR(err);
2480
Al Viro72bd8662012-06-10 17:17:17 -04002481 return __lookup_hash(&this, base, 0);
James Morris057f6c02007-04-26 00:12:05 -07002482}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002483EXPORT_SYMBOL(lookup_one_len2);
2484
2485struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
2486{
2487 return lookup_one_len2(name, NULL, base, len);
2488}
Al Viro4d359502014-03-14 12:20:17 -04002489EXPORT_SYMBOL(lookup_one_len);
James Morris057f6c02007-04-26 00:12:05 -07002490
NeilBrownbbddca82016-01-07 16:08:20 -05002491/**
2492 * lookup_one_len_unlocked - filesystem helper to lookup single pathname component
2493 * @name: pathname component to lookup
2494 * @base: base directory to lookup from
2495 * @len: maximum length @len should be interpreted to
2496 *
2497 * Note that this routine is purely a helper for filesystem usage and should
2498 * not be called by generic code.
2499 *
2500 * Unlike lookup_one_len, it should be called without the parent
2501 * i_mutex held, and will take the i_mutex itself if necessary.
2502 */
2503struct dentry *lookup_one_len_unlocked(const char *name,
2504 struct dentry *base, int len)
2505{
2506 struct qstr this;
2507 unsigned int c;
2508 int err;
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002509 struct dentry *ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002510
2511 this.name = name;
2512 this.len = len;
Linus Torvalds8387ff22016-06-10 07:51:30 -07002513 this.hash = full_name_hash(base, name, len);
NeilBrownbbddca82016-01-07 16:08:20 -05002514 if (!len)
2515 return ERR_PTR(-EACCES);
2516
2517 if (unlikely(name[0] == '.')) {
2518 if (len < 2 || (len == 2 && name[1] == '.'))
2519 return ERR_PTR(-EACCES);
2520 }
2521
2522 while (len--) {
2523 c = *(const unsigned char *)name++;
2524 if (c == '/' || c == '\0')
2525 return ERR_PTR(-EACCES);
2526 }
2527 /*
2528 * See if the low-level filesystem might want
2529 * to use its own hash..
2530 */
2531 if (base->d_flags & DCACHE_OP_HASH) {
2532 int err = base->d_op->d_hash(base, &this);
2533 if (err < 0)
2534 return ERR_PTR(err);
2535 }
2536
2537 err = inode_permission(base->d_inode, MAY_EXEC);
2538 if (err)
2539 return ERR_PTR(err);
2540
Linus Torvalds20d00ee2016-07-29 12:17:52 -07002541 ret = lookup_dcache(&this, base, 0);
2542 if (!ret)
2543 ret = lookup_slow(&this, base, 0);
2544 return ret;
NeilBrownbbddca82016-01-07 16:08:20 -05002545}
2546EXPORT_SYMBOL(lookup_one_len_unlocked);
2547
Eric W. Biedermaneedf2652016-06-02 10:29:47 -05002548#ifdef CONFIG_UNIX98_PTYS
2549int path_pts(struct path *path)
2550{
2551 /* Find something mounted on "pts" in the same directory as
2552 * the input path.
2553 */
2554 struct dentry *child, *parent;
2555 struct qstr this;
2556 int ret;
2557
2558 ret = path_parent_directory(path);
2559 if (ret)
2560 return ret;
2561
2562 parent = path->dentry;
2563 this.name = "pts";
2564 this.len = 3;
2565 child = d_hash_and_lookup(parent, &this);
2566 if (!child)
2567 return -ENOENT;
2568
2569 path->dentry = child;
2570 dput(parent);
2571 follow_mount(path);
2572 return 0;
2573}
2574#endif
2575
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002576int user_path_at_empty(int dfd, const char __user *name, unsigned flags,
2577 struct path *path, int *empty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Al Viroabc9f5b2015-05-12 16:53:42 -04002579 return filename_lookup(dfd, getname_flags(name, flags, empty),
2580 flags, path, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
Al Virob853a162015-05-13 09:12:02 -04002582EXPORT_SYMBOL(user_path_at_empty);
Andy Whitcroft1fa1e7f2011-11-02 09:44:39 +01002583
Jeff Layton873f1ee2012-10-10 15:25:29 -04002584/*
2585 * NB: most callers don't do anything directly with the reference to the
2586 * to struct filename, but the nd->last pointer points into the name string
2587 * allocated by getname. So we must hold the reference to it until all
2588 * path-walking is complete.
2589 */
Al Viroa2ec4a22015-05-13 06:57:49 -04002590static inline struct filename *
Al Virof5beed72015-04-30 16:09:11 -04002591user_path_parent(int dfd, const char __user *path,
2592 struct path *parent,
2593 struct qstr *last,
2594 int *type,
Jeff Layton9e790bd2012-12-11 12:10:09 -05002595 unsigned int flags)
Al Viro2ad94ae2008-07-21 09:32:51 -04002596{
Jeff Layton9e790bd2012-12-11 12:10:09 -05002597 /* only LOOKUP_REVAL is allowed in extra flags */
Al Viro5c31b6c2015-05-12 17:32:54 -04002598 return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL,
2599 parent, last, type);
Al Viro2ad94ae2008-07-21 09:32:51 -04002600}
2601
Jeff Layton80334262013-07-26 06:23:25 -04002602/**
Al Viro197df042013-09-08 14:03:27 -04002603 * mountpoint_last - look up last component for umount
Jeff Layton80334262013-07-26 06:23:25 -04002604 * @nd: pathwalk nameidata - currently pointing at parent directory of "last"
2605 * @path: pointer to container for result
2606 *
2607 * This is a special lookup_last function just for umount. In this case, we
2608 * need to resolve the path without doing any revalidation.
2609 *
2610 * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since
2611 * mountpoints are always pinned in the dcache, their ancestors are too. Thus,
2612 * in almost all cases, this lookup will be served out of the dcache. The only
2613 * cases where it won't are if nd->last refers to a symlink or the path is
2614 * bogus and it doesn't exist.
2615 *
2616 * Returns:
2617 * -error: if there was an error during lookup. This includes -ENOENT if the
2618 * lookup found a negative dentry. The nd->path reference will also be
2619 * put in this case.
2620 *
2621 * 0: if we successfully resolved nd->path and found it to not to be a
2622 * symlink that needs to be followed. "path" will also be populated.
2623 * The nd->path reference will also be put.
2624 *
2625 * 1: if we successfully resolved nd->last and found it to be a symlink
2626 * that needs to be followed. "path" will be populated with the path
2627 * to the link, and nd->path will *not* be put.
2628 */
2629static int
Al Viro197df042013-09-08 14:03:27 -04002630mountpoint_last(struct nameidata *nd, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002631{
2632 int error = 0;
2633 struct dentry *dentry;
2634 struct dentry *dir = nd->path.dentry;
2635
Al Viro35759522013-09-08 13:41:33 -04002636 /* If we're in rcuwalk, drop out of it to handle last component */
2637 if (nd->flags & LOOKUP_RCU) {
Al Viro6e9918b2015-05-05 09:26:05 -04002638 if (unlazy_walk(nd, NULL, 0))
Al Virodeb106c2015-05-08 18:05:21 -04002639 return -ECHILD;
Jeff Layton80334262013-07-26 06:23:25 -04002640 }
2641
2642 nd->flags &= ~LOOKUP_PARENT;
2643
2644 if (unlikely(nd->last_type != LAST_NORM)) {
2645 error = handle_dots(nd, nd->last_type);
Al Viro35759522013-09-08 13:41:33 -04002646 if (error)
Al Virodeb106c2015-05-08 18:05:21 -04002647 return error;
Al Viro35759522013-09-08 13:41:33 -04002648 dentry = dget(nd->path.dentry);
Al Viro949a8522016-03-06 14:20:52 -05002649 } else {
2650 dentry = d_lookup(dir, &nd->last);
Jeff Layton80334262013-07-26 06:23:25 -04002651 if (!dentry) {
Al Viro949a8522016-03-06 14:20:52 -05002652 /*
2653 * No cached dentry. Mounted dentries are pinned in the
2654 * cache, so that means that this dentry is probably
2655 * a symlink or the path doesn't actually point
2656 * to a mounted dentry.
2657 */
2658 dentry = lookup_slow(&nd->last, dir,
2659 nd->flags | LOOKUP_NO_REVAL);
2660 if (IS_ERR(dentry))
2661 return PTR_ERR(dentry);
Dave Jonesbcceeeb2013-09-10 17:04:25 -04002662 }
Jeff Layton80334262013-07-26 06:23:25 -04002663 }
David Howells698934d2015-03-17 17:33:52 +00002664 if (d_is_negative(dentry)) {
Al Viro35759522013-09-08 13:41:33 -04002665 dput(dentry);
Al Virodeb106c2015-05-08 18:05:21 -04002666 return -ENOENT;
Jeff Layton80334262013-07-26 06:23:25 -04002667 }
Al Viro191d7f72015-05-04 08:26:45 -04002668 if (nd->depth)
2669 put_link(nd);
Al Viro35759522013-09-08 13:41:33 -04002670 path->dentry = dentry;
Vasily Averin295dc392014-07-21 12:30:23 +04002671 path->mnt = nd->path.mnt;
Al Viro181548c2015-05-07 19:54:34 -04002672 error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW,
2673 d_backing_inode(dentry), 0);
Al Virodeb106c2015-05-08 18:05:21 -04002674 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04002675 return error;
Vasily Averin295dc392014-07-21 12:30:23 +04002676 mntget(path->mnt);
Al Viro35759522013-09-08 13:41:33 -04002677 follow_mount(path);
Al Virodeb106c2015-05-08 18:05:21 -04002678 return 0;
Jeff Layton80334262013-07-26 06:23:25 -04002679}
2680
2681/**
Al Viro197df042013-09-08 14:03:27 -04002682 * path_mountpoint - look up a path to be umounted
Masanari Iida2a78b852015-09-09 15:39:23 -07002683 * @nd: lookup context
Jeff Layton80334262013-07-26 06:23:25 -04002684 * @flags: lookup flags
Al Viroc8a53ee2015-05-12 18:43:07 -04002685 * @path: pointer to container for result
Jeff Layton80334262013-07-26 06:23:25 -04002686 *
2687 * Look up the given name, but don't attempt to revalidate the last component.
Randy Dunlap606d6fe2013-10-19 14:56:55 -07002688 * Returns 0 and "path" will be valid on success; Returns error otherwise.
Jeff Layton80334262013-07-26 06:23:25 -04002689 */
2690static int
Al Viroc8a53ee2015-05-12 18:43:07 -04002691path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path)
Jeff Layton80334262013-07-26 06:23:25 -04002692{
Al Viroc8a53ee2015-05-12 18:43:07 -04002693 const char *s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04002694 int err;
2695 if (IS_ERR(s))
2696 return PTR_ERR(s);
Al Viro3bdba282015-05-08 17:37:07 -04002697 while (!(err = link_path_walk(s, nd)) &&
2698 (err = mountpoint_last(nd, path)) > 0) {
2699 s = trailing_symlink(nd);
2700 if (IS_ERR(s)) {
2701 err = PTR_ERR(s);
Jeff Layton80334262013-07-26 06:23:25 -04002702 break;
Al Viro3bdba282015-05-08 17:37:07 -04002703 }
Jeff Layton80334262013-07-26 06:23:25 -04002704 }
Al Virodeb106c2015-05-08 18:05:21 -04002705 terminate_walk(nd);
Jeff Layton80334262013-07-26 06:23:25 -04002706 return err;
2707}
2708
Al Viro2d864652013-09-08 20:18:44 -04002709static int
Al Viro668696d2015-02-22 19:44:00 -05002710filename_mountpoint(int dfd, struct filename *name, struct path *path,
Al Viro2d864652013-09-08 20:18:44 -04002711 unsigned int flags)
2712{
Al Viro9883d182015-05-13 07:28:08 -04002713 struct nameidata nd;
Al Virocbaab2d2015-01-22 02:49:00 -05002714 int error;
Al Viro668696d2015-02-22 19:44:00 -05002715 if (IS_ERR(name))
2716 return PTR_ERR(name);
Al Viro9883d182015-05-13 07:28:08 -04002717 set_nameidata(&nd, dfd, name);
Al Viroc8a53ee2015-05-12 18:43:07 -04002718 error = path_mountpoint(&nd, flags | LOOKUP_RCU, path);
Al Viro2d864652013-09-08 20:18:44 -04002719 if (unlikely(error == -ECHILD))
Al Viroc8a53ee2015-05-12 18:43:07 -04002720 error = path_mountpoint(&nd, flags, path);
Al Viro2d864652013-09-08 20:18:44 -04002721 if (unlikely(error == -ESTALE))
Al Viroc8a53ee2015-05-12 18:43:07 -04002722 error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path);
Al Viro2d864652013-09-08 20:18:44 -04002723 if (likely(!error))
Al Viro668696d2015-02-22 19:44:00 -05002724 audit_inode(name, path->dentry, 0);
Al Viro9883d182015-05-13 07:28:08 -04002725 restore_nameidata();
Al Viro668696d2015-02-22 19:44:00 -05002726 putname(name);
Al Viro2d864652013-09-08 20:18:44 -04002727 return error;
2728}
2729
Jeff Layton80334262013-07-26 06:23:25 -04002730/**
Al Viro197df042013-09-08 14:03:27 -04002731 * user_path_mountpoint_at - lookup a path from userland in order to umount it
Jeff Layton80334262013-07-26 06:23:25 -04002732 * @dfd: directory file descriptor
2733 * @name: pathname from userland
2734 * @flags: lookup flags
2735 * @path: pointer to container to hold result
2736 *
2737 * A umount is a special case for path walking. We're not actually interested
2738 * in the inode in this situation, and ESTALE errors can be a problem. We
2739 * simply want track down the dentry and vfsmount attached at the mountpoint
2740 * and avoid revalidating the last component.
2741 *
2742 * Returns 0 and populates "path" on success.
2743 */
2744int
Al Viro197df042013-09-08 14:03:27 -04002745user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags,
Jeff Layton80334262013-07-26 06:23:25 -04002746 struct path *path)
2747{
Al Virocbaab2d2015-01-22 02:49:00 -05002748 return filename_mountpoint(dfd, getname(name), path, flags);
Jeff Layton80334262013-07-26 06:23:25 -04002749}
2750
Al Viro2d864652013-09-08 20:18:44 -04002751int
2752kern_path_mountpoint(int dfd, const char *name, struct path *path,
2753 unsigned int flags)
2754{
Al Virocbaab2d2015-01-22 02:49:00 -05002755 return filename_mountpoint(dfd, getname_kernel(name), path, flags);
Al Viro2d864652013-09-08 20:18:44 -04002756}
2757EXPORT_SYMBOL(kern_path_mountpoint);
2758
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002759int __check_sticky(struct inode *dir, struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002761 kuid_t fsuid = current_fsuid();
David Howellsda9592e2008-11-14 10:39:05 +11002762
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002763 if (uid_eq(inode->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return 0;
Eric W. Biederman8e96e3b2012-03-03 21:17:15 -08002765 if (uid_eq(dir->i_uid, fsuid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 return 0;
Andy Lutomirski23adbe12014-06-10 12:45:42 -07002767 return !capable_wrt_inode_uidgid(inode, CAP_FOWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768}
Miklos Szeredicbdf35b2014-10-24 00:14:36 +02002769EXPORT_SYMBOL(__check_sticky);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771/*
2772 * Check whether we can remove a link victim from directory dir, check
2773 * whether the type of victim is right.
2774 * 1. We can't do it if dir is read-only (done in permission())
2775 * 2. We should have write and exec permissions on dir
2776 * 3. We can't remove anything from append-only dir
2777 * 4. We can't do anything with immutable dir (done in permission())
2778 * 5. If the sticky bit on dir is set we should either
2779 * a. be owner of dir, or
2780 * b. be owner of victim, or
2781 * c. have CAP_FOWNER capability
2782 * 6. If the victim is append-only or immutable we can't do antyhing with
2783 * links pointing to it.
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002784 * 7. If the victim has an unknown uid or gid we can't change the inode.
2785 * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR.
2786 * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR.
2787 * 10. We can't remove a root or mountpoint.
2788 * 11. We don't allow removal of NFS sillyrenamed files; it's handled by
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 * nfs_async_unlink().
2790 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002791static int may_delete(struct vfsmount *mnt, struct inode *dir, struct dentry *victim, bool isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792{
David Howells63afdfc2015-05-06 15:59:00 +01002793 struct inode *inode = d_backing_inode(victim);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 int error;
2795
David Howellsb18825a2013-09-12 19:22:53 +01002796 if (d_is_negative(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 return -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01002798 BUG_ON(!inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
2800 BUG_ON(victim->d_parent->d_inode != dir);
Jeff Layton4fa6b5e2012-10-10 15:25:25 -04002801 audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002803 error = inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 if (error)
2805 return error;
2806 if (IS_APPEND(dir))
2807 return -EPERM;
David Howellsb18825a2013-09-12 19:22:53 +01002808
2809 if (check_sticky(dir, inode) || IS_APPEND(inode) ||
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05002810 IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || HAS_UNMAPPED_ID(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 return -EPERM;
2812 if (isdir) {
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002813 if (!d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 return -ENOTDIR;
2815 if (IS_ROOT(victim))
2816 return -EBUSY;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02002817 } else if (d_is_dir(victim))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 return -EISDIR;
2819 if (IS_DEADDIR(dir))
2820 return -ENOENT;
2821 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
2822 return -EBUSY;
2823 return 0;
2824}
2825
2826/* Check whether we can create an object with dentry child in directory
2827 * dir.
2828 * 1. We can't do it if child already exists (open has special treatment for
2829 * this case, but since we are inlined it's OK)
2830 * 2. We can't do it if dir is read-only (done in permission())
Eric W. Biederman036d5232016-07-01 12:52:06 -05002831 * 3. We can't do it if the fs can't represent the fsuid or fsgid.
2832 * 4. We should have write and exec permissions on dir
2833 * 5. We can't do it if dir is immutable (done in permission())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002835static inline int may_create(struct vfsmount *mnt, struct inode *dir, struct dentry *child)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Eric W. Biederman036d5232016-07-01 12:52:06 -05002837 struct user_namespace *s_user_ns;
Jeff Layton14e972b2013-05-08 10:25:58 -04002838 audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (child->d_inode)
2840 return -EEXIST;
2841 if (IS_DEADDIR(dir))
2842 return -ENOENT;
Eric W. Biederman036d5232016-07-01 12:52:06 -05002843 s_user_ns = dir->i_sb->s_user_ns;
2844 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
2845 !kgid_has_mapping(s_user_ns, current_fsgid()))
2846 return -EOVERFLOW;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002847 return inode_permission2(mnt, dir, MAY_WRITE | MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848}
2849
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850/*
2851 * p1 and p2 should be directories on the same fs.
2852 */
2853struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
2854{
2855 struct dentry *p;
2856
2857 if (p1 == p2) {
Al Viro59551022016-01-22 15:40:57 -05002858 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 return NULL;
2860 }
2861
Al Virofc640052016-04-10 01:33:30 -04002862 mutex_lock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002864 p = d_ancestor(p2, p1);
2865 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002866 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT);
2867 inode_lock_nested(p1->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002868 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 }
2870
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002871 p = d_ancestor(p1, p2);
2872 if (p) {
Al Viro59551022016-01-22 15:40:57 -05002873 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2874 inode_lock_nested(p2->d_inode, I_MUTEX_CHILD);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002875 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
2877
Al Viro59551022016-01-22 15:40:57 -05002878 inode_lock_nested(p1->d_inode, I_MUTEX_PARENT);
2879 inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return NULL;
2881}
Al Viro4d359502014-03-14 12:20:17 -04002882EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884void unlock_rename(struct dentry *p1, struct dentry *p2)
2885{
Al Viro59551022016-01-22 15:40:57 -05002886 inode_unlock(p1->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 if (p1 != p2) {
Al Viro59551022016-01-22 15:40:57 -05002888 inode_unlock(p2->d_inode);
Al Virofc640052016-04-10 01:33:30 -04002889 mutex_unlock(&p1->d_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891}
Al Viro4d359502014-03-14 12:20:17 -04002892EXPORT_SYMBOL(unlock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002894int vfs_create2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry,
2895 umode_t mode, bool want_excl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002897 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 if (error)
2899 return error;
2900
Al Viroacfa4382008-12-04 10:06:33 -05002901 if (!dir->i_op->create)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return -EACCES; /* shouldn't it be ENOSYS? */
2903 mode &= S_IALLUGO;
2904 mode |= S_IFREG;
2905 error = security_inode_create(dir, dentry, mode);
2906 if (error)
2907 return error;
Al Viro312b63f2012-06-10 18:09:36 -04002908 error = dir->i_op->create(dir, dentry, mode, want_excl);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002909 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002910 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 return error;
2912}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002913EXPORT_SYMBOL(vfs_create2);
2914
2915int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
2916 bool want_excl)
2917{
2918 return vfs_create2(NULL, dir, dentry, mode, want_excl);
2919}
Al Viro4d359502014-03-14 12:20:17 -04002920EXPORT_SYMBOL(vfs_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002922bool may_open_dev(const struct path *path)
2923{
2924 return !(path->mnt->mnt_flags & MNT_NODEV) &&
2925 !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV);
2926}
2927
Al Viro73d049a2011-03-11 12:08:24 -05002928static int may_open(struct path *path, int acc_mode, int flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Christoph Hellwig3fb64192008-10-24 09:58:10 +02002930 struct dentry *dentry = path->dentry;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002931 struct vfsmount *mnt = path->mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 struct inode *inode = dentry->d_inode;
2933 int error;
2934
2935 if (!inode)
2936 return -ENOENT;
2937
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002938 switch (inode->i_mode & S_IFMT) {
2939 case S_IFLNK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 return -ELOOP;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002941 case S_IFDIR:
2942 if (acc_mode & MAY_WRITE)
2943 return -EISDIR;
2944 break;
2945 case S_IFBLK:
2946 case S_IFCHR:
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002947 if (!may_open_dev(path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return -EACCES;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002949 /*FALLTHRU*/
2950 case S_IFIFO:
2951 case S_IFSOCK:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 flag &= ~O_TRUNC;
Christoph Hellwigc8fe8f32009-01-05 19:27:23 +01002953 break;
Dave Hansen4a3fd212008-02-15 14:37:48 -08002954 }
Dave Hansenb41572e2007-10-16 23:31:14 -07002955
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07002956 error = inode_permission2(mnt, inode, MAY_OPEN | acc_mode);
Dave Hansenb41572e2007-10-16 23:31:14 -07002957 if (error)
2958 return error;
Mimi Zohar6146f0d2009-02-04 09:06:57 -05002959
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 /*
2961 * An append-only file must be opened in append mode for writing.
2962 */
2963 if (IS_APPEND(inode)) {
Al Viro8737c932009-12-24 06:47:55 -05002964 if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND))
Al Viro7715b522009-12-16 03:54:00 -05002965 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (flag & O_TRUNC)
Al Viro7715b522009-12-16 03:54:00 -05002967 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 }
2969
2970 /* O_NOATIME can only be set by the owner or superuser */
Serge E. Hallyn2e149672011-03-23 16:43:26 -07002971 if (flag & O_NOATIME && !inode_owner_or_capable(inode))
Al Viro7715b522009-12-16 03:54:00 -05002972 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973
J. Bruce Fieldsf3c7691e2011-09-21 10:58:13 -04002974 return 0;
Al Viro7715b522009-12-16 03:54:00 -05002975}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Jeff Laytone1181ee2010-12-07 16:19:50 -05002977static int handle_truncate(struct file *filp)
Al Viro7715b522009-12-16 03:54:00 -05002978{
Jeff Laytone1181ee2010-12-07 16:19:50 -05002979 struct path *path = &filp->f_path;
Al Viro7715b522009-12-16 03:54:00 -05002980 struct inode *inode = path->dentry->d_inode;
2981 int error = get_write_access(inode);
2982 if (error)
2983 return error;
2984 /*
2985 * Refuse to truncate files with mandatory locks held on them.
2986 */
Jeff Laytond7a06982014-03-10 09:54:15 -04002987 error = locks_verify_locked(filp);
Al Viro7715b522009-12-16 03:54:00 -05002988 if (!error)
Tetsuo Handaea0d3ab2010-06-02 13:24:43 +09002989 error = security_path_truncate(path);
Al Viro7715b522009-12-16 03:54:00 -05002990 if (!error) {
Daniel Rosenberg2757e9b2016-10-26 16:33:11 -07002991 error = do_truncate2(path->mnt, path->dentry, 0,
Al Viro7715b522009-12-16 03:54:00 -05002992 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
Jeff Laytone1181ee2010-12-07 16:19:50 -05002993 filp);
Al Viro7715b522009-12-16 03:54:00 -05002994 }
2995 put_write_access(inode);
Mimi Zoharacd0c932009-09-04 13:08:46 -04002996 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997}
2998
Dave Hansend57999e2008-02-15 14:37:27 -08002999static inline int open_to_namei_flags(int flag)
3000{
Al Viro8a5e9292011-06-25 19:15:54 -04003001 if ((flag & O_ACCMODE) == 3)
3002 flag--;
Dave Hansend57999e2008-02-15 14:37:27 -08003003 return flag;
3004}
3005
Al Virod3607752016-03-25 15:21:09 -04003006static int may_o_create(const struct path *dir, struct dentry *dentry, umode_t mode)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003007{
Seth Forshee2e727b82017-10-07 22:36:55 +00003008 struct user_namespace *s_user_ns;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003009 int error = security_path_mknod(dir, dentry, mode, 0);
3010 if (error)
3011 return error;
3012
Seth Forshee2e727b82017-10-07 22:36:55 +00003013 s_user_ns = dir->dentry->d_sb->s_user_ns;
3014 if (!kuid_has_mapping(s_user_ns, current_fsuid()) ||
3015 !kgid_has_mapping(s_user_ns, current_fsgid()))
3016 return -EOVERFLOW;
3017
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003018 error = inode_permission2(dir->mnt, dir->dentry->d_inode, MAY_WRITE | MAY_EXEC);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003019 if (error)
3020 return error;
3021
3022 return security_inode_create(dir->dentry->d_inode, dentry, mode);
3023}
3024
David Howells1acf0af2012-06-14 16:13:46 +01003025/*
3026 * Attempt to atomically look up, create and open a file from a negative
3027 * dentry.
3028 *
3029 * Returns 0 if successful. The file will have been created and attached to
3030 * @file by the filesystem calling finish_open().
3031 *
3032 * Returns 1 if the file was looked up only or didn't need creating. The
3033 * caller will need to perform the open themselves. @path will have been
3034 * updated to point to the new dentry. This may be negative.
3035 *
3036 * Returns an error code otherwise.
3037 */
Al Viro2675a4e2012-06-22 12:41:10 +04003038static int atomic_open(struct nameidata *nd, struct dentry *dentry,
3039 struct path *path, struct file *file,
3040 const struct open_flags *op,
Al Viro1643b432016-04-27 19:14:10 -04003041 int open_flag, umode_t mode,
Al Viro2675a4e2012-06-22 12:41:10 +04003042 int *opened)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003043{
Miklos Szeredid18e9002012-06-05 15:10:17 +02003044 struct dentry *const DENTRY_NOT_SET = (void *) -1UL;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003045 struct inode *dir = nd->path.dentry->d_inode;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003046 int error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003047
Al Viro384f26e2016-04-28 02:03:55 -04003048 if (!(~open_flag & (O_EXCL | O_CREAT))) /* both O_EXCL and O_CREAT */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003049 open_flag &= ~O_TRUNC;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003050
Miklos Szeredid18e9002012-06-05 15:10:17 +02003051 if (nd->flags & LOOKUP_DIRECTORY)
3052 open_flag |= O_DIRECTORY;
3053
Al Viro30d90492012-06-22 12:40:19 +04003054 file->f_path.dentry = DENTRY_NOT_SET;
3055 file->f_path.mnt = nd->path.mnt;
Al Viro0fb1ea02016-04-27 14:13:10 -04003056 error = dir->i_op->atomic_open(dir, dentry, file,
3057 open_to_namei_flags(open_flag),
3058 mode, opened);
Al Viro6fbd0712016-04-28 11:50:59 -04003059 d_lookup_done(dentry);
Al Viro384f26e2016-04-28 02:03:55 -04003060 if (!error) {
3061 /*
3062 * We didn't have the inode before the open, so check open
3063 * permission here.
3064 */
3065 int acc_mode = op->acc_mode;
3066 if (*opened & FILE_CREATED) {
3067 WARN_ON(!(open_flag & O_CREAT));
3068 fsnotify_create(dir, dentry);
3069 acc_mode = 0;
3070 }
3071 error = may_open(&file->f_path, acc_mode, open_flag);
3072 if (WARN_ON(error > 0))
3073 error = -EINVAL;
3074 } else if (error > 0) {
Al Viro30d90492012-06-22 12:40:19 +04003075 if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) {
Al Viro2675a4e2012-06-22 12:41:10 +04003076 error = -EIO;
Al Viro03da6332013-09-16 19:22:33 -04003077 } else {
Al Viro384f26e2016-04-28 02:03:55 -04003078 if (file->f_path.dentry) {
3079 dput(dentry);
3080 dentry = file->f_path.dentry;
Al Viro03da6332013-09-16 19:22:33 -04003081 }
Al Viro384f26e2016-04-28 02:03:55 -04003082 if (*opened & FILE_CREATED)
3083 fsnotify_create(dir, dentry);
Al Viroa01e7182016-06-07 21:53:51 -04003084 if (unlikely(d_is_negative(dentry))) {
3085 error = -ENOENT;
3086 } else {
3087 path->dentry = dentry;
3088 path->mnt = nd->path.mnt;
3089 return 1;
3090 }
Sage Weil62b2ce92012-08-15 13:30:12 -07003091 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003092 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003093 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003094 return error;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003095}
3096
Nick Piggin31e6b012011-01-07 17:49:52 +11003097/*
David Howells1acf0af2012-06-14 16:13:46 +01003098 * Look up and maybe create and open the last component.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003099 *
3100 * Must be called with i_mutex held on parent.
3101 *
David Howells1acf0af2012-06-14 16:13:46 +01003102 * Returns 0 if the file was successfully atomically created (if necessary) and
3103 * opened. In this case the file will be returned attached to @file.
3104 *
3105 * Returns 1 if the file was not completely opened at this time, though lookups
3106 * and creations will have been performed and the dentry returned in @path will
3107 * be positive upon return if O_CREAT was specified. If O_CREAT wasn't
3108 * specified then a negative dentry may be returned.
3109 *
3110 * An error code is returned otherwise.
3111 *
3112 * FILE_CREATE will be set in @*opened if the dentry was created and will be
3113 * cleared otherwise prior to returning.
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003114 */
Al Viro2675a4e2012-06-22 12:41:10 +04003115static int lookup_open(struct nameidata *nd, struct path *path,
3116 struct file *file,
3117 const struct open_flags *op,
Al Viro64894cf2012-07-31 00:53:35 +04003118 bool got_write, int *opened)
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003119{
3120 struct dentry *dir = nd->path.dentry;
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003121 struct inode *dir_inode = dir->d_inode;
Al Viro1643b432016-04-27 19:14:10 -04003122 int open_flag = op->open_flag;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003123 struct dentry *dentry;
Al Viro1643b432016-04-27 19:14:10 -04003124 int error, create_error = 0;
Al Viro1643b432016-04-27 19:14:10 -04003125 umode_t mode = op->mode;
Al Viro6fbd0712016-04-28 11:50:59 -04003126 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003127
Al Viroce8644f2016-04-26 14:17:56 -04003128 if (unlikely(IS_DEADDIR(dir_inode)))
3129 return -ENOENT;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003130
Al Viro47237682012-06-10 05:01:45 -04003131 *opened &= ~FILE_CREATED;
Al Viro6fbd0712016-04-28 11:50:59 -04003132 dentry = d_lookup(dir, &nd->last);
3133 for (;;) {
3134 if (!dentry) {
3135 dentry = d_alloc_parallel(dir, &nd->last, &wq);
3136 if (IS_ERR(dentry))
3137 return PTR_ERR(dentry);
3138 }
3139 if (d_in_lookup(dentry))
3140 break;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003141
Al Viro6fbd0712016-04-28 11:50:59 -04003142 if (!(dentry->d_flags & DCACHE_OP_REVALIDATE))
3143 break;
3144
3145 error = d_revalidate(dentry, nd->flags);
3146 if (likely(error > 0))
3147 break;
3148 if (error)
3149 goto out_dput;
3150 d_invalidate(dentry);
3151 dput(dentry);
3152 dentry = NULL;
3153 }
3154 if (dentry->d_inode) {
Al Viro6c51e512016-03-05 20:09:32 -05003155 /* Cached positive dentry: will open in f_op->open */
Miklos Szeredid18e9002012-06-05 15:10:17 +02003156 goto out_no_open;
Al Viro6c51e512016-03-05 20:09:32 -05003157 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003158
Al Viro1643b432016-04-27 19:14:10 -04003159 /*
3160 * Checking write permission is tricky, bacuse we don't know if we are
3161 * going to actually need it: O_CREAT opens should work as long as the
3162 * file exists. But checking existence breaks atomicity. The trick is
3163 * to check access and if not granted clear O_CREAT from the flags.
3164 *
3165 * Another problem is returing the "right" error value (e.g. for an
3166 * O_EXCL open we want to return EEXIST not EROFS).
3167 */
3168 if (open_flag & O_CREAT) {
3169 if (!IS_POSIXACL(dir->d_inode))
3170 mode &= ~current_umask();
3171 if (unlikely(!got_write)) {
3172 create_error = -EROFS;
3173 open_flag &= ~O_CREAT;
3174 if (open_flag & (O_EXCL | O_TRUNC))
3175 goto no_open;
3176 /* No side effects, safe to clear O_CREAT */
3177 } else {
3178 create_error = may_o_create(&nd->path, dentry, mode);
3179 if (create_error) {
3180 open_flag &= ~O_CREAT;
3181 if (open_flag & O_EXCL)
3182 goto no_open;
3183 }
3184 }
3185 } else if ((open_flag & (O_TRUNC|O_WRONLY|O_RDWR)) &&
3186 unlikely(!got_write)) {
3187 /*
3188 * No O_CREATE -> atomicity not a requirement -> fall
3189 * back to lookup + open
3190 */
3191 goto no_open;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003192 }
3193
Al Viro1643b432016-04-27 19:14:10 -04003194 if (dir_inode->i_op->atomic_open) {
3195 error = atomic_open(nd, dentry, path, file, op, open_flag,
3196 mode, opened);
3197 if (unlikely(error == -ENOENT) && create_error)
3198 error = create_error;
3199 return error;
3200 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003201
Al Viro1643b432016-04-27 19:14:10 -04003202no_open:
Al Viro6fbd0712016-04-28 11:50:59 -04003203 if (d_in_lookup(dentry)) {
Al Viro12fa5e22016-04-28 11:19:43 -04003204 struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry,
3205 nd->flags);
Al Viro6fbd0712016-04-28 11:50:59 -04003206 d_lookup_done(dentry);
Al Viro12fa5e22016-04-28 11:19:43 -04003207 if (unlikely(res)) {
3208 if (IS_ERR(res)) {
3209 error = PTR_ERR(res);
3210 goto out_dput;
3211 }
3212 dput(dentry);
3213 dentry = res;
3214 }
Miklos Szeredi54ef4872012-06-05 15:10:16 +02003215 }
3216
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003217 /* Negative dentry, just create the file */
Al Viro1643b432016-04-27 19:14:10 -04003218 if (!dentry->d_inode && (open_flag & O_CREAT)) {
Al Viro47237682012-06-10 05:01:45 -04003219 *opened |= FILE_CREATED;
Al Viroce8644f2016-04-26 14:17:56 -04003220 audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE);
Al Viroce8644f2016-04-26 14:17:56 -04003221 if (!dir_inode->i_op->create) {
3222 error = -EACCES;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003223 goto out_dput;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003224 }
Al Viroce8644f2016-04-26 14:17:56 -04003225 error = dir_inode->i_op->create(dir_inode, dentry, mode,
Al Viro1643b432016-04-27 19:14:10 -04003226 open_flag & O_EXCL);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003227 if (error)
3228 goto out_dput;
Al Viroce8644f2016-04-26 14:17:56 -04003229 fsnotify_create(dir_inode, dentry);
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003230 }
Al Viro1643b432016-04-27 19:14:10 -04003231 if (unlikely(create_error) && !dentry->d_inode) {
3232 error = create_error;
3233 goto out_dput;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003234 }
Miklos Szeredid18e9002012-06-05 15:10:17 +02003235out_no_open:
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003236 path->dentry = dentry;
3237 path->mnt = nd->path.mnt;
Al Viro2675a4e2012-06-22 12:41:10 +04003238 return 1;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003239
3240out_dput:
3241 dput(dentry);
Al Viro2675a4e2012-06-22 12:41:10 +04003242 return error;
Miklos Szeredid58ffd32012-06-05 15:10:15 +02003243}
3244
3245/*
Al Virofe2d35f2011-03-05 22:58:25 -05003246 * Handle the last step of open()
Nick Piggin31e6b012011-01-07 17:49:52 +11003247 */
Al Viro896475d2015-04-22 18:02:17 -04003248static int do_last(struct nameidata *nd,
Al Viro2675a4e2012-06-22 12:41:10 +04003249 struct file *file, const struct open_flags *op,
Al Viro76ae2a52015-05-12 18:44:32 -04003250 int *opened)
Al Virofb1cc552009-12-24 01:58:28 -05003251{
Al Viroa1e28032009-12-24 02:12:06 -05003252 struct dentry *dir = nd->path.dentry;
Al Viroca344a892011-03-09 00:36:45 -05003253 int open_flag = op->open_flag;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003254 bool will_truncate = (open_flag & O_TRUNC) != 0;
Al Viro64894cf2012-07-31 00:53:35 +04003255 bool got_write = false;
Al Virobcda7652011-03-13 16:42:14 -04003256 int acc_mode = op->acc_mode;
Al Viro254cf582015-05-05 09:40:46 -04003257 unsigned seq;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003258 struct inode *inode;
Al Viro896475d2015-04-22 18:02:17 -04003259 struct path path;
Al Viro16c2cd72011-02-22 15:50:10 -05003260 int error;
Al Virofb1cc552009-12-24 01:58:28 -05003261
Al Viroc3e380b2011-02-23 13:39:45 -05003262 nd->flags &= ~LOOKUP_PARENT;
3263 nd->flags |= op->intent;
3264
Al Virobc77daa2013-06-06 09:12:33 -04003265 if (nd->last_type != LAST_NORM) {
Al Virofe2d35f2011-03-05 22:58:25 -05003266 error = handle_dots(nd, nd->last_type);
Al Virodeb106c2015-05-08 18:05:21 -04003267 if (unlikely(error))
Al Viro2675a4e2012-06-22 12:41:10 +04003268 return error;
Miklos Szeredie83db162012-06-05 15:10:29 +02003269 goto finish_open;
Al Viro1f36f772009-12-26 10:56:19 -05003270 }
Al Viro67ee3ad2009-12-26 07:01:01 -05003271
Al Viroca344a892011-03-09 00:36:45 -05003272 if (!(open_flag & O_CREAT)) {
Al Virofe2d35f2011-03-05 22:58:25 -05003273 if (nd->last.name[nd->last.len])
3274 nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
3275 /* we _can_ be in RCU mode here */
Al Viro254cf582015-05-05 09:40:46 -04003276 error = lookup_fast(nd, &path, &inode, &seq);
Al Viroe9742b52016-03-05 22:04:59 -05003277 if (likely(error > 0))
Miklos Szeredi71574862012-06-05 15:10:14 +02003278 goto finish_lookup;
Miklos Szeredia1eb3312012-05-21 17:30:07 +02003279
Miklos Szeredi71574862012-06-05 15:10:14 +02003280 if (error < 0)
Al Virodeb106c2015-05-08 18:05:21 -04003281 return error;
Miklos Szeredi37d7fff2012-06-05 15:10:12 +02003282
Miklos Szeredi71574862012-06-05 15:10:14 +02003283 BUG_ON(nd->inode != dir->d_inode);
Al Viro6583fe22016-03-05 18:14:03 -05003284 BUG_ON(nd->flags & LOOKUP_RCU);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003285 } else {
3286 /* create side of things */
3287 /*
3288 * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED
3289 * has been cleared when we got to the last component we are
3290 * about to look up
3291 */
3292 error = complete_walk(nd);
Al Viroe8bb73d2015-05-08 16:28:42 -04003293 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003294 return error;
Miklos Szeredib6183df2012-06-05 15:10:13 +02003295
Al Viro76ae2a52015-05-12 18:44:32 -04003296 audit_inode(nd->name, dir, LOOKUP_PARENT);
Miklos Szeredib6183df2012-06-05 15:10:13 +02003297 /* trailing slashes? */
Al Virodeb106c2015-05-08 18:05:21 -04003298 if (unlikely(nd->last.name[nd->last.len]))
3299 return -EISDIR;
Al Virofe2d35f2011-03-05 22:58:25 -05003300 }
3301
Al Viro9cf843e2016-04-28 19:35:16 -04003302 if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) {
Al Viro64894cf2012-07-31 00:53:35 +04003303 error = mnt_want_write(nd->path.mnt);
3304 if (!error)
3305 got_write = true;
3306 /*
3307 * do _not_ fail yet - we might not need that or fail with
3308 * a different error; let lookup_open() decide; we'll be
3309 * dropping this one anyway.
3310 */
3311 }
Al Viro9cf843e2016-04-28 19:35:16 -04003312 if (open_flag & O_CREAT)
3313 inode_lock(dir->d_inode);
3314 else
3315 inode_lock_shared(dir->d_inode);
Al Viro896475d2015-04-22 18:02:17 -04003316 error = lookup_open(nd, &path, file, op, got_write, opened);
Al Viro9cf843e2016-04-28 19:35:16 -04003317 if (open_flag & O_CREAT)
3318 inode_unlock(dir->d_inode);
3319 else
3320 inode_unlock_shared(dir->d_inode);
Al Viroa1e28032009-12-24 02:12:06 -05003321
Al Viro2675a4e2012-06-22 12:41:10 +04003322 if (error <= 0) {
3323 if (error)
Miklos Szeredid18e9002012-06-05 15:10:17 +02003324 goto out;
3325
Al Viro47237682012-06-10 05:01:45 -04003326 if ((*opened & FILE_CREATED) ||
Al Viro496ad9a2013-01-23 17:07:38 -05003327 !S_ISREG(file_inode(file)->i_mode))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003328 will_truncate = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003329
Al Viro76ae2a52015-05-12 18:44:32 -04003330 audit_inode(nd->name, file->f_path.dentry, 0);
Miklos Szeredid18e9002012-06-05 15:10:17 +02003331 goto opened;
3332 }
Al Virofb1cc552009-12-24 01:58:28 -05003333
Al Viro47237682012-06-10 05:01:45 -04003334 if (*opened & FILE_CREATED) {
Al Viro9b44f1b2011-03-09 00:17:27 -05003335 /* Don't check for write permission, don't truncate */
Al Viroca344a892011-03-09 00:36:45 -05003336 open_flag &= ~O_TRUNC;
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003337 will_truncate = false;
Al Viro62fb4a12015-12-26 22:33:24 -05003338 acc_mode = 0;
Al Viro896475d2015-04-22 18:02:17 -04003339 path_to_nameidata(&path, nd);
Miklos Szeredie83db162012-06-05 15:10:29 +02003340 goto finish_open_created;
Al Virofb1cc552009-12-24 01:58:28 -05003341 }
3342
3343 /*
Miklos Szeredid18e9002012-06-05 15:10:17 +02003344 * If atomic_open() acquired write access it is dropped now due to
3345 * possible mount and symlink following (this might be optimized away if
3346 * necessary...)
3347 */
Al Viro64894cf2012-07-31 00:53:35 +04003348 if (got_write) {
Miklos Szeredid18e9002012-06-05 15:10:17 +02003349 mnt_drop_write(nd->path.mnt);
Al Viro64894cf2012-07-31 00:53:35 +04003350 got_write = false;
Miklos Szeredid18e9002012-06-05 15:10:17 +02003351 }
3352
Al Viroe6ec03a2016-06-05 00:23:09 -04003353 error = follow_managed(&path, nd);
3354 if (unlikely(error < 0))
3355 return error;
3356
Al Viro6583fe22016-03-05 18:14:03 -05003357 if (unlikely(d_is_negative(path.dentry))) {
3358 path_to_nameidata(&path, nd);
3359 return -ENOENT;
3360 }
3361
3362 /*
3363 * create/update audit record if it already exists.
3364 */
3365 audit_inode(nd->name, path.dentry, 0);
3366
Al Virodeb106c2015-05-08 18:05:21 -04003367 if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) {
3368 path_to_nameidata(&path, nd);
3369 return -EEXIST;
3370 }
Al Virofb1cc552009-12-24 01:58:28 -05003371
Al Viro254cf582015-05-05 09:40:46 -04003372 seq = 0; /* out of RCU mode, so the value doesn't matter */
Al Virod4565642016-02-27 19:23:16 -05003373 inode = d_backing_inode(path.dentry);
Al Viro766c4cb2015-05-07 19:24:57 -04003374finish_lookup:
Al Virod63ff282015-05-04 18:13:23 -04003375 if (nd->depth)
3376 put_link(nd);
Al Viro181548c2015-05-07 19:54:34 -04003377 error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW,
3378 inode, seq);
Al Virodeb106c2015-05-08 18:05:21 -04003379 if (unlikely(error))
Al Virod63ff282015-05-04 18:13:23 -04003380 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003381
Al Virofac7d192016-06-04 11:41:49 -04003382 path_to_nameidata(&path, nd);
Miklos Szeredidecf3402012-05-21 17:30:08 +02003383 nd->inode = inode;
Al Viro254cf582015-05-05 09:40:46 -04003384 nd->seq = seq;
Al Viroa3fbbde2011-11-07 21:21:26 +00003385 /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */
Al Virobc77daa2013-06-06 09:12:33 -04003386finish_open:
Al Viroa3fbbde2011-11-07 21:21:26 +00003387 error = complete_walk(nd);
Al Virofac7d192016-06-04 11:41:49 -04003388 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003389 return error;
Al Viro76ae2a52015-05-12 18:44:32 -04003390 audit_inode(nd->name, nd->path.dentry, 0);
Al Virofb1cc552009-12-24 01:58:28 -05003391 error = -EISDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003392 if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003393 goto out;
Miklos Szerediaf2f5542012-05-21 17:30:11 +02003394 error = -ENOTDIR;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02003395 if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry))
Al Viro2675a4e2012-06-22 12:41:10 +04003396 goto out;
David Howells4bbcbd32015-03-17 22:16:40 +00003397 if (!d_is_reg(nd->path.dentry))
Miklos Szeredi77d660a2012-06-05 15:10:30 +02003398 will_truncate = false;
Al Viro6c0d46c2011-03-09 00:59:59 -05003399
Al Viro0f9d1a12011-03-09 00:13:14 -05003400 if (will_truncate) {
3401 error = mnt_want_write(nd->path.mnt);
3402 if (error)
Al Viro2675a4e2012-06-22 12:41:10 +04003403 goto out;
Al Viro64894cf2012-07-31 00:53:35 +04003404 got_write = true;
Al Viro0f9d1a12011-03-09 00:13:14 -05003405 }
Miklos Szeredie83db162012-06-05 15:10:29 +02003406finish_open_created:
Al Viro6ac08702016-04-26 00:02:50 -04003407 error = may_open(&nd->path, acc_mode, open_flag);
3408 if (error)
3409 goto out;
Miklos Szeredi4aa7c632014-10-24 00:14:35 +02003410 BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
3411 error = vfs_open(&nd->path, file, current_cred());
Al Virofac7d192016-06-04 11:41:49 -04003412 if (error)
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003413 goto out;
Al Virofac7d192016-06-04 11:41:49 -04003414 *opened |= FILE_OPENED;
Miklos Szeredia8277b92012-06-05 15:10:32 +02003415opened:
Al Viro2675a4e2012-06-22 12:41:10 +04003416 error = open_check_o_direct(file);
Al Virofe9ec822016-04-27 03:14:20 -04003417 if (!error)
3418 error = ima_file_check(file, op->acc_mode, *opened);
3419 if (!error && will_truncate)
Al Viro2675a4e2012-06-22 12:41:10 +04003420 error = handle_truncate(file);
Al Viroca344a892011-03-09 00:36:45 -05003421out:
Al Virofe9ec822016-04-27 03:14:20 -04003422 if (unlikely(error) && (*opened & FILE_OPENED))
3423 fput(file);
Al Viroc80567c2016-02-27 19:17:33 -05003424 if (unlikely(error > 0)) {
3425 WARN_ON(1);
3426 error = -EINVAL;
3427 }
Al Viro64894cf2012-07-31 00:53:35 +04003428 if (got_write)
Al Viro0f9d1a12011-03-09 00:13:14 -05003429 mnt_drop_write(nd->path.mnt);
Al Viro2675a4e2012-06-22 12:41:10 +04003430 return error;
Al Virofb1cc552009-12-24 01:58:28 -05003431}
3432
Al Viroc8a53ee2015-05-12 18:43:07 -04003433static int do_tmpfile(struct nameidata *nd, unsigned flags,
Al Viro60545d02013-06-07 01:20:27 -04003434 const struct open_flags *op,
3435 struct file *file, int *opened)
3436{
3437 static const struct qstr name = QSTR_INIT("/", 1);
Al Viro625b6d12015-05-12 16:36:12 -04003438 struct dentry *child;
Al Viro60545d02013-06-07 01:20:27 -04003439 struct inode *dir;
Al Viro625b6d12015-05-12 16:36:12 -04003440 struct path path;
Al Viroc8a53ee2015-05-12 18:43:07 -04003441 int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path);
Al Viro60545d02013-06-07 01:20:27 -04003442 if (unlikely(error))
3443 return error;
Al Viro625b6d12015-05-12 16:36:12 -04003444 error = mnt_want_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003445 if (unlikely(error))
3446 goto out;
Al Viro625b6d12015-05-12 16:36:12 -04003447 dir = path.dentry->d_inode;
Al Viro60545d02013-06-07 01:20:27 -04003448 /* we want directory to be writable */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003449 error = inode_permission2(nd->path.mnt, dir, MAY_WRITE | MAY_EXEC);
Al Viro60545d02013-06-07 01:20:27 -04003450 if (error)
3451 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003452 if (!dir->i_op->tmpfile) {
3453 error = -EOPNOTSUPP;
3454 goto out2;
3455 }
Al Viro625b6d12015-05-12 16:36:12 -04003456 child = d_alloc(path.dentry, &name);
Al Viro60545d02013-06-07 01:20:27 -04003457 if (unlikely(!child)) {
3458 error = -ENOMEM;
3459 goto out2;
3460 }
Al Viro625b6d12015-05-12 16:36:12 -04003461 dput(path.dentry);
3462 path.dentry = child;
3463 error = dir->i_op->tmpfile(dir, child, op->mode);
Al Viro60545d02013-06-07 01:20:27 -04003464 if (error)
3465 goto out2;
Al Viroc8a53ee2015-05-12 18:43:07 -04003466 audit_inode(nd->name, child, 0);
Eric Rannaud69a91c22014-10-30 01:51:01 -07003467 /* Don't check for other permissions, the inode was just created */
Al Viro62fb4a12015-12-26 22:33:24 -05003468 error = may_open(&path, 0, op->open_flag);
Al Viro60545d02013-06-07 01:20:27 -04003469 if (error)
3470 goto out2;
Al Viro625b6d12015-05-12 16:36:12 -04003471 file->f_path.mnt = path.mnt;
3472 error = finish_open(file, child, NULL, opened);
Al Viro60545d02013-06-07 01:20:27 -04003473 if (error)
3474 goto out2;
3475 error = open_check_o_direct(file);
Al Virof4e0c302013-06-11 08:34:36 +04003476 if (error) {
Al Viro60545d02013-06-07 01:20:27 -04003477 fput(file);
Al Virof4e0c302013-06-11 08:34:36 +04003478 } else if (!(op->open_flag & O_EXCL)) {
3479 struct inode *inode = file_inode(file);
3480 spin_lock(&inode->i_lock);
3481 inode->i_state |= I_LINKABLE;
3482 spin_unlock(&inode->i_lock);
3483 }
Al Viro60545d02013-06-07 01:20:27 -04003484out2:
Al Viro625b6d12015-05-12 16:36:12 -04003485 mnt_drop_write(path.mnt);
Al Viro60545d02013-06-07 01:20:27 -04003486out:
Al Viro625b6d12015-05-12 16:36:12 -04003487 path_put(&path);
Al Viro60545d02013-06-07 01:20:27 -04003488 return error;
3489}
3490
Al Viro6ac08702016-04-26 00:02:50 -04003491static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)
3492{
3493 struct path path;
3494 int error = path_lookupat(nd, flags, &path);
3495 if (!error) {
3496 audit_inode(nd->name, path.dentry, 0);
3497 error = vfs_open(&path, file, current_cred());
3498 path_put(&path);
3499 }
3500 return error;
3501}
3502
Al Viroc8a53ee2015-05-12 18:43:07 -04003503static struct file *path_openat(struct nameidata *nd,
3504 const struct open_flags *op, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505{
Al Viro368ee9b2015-05-08 17:19:59 -04003506 const char *s;
Al Viro30d90492012-06-22 12:40:19 +04003507 struct file *file;
Al Viro47237682012-06-10 05:01:45 -04003508 int opened = 0;
Al Viro13aab422011-02-23 17:54:08 -05003509 int error;
Nick Piggin31e6b012011-01-07 17:49:52 +11003510
Al Viro30d90492012-06-22 12:40:19 +04003511 file = get_empty_filp();
Al Viro1afc99b2013-02-14 20:41:04 -05003512 if (IS_ERR(file))
3513 return file;
Nick Piggin31e6b012011-01-07 17:49:52 +11003514
Al Viro30d90492012-06-22 12:40:19 +04003515 file->f_flags = op->open_flag;
Nick Piggin31e6b012011-01-07 17:49:52 +11003516
Al Virobb458c62013-07-13 13:26:37 +04003517 if (unlikely(file->f_flags & __O_TMPFILE)) {
Al Viroc8a53ee2015-05-12 18:43:07 -04003518 error = do_tmpfile(nd, flags, op, file, &opened);
Al Virof15133d2015-05-08 22:53:15 -04003519 goto out2;
Al Viro60545d02013-06-07 01:20:27 -04003520 }
3521
Al Viro6ac08702016-04-26 00:02:50 -04003522 if (unlikely(file->f_flags & O_PATH)) {
3523 error = do_o_path(nd, flags, file);
3524 if (!error)
3525 opened |= FILE_OPENED;
3526 goto out2;
3527 }
3528
Al Viroc8a53ee2015-05-12 18:43:07 -04003529 s = path_init(nd, flags);
Al Viro368ee9b2015-05-08 17:19:59 -04003530 if (IS_ERR(s)) {
3531 put_filp(file);
3532 return ERR_CAST(s);
3533 }
Al Viro3bdba282015-05-08 17:37:07 -04003534 while (!(error = link_path_walk(s, nd)) &&
Al Viro76ae2a52015-05-12 18:44:32 -04003535 (error = do_last(nd, file, op, &opened)) > 0) {
Al Viro73d049a2011-03-11 12:08:24 -05003536 nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL);
Al Viro3bdba282015-05-08 17:37:07 -04003537 s = trailing_symlink(nd);
3538 if (IS_ERR(s)) {
3539 error = PTR_ERR(s);
Al Viro2675a4e2012-06-22 12:41:10 +04003540 break;
Al Viro3bdba282015-05-08 17:37:07 -04003541 }
Al Viro806b6812009-12-26 07:16:40 -05003542 }
Al Virodeb106c2015-05-08 18:05:21 -04003543 terminate_walk(nd);
Al Virof15133d2015-05-08 22:53:15 -04003544out2:
Al Viro2675a4e2012-06-22 12:41:10 +04003545 if (!(opened & FILE_OPENED)) {
3546 BUG_ON(!error);
Al Viro30d90492012-06-22 12:40:19 +04003547 put_filp(file);
Miklos Szeredi015c3bb2012-06-05 15:10:27 +02003548 }
Al Viro2675a4e2012-06-22 12:41:10 +04003549 if (unlikely(error)) {
3550 if (error == -EOPENSTALE) {
3551 if (flags & LOOKUP_RCU)
3552 error = -ECHILD;
3553 else
3554 error = -ESTALE;
3555 }
3556 file = ERR_PTR(error);
3557 }
3558 return file;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560
Jeff Layton669abf42012-10-10 16:43:10 -04003561struct file *do_filp_open(int dfd, struct filename *pathname,
Al Virof9652e12013-06-11 08:23:01 +04003562 const struct open_flags *op)
Al Viro13aab422011-02-23 17:54:08 -05003563{
Al Viro9883d182015-05-13 07:28:08 -04003564 struct nameidata nd;
Al Virof9652e12013-06-11 08:23:01 +04003565 int flags = op->lookup_flags;
Al Viro13aab422011-02-23 17:54:08 -05003566 struct file *filp;
3567
Al Viro9883d182015-05-13 07:28:08 -04003568 set_nameidata(&nd, dfd, pathname);
Al Viroc8a53ee2015-05-12 18:43:07 -04003569 filp = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro13aab422011-02-23 17:54:08 -05003570 if (unlikely(filp == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003571 filp = path_openat(&nd, op, flags);
Al Viro13aab422011-02-23 17:54:08 -05003572 if (unlikely(filp == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003573 filp = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003574 restore_nameidata();
Al Viro13aab422011-02-23 17:54:08 -05003575 return filp;
3576}
3577
Al Viro73d049a2011-03-11 12:08:24 -05003578struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt,
Al Virof9652e12013-06-11 08:23:01 +04003579 const char *name, const struct open_flags *op)
Al Viro73d049a2011-03-11 12:08:24 -05003580{
Al Viro9883d182015-05-13 07:28:08 -04003581 struct nameidata nd;
Al Viro73d049a2011-03-11 12:08:24 -05003582 struct file *file;
Paul Moore51689102015-01-22 00:00:03 -05003583 struct filename *filename;
Al Virof9652e12013-06-11 08:23:01 +04003584 int flags = op->lookup_flags | LOOKUP_ROOT;
Al Viro73d049a2011-03-11 12:08:24 -05003585
3586 nd.root.mnt = mnt;
3587 nd.root.dentry = dentry;
3588
David Howellsb18825a2013-09-12 19:22:53 +01003589 if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN)
Al Viro73d049a2011-03-11 12:08:24 -05003590 return ERR_PTR(-ELOOP);
3591
Paul Moore51689102015-01-22 00:00:03 -05003592 filename = getname_kernel(name);
Viresh Kumara1c83682015-08-12 15:59:44 +05303593 if (IS_ERR(filename))
Paul Moore51689102015-01-22 00:00:03 -05003594 return ERR_CAST(filename);
3595
Al Viro9883d182015-05-13 07:28:08 -04003596 set_nameidata(&nd, -1, filename);
Al Viroc8a53ee2015-05-12 18:43:07 -04003597 file = path_openat(&nd, op, flags | LOOKUP_RCU);
Al Viro73d049a2011-03-11 12:08:24 -05003598 if (unlikely(file == ERR_PTR(-ECHILD)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003599 file = path_openat(&nd, op, flags);
Al Viro73d049a2011-03-11 12:08:24 -05003600 if (unlikely(file == ERR_PTR(-ESTALE)))
Al Viroc8a53ee2015-05-12 18:43:07 -04003601 file = path_openat(&nd, op, flags | LOOKUP_REVAL);
Al Viro9883d182015-05-13 07:28:08 -04003602 restore_nameidata();
Paul Moore51689102015-01-22 00:00:03 -05003603 putname(filename);
Al Viro73d049a2011-03-11 12:08:24 -05003604 return file;
3605}
3606
Al Virofa14a0b2015-01-22 02:16:49 -05003607static struct dentry *filename_create(int dfd, struct filename *name,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003608 struct path *path, unsigned int lookup_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003610 struct dentry *dentry = ERR_PTR(-EEXIST);
Al Viro391172c2015-05-09 11:19:16 -04003611 struct qstr last;
3612 int type;
Jan Karac30dabf2012-06-12 16:20:30 +02003613 int err2;
Jeff Layton1ac12b42012-12-11 12:10:06 -05003614 int error;
3615 bool is_dir = (lookup_flags & LOOKUP_DIRECTORY);
3616
3617 /*
3618 * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any
3619 * other flags passed in are ignored!
3620 */
3621 lookup_flags &= LOOKUP_REVAL;
3622
Al Viro5c31b6c2015-05-12 17:32:54 -04003623 name = filename_parentat(dfd, name, lookup_flags, path, &last, &type);
3624 if (IS_ERR(name))
3625 return ERR_CAST(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003627 /*
3628 * Yucky last component or no last component at all?
3629 * (foo/., foo/.., /////)
3630 */
Al Viro5c31b6c2015-05-12 17:32:54 -04003631 if (unlikely(type != LAST_NORM))
Al Viroed75e952011-06-27 16:53:43 -04003632 goto out;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003633
Jan Karac30dabf2012-06-12 16:20:30 +02003634 /* don't fail immediately if it's r/o, at least try to report other errors */
Al Viro391172c2015-05-09 11:19:16 -04003635 err2 = mnt_want_write(path->mnt);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003636 /*
3637 * Do the final lookup.
3638 */
Al Viro391172c2015-05-09 11:19:16 -04003639 lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL;
Al Viro59551022016-01-22 15:40:57 -05003640 inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
Al Viro391172c2015-05-09 11:19:16 -04003641 dentry = __lookup_hash(&last, path->dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 if (IS_ERR(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003643 goto unlock;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003644
Al Viroa8104a92012-07-20 02:25:00 +04003645 error = -EEXIST;
David Howellsb18825a2013-09-12 19:22:53 +01003646 if (d_is_positive(dentry))
Al Viroa8104a92012-07-20 02:25:00 +04003647 goto fail;
David Howellsb18825a2013-09-12 19:22:53 +01003648
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07003649 /*
3650 * Special case - lookup gave negative, but... we had foo/bar/
3651 * From the vfs_mknod() POV we just have a negative dentry -
3652 * all is fine. Let's be bastards - you had / on the end, you've
3653 * been asking for (non-existent) directory. -ENOENT for you.
3654 */
Al Viro391172c2015-05-09 11:19:16 -04003655 if (unlikely(!is_dir && last.name[last.len])) {
Al Viroa8104a92012-07-20 02:25:00 +04003656 error = -ENOENT;
Al Viroed75e952011-06-27 16:53:43 -04003657 goto fail;
Al Viroe9baf6e2008-05-15 04:49:12 -04003658 }
Jan Karac30dabf2012-06-12 16:20:30 +02003659 if (unlikely(err2)) {
3660 error = err2;
Al Viroa8104a92012-07-20 02:25:00 +04003661 goto fail;
Jan Karac30dabf2012-06-12 16:20:30 +02003662 }
Al Viro181c37b2015-05-12 17:21:25 -04003663 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 return dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665fail:
Al Viroa8104a92012-07-20 02:25:00 +04003666 dput(dentry);
3667 dentry = ERR_PTR(error);
3668unlock:
Al Viro59551022016-01-22 15:40:57 -05003669 inode_unlock(path->dentry->d_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02003670 if (!err2)
Al Viro391172c2015-05-09 11:19:16 -04003671 mnt_drop_write(path->mnt);
Al Viroed75e952011-06-27 16:53:43 -04003672out:
Al Viro391172c2015-05-09 11:19:16 -04003673 path_put(path);
Al Viro181c37b2015-05-12 17:21:25 -04003674 putname(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 return dentry;
3676}
Al Virofa14a0b2015-01-22 02:16:49 -05003677
3678struct dentry *kern_path_create(int dfd, const char *pathname,
3679 struct path *path, unsigned int lookup_flags)
3680{
Al Viro181c37b2015-05-12 17:21:25 -04003681 return filename_create(dfd, getname_kernel(pathname),
3682 path, lookup_flags);
Al Virofa14a0b2015-01-22 02:16:49 -05003683}
Al Virodae6ad82011-06-26 11:50:15 -04003684EXPORT_SYMBOL(kern_path_create);
3685
Al Viro921a1652012-07-20 01:15:31 +04003686void done_path_create(struct path *path, struct dentry *dentry)
3687{
3688 dput(dentry);
Al Viro59551022016-01-22 15:40:57 -05003689 inode_unlock(path->dentry->d_inode);
Al Viroa8104a92012-07-20 02:25:00 +04003690 mnt_drop_write(path->mnt);
Al Viro921a1652012-07-20 01:15:31 +04003691 path_put(path);
3692}
3693EXPORT_SYMBOL(done_path_create);
3694
Al Viro520ae682015-05-13 07:00:28 -04003695inline struct dentry *user_path_create(int dfd, const char __user *pathname,
Jeff Layton1ac12b42012-12-11 12:10:06 -05003696 struct path *path, unsigned int lookup_flags)
Al Virodae6ad82011-06-26 11:50:15 -04003697{
Al Viro181c37b2015-05-12 17:21:25 -04003698 return filename_create(dfd, getname(pathname), path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003699}
3700EXPORT_SYMBOL(user_path_create);
3701
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003702int vfs_mknod2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703{
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003704 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705
3706 if (error)
3707 return error;
3708
Eric W. Biederman975d6b32011-11-13 12:16:43 -08003709 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 return -EPERM;
3711
Al Viroacfa4382008-12-04 10:06:33 -05003712 if (!dir->i_op->mknod)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 return -EPERM;
3714
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07003715 error = devcgroup_inode_mknod(mode, dev);
3716 if (error)
3717 return error;
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 error = security_inode_mknod(dir, dentry, mode, dev);
3720 if (error)
3721 return error;
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003724 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003725 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 return error;
3727}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003728EXPORT_SYMBOL(vfs_mknod2);
3729
3730int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
3731{
3732 return vfs_mknod2(NULL, dir, dentry, mode, dev);
3733}
Al Viro4d359502014-03-14 12:20:17 -04003734EXPORT_SYMBOL(vfs_mknod);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735
Al Virof69aac02011-07-26 04:31:40 -04003736static int may_mknod(umode_t mode)
Dave Hansen463c3192008-02-15 14:37:57 -08003737{
3738 switch (mode & S_IFMT) {
3739 case S_IFREG:
3740 case S_IFCHR:
3741 case S_IFBLK:
3742 case S_IFIFO:
3743 case S_IFSOCK:
3744 case 0: /* zero mode translates to S_IFREG */
3745 return 0;
3746 case S_IFDIR:
3747 return -EPERM;
3748 default:
3749 return -EINVAL;
3750 }
3751}
3752
Al Viro8208a222011-07-25 17:32:17 -04003753SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode,
Heiko Carstens2e4d0922009-01-14 14:14:31 +01003754 unsigned, dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Al Viro2ad94ae2008-07-21 09:32:51 -04003756 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003757 struct path path;
3758 int error;
Jeff Layton972567f2012-12-20 16:00:10 -05003759 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Al Viro8e4bfca2012-07-20 01:17:26 +04003761 error = may_mknod(mode);
3762 if (error)
3763 return error;
Jeff Layton972567f2012-12-20 16:00:10 -05003764retry:
3765 dentry = user_path_create(dfd, filename, &path, lookup_flags);
Al Virodae6ad82011-06-26 11:50:15 -04003766 if (IS_ERR(dentry))
3767 return PTR_ERR(dentry);
Al Viro2ad94ae2008-07-21 09:32:51 -04003768
Al Virodae6ad82011-06-26 11:50:15 -04003769 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003770 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003771 error = security_path_mknod(&path, dentry, mode, dev);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003772 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04003773 goto out;
Dave Hansen463c3192008-02-15 14:37:57 -08003774 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 case 0: case S_IFREG:
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003776 error = vfs_create2(path.mnt, path.dentry->d_inode,dentry,mode,true);
Mimi Zohar05d1a712016-02-29 19:52:05 -05003777 if (!error)
3778 ima_post_path_mknod(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 break;
3780 case S_IFCHR: case S_IFBLK:
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003781 error = vfs_mknod2(path.mnt, path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 new_decode_dev(dev));
3783 break;
3784 case S_IFIFO: case S_IFSOCK:
Al Virodae6ad82011-06-26 11:50:15 -04003785 error = vfs_mknod(path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
Al Viroa8104a92012-07-20 02:25:00 +04003788out:
Al Viro921a1652012-07-20 01:15:31 +04003789 done_path_create(&path, dentry);
Jeff Layton972567f2012-12-20 16:00:10 -05003790 if (retry_estale(error, lookup_flags)) {
3791 lookup_flags |= LOOKUP_REVAL;
3792 goto retry;
3793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return error;
3795}
3796
Al Viro8208a222011-07-25 17:32:17 -04003797SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003798{
3799 return sys_mknodat(AT_FDCWD, filename, mode, dev);
3800}
3801
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003802int vfs_mkdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, umode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803{
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003804 int error = may_create(mnt, dir, dentry);
Al Viro8de52772012-02-06 12:45:27 -05003805 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806
3807 if (error)
3808 return error;
3809
Al Viroacfa4382008-12-04 10:06:33 -05003810 if (!dir->i_op->mkdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return -EPERM;
3812
3813 mode &= (S_IRWXUGO|S_ISVTX);
3814 error = security_inode_mkdir(dir, dentry, mode);
3815 if (error)
3816 return error;
3817
Al Viro8de52772012-02-06 12:45:27 -05003818 if (max_links && dir->i_nlink >= max_links)
3819 return -EMLINK;
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07003822 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00003823 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 return error;
3825}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003826EXPORT_SYMBOL(vfs_mkdir2);
3827
3828int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
3829{
3830 return vfs_mkdir2(NULL, dir, dentry, mode);
3831}
Al Viro4d359502014-03-14 12:20:17 -04003832EXPORT_SYMBOL(vfs_mkdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833
Al Viroa218d0f2011-11-21 14:59:34 -05003834SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
Dave Hansen6902d922006-09-30 23:29:01 -07003836 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04003837 struct path path;
3838 int error;
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003839 unsigned int lookup_flags = LOOKUP_DIRECTORY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003841retry:
3842 dentry = user_path_create(dfd, pathname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07003843 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04003844 return PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003845
Al Virodae6ad82011-06-26 11:50:15 -04003846 if (!IS_POSIXACL(path.dentry->d_inode))
Al Viroce3b0f82009-03-29 19:08:22 -04003847 mode &= ~current_umask();
Al Virodae6ad82011-06-26 11:50:15 -04003848 error = security_path_mkdir(&path, dentry, mode);
Al Viroa8104a92012-07-20 02:25:00 +04003849 if (!error)
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003850 error = vfs_mkdir2(path.mnt, path.dentry->d_inode, dentry, mode);
Al Viro921a1652012-07-20 01:15:31 +04003851 done_path_create(&path, dentry);
Jeff Laytonb76d8b82012-12-20 16:04:09 -05003852 if (retry_estale(error, lookup_flags)) {
3853 lookup_flags |= LOOKUP_REVAL;
3854 goto retry;
3855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return error;
3857}
3858
Al Viroa218d0f2011-11-21 14:59:34 -05003859SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003860{
3861 return sys_mkdirat(AT_FDCWD, pathname, mode);
3862}
3863
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003864int vfs_rmdir2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003866 int error = may_delete(mnt, dir, dentry, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867
3868 if (error)
3869 return error;
3870
Al Viroacfa4382008-12-04 10:06:33 -05003871 if (!dir->i_op->rmdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 return -EPERM;
3873
Al Viro1d2ef592011-09-14 18:55:41 +01003874 dget(dentry);
Al Viro59551022016-01-22 15:40:57 -05003875 inode_lock(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876
Sage Weil912dbc12011-05-24 13:06:11 -07003877 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07003878 if (is_local_mountpoint(dentry))
Sage Weil912dbc12011-05-24 13:06:11 -07003879 goto out;
3880
3881 error = security_inode_rmdir(dir, dentry);
3882 if (error)
3883 goto out;
3884
Sage Weil3cebde22011-05-29 21:20:59 -07003885 shrink_dcache_parent(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003886 error = dir->i_op->rmdir(dir, dentry);
3887 if (error)
3888 goto out;
3889
3890 dentry->d_inode->i_flags |= S_DEAD;
3891 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07003892 detach_mounts(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003893
3894out:
Al Viro59551022016-01-22 15:40:57 -05003895 inode_unlock(dentry->d_inode);
Al Viro1d2ef592011-09-14 18:55:41 +01003896 dput(dentry);
Sage Weil912dbc12011-05-24 13:06:11 -07003897 if (!error)
3898 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return error;
3900}
Guenter Roeckf1222162017-01-30 12:29:00 -08003901EXPORT_SYMBOL(vfs_rmdir2);
3902
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003903int vfs_rmdir(struct inode *dir, struct dentry *dentry)
3904{
3905 return vfs_rmdir2(NULL, dir, dentry);
3906}
Al Viro4d359502014-03-14 12:20:17 -04003907EXPORT_SYMBOL(vfs_rmdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003909static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
3911 int error = 0;
Jeff Layton91a27b22012-10-10 15:25:28 -04003912 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04003914 struct path path;
3915 struct qstr last;
3916 int type;
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003917 unsigned int lookup_flags = 0;
3918retry:
Al Virof5beed72015-04-30 16:09:11 -04003919 name = user_path_parent(dfd, pathname,
3920 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04003921 if (IS_ERR(name))
3922 return PTR_ERR(name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Al Virof5beed72015-04-30 16:09:11 -04003924 switch (type) {
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003925 case LAST_DOTDOT:
3926 error = -ENOTEMPTY;
3927 goto exit1;
3928 case LAST_DOT:
3929 error = -EINVAL;
3930 goto exit1;
3931 case LAST_ROOT:
3932 error = -EBUSY;
3933 goto exit1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 }
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003935
Al Virof5beed72015-04-30 16:09:11 -04003936 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02003937 if (error)
3938 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09003939
Al Viro59551022016-01-22 15:40:57 -05003940 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04003941 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07003943 if (IS_ERR(dentry))
3944 goto exit2;
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04003945 if (!dentry->d_inode) {
3946 error = -ENOENT;
3947 goto exit3;
3948 }
Al Virof5beed72015-04-30 16:09:11 -04003949 error = security_path_rmdir(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09003950 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02003951 goto exit3;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003952 error = vfs_rmdir2(path.mnt, path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08003953exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07003954 dput(dentry);
3955exit2:
Al Viro59551022016-01-22 15:40:57 -05003956 inode_unlock(path.dentry->d_inode);
Al Virof5beed72015-04-30 16:09:11 -04003957 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958exit1:
Al Virof5beed72015-04-30 16:09:11 -04003959 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 putname(name);
Jeff Laytonc6ee9202012-12-20 16:28:33 -05003961 if (retry_estale(error, lookup_flags)) {
3962 lookup_flags |= LOOKUP_REVAL;
3963 goto retry;
3964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 return error;
3966}
3967
Heiko Carstens3cdad422009-01-14 14:14:22 +01003968SYSCALL_DEFINE1(rmdir, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08003969{
3970 return do_rmdir(AT_FDCWD, pathname);
3971}
3972
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04003973/**
3974 * vfs_unlink - unlink a filesystem object
3975 * @dir: parent directory
3976 * @dentry: victim
3977 * @delegated_inode: returns victim inode, if the inode is delegated.
3978 *
3979 * The caller must hold dir->i_mutex.
3980 *
3981 * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and
3982 * return a reference to the inode in delegated_inode. The caller
3983 * should then break the delegation on that inode and retry. Because
3984 * breaking a delegation may take a long time, the caller should drop
3985 * dir->i_mutex before doing so.
3986 *
3987 * Alternatively, a caller may pass NULL for delegated_inode. This may
3988 * be appropriate for callers that expect the underlying filesystem not
3989 * to be NFS exported.
3990 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003991int vfs_unlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
J. Bruce Fields9accbb92012-08-28 07:03:24 -04003993 struct inode *target = dentry->d_inode;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07003994 int error = may_delete(mnt, dir, dentry, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
3996 if (error)
3997 return error;
3998
Al Viroacfa4382008-12-04 10:06:33 -05003999 if (!dir->i_op->unlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 return -EPERM;
4001
Al Viro59551022016-01-22 15:40:57 -05004002 inode_lock(target);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004003 if (is_local_mountpoint(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 error = -EBUSY;
4005 else {
4006 error = security_inode_unlink(dir, dentry);
Al Virobec10522010-03-03 14:12:08 -05004007 if (!error) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004008 error = try_break_deleg(target, delegated_inode);
4009 if (error)
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004010 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 error = dir->i_op->unlink(dir, dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004012 if (!error) {
Al Virod83c49f2010-04-30 17:17:09 -04004013 dont_mount(dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004014 detach_mounts(dentry);
4015 }
Al Virobec10522010-03-03 14:12:08 -05004016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 }
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004018out:
Al Viro59551022016-01-22 15:40:57 -05004019 inode_unlock(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
4022 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
J. Bruce Fields9accbb92012-08-28 07:03:24 -04004023 fsnotify_link_count(target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 }
Robert Love0eeca282005-07-12 17:06:03 -04004026
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 return error;
4028}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004029EXPORT_SYMBOL(vfs_unlink2);
4030
4031int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode)
4032{
4033 return vfs_unlink2(NULL, dir, dentry, delegated_inode);
4034}
Al Viro4d359502014-03-14 12:20:17 -04004035EXPORT_SYMBOL(vfs_unlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
4037/*
4038 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004039 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 * writeout happening, and we don't want to prevent access to the directory
4041 * while waiting on the I/O.
4042 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004043static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Al Viro2ad94ae2008-07-21 09:32:51 -04004045 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004046 struct filename *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 struct dentry *dentry;
Al Virof5beed72015-04-30 16:09:11 -04004048 struct path path;
4049 struct qstr last;
4050 int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 struct inode *inode = NULL;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004052 struct inode *delegated_inode = NULL;
Jeff Layton5d18f812012-12-20 16:38:04 -05004053 unsigned int lookup_flags = 0;
4054retry:
Al Virof5beed72015-04-30 16:09:11 -04004055 name = user_path_parent(dfd, pathname,
4056 &path, &last, &type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004057 if (IS_ERR(name))
4058 return PTR_ERR(name);
Al Viro2ad94ae2008-07-21 09:32:51 -04004059
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 error = -EISDIR;
Al Virof5beed72015-04-30 16:09:11 -04004061 if (type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 goto exit1;
OGAWA Hirofumi0612d9f2008-10-16 07:50:29 +09004063
Al Virof5beed72015-04-30 16:09:11 -04004064 error = mnt_want_write(path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004065 if (error)
4066 goto exit1;
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004067retry_deleg:
Al Viro59551022016-01-22 15:40:57 -05004068 inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT);
Al Virof5beed72015-04-30 16:09:11 -04004069 dentry = __lookup_hash(&last, path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 error = PTR_ERR(dentry);
4071 if (!IS_ERR(dentry)) {
4072 /* Why not before? Because we want correct error value */
Al Virof5beed72015-04-30 16:09:11 -04004073 if (last.name[last.len])
Török Edwin50338b82011-06-16 00:06:14 +03004074 goto slashes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 inode = dentry->d_inode;
David Howellsb18825a2013-09-12 19:22:53 +01004076 if (d_is_negative(dentry))
Theodore Ts'oe6bc45d2011-06-06 19:19:40 -04004077 goto slashes;
4078 ihold(inode);
Al Virof5beed72015-04-30 16:09:11 -04004079 error = security_path_unlink(&path, dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004080 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004081 goto exit2;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004082 error = vfs_unlink2(path.mnt, path.dentry->d_inode, dentry, &delegated_inode);
Jan Karac30dabf2012-06-12 16:20:30 +02004083exit2:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 dput(dentry);
4085 }
Al Viro59551022016-01-22 15:40:57 -05004086 inode_unlock(path.dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 if (inode)
4088 iput(inode); /* truncate the inode here */
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004089 inode = NULL;
4090 if (delegated_inode) {
J. Bruce Fields5a146962012-08-28 07:50:40 -07004091 error = break_deleg_wait(&delegated_inode);
J. Bruce Fieldsb21996e2011-09-20 09:14:34 -04004092 if (!error)
4093 goto retry_deleg;
4094 }
Al Virof5beed72015-04-30 16:09:11 -04004095 mnt_drop_write(path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096exit1:
Al Virof5beed72015-04-30 16:09:11 -04004097 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 putname(name);
Jeff Layton5d18f812012-12-20 16:38:04 -05004099 if (retry_estale(error, lookup_flags)) {
4100 lookup_flags |= LOOKUP_REVAL;
4101 inode = NULL;
4102 goto retry;
4103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 return error;
4105
4106slashes:
David Howellsb18825a2013-09-12 19:22:53 +01004107 if (d_is_negative(dentry))
4108 error = -ENOENT;
Miklos Szeredi44b1d532014-04-01 17:08:41 +02004109 else if (d_is_dir(dentry))
David Howellsb18825a2013-09-12 19:22:53 +01004110 error = -EISDIR;
4111 else
4112 error = -ENOTDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113 goto exit2;
4114}
4115
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004116SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004117{
4118 if ((flag & ~AT_REMOVEDIR) != 0)
4119 return -EINVAL;
4120
4121 if (flag & AT_REMOVEDIR)
4122 return do_rmdir(dfd, pathname);
4123
4124 return do_unlinkat(dfd, pathname);
4125}
4126
Heiko Carstens3480b252009-01-14 14:14:16 +01004127SYSCALL_DEFINE1(unlink, const char __user *, pathname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004128{
4129 return do_unlinkat(AT_FDCWD, pathname);
4130}
4131
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004132int vfs_symlink2(struct vfsmount *mnt, struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133{
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004134 int error = may_create(mnt, dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
4136 if (error)
4137 return error;
4138
Al Viroacfa4382008-12-04 10:06:33 -05004139 if (!dir->i_op->symlink)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 return -EPERM;
4141
4142 error = security_inode_symlink(dir, dentry, oldname);
4143 if (error)
4144 return error;
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07004147 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00004148 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 return error;
4150}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004151EXPORT_SYMBOL(vfs_symlink2);
4152
4153int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
4154{
4155 return vfs_symlink2(NULL, dir, dentry, oldname);
4156}
Al Viro4d359502014-03-14 12:20:17 -04004157EXPORT_SYMBOL(vfs_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004159SYSCALL_DEFINE3(symlinkat, const char __user *, oldname,
4160 int, newdfd, const char __user *, newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Al Viro2ad94ae2008-07-21 09:32:51 -04004162 int error;
Jeff Layton91a27b22012-10-10 15:25:28 -04004163 struct filename *from;
Dave Hansen6902d922006-09-30 23:29:01 -07004164 struct dentry *dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004165 struct path path;
Jeff Laytonf46d3562012-12-11 12:10:08 -05004166 unsigned int lookup_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
4168 from = getname(oldname);
Al Viro2ad94ae2008-07-21 09:32:51 -04004169 if (IS_ERR(from))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return PTR_ERR(from);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004171retry:
4172 dentry = user_path_create(newdfd, newname, &path, lookup_flags);
Dave Hansen6902d922006-09-30 23:29:01 -07004173 error = PTR_ERR(dentry);
4174 if (IS_ERR(dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004175 goto out_putname;
Dave Hansen6902d922006-09-30 23:29:01 -07004176
Jeff Layton91a27b22012-10-10 15:25:28 -04004177 error = security_path_symlink(&path, dentry, from->name);
Al Viroa8104a92012-07-20 02:25:00 +04004178 if (!error)
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004179 error = vfs_symlink2(path.mnt, path.dentry->d_inode, dentry, from->name);
Al Viro921a1652012-07-20 01:15:31 +04004180 done_path_create(&path, dentry);
Jeff Laytonf46d3562012-12-11 12:10:08 -05004181 if (retry_estale(error, lookup_flags)) {
4182 lookup_flags |= LOOKUP_REVAL;
4183 goto retry;
4184 }
Dave Hansen6902d922006-09-30 23:29:01 -07004185out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 putname(from);
4187 return error;
4188}
4189
Heiko Carstens3480b252009-01-14 14:14:16 +01004190SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004191{
4192 return sys_symlinkat(oldname, AT_FDCWD, newname);
4193}
4194
J. Bruce Fields146a8592011-09-20 17:14:31 -04004195/**
4196 * vfs_link - create a new link
4197 * @old_dentry: object to be linked
4198 * @dir: new parent
4199 * @new_dentry: where to create the new link
4200 * @delegated_inode: returns inode needing a delegation break
4201 *
4202 * The caller must hold dir->i_mutex
4203 *
4204 * If vfs_link discovers a delegation on the to-be-linked file in need
4205 * of breaking, it will return -EWOULDBLOCK and return a reference to the
4206 * inode in delegated_inode. The caller should then break the delegation
4207 * and retry. Because breaking a delegation may take a long time, the
4208 * caller should drop the i_mutex before doing so.
4209 *
4210 * Alternatively, a caller may pass NULL for delegated_inode. This may
4211 * be appropriate for callers that expect the underlying filesystem not
4212 * to be NFS exported.
4213 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004214int vfs_link2(struct vfsmount *mnt, struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215{
4216 struct inode *inode = old_dentry->d_inode;
Al Viro8de52772012-02-06 12:45:27 -05004217 unsigned max_links = dir->i_sb->s_max_links;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 int error;
4219
4220 if (!inode)
4221 return -ENOENT;
4222
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004223 error = may_create(mnt, dir, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 if (error)
4225 return error;
4226
4227 if (dir->i_sb != inode->i_sb)
4228 return -EXDEV;
4229
4230 /*
4231 * A link to an append-only or immutable file cannot be created.
4232 */
4233 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4234 return -EPERM;
Eric W. Biederman0bd23d092016-06-29 14:54:46 -05004235 /*
4236 * Updating the link count will likely cause i_uid and i_gid to
4237 * be writen back improperly if their true value is unknown to
4238 * the vfs.
4239 */
4240 if (HAS_UNMAPPED_ID(inode))
4241 return -EPERM;
Al Viroacfa4382008-12-04 10:06:33 -05004242 if (!dir->i_op->link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004244 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 return -EPERM;
4246
4247 error = security_inode_link(old_dentry, dir, new_dentry);
4248 if (error)
4249 return error;
4250
Al Viro59551022016-01-22 15:40:57 -05004251 inode_lock(inode);
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304252 /* Make sure we don't allow creating hardlink to an unlinked file */
Al Virof4e0c302013-06-11 08:34:36 +04004253 if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE))
Aneesh Kumar K.Vaae8a972011-01-29 18:43:27 +05304254 error = -ENOENT;
Al Viro8de52772012-02-06 12:45:27 -05004255 else if (max_links && inode->i_nlink >= max_links)
4256 error = -EMLINK;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004257 else {
4258 error = try_break_deleg(inode, delegated_inode);
4259 if (!error)
4260 error = dir->i_op->link(old_dentry, dir, new_dentry);
4261 }
Al Virof4e0c302013-06-11 08:34:36 +04004262
4263 if (!error && (inode->i_state & I_LINKABLE)) {
4264 spin_lock(&inode->i_lock);
4265 inode->i_state &= ~I_LINKABLE;
4266 spin_unlock(&inode->i_lock);
4267 }
Al Viro59551022016-01-22 15:40:57 -05004268 inode_unlock(inode);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07004269 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02004270 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 return error;
4272}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004273EXPORT_SYMBOL(vfs_link2);
4274
4275int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode)
4276{
4277 return vfs_link2(NULL, old_dentry, dir, new_dentry, delegated_inode);
4278}
Al Viro4d359502014-03-14 12:20:17 -04004279EXPORT_SYMBOL(vfs_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281/*
4282 * Hardlinks are often used in delicate situations. We avoid
4283 * security-related surprises by not following symlinks on the
4284 * newname. --KAB
4285 *
4286 * We don't follow them on the oldname either to be compatible
4287 * with linux 2.0, and to avoid hard-linking to directories
4288 * and other special files. --ADM
4289 */
Heiko Carstens2e4d0922009-01-14 14:14:31 +01004290SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname,
4291 int, newdfd, const char __user *, newname, int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292{
4293 struct dentry *new_dentry;
Al Virodae6ad82011-06-26 11:50:15 -04004294 struct path old_path, new_path;
J. Bruce Fields146a8592011-09-20 17:14:31 -04004295 struct inode *delegated_inode = NULL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304296 int how = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304299 if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004300 return -EINVAL;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304301 /*
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004302 * To use null names we require CAP_DAC_READ_SEARCH
4303 * This ensures that not everyone will be able to create
4304 * handlink using the passed filedescriptor.
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304305 */
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004306 if (flags & AT_EMPTY_PATH) {
4307 if (!capable(CAP_DAC_READ_SEARCH))
4308 return -ENOENT;
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304309 how = LOOKUP_EMPTY;
Linus Torvaldsf0cc6ff2013-08-28 09:18:05 -07004310 }
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004311
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304312 if (flags & AT_SYMLINK_FOLLOW)
4313 how |= LOOKUP_FOLLOW;
Jeff Layton442e31c2012-12-20 16:15:38 -05004314retry:
Aneesh Kumar K.V11a7b372011-01-29 18:43:42 +05304315 error = user_path_at(olddfd, oldname, how, &old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 if (error)
Al Viro2ad94ae2008-07-21 09:32:51 -04004317 return error;
4318
Jeff Layton442e31c2012-12-20 16:15:38 -05004319 new_dentry = user_path_create(newdfd, newname, &new_path,
4320 (how & LOOKUP_REVAL));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07004322 if (IS_ERR(new_dentry))
Al Virodae6ad82011-06-26 11:50:15 -04004323 goto out;
4324
4325 error = -EXDEV;
4326 if (old_path.mnt != new_path.mnt)
4327 goto out_dput;
Kees Cook800179c2012-07-25 17:29:07 -07004328 error = may_linkat(&old_path);
4329 if (unlikely(error))
4330 goto out_dput;
Al Virodae6ad82011-06-26 11:50:15 -04004331 error = security_path_link(old_path.dentry, &new_path, new_dentry);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004332 if (error)
Al Viroa8104a92012-07-20 02:25:00 +04004333 goto out_dput;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004334 error = vfs_link2(old_path.mnt, old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode);
Dave Hansen75c3f292008-02-15 14:37:45 -08004335out_dput:
Al Viro921a1652012-07-20 01:15:31 +04004336 done_path_create(&new_path, new_dentry);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004337 if (delegated_inode) {
4338 error = break_deleg_wait(&delegated_inode);
Oleg Drokind22e6332014-01-31 15:41:58 -05004339 if (!error) {
4340 path_put(&old_path);
J. Bruce Fields146a8592011-09-20 17:14:31 -04004341 goto retry;
Oleg Drokind22e6332014-01-31 15:41:58 -05004342 }
J. Bruce Fields146a8592011-09-20 17:14:31 -04004343 }
Jeff Layton442e31c2012-12-20 16:15:38 -05004344 if (retry_estale(error, how)) {
Oleg Drokind22e6332014-01-31 15:41:58 -05004345 path_put(&old_path);
Jeff Layton442e31c2012-12-20 16:15:38 -05004346 how |= LOOKUP_REVAL;
4347 goto retry;
4348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349out:
Al Viro2d8f3032008-07-22 09:59:21 -04004350 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 return error;
4353}
4354
Heiko Carstens3480b252009-01-14 14:14:16 +01004355SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004356{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08004357 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004358}
4359
Miklos Szeredibc270272014-04-01 17:08:42 +02004360/**
4361 * vfs_rename - rename a filesystem object
4362 * @old_dir: parent of source
4363 * @old_dentry: source
4364 * @new_dir: parent of destination
4365 * @new_dentry: destination
4366 * @delegated_inode: returns an inode needing a delegation break
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004367 * @flags: rename flags
Miklos Szeredibc270272014-04-01 17:08:42 +02004368 *
4369 * The caller must hold multiple mutexes--see lock_rename()).
4370 *
4371 * If vfs_rename discovers a delegation in need of breaking at either
4372 * the source or destination, it will return -EWOULDBLOCK and return a
4373 * reference to the inode in delegated_inode. The caller should then
4374 * break the delegation and retry. Because breaking a delegation may
4375 * take a long time, the caller should drop all locks before doing
4376 * so.
4377 *
4378 * Alternatively, a caller may pass NULL for delegated_inode. This may
4379 * be appropriate for callers that expect the underlying filesystem not
4380 * to be NFS exported.
4381 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382 * The worst of all namespace operations - renaming directory. "Perverted"
4383 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
4384 * Problems:
J. Bruce Fieldsd03b29a2014-02-17 16:52:33 -05004385 * a) we can get into loop creation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 * b) race potential - two innocent renames can create a loop together.
4387 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004388 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389 * story.
J. Bruce Fields6cedba82012-03-05 11:40:41 -05004390 * c) we have to lock _four_ objects - parents and victim (if it exists),
4391 * and source (if it is not a directory).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004392 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 * whether the target exists). Solution: try to be smart with locking
4394 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004395 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 * move will be locked. Thus we can rank directories by the tree
4397 * (ancestors first) and rank all non-directories after them.
4398 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08004399 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 * HOWEVER, it relies on the assumption that any object with ->lookup()
4401 * has no more than 1 dentry. If "hybrid" objects will ever appear,
4402 * we'd better make sure that there's no link(2) for them.
Sage Weile4eaac02011-05-24 13:06:07 -07004403 * d) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08004404 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Adam Buchbinderc41b20e2009-12-11 16:35:39 -05004406 * ->i_mutex on parents, which works but leads to some truly excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 * locking].
4408 */
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004409int vfs_rename2(struct vfsmount *mnt,
4410 struct inode *old_dir, struct dentry *old_dentry,
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004411 struct inode *new_dir, struct dentry *new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004412 struct inode **delegated_inode, unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
4414 int error;
Miklos Szeredibc270272014-04-01 17:08:42 +02004415 bool is_dir = d_is_dir(old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004416 struct inode *source = old_dentry->d_inode;
4417 struct inode *target = new_dentry->d_inode;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004418 bool new_is_dir = false;
4419 unsigned max_links = new_dir->i_sb->s_max_links;
Al Viroad25f112017-07-07 14:51:19 -04004420 struct name_snapshot old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Miklos Szeredi9409e222016-05-11 01:16:37 +02004422 /*
4423 * Check source == target.
4424 * On overlayfs need to look at underlying inodes.
4425 */
Miklos Szeredi2d902672016-06-30 08:53:27 +02004426 if (d_real_inode(old_dentry) == d_real_inode(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004427 return 0;
4428
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004429 error = may_delete(mnt, old_dir, old_dentry, is_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 if (error)
4431 return error;
4432
Miklos Szeredida1ce062014-04-01 17:08:43 +02004433 if (!target) {
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004434 error = may_create(mnt, new_dir, new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004435 } else {
4436 new_is_dir = d_is_dir(new_dentry);
4437
4438 if (!(flags & RENAME_EXCHANGE))
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004439 error = may_delete(mnt, new_dir, new_dentry, is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004440 else
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004441 error = may_delete(mnt, new_dir, new_dentry, new_is_dir);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 if (error)
4444 return error;
4445
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004446 if (!old_dir->i_op->rename)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 return -EPERM;
4448
Miklos Szeredibc270272014-04-01 17:08:42 +02004449 /*
4450 * If we are going to change the parent - check write permissions,
4451 * we'll need to flip '..'.
4452 */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004453 if (new_dir != old_dir) {
4454 if (is_dir) {
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004455 error = inode_permission2(mnt, source, MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004456 if (error)
4457 return error;
4458 }
4459 if ((flags & RENAME_EXCHANGE) && new_is_dir) {
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004460 error = inode_permission2(mnt, target, MAY_WRITE);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004461 if (error)
4462 return error;
4463 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004464 }
Robert Love0eeca282005-07-12 17:06:03 -04004465
Miklos Szeredi0b3974e2014-04-01 17:08:43 +02004466 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry,
4467 flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004468 if (error)
4469 return error;
4470
Al Viroad25f112017-07-07 14:51:19 -04004471 take_dentry_name_snapshot(&old_name, old_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004472 dget(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004473 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004474 lock_two_nondirectories(source, target);
4475 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004476 inode_lock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004477
4478 error = -EBUSY;
Eric W. Biederman7af13642013-10-04 19:15:13 -07004479 if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry))
Miklos Szeredibc270272014-04-01 17:08:42 +02004480 goto out;
4481
Miklos Szeredida1ce062014-04-01 17:08:43 +02004482 if (max_links && new_dir != old_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004483 error = -EMLINK;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004484 if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links)
Miklos Szeredibc270272014-04-01 17:08:42 +02004485 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004486 if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir &&
4487 old_dir->i_nlink >= max_links)
4488 goto out;
4489 }
4490 if (is_dir && !(flags & RENAME_EXCHANGE) && target)
4491 shrink_dcache_parent(new_dentry);
4492 if (!is_dir) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004493 error = try_break_deleg(source, delegated_inode);
4494 if (error)
4495 goto out;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004496 }
4497 if (target && !new_is_dir) {
4498 error = try_break_deleg(target, delegated_inode);
4499 if (error)
4500 goto out;
Miklos Szeredibc270272014-04-01 17:08:42 +02004501 }
Miklos Szeredi2773bf02016-09-27 11:03:58 +02004502 error = old_dir->i_op->rename(old_dir, old_dentry,
Miklos Szeredi18fc84d2016-09-27 11:03:58 +02004503 new_dir, new_dentry, flags);
Miklos Szeredibc270272014-04-01 17:08:42 +02004504 if (error)
4505 goto out;
4506
Miklos Szeredida1ce062014-04-01 17:08:43 +02004507 if (!(flags & RENAME_EXCHANGE) && target) {
Miklos Szeredibc270272014-04-01 17:08:42 +02004508 if (is_dir)
4509 target->i_flags |= S_DEAD;
4510 dont_mount(new_dentry);
Eric W. Biederman8ed936b2013-10-01 18:33:48 -07004511 detach_mounts(new_dentry);
Miklos Szeredibc270272014-04-01 17:08:42 +02004512 }
Miklos Szeredida1ce062014-04-01 17:08:43 +02004513 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) {
4514 if (!(flags & RENAME_EXCHANGE))
4515 d_move(old_dentry, new_dentry);
4516 else
4517 d_exchange(old_dentry, new_dentry);
4518 }
Miklos Szeredibc270272014-04-01 17:08:42 +02004519out:
Miklos Szeredida1ce062014-04-01 17:08:43 +02004520 if (!is_dir || (flags & RENAME_EXCHANGE))
Miklos Szeredibc270272014-04-01 17:08:42 +02004521 unlock_two_nondirectories(source, target);
4522 else if (target)
Al Viro59551022016-01-22 15:40:57 -05004523 inode_unlock(target);
Miklos Szeredibc270272014-04-01 17:08:42 +02004524 dput(new_dentry);
Miklos Szeredida1ce062014-04-01 17:08:43 +02004525 if (!error) {
Al Viroad25f112017-07-07 14:51:19 -04004526 fsnotify_move(old_dir, new_dir, old_name.name, is_dir,
Miklos Szeredida1ce062014-04-01 17:08:43 +02004527 !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry);
4528 if (flags & RENAME_EXCHANGE) {
4529 fsnotify_move(new_dir, old_dir, old_dentry->d_name.name,
4530 new_is_dir, NULL, new_dentry);
4531 }
4532 }
Al Viroad25f112017-07-07 14:51:19 -04004533 release_dentry_name_snapshot(&old_name);
Robert Love0eeca282005-07-12 17:06:03 -04004534
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 return error;
4536}
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004537EXPORT_SYMBOL(vfs_rename2);
4538
4539int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
4540 struct inode *new_dir, struct dentry *new_dentry,
4541 struct inode **delegated_inode, unsigned int flags)
4542{
4543 return vfs_rename2(NULL, old_dir, old_dentry, new_dir, new_dentry, delegated_inode, flags);
4544}
Al Viro4d359502014-03-14 12:20:17 -04004545EXPORT_SYMBOL(vfs_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004547SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname,
4548 int, newdfd, const char __user *, newname, unsigned int, flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549{
Al Viro2ad94ae2008-07-21 09:32:51 -04004550 struct dentry *old_dentry, *new_dentry;
4551 struct dentry *trap;
Al Virof5beed72015-04-30 16:09:11 -04004552 struct path old_path, new_path;
4553 struct qstr old_last, new_last;
4554 int old_type, new_type;
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004555 struct inode *delegated_inode = NULL;
Jeff Layton91a27b22012-10-10 15:25:28 -04004556 struct filename *from;
4557 struct filename *to;
Al Virof5beed72015-04-30 16:09:11 -04004558 unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET;
Jeff Laytonc6a94282012-12-11 12:10:10 -05004559 bool should_retry = false;
Al Viro2ad94ae2008-07-21 09:32:51 -04004560 int error;
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004561
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004562 if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
Miklos Szeredida1ce062014-04-01 17:08:43 +02004563 return -EINVAL;
4564
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004565 if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) &&
4566 (flags & RENAME_EXCHANGE))
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004567 return -EINVAL;
4568
Miklos Szeredi0d7a8552014-10-24 00:14:37 +02004569 if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD))
4570 return -EPERM;
4571
Al Virof5beed72015-04-30 16:09:11 -04004572 if (flags & RENAME_EXCHANGE)
4573 target_flags = 0;
4574
Jeff Laytonc6a94282012-12-11 12:10:10 -05004575retry:
Al Virof5beed72015-04-30 16:09:11 -04004576 from = user_path_parent(olddfd, oldname,
4577 &old_path, &old_last, &old_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004578 if (IS_ERR(from)) {
4579 error = PTR_ERR(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 goto exit;
Jeff Layton91a27b22012-10-10 15:25:28 -04004581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582
Al Virof5beed72015-04-30 16:09:11 -04004583 to = user_path_parent(newdfd, newname,
4584 &new_path, &new_last, &new_type, lookup_flags);
Jeff Layton91a27b22012-10-10 15:25:28 -04004585 if (IS_ERR(to)) {
4586 error = PTR_ERR(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 goto exit1;
Jeff Layton91a27b22012-10-10 15:25:28 -04004588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
4590 error = -EXDEV;
Al Virof5beed72015-04-30 16:09:11 -04004591 if (old_path.mnt != new_path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 goto exit2;
4593
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 error = -EBUSY;
Al Virof5beed72015-04-30 16:09:11 -04004595 if (old_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 goto exit2;
4597
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004598 if (flags & RENAME_NOREPLACE)
4599 error = -EEXIST;
Al Virof5beed72015-04-30 16:09:11 -04004600 if (new_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 goto exit2;
4602
Al Virof5beed72015-04-30 16:09:11 -04004603 error = mnt_want_write(old_path.mnt);
Jan Karac30dabf2012-06-12 16:20:30 +02004604 if (error)
4605 goto exit2;
4606
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004607retry_deleg:
Al Virof5beed72015-04-30 16:09:11 -04004608 trap = lock_rename(new_path.dentry, old_path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609
Al Virof5beed72015-04-30 16:09:11 -04004610 old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 error = PTR_ERR(old_dentry);
4612 if (IS_ERR(old_dentry))
4613 goto exit3;
4614 /* source must exist */
4615 error = -ENOENT;
David Howellsb18825a2013-09-12 19:22:53 +01004616 if (d_is_negative(old_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 goto exit4;
Al Virof5beed72015-04-30 16:09:11 -04004618 new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 error = PTR_ERR(new_dentry);
4620 if (IS_ERR(new_dentry))
4621 goto exit4;
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004622 error = -EEXIST;
4623 if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry))
4624 goto exit5;
Miklos Szeredida1ce062014-04-01 17:08:43 +02004625 if (flags & RENAME_EXCHANGE) {
4626 error = -ENOENT;
4627 if (d_is_negative(new_dentry))
4628 goto exit5;
4629
4630 if (!d_is_dir(new_dentry)) {
4631 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004632 if (new_last.name[new_last.len])
Miklos Szeredida1ce062014-04-01 17:08:43 +02004633 goto exit5;
4634 }
4635 }
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004636 /* unless the source is a directory trailing slashes give -ENOTDIR */
4637 if (!d_is_dir(old_dentry)) {
4638 error = -ENOTDIR;
Al Virof5beed72015-04-30 16:09:11 -04004639 if (old_last.name[old_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004640 goto exit5;
Al Virof5beed72015-04-30 16:09:11 -04004641 if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len])
Miklos Szeredi0a7c3932014-04-01 17:08:43 +02004642 goto exit5;
4643 }
4644 /* source should not be ancestor of target */
4645 error = -EINVAL;
4646 if (old_dentry == trap)
4647 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 /* target should not be an ancestor of source */
Miklos Szeredida1ce062014-04-01 17:08:43 +02004649 if (!(flags & RENAME_EXCHANGE))
4650 error = -ENOTEMPTY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 if (new_dentry == trap)
4652 goto exit5;
4653
Al Virof5beed72015-04-30 16:09:11 -04004654 error = security_path_rename(&old_path, old_dentry,
4655 &new_path, new_dentry, flags);
Kentaro Takedabe6d3e52008-12-17 13:24:15 +09004656 if (error)
Jan Karac30dabf2012-06-12 16:20:30 +02004657 goto exit5;
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004658 error = vfs_rename2(old_path.mnt, old_path.dentry->d_inode, old_dentry,
Al Virof5beed72015-04-30 16:09:11 -04004659 new_path.dentry->d_inode, new_dentry,
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004660 &delegated_inode, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661exit5:
4662 dput(new_dentry);
4663exit4:
4664 dput(old_dentry);
4665exit3:
Al Virof5beed72015-04-30 16:09:11 -04004666 unlock_rename(new_path.dentry, old_path.dentry);
J. Bruce Fields8e6d7822011-09-20 16:59:58 -04004667 if (delegated_inode) {
4668 error = break_deleg_wait(&delegated_inode);
4669 if (!error)
4670 goto retry_deleg;
4671 }
Al Virof5beed72015-04-30 16:09:11 -04004672 mnt_drop_write(old_path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673exit2:
Jeff Laytonc6a94282012-12-11 12:10:10 -05004674 if (retry_estale(error, lookup_flags))
4675 should_retry = true;
Al Virof5beed72015-04-30 16:09:11 -04004676 path_put(&new_path);
Al Viro2ad94ae2008-07-21 09:32:51 -04004677 putname(to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678exit1:
Al Virof5beed72015-04-30 16:09:11 -04004679 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 putname(from);
Jeff Laytonc6a94282012-12-11 12:10:10 -05004681 if (should_retry) {
4682 should_retry = false;
4683 lookup_flags |= LOOKUP_REVAL;
4684 goto retry;
4685 }
Al Viro2ad94ae2008-07-21 09:32:51 -04004686exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 return error;
4688}
4689
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004690SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname,
4691 int, newdfd, const char __user *, newname)
4692{
4693 return sys_renameat2(olddfd, oldname, newdfd, newname, 0);
4694}
4695
Heiko Carstensa26eab22009-01-14 14:14:17 +01004696SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004697{
Miklos Szeredi520c8b12014-04-01 17:08:42 +02004698 return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08004699}
4700
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004701int vfs_whiteout(struct inode *dir, struct dentry *dentry)
4702{
Daniel Rosenbergbbcd0ff2016-10-26 16:27:45 -07004703 int error = may_create(NULL, dir, dentry);
Miklos Szeredi787fb6b2014-10-24 00:14:36 +02004704 if (error)
4705 return error;
4706
4707 if (!dir->i_op->mknod)
4708 return -EPERM;
4709
4710 return dir->i_op->mknod(dir, dentry,
4711 S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV);
4712}
4713EXPORT_SYMBOL(vfs_whiteout);
4714
Al Viro5d826c82014-03-14 13:42:45 -04004715int readlink_copy(char __user *buffer, int buflen, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Al Viro5d826c82014-03-14 13:42:45 -04004717 int len = PTR_ERR(link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 if (IS_ERR(link))
4719 goto out;
4720
4721 len = strlen(link);
4722 if (len > (unsigned) buflen)
4723 len = buflen;
4724 if (copy_to_user(buffer, link, len))
4725 len = -EFAULT;
4726out:
4727 return len;
4728}
4729
4730/*
4731 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
Al Viro6b255392015-11-17 10:20:54 -05004732 * have ->get_link() not calling nd_jump_link(). Using (or not using) it
4733 * for any given inode is up to filesystem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 */
4735int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4736{
Al Virofceef392015-12-29 15:58:39 -05004737 DEFINE_DELAYED_CALL(done);
Al Viro5f2c4172015-05-07 11:14:26 -04004738 struct inode *inode = d_inode(dentry);
4739 const char *link = inode->i_link;
Marcin Slusarz694a1762008-06-09 16:40:37 -07004740 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07004741
Al Virod4dee482015-04-30 20:08:02 -04004742 if (!link) {
Al Virofceef392015-12-29 15:58:39 -05004743 link = inode->i_op->get_link(dentry, inode, &done);
Al Virod4dee482015-04-30 20:08:02 -04004744 if (IS_ERR(link))
4745 return PTR_ERR(link);
4746 }
Al Viro680baac2015-05-02 13:32:22 -04004747 res = readlink_copy(buffer, buflen, link);
Al Virofceef392015-12-29 15:58:39 -05004748 do_delayed_call(&done);
Marcin Slusarz694a1762008-06-09 16:40:37 -07004749 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750}
Al Viro4d359502014-03-14 12:20:17 -04004751EXPORT_SYMBOL(generic_readlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752
Miklos Szeredid60874c2016-10-04 14:40:45 +02004753/**
4754 * vfs_get_link - get symlink body
4755 * @dentry: dentry on which to get symbolic link
4756 * @done: caller needs to free returned data with this
4757 *
4758 * Calls security hook and i_op->get_link() on the supplied inode.
4759 *
4760 * It does not touch atime. That's up to the caller if necessary.
4761 *
4762 * Does not work on "special" symlinks like /proc/$$/fd/N
4763 */
4764const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done)
4765{
4766 const char *res = ERR_PTR(-EINVAL);
4767 struct inode *inode = d_inode(dentry);
4768
4769 if (d_is_symlink(dentry)) {
4770 res = ERR_PTR(security_inode_readlink(dentry));
4771 if (!res)
4772 res = inode->i_op->get_link(dentry, inode, done);
4773 }
4774 return res;
4775}
4776EXPORT_SYMBOL(vfs_get_link);
4777
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778/* get the link contents into pagecache */
Al Viro6b255392015-11-17 10:20:54 -05004779const char *page_get_link(struct dentry *dentry, struct inode *inode,
Al Virofceef392015-12-29 15:58:39 -05004780 struct delayed_call *callback)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781{
Duane Griffinebd09ab2008-12-19 20:47:12 +00004782 char *kaddr;
4783 struct page *page;
Al Viro6b255392015-11-17 10:20:54 -05004784 struct address_space *mapping = inode->i_mapping;
4785
Al Virod3883d42015-11-17 10:41:04 -05004786 if (!dentry) {
4787 page = find_get_page(mapping, 0);
4788 if (!page)
4789 return ERR_PTR(-ECHILD);
4790 if (!PageUptodate(page)) {
4791 put_page(page);
4792 return ERR_PTR(-ECHILD);
4793 }
4794 } else {
4795 page = read_mapping_page(mapping, 0, NULL);
4796 if (IS_ERR(page))
4797 return (char*)page;
4798 }
Al Virofceef392015-12-29 15:58:39 -05004799 set_delayed_call(callback, page_put_link, page);
Al Viro21fc61c2015-11-17 01:07:57 -05004800 BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM);
4801 kaddr = page_address(page);
Al Viro6b255392015-11-17 10:20:54 -05004802 nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1);
Duane Griffinebd09ab2008-12-19 20:47:12 +00004803 return kaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804}
4805
Al Viro6b255392015-11-17 10:20:54 -05004806EXPORT_SYMBOL(page_get_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Al Virofceef392015-12-29 15:58:39 -05004808void page_put_link(void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Al Virofceef392015-12-29 15:58:39 -05004810 put_page(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811}
Al Viro4d359502014-03-14 12:20:17 -04004812EXPORT_SYMBOL(page_put_link);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813
Al Viroaa80dea2015-11-16 18:26:34 -05004814int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
4815{
Al Virofceef392015-12-29 15:58:39 -05004816 DEFINE_DELAYED_CALL(done);
Al Viro6b255392015-11-17 10:20:54 -05004817 int res = readlink_copy(buffer, buflen,
4818 page_get_link(dentry, d_inode(dentry),
Al Virofceef392015-12-29 15:58:39 -05004819 &done));
4820 do_delayed_call(&done);
Al Viroaa80dea2015-11-16 18:26:34 -05004821 return res;
4822}
4823EXPORT_SYMBOL(page_readlink);
4824
Nick Piggin54566b22009-01-04 12:00:53 -08004825/*
4826 * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS
4827 */
4828int __page_symlink(struct inode *inode, const char *symname, int len, int nofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
4830 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004831 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07004832 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08004833 int err;
Nick Piggin54566b22009-01-04 12:00:53 -08004834 unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE;
4835 if (nofs)
4836 flags |= AOP_FLAG_NOFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837
NeilBrown7e53cac2006-03-25 03:07:57 -08004838retry:
Nick Pigginafddba42007-10-16 01:25:01 -07004839 err = pagecache_write_begin(NULL, mapping, 0, len-1,
Nick Piggin54566b22009-01-04 12:00:53 -08004840 flags, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07004842 goto fail;
4843
Al Viro21fc61c2015-11-17 01:07:57 -05004844 memcpy(page_address(page), symname, len-1);
Nick Pigginafddba42007-10-16 01:25:01 -07004845
4846 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
4847 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 if (err < 0)
4849 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07004850 if (err < len-1)
4851 goto retry;
4852
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 mark_inode_dirty(inode);
4854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855fail:
4856 return err;
4857}
Al Viro4d359502014-03-14 12:20:17 -04004858EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004860int page_symlink(struct inode *inode, const char *symname, int len)
4861{
4862 return __page_symlink(inode, symname, len,
Michal Hockoc62d2552015-11-06 16:28:49 -08004863 !mapping_gfp_constraint(inode->i_mapping, __GFP_FS));
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004864}
Al Viro4d359502014-03-14 12:20:17 -04004865EXPORT_SYMBOL(page_symlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08004866
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08004867const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 .readlink = generic_readlink,
Al Viro6b255392015-11-17 10:20:54 -05004869 .get_link = page_get_link,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870};
Linus Torvalds1da177e2005-04-16 15:20:36 -07004871EXPORT_SYMBOL(page_symlink_inode_operations);